Skip to content

Commit

Permalink
temporal accumulation with EMA
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Jun 24, 2024
1 parent a664308 commit 3cbb12d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/pt/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ void DeferredRenderer::render(
renderDesc.targetTextureView,
framebufferSize,
renderDesc.exposure,
frameCount,
gui);
}
wgpuCommandEncoderWriteTimestamp(
Expand Down Expand Up @@ -1970,10 +1971,11 @@ void DeferredRenderer::ResolvePass::render(
WGPUTextureView targetTextureView,
const Extent2f& fbsize,
const float exposure,
const std::uint32_t frameCount,
Gui& gui)
{
{
const Uniforms uniforms{glm::vec2(fbsize.x, fbsize.y), exposure, 0.f};
const Uniforms uniforms{glm::vec2(fbsize.x, fbsize.y), exposure, frameCount};
wgpuQueueWriteBuffer(
gpuContext.queue, mUniformBuffer.ptr(), 0, &uniforms, sizeof(Uniforms));
}
Expand Down
7 changes: 4 additions & 3 deletions src/pt/deferred_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ class DeferredRenderer

struct Uniforms
{
glm::vec2 framebufferSize;
float exposure;
float _padding;
glm::vec2 framebufferSize;
float exposure;
std::uint32_t frameCount;
};

public:
Expand All @@ -270,6 +270,7 @@ class DeferredRenderer
WGPUTextureView targetTextureView,
const Extent2f& framebufferSize,
float exposure,
std::uint32_t frameCount,
Gui& gui);
};

Expand Down
13 changes: 12 additions & 1 deletion src/pt/deferred_renderer_resolve_pass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn vsMain(in: VertexInput) -> VertexOutput {
struct Uniforms {
framebufferSize: vec2f,
exposure: f32,
frameCount: u32,
}

// TODO: consider merging these into one bind group
Expand All @@ -35,7 +36,17 @@ 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 color = vec3f(sample[0], sample[1], sample[2]);
let currentColor = vec3f(sample[0], sample[1], sample[2]);
var color = vec3f(0f);
if uniforms.frameCount == 0u {
accumulationBuffer[sampleBufferIdx] = sample;
color = currentColor;
} else {
let previousSample: array<f32, 3> = accumulationBuffer[sampleBufferIdx];
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);
}

let rgb = acesFilmic(uniforms.exposure * color);
let srgb = pow(rgb, vec3(1f / 2.2f));
Expand Down
13 changes: 12 additions & 1 deletion src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@ fn vsMain(in: VertexInput) -> VertexOutput {
struct Uniforms {
framebufferSize: vec2f,
exposure: f32,
frameCount: u32,
}
// TODO: consider merging these into one bind group
Expand All @@ -1310,7 +1311,17 @@ 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 color = vec3f(sample[0], sample[1], sample[2]);
let currentColor = vec3f(sample[0], sample[1], sample[2]);
var color = vec3f(0f);
if uniforms.frameCount == 0u {
accumulationBuffer[sampleBufferIdx] = sample;
color = currentColor;
} else {
let previousSample: array<f32, 3> = accumulationBuffer[sampleBufferIdx];
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);
}
let rgb = acesFilmic(uniforms.exposure * color);
let srgb = pow(rgb, vec3(1f / 2.2f));
Expand Down

0 comments on commit 3cbb12d

Please sign in to comment.