rasterization - lunds tekniska...
TRANSCRIPT
![Page 1: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/1.jpg)
Rasterization
Michael DoggettDepartment of Computer Science
Lund university
![Page 2: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/2.jpg)
© 2011 Michael Doggett
Pixel shader
Vertex shader
Today’s stage of the Graphics Pipeline
Rasterization
FrameBuffer
Z & Alpha
Programmable
Fixed Function
![Page 3: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/3.jpg)
© 2011 Michael Doggett
How to rasterize a triangle?
Edge functions
Vertex positioning
Traversal
Interpolation
![Page 4: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/4.jpg)
© 2011 Michael Doggett
Edge functions
Vertex positioning
Traversal
Interpolation
![Page 5: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/5.jpg)
© 2008 Tomas Akenine-Möller
Which pixel is inside a triangle?
• Triangle traversal
Clearly, this pixelbelongs to the triangle
Clearly, this pixel does NOTbelong to the triangle
![Page 6: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/6.jpg)
© 2008 Tomas Akenine-Möller
Which pixel is inside a triangle?
• Sample at the center
How about this case? And this? And this? And this?
![Page 7: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/7.jpg)
© 2008 Tomas Akenine-Möller
How are we computing pixel center?
Screen space coordinates
(px,py) are in [0,w] x [0,h]
![Page 8: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/8.jpg)
© 2008 Tomas Akenine-Möller
What happens if you round off floating point vertices to nearest
pixel center?Triangleedgeusingfloatingpointcoords
Edge with ”snapped”vertex coordinates
Frame 1 Frame 2 Frame 3
Big jump here... looks really bad.
With sub-pixelcoordinates thiswill get solved
![Page 9: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/9.jpg)
© 2008 Tomas Akenine-Möller
We need sub-pixel coordinates!
Remember: integer coordsat pixel corners!
Sub-pixelSample points
Floating pointcoordinatessnap to the closest sub-pixel sample
• We can use fixed point math (integer)
• Use 2 sub-pixel fractional bits per x, and y
![Page 10: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/10.jpg)
© 2011 Michael Doggett
Edge functions Vertex positioning
Traversal
Interpolation
![Page 11: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/11.jpg)
© 2008 Tomas Akenine-Möller
How do we determine if a sample is inside a triangle?• Convert edges into functions
• line equation
• Edge function for 2 points p0 and p1 is:
Can be thought of as the ‘normal’ of the line
![Page 12: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/12.jpg)
© 2008 Tomas Akenine-Möller
How do points relate to the edge function?
![Page 13: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/13.jpg)
© 2008 Tomas Akenine-Möller
Points are inside if all edge functions are positive!
![Page 14: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/14.jpg)
© 2008 Tomas Akenine-Möller
What happens to pixels exactly on an edge?
• One and only one of A or B
• Because :
• No cracks between triangles
• No overlapping triangles
A
BDoes the pixel belong to A or B,
or both ? or neither?
![Page 15: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/15.jpg)
© 2008 Tomas Akenine-Möller
How to decide which triangle an edge sample is in?
One solution (by McCool el at)
• Another way to think about it:
• We exclude shadowed edges
![Page 16: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/16.jpg)
© 2011 Michael Doggett
Edge functions
Vertex positioning
Traversal
Interpolation
![Page 17: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/17.jpg)
© 2008 Tomas Akenine-Möller
Triangle traversal strategies
• Simple (and naive):
• execute Inside() for every pixel on screen, and for every edge
• Little better: compute bounding box first
• Called ”bounding box traversal”
Visits all gray pixels
Only dark gray pixels are inside
So only keep those
![Page 18: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/18.jpg)
© 2008 Tomas Akenine-Möller
• Was used for mobile graphics chip
• by Korean research group (KAIST)
• Advantage: only traverse from left to right
• Could make for more efficient memory accesses
• Could backtrack at a faster pace (because no mem acc)
Backtrack traversal
![Page 19: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/19.jpg)
© 2008 Tomas Akenine-Möller
Zigzag traversal
• Simple technique that avoids backtracking
• Still visits outside pixels
• see the last scanline
![Page 20: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/20.jpg)
© 2008 Tomas Akenine-Möller
Side by side comparisonBacktrack vs zigzag
Backtrack never visits unnecessary
pixels to the left
Zigzag never visits unnecessary pixels to the left on even scanlines and to the right on odd scanlines
(and avoids backtracking)
![Page 21: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/21.jpg)
© 2008 Tomas Akenine-Möller
Tiled traversal
• Divide screen into tiles
• each tile is w x h pixels
• 8x8 tile size is common in desktop GPUs
4x4 tiles
![Page 22: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/22.jpg)
© 2008 Tomas Akenine-Möller
Tiled traversal
• Gives better texture cache performance
• Enables simple culling (Zmin & Zmax)
• Real-time buffer compression (color and depth)
![Page 23: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/23.jpg)
© 2008 Tomas Akenine-Möller
Is tiled traversal that different?
• We need:
• 1 : Traverse to tiles overlapping triangle
• 2 : Test if tile overlaps with triangle
• 3 : Traverse pixels inside tile
• We only need new algorithm for part 2
• Can use Haines and Wallace’s box line intersection test (EGSR94)
![Page 24: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/24.jpg)
© 2011 Michael Doggett
Edge functions
Vertex positioning
Traversal
Interpolation
![Page 25: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/25.jpg)
© 2008 Tomas Akenine-Möller
How can we interpolate parameters across triangles?
image
![Page 26: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/26.jpg)
© 2008 Tomas Akenine-Möller
How can we interpolate parameters across triangles?
• What is s at p?
• S should vary smoothly across triangle
• Use barycentric coordinates, (u,v,w)
![Page 27: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/27.jpg)
© 2008 Tomas Akenine-Möller
Barycentric Coordinates
Proportional to the signed areas of the subtriangles
formed by p and the vertices
Area computed using cross product, e.g.:
In graphics, we use barycentric coordinates normalized with respect to triangle area:
Not perspective correct
![Page 28: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/28.jpg)
© 2008 Tomas Akenine-Möller
What do barycentric coordinates look like?
• Constant on lines parallel to an edge
• because the height of the subtriangle is constant
![Page 29: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/29.jpg)
© 2008 Tomas Akenine-Möller
How to use them?
Interpolate vertex parameters s0, s1, s2
![Page 30: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/30.jpg)
© 2008 Tomas Akenine-Möller
Barycentric coordinates from edge functions (1)
• The a and b parameters of an edge function must be proportional to the normal
• We can use the edge functions directly to compute barycentric coordinates as well!
• Focus on edge, e2:
![Page 31: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/31.jpg)
© 2008 Tomas Akenine-Möller
Barycentric coordinates from edge functions (2)
• We can show that ||n2|| = b (base of triangle)
• ||p-p0||cos α is the length of projection of p-p0 onto n2 i.e., h (height of triangle)
• From definition of dot product:
![Page 32: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/32.jpg)
© 2008 Tomas Akenine-Möller
Barycentric coordinates from edge functions (3)
• This means:
• And 1/(2AΔ) can be computed in the triangle setup (once per triangle)
![Page 33: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/33.jpg)
© 2008 Tomas Akenine-Möller 33
Resulting interpolation
• Looks even worse when animated...
• Clearly, perspective correction is needed!
Which is which?
With barycentric coordinates,i.e., without perspective correction
With perspective correction
© 2008 Tomas Akenine-Möller
![Page 34: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/34.jpg)
© 2008 Tomas Akenine-Möller 34
• Why?–Things farther away appear smaller!
• And even inside objects, of course:
Perspective-correct interpolation
![Page 35: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/35.jpg)
© 2008 Tomas Akenine-Möller
Remember homogeneous coordinates
p = (px, py, pz, 1) in screen space
![Page 36: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/36.jpg)
© 2008 Tomas Akenine-Möller
Perspective correct interpolation
• An overly simplified way to think of it
• Linearly interpolate
• s/w in screen space
• 1/w in screen space
• Then divide
![Page 37: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/37.jpg)
© 2008 Tomas Akenine-Möller
Perspective correct interpolation coordinates
• Compute perspective correct barycentric coordinates (u,v,w) first
• Then interpolate vertex parameters
![Page 38: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/38.jpg)
© 2008 Tomas Akenine-Möller
Perspectively correct barycentric coordinates
Recall perspective correction
Simplify:
© 2008 Tomas Akenine-Möller
![Page 39: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/39.jpg)
© 2008 Tomas Akenine-Möller
Once per triangle vs Once per pixel
Triangle setup
Per pixel (simple)
![Page 40: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/40.jpg)
© 2011 Michael Doggett
What’s next• Read chapter 2 & 3 in Graphics Hardware notes
• Rasterization and interpolation
• Next Week
• Fixed point math (for the sub-pixel sampling)
• Texturing
• Caching
• Read chapter 5.5 General Caching
• Assignment 1 available on the web page
![Page 41: Rasterization - Lunds tekniska högskolafileadmin.cs.lth.se/.../EDAN35/lectures/L2-rasterization.pdf · 2017. 11. 2. · • Rasterization and interpolation • Next Week • Fixed](https://reader036.vdocuments.net/reader036/viewer/2022081523/5fe10f71a248041af453f5e1/html5/thumbnails/41.jpg)
The End!