Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
- everything is flickering wildly. lighting pass probably needs the
  jitter matrix as well. consider rolling it into the viewproj matrix
  for those two steps.
  • Loading branch information
Nelarius committed Jun 23, 2024
1 parent fd39bd0 commit 6a425d0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/common/r_sequence.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "math.hpp"

#include <glm/glm.hpp>

#include <cmath>

namespace nlrs
{
inline glm::vec2 r2Sequence(const std::uint32_t n, const std::uint32_t sequenceLength)
{
// https://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/
constexpr float G = 1.32471795f;
constexpr float A1 = 1.0f / G;
constexpr float A2 = 1.0f / (G * G);

const float i = static_cast<float>(n % sequenceLength);
const float x = fract(0.5f + A1 * i);
const float y = fract(0.5f + A2 * i);
return glm::vec2(x, y);
}
} // namespace nlrs
15 changes: 12 additions & 3 deletions src/pt/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "window.hpp"

#include <common/assert.hpp>
#include <common/r_sequence.hpp>

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -296,6 +297,7 @@ void DeferredRenderer::render(
return wgpuDeviceCreateCommandEncoder(gpuContext.device, &cmdEncoderDesc);
}();

const Extent2f framebufferSize = Extent2f(renderDesc.framebufferSize);
const std::uint32_t frameCount = mFrameCount++;

// GBuffer pass
Expand All @@ -311,6 +313,7 @@ void DeferredRenderer::render(
mDepthTextureView,
mAlbedoTextureView,
mNormalTextureView,
framebufferSize,
frameCount);
wgpuCommandEncoderWriteTimestamp(
encoder,
Expand All @@ -323,7 +326,6 @@ void DeferredRenderer::render(
encoder,
mQuerySet,
offsetof(TimestampsLayout, lightingPassStart) / TimestampsLayout::MEMBER_SIZE);
const Extent2f framebufferSize = Extent2f(renderDesc.framebufferSize);
{
const glm::mat4 inverseViewProjectionMat =
glm::inverse(renderDesc.viewReverseZProjectionMatrix);
Expand Down Expand Up @@ -456,6 +458,7 @@ void DeferredRenderer::renderDebug(
mDepthTextureView,
mAlbedoTextureView,
mNormalTextureView,
framebufferSize,
mFrameCount++);

mDebugPass.render(gpuContext, encoder, textureView, framebufferSize, gui);
Expand Down Expand Up @@ -1005,9 +1008,15 @@ void DeferredRenderer::GbufferPass::render(
const WGPUTextureView depthTextureView,
const WGPUTextureView albedoTextureView,
const WGPUTextureView normalTextureView,
const std::uint32_t /*frameCount*/)
const Extent2f& framebufferSize,
const std::uint32_t frameCount)
{
const Uniforms uniforms{viewReverseZProjectionMatrix};
Uniforms uniforms{viewReverseZProjectionMatrix, glm::mat4(1.0f)};
{
const glm::vec2 j = r2Sequence(frameCount, 1 << 20);
uniforms.jitterMat[3][0] = (j.x - 0.5f) / framebufferSize.x;
uniforms.jitterMat[3][1] = (j.y - 0.5f) / framebufferSize.y;
}
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 @@ -129,6 +130,7 @@ class DeferredRenderer
WGPUTextureView depthTextureView,
WGPUTextureView albedoTextureView,
WGPUTextureView normalTextureView,
const Extent2f& framebufferSize,
std::uint32_t frameCount);
};

Expand Down
9 changes: 7 additions & 2 deletions src/pt/deferred_renderer_gbuffer_pass.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
@group(0) @binding(0) var<uniform> viewReverseZProjectionMat: mat4x4f;
struct Uniforms {
viewReverseZProjectionMat: mat4x4f,
jitterMat: mat4x4f
}

@group(0) @binding(0) var<uniform> uniforms: Uniforms;

struct VertexInput {
@location(0) position: vec4f,
Expand All @@ -15,7 +20,7 @@ struct VertexOutput {
@vertex
fn vsMain(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.position = viewReverseZProjectionMat * in.position;
out.position = uniforms.jitterMat * uniforms.viewReverseZProjectionMat * in.position;
out.normal = in.normal;
out.texCoord = in.texCoord;
return out;
Expand Down
9 changes: 7 additions & 2 deletions src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,12 @@ fn animatedBlueNoise(coord: vec2u, frameIdx: u32, totalSampleCount: u32) -> vec2
}
)";

const char* const DEFERRED_RENDERER_GBUFFER_PASS_SOURCE = R"(@group(0) @binding(0) var<uniform> viewReverseZProjectionMat: mat4x4f;
const char* const DEFERRED_RENDERER_GBUFFER_PASS_SOURCE = R"(struct Uniforms {
viewReverseZProjectionMat: mat4x4f,
jitterMat: mat4x4f
}
@group(0) @binding(0) var<uniform> uniforms: Uniforms;
struct VertexInput {
@location(0) position: vec4f,
Expand All @@ -640,7 +645,7 @@ struct VertexOutput {
@vertex
fn vsMain(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.position = 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 6a425d0

Please sign in to comment.