Skip to content

Commit

Permalink
debugging carnage
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Jun 8, 2024
1 parent 6876adb commit 907ad39
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 107 deletions.
75 changes: 23 additions & 52 deletions src/pt/deferred_renderer_lighting_pass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ struct Uniforms {
cameraEye: vec4f,
framebufferSize: vec2f,
frameCount: u32,
_pad: u32,
}

struct Aabb {
min: vec3f,
_pad1: f32,
max: vec3f,
_pad2: f32,
}

struct BvhNode {
Expand All @@ -27,20 +30,27 @@ struct BvhNode {

struct Positions {
p0: vec3f,
_pad1: f32,
p1: vec3f,
_pad2: f32,
p2: vec3f,
_pad3: f32,
}

struct VertexAttributes {
n0: vec3f,
_pad1: f32,
n1: vec3f,
_pad2: f32,
n2: vec3f,
_pad3: f32,

uv0: vec2f,
uv1: vec2f,
uv2: vec2f,

textureDescriptorIdx: u32,
_pad4: u32,
}

struct TextureDescriptor {
Expand Down Expand Up @@ -100,25 +110,13 @@ fn main(@builtin(global_invocation_id) globalInvocationId: vec3<u32>) {

var color = vec3f(0.0, 0.0, 0.0);
let depthSample = textureLoad(gbufferDepth, textureIdx, 0);
if depthSample == 0.0 {
let world = worldFromUv(uv, depthSample);
let v = normalize(world - uniforms.cameraEye.xyz);
let s = skyState.sunDirection;

let theta = acos(v.y);
let gamma = acos(clamp(dot(v, s), -1f, 1f));
color = vec3f(
skyRadiance(theta, gamma, CHANNEL_R),
skyRadiance(theta, gamma, CHANNEL_G),
skyRadiance(theta, gamma, CHANNEL_B)
);
} else {
if depthSample != 0.0 {
let coord = vec2u(uv * uniforms.framebufferSize);
let position = worldFromUv(uv, depthSample);
let encodedNormal = textureLoad(gbufferNormal, textureIdx, 0).rgb;
let decodedNormal = 2f * encodedNormal - vec3(1f);
let albedo = textureLoad(gbufferAlbedo, textureIdx, 0).rgb;
color = surfaceColor(coord, offsetPosition(position, decodedNormal), decodedNormal, albedo);
color = surfaceColor(coord, position, decodedNormal, albedo);
}

let sampleBufferIdx = textureIdx.y * u32(uniforms.framebufferSize.x) + textureIdx.x;
Expand All @@ -128,8 +126,8 @@ fn main(@builtin(global_invocation_id) globalInvocationId: vec3<u32>) {
@must_use
fn worldFromUv(uv: vec2f, depthSample: f32) -> vec3f {
let ndc = vec4(2.0 * vec2(uv.x, 1.0 - uv.y) - vec2(1.0), depthSample, 1.0);
let worldInvW = uniforms.inverseViewReverseZProjectionMat * ndc;
let world = worldInvW / worldInvW.w;
let worldInvW: vec4f = uniforms.inverseViewReverseZProjectionMat * ndc;
let world: vec4f = worldInvW / worldInvW.w;
return world.xyz;
}

Expand All @@ -140,45 +138,18 @@ fn surfaceColor(coord: vec2u, primaryPos: vec3f, primaryNormal: vec3f, primaryAl
var position = primaryPos;
var normal = primaryNormal;
var albedo = primaryAlbedo;
var radiance = vec3(0f);
var throughput = vec3(1f);
var color = vec3f(0.0);
let blueNoise = animatedBlueNoise(coord, uniforms.frameCount, 512u);

radiance += throughput * lightSample(blueNoise, position, normal, albedo);

for (var bounce = 1; bounce < NUM_BOUNCES; bounce += 1) {
let wi = evalImplicitLambertian(blueNoise, normal);
let ray = Ray(position, wi);
throughput *= albedo;

var hit: Intersection;
if rayIntersectBvh(ray, T_MAX, &hit) {
position = hit.p;
normal = hit.n;
let uv = hit.uv;
let textureDescriptorIdx = hit.textureDescriptorIdx;
albedo = evalTexture(textureDescriptorIdx, uv);
} else {
let v = ray.direction;
let s = skyState.sunDirection;

let theta = acos(v.y);
let gamma = acos(clamp(dot(v, s), -1f, 1f));

let skyRadiance = vec3f(
skyRadiance(theta, gamma, CHANNEL_R),
skyRadiance(theta, gamma, CHANNEL_G),
skyRadiance(theta, gamma, CHANNEL_B)
);

radiance += throughput * skyRadiance;
break;
}

radiance += throughput * lightSample(blueNoise, position, normal, albedo);
let lightDirection = sampleSolarDiskDirection(blueNoise, SOLAR_COS_THETA_MAX, skyState.sunDirection);
let ray = Ray(position, lightDirection);
var hit: Intersection;
if rayIntersectBvh(ray, T_MAX, &hit) {
let diff = hit.p - position;
let a = 0.1;
color = (1.0 + a) * diff / (diff + vec3(a));
}

return radiance;
return color;
}

@must_use
Expand Down
81 changes: 26 additions & 55 deletions src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,14 @@ struct Uniforms {
cameraEye: vec4f,
framebufferSize: vec2f,
frameCount: u32,
_pad: u32,
}
struct Aabb {
min: vec3f,
_pad1: f32,
max: vec3f,
_pad2: f32,
}
struct BvhNode {
Expand All @@ -735,20 +738,27 @@ struct BvhNode {
struct Positions {
p0: vec3f,
_pad1: f32,
p1: vec3f,
_pad2: f32,
p2: vec3f,
_pad3: f32,
}
struct VertexAttributes {
n0: vec3f,
_pad1: f32,
n1: vec3f,
_pad2: f32,
n2: vec3f,
_pad3: f32,
uv0: vec2f,
uv1: vec2f,
uv2: vec2f,
textureDescriptorIdx: u32,
_pad4: u32,
}
struct TextureDescriptor {
Expand Down Expand Up @@ -808,25 +818,13 @@ fn main(@builtin(global_invocation_id) globalInvocationId: vec3<u32>) {
var color = vec3f(0.0, 0.0, 0.0);
let depthSample = textureLoad(gbufferDepth, textureIdx, 0);
if depthSample == 0.0 {
let world = worldFromUv(uv, depthSample);
let v = normalize(world - uniforms.cameraEye.xyz);
let s = skyState.sunDirection;
let theta = acos(v.y);
let gamma = acos(clamp(dot(v, s), -1f, 1f));
color = vec3f(
skyRadiance(theta, gamma, CHANNEL_R),
skyRadiance(theta, gamma, CHANNEL_G),
skyRadiance(theta, gamma, CHANNEL_B)
);
} else {
if depthSample != 0.0 {
let coord = vec2u(uv * uniforms.framebufferSize);
let position = worldFromUv(uv, depthSample);
let encodedNormal = textureLoad(gbufferNormal, textureIdx, 0).rgb;
let decodedNormal = 2f * encodedNormal - vec3(1f);
let albedo = textureLoad(gbufferAlbedo, textureIdx, 0).rgb;
color = surfaceColor(coord, offsetPosition(position, decodedNormal), decodedNormal, albedo);
color = surfaceColor(coord, position, decodedNormal, albedo);
}
let sampleBufferIdx = textureIdx.y * u32(uniforms.framebufferSize.x) + textureIdx.x;
Expand All @@ -836,8 +834,8 @@ fn main(@builtin(global_invocation_id) globalInvocationId: vec3<u32>) {
@must_use
fn worldFromUv(uv: vec2f, depthSample: f32) -> vec3f {
let ndc = vec4(2.0 * vec2(uv.x, 1.0 - uv.y) - vec2(1.0), depthSample, 1.0);
let worldInvW = uniforms.inverseViewReverseZProjectionMat * ndc;
let world = worldInvW / worldInvW.w;
let worldInvW: vec4f = uniforms.inverseViewReverseZProjectionMat * ndc;
let world: vec4f = worldInvW / worldInvW.w;
return world.xyz;
}
Expand All @@ -848,45 +846,18 @@ fn surfaceColor(coord: vec2u, primaryPos: vec3f, primaryNormal: vec3f, primaryAl
var position = primaryPos;
var normal = primaryNormal;
var albedo = primaryAlbedo;
var radiance = vec3(0f);
var throughput = vec3(1f);
var color = vec3f(0.0);
let blueNoise = animatedBlueNoise(coord, uniforms.frameCount, 512u);
radiance += throughput * lightSample(blueNoise, position, normal, albedo);
for (var bounce = 1; bounce < NUM_BOUNCES; bounce += 1) {
let wi = evalImplicitLambertian(blueNoise, normal);
let ray = Ray(position, wi);
throughput *= albedo;
var hit: Intersection;
if rayIntersectBvh(ray, T_MAX, &hit) {
position = hit.p;
normal = hit.n;
let uv = hit.uv;
let textureDescriptorIdx = hit.textureDescriptorIdx;
albedo = evalTexture(textureDescriptorIdx, uv);
} else {
let v = ray.direction;
let s = skyState.sunDirection;
let theta = acos(v.y);
let gamma = acos(clamp(dot(v, s), -1f, 1f));
let skyRadiance = vec3f(
skyRadiance(theta, gamma, CHANNEL_R),
skyRadiance(theta, gamma, CHANNEL_G),
skyRadiance(theta, gamma, CHANNEL_B)
);
radiance += throughput * skyRadiance;
break;
}
radiance += throughput * lightSample(blueNoise, position, normal, albedo);
let lightDirection = sampleSolarDiskDirection(blueNoise, SOLAR_COS_THETA_MAX, skyState.sunDirection);
let ray = Ray(position, lightDirection);
var hit: Intersection;
if rayIntersectBvh(ray, T_MAX, &hit) {
let diff = hit.p - position;
let a = 0.1;
color = (1.0 + a) * diff / (diff + vec3(a));
}
return radiance;
return color;
}
@must_use
Expand Down Expand Up @@ -1212,8 +1183,7 @@ fn offsetPosition(p: vec3f, n: vec3f) -> vec3f {
// Offset added straight into the mantissa bits to ensure the offset is scale-invariant,
// except for when close to the origin, where we use FLOAT_SCALE as a small epsilon.
let po = vec3f(
bitcast<f32>(bitcast<i32>(p.x) + select(offset.x, -offs)"
R"(et.x, (p.x < 0))),
bitcast<f32>(bitcast<i32>(p.x) + select(offset.x, -offset.x, (p.x < 0))),
bitcast<f32>(bitcast<i32>(p.y) + select(offset.y, -offset.y, (p.y < 0))),
bitcast<f32>(bitcast<i32>(p.z) + select(offset.z, -offset.z, (p.z < 0)))
);
Expand Down Expand Up @@ -1253,7 +1223,8 @@ fn directionInCosineWeightedHemisphere(u: vec2f) -> vec3f {
}
@must_use
fn animatedBlueNoise(coord: vec2u, frameIdx: u32, totalSampleCount: u32) -> vec2f {
fn animatedBlueNoise(coord: vec2u, frameI)"
R"(dx: u32, totalSampleCount: u32) -> vec2f {
let idx = (coord.y % blueNoise.height) * blueNoise.width + (coord.x % blueNoise.width);
let blueNoise = blueNoise.data[idx];
// 2-dimensional golden ratio additive recurrence sequence
Expand Down

0 comments on commit 907ad39

Please sign in to comment.