Skip to content

Commit

Permalink
wip reproject previous samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Jun 25, 2024
1 parent 79bebae commit 97301c2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/pt/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,7 @@ DeferredRenderer::ResolvePass::ResolvePass(
mGbufferBindGroupLayout = GpuBindGroupLayout{
gpuContext.device,
"Resolve pass gbuffer bind group layout",
textureBindGroupLayoutEntry(0, WGPUTextureSampleType_Depth, WGPUShaderStage_Compute)};
textureBindGroupLayoutEntry(0, WGPUTextureSampleType_Depth, WGPUShaderStage_Fragment)};

mGbufferBindGroup = GpuBindGroup{
gpuContext.device,
Expand Down
30 changes: 24 additions & 6 deletions src/pt/deferred_renderer_resolve_pass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,41 @@ fn fsMain(in: VertexOutput) -> @location(0) vec4f {
let textureIdx = vec2u(floor(uv * uniforms.framebufferSize));
let sampleBufferIdx = textureIdx.y * u32(uniforms.framebufferSize.x) + textureIdx.x;
let sample: array<f32, 3> = sampleBuffer[sampleBufferIdx];

let depth = textureLoad(gbufferDepth, textureIdx, 0);
let previousUv = cameraReproject(uv, depth);
let previousTextureIdx = vec2u(floor(previousUv * uniforms.framebufferSize));
let accumulationBufferIdx = previousTextureIdx.y * u32(uniforms.framebufferSize.x) + previousTextureIdx.x;
let previousSample: array<f32, 3> = accumulationBuffer[accumulationBufferIdx];

let currentColor = vec3f(sample[0], sample[1], sample[2]);
var color = vec3f(0f);
var outputColor = vec3f(0f);
if uniforms.frameCount == 0u {
accumulationBuffer[sampleBufferIdx] = sample;
color = currentColor;
outputColor = currentColor;
} else {
let previousSample: array<f32, 3> = accumulationBuffer[sampleBufferIdx];
let previousSample: array<f32, 3> = accumulationBuffer[accumulationBufferIdx];
let previousColor = vec3f(previousSample[0], previousSample[1], previousSample[2]);
color = 0.1 * currentColor + 0.9 * previousColor;
accumulationBuffer[sampleBufferIdx] = array<f32, 3>(color.r, color.g, color.b);
outputColor = 0.1 * currentColor + 0.9 * previousColor;
accumulationBuffer[sampleBufferIdx] = array<f32, 3>(outputColor.r, outputColor.g, outputColor.b);
}

let rgb = acesFilmic(uniforms.exposure * color);
let rgb = acesFilmic(uniforms.exposure * outputColor);
let srgb = pow(rgb, vec3(1f / 2.2f));
return vec4(srgb, 1f);
}

@must_use
fn cameraReproject(uv: vec2f, depth: f32) -> vec2f {
let ndc = vec4f(2f * vec2(uv.x, 1f - uv.y) - vec2(1f), depth, 1f);
let worldInvW = uniforms.currentInverseViewProjectionMat * ndc;
let world = worldInvW / worldInvW.w;
let previousClip = uniforms.previousViewProjectionMat * world;
let previousNdc = previousClip / previousClip.w;
let flippedUv = 0.5 * previousNdc.xy + 0.5;
return vec2(flippedUv.x, 1.0 - flippedUv.y);
}

@must_use
fn acesFilmic(x: vec3f) -> vec3f {
let a = 2.51f;
Expand Down
30 changes: 24 additions & 6 deletions src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,23 +1319,41 @@ fn fsMain(in: VertexOutput) -> @location(0) vec4f {
let textureIdx = vec2u(floor(uv * uniforms.framebufferSize));
let sampleBufferIdx = textureIdx.y * u32(uniforms.framebufferSize.x) + textureIdx.x;
let sample: array<f32, 3> = sampleBuffer[sampleBufferIdx];
let depth = textureLoad(gbufferDepth, textureIdx, 0);
let previousUv = cameraReproject(uv, depth);
let previousTextureIdx = vec2u(floor(previousUv * uniforms.framebufferSize));
let accumulationBufferIdx = previousTextureIdx.y * u32(uniforms.framebufferSize.x) + previousTextureIdx.x;
let previousSample: array<f32, 3> = accumulationBuffer[accumulationBufferIdx];
let currentColor = vec3f(sample[0], sample[1], sample[2]);
var color = vec3f(0f);
var outputColor = vec3f(0f);
if uniforms.frameCount == 0u {
accumulationBuffer[sampleBufferIdx] = sample;
color = currentColor;
outputColor = currentColor;
} else {
let previousSample: array<f32, 3> = accumulationBuffer[sampleBufferIdx];
let previousSample: array<f32, 3> = accumulationBuffer[accumulationBufferIdx];
let previousColor = vec3f(previousSample[0], previousSample[1], previousSample[2]);
color = 0.1 * currentColor + 0.9 * previousColor;
accumulationBuffer[sampleBufferIdx] = array<f32, 3>(color.r, color.g, color.b);
outputColor = 0.1 * currentColor + 0.9 * previousColor;
accumulationBuffer[sampleBufferIdx] = array<f32, 3>(outputColor.r, outputColor.g, outputColor.b);
}
let rgb = acesFilmic(uniforms.exposure * color);
let rgb = acesFilmic(uniforms.exposure * outputColor);
let srgb = pow(rgb, vec3(1f / 2.2f));
return vec4(srgb, 1f);
}
@must_use
fn cameraReproject(uv: vec2f, depth: f32) -> vec2f {
let ndc = vec4f(2f * vec2(uv.x, 1f - uv.y) - vec2(1f), depth, 1f);
let worldInvW = uniforms.currentInverseViewProjectionMat * ndc;
let world = worldInvW / worldInvW.w;
let previousClip = uniforms.previousViewProjectionMat * world;
let previousNdc = previousClip / previousClip.w;
let flippedUv = 0.5 * previousNdc.xy + 0.5;
return vec2(flippedUv.x, 1.0 - flippedUv.y);
}
@must_use
fn acesFilmic(x: vec3f) -> vec3f {
let a = 2.51f;
Expand Down

0 comments on commit 97301c2

Please sign in to comment.