Skip to content

Commit

Permalink
wip jitter matrix logistics
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Jun 28, 2024
1 parent b699b07 commit e131675
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
26 changes: 17 additions & 9 deletions src/pt/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ WGPUTextureView createGbufferTextureView(
};
return wgpuTextureCreateView(texture, &desc);
}

glm::mat4 jitterMatrix(const Extent2f& framebufferSize, const std::uint32_t frameCount)
{
glm::mat4 jitterMat = glm::mat4(1.0f);
const glm::vec2 j = r2Sequence(frameCount, 1 << 20);
jitterMat[3][0] = (j.x - 0.5f) / framebufferSize.x;
jitterMat[3][1] = (j.y - 0.5f) / framebufferSize.y;
return jitterMat;
}
} // namespace

DeferredRenderer::DeferredRenderer(
Expand Down Expand Up @@ -299,13 +308,7 @@ void DeferredRenderer::render(

const Extent2f framebufferSize = Extent2f(renderDesc.framebufferSize);
const std::uint32_t frameCount = mFrameCount++;
const glm::mat4 jitterMat = [framebufferSize, frameCount]() -> glm::mat4 {
glm::mat4 jitterMat = glm::mat4(1.0f);
const glm::vec2 j = r2Sequence(frameCount, 1 << 20);
jitterMat[3][0] = (j.x - 0.5f) / framebufferSize.x;
jitterMat[3][1] = (j.y - 0.5f) / framebufferSize.y;
return jitterMat;
}();
const glm::mat4 jitterMat = jitterMatrix(framebufferSize, frameCount);

// GBuffer pass

Expand All @@ -315,7 +318,8 @@ void DeferredRenderer::render(
offsetof(TimestampsLayout, gbufferPassStart) / TimestampsLayout::MEMBER_SIZE);
mGbufferPass.render(
gpuContext,
jitterMat * renderDesc.viewReverseZProjectionMatrix,
renderDesc.viewReverseZProjectionMatrix,
jitterMat,
encoder,
mDepthTextureView,
mAlbedoTextureView,
Expand Down Expand Up @@ -457,9 +461,12 @@ void DeferredRenderer::renderDebug(
return wgpuDeviceCreateCommandEncoder(gpuContext.device, &cmdEncoderDesc);
}();

const glm::mat4 jitterMat = jitterMatrix(framebufferSize, mFrameCount);

mGbufferPass.render(
gpuContext,
viewProjectionMat,
jitterMat,
encoder,
mDepthTextureView,
mAlbedoTextureView,
Expand Down Expand Up @@ -1008,12 +1015,13 @@ DeferredRenderer::GbufferPass& DeferredRenderer::GbufferPass::operator=(
void DeferredRenderer::GbufferPass::render(
const GpuContext& gpuContext,
const glm::mat4& viewReverseZProjectionMatrix,
const glm::mat4& jitterMat,
const WGPUCommandEncoder cmdEncoder,
const WGPUTextureView depthTextureView,
const WGPUTextureView albedoTextureView,
const WGPUTextureView normalTextureView)
{
const Uniforms uniforms{viewReverseZProjectionMatrix};
const Uniforms uniforms{viewReverseZProjectionMatrix, jitterMat};
wgpuQueueWriteBuffer(gpuContext.queue, mUniformBuffer.ptr(), 0, &uniforms, sizeof(Uniforms));

const WGPURenderPassEncoder renderPassEncoder = [cmdEncoder,
Expand Down
2 changes: 2 additions & 0 deletions src/pt/deferred_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class DeferredRenderer
struct Uniforms
{
glm::mat4 viewProjectionMat;
glm::mat4 jitterMat;
};

public:
Expand All @@ -125,6 +126,7 @@ class DeferredRenderer
void render(
const GpuContext& gpuContext,
const glm::mat4& viewProjectionMat,
const glm::mat4& jitterMat,
WGPUCommandEncoder cmdEncoder,
WGPUTextureView depthTextureView,
WGPUTextureView albedoTextureView,
Expand Down
5 changes: 3 additions & 2 deletions src/pt/deferred_renderer_gbuffer_pass.wgsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
struct Uniforms {
viewReverseZProjectionMat: mat4x4f
viewReverseZProjectionMat: mat4x4f,
jitterMat: mat4x4f
}

@group(0) @binding(0) var<uniform> uniforms: Uniforms;
Expand All @@ -19,7 +20,7 @@ struct VertexOutput {
@vertex
fn vsMain(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.position = uniforms.viewReverseZProjectionMat * in.position;
out.position = uniforms.jitterMat * uniforms.viewReverseZProjectionMat * in.position;
out.normal = in.normal;
out.texCoord = in.texCoord;
return out;
Expand Down
5 changes: 3 additions & 2 deletions src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,8 @@ fn animatedBlueNoise(coord: vec2u, frameIdx: u32, totalSampleCount: u32) -> vec2
)";

const char* const DEFERRED_RENDERER_GBUFFER_PASS_SOURCE = R"(struct Uniforms {
viewReverseZProjectionMat: mat4x4f
viewReverseZProjectionMat: mat4x4f,
jitterMat: mat4x4f
}
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
Expand All @@ -644,7 +645,7 @@ struct VertexOutput {
@vertex
fn vsMain(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.position = uniforms.viewReverseZProjectionMat * in.position;
out.position = uniforms.jitterMat * uniforms.viewReverseZProjectionMat * in.position;
out.normal = in.normal;
out.texCoord = in.texCoord;
return out;
Expand Down

0 comments on commit e131675

Please sign in to comment.