-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathRenderPass.cpp
187 lines (154 loc) · 8.42 KB
/
RenderPass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/***************************************************************************
# Copyright (c) 2020-2022, 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.
**************************************************************************/
#include "RenderPass.h"
#include "RenderTargets.h"
#include "RtxdiResources.h"
#include <donut/engine/Scene.h>
#include <donut/engine/CommonRenderPasses.h>
#include <donut/engine/ShaderFactory.h>
#include <donut/engine/View.h>
#include <donut/core/log.h>
#include <nvrhi/utils.h>
#include <Rtxdi/DI/ReSTIRDI.h>
using namespace donut::math;
#include "../shaders/ShaderParameters.h"
using namespace donut::engine;
RenderPass::RenderPass(
nvrhi::IDevice* device,
std::shared_ptr<ShaderFactory> shaderFactory,
std::shared_ptr<donut::engine::CommonRenderPasses> commonPasses,
std::shared_ptr<donut::engine::Scene> scene,
nvrhi::IBindingLayout* bindlessLayout
)
: m_device(device)
, m_bindlessLayout(bindlessLayout)
, m_shaderFactory(std::move(shaderFactory))
, m_commonPasses(std::move(commonPasses))
, m_scene(std::move(scene))
{
// The binding layout descriptor must match the binding set descriptor defined in CreateBindingSet(...) below
nvrhi::BindingLayoutDesc globalBindingLayoutDesc;
globalBindingLayoutDesc.visibility = nvrhi::ShaderType::Compute | nvrhi::ShaderType::AllRayTracing;
globalBindingLayoutDesc.bindings = {
nvrhi::BindingLayoutItem::Texture_SRV(0),
nvrhi::BindingLayoutItem::Texture_SRV(1),
nvrhi::BindingLayoutItem::Texture_SRV(2),
nvrhi::BindingLayoutItem::Texture_SRV(3),
nvrhi::BindingLayoutItem::Texture_SRV(4),
nvrhi::BindingLayoutItem::RayTracingAccelStruct(30),
nvrhi::BindingLayoutItem::StructuredBuffer_SRV(32),
nvrhi::BindingLayoutItem::StructuredBuffer_SRV(33),
nvrhi::BindingLayoutItem::StructuredBuffer_SRV(34),
nvrhi::BindingLayoutItem::StructuredBuffer_SRV(20),
nvrhi::BindingLayoutItem::TypedBuffer_SRV(21),
nvrhi::BindingLayoutItem::StructuredBuffer_SRV(22),
nvrhi::BindingLayoutItem::StructuredBuffer_UAV(0),
nvrhi::BindingLayoutItem::Texture_UAV(1),
nvrhi::BindingLayoutItem::Texture_UAV(2),
nvrhi::BindingLayoutItem::Texture_UAV(3),
nvrhi::BindingLayoutItem::Texture_UAV(4),
nvrhi::BindingLayoutItem::Texture_UAV(5),
nvrhi::BindingLayoutItem::Texture_UAV(6),
nvrhi::BindingLayoutItem::VolatileConstantBuffer(0),
nvrhi::BindingLayoutItem::Sampler(0),
nvrhi::BindingLayoutItem::Sampler(1),
};
m_bindingLayout = m_device->createBindingLayout(globalBindingLayoutDesc);
m_constantBuffer = m_device->createBuffer(nvrhi::utils::CreateVolatileConstantBufferDesc(sizeof(ResamplingConstants), "ResamplingConstants", 16));
}
void RenderPass::CreateBindingSet(
nvrhi::rt::IAccelStruct* topLevelAS,
const RenderTargets& renderTargets,
const RtxdiResources& resources)
{
assert(&renderTargets);
assert(&resources);
for (int currentFrame = 0; currentFrame <= 1; currentFrame++)
{
// This list must match the binding declarations in RtxdiApplicationBridge.hlsli
nvrhi::BindingSetDesc bindingSetDesc;
bindingSetDesc.bindings = {
nvrhi::BindingSetItem::Texture_SRV(0, currentFrame ? renderTargets.PrevDepth : renderTargets.Depth),
nvrhi::BindingSetItem::Texture_SRV(1, currentFrame ? renderTargets.PrevGBufferNormals : renderTargets.GBufferNormals),
nvrhi::BindingSetItem::Texture_SRV(2, currentFrame ? renderTargets.PrevGBufferGeoNormals : renderTargets.GBufferGeoNormals),
nvrhi::BindingSetItem::Texture_SRV(3, currentFrame ? renderTargets.PrevGBufferDiffuseAlbedo : renderTargets.GBufferDiffuseAlbedo),
nvrhi::BindingSetItem::Texture_SRV(4, currentFrame ? renderTargets.PrevGBufferSpecularRough : renderTargets.GBufferSpecularRough),
nvrhi::BindingSetItem::RayTracingAccelStruct(30, topLevelAS),
nvrhi::BindingSetItem::StructuredBuffer_SRV(32, m_scene->GetInstanceBuffer()),
nvrhi::BindingSetItem::StructuredBuffer_SRV(33, m_scene->GetGeometryBuffer()),
nvrhi::BindingSetItem::StructuredBuffer_SRV(34, m_scene->GetMaterialBuffer()),
nvrhi::BindingSetItem::StructuredBuffer_SRV(20, resources.LightDataBuffer),
nvrhi::BindingSetItem::TypedBuffer_SRV(21, resources.NeighborOffsetsBuffer),
nvrhi::BindingSetItem::StructuredBuffer_SRV(22, resources.GeometryInstanceToLightBuffer),
nvrhi::BindingSetItem::StructuredBuffer_UAV(0, resources.LightReservoirBuffer),
nvrhi::BindingSetItem::Texture_UAV(1, renderTargets.HdrColor),
nvrhi::BindingSetItem::Texture_UAV(2, currentFrame ? renderTargets.Depth : renderTargets.PrevDepth),
nvrhi::BindingSetItem::Texture_UAV(3, currentFrame ? renderTargets.GBufferNormals : renderTargets.PrevGBufferNormals),
nvrhi::BindingSetItem::Texture_UAV(4, currentFrame ? renderTargets.GBufferGeoNormals : renderTargets.PrevGBufferGeoNormals),
nvrhi::BindingSetItem::Texture_UAV(5, currentFrame ? renderTargets.GBufferDiffuseAlbedo : renderTargets.PrevGBufferDiffuseAlbedo),
nvrhi::BindingSetItem::Texture_UAV(6, currentFrame ? renderTargets.GBufferSpecularRough : renderTargets.PrevGBufferSpecularRough),
nvrhi::BindingSetItem::ConstantBuffer(0, m_constantBuffer),
nvrhi::BindingSetItem::Sampler(0, m_commonPasses->m_LinearWrapSampler),
nvrhi::BindingSetItem::Sampler(1, m_commonPasses->m_LinearWrapSampler)
};
const nvrhi::BindingSetHandle bindingSet = m_device->createBindingSet(bindingSetDesc, m_bindingLayout);
if (currentFrame)
m_bindingSet = bindingSet;
else
m_prevBindingSet = bindingSet;
}
m_lightReservoirBuffer = resources.LightReservoirBuffer;
}
void RenderPass::CreatePipeline()
{
m_computeShader = m_shaderFactory->CreateShader("app/Render.hlsl", "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 RenderPass::Render(
nvrhi::ICommandList* commandList,
rtxdi::ReSTIRDIContext& context,
const donut::engine::IView& view,
const donut::engine::IView& previousView,
const Settings& localSettings,
const RTXDI_LightBufferParameters& lightBufferParams)
{
ResamplingConstants constants = {};
constants.frameIndex = context.GetFrameIndex();
view.FillPlanarViewConstants(constants.view);
previousView.FillPlanarViewConstants(constants.prevView);
constants.enableResampling = localSettings.enableResampling;
constants.unbiasedMode = localSettings.unbiasedMode;
constants.numInitialSamples = localSettings.numInitialSamples;
constants.numInitialBRDFSamples = localSettings.numInitialBRDFSamples;
constants.numSpatialSamples = localSettings.numSpatialSamples;
constants.restirDIReservoirBufferParams = context.GetReservoirBufferParameters();
constants.lightBufferParams = lightBufferParams;
constants.runtimeParams.neighborOffsetMask = context.GetStaticParameters().NeighborOffsetCount - 1;
constants.runtimeParams.activeCheckerboardField = 0;
constants.inputBufferIndex = !(context.GetFrameIndex() & 1);
constants.outputBufferIndex = context.GetFrameIndex() & 1;
commandList->writeBuffer(m_constantBuffer, &constants, sizeof(constants));
commandList->beginMarker("Render");
nvrhi::ComputeState state;
state.bindings = { m_bindingSet, m_scene->GetDescriptorTable() };
state.pipeline = m_computePipeline;
commandList->setComputeState(state);
commandList->dispatch(
dm::div_ceil(view.GetViewExtent().width(), RTXDI_SCREEN_SPACE_GROUP_SIZE),
dm::div_ceil(view.GetViewExtent().height(), RTXDI_SCREEN_SPACE_GROUP_SIZE));
commandList->endMarker();
}
void RenderPass::NextFrame()
{
std::swap(m_bindingSet, m_prevBindingSet);
}