Skip to content

Commit

Permalink
Added debug visualization for intermediate render targets
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeNvidiaGuy committed Mar 1, 2023
1 parent 0905db1 commit b26080b
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 2 deletions.
29 changes: 29 additions & 0 deletions shaders/DebugViz/NDirOctUNorm32Viz.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/***************************************************************************
# Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
**************************************************************************/

#ifndef PACKED_NORMAL_HLSL
#define PACKED_NORMAL_HLSL

#include "../ShaderParameters.h"

#include <donut/shaders/utils.hlsli>

Texture2D<uint> t_Normals : register(t0);
RWTexture2D<float4> t_Output : register(u0);

[numthreads(16, 16, 1)]
void main(uint2 pixelPosition : SV_DispatchThreadID)
{
uint packedNormal = t_Normals[pixelPosition];
float3 unpackedNormal = octToNdirUnorm32(packedNormal);
t_Output[pixelPosition] = float4(unpackedNormal, 1.0);
}

#endif // PACKED_NORMAL_HLSL
29 changes: 29 additions & 0 deletions shaders/DebugViz/PackedR11G11B10UFloatViz.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/***************************************************************************
# Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
**************************************************************************/

#ifndef PACKED_NORMAL_HLSL
#define PACKED_NORMAL_HLSL

#include "../ShaderParameters.h"

#include <donut/shaders/packing.hlsli>

Texture2D<uint> t_PackedVecs : register(t0);
RWTexture2D<float4> t_Output : register(u0);

[numthreads(16, 16, 1)]
void main(uint2 pixelPosition : SV_DispatchThreadID)
{
uint packedVec = t_PackedVecs[pixelPosition];
float3 unpackedVec = Unpack_R11G11B10_UFLOAT(packedVec);
t_Output[pixelPosition] = float4(unpackedVec, 1.0);
}

#endif // PACKED_NORMAL_HLSL
29 changes: 29 additions & 0 deletions shaders/DebugViz/PackedR8G8B8A8GammaUFloatViz.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/***************************************************************************
# Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
**************************************************************************/

#ifndef PACKED_NORMAL_HLSL
#define PACKED_NORMAL_HLSL

#include "../ShaderParameters.h"

#include <donut/shaders/packing.hlsli>

Texture2D<uint> t_PackedVecs : register(t0);
RWTexture2D<float4> t_Output : register(u0);

[numthreads(16, 16, 1)]
void main(uint2 pixelPosition : SV_DispatchThreadID)
{
uint packedVec = t_PackedVecs[pixelPosition];
float4 unpackedVec = Unpack_R8G8B8A8_Gamma_UFLOAT(packedVec);
t_Output[pixelPosition] = unpackedVec;
}

#endif // PACKED_NORMAL_HLSL
3 changes: 3 additions & 0 deletions shaders/Shaders.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ VisualizeHdrSignals.hlsl -T ps_5_0 -E main
VisualizeConfidence.hlsl -T ps_5_0 -E main
DlssExposure.hlsl -T cs_5_0 -E main
PostprocessGBuffer.hlsl -T cs_5_0 -E main
DebugViz/NDirOctUNorm32Viz.hlsl -T cs_5_0 -E main
DebugViz/PackedR8G8B8A8GammaUFloatViz.hlsl -T cs_5_0 -E main
DebugViz/PackedR11G11B10UFloatViz.hlsl -T cs_5_0 -E main

PrepareLights.hlsl -T cs_6_3 -E main
LightingPasses/PresampleLights.hlsl -T cs_6_3 -E main
Expand Down
60 changes: 60 additions & 0 deletions src/DebugViz/DebugVizPasses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "DebugVizPasses.h"

#include "../RenderTargets.h"

