-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNrdIntegration.cpp
481 lines (407 loc) · 19.7 KB
/
NrdIntegration.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright (c) 2025, 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 "NrdIntegration.h"
static_assert(NRD_VERSION_MAJOR >= 4 && NRD_VERSION_MINOR >= 0, "Unsupported NRD version!");
#include "RenderTargets.h"
#include <nvrhi/utils.h>
#include <donut/core/math/math.h>
#include <donut/engine/View.h>
#include <donut/engine/ShaderFactory.h>
#include <sstream>
#include <donut/core/log.h>
static nvrhi::Format GetNvrhiFormat(nrd::Format format)
{
switch (format)
{
case nrd::Format::R8_UNORM:
return nvrhi::Format::R8_UNORM;
case nrd::Format::R8_SNORM:
return nvrhi::Format::R8_SNORM;
case nrd::Format::R8_UINT:
return nvrhi::Format::R8_UINT;
case nrd::Format::R8_SINT:
return nvrhi::Format::R8_SINT;
case nrd::Format::RG8_UNORM:
return nvrhi::Format::RG8_UNORM;
case nrd::Format::RG8_SNORM:
return nvrhi::Format::RG8_SNORM;
case nrd::Format::RG8_UINT:
return nvrhi::Format::RG8_UINT;
case nrd::Format::RG8_SINT:
return nvrhi::Format::RG8_SINT;
case nrd::Format::RGBA8_UNORM:
return nvrhi::Format::RGBA8_UNORM;
case nrd::Format::RGBA8_SNORM:
return nvrhi::Format::RGBA8_SNORM;
case nrd::Format::RGBA8_UINT:
return nvrhi::Format::RGBA8_UINT;
case nrd::Format::RGBA8_SINT:
return nvrhi::Format::RGBA8_SINT;
case nrd::Format::RGBA8_SRGB:
return nvrhi::Format::SRGBA8_UNORM;
case nrd::Format::R16_UNORM:
return nvrhi::Format::R16_UNORM;
case nrd::Format::R16_SNORM:
return nvrhi::Format::R16_SNORM;
case nrd::Format::R16_UINT:
return nvrhi::Format::R16_UINT;
case nrd::Format::R16_SINT:
return nvrhi::Format::R16_SINT;
case nrd::Format::R16_SFLOAT:
return nvrhi::Format::R16_FLOAT;
case nrd::Format::RG16_UNORM:
return nvrhi::Format::RG16_UNORM;
case nrd::Format::RG16_SNORM:
return nvrhi::Format::RG16_SNORM;
case nrd::Format::RG16_UINT:
return nvrhi::Format::RG16_UINT;
case nrd::Format::RG16_SINT:
return nvrhi::Format::RG16_SINT;
case nrd::Format::RG16_SFLOAT:
return nvrhi::Format::RG16_FLOAT;
case nrd::Format::RGBA16_UNORM:
return nvrhi::Format::RGBA16_UNORM;
case nrd::Format::RGBA16_SNORM:
return nvrhi::Format::RGBA16_SNORM;
case nrd::Format::RGBA16_UINT:
return nvrhi::Format::RGBA16_UINT;
case nrd::Format::RGBA16_SINT:
return nvrhi::Format::RGBA16_SINT;
case nrd::Format::RGBA16_SFLOAT:
return nvrhi::Format::RGBA16_FLOAT;
case nrd::Format::R32_UINT:
return nvrhi::Format::R32_UINT;
case nrd::Format::R32_SINT:
return nvrhi::Format::R32_SINT;
case nrd::Format::R32_SFLOAT:
return nvrhi::Format::R32_FLOAT;
case nrd::Format::RG32_UINT:
return nvrhi::Format::RG32_UINT;
case nrd::Format::RG32_SINT:
return nvrhi::Format::RG32_SINT;
case nrd::Format::RG32_SFLOAT:
return nvrhi::Format::RG32_FLOAT;
case nrd::Format::RGB32_UINT:
return nvrhi::Format::RGB32_UINT;
case nrd::Format::RGB32_SINT:
return nvrhi::Format::RGB32_SINT;
case nrd::Format::RGB32_SFLOAT:
return nvrhi::Format::RGB32_FLOAT;
case nrd::Format::RGBA32_UINT:
return nvrhi::Format::RGBA32_UINT;
case nrd::Format::RGBA32_SINT:
return nvrhi::Format::RGBA32_SINT;
case nrd::Format::RGBA32_SFLOAT:
return nvrhi::Format::RGBA32_FLOAT;
case nrd::Format::R10_G10_B10_A2_UNORM:
return nvrhi::Format::R10G10B10A2_UNORM;
case nrd::Format::R10_G10_B10_A2_UINT:
return nvrhi::Format::UNKNOWN; // not representable and not used
case nrd::Format::R11_G11_B10_UFLOAT:
return nvrhi::Format::R11G11B10_FLOAT;
case nrd::Format::R9_G9_B9_E5_UFLOAT:
return nvrhi::Format::UNKNOWN; // not representable and not used
default:
return nvrhi::Format::UNKNOWN;
}
}
NrdIntegration::NrdIntegration(nvrhi::IDevice* device, nrd::Denoiser denoiser)
: m_device(device), m_initialized(false), m_instance(nullptr), m_denoiser(denoiser), m_bindingCache(device), m_identifier(0)
{
}
NrdIntegration::~NrdIntegration()
{
if (m_initialized)
nrd::DestroyInstance(*m_instance);
}
bool NrdIntegration::Initialize(uint32_t width, uint32_t height, donut::engine::ShaderFactory& shaderFactory)
{
const nrd::LibraryDesc& libraryDesc = nrd::GetLibraryDesc();
const nrd::DenoiserDesc denoiserDescs[] = { { m_identifier, m_denoiser } };
nrd::InstanceCreationDesc instanceCreationDesc = {};
instanceCreationDesc.denoisers = denoiserDescs;
instanceCreationDesc.denoisersNum = _countof(denoiserDescs);
nrd::Result result = nrd::CreateInstance(instanceCreationDesc, m_instance);
if (result != nrd::Result::SUCCESS)
return false;
const nrd::InstanceDesc& instanceDesc = nrd::GetInstanceDesc(*m_instance);
const nvrhi::BufferDesc constantBufferDesc =
nvrhi::utils::CreateVolatileConstantBufferDesc(instanceDesc.constantBufferMaxDataSize, "NrdConstantBuffer", instanceDesc.descriptorPoolDesc.setsMaxNum * 4);
m_constantBuffer = m_device->createBuffer(constantBufferDesc);
for (uint32_t samplerIndex = 0; samplerIndex < instanceDesc.samplersNum; samplerIndex++)
{
const nrd::Sampler& samplerMode = instanceDesc.samplers[samplerIndex];
nvrhi::SamplerAddressMode addressMode = nvrhi::SamplerAddressMode::Wrap;
bool filter = false;
switch (samplerMode)
{
case nrd::Sampler::NEAREST_CLAMP:
addressMode = nvrhi::SamplerAddressMode::Clamp;
filter = false;
break;
case nrd::Sampler::LINEAR_CLAMP:
addressMode = nvrhi::SamplerAddressMode::Clamp;
filter = true;
break;
default:
assert(!"Unknown NRD sampler mode");
break;
}
auto samplerDesc = nvrhi::SamplerDesc().setAllAddressModes(addressMode).setAllFilters(filter);
const nvrhi::SamplerHandle sampler = m_device->createSampler(samplerDesc);
if (!sampler)
{
assert(!"Cannot create an NRD sampler");
return false;
}
m_samplers.push_back(sampler);
}
for (uint32_t pipelineIndex = 0; pipelineIndex < instanceDesc.pipelinesNum; pipelineIndex++)
{
const nrd::PipelineDesc& nrdPipelineDesc = instanceDesc.pipelines[pipelineIndex];
std::string fileName = std::string("nrd/RayTracingDenoiser/Shaders/Source/") + nrdPipelineDesc.shaderFileName;
std::vector<donut::engine::ShaderMacro> macros = { { "NRD_COMPILER_DXC", "1" }, { "NRD_NORMAL_ENCODING", "2" }, { "NRD_ROUGHNESS_ENCODING", "1" } };
NrdPipeline pipeline;
pipeline.shader = shaderFactory.CreateShader(fileName.c_str(), "main", ¯os, nvrhi::ShaderType::Compute);
if (!pipeline.shader)
{
assert(!"Cannot create an NRD shader");
return false;
}
nvrhi::BindingLayoutDesc layoutDesc;
layoutDesc.visibility = nvrhi::ShaderType::Compute;
nvrhi::BindingLayoutItem constantBufferItem = {};
constantBufferItem.type = nvrhi::ResourceType::VolatileConstantBuffer;
constantBufferItem.slot = instanceDesc.constantBufferRegisterIndex;
layoutDesc.bindings.push_back(constantBufferItem);
assert(instanceDesc.samplersSpaceIndex == 0);
for (uint32_t samplerIndex = 0; samplerIndex < instanceDesc.samplersNum; samplerIndex++)
{
// const nrd::StaticSamplerDesc& nrdStaticSampler = denoiserDesc.samplers[samplerIndex];
nvrhi::BindingLayoutItem samplerItem = {};
samplerItem.type = nvrhi::ResourceType::Sampler;
samplerItem.slot = instanceDesc.samplersBaseRegisterIndex + samplerIndex;
layoutDesc.bindings.push_back(samplerItem);
}
for (uint32_t descriptorRangeIndex = 0; descriptorRangeIndex < nrdPipelineDesc.resourceRangesNum; descriptorRangeIndex++)
{
const nrd::ResourceRangeDesc& nrdDescriptorRange = nrdPipelineDesc.resourceRanges[descriptorRangeIndex];
nvrhi::BindingLayoutItem resourceItem = {};
switch (nrdDescriptorRange.descriptorType)
{
case nrd::DescriptorType::TEXTURE:
resourceItem.type = nvrhi::ResourceType::Texture_SRV;
break;
case nrd::DescriptorType::STORAGE_TEXTURE:
resourceItem.type = nvrhi::ResourceType::Texture_UAV;
break;
default:
assert(!"Unknown NRD descriptor type");
break;
}
for (uint32_t descriptorOffset = 0; descriptorOffset < nrdDescriptorRange.descriptorsNum; descriptorOffset++)
{
resourceItem.slot = nrdDescriptorRange.baseRegisterIndex + descriptorOffset;
layoutDesc.bindings.push_back(resourceItem);
}
}
pipeline.bindingLayout = m_device->createBindingLayout(layoutDesc);
if (!pipeline.bindingLayout)
{
assert(!"Cannot create an NRD binding layout");
return false;
}
nvrhi::ComputePipelineDesc pipelineDesc;
pipelineDesc.bindingLayouts = { pipeline.bindingLayout };
pipelineDesc.CS = pipeline.shader;
pipeline.pipeline = m_device->createComputePipeline(pipelineDesc);
if (!pipeline.pipeline)
{
assert(!"Cannot create an NRD pipeline");
return false;
}
m_pipelines.push_back(pipeline);
}
const uint32_t poolSize = instanceDesc.permanentPoolSize + instanceDesc.transientPoolSize;
for (uint32_t i = 0; i < poolSize; i++)
{
const bool isPermanent = (i < instanceDesc.permanentPoolSize);
const nrd::TextureDesc& nrdTextureDesc = isPermanent ? instanceDesc.permanentPool[i] : instanceDesc.transientPool[i - instanceDesc.permanentPoolSize];
const nvrhi::Format format = GetNvrhiFormat(nrdTextureDesc.format);
if (format == nvrhi::Format::UNKNOWN)
{
assert(!"Unknown or unsupported NRD format");
return false;
}
std::stringstream ss;
ss << "NRD " << (isPermanent ? "Permanent" : "Transient") << "Texture [" << (isPermanent ? i : i - instanceDesc.permanentPoolSize) << "]";
nvrhi::TextureDesc textureDesc;
textureDesc.width = width;
textureDesc.height = height;
textureDesc.format = format;
textureDesc.dimension = nvrhi::TextureDimension::Texture2D;
textureDesc.initialState = nvrhi::ResourceStates::ShaderResource;
textureDesc.keepInitialState = true;
textureDesc.isUAV = true;
textureDesc.debugName = ss.str();
const nvrhi::TextureHandle texture = m_device->createTexture(textureDesc);
if (!texture)
{
assert(!"Cannot create an NRD texture");
return false;
}
if (isPermanent)
m_permanentTextures.push_back(texture);
else
m_transientTextures.push_back(texture);
}
m_initialized = true;
return true;
}
bool NrdIntegration::IsAvailable() const
{
return m_initialized;
}
static inline void MatrixToNrd(float* dest, const dm::float4x4& m)
{
dm::float4x4 tm = dm::transpose(m);
memcpy(dest, &m, sizeof(m));
}
void NrdIntegration::RunDenoiserPasses(nvrhi::ICommandList* commandList,
const RenderTargets& renderTargets,
int pass,
const donut::engine::PlanarView& view,
const donut::engine::PlanarView& viewPrev,
uint32_t frameIndex,
float disocclusionThreshold,
float disocclusionThresholdAlternate,
bool useDisocclusionThresholdAlternateMix,
bool enableValidation,
const void* methodSettings,
bool reset)
{
if (methodSettings)
nrd::SetDenoiserSettings(*m_instance, m_identifier, methodSettings);
nrd::CommonSettings commonSettings;
MatrixToNrd(commonSettings.worldToViewMatrix, dm::affineToHomogeneous(view.GetViewMatrix())); // MatrixToNrd(commonSettings.worldToViewRotationMatrix,
// dm::affineToHomogeneous(view.GetViewMatrix()));
MatrixToNrd(commonSettings.worldToViewMatrixPrev, dm::affineToHomogeneous(viewPrev.GetViewMatrix())); // MatrixToNrd(commonSettings.worldToViewRotationMatrixPrev,
// dm::affineToHomogeneous(viewPrev.GetViewMatrix()));
MatrixToNrd(commonSettings.viewToClipMatrix, view.GetProjectionMatrix(false));
MatrixToNrd(commonSettings.viewToClipMatrixPrev, viewPrev.GetProjectionMatrix(false));
dm::float2 pixelOffset = view.GetPixelOffset();
dm::float2 prevPixelOffset = viewPrev.GetPixelOffset();
commonSettings.isMotionVectorInWorldSpace = false;
commonSettings.motionVectorScale[0] = (commonSettings.isMotionVectorInWorldSpace) ? 1.0f : 1.0f / view.GetViewExtent().width();
commonSettings.motionVectorScale[1] = (commonSettings.isMotionVectorInWorldSpace) ? 1.0f : 1.0f / view.GetViewExtent().height();
commonSettings.motionVectorScale[2] = 1.0f;
commonSettings.cameraJitter[0] = pixelOffset.x;
commonSettings.cameraJitter[1] = pixelOffset.y;
commonSettings.cameraJitterPrev[0] = prevPixelOffset.x;
commonSettings.cameraJitterPrev[1] = prevPixelOffset.y;
commonSettings.resourceSize[0] = view.GetViewExtent().width();
commonSettings.resourceSize[1] = view.GetViewExtent().height();
commonSettings.resourceSizePrev[0] = viewPrev.GetViewExtent().width();
commonSettings.resourceSizePrev[1] = viewPrev.GetViewExtent().height();
commonSettings.rectSize[0] = view.GetViewExtent().width();
commonSettings.rectSize[1] = view.GetViewExtent().height();
commonSettings.rectSizePrev[0] = viewPrev.GetViewExtent().width();
commonSettings.rectSizePrev[1] = viewPrev.GetViewExtent().height();
commonSettings.frameIndex = frameIndex;
commonSettings.enableValidation = enableValidation;
commonSettings.disocclusionThreshold = disocclusionThreshold;
commonSettings.disocclusionThresholdAlternate = disocclusionThresholdAlternate;
commonSettings.isDisocclusionThresholdMixAvailable = useDisocclusionThresholdAlternateMix;
commonSettings.accumulationMode = reset ? nrd::AccumulationMode::RESTART : nrd::AccumulationMode::CONTINUE;
nrd::SetCommonSettings(*m_instance, commonSettings);
const nrd::DispatchDesc* dispatchDescs = nullptr;
uint32_t dispatchDescNum = 0;
nrd::GetComputeDispatches(*m_instance, &m_identifier, 1, dispatchDescs, dispatchDescNum);
const nrd::InstanceDesc& instanceDesc = nrd::GetInstanceDesc(*m_instance);
for (uint32_t dispatchIndex = 0; dispatchIndex < dispatchDescNum; dispatchIndex++)
{
const nrd::DispatchDesc& dispatchDesc = dispatchDescs[dispatchIndex];
if (dispatchDesc.name)
commandList->beginMarker(dispatchDesc.name);
assert(m_constantBuffer);
commandList->writeBuffer(m_constantBuffer, dispatchDesc.constantBufferData, dispatchDesc.constantBufferDataSize);
nvrhi::BindingSetDesc setDesc;
setDesc.bindings.push_back(nvrhi::BindingSetItem::ConstantBuffer(instanceDesc.constantBufferRegisterIndex, m_constantBuffer));
for (uint32_t samplerIndex = 0; samplerIndex < instanceDesc.samplersNum; samplerIndex++)
{
assert(m_samplers[samplerIndex]);
setDesc.bindings.push_back(nvrhi::BindingSetItem::Sampler(instanceDesc.samplersBaseRegisterIndex + samplerIndex, m_samplers[samplerIndex]));
}
const nrd::PipelineDesc& nrdPipelineDesc = instanceDesc.pipelines[dispatchDesc.pipelineIndex];
uint32_t resourceIndex = 0;
for (uint32_t descriptorRangeIndex = 0; descriptorRangeIndex < nrdPipelineDesc.resourceRangesNum; descriptorRangeIndex++)
{
const nrd::ResourceRangeDesc& nrdDescriptorRange = nrdPipelineDesc.resourceRanges[descriptorRangeIndex];
for (uint32_t descriptorOffset = 0; descriptorOffset < nrdDescriptorRange.descriptorsNum; descriptorOffset++)
{
assert(resourceIndex < dispatchDesc.resourcesNum);
const nrd::ResourceDesc& resource = dispatchDesc.resources[resourceIndex];
nvrhi::TextureHandle texture;
switch (resource.type)
{
case nrd::ResourceType::IN_MV:
texture = renderTargets.denoiserMotionVectors;
break;
case nrd::ResourceType::IN_NORMAL_ROUGHNESS:
texture = renderTargets.denoiserNormalRoughness;
break;
case nrd::ResourceType::IN_VIEWZ:
texture = renderTargets.denoiserViewSpaceZ;
break;
case nrd::ResourceType::IN_SPEC_RADIANCE_HITDIST:
texture = renderTargets.denoiserInSpecRadianceHitDist;
break;
case nrd::ResourceType::IN_DIFF_RADIANCE_HITDIST:
texture = renderTargets.denoiserInDiffRadianceHitDist;
break;
case nrd::ResourceType::OUT_SPEC_RADIANCE_HITDIST:
texture = renderTargets.denoiserOutSpecRadianceHitDist;
break;
case nrd::ResourceType::OUT_DIFF_RADIANCE_HITDIST:
texture = renderTargets.denoiserOutDiffRadianceHitDist;
break;
case nrd::ResourceType::TRANSIENT_POOL:
texture = m_transientTextures[resource.indexInPool];
break;
case nrd::ResourceType::PERMANENT_POOL:
texture = m_permanentTextures[resource.indexInPool];
break;
default:
assert(!"Unavailable resource type");
break;
}
assert(texture);
nvrhi::TextureSubresourceSet subresources = nvrhi::AllSubresources;
nvrhi::BindingSetItem setItem = nvrhi::BindingSetItem::None();
setItem.resourceHandle = texture;
setItem.slot = nrdDescriptorRange.baseRegisterIndex + descriptorOffset;
setItem.subresources = subresources;
setItem.type = (nrdDescriptorRange.descriptorType == nrd::DescriptorType::TEXTURE) ? nvrhi::ResourceType::Texture_SRV : nvrhi::ResourceType::Texture_UAV;
setDesc.bindings.push_back(setItem);
resourceIndex++;
}
}
assert(resourceIndex == dispatchDesc.resourcesNum);
const NrdPipeline& pipeline = m_pipelines[dispatchDesc.pipelineIndex];
nvrhi::BindingSetHandle bindingSet = m_bindingCache.GetOrCreateBindingSet(setDesc, pipeline.bindingLayout);
nvrhi::ComputeState state;
state.bindings = { bindingSet };
state.pipeline = pipeline.pipeline;
commandList->setComputeState(state);
commandList->dispatch(dispatchDesc.gridWidth, dispatchDesc.gridHeight);
if (dispatchDesc.name)
commandList->endMarker();
}
}