Skip to content

Commit

Permalink
visualize BVH
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Jun 18, 2024
1 parent c18090d commit abd74b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
28 changes: 12 additions & 16 deletions src/pt/deferred_renderer_lighting_pass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,7 @@ fn main(@builtin(global_invocation_id) globalInvocationId: vec3<u32>) {
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<f32, 3>(color.r, color.g, color.b);
Expand All @@ -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;
}
Expand Down Expand Up @@ -267,18 +259,20 @@ struct Intersection {
textureDescriptorIdx: u32,
}

@must_use
fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr<function, Intersection>) -> bool {
fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr<function, Intersection>, nodesVisited: ptr<function, u32>) -> bool {
let intersector = rayAabbIntersector(ray);
var toVisitOffset = 0u;
var currentNodeIdx = 0u;
var nodesToVisit: array<u32, 32u>;
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) {
Expand Down Expand Up @@ -325,6 +319,8 @@ fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr<function, Intersection>) ->
}
}

*nodesVisited = visitCount;

return didIntersect;
}

Expand Down Expand Up @@ -468,7 +464,7 @@ fn rayIntersectTriangle(ray: Ray, tri: Positions, tmax: f32, hit: ptr<function,
let p = tri.p0 + u * e1 + v * e2;
let n = normalize(cross(e1, e2));
let b = vec3f(1f - u - v, u, v);
*hit = TriangleHit(offsetPosition(p, n), b, t);
*hit = TriangleHit(p, b, t);
return true;
} else {
return false;
Expand Down
34 changes: 15 additions & 19 deletions src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,13 +827,7 @@ fn main(@builtin(global_invocation_id) globalInvocationId: vec3<u32>) {
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<f32, 3>(color.r, color.g, color.b);
Expand All @@ -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;
}
Expand Down Expand Up @@ -975,18 +967,20 @@ struct Intersection {
textureDescriptorIdx: u32,
}
@must_use
fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr<function, Intersection>) -> bool {
fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr<function, Intersection>, nodesVisited: ptr<function, u32>) -> bool {
let intersector = rayAabbIntersector(ray);
var toVisitOffset = 0u;
var currentNodeIdx = 0u;
var nodesToVisit: array<u32, 32u>;
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) {
Expand Down Expand Up @@ -1033,6 +1027,8 @@ fn rayIntersectBvh(ray: Ray, rayTMax: f32, hit: ptr<function, Intersection>) ->
}
}
*nodesVisited = visitCount;
return didIntersect;
}
Expand Down Expand Up @@ -1176,7 +1172,7 @@ fn rayIntersectTriangle(ray: Ray, tri: Positions, tmax: f32, hit: ptr<function,
let p = tri.p0 + u * e1 + v * e2;
let n = normalize(cross(e1, e2));
let b = vec3f(1f - u - v, u, v);
*hit = TriangleHit(offsetPosition(p, n), b, t);
*hit = TriangleHit(p, b, t);
return true;
} else {
return false;
Expand Down Expand Up @@ -1235,11 +1231,11 @@ fn directionInCosineWeightedHemisphere(u: vec2f) -> 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;
Expand Down

0 comments on commit abd74b8

Please sign in to comment.