DebugVizPasses::DebugVizPasses(
nvrhi::IDevice* device,
std::shared_ptr<donut::engine::ShaderFactory> shaderFactory,
std::shared_ptr<donut::engine::Scene> scene,
nvrhi::IBindingLayout* bindlessLayout) :
m_GBufferNormalsViz(std::make_unique<PackedDataVizPass>(device, shaderFactory, scene, bindlessLayout)),
m_GBufferGeoNormalsViz(std::make_unique<PackedDataVizPass>(device, shaderFactory, scene, bindlessLayout)),
m_GBufferDiffuseAlbedoViz(std::make_unique<PackedDataVizPass>(device, shaderFactory, scene, bindlessLayout)),
m_GBufferSpecularRoughnessViz(std::make_unique<PackedDataVizPass>(device, shaderFactory, scene, bindlessLayout))
{

}

void DebugVizPasses::CreatePipelines()
{
m_GBufferNormalsViz->CreatePipeline("app/DebugViz/NDirOctUNorm32Viz.hlsl");
m_GBufferGeoNormalsViz->CreatePipeline("app/DebugViz/NDirOctUNorm32Viz.hlsl");
m_GBufferDiffuseAlbedoViz->CreatePipeline("app/DebugViz/PackedR11G11B10UFloatViz.hlsl");
m_GBufferSpecularRoughnessViz->CreatePipeline("app/DebugViz/PackedR8G8B8A8GammaUFloatViz.hlsl");
}

void DebugVizPasses::CreateBindingSets(RenderTargets& renderTargets, nvrhi::TextureHandle dst)
{
m_GBufferNormalsViz->CreateBindingSet(renderTargets.GBufferNormals, renderTargets.PrevGBufferNormals, renderTargets.DebugColor);
m_GBufferGeoNormalsViz->CreateBindingSet(renderTargets.GBufferGeoNormals, renderTargets.PrevGBufferGeoNormals, renderTargets.DebugColor);
m_GBufferDiffuseAlbedoViz->CreateBindingSet(renderTargets.GBufferDiffuseAlbedo, renderTargets.PrevGBufferDiffuseAlbedo, renderTargets.DebugColor);
m_GBufferSpecularRoughnessViz->CreateBindingSet(renderTargets.GBufferSpecularRough, renderTargets.PrevGBufferSpecularRough, renderTargets.DebugColor);
}

void DebugVizPasses::RenderUnpackedNormals(nvrhi::ICommandList* commandList, const donut::engine::IView& view)
{
m_GBufferNormalsViz->Render(commandList, view);
}

void DebugVizPasses::RenderUnpackedGeoNormals(nvrhi::ICommandList* commandList, const donut::engine::IView& view)
{
m_GBufferGeoNormalsViz->Render(commandList, view);
}

void DebugVizPasses::RenderUnpackedDiffuseAlbeo(nvrhi::ICommandList* commandList, const donut::engine::IView& view)
{
m_GBufferDiffuseAlbedoViz->Render(commandList, view);
}

void DebugVizPasses::RenderUnpackedSpecularRoughness(nvrhi::ICommandList* commandList, const donut::engine::IView& view)
{
m_GBufferSpecularRoughnessViz->Render(commandList, view);
}

void DebugVizPasses::NextFrame()
{
m_GBufferNormalsViz->NextFrame();
m_GBufferGeoNormalsViz->NextFrame();
m_GBufferDiffuseAlbedoViz->NextFrame();
m_GBufferSpecularRoughnessViz->NextFrame();
}
33 changes: 33 additions & 0 deletions src/DebugViz/DebugVizPasses.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <memory>

#include "PackedDataVizPass.h"

class DebugVizPasses
{
public:
DebugVizPasses(
nvrhi::IDevice* device,
std::shared_ptr<donut::engine::ShaderFactory> shaderFactory,
std::shared_ptr<donut::engine::Scene> scene,
nvrhi::IBindingLayout* bindlessLayout);

void CreatePipelines();

void CreateBindingSets(RenderTargets& renderTargets, nvrhi::TextureHandle dst);

void RenderUnpackedNormals(nvrhi::ICommandList* commandList, const donut::engine::IView& view);
void RenderUnpackedGeoNormals(nvrhi::ICommandList* commandList, const donut::engine::IView& view);
void RenderUnpackedDiffuseAlbeo(nvrhi::ICommandList* commandList, const donut::engine::IView& view);
void RenderUnpackedSpecularRoughness(nvrhi::ICommandList* commandList, const donut::engine::IView& view);

void NextFrame();

private:
std::unique_ptr<PackedDataVizPass> m_GBufferNormalsViz;
std::unique_ptr<PackedDataVizPass> m_GBufferGeoNormalsViz;
std::unique_ptr<PackedDataVizPass> m_GBufferDiffuseAlbedoViz;
std::unique_ptr<PackedDataVizPass> m_GBufferSpecularRoughnessViz;
};

