DM2336 Programming hardware shaders Dioselin Gonzalez - 2007 S2 Week 2: Review of graphics API Programmable pipeline Background image by chromosphere.deviantart.com Fella in following slides by devart.deviantart.com DM2336 PHS – Dioselin Gonzalez – 2007 S2 OpenGL block diagram Generation Traversal Xformation Rasteriz. & tex. Display Display list Evaluator Application generation Application traversal Per-vertex operations & Primitive assembly Rasterization Perfragment operations Frame buffer Video display Texture memory Pixel operations Application & higher level graphics libraries OpenGL implementation 2 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Per-vertex operations • • • • Transform vertex coords. (modelview m.) Transform and renormalize normals Generate and transform texture coords. Lighting calculations Display list Per-vertex operations Evaluator Application generation Application traversal Rasterization & Primitive assembly Perfragment operations Frame buffer Video display Texture memory Pixel operations 3 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Primitive assembly • • • • • Flat shading Clipping Projection transformations Viewport and depth-range operations Culling Display list Per-vertex operations Evaluator Application generation Application traversal Rasterization & Primitive assembly Perfragment operations Frame buffer Video display Texture memory Pixel operations 4 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Rasterization • Convert each primitive into fragments • Fragment: “transient data structures” – position (x,y); depth; color; texture coordinates; coverage; … Display list Per-vertex operations Rasterization Evaluator Application generation Application traversal & Primitive assembly Perfragment operations Frame buffer Video display Texture memory Pixel operations 5 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Interpolation issues http://www.cse.ohio-state.edu/~hwshen/781/pipeline.ppt Background image by katosu.deviantart.com DM2336 PHS – Dioselin Gonzalez – 2007 S2 Linear Interpolation Issues • Linear interpolation takes place in screen space • Problems can happen when projecting – Texture coordinates – Colors – Positions are okay. • Cannot recover p’s color and texture coordinates from linear Interpolation Lerp(Cp1’,Cp2’) along L != Cp • Need perspective correct interpolation for unprojected values http://www.cse.ohio-state.edu/~hwshen/781/pipeline.ppt p1 P1’ p p P2’ L p2 7 DM2336 PHS – Dioselin Gonzalez – 2007 S2 More explanations • Equal spacing in screen (pixel) space is not the same as in texture space in perspective projection – Perspective foreshortening from Hill courtesy of H. Pfister http://www.cse.ohio-state.edu/~hwshen/781/pipeline.ppt 8 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Perspective-Correct Texture Coordinate Interpolation • Interpolate (tex_coord/w) over the polygon, then do perspective divide after interpolation • Compute at each vertex after perspective transformation s/w, t/w “Denominator” 1/w – “Numerators” – http://www.cse.ohio-state.edu/~hwshen/781/pipeline.ppt 9 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Perspective-Correct Texture Coordinate Interpolation • Linearly interpolate 1/w, s/w, and t/w across the polygon • At each pixel Perform perspective division of interpolated texture coordinates (s/w, t/w) by interpolated 1/w (i.e., numerator over denominator) to get (s, t) http://www.cse.ohio-state.edu/~hwshen/781/pipeline.ppt 10 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Perspective-Correct Interpolation • That fixed it! 11 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Back to the pipeline Background image by katosu.deviantart.com DM2336 PHS – Dioselin Gonzalez – 2007 S2 Rasterization • Convert each primitive into fragments • Fragment: “transient data structures” – position (x,y); depth; color; texture coordinates; coverage; … Display list Per-vertex operations Rasterization Evaluator Application generation Application traversal & Primitive assembly Perfragment operations Frame buffer Video display Texture memory Pixel operations 13 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Per fragment operations (I) • Generate texel for every fragment • Fog calculations • Coverage (antialiasing) values application Display list Per-vertex operations Evaluator Application generation Application traversal Rasterization & Primitive assembly Perfragment operations Frame buffer Video display Texture memory Pixel operations 14 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Per fragment operations (II) • • • • Scissoring Alpha test Stencil test Depth-buffer test • Blending • Dithering and logical operation • Masking Display list Per-vertex operations Evaluator Application generation Application traversal Rasterization & Primitive assembly Perfragment operations Frame buffer Video display Texture memory Pixel operations 15 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Hello world Pervertex Operations Perfragment Operations (I) 16 DM2336 PHS – Dioselin Gonzalez – 2007 S2 17 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Vertex shader • Is run once every time a vertex position is specified – glVertex, glDrawArrays, … • Must compute gl_Position • May compute gl_ClipVertex or gl_PointSize 18 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Hello world 19 DM2336 PHS – Dioselin Gonzalez – 2007 S2 20 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Fragment shader • Is run once for every fragment produced • Has access to the interpolated value for each varying variable – Color, normal, texture coordinates, arbitrary values • Output goes to the fixed pipeline – gl_FragColor – computed R, G, B, A for the fragment – gl_FragDepth – computed depth value for the fragment – gl_FragData[n] – arbitrary data per fragment, stored in multiple render targets 21 DM2336 PHS – Dioselin Gonzalez – 2007 S2 Playing with GLSL Demo Background image by katosu.deviantart.com