-
Notifications
You must be signed in to change notification settings - Fork 55
/
RtxdiResources.cpp
179 lines (147 loc) · 8.82 KB
/
RtxdiResources.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
/***************************************************************************
# 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.
**************************************************************************/
#include "RtxdiResources.h"
#include <rtxdi/ReSTIRDI.h>
#include <rtxdi/ReSTIRGI.h>
#include <rtxdi/RISBufferSegmentAllocator.h>
#include <donut/core/math/math.h>
using namespace dm;
#include "../shaders/ShaderParameters.h"
RtxdiResources::RtxdiResources(
nvrhi::IDevice* device,
const rtxdi::ReSTIRDIContext& context,
const rtxdi::RISBufferSegmentAllocator& risBufferSegmentAllocator,
uint32_t maxEmissiveMeshes,
uint32_t maxEmissiveTriangles,
uint32_t maxPrimitiveLights,
uint32_t maxGeometryInstances,
uint32_t environmentMapWidth,
uint32_t environmentMapHeight)
: m_MaxEmissiveMeshes(maxEmissiveMeshes)
, m_MaxEmissiveTriangles(maxEmissiveTriangles)
, m_MaxPrimitiveLights(maxPrimitiveLights)
, m_MaxGeometryInstances(maxGeometryInstances)
{
nvrhi::BufferDesc taskBufferDesc;
taskBufferDesc.byteSize = sizeof(PrepareLightsTask) * (maxEmissiveMeshes + maxPrimitiveLights);
taskBufferDesc.structStride = sizeof(PrepareLightsTask);
taskBufferDesc.initialState = nvrhi::ResourceStates::ShaderResource;
taskBufferDesc.keepInitialState = true;
taskBufferDesc.debugName = "TaskBuffer";
taskBufferDesc.canHaveUAVs = true;
TaskBuffer = device->createBuffer(taskBufferDesc);
nvrhi::BufferDesc primitiveLightBufferDesc;
primitiveLightBufferDesc.byteSize = sizeof(PolymorphicLightInfo) * maxPrimitiveLights;
primitiveLightBufferDesc.structStride = sizeof(PolymorphicLightInfo);
primitiveLightBufferDesc.initialState = nvrhi::ResourceStates::ShaderResource;
primitiveLightBufferDesc.keepInitialState = true;
primitiveLightBufferDesc.debugName = "PrimitiveLightBuffer";
PrimitiveLightBuffer = device->createBuffer(primitiveLightBufferDesc);
nvrhi::BufferDesc risBufferDesc;
risBufferDesc.byteSize = sizeof(uint32_t) * 2 * std::max(risBufferSegmentAllocator.getTotalSizeInElements(), 1u); // RG32_UINT per element
risBufferDesc.format = nvrhi::Format::RG32_UINT;
risBufferDesc.canHaveTypedViews = true;
risBufferDesc.initialState = nvrhi::ResourceStates::ShaderResource;
risBufferDesc.keepInitialState = true;
risBufferDesc.debugName = "RisBuffer";
risBufferDesc.canHaveUAVs = true;
RisBuffer = device->createBuffer(risBufferDesc);
risBufferDesc.byteSize = sizeof(uint32_t) * 8 * std::max(risBufferSegmentAllocator.getTotalSizeInElements(), 1u); // RGBA32_UINT x 2 per element
risBufferDesc.format = nvrhi::Format::RGBA32_UINT;
risBufferDesc.debugName = "RisLightDataBuffer";
RisLightDataBuffer = device->createBuffer(risBufferDesc);
uint32_t maxLocalLights = maxEmissiveTriangles + maxPrimitiveLights;
uint32_t lightBufferElements = maxLocalLights * 2;
nvrhi::BufferDesc lightBufferDesc;
lightBufferDesc.byteSize = sizeof(PolymorphicLightInfo) * lightBufferElements;
lightBufferDesc.structStride = sizeof(PolymorphicLightInfo);
lightBufferDesc.initialState = nvrhi::ResourceStates::ShaderResource;
lightBufferDesc.keepInitialState = true;
lightBufferDesc.debugName = "LightDataBuffer";
lightBufferDesc.canHaveUAVs = true;
LightDataBuffer = device->createBuffer(lightBufferDesc);
nvrhi::BufferDesc geometryInstanceToLightBufferDesc;
geometryInstanceToLightBufferDesc.byteSize = sizeof(uint32_t) * maxGeometryInstances;
geometryInstanceToLightBufferDesc.structStride = sizeof(uint32_t);
geometryInstanceToLightBufferDesc.initialState = nvrhi::ResourceStates::ShaderResource;
geometryInstanceToLightBufferDesc.keepInitialState = true;
geometryInstanceToLightBufferDesc.debugName = "GeometryInstanceToLightBuffer";
GeometryInstanceToLightBuffer = device->createBuffer(geometryInstanceToLightBufferDesc);
nvrhi::BufferDesc lightIndexMappingBufferDesc;
lightIndexMappingBufferDesc.byteSize = sizeof(uint32_t) * lightBufferElements;
lightIndexMappingBufferDesc.format = nvrhi::Format::R32_UINT;
lightIndexMappingBufferDesc.canHaveTypedViews = true;
lightIndexMappingBufferDesc.initialState = nvrhi::ResourceStates::ShaderResource;
lightIndexMappingBufferDesc.keepInitialState = true;
lightIndexMappingBufferDesc.debugName = "LightIndexMappingBuffer";
lightIndexMappingBufferDesc.canHaveUAVs = true;
LightIndexMappingBuffer = device->createBuffer(lightIndexMappingBufferDesc);
nvrhi::BufferDesc neighborOffsetBufferDesc;
neighborOffsetBufferDesc.byteSize = context.getStaticParameters().NeighborOffsetCount * 2;
neighborOffsetBufferDesc.format = nvrhi::Format::RG8_SNORM;
neighborOffsetBufferDesc.canHaveTypedViews = true;
neighborOffsetBufferDesc.debugName = "NeighborOffsets";
neighborOffsetBufferDesc.initialState = nvrhi::ResourceStates::ShaderResource;
neighborOffsetBufferDesc.keepInitialState = true;
NeighborOffsetsBuffer = device->createBuffer(neighborOffsetBufferDesc);
nvrhi::BufferDesc lightReservoirBufferDesc;
lightReservoirBufferDesc.byteSize = sizeof(RTXDI_PackedDIReservoir) * context.getReservoirBufferParameters().reservoirArrayPitch * rtxdi::c_NumReSTIRDIReservoirBuffers;
lightReservoirBufferDesc.structStride = sizeof(RTXDI_PackedDIReservoir);
lightReservoirBufferDesc.initialState = nvrhi::ResourceStates::UnorderedAccess;
lightReservoirBufferDesc.keepInitialState = true;
lightReservoirBufferDesc.debugName = "LightReservoirBuffer";
lightReservoirBufferDesc.canHaveUAVs = true;
LightReservoirBuffer = device->createBuffer(lightReservoirBufferDesc);
nvrhi::BufferDesc secondaryGBufferDesc;
secondaryGBufferDesc.byteSize = sizeof(SecondaryGBufferData) * context.getReservoirBufferParameters().reservoirArrayPitch;
secondaryGBufferDesc.structStride = sizeof(SecondaryGBufferData);
secondaryGBufferDesc.initialState = nvrhi::ResourceStates::UnorderedAccess;
secondaryGBufferDesc.keepInitialState = true;
secondaryGBufferDesc.debugName = "SecondaryGBuffer";
secondaryGBufferDesc.canHaveUAVs = true;
SecondaryGBuffer = device->createBuffer(secondaryGBufferDesc);
nvrhi::TextureDesc environmentPdfDesc;
environmentPdfDesc.width = environmentMapWidth;
environmentPdfDesc.height = environmentMapHeight;
environmentPdfDesc.mipLevels = uint32_t(ceilf(::log2f(float(std::max(environmentPdfDesc.width, environmentPdfDesc.height)))) + 1); // full mip chain up to 1x1
environmentPdfDesc.isUAV = true;
environmentPdfDesc.debugName = "EnvironmentPdf";
environmentPdfDesc.initialState = nvrhi::ResourceStates::ShaderResource;
environmentPdfDesc.keepInitialState = true;
environmentPdfDesc.format = nvrhi::Format::R16_FLOAT;
EnvironmentPdfTexture = device->createTexture(environmentPdfDesc);
nvrhi::TextureDesc localLightPdfDesc;
rtxdi::ComputePdfTextureSize(maxLocalLights, localLightPdfDesc.width, localLightPdfDesc.height, localLightPdfDesc.mipLevels);
assert(localLightPdfDesc.width * localLightPdfDesc.height >= maxLocalLights);
localLightPdfDesc.isUAV = true;
localLightPdfDesc.debugName = "LocalLightPdf";
localLightPdfDesc.initialState = nvrhi::ResourceStates::ShaderResource;
localLightPdfDesc.keepInitialState = true;
localLightPdfDesc.format = nvrhi::Format::R32_FLOAT; // Use FP32 here to allow a wide range of flux values, esp. when downsampled.
LocalLightPdfTexture = device->createTexture(localLightPdfDesc);
nvrhi::BufferDesc giReservoirBufferDesc;
giReservoirBufferDesc.byteSize = sizeof(RTXDI_PackedGIReservoir) * context.getReservoirBufferParameters().reservoirArrayPitch * rtxdi::c_NumReSTIRGIReservoirBuffers;
giReservoirBufferDesc.structStride = sizeof(RTXDI_PackedGIReservoir);
giReservoirBufferDesc.initialState = nvrhi::ResourceStates::UnorderedAccess;
giReservoirBufferDesc.keepInitialState = true;
giReservoirBufferDesc.debugName = "GIReservoirBuffer";
giReservoirBufferDesc.canHaveUAVs = true;
GIReservoirBuffer = device->createBuffer(giReservoirBufferDesc);
}
void RtxdiResources::InitializeNeighborOffsets(nvrhi::ICommandList* commandList, uint32_t neighborOffsetCount)
{
if (m_NeighborOffsetsInitialized)
return;
std::vector<uint8_t> offsets;
offsets.resize(neighborOffsetCount * 2);
rtxdi::FillNeighborOffsetBuffer(offsets.data(), neighborOffsetCount);
commandList->writeBuffer(NeighborOffsetsBuffer, offsets.data(), offsets.size());
m_NeighborOffsetsInitialized = true;
}