92 changes: 92 additions & 0 deletions src/DebugViz/PackedDataVizPass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "PackedDataVizPass.h"

#include "../RenderTargets.h"
#include "../SampleScene.h"
#include "../UserInterface.h"

#include <donut/engine/ShaderFactory.h>
#include <donut/engine/View.h>
#include <donut/engine/Scene.h>
#include <donut/engine/CommonRenderPasses.h>
#include <donut/core/log.h>
#include <nvrhi/utils.h>

#include <utility>

using namespace donut::math;
using namespace donut::engine;

PackedDataVizPass::PackedDataVizPass(
nvrhi::IDevice* device,
std::shared_ptr<donut::engine::ShaderFactory> shaderFactory,
std::shared_ptr<donut::engine::Scene> scene,
nvrhi::IBindingLayout* bindlessLayout) :
m_Device(device)
, m_BindlessLayout(bindlessLayout)
, m_ShaderFactory(std::move(shaderFactory))
, m_Scene(std::move(scene))
{
nvrhi::BindingLayoutDesc bindingLayoutDesc;
bindingLayoutDesc.visibility = nvrhi::ShaderType::Compute;
bindingLayoutDesc.bindings = {
nvrhi::BindingLayoutItem::Texture_SRV(0),
nvrhi::BindingLayoutItem::Texture_UAV(0),
};

m_BindingLayout = m_Device->createBindingLayout(bindingLayoutDesc);
}

void PackedDataVizPass::CreatePipeline(const std::string& shaderPath)
{
std::string debugMsg = "Initializing PackedDataVizPass with " + shaderPath + "...";
donut::log::debug(debugMsg.c_str());
gpuPerfMarker = "Packed Data Viz Pass:" + shaderPath;

m_ComputeShader = m_ShaderFactory->CreateShader(shaderPath.c_str(), "main", nullptr, nvrhi::ShaderType::Compute);

nvrhi::ComputePipelineDesc pipelineDesc;
pipelineDesc.bindingLayouts = { m_BindingLayout, m_BindlessLayout };
pipelineDesc.CS = m_ComputeShader;
m_ComputePipeline = m_Device->createComputePipeline(pipelineDesc);
}

void PackedDataVizPass::CreateBindingSet(nvrhi::TextureHandle src, nvrhi::TextureHandle prevSrc, nvrhi::TextureHandle dst)
{
nvrhi::BindingSetDesc bindingSetDesc;

bindingSetDesc.bindings = {
// GBuffer bindings --- CAUTION: these items are addressed below to swap the even and odd frames
nvrhi::BindingSetItem::Texture_SRV(0, src),
nvrhi::BindingSetItem::Texture_UAV(0, dst)
};

m_BindingSetEven = m_Device->createBindingSet(bindingSetDesc, m_BindingLayout);

bindingSetDesc.bindings[0].resourceHandle = prevSrc;

m_BindingSetOdd = m_Device->createBindingSet(bindingSetDesc, m_BindingLayout);
}

void PackedDataVizPass::Render(
nvrhi::ICommandList* commandList,
const donut::engine::IView& view)
{
commandList->beginMarker(gpuPerfMarker.c_str());

nvrhi::ComputeState state;
state.bindings = { m_BindingSetEven, m_Scene->GetDescriptorTable() };
state.pipeline = m_ComputePipeline;
commandList->setComputeState(state);

commandList->dispatch(
dm::div_ceil(view.GetViewExtent().width(), 16),
dm::div_ceil(view.GetViewExtent().height(), 16),
1);

commandList->endMarker();
}

