From b34ab9b377152bbf6cccdfb22b8f0ab78c0b1d41 Mon Sep 17 00:00:00 2001 From: Johann Muszynski Date: Thu, 28 Mar 2024 09:33:25 +0200 Subject: [PATCH] Visualize depth buffer in debug pass --- src/pt/hybrid_renderer.cpp | 19 +++++++++++-------- src/pt/hybrid_renderer_debug_pass.wgsl | 10 ++++++++-- src/pt/shader_source.hpp | 10 ++++++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/pt/hybrid_renderer.cpp b/src/pt/hybrid_renderer.cpp index e9a0677..7cf316c 100644 --- a/src/pt/hybrid_renderer.cpp +++ b/src/pt/hybrid_renderer.cpp @@ -80,7 +80,7 @@ HybridRenderer::HybridRenderer( const WGPUTextureDescriptor depthTextureDesc{ .nextInChain = nullptr, .label = "Depth texture", - .usage = WGPUTextureUsage_RenderAttachment, + .usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding, .dimension = WGPUTextureDimension_2D, .size = {rendererDesc.framebufferSize.x, rendererDesc.framebufferSize.y, 1}, .format = DEPTH_TEXTURE_FORMAT, @@ -134,17 +134,19 @@ HybridRenderer::HybridRenderer( mGbufferBindGroupLayout = GpuBindGroupLayout{ gpuContext.device, "Gbuffer bind group layout", - std::array{ + std::array{ textureBindGroupLayoutEntry(0, WGPUTextureSampleType_UnfilterableFloat), - textureBindGroupLayoutEntry(1, WGPUTextureSampleType_UnfilterableFloat)}}; + textureBindGroupLayoutEntry(1, WGPUTextureSampleType_UnfilterableFloat), + textureBindGroupLayoutEntry(2, WGPUTextureSampleType_Depth)}}; mGbufferBindGroup = GpuBindGroup{ gpuContext.device, "Gbuffer bind group", mGbufferBindGroupLayout.ptr(), - std::array{ + std::array{ textureBindGroupEntry(0, mAlbedoTextureView), - textureBindGroupEntry(1, mNormalTextureView)}}; + textureBindGroupEntry(1, mNormalTextureView), + textureBindGroupEntry(2, mDepthTextureView)}}; mDebugPass = DebugPass{gpuContext, mGbufferBindGroupLayout, rendererDesc.framebufferSize}; } @@ -227,7 +229,7 @@ void HybridRenderer::resize(const GpuContext& gpuContext, const Extent2u& newSiz const WGPUTextureDescriptor depthTextureDesc{ .nextInChain = nullptr, .label = "Depth texture", - .usage = WGPUTextureUsage_RenderAttachment, + .usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding, .dimension = WGPUTextureDimension_2D, .size = {newSize.x, newSize.y, 1}, .format = DEPTH_TEXTURE_FORMAT, @@ -282,9 +284,10 @@ void HybridRenderer::resize(const GpuContext& gpuContext, const Extent2u& newSiz gpuContext.device, "Gbuffer bind group", mGbufferBindGroupLayout.ptr(), - std::array{ + std::array{ textureBindGroupEntry(0, mAlbedoTextureView), - textureBindGroupEntry(1, mNormalTextureView)}}; + textureBindGroupEntry(1, mNormalTextureView), + textureBindGroupEntry(2, mDepthTextureView)}}; } HybridRenderer::GbufferPass::GbufferPass( diff --git a/src/pt/hybrid_renderer_debug_pass.wgsl b/src/pt/hybrid_renderer_debug_pass.wgsl index 4c2d03c..cbc0721 100644 --- a/src/pt/hybrid_renderer_debug_pass.wgsl +++ b/src/pt/hybrid_renderer_debug_pass.wgsl @@ -20,15 +20,21 @@ fn vsMain(in: VertexInput) -> VertexOutput { @group(0) @binding(0) var framebufferSize: vec2f; @group(1) @binding(0) var gbufferAlbedo: texture_2d; @group(1) @binding(1) var gbufferNormal: texture_2d; +@group(1) @binding(2) var gbufferDepth: texture_depth_2d; @fragment fn fsMain(in: VertexOutput) -> @location(0) vec4f { let c = in.texCoord; let idx = vec2u(floor(c * framebufferSize)); - if c.x < 0.5 { + if c.x < 0.333 { return textureLoad(gbufferAlbedo, idx, 0); - } else { + } else if c.x < 0.666 { let n = textureLoad(gbufferNormal, idx, 0); return vec4(vec3(0.5) * (n.xyz + vec3(1f)), 1.0); + } else { + let d = vec3(1.0) - textureLoad(gbufferDepth, idx, 0); + let x = d; + let a = 0.05; + return vec4((1.0 + a) * x / (x + vec3(a)), 1.0); } } diff --git a/src/pt/shader_source.hpp b/src/pt/shader_source.hpp index 02d984e..198045d 100644 --- a/src/pt/shader_source.hpp +++ b/src/pt/shader_source.hpp @@ -759,16 +759,22 @@ fn vsMain(in: VertexInput) -> VertexOutput { @group(0) @binding(0) var framebufferSize: vec2f; @group(1) @binding(0) var gbufferAlbedo: texture_2d; @group(1) @binding(1) var gbufferNormal: texture_2d; +@group(1) @binding(2) var gbufferDepth: texture_depth_2d; @fragment fn fsMain(in: VertexOutput) -> @location(0) vec4f { let c = in.texCoord; let idx = vec2u(floor(c * framebufferSize)); - if c.x < 0.5 { + if c.x < 0.333 { return textureLoad(gbufferAlbedo, idx, 0); - } else { + } else if c.x < 0.666 { let n = textureLoad(gbufferNormal, idx, 0); return vec4(vec3(0.5) * (n.xyz + vec3(1f)), 1.0); + } else { + let d = vec3(1.0) - textureLoad(gbufferDepth, idx, 0); + let x = d; + let a = 0.05; + return vec4((1.0 + a) * x / (x + vec3(a)), 1.0); } } )";