Presentation is loading. Please wait.

Presentation is loading. Please wait.

GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads.

Similar presentations


Presentation on theme: "GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads."— Presentation transcript:

1 GPU Programming Robert Hero rghero@soe.ucsc.edu

2 Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads or other polygons are broken down into triangles Quads or other polygons are broken down into triangles Each triangle processed in two steps- Each triangle processed in two steps- Vertex operations Vertex operations Pixel operations Pixel operations

3 Transformation and Lighting Each vertex is handled separately Each vertex is handled separately First the vertex is transformed into screen coordinates First the vertex is transformed into screen coordinates Next lighting for each vertex is calculated Next lighting for each vertex is calculated Only ambient, diffuse, and specular properties of the vertex are calculated Only ambient, diffuse, and specular properties of the vertex are calculated

4 Pixel Rasterization Each pixel in the triangle is compared to the depth buffer Each pixel in the triangle is compared to the depth buffer If the depth test passes, the texture for the pixel is looked up If the depth test passes, the texture for the pixel is looked up The texture value, along with the color of the pixel is blended together The texture value, along with the color of the pixel is blended together Gouraud Shading is used for pixel color Gouraud Shading is used for pixel color Possibly with previous color of the pixel Possibly with previous color of the pixel

5 Gouraud Shading isn’t that good Should be a nice circular pattern

6 We want better (most of the time) Possible solution- put all lighting calculations in the pixel step Possible solution- put all lighting calculations in the pixel step Expensive to compute Expensive to compute Not always important Not always important Better solution- Programmable Shaders Better solution- Programmable Shaders Only perform expensive calculations for objects that really need it Only perform expensive calculations for objects that really need it Allows programmers to come up with their own effects Allows programmers to come up with their own effects

7 Pipeline Fixed T&L Vertex ShaderPixel Shader Tri Result Image Position, Lighting, Texturing More Lighting, Blending, more Texturing

8 Take control of the GPU Two new types of processors to control Two new types of processors to control Vertex shaders Vertex shaders Pixel (or Fragment) shaders Pixel (or Fragment) shaders Huge amount of power given to programmer Huge amount of power given to programmer Vertices can be manipulated before they are transformed to screen coordinates Vertices can be manipulated before they are transformed to screen coordinates Lighting can now be done on a per pixel basis Lighting can now be done on a per pixel basis We can even do pure number crunching on the processor – No graphics needed We can even do pure number crunching on the processor – No graphics needed

9 Why not do this on the CPU? Graphics cards have more floating point power than any CPU on the market Graphics cards have more floating point power than any CPU on the market Specialized hardware allows for highly optimized calculations Specialized hardware allows for highly optimized calculations Did I mention Parallel processing? Did I mention Parallel processing? (A NVIDIA 7800GTX has 24 pixel shaders) CPU’s aren’t increasing in speed like GPUs are CPU’s aren’t increasing in speed like GPUs are

10 So how do we do it? Learn a new language or new tool Learn a new language or new tool Assembly Assembly GLSL (OpenGL) GLSL (OpenGL) HLSL (DirectX) HLSL (DirectX) Cg Cg ATI RenderMonkey ATI RenderMonkey NVIDIA FX Composer NVIDIA FX Composer …. ….

11

12 Its not that bad, really… HLSL (High Level Shading Language) and GLSL (GL Shading Language) are very similar HLSL (High Level Shading Language) and GLSL (GL Shading Language) are very similar Very similar to C++ Very similar to C++ Created to replace the need to learn assembly for each graphics card on the market Created to replace the need to learn assembly for each graphics card on the market Simple to use and learn (assuming you get a good book) Simple to use and learn (assuming you get a good book) Most commands you will use are mul, add, dot, sub, and texture lookup. Most commands you will use are mul, add, dot, sub, and texture lookup.