void PackedDataVizPass::NextFrame()
{
std::swap(m_BindingSetEven, m_BindingSetOdd);
}
52 changes: 52 additions & 0 deletions src/DebugViz/PackedDataVizPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <nvrhi/nvrhi.h>
#include <memory>

namespace donut::engine
{
class Scene;
class CommonRenderPasses;
class ShaderFactory;
class IView;
}

class RenderTargets;
class EnvironmentLight;
struct UIData;

class PackedDataVizPass
{
private:
nvrhi::DeviceHandle m_Device;

nvrhi::ShaderHandle m_ComputeShader;
nvrhi::ComputePipelineHandle m_ComputePipeline;
nvrhi::BindingLayoutHandle m_BindingLayout;
nvrhi::BindingLayoutHandle m_BindlessLayout;
nvrhi::BindingSetHandle m_BindingSetEven;
nvrhi::BindingSetHandle m_BindingSetOdd;

nvrhi::BufferHandle m_ConstantBuffer;

std::shared_ptr<donut::engine::ShaderFactory> m_ShaderFactory;
std::shared_ptr<donut::engine::Scene> m_Scene;
std::string gpuPerfMarker;

public:
PackedDataVizPass(
nvrhi::IDevice* device,
std::shared_ptr<donut::engine::ShaderFactory> shaderFactory,
std::shared_ptr<donut::engine::Scene> scene,
nvrhi::IBindingLayout* bindlessLayout);

void CreatePipeline(const std::string& shaderPath);

void CreateBindingSet(nvrhi::TextureHandle src, nvrhi::TextureHandle prevSrc, nvrhi::TextureHandle dst);

void Render(
nvrhi::ICommandList* commandList,
const donut::engine::IView& view);

void NextFrame();
};
11 changes: 11 additions & 0 deletions src/RenderTargets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ RenderTargets::RenderTargets(nvrhi::IDevice* device, int2 size)
desc.format = nvrhi::Format::RGBA16_FLOAT;
desc.debugName = "Gradients";
Gradients = device->createTexture(desc);

nvrhi::TextureDesc debugDesc;
debugDesc.width = size.x;
debugDesc.height = size.y;
debugDesc.keepInitialState = false;
debugDesc.isUAV = true;
debugDesc.isRenderTarget = false;
debugDesc.initialState = nvrhi::ResourceStates::UnorderedAccess;
debugDesc.format = nvrhi::Format::RGBA16_FLOAT;
debugDesc.debugName = "DebugColor";
DebugColor = device->createTexture(debugDesc);
}

bool RenderTargets::IsUpdateRequired(int2 size)
Expand Down
2 changes: 2 additions & 0 deletions src/RenderTargets.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class RenderTargets
nvrhi::TextureHandle PrevDiffuseConfidence;
nvrhi::TextureHandle PrevSpecularConfidence;

nvrhi::TextureHandle DebugColor;

std::shared_ptr<donut::engine::FramebufferFactory> LdrFramebuffer;
std::shared_ptr<donut::engine::FramebufferFactory> ResolvedFramebuffer;
std::shared_ptr<donut::engine::FramebufferFactory> GBufferFramebuffer;
Expand Down
18 changes: 18 additions & 0 deletions src/UserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,24 @@ void UserInterface::PostProcessSettings()
"The yellow \"fire\" at the bottom is shown where the displayed value is 0.\n"
"For confidence, shows a heat map with blue at full confidence and red at zero."
);
ImGui::Combo("Debug Render Target", (int*)&m_ui.debugRenderOutputBuffer,
"LDR Color\0"
"Depth\0"
"GBufferDiffuseAlbedo\0"
"GBufferSpecularRough\0"
"GBufferNormals\0"
"GBufferGeoNormals\0"
"GBufferEmissive\0"
"DiffuseLighting\0"
"SpecularLighting\0"
"DenoisedDiffuseLighting\0"
"DenoisedSpecularLighting\0"
"RestirLuminance\0"
"PrevRestirLuminance\0"
"DiffuseConfidence\0"
"SpecularConfidence\0"
"MotionVectors\0"
);
ImGui::PopItemWidth();

ImGui::TreePop();
Expand Down
Loading

0 comments on commit b26080b

Please sign in to comment.