INFOMAGR – Advanced Graphics Jacco Bikker - November 2018 - February 2019 Lecture 4 - “Real - time Ray Tracing” Welcome! 𝑱 𝒚, 𝒚 ′ = 𝒉(𝒚, 𝒚 ′ ) 𝝑 𝒚, 𝒚 ′ + න 𝝇 𝒚, 𝒚 ′ , 𝒚 ′′ 𝑱 𝒚 ′ , 𝒚 ′′ 𝒆𝒚′′ 𝑻
Today’s Agenda: ▪ Introduction ▪ Ray Distributions ▪ The Top-level BVH ▪ Real-time Ray Tracing ▪ Assignment 2
Advanced Graphics – Real-time Ray Tracing 3 Introduction Cost Breakdown for Ray Tracing: Using the BVH: ▪ Pixels ▪ Pixels ▪ Primitives ▪ N primitives ➔ log N deep tree ▪ Light sources ▪ Light sources ▪ Path segments ▪ Path segments Mind scalability as well as constant cost. Example: scene consisting of 1k spheres and Example: scene consisting of 1k spheres and 4 light sources, diffuse materials, rendered 4 light sources, diffuse materials, rendered to 1M pixels: to 1M pixels: 1𝑁 × 5 × 1𝑙 = 5 ∙ 10 9 ray/prim 1𝑁 × 5 × 10 = 5 ∙ 10 7 ray/(prim or node) intersections. intersections. (multiply by desired framerate for realtime) (multiply by desired framerate for realtime)
Advanced Graphics – Real-time Ray Tracing 4 Introduction
Advanced Graphics – Real-time Ray Tracing 5 Introduction Reality Check Performance is now OK, but we’re not quite ready to render a game world.
Advanced Graphics – Real-time Ray Tracing 6 Introduction
Today’s Agenda: ▪ Introduction ▪ Ray Distributions ▪ The Top-level BVH ▪ Real-time Ray Tracing ▪ Assignment 2
Advanced Graphics – Real-time Ray Tracing 8 Ray Distributions Cost of ray tracing: Dominated by memory access cost. ▪ Ray data ▪ Node data ▪ Triangle data ▪ Material data (incl. textures)
Advanced Graphics – Real-time Ray Tracing 9 Ray Distributions Primary rays: For a tile of pixels, these are organized in a narrow frustum. All rays share a common origin.
Advanced Graphics – Real-time Ray Tracing 10 Ray Distributions Shadow rays: For point lights, shadow rays also tend to travel close together. When traced from the light source, they too have a common origin.
Advanced Graphics – Real-time Ray Tracing 11 Ray Distributions Secondary rays: Reflected and refracted rays tend to diverge significantly.
Advanced Graphics – Real-time Ray Tracing 12 Ray Distributions Coherence Primary rays and shadow rays for point lights are coherent : ▪ they tend to intersect the same primitives; ▪ they tend to traverse the same BVH nodes. Our problem: Ray tracing cost is dominated by memory latency. Solution: Amortize cost of fetching data over multiple rays.
Advanced Graphics – Real-time Ray Tracing 13 Ray Distributions Coherent Ray Tracing* SIMD: four rays for the price of one. BVHNode::Traverse( Ray r ) { if (!r.Intersects( bounds )) return; if (isleaf()) { IntersectPrimitives(); } else { pool[left].Traverse( r ); pool[left + 1].Traverse( r ); } } *: Interactive Rendering with Coherent Ray Tracing, Wald et al., 2001
Advanced Graphics – Real-time Ray Tracing 14 Ray Distributions Coherent Ray Tracing* SIMD: four rays for the price of one. BVHNode::Traverse( Ray4 r4 ) { if (!r4.Intersects( bounds )) return; if (isleaf()) { IntersectPrimitives(); } else { pool[left].Traverse( r4 ); pool[left + 1].Traverse( r4 ); } } *: Interactive Rendering with Coherent Ray Tracing, Wald et al., 2001
Advanced Graphics – Real-time Ray Tracing 15 Ray Distributions Coherent Ray Tracing Ray packet traversal: ▪ intersect four rays with a single BVH node; ▪ if any ray in the packet intersects the node, we traverse it; ▪ if the node is a leaf node, we intersect the four rays with each primitive in the leaf. Masking: ▪ We maintain an ‘active’ mask for disabling rays that do not intersect a node.
Advanced Graphics – Real-time Ray Tracing 16 Ray Distributions Coherent Ray Tracing* SIMD: four rays for the price of one. BVHNode::Traverse( Ray4 r4, bool4 mask4 ) { bool4 hit4 = r4.Intersects( bounds ) & mask4; if (none( hit4 )) return; if (isleaf()) { IntersectPrimitives(); } else { pool[left].Traverse( r4, hit4 ); pool[left + 1].Traverse( r4, hit4 ); } }
Advanced Graphics – Real-time Ray Tracing 17 Ray Distributions Coherent Ray Tracing Results: ▪ for coherent packets, memory traffic is reduced; ▪ overall performance is improved by ~2.3x. Overhead: ▪ if only a single ray requires traversal or intersection, all four rays perform this operation.
Advanced Graphics – Real-time Ray Tracing 18 Ray Distributions Large Packets* Cost of memory access can be amortized over more rays by using larger packets. Note that a naïve approach will lead to significant overhead. We therefore add a frustum test to rapidly reject BVH nodes: If the packet frustum does not intersect the node AABB, we discard the node. The cost of this operation is independent of the number of rays in the packet. Likewise, a node is traversed as soon as we find that a ray intersects it. This is also independent of packet size. *: Large Ray Packets for Real-time Whitted Ray Tracing, Overbeck et al., 2008
Advanced Graphics – Real-time Ray Tracing 19 Ray Distributions BVHNode::Traverse( RayPacket rp, int first ) Large Packets { if (!Intersects( rp[first )) // 1 Algorithm: { if (!Intersects( rp.frustum )) 1. Early hit test: test first active ray against AABB return; // 2 FindFirstActive( rp, ++first ); // 3 2. Early miss test: test AABB against frustum } 3. Brute force test: test all rays until a hit is if (first < rp.rayCount) found. { This step yields a new first active ray index. if (isleaf()) { IntersectPrimitives( rp ); } else { left.Traverse( rp, first ); right.Traverse( rp, first ); } } }
Advanced Graphics – Real-time Ray Tracing 20 Ray Distributions BVHNode::Traverse( RayPacket rp, int first ) Large Packets { if (!Intersects( rp[first )) // 1 Details: { if (!Intersects( rp.frustum )) return; // 2 ▪ Constructing the frustum FindFirstActive( rp, ++first ); // 3 ▪ Ray order & overhead } if (first < rp.rayCount) ▪ First / last { if (isleaf()) ▪ Optimizations: recursion, SIMD { IntersectPrimitives( rp ); } else { left.Traverse( rp, first ); right.Traverse( rp, first ); } } }
Advanced Graphics – Real-time Ray Tracing 21 Ray Distributions Frustum Construction 𝑞 1 Method 1, for primary rays: 𝑞 0 Planes are easily defined using the corner rays: 𝑂 1 = (𝑞 0 −𝐹) × (𝑞 1 − 𝑞 0 ) , 𝑒 1 = 𝑂 1 ∙ 𝐹 𝐹 𝑂 2 = (𝑞 1 −𝐹) × (𝑞 2 − 𝑞 1 ) , 𝑒 2 = 𝑂 2 ∙ 𝐹 𝑂 3 = (𝑞 2 −𝐹) × (𝑞 3 − 𝑞 2 ) , 𝑒 3 = 𝑂 3 ∙ 𝐹 𝑂 4 = (𝑞 3 −𝐹) × (𝑞 0 − 𝑞 3 ) , 𝑒 4 = 𝑂 4 ∙ 𝐹 𝑞 2 Note: for secondary rays, we will not have a common origin, nor corner rays. 𝑞 3
Advanced Graphics – Real-time Ray Tracing 22 Ray Distributions 1 𝑙 Frustum Construction Method 2, for shadow rays: 1. Determine dominant axis and direction: 𝑂 𝐸 𝑗 , chose axis 𝐸 𝑡 = σ 𝑗=0 𝑙 as the largest component of 𝐸 𝑡 , ො 𝑣 and ො 𝑤 are the other axes. 𝑣 ො 2. Construct a plane 𝑄 orthogonal to 𝑙 3. Calculate intersection coordinates 𝑣, 𝑤 of the rays with P 4. Determine 𝑣 𝑛𝑗𝑜 , 𝑣 𝑛𝑏𝑦 and 𝑤 𝑛𝑗𝑜 , 𝑤 𝑛𝑏𝑦 5. Corner rays are now: 𝑣 𝑛𝑗𝑜 , 𝑤 𝑛𝑗𝑜 𝑣 𝑛𝑏𝑦 , 𝑤 𝑛𝑗𝑜 𝑣 𝑛𝑏𝑦 , 𝑤 𝑛𝑏𝑦 (𝑣 𝑛𝑗𝑜 , 𝑤 𝑛𝑏𝑦 ) Note: this still requires a common origin.
Advanced Graphics – Real-time Ray Tracing 23 Ray Distributions 𝑙 Frustum Construction Method 3, for generic rays: 1. Construct two planes: 𝑔𝑏𝑠 , orthogonal to 𝑄 𝑙 , at location 𝑙 𝑔𝑏𝑠 which is obtained from the scene AABB; 𝑜𝑓𝑏𝑠 , orthogonal to 𝑄 𝑙 , at location 𝑙 𝑜𝑓𝑏𝑠 , which is obtained from the AABB over the ray origins. 2. Calculate intersection coordinates 𝑣 𝑔𝑏𝑠 , 𝑤 𝑔𝑏𝑠 𝑙 𝑜𝑓𝑏𝑠 𝑙 𝑔𝑏𝑠 and 𝑣 𝑜𝑓𝑏𝑠 , 𝑤 𝑜𝑓𝑏𝑠 of the rays with 𝑄 𝑔𝑏𝑠 and 𝑄 𝑜𝑓𝑏𝑠 . 𝑔𝑏𝑠 , 𝑤 𝑛𝑗𝑜 𝑔𝑏𝑠 . 𝑜𝑓𝑏𝑠 and 𝑣 𝑛𝑗𝑜 𝑔𝑏𝑠 , 𝑣 𝑛𝑏𝑦 𝑔𝑏𝑠 , 𝑣 𝑛𝑏𝑦 𝑜𝑓𝑏𝑠 , 𝑣 𝑛𝑏𝑦 𝑜𝑓𝑏𝑠 , 𝑣 𝑛𝑏𝑦 𝑜𝑓𝑏𝑠 , 𝑤 𝑛𝑗𝑜 3. Determine 𝑣 𝑛𝑗𝑜 4. Corner rays are now defined as 𝑔𝑏𝑠 − 𝑣 𝑛𝑗𝑜 𝑜𝑓𝑏𝑠 , 𝑔𝑏𝑠 , 𝑤 𝑛𝑗𝑜 𝑜𝑓𝑏𝑠 , 𝑤 𝑛𝑗𝑜 𝑣 𝑛𝑗𝑜 𝑔𝑏𝑠 , 𝑤 𝑛𝑗𝑜 𝑔𝑏𝑠 − 𝑣 𝑛𝑏𝑦 𝑜𝑓𝑏𝑠 , 𝑜𝑓𝑏𝑠 , 𝑤 𝑛𝑗𝑜 𝑣 𝑛𝑏𝑦 𝑔𝑏𝑠 , 𝑤 𝑛𝑏𝑦 𝑜𝑓𝑏𝑠 , 𝑔𝑏𝑠 𝑜𝑓𝑏𝑠 , 𝑤 𝑛𝑏𝑦 𝑣 𝑛𝑏𝑦 − 𝑣 𝑛𝑏𝑦 𝑔𝑏𝑠 , 𝑤 𝑛𝑏𝑦 𝑔𝑏𝑠 𝑜𝑓𝑏𝑠 , 𝑤 𝑛𝑏𝑦 𝑜𝑓𝑏𝑠 ) . 𝑣 𝑛𝑗𝑜 − (𝑣 𝑛𝑗𝑜
Advanced Graphics – Real-time Ray Tracing 24 Ray Distributions Ray Order 0 7 The order of the rays in a packet is important. 8 We keep track of the first active ray: in this case the green dot. We thus enter the node with 61 rays, while only 12 rays actually intersect the node. Keeping track of the last ray helps somewhat. 63
Recommend
More recommend