From abd74b8f11e20d3093642c03108c79653603b84c Mon Sep 17 00:00:00 2001 From: Johann Muszynski Date: Tue, 18 Jun 2024 09:08:04 +0300 Subject: [PATCH] visualize BVH --- src/pt/deferred_renderer_lighting_pass.wgsl | 28 ++++++++--------- src/pt/shader_source.hpp | 34 +++++++++------------ 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/src/pt/deferred_renderer_lighting_pass.wgsl b/src/pt/deferred_renderer_lighting_pass.wgsl index b474103..a2ed389 100644 --- a/src/pt/deferred_renderer_lighting_pass.wgsl +++ b/src/pt/deferred_renderer_lighting_pass.wgsl @@ -119,13 +119,7 @@ fn main(@builtin(global_invocation_id) globalInvocationId: vec3) { let textureIdx = globalInvocationId.xy; let uv = vec2f(globalInvocationId.xy) / uniforms.framebufferSize; - var color = vec3f(0.0, 0.0, 0.0); - let depthSample = textureLoad(gbufferDepth, textureIdx, 0); - if depthSample != 0.0 { - let coord = vec2u(uv * uniforms.framebufferSize); - let position = worldFromUv(uv, depthSample); - color = surfaceColor(uv, position); - } + var color = surfaceColor(uv); let sampleBufferIdx = textureIdx.y * u32(uniforms.framebufferSize.x) + textureIdx.x; sampleBuffer[sampleBufferIdx] = array(color.r, color.g, color.b); @@ -142,15 +136,13 @@ fn worldFromUv(uv: vec2f, depthSample: f32) -> vec3f { const NUM_BOUNCES = 2; @must_use -fn surfaceColor(uv: vec2f, primaryPos: vec3f) -> vec3f { +fn surfaceColor(uv: vec2f) -> vec3f { var color = vec3f(0.0); let primaryRay = generateCameraRay(uniforms.camera, uv.x, 1.0 - uv.y); var hit: Intersection; - if rayIntersectBvh(primaryRay, T_MAX, &hit) { - let diff = abs(hit.p - primaryPos); - let a = 0.05; - color = (1.0 + a) * diff / (diff + vec3(a)); - } + var nodesVisited: u32; + rayIntersectBvh(primaryRay, T_MAX, &hit, &nodesVisited); + color = vec3f(0.01 * f32(nodesVisited)); return color; } @@ -267,18 +259,20 @@ struct Intersection { textureDescriptorIdx: u32, } -@must_use -fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr) -> bool { +fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr, nodesVisited: ptr) -> bool { let intersector = rayAabbIntersector(ray); var toVisitOffset = 0u; var currentNodeIdx = 0u; var nodesToVisit: array; var didIntersect: bool = false; var tmax = rayTMax; + var visitCount = 0u; loop { let node: BvhNode = bvhNodes[currentNodeIdx]; + visitCount += 1u; + if rayIntersectAabb(intersector, node.aabb, tmax) { if node.triangleCount > 0u { for (var idx = 0u; idx < node.triangleCount; idx = idx + 1u) { @@ -325,6 +319,8 @@ fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr) -> } } + *nodesVisited = visitCount; + return didIntersect; } @@ -468,7 +464,7 @@ fn rayIntersectTriangle(ray: Ray, tri: Positions, tmax: f32, hit: ptr) { let textureIdx = globalInvocationId.xy; let uv = vec2f(globalInvocationId.xy) / uniforms.framebufferSize; - var color = vec3f(0.0, 0.0, 0.0); - let depthSample = textureLoad(gbufferDepth, textureIdx, 0); - if depthSample != 0.0 { - let coord = vec2u(uv * uniforms.framebufferSize); - let position = worldFromUv(uv, depthSample); - color = surfaceColor(uv, position); - } + var color = surfaceColor(uv); let sampleBufferIdx = textureIdx.y * u32(uniforms.framebufferSize.x) + textureIdx.x; sampleBuffer[sampleBufferIdx] = array(color.r, color.g, color.b); @@ -850,15 +844,13 @@ fn worldFromUv(uv: vec2f, depthSample: f32) -> vec3f { const NUM_BOUNCES = 2; @must_use -fn surfaceColor(uv: vec2f, primaryPos: vec3f) -> vec3f { +fn surfaceColor(uv: vec2f) -> vec3f { var color = vec3f(0.0); let primaryRay = generateCameraRay(uniforms.camera, uv.x, 1.0 - uv.y); var hit: Intersection; - if rayIntersectBvh(primaryRay, T_MAX, &hit) { - let diff = abs(hit.p - primaryPos); - let a = 0.05; - color = (1.0 + a) * diff / (diff + vec3(a)); - } + var nodesVisited: u32; + rayIntersectBvh(primaryRay, T_MAX, &hit, &nodesVisited); + color = vec3f(0.01 * f32(nodesVisited)); return color; } @@ -975,18 +967,20 @@ struct Intersection { textureDescriptorIdx: u32, } -@must_use -fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr) -> bool { +fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr, nodesVisited: ptr) -> bool { let intersector = rayAabbIntersector(ray); var toVisitOffset = 0u; var currentNodeIdx = 0u; var nodesToVisit: array; var didIntersect: bool = false; var tmax = rayTMax; + var visitCount = 0u; loop { let node: BvhNode = bvhNodes[currentNodeIdx]; + visitCount += 1u; + if rayIntersectAabb(intersector, node.aabb, tmax) { if node.triangleCount > 0u { for (var idx = 0u; idx < node.triangleCount; idx = idx + 1u) { @@ -1033,6 +1027,8 @@ fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr) -> } } + *nodesVisited = visitCount; + return didIntersect; } @@ -1176,7 +1172,7 @@ fn rayIntersectTriangle(ray: Ray, tri: Positions, tmax: f32, hit: ptr vec3f { @must_use fn animatedBlueNoise(coord: vec2u, frameIdx: u32, totalSampleCount: u32) -> vec2f { - let idx = (coord.y %)" -R"( blueNoise.height) * blueNoise.width + (coord.x % blueNoise.width); + let idx = (coord.y % blueNoise.height) * blueNoise.width + (coord.x % blueNoise.width); let blueNoise = blueNoise.data[idx]; // 2-dimensional golden ratio additive recurrence sequence - // https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ + // https://extremelearning.com.au/unreasonable-effec)" +R"(tiveness-of-quasirandom-sequences/ let n = frameIdx % totalSampleCount; let a1 = 0.7548776662466927f; let a2 = 0.5698402909980532f;