CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January...
Transcript of CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January...
![Page 1: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/1.jpg)
CS 6958 LECTURE 4
INTRO TO GRAPHICS GEOMETRY
January 15, 2014
![Page 2: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/2.jpg)
Administrative
¨ Email me if you are not on the mailing list ¨ [email protected] ¨ [email protected]
![Page 3: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/3.jpg)
Geometry for Graphics
![Page 4: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/4.jpg)
Geometry for Graphics
“If it doesn’t have any math, it probably isn’t worth doing. If it has too much math, it definitely isn’t worth doing”
-Steve Parker
![Page 5: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/5.jpg)
Geometry for Ray Tracing
• Point • Vector • Triangle • Ray • Plane, Sphere, Disk, Cylinder, …
• All of these can be made with tessellated triangles
• Assumed that all of these are 3D
![Page 6: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/6.jpg)
Vectors
• In 3D, a vector is just 3 floats • <x, y, z>
• Intuitive meaning: a direction and length
2D example
<3, 2>
![Page 7: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/7.jpg)
Points
• Also just 3 floats • <x, y, z>
• Intuitive meaning: a point in space
<3, 2>
![Page 8: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/8.jpg)
Vectors
• Addition • A + B = <A.x + B.x, A.y + B.y, A.z + B.z>
<3, 2> + <1, 2>
![Page 9: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/9.jpg)
Vectors
• Scalar multiplication • 2A = <2*A.x, 2*A.y, 2*A.z>
2.0 * <3, 2>
![Page 10: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/10.jpg)
Vector Properties
• If A, B, C are vectors • Commutative
• A + B = B + A
• Associative addition • (A + B) + C = A + (B + C)
• Additive Inverse • A + (-A) = 0
![Page 11: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/11.jpg)
Vector Properties
• A, B, C are vectors, r, s are scalars • Associativity of scalar multiplication
• (rs)A = r(sA)
• Distributivity of scalar sums • (r + s)A = rA + sA
• Distributivity of vector sums • r (A + B) = rA + rB
![Page 12: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/12.jpg)
Vectors vs. Points
• If P is a point, V is a vector • What does P + P mean?
• Translation?
• What does translation mean for V? • In code they’re both just a set of 3 floats
![Page 13: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/13.jpg)
Vectors/Points
• P1 = P0 + V
P0
P1
V
![Page 14: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/14.jpg)
Vectors/Points
• V = P1 – P0 • Distance between P0 and P1 = |V|
V
P0
P1
![Page 15: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/15.jpg)
Points vs. Vectors
¨ Operators that do make sense: P = P+V P = P-V V = P-P V = V + V V = V - V V = s * V V = - V V = V * V
![Page 16: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/16.jpg)
Other Operators
¨ A, B are vectors ¤ Dot product “multiplies” them, but results in a scalar
¨ A·∙B = Ax * Bx + Ay * By + Az * Bz
![Page 17: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/17.jpg)
Useful Properties
cos(θ) = A ·∙ B
θ
|A||B|
B
A
![Page 18: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/18.jpg)
Useful properties
¨ Length of a vector V = |V|
= sqrt(Vx^2 + Vy^2 + Vz^2) Or: |V| = sqrt(V·∙V) ¨ V·∙V = length squared
![Page 19: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/19.jpg)
Unit Vector
¨ A vector with unit length (1.0) is “normalized”
¨ If V·∙V = 1, V is normalized
![Page 20: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/20.jpg)
Rays
• Origin O, direction V, distance t • Normalize V so we can parameterize distance • t = distance to object • |V| = 1 • Intersection point P:
• O + tV
O
V
O + 1V
O + 2V
O + tV
P
![Page 21: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/21.jpg)
Normalizing a Vector
Vx, Vy, Vz
|V| |V| |V| < > unit(V) =
|V| = sqrt(V·∙V) Preserves direction of V
![Page 22: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/22.jpg)
Normalized Vectors
If A and B are normalized vectors cos(θ) = A ·∙ B = A ·∙ B
¨ We find ourselves regularly normalizing ray directions!
|A||B|
![Page 23: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/23.jpg)
Cross Product
• A, B, C are vectors • C = A x B • “right hand rule” <AyBz - AzBy, AzBx - AxBz, AxBy - AyBx>
A
B
AxB
![Page 24: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/24.jpg)
Cross Product
• C = A x B • C is perpendicular to both A and B • |A x B| = |A||B|sinθ • |A x B| = area of swept rectangle • If A = B or A = -B, then A x B = 0
A
B
AxB
![Page 25: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/25.jpg)
Colors
• We will use Red, Green, Blue (RGB) • Other color spaces exist
• A color is 3 floats (but don’t use Vector!) • Range is 0 – 1 (but allow them to go higher)
• Operators: • Color * Color • Color * scalar • Color - Color • Color + Color
![Page 26: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/26.jpg)
Images
• An image is a helper class • 2D array of pixels • Pixel != Color (must clamp to 0 – 1 first)
• Knows where the framebuffer is • Has function to set pixel, given (color, i, j)
• Convert i, j coordinates to address in framebuffer
![Page 27: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/27.jpg)
Framebuffer is “upside down”
y=0
y=0
Real world Our ray tracer OpenGL Taught since 2nd grade
Televisions Raster Images Other 1950’s technology 2D arrays
![Page 28: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/28.jpg)
Framebuffer is “upside down”
start_fb start_fb + (xres*3)
start_fb + ((xres*3) * yres) start_fb + ((xres*3) * yres) + (xres * 3)
![Page 29: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/29.jpg)
Geometric Queries
¨ Back to the original question: ¨ Ray tracing: determine if (and where) rays hit an
object
¨ May be interested in the point itself, or just the distance
Where?
!O + t
!V
![Page 30: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/30.jpg)
Geometric Queries
¨ Ray-Plane ¨ Ray-Sphere ¨ Ray-Triangle ¨ Ray-Cylinder, Disk, Ring, Torus, Mobius Strip ¨ Ray-Box ¨ Ray-implicit function
![Page 31: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/31.jpg)
Ray-sphere intersection
¨ Points on a sphere are equidistant from the center
¨ Our measure of distance: dot product ¨ Equation for sphere: Assume intersection point P exists
!P −!C( )i!P −!C( ) − r2 = 0
P
C P-C
r
![Page 32: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/32.jpg)
Ray-sphere intersection
¨ Now represent P as a function of the ray ¤ Assume intersection point P exists
¨ t is what we need
P
O V
t
![Page 33: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/33.jpg)
Ray-sphere intersection
¨ Using both equations:
¨ Substitute:
P
O V
t
C P-C
r
![Page 34: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/34.jpg)
Ray-sphere intersection
¨ Quadratic equation with:
¤ a = V ·∙ V ¤ b = 2(O – C) ·∙ V ¤ c = (O – C) ·∙ (O – C) – r^2
¨ Roots:
![Page 35: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/35.jpg)
CS6620 Spring 07
Ray-sphere intersection ¨ If the discriminant is negative, the ray misses the
sphere ¨ Otherwise, there are two distinct intersection
points (the two roots)
![Page 36: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/36.jpg)
CS6620 Spring 07
Ray-sphere intersection ¨ Which root do we care about? ¨ Be careful!
¤ Don’t want rays behind the camera showing in our image
![Page 37: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/37.jpg)
Assignment 1
![Page 38: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/38.jpg)
Assignment 1
¨ Implement: ¤ Vector ¤ Color ¤ Image ¤ Ray ¤ Sphere
![Page 39: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/39.jpg)
Ray Skeleton
class Ray { public: Vector org; Vector dir; Ray() { } Ray(const Vector& origin, const Vector& direction) : org(origin), dir(direction) { }
![Page 40: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/40.jpg)
TRaX Programming
¨ https://code.google.com/p/simtrax/wiki/Programming
¨ See simtrax/samples/src/… for examples
¨ Always include “trax.hpp”
¨ void trax_main() instead of int main()
![Page 41: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/41.jpg)
Guidelines/Restrictions
¨ The TRaX compiler can be a bit fragile ¤ Doesn’t quite support full C++
¨ However, these restrictions are likely to result in faster code
![Page 42: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/42.jpg)
No double data types
¨ Single-precision float only! ¤ Using double may cause compilation to fail
¨ Can be tricky to avoid ¤ float pi = 3.14 ¤ float pi = 3.14f ✔ ¤ float f = 5; ¤ float f = 5.f; ✔
![Page 43: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/43.jpg)
No dynamic memory
¨ new/malloc, free/delete ¤ We will see how to deal with this when we start using
main memory
¨ No destructors! ¤ Compiler will fail
¨ Any variables you declare are on the per-thread stacks ¤ Including arrays ¤ Remember tiny stack space
![Page 44: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/44.jpg)
Global variables
¨ Avoid them
¨ Primitive types (float, int, …) should be OK
¨ class/struct types will cause compiler to fail
![Page 45: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/45.jpg)
Inheritance/Templates
¨ Avoid them
¨ Polymorphism is slow
![Page 46: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/46.jpg)
Pass By Reference
¨ Do this whenever possible ¨ Copying arguments is slow
¤ Especially for large data types
¨ Pass by const reference keeps the original data safe
![Page 47: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/47.jpg)
Standard libraries
¨ They don’t exist for TRaX ¤ No one has compiled them for TRaX
¨ Only include “trax.hpp” and your own source files ¤ trax.hpp gives you some standard library
functionality ¤ printf ¤ Various math routines
![Page 48: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/48.jpg)
trax.hpp
¨ Some of the useful functions (more as we need them) ¤ invsqrt(float f) ¤ sqrt(float f) ¤ min(float a, float b) ¤ max(float a, float b) ¤ GetXRes() ¤ GetYRes() ¤ GetFrameBuffer()
![Page 49: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/49.jpg)
Implementation
¨ For Vector, Color ¤ Overload all operators that make sense ¤ Vector operator*(float s) const; ¤ Vector operator+(const Vector& v) const; ¤ …
¨ Up to you if you want to distinguish Point/Vector ¨ Other useful Vector ops
¤ cross ¤ dot ¤ normalize ¤ length
![Page 50: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/50.jpg)
Compiling
¨ The easiest thing to do:
¨ Put your new programs in a subdirectory of: ¤ simtrax/samples/src
¨ Copy an existing Makefile in to your directory
¨ Run make
![Page 51: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/51.jpg)
Writing to the Framebuffer
¨ See mandelbrot sample ¨ int start_fb = GetFrameBuffer();
¤ “pointer” to framebuffer ¤ Main memory pointers are just integers (untyped) ¤ No array indexing! []
storef(red, start_fb + ( i * xres + j ) * 3, 0 ); storef(green, start_fb + ( i * xres + j ) * 3, 1 ); storef(blue, start_fb + ( i * xres + j ) * 3, 2 );
3 floats per pixel
![Page 52: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/52.jpg)
Simtrax Configuration
FUs Int Add
FP Mul
FP Inv
…
Instruction Cache
Thread
RF
Stack RAM
PC
Thread
RF
Stack RAM
PC
Thread
RF
Stack RAM
PC
Bank 1
Bank 0
--num-thread-procs
--num-icaches --num-icache-banks
--config-file
![Page 53: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/53.jpg)
Analysis
¨ What is holding performance back?
¨ For now, this is mostly determined by ¤ “Issue Statistics” ¤ “Module Utilization”
![Page 54: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/54.jpg)
Issue Statistics
¨ Issue Rate: ¤ Want this as high as possible
¨ iCache conflicts ¤ Tracked separately from “resource” conflicts
¨ thread*cycles of resource conflicts ¤ FU conflicts, L1, L2, DRAM
¨ thread*cycles of data dependence ¤ Nothing you can do about this (for now) ¤ Includes RF conflicts
![Page 55: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/55.jpg)
Default Areas (square mm)
¨ FPADD .003 ¨ FPMIN .00072 ¨ FPCMP .00072 ¨ INTADD .00066 ¨ FPMUL .0165 ¨ INTMUL .0117 ¨ FPINV .112 ¨ CONV .001814 ¨ BLT .00066 ¨ BITWISE .00066
![Page 56: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/56.jpg)
Module Utilization
FP AddSub: 41.41 FP Compare: 12.63 … ¨ Does not show every module!
¤ Only the large ones ¤ You will likely need to increase the number of smaller
units as well (Bitwise for ORI, BLT for branches)
¨ A utilization of 40% is pretty high, try to drop it down
![Page 57: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/57.jpg)
Instruction Caches: Multiple Banks
¨ Address à bank mapping is strided
Instruction Cache
Bank 1
Bank 0
✔ Thread a: PC = 0
✔ Thread b: PC = 1
Instruction Cache
Bank 1
Bank 0
✔ Thread a: PC = 0
Thread b: PC = 2 (bank conflict!)
![Page 58: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/58.jpg)
Instruction Caches: Multiple Caches
¨ Consumes much more area, but potentially reduces conflicts
icache 1
✔ Thread a: PC = 0
✔ Thread b: PC = 2 icache 2
![Page 59: CS 6958 LECTURE 4 INTRO TO GRAPHICS GEOMETRYcs6958/slides/Lec4.pdfINTRO TO GRAPHICS GEOMETRY January 15, 2014 Administrative ! Email me if you are not on the mailing list ! dkopta@cs.utah.edu](https://reader034.fdocuments.us/reader034/viewer/2022051908/5ffb344266b6ff58cf6564be/html5/thumbnails/59.jpg)
Instruction Caches
¨ Instruction caches are actually “double pumped”
¨ Each bank can service 2 requests every cycle