From 90feba08b8899a342aed6cfea18ee4c9a98e9109 Mon Sep 17 00:00:00 2001 From: Johann Muszynski Date: Fri, 5 Jul 2024 23:00:51 +0300 Subject: [PATCH] wip reject samples out of bounds --- src/pt/deferred_renderer_resolve_pass.wgsl | 23 +++++++++++++--------- src/pt/shader_source.hpp | 23 +++++++++++++--------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/pt/deferred_renderer_resolve_pass.wgsl b/src/pt/deferred_renderer_resolve_pass.wgsl index b81218e..a21dd6b 100644 --- a/src/pt/deferred_renderer_resolve_pass.wgsl +++ b/src/pt/deferred_renderer_resolve_pass.wgsl @@ -42,22 +42,27 @@ fn fsMain(in: VertexOutput) -> @location(0) vec4f { let sample: array = sampleBuffer[sampleBufferIdx]; let currentColor = vec3f(sample[0], sample[1], sample[2]); - var outputColor = vec3f(0f); + var color = vec3f(0f); if uniforms.frameCount == 0u { accumulationBuffer[sampleBufferIdx] = sample; - outputColor = currentColor; + color = currentColor; } else { let depth = textureLoad(gbufferDepth, textureIdx, 0); let previousUv = cameraReproject(uv, depth); - let previousTextureIdx = vec2u(floor(previousUv * uniforms.framebufferSize)); - let previousSampleBufferIdx = previousTextureIdx.y * u32(uniforms.framebufferSize.x) + previousTextureIdx.x; - let previousSample: array = accumulationBuffer[previousSampleBufferIdx]; - let previousColor = vec3f(previousSample[0], previousSample[1], previousSample[2]); - outputColor = 0.1 * currentColor + 0.9 * previousColor; - accumulationBuffer[sampleBufferIdx] = array(outputColor.r, outputColor.g, outputColor.b); + if previousUv.x >= 0f && previousUv.x < 1f && previousUv.y >= 0f && previousUv.y < 1f { + let previousTextureIdx = vec2u(floor(previousUv * uniforms.framebufferSize)); + let previousSampleBufferIdx = previousTextureIdx.y * u32(uniforms.framebufferSize.x) + previousTextureIdx.x; + let previousSample: array = accumulationBuffer[previousSampleBufferIdx]; + let previousColor = vec3f(previousSample[0], previousSample[1], previousSample[2]); + color = 0.1 * currentColor + 0.9 * previousColor; + accumulationBuffer[sampleBufferIdx] = array(color.r, color.g, color.b); + } else { + accumulationBuffer[sampleBufferIdx] = sample; + color = currentColor; + } } - let rgb = acesFilmic(uniforms.exposure * outputColor); + let rgb = acesFilmic(uniforms.exposure * color); let srgb = pow(rgb, vec3(1f / 2.2f)); return vec4(srgb, 1f); } diff --git a/src/pt/shader_source.hpp b/src/pt/shader_source.hpp index 28cd9c0..7fdd594 100644 --- a/src/pt/shader_source.hpp +++ b/src/pt/shader_source.hpp @@ -1321,22 +1321,27 @@ fn fsMain(in: VertexOutput) -> @location(0) vec4f { let sample: array = sampleBuffer[sampleBufferIdx]; let currentColor = vec3f(sample[0], sample[1], sample[2]); - var outputColor = vec3f(0f); + var color = vec3f(0f); if uniforms.frameCount == 0u { accumulationBuffer[sampleBufferIdx] = sample; - outputColor = currentColor; + color = currentColor; } else { let depth = textureLoad(gbufferDepth, textureIdx, 0); let previousUv = cameraReproject(uv, depth); - let previousTextureIdx = vec2u(floor(previousUv * uniforms.framebufferSize)); - let previousSampleBufferIdx = previousTextureIdx.y * u32(uniforms.framebufferSize.x) + previousTextureIdx.x; - let previousSample: array = accumulationBuffer[previousSampleBufferIdx]; - let previousColor = vec3f(previousSample[0], previousSample[1], previousSample[2]); - outputColor = 0.1 * currentColor + 0.9 * previousColor; - accumulationBuffer[sampleBufferIdx] = array(outputColor.r, outputColor.g, outputColor.b); + if previousUv.x >= 0f && previousUv.x < 1f && previousUv.y >= 0f && previousUv.y < 1f { + let previousTextureIdx = vec2u(floor(previousUv * uniforms.framebufferSize)); + let previousSampleBufferIdx = previousTextureIdx.y * u32(uniforms.framebufferSize.x) + previousTextureIdx.x; + let previousSample: array = accumulationBuffer[previousSampleBufferIdx]; + let previousColor = vec3f(previousSample[0], previousSample[1], previousSample[2]); + color = 0.1 * currentColor + 0.9 * previousColor; + accumulationBuffer[sampleBufferIdx] = array(color.r, color.g, color.b); + } else { + accumulationBuffer[sampleBufferIdx] = sample; + color = currentColor; + } } - let rgb = acesFilmic(uniforms.exposure * outputColor); + let rgb = acesFilmic(uniforms.exposure * color); let srgb = pow(rgb, vec3(1f / 2.2f)); return vec4(srgb, 1f); }