CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture...

27
CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU Texturing 1+2 Markus Hadwiger, KAUST

Transcript of CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture...

Page 1: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

CS 380 - GPU and GPGPU ProgrammingLecture 16+17: GPU Texturing 1+2

Markus Hadwiger, KAUST

Page 2: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

2

Reading Assignment #10 (until April 23)

Read (required):

• Brook for GPUs: Stream Computing on Graphics HardwareIan Buck et al.

http://graphics.stanford.edu/papers/brookgpu/

Read (optional):

• The Imagine Stream ProcessorUjval Kapasi et al.

http://cva.stanford.edu/publications/2002/imagine-overview-iccd/

• Merrimac: Supercomputing with StreamsBill Dally et al.

http://merrimac.stanford.edu/publications/sc2003_merrimac.html

Page 3: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

3

Quiz #2: April 23

Organization• First 30 min of lecture

• No material (book, notes, ...) allowed

Content of questions• Lectures (both actual lectures and slides)

• Reading assigments

• Programming assignments (algorithms, methods)

• Solve short practical examples

Page 4: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

4

GPU Texturing

Rage / id Tech 5 (id Software)

Page 5: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 5

Remember: Basic Shading

Flat shadingcompute light interaction per polygonthe whole polygon has the same color

Gouraud shadingcompute light interaction per vertexinterpolate the colors

Phong shadinginterpolate normals per pixel

Remember: difference betweenPhong Lighting ModelPhong Shading

Page 6: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 6

Traditional OpenGL Lighting

Phong lighting model at each vertex (glLight, …)Local model only (no shadows, radiosity, …)ambient + diffuse + specular (glMaterial!)

Fixed function: Gouraud shadingNote: need to interpolate specular separately!

Phong shading: evaluate Phong lighting model in fragment shader (per-fragment evaluation!)

Page 7: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

7

Why Texturing?

Idea: enhance visual appearance of surfaces by applying fine / high-resolution details

Vienna University of Technology

Page 8: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 8

OpenGL Texture Mapping

Basis for most real-time rendering effectsLook and feel of a surfaceDefinition:

A regularly sampled function that is mapped onto every fragment of a surfaceTraditionally an image, but…

Can hold arbitrary informationTextures become general data structuresSampled and interpreted by fragment programsCan render into textures important!

Page 9: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 9

Types of Textures

Spatial layoutCartesian grids: 1D, 2D, 3D, 2D_ARRAY, …Cube maps, …

Formats (too many), e.g. OpenGLGL_LUMINANCE16_ALPHA16GL_RGB8, GL_RGBA8, …: integer texture formatsGL_RGB16F, GL_RGBA32F, …: float texture formatscompressed formats, high dynamic range formats, …

External (CPU) format vs. internal (GPU) formatOpenGL driver converts from external to internal

Page 10: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Eduard Gröller, Stefan Jeschke 10

Texturing: General Approach

Texture space (u,v) Object space (xO,yO,zO) Image Space (xI,yI)

Parametrization Rendering(Projection etc.)

Texels

Page 11: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 11

Texture Projectors

Where do texture coordinates come from?Online: texture matrix/texcoord generationOffline: manually (or by modeling program)spherical cylindrical planar natural

Page 12: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 12

Texture ProjectorsWhere do texture coordinates come from?

Offline: manual UV coordinates by DCC programNote: a modeling problem!

Page 13: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 13

Texture Wrap Mode

How to extend texture beyond the border?Border and repeat/clamp modesArbitrary (s,t,…) [0,1] x [0,1] [0,255] x [0,255]

repeat mirror/repeat clamp border

Page 14: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

14

Texture Reconstruction: Magnification

Bilinear reconstruction for texture magnification (D<0)("upsampling")

Weight adjacent texels by distance to pixel position

Texture space u

-v

X

T(u+du,v+dv) = du·dv·T(u+1,v+1)+ du·(1–dv)·T(u+1,v)+ (1-du)·dv·T(u,v+1)+ (1-du)·(1-dv)·T(u,v)

du

dv(u,v) (u+1,v)

(u+1,v+1)(u,v+1)

Vienna University of Technology

Page 15: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

15

Magnification (Bilinear Filtering Example)

Original image

Nearest neighbor Bilinear filteringVienna University of Technology

Page 16: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

16

Texture Aliasing: Minification

Problem: One pixel in image space covers many texels

Vienna University of Technology

Page 17: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

17

Texture Aliasing: Minification

Caused by undersampling: texture information is lost

Texture space

Image spaceVienna University of Technology

Page 18: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

18

Texture Anti-Aliasing: Minification

A good pixel value is the weighted mean of the pixel area projected into texture space

Texture space u

v

Image space

Pixel

XX

Vienna University of Technology

Page 19: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

19

Texture Anti-Aliasing: MIP Mapping

MIP Mapping (“Multum In Parvo”)Texture size is reduced by factors of 2(downsampling = "many things in a small place")Simple (4 pixel average) and memory efficientLast image is only ONE texel

Vienna University of Technology

Page 20: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Eduard Gröller, Stefan Jeschke 20

Texture Anti-Aliasing: MIP Mapping

MIP Mapping AlgorithmD := ld(max(d1,d2))T0 := value from texture D0= trunc (D)

Use bilinear interpolation

d1

d2

Bilinear interpolation Trilinear interpolation

X

"Mip Map level"

Page 21: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

21

Texture Anti-Aliasing: MIP Mapping

Trilinear interpolation:T1 := value from texture D1 = D0+1 (bilin.interpolation)Pixel value := (D1–D)·T0 + (D–D0)·T1

Linear interpolation between successive MIP MapsAvoids "Mip banding" (but doubles texture lookups)

Vienna University of Technology

Page 22: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

22

Texture Anti-Aliasing: MIP Mapping

Other example for bilinear vs. trilinear filtering

Vienna University of Technology

Page 23: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

23

Anti-Aliasing: Anisotropic Filtering

Anisotropic filteringView-dependent filter kernelImplementation: summed area table, "RIP Mapping", footprint assembly, elliptical weighted average (EWA)

Texture space

Vienna University of Technology

Page 24: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

24

Anti-Aliasing: Anisotropic Filtering

Example

Vienna University of Technology

Page 25: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 25

Texture Anti-aliasing

Basically, everything done in hardwaregluBuild2DMipmaps()generates MIPmapsSet parameters in glTexParameter()

GL_LINEAR_MIPMAP_NEARESTGL_TEXTURE_MAG_FILTER

Anisotropic filtering is an extension:GL_EXT_texture_filter_anisotropic

Number of samples can be varied (4x,8x,16x)Vendor specific support and extensions

Page 26: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Vienna University of Technology 26

Texture Coordinates

Specified manually (glMultiTexCoord())Using classical OpenGL texture coordinate generation

Linear: from object or eye space vertex coordsSpecial texturing modes (env-maps)Can be further modified with texture matrix

E.g., to add texture animationCan use 3rd or 4th texture coordinate for projective texturing!

Shader allows complex texture lookups!

Page 27: CS 380 - GPU and GPGPU Programming Lecture 16+17: GPU ... · Using classical OpenGL texture coordinate generation Linear: from object or eye space vertex coords Special texturing

Thank you.