Skip to content

Commit

Permalink
Visualize depth buffer in debug pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Mar 28, 2024
1 parent bdc371f commit b34ab9b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/pt/hybrid_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -134,17 +134,19 @@ HybridRenderer::HybridRenderer(
mGbufferBindGroupLayout = GpuBindGroupLayout{
gpuContext.device,
"Gbuffer bind group layout",
std::array<WGPUBindGroupLayoutEntry, 2>{
std::array<WGPUBindGroupLayoutEntry, 3>{
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<WGPUBindGroupEntry, 2>{
std::array<WGPUBindGroupEntry, 3>{
textureBindGroupEntry(0, mAlbedoTextureView),
textureBindGroupEntry(1, mNormalTextureView)}};
textureBindGroupEntry(1, mNormalTextureView),
textureBindGroupEntry(2, mDepthTextureView)}};

mDebugPass = DebugPass{gpuContext, mGbufferBindGroupLayout, rendererDesc.framebufferSize};
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -282,9 +284,10 @@ void HybridRenderer::resize(const GpuContext& gpuContext, const Extent2u& newSiz
gpuContext.device,
"Gbuffer bind group",
mGbufferBindGroupLayout.ptr(),
std::array<WGPUBindGroupEntry, 2>{
std::array<WGPUBindGroupEntry, 3>{
textureBindGroupEntry(0, mAlbedoTextureView),
textureBindGroupEntry(1, mNormalTextureView)}};
textureBindGroupEntry(1, mNormalTextureView),
textureBindGroupEntry(2, mDepthTextureView)}};
}

HybridRenderer::GbufferPass::GbufferPass(
Expand Down
10 changes: 8 additions & 2 deletions src/pt/hybrid_renderer_debug_pass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ fn vsMain(in: VertexInput) -> VertexOutput {
@group(0) @binding(0) var<uniform> framebufferSize: vec2f;
@group(1) @binding(0) var gbufferAlbedo: texture_2d<f32>;
@group(1) @binding(1) var gbufferNormal: texture_2d<f32>;
@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);
}
}
10 changes: 8 additions & 2 deletions src/pt/shader_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,16 +759,22 @@ fn vsMain(in: VertexInput) -> VertexOutput {
@group(0) @binding(0) var<uniform> framebufferSize: vec2f;
@group(1) @binding(0) var gbufferAlbedo: texture_2d<f32>;
@group(1) @binding(1) var gbufferNormal: texture_2d<f32>;
@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);
}
}
)";
Expand Down

0 comments on commit b34ab9b

Please sign in to comment.