13 vertexOutput VS_TransformAndTexture(vertexInput IN) vertexOutput VS_TransformAndTexture(vertexInput IN) { vertexOutput OUT; vertexOutput OUT; OUT.hPosition = mul( float4(IN.position.xyz, 1.0), worldViewProj); OUT.hPosition = mul( float4(IN.position.xyz, 1.0), worldViewProj); OUT.texCoordDiffuse = IN.texCoordDiffuse; OUT.texCoordDiffuse = IN.texCoordDiffuse; //calculate our vectors N, E, L, and H //calculate our vectors N, E, L, and H float3 worldEyePos = viewInverse[3].xyz; float3 worldEyePos = viewInverse[3].xyz; float3 worldVertPos = mul(IN.position, world).xyz; float3 worldVertPos = mul(IN.position, world).xyz; float4 N = mul(IN.normal, worldInverseTranspose); //normal vector float4 N = mul(IN.normal, worldInverseTranspose); //normal vector float3 E = normalize(worldEyePos - worldVertPos); //eye vector float3 E = normalize(worldEyePos - worldVertPos); //eye vector float3 L = normalize( -lightDir.xyz); //light vector float3 L = normalize( -lightDir.xyz); //light vector float3 H = normalize(E + L); //half angle vector float3 H = normalize(E + L); //half angle vector //calculate the diffuse and specular contributions //calculate the diffuse and specular contributions float diff = max(0, dot(N,L)); float diff = max(0, dot(N,L)); float spec = pow( max(0, dot(N,H) ), shininess ); float spec = pow( max(0, dot(N,H) ), shininess ); if( diff <= 0 ) if( diff <= 0 ) { { spec = 0; spec = 0; } } //output diffuse //output diffuse float4 ambColor = materialDiffuse * lightAmbient; float4 ambColor = materialDiffuse * lightAmbient; float4 diffColor = materialDiffuse * diff * lightColor ; float4 diffColor = materialDiffuse * diff * lightColor ; OUT.diffAmbColor = diffColor + ambColor; OUT.diffAmbColor = diffColor + ambColor; //output specular //output specular float4 specColor = materialSpecular * lightColor * spec; float4 specColor = materialSpecular * lightColor * spec; OUT.specCol = specColor; OUT.specCol = specColor; return OUT; return OUT; }

14

15

16

17 Difference… float4 PS_Textured( vertexOutput IN): COLOR { float4 diffuseTexture = tex2D(TextureSampler, IN.texCoord0Diffuse ); float4 diffuseTexture = tex2D(TextureSampler, IN.texCoord0Diffuse ); float4 diffuse2Texture = tex2D( TextureSampler2, IN.texCoord1Diffuse ); float4 diffuse2Texture = tex2D( TextureSampler2, IN.texCoord1Diffuse ); return IN.diffAmbColor * diffuseTexture + IN.specCol; return IN.diffAmbColor * diffuseTexture + IN.specCol;} float4 PS_Textured( vertexOutput IN): COLOR { float4 diffuseTexture = tex2D( TextureSampler, IN.texCoord0Diffuse ); float3 normTexture = (tex2D( TextureSampler2, IN.texCoord1Diffuse ).xyz - 0.5)*2.0; float4 N = mul(normTexture, worldInverseTranspose); float3 L = normalize( -lightDir.xyz); //light vector float diff = max(0, dot(N,L)); return diffuseTexture * diff; }

18 General Requirements for writing shaders Hardware is optimized for graphics Hardware is optimized for graphics This means you can’t create your own datatypes This means you can’t create your own datatypes Focused on vectors and matrices Focused on vectors and matrices Vertex and Pixel Shaders have limited input and outputs Vertex and Pixel Shaders have limited input and outputs Shaders have no knowledge of what pixel or vertex they are processing Shaders have no knowledge of what pixel or vertex they are processing Tricks must be used Tricks must be used Ie. Encode additional position information in color channels Ie. Encode additional position information in color channels Set texture coordinates to give information about which pixel is being processed Set texture coordinates to give information about which pixel is being processed Don’t use if statements unless you have a really new card… Don’t use if statements unless you have a really new card…

19 Cont… Shaders can only be so many lines of code (at least until DirectX10) Shaders can only be so many lines of code (at least until DirectX10) Most newer graphics card have limits around 32000 lines of code Most newer graphics card have limits around 32000 lines of code There are different versions with different features There are different versions with different features For instance if statements don’t exist in Shader Model 1.0 For instance if statements don’t exist in Shader Model 1.0 Allows the programmer to write effects for many types of graphics cards (FX files) Allows the programmer to write effects for many types of graphics cards (FX files)

20 Vertex Shaders Input Input Position Position Normal Normal Color (Ambient,Diffuse,Specular) Color (Ambient,Diffuse,Specular) Texture Coordinates Texture Coordinates Output Output Position Position Color Color Texture Coordinates Texture Coordinates New features that aren’t available with the fixed pipeline New features that aren’t available with the fixed pipeline Move vertices (Bump mapping, hair,… ) Move vertices (Bump mapping, hair,… ) Texture lookup (Get neighbor information… ) Texture lookup (Get neighbor information… ) If statements (Not the best idea here, but can be ok) If statements (Not the best idea here, but can be ok)

21 Pixel Shaders Input Input Color information Color information Texture Coordinates Texture Coordinates Position Information Position Information Output Output Final Color Final Color Depth Value Depth Value This is it! This is it! Changes from fixed pipeline Changes from fixed pipeline Dependant Texture Lookup (Use a texture to lookup into another texture) Dependant Texture Lookup (Use a texture to lookup into another texture) If statements (Really bad idea!) If statements (Really bad idea!) Ability to do lighting per pixel Ability to do lighting per pixel

22

23 What if you don’t want to make an Image? (General Purpose GPU programming) Encode all your data in a texture map Encode all your data in a texture map Write your program in a pixel shader Write your program in a pixel shader Do a texture lookup to get data and “render” the result to the image buffer Do a texture lookup to get data and “render” the result to the image buffer Instead of displaying the image buffer read it back out and you’re done Instead of displaying the image buffer read it back out and you’re done Or if you need more processing use the results as a new texture and process again Or if you need more processing use the results as a new texture and process again

24

25 General Tips Texture maps are the keys! Texture maps are the keys! You can store 4 different values per texel- who says they have to be an image You can store 4 different values per texel- who says they have to be an image Be careful – texture maps are generally only 8 bits per channel, and values only range from 0-255 Be careful – texture maps are generally only 8 bits per channel, and values only range from 0-255 You can make texture maps up to 32bits per channel You can make texture maps up to 32bits per channel Values are always clamped 0..1 so make sure you scale your values Values are always clamped 0..1 so make sure you scale your values Use built-in functions where ever possible Use built-in functions where ever possible

26 Resources and Cool Things developer.nvidia.com developer.nvidia.com FX Composer FX Composer NVIDIA SDK (lots of code demos) NVIDIA SDK (lots of code demos) ATI SDK and RenderMonkey ATI SDK and RenderMonkey DirectX9 SDK (An absolute must for programming GPUs) DirectX9 SDK (An absolute must for programming GPUs) GPU Gems books GPU Gems books OpenGL.org OpenGL.org OpenGL Orange Book OpenGL Orange Book Introduction to 3D Game Programming with DirectX 9.0 by Frank Luna Introduction to 3D Game Programming with DirectX 9.0 by Frank Luna

27 Thank you and Questions?


Download ppt "GPU Programming Robert Hero Quick Overview (The Old Way) Graphics cards process Triangles Graphics cards process Triangles Quads."

Similar presentations


Ads by Google