CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU...

27
CS 380 - GPU and GPGPU Programming CS 380 GPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUST Markus Hadwiger, KAUST

Transcript of CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU...

Page 1: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

CS 380 - GPU and GPGPU ProgrammingCS 380 GPU and GPGPU ProgrammingLecture 10: GPU Texturing 1

Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Page 2: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Reading Assignment #5 (until Mar 21)Reading Assignment #5 (until Mar. 21)

Read (required):• GLSL book, chapter 6 (Simple Shading Example)

GLSL b k h t 8 1 8 3 (Sh d D l t)• GLSL book, chapter 8.1-8.3 (Shader Development)

R d ( ti l)Read (optional):• GLSL book, chapter 7 (OpenGL Shading Language API)

2

Page 3: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Programming Assignment #2 (until Apr 4)Programming Assignment #2 (until Apr. 4)

Shading example• Phong shading, procedural shading, sphere tessellation

O GL/GLSL t t f t h d• OpenGL/GLSL vertex, geometry, fragment shaders

D l d f kDownload framework:http://faculty.kaust.edu.sa/sites/markushadwiger/Documents/...

/CS380 prog2 Windows zip for Windows... /CS380_prog2_Windows.zip for Windows

... /CS380_prog2_Linux_MacOS.tgz for Linux and MacOS X

Windows: solution/project file for Visual Studio 2008 (easy to do your own)

Linux/MacOS: Makefile. Look at it and edit as needed!

3

Page 4: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Programming Assignment #2 (until Apr 4)Programming Assignment #2 (until Apr. 4)

4

Page 5: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Programming Assignment #2 (until Apr 4)Programming Assignment #2 (until Apr. 4)

5

Page 6: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Programming Assignment #2 (until Apr 4)Programming Assignment #2 (until Apr. 4)

6

Page 7: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Semester Project (Proposal until Apr 4!)Semester Project (Proposal until Apr. 4!)

• Try to find your own topic of interestTry to find your own topic of interest• Pick something that you think is really cool

• Can be completely graphics or completely computation or both combinedCan be completely graphics or completely computation or both combined

• Browse GPU Gems 2, GPU Gems 3 books (in library!) for ideas, browse SDK examples, look online

• Can be built on NVIDIA OpenGL SDK or NVIDIA CUDA SDK

• Amount of work ~all four programming assignments together

• Write project proposal• 1-2 pages (pdf), just overview of plan

• Talk to us before you start writing! (before spring break!)(regarding content and complexity)

• Hand in proposal after spring break (Apr 4)

7

• Hand in proposal after spring break (Apr. 4)

• Project presentations May 22-25, before that write report

Page 8: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

GPU TexturingGPU Texturing

8

Rage / id Tech 5 (id Software)

Page 9: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Before We Start: Shading

Flat shadingcompute light interaction per polygonp g p p ygthe whole polygon has the same color

Gouraud shadinggcompute light interaction per vertexinterpolate the colorsinterpolate the colors

Phong shadinginterpolate normals per pixelinterpolate normals per pixel

Remember: difference betweenPhong Lighting ModelPhong Lighting ModelPhong Shading

Vienna University of Technology 9

Page 10: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Traditional OpenGL Lighting

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

f GFixed function: Gouraud shadingNote: need to interpolate specular separately!

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

Vienna University of Technology 10

Page 11: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Why Texturing?

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

Eduard Gröller, Stefan Jeschke 11

Page 12: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

OpenGL Texture Mapping

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

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

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

Vienna University of Technology 12

Page 13: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Types of Textures

Spatial layoutCartesian grids: 1D, 2D, 3D, 2D ARRAY, …g , , , _ ,Cube maps, …

Formats (too many), e.g. OpenGLGL LUMINANCE16 ALPHA16GL_LUMINANCE16_ALPHA16GL_RGB8, GL_RGBA8, …: integer texture formatsGL RGB16F GL RGBA32F : float texture formatsGL_RGB16F, GL_RGBA32F, …: float texture formatscompressed formats, high dynamic range formats, …

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

Vienna University of Technology 13

OpenGL driver converts from external to internal

Page 14: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texturing: General Approach

Texels

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

Parametrization Rendering(Projection etc.)

Eduard Gröller, Stefan Jeschke 14

Page 15: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Projectors

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

Vienna University of Technology 15

Page 16: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture ProjectorsWhere do texture coordinates come from?

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

Vienna University of Technology 16

Page 17: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Wrap Mode

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

repeat mirror/repeat clamp border

Vienna University of Technology 17

Page 18: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Reconstruction: Magnification

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

Weight adjacent texels by distance to pixel position

-v

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

X

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

d (1 d ) T( 1 )d

dv( , )

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

du

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

Eduard Gröller, Stefan Jeschke 18Texture space u

Page 19: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Magnification (Bilinear Filtering Example)

Original imageg g

Eduard Gröller, Stefan Jeschke 19Nearest neighbor Bilinear filtering

Page 20: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Aliasing: Minification

Problem: One pixel in image space covers many texels

Eduard Gröller, Stefan Jeschke 20

Page 21: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Aliasing: Minification

Caused by undersampling: texture information is lost

Texture spacep

Eduard Gröller, Stefan Jeschke 21

Image space

Page 22: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Anti-Aliasing: Minification

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

vPixel

XXX

Texture space u Image space

Eduard Gröller, Stefan Jeschke 22

Page 23: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Anti-Aliasing: MIP Mapping

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

Eduard Gröller, Stefan Jeschke 23

Page 24: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Anti-Aliasing: MIP Mapping

MIP Mapping AlgorithmD := ld(max(d d )) d

d2"Mip Map level"D := ld(max(d1,d2))

T0 := value from texture D0= trunc (D)U bili i t l ti

d1X

p p

Use bilinear interpolation

Eduard Gröller, Stefan Jeschke 24Bilinear interpolation Trilinear interpolation

Page 25: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Anti-Aliasing: MIP Mapping

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

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

Eduard Gröller, Stefan Jeschke 25

Page 26: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Texture Anti-Aliasing: MIP Mapping

Other example for bilinear vs. trilinear filtering

Eduard Gröller, Stefan Jeschke 26

Page 27: CS 380CS 380 - subdude-site.com · CS 380CS 380 - GPU and GPGPU ProgrammingGPU and GPGPU Programming Lecture 10: GPU Texturing 1 Markus Hadwiger, KAUSTMarkus Hadwiger, KAUST

Thank youThank you.

Thanks for slides and imagesThanks for slides and images• Michael Wimmer, Stefan Jeschke, Meister Eduard Gröller, TU Vienna