Skip to content

Commit

Permalink
wip pass depth texture to resolve pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Jul 5, 2024
1 parent 3d3a8e6 commit 89b7cd0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
51 changes: 42 additions & 9 deletions src/pt/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ DeferredRenderer::DeferredRenderer(
rendererDesc.scenePositionAttributes,
rendererDesc.sceneVertexAttributes,
rendererDesc.sceneBaseColorTextures};
mResolvePass = ResolvePass{gpuContext, mSampleBuffer, rendererDesc};
mResolvePass = ResolvePass{gpuContext, mSampleBuffer, mDepthTextureView, rendererDesc};
}

DeferredRenderer::~DeferredRenderer()
Expand Down Expand Up @@ -558,6 +558,7 @@ void DeferredRenderer::resize(const GpuContext& gpuContext, const Extent2u& newS

mDebugPass.resize(gpuContext, mAlbedoTextureView, mNormalTextureView, mDepthTextureView);
mLightingPass.resize(gpuContext, mAlbedoTextureView, mNormalTextureView, mDepthTextureView);
mResolvePass.resize(gpuContext, mDepthTextureView);

invalidateTemporalAccumulation();
}
Expand Down Expand Up @@ -1767,6 +1768,7 @@ void DeferredRenderer::LightingPass::resize(
DeferredRenderer::ResolvePass::ResolvePass(
const GpuContext& gpuContext,
const GpuBuffer& sampleBuffer,
WGPUTextureView depthTextureView,
const DeferredRendererDescriptor& rendererDesc)
: mVertexBuffer{gpuContext.device, "Resolve pass vertex buffer", {GpuBufferUsage::Vertex, GpuBufferUsage::CopyDst}, std::span<const float[2]>(quadVertexData)},
mUniformBuffer{
Expand All @@ -1780,7 +1782,9 @@ DeferredRenderer::ResolvePass::ResolvePass(
"Deferred renderer :: accumulation buffer",
GpuBufferUsages{GpuBufferUsage::Storage},
3 * sizeof(float) * area(rendererDesc.maxFramebufferSize)},
mTaaBindGroup{},
mSampleBufferBindGroup{},
mGbufferBindGroupLayout{},
mGbufferBindGroup{},
mPipeline(nullptr),
mPreviousViewProjectionMat(1.f)
{
Expand All @@ -1795,25 +1799,38 @@ DeferredRenderer::ResolvePass::ResolvePass(
uniformBindGroupLayout.ptr(),
mUniformBuffer.bindGroupEntry(0)};

const GpuBindGroupLayout taaBindGroupLayout{
const GpuBindGroupLayout sampleBufferBindGroupLayout{
gpuContext.device,
"TAA bind group layout",
std::array<WGPUBindGroupLayoutEntry, 2>{
sampleBuffer.bindGroupLayoutEntry(0, WGPUShaderStage_Fragment),
mAccumulationBuffer.bindGroupLayoutEntry(1, WGPUShaderStage_Fragment)}};

mTaaBindGroup = GpuBindGroup{
mSampleBufferBindGroup = GpuBindGroup{
gpuContext.device,
"TAA bind group",
taaBindGroupLayout.ptr(),
sampleBufferBindGroupLayout.ptr(),
std::array<WGPUBindGroupEntry, 2>{
sampleBuffer.bindGroupEntry(0), mAccumulationBuffer.bindGroupEntry(1)}};

mGbufferBindGroupLayout = GpuBindGroupLayout{
gpuContext.device,
"Resolve pass gbuffer bind group layout",
textureBindGroupLayoutEntry(0, WGPUTextureSampleType_Depth, WGPUShaderStage_Compute)};

mGbufferBindGroup = GpuBindGroup{
gpuContext.device,
"Resolve pass gbuffer bind group",
mGbufferBindGroupLayout.ptr(),
textureBindGroupEntry(0, depthTextureView)};

{
// Pipeline layout

const WGPUBindGroupLayout bindGroupLayouts[] = {
uniformBindGroupLayout.ptr(), taaBindGroupLayout.ptr()};
uniformBindGroupLayout.ptr(),
sampleBufferBindGroupLayout.ptr(),
mGbufferBindGroupLayout.ptr()};

const WGPUPipelineLayoutDescriptor pipelineLayoutDesc{
.nextInChain = nullptr,
Expand Down Expand Up @@ -1948,7 +1965,9 @@ DeferredRenderer::ResolvePass::ResolvePass(ResolvePass&& other) noexcept
mUniformBuffer = std::move(other.mUniformBuffer);
mUniformBindGroup = std::move(other.mUniformBindGroup);
mAccumulationBuffer = std::move(other.mAccumulationBuffer);
mTaaBindGroup = std::move(other.mTaaBindGroup);
mSampleBufferBindGroup = std::move(other.mSampleBufferBindGroup);
mGbufferBindGroupLayout = std::move(other.mGbufferBindGroupLayout);
mGbufferBindGroup = std::move(other.mGbufferBindGroup);
mPipeline = other.mPipeline;
other.mPipeline = nullptr;
mPreviousViewProjectionMat = other.mPreviousViewProjectionMat;
Expand All @@ -1964,7 +1983,9 @@ DeferredRenderer::ResolvePass& DeferredRenderer::ResolvePass::operator=(
mUniformBuffer = std::move(other.mUniformBuffer);
mUniformBindGroup = std::move(other.mUniformBindGroup);
mAccumulationBuffer = std::move(other.mAccumulationBuffer);
mTaaBindGroup = std::move(other.mTaaBindGroup);
mSampleBufferBindGroup = std::move(other.mSampleBufferBindGroup);
mGbufferBindGroupLayout = std::move(other.mGbufferBindGroupLayout);
mGbufferBindGroup = std::move(other.mGbufferBindGroup);
renderPipelineSafeRelease(mPipeline);
mPipeline = other.mPipeline;
other.mPipeline = nullptr;
Expand Down Expand Up @@ -2030,7 +2051,8 @@ void DeferredRenderer::ResolvePass::render(

wgpuRenderPassEncoderSetPipeline(renderPass, mPipeline);
wgpuRenderPassEncoderSetBindGroup(renderPass, 0, mUniformBindGroup.ptr(), 0, nullptr);
wgpuRenderPassEncoderSetBindGroup(renderPass, 1, mTaaBindGroup.ptr(), 0, nullptr);
wgpuRenderPassEncoderSetBindGroup(renderPass, 1, mSampleBufferBindGroup.ptr(), 0, nullptr);
wgpuRenderPassEncoderSetBindGroup(renderPass, 2, mGbufferBindGroup.ptr(), 0, nullptr);
wgpuRenderPassEncoderSetVertexBuffer(
renderPass, 0, mVertexBuffer.ptr(), 0, mVertexBuffer.byteSize());
wgpuRenderPassEncoderDraw(renderPass, 6, 1, 0, 0);
Expand All @@ -2040,6 +2062,17 @@ void DeferredRenderer::ResolvePass::render(
wgpuRenderPassEncoderEnd(renderPass);
}

void DeferredRenderer::ResolvePass::resize(
const GpuContext& gpuContext,
const WGPUTextureView depthTextureView)
{
mGbufferBindGroup = GpuBindGroup{
gpuContext.device,
"Resolve pass gbuffer bind group",
mGbufferBindGroupLayout.ptr(),
textureBindGroupEntry(0, depthTextureView)};
}

DeferredRenderer::PerfStats DeferredRenderer::getPerfStats() const
{
NLRS_ASSERT(mGbufferPassDurationsNs.size() == mLightingPassDurationsNs.size());
Expand Down
6 changes: 5 additions & 1 deletion src/pt/deferred_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ class DeferredRenderer
GpuBuffer mUniformBuffer = GpuBuffer{};
GpuBindGroup mUniformBindGroup = GpuBindGroup{};
GpuBuffer mAccumulationBuffer = GpuBuffer{};
GpuBindGroup mTaaBindGroup = GpuBindGroup{};
GpuBindGroup mSampleBufferBindGroup = GpuBindGroup{};
GpuBindGroupLayout mGbufferBindGroupLayout = GpuBindGroupLayout{};
GpuBindGroup mGbufferBindGroup = GpuBindGroup{};
WGPURenderPipeline mPipeline = nullptr;
glm::mat4 mPreviousViewProjectionMat;

Expand All @@ -258,6 +260,7 @@ class DeferredRenderer
ResolvePass(
const GpuContext& gpuContext,
const GpuBuffer& sampleBuffer,
WGPUTextureView depthTextureView,
const DeferredRendererDescriptor& desc);
~ResolvePass();

Expand All @@ -276,6 +279,7 @@ class DeferredRenderer
float exposure,
std::uint32_t frameCount,
Gui& gui);
void resize(const GpuContext&, WGPUTextureView depthTextureView);
};

void invalidateTemporalAccumulation();
Expand Down
2 changes: 2 additions & 0 deletions src/pt/deferred_renderer_resolve_pass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct Uniforms {
@group(1) @binding(0) var<storage, read_write> sampleBuffer: array<array<f32, 3>>;
@group(1) @binding(1) var<storage, read_write> accumulationBuffer: array<array<f32, 3>>;

@group(2) @binding(0) var gbufferDepth: texture_depth_2d;

@fragment
fn fsMain(in: VertexOutput) -> @location(0) vec4f {
let uv = in.texCoord;
Expand Down
2 changes: 2 additions & 0 deletions src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,8 @@ struct Uniforms {
@group(1) @binding(0) var<storage, read_write> sampleBuffer: array<array<f32, 3>>;
@group(1) @binding(1) var<storage, read_write> accumulationBuffer: array<array<f32, 3>>;
@group(2) @binding(0) var gbufferDepth: texture_depth_2d;
@fragment
fn fsMain(in: VertexOutput) -> @location(0) vec4f {
let uv = in.texCoord;
Expand Down

0 comments on commit 89b7cd0

Please sign in to comment.