Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Jun 23, 2024
1 parent d06c8c9 commit fd39bd0
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 25 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ set(TESTS_SOURCE_FILES
bvh.cpp
gltf.cpp
intersection.cpp
math.cpp
pt_format.cpp
stream.cpp
vector_set.cpp)
Expand Down
18 changes: 18 additions & 0 deletions src/common/math.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <cmath>

namespace nlrs
{
inline float fract(const float x)
{
if (x >= 0.0f)
{
return x - std::floor(x);
}
else
{
return x - std::ceil(x);
}
}
} // namespace nlrs
46 changes: 29 additions & 17 deletions src/pt/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ DeferredRenderer::DeferredRenderer(
mLightingPass(),
mResolvePass(),
mGbufferPassDurationsNs(),
mLightingPassDurationsNs()
mLightingPassDurationsNs(),
mFrameCount(0)
{
{
const std::array<WGPUTextureFormat, 1> depthFormats{
Expand Down Expand Up @@ -240,6 +241,7 @@ DeferredRenderer::DeferredRenderer(DeferredRenderer&& other)
mResolvePass = std::move(other.mResolvePass);
mGbufferPassDurationsNs = std::move(other.mGbufferPassDurationsNs);
mLightingPassDurationsNs = std::move(other.mLightingPassDurationsNs);
mFrameCount = other.mFrameCount;
}
}

Expand Down Expand Up @@ -270,6 +272,7 @@ DeferredRenderer& DeferredRenderer::operator=(DeferredRenderer&& other)
mResolvePass = std::move(other.mResolvePass);
mGbufferPassDurationsNs = std::move(other.mGbufferPassDurationsNs);
mLightingPassDurationsNs = std::move(other.mLightingPassDurationsNs);
mFrameCount = other.mFrameCount;
}
return *this;
}
Expand All @@ -293,6 +296,10 @@ void DeferredRenderer::render(
return wgpuDeviceCreateCommandEncoder(gpuContext.device, &cmdEncoderDesc);
}();

const std::uint32_t frameCount = mFrameCount++;

// GBuffer pass

wgpuCommandEncoderWriteTimestamp(
encoder,
mQuerySet,
Expand All @@ -303,12 +310,15 @@ void DeferredRenderer::render(
encoder,
mDepthTextureView,
mAlbedoTextureView,
mNormalTextureView);
mNormalTextureView,
frameCount);
wgpuCommandEncoderWriteTimestamp(
encoder,
mQuerySet,
offsetof(TimestampsLayout, gbufferPassEnd) / TimestampsLayout::MEMBER_SIZE);

// Lighting pass

wgpuCommandEncoderWriteTimestamp(
encoder,
mQuerySet,
Expand All @@ -323,13 +333,16 @@ void DeferredRenderer::render(
inverseViewProjectionMat,
renderDesc.cameraPosition,
framebufferSize,
renderDesc.sky);
renderDesc.sky,
frameCount);
}
wgpuCommandEncoderWriteTimestamp(
encoder,
mQuerySet,
offsetof(TimestampsLayout, lightingPassEnd) / TimestampsLayout::MEMBER_SIZE);

// Resolve pass

wgpuCommandEncoderWriteTimestamp(
encoder,
mQuerySet,
Expand All @@ -348,6 +361,8 @@ void DeferredRenderer::render(
mQuerySet,
offsetof(TimestampsLayout, resolvePassEnd) / TimestampsLayout::MEMBER_SIZE);

// Resolve timestamp queries

wgpuCommandEncoderResolveQuerySet(
encoder, mQuerySet, 0, TimestampsLayout::QUERY_COUNT, mQueryBuffer.ptr(), 0);
wgpuCommandEncoderCopyBufferToBuffer(
Expand Down Expand Up @@ -440,7 +455,8 @@ void DeferredRenderer::renderDebug(
encoder,
mDepthTextureView,
mAlbedoTextureView,
mNormalTextureView);
mNormalTextureView,
mFrameCount++);

mDebugPass.render(gpuContext, encoder, textureView, framebufferSize, gui);

Expand Down Expand Up @@ -676,15 +692,15 @@ DeferredRenderer::GbufferPass::GbufferPass(
gpuContext.device,
"Uniform buffer",
{GpuBufferUsage::Uniform, GpuBufferUsage::CopyDst},
sizeof(glm::mat4)),
sizeof(Uniforms)),
mUniformBindGroup(),
mSamplerBindGroup(),
mPipeline(nullptr)
{
const GpuBindGroupLayout uniformBindGroupLayout{
gpuContext.device,
"Uniform bind group layout",
mUniformBuffer.bindGroupLayoutEntry(0, WGPUShaderStage_Vertex, sizeof(glm::mat4))};
mUniformBuffer.bindGroupLayoutEntry(0, WGPUShaderStage_Vertex, sizeof(Uniforms))};

mUniformBindGroup = GpuBindGroup{
gpuContext.device,
Expand Down Expand Up @@ -988,14 +1004,11 @@ void DeferredRenderer::GbufferPass::render(
const WGPUCommandEncoder cmdEncoder,
const WGPUTextureView depthTextureView,
const WGPUTextureView albedoTextureView,
const WGPUTextureView normalTextureView)
const WGPUTextureView normalTextureView,
const std::uint32_t /*frameCount*/)
{
wgpuQueueWriteBuffer(
gpuContext.queue,
mUniformBuffer.ptr(),
0,
&viewReverseZProjectionMatrix[0][0],
sizeof(glm::mat4));
const Uniforms uniforms{viewReverseZProjectionMatrix};
wgpuQueueWriteBuffer(gpuContext.queue, mUniformBuffer.ptr(), 0, &uniforms, sizeof(Uniforms));

const WGPURenderPassEncoder renderPassEncoder = [cmdEncoder,
depthTextureView,
Expand Down Expand Up @@ -1644,7 +1657,6 @@ DeferredRenderer::LightingPass::LightingPass(LightingPass&& other) noexcept
mSampleBindGroup = std::move(other.mSampleBindGroup);
mPipeline = other.mPipeline;
other.mPipeline = nullptr;
mFrameCount = other.mFrameCount;
}
}

Expand All @@ -1670,7 +1682,6 @@ DeferredRenderer::LightingPass& DeferredRenderer::LightingPass::operator=(
computePipelineSafeRelease(mPipeline);
mPipeline = other.mPipeline;
other.mPipeline = nullptr;
mFrameCount = other.mFrameCount;
}
return *this;
}
Expand All @@ -1681,7 +1692,8 @@ void DeferredRenderer::LightingPass::render(
const glm::mat4& inverseViewReverseZProjectionMatrix,
const glm::vec3& cameraPosition,
const Extent2f& fbsize,
const Sky& sky)
const Sky& sky,
const std::uint32_t frameCount)
{
if (mCurrentSky != sky)
{
Expand All @@ -1696,7 +1708,7 @@ void DeferredRenderer::LightingPass::render(
inverseViewReverseZProjectionMatrix,
glm::vec4(cameraPosition, 1.f),
glm::vec2(fbsize.x, fbsize.y),
mFrameCount++,
frameCount,
0};
wgpuQueueWriteBuffer(
gpuContext.queue, mUniformBuffer.ptr(), 0, &uniforms, sizeof(Uniforms));
Expand Down
14 changes: 10 additions & 4 deletions src/pt/deferred_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ class DeferredRenderer
GpuBindGroup mSamplerBindGroup{};
WGPURenderPipeline mPipeline = nullptr;

struct Uniforms
{
glm::mat4 viewProjectionMat;
};

public:
GbufferPass() = default;
GbufferPass(const GpuContext&, const DeferredRendererDescriptor&);
Expand All @@ -123,7 +128,8 @@ class DeferredRenderer
WGPUCommandEncoder cmdEncoder,
WGPUTextureView depthTextureView,
WGPUTextureView albedoTextureView,
WGPUTextureView normalTextureView);
WGPUTextureView normalTextureView,
std::uint32_t frameCount);
};

struct DebugPass
Expand Down Expand Up @@ -184,8 +190,6 @@ class DeferredRenderer
GpuBindGroup mSampleBindGroup = GpuBindGroup{};
WGPUComputePipeline mPipeline = nullptr;

std::uint32_t mFrameCount = 0;

struct Uniforms
{
glm::mat4 inverseViewReverseZProjectionMat;
Expand Down Expand Up @@ -221,7 +225,8 @@ class DeferredRenderer
const glm::mat4& inverseViewProjection,
const glm::vec3& cameraPosition,
const Extent2f& framebufferSize,
const Sky& sky);
const Sky& sky,
std::uint32_t frameCount);
void resize(
const GpuContext&,
WGPUTextureView albedoTextureView,
Expand Down Expand Up @@ -286,5 +291,6 @@ class DeferredRenderer
std::deque<std::uint64_t> mGbufferPassDurationsNs;
std::deque<std::uint64_t> mLightingPassDurationsNs;
std::deque<std::uint64_t> mResolvePassDurationsNs;
std::uint32_t mFrameCount;
};
} // namespace nlrs
4 changes: 2 additions & 2 deletions src/pt/deferred_renderer_gbuffer_pass.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@group(0) @binding(0) var<uniform> viewProjectionMat: mat4x4f;
@group(0) @binding(0) var<uniform> viewReverseZProjectionMat: mat4x4f;

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

const char* const DEFERRED_RENDERER_GBUFFER_PASS_SOURCE = R"(@group(0) @binding(0) var<uniform> viewProjectionMat: mat4x4f;
const char* const DEFERRED_RENDERER_GBUFFER_PASS_SOURCE = R"(@group(0) @binding(0) var<uniform> viewReverseZProjectionMat: mat4x4f;
struct VertexInput {
@location(0) position: vec4f,
Expand All @@ -640,7 +640,7 @@ struct VertexOutput {
@vertex
fn vsMain(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.position = viewProjectionMat * in.position;
out.position = viewReverseZProjectionMat * in.position;
out.normal = in.normal;
out.texCoord = in.texCoord;
return out;
Expand Down
14 changes: 14 additions & 0 deletions src/tests/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <common/math.hpp>

#include <catch2/catch_test_macros.hpp>

TEST_CASE("Fract", "[math]")
{
REQUIRE(nlrs::fract(0.0f) == 0.0f);
REQUIRE(nlrs::fract(1.0f) == 0.0f);
REQUIRE(nlrs::fract(1.5f) == 0.5f);
REQUIRE(nlrs::fract(-1.5f) == -0.5f);
REQUIRE(nlrs::fract(-1.0f) == -0.0f);
REQUIRE(nlrs::fract(-0.5f) == -0.5f);
REQUIRE(nlrs::fract(-0.0f) == -0.0f);
}

0 comments on commit fd39bd0

Please sign in to comment.