Skip to content

Commit d4d5e67

Browse files
committed
Update to v0.10.0
1 parent c8268a5 commit d4d5e67

10 files changed

+77
-28
lines changed

bin/NRC_D3D12.dll

956 KB
Binary file not shown.

bin/NRC_Vulkan.dll

957 KB
Binary file not shown.

include/Nrc.hlsli

+54-10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,50 @@ const NrcMode g_nrcMode = NrcMode::Disabled;
9999

100100
#endif
101101

102+
#if !defined(NRC_USE_CUSTOM_BUFFER_ACCESSORS)
103+
#define NRC_USE_CUSTOM_BUFFER_ACCESSORS 0
104+
#endif
105+
106+
// Allow the integration to define its own type for a read-write structured buffer
107+
// E.g. if you are doing a Pirate Engine integration, the correct syntax is
108+
//
109+
// Tharr be RW Structure Buffer <T> arrrrr
110+
//
111+
// So you would do
112+
//
113+
// #define NRC_RW_STRUCTURED_BUFFER(T) Tharr be RW Structure Buffer <T> arrrrr
114+
//
115+
#if ENABLE_NRC && !defined(NRC_RW_STRUCTURED_BUFFER)
116+
#define NRC_RW_STRUCTURED_BUFFER(T) RWStructuredBuffer<T>
117+
#endif
118+
119+
// Allow engine to override declaring buffers in the `NrcBuffers` struct as part
120+
// of the `NrcContext` in the case that buffers within structs is not supported.
121+
// In order to do this, the engine must first
122+
//
123+
// #define NRC_USE_CUSTOM_BUFFER_ACCESSORS 1
124+
//
125+
// and then it must declare the following macros to provide a read-write
126+
// object that can be indexed with the square bracket operator
127+
//
128+
// NRC_BUFFER_QUERY_PATH_INFO
129+
// NRC_BUFFER_TRAINING_PATH_INFO
130+
// NRC_BUFFER_TRAINING_PATH_VERTICES
131+
// NRC_BUFFER_QUERY_RADIANCE_PARAMS
132+
// NRC_BUFFER_QUERY_COUNTERS_DATA
133+
//
134+
#if ENABLE_NRC && !NRC_USE_CUSTOM_BUFFER_ACCESSORS
135+
#if defined(NRC_BUFFER_QUERY_PATH_INFO) || defined(NRC_BUFFER_TRAINING_PATH_INFO) || defined(NRC_BUFFER_TRAINING_PATH_VERTICES) || defined(NRC_BUFFER_QUERY_RADIANCE_PARAMS) || defined(NRC_BUFFER_QUERY_COUNTERS_DATA) || defined(NRC_BUFFER_DEBUG_TRAINING_PATH_INFO)
136+
#error "If you enable any NRC_BUFFER macros, then please #define NRC_USE_CUSTOM_BUFFER_ACCESSORS 1"
137+
#endif
138+
// Define the standard NRC buffer accessor macros
139+
#define NRC_BUFFER_QUERY_PATH_INFO context.buffers.queryPathInfo
140+
#define NRC_BUFFER_TRAINING_PATH_INFO context.buffers.trainingPathInfo
141+
#define NRC_BUFFER_TRAINING_PATH_VERTICES context.buffers.trainingPathVertices
142+
#define NRC_BUFFER_QUERY_RADIANCE_PARAMS context.buffers.queryRadianceParams
143+
#define NRC_BUFFER_QUERY_COUNTERS_DATA context.buffers.countersData
144+
#endif
145+
102146
// Validate defines
103147
#if !defined(ENABLE_NRC)
104148
#error "Expected ENABLE_NRC to be defined to something"
@@ -338,7 +382,7 @@ NrcProgressState NrcUpdateOnHit(
338382
// Finalize the previous vertex with the radiance and throughput that the path tracer accumulated
339383
// during its previous iteration
340384
const uint previousTrainingPathVertexIndex = trainingPathVertexIndex - 1;
341-
context.buffers.trainingPathVertices[previousTrainingPathVertexIndex] = NrcUpdateTrainingPathVertex(context.buffers.trainingPathVertices[previousTrainingPathVertexIndex], radiance, throughput);
385+
NRC_BUFFER_TRAINING_PATH_VERTICES[previousTrainingPathVertexIndex] = NrcUpdateTrainingPathVertex(NRC_BUFFER_TRAINING_PATH_VERTICES[previousTrainingPathVertexIndex], radiance, throughput);
342386
}
343387

344388
// Always update vertex counts. The pathState vertexCount variable mostly mirrors 'bounce' variable,
@@ -353,7 +397,7 @@ NrcProgressState NrcUpdateOnHit(
353397
radiance = 0..xxx;
354398

355399
// Store path vertex
356-
context.buffers.trainingPathVertices[trainingPathVertexIndex] = NrcInitializePackedPathVertex(
400+
NRC_BUFFER_TRAINING_PATH_VERTICES[trainingPathVertexIndex] = NrcInitializePackedPathVertex(
357401
surfaceAttributes.roughness, surfaceAttributes.shadingNormal, surfaceAttributes.viewVector, surfaceAttributes.diffuseReflectance, surfaceAttributes.specularF0, surfaceAttributes.encodedPosition);
358402

359403
bool terminate = (bounce == context.constants.maxPathVertices - 1) || //< Is this path at last vertex already? If yes, we can terminate.
@@ -395,7 +439,7 @@ NrcProgressState NrcUpdateOnHit(
395439
prefixThroughput = max(0.0f, NrcSanitizeNansInfs(prefixThroughput));
396440
pathState.packedPrefixThroughput = NrcEncodeLogLuvHdr(prefixThroughput);
397441

398-
pathState.queryBufferIndex = NrcIncrementCounter(context.buffers.countersData, NrcCounter::Queries);
442+
pathState.queryBufferIndex = NrcIncrementCounter(NRC_BUFFER_QUERY_COUNTERS_DATA, NrcCounter::Queries);
399443

400444
NrcRadianceParams params;
401445
params.encodedPosition = surfaceAttributes.encodedPosition;
@@ -405,7 +449,7 @@ NrcProgressState NrcUpdateOnHit(
405449
params.albedo = surfaceAttributes.diffuseReflectance;
406450
params.specular = surfaceAttributes.specularF0;
407451

408-
context.buffers.queryRadianceParams[pathState.queryBufferIndex] = params;
452+
NRC_BUFFER_QUERY_RADIANCE_PARAMS[pathState.queryBufferIndex] = params;
409453

410454
// Terminate now if the cache already includes direct reflected radiance.
411455
// Otherwise, we will terminate later, after NEE and the scatter ray has been computed.
@@ -475,7 +519,7 @@ void NrcWriteFinalPathInfo(in NrcContext context,
475519
const uint vertexIndex = vertexCount - 1;
476520
const uint arrayIndex = NrcCalculateTrainingPathVertexIndex(
477521
context.constants.trainingDimensions, context.pixelIndex, vertexIndex, context.constants.maxPathVertices);
478-
context.buffers.trainingPathVertices[arrayIndex] = NrcUpdateTrainingPathVertex(context.buffers.trainingPathVertices[arrayIndex], radiance, throughput);
522+
NRC_BUFFER_TRAINING_PATH_VERTICES[arrayIndex] = NrcUpdateTrainingPathVertex(NRC_BUFFER_TRAINING_PATH_VERTICES[arrayIndex], radiance, throughput);
479523

480524
// Create self-training records for _all_ training paths, including unbiased ones.
481525
// Without self-training, each training vertex position within the path would matter.
@@ -485,10 +529,10 @@ void NrcWriteFinalPathInfo(in NrcContext context,
485529
// this complicates the task of the network.
486530
if (!NrcGetFlag(pathState, nrcPathFlagHasExitedScene) && (context.constants.maxPathVertices > 1)) // && !NrcGetFlag(pathState, nrcPathFlagIsUnbiased))
487531
{
488-
NrcPathVertex vertex = NrcUnpackPathVertex(context.buffers.trainingPathVertices[arrayIndex], radiance, throughput);
489-
pathState.queryBufferIndex = NrcIncrementCounter(context.buffers.countersData, NrcCounter::Queries);
532+
NrcPathVertex vertex = NrcUnpackPathVertex(NRC_BUFFER_TRAINING_PATH_VERTICES[arrayIndex], radiance, throughput);
533+
pathState.queryBufferIndex = NrcIncrementCounter(NRC_BUFFER_QUERY_COUNTERS_DATA, NrcCounter::Queries);
490534

491-
context.buffers.queryRadianceParams[pathState.queryBufferIndex] = NrcCreateRadianceParams(vertex);
535+
NRC_BUFFER_QUERY_RADIANCE_PARAMS[pathState.queryBufferIndex] = NrcCreateRadianceParams(vertex);
492536
}
493537
}
494538

@@ -498,7 +542,7 @@ void NrcWriteFinalPathInfo(in NrcContext context,
498542
unpackedPathInfo.queryBufferIndex = pathState.queryBufferIndex;
499543

500544
const uint trainingPathIndex = NrcCalculateTrainingPathIndex(context.constants.trainingDimensions, context.pixelIndex);
501-
context.buffers.trainingPathInfo[trainingPathIndex] = NrcPackTrainingPathInfo(unpackedPathInfo);
545+
NRC_BUFFER_TRAINING_PATH_INFO[trainingPathIndex] = NrcPackTrainingPathInfo(unpackedPathInfo);
502546

503547
}
504548
else
@@ -515,7 +559,7 @@ void NrcWriteFinalPathInfo(in NrcContext context,
515559
packedQueryPathInfo.prefixThroughput = pathState.packedPrefixThroughput;
516560
packedQueryPathInfo.queryBufferIndex = pathState.queryBufferIndex;
517561

518-
context.buffers.queryPathInfo[queryPathIndex] = packedQueryPathInfo;
562+
NRC_BUFFER_QUERY_PATH_INFO[queryPathIndex] = packedQueryPathInfo;
519563
}
520564
}
521565

include/NrcCommon.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#define NRC_VERSION_MAJOR 0
2222
#define NRC_VERSION_MINOR 9
23-
#define NRC_VERSION_DATE "24 March 2023"
23+
#define NRC_VERSION_DATE "18 March 2024"
2424

2525
namespace nrc
2626
{
@@ -258,7 +258,6 @@ struct AllocationInfo
258258
bool isOnSharedHeap;
259259
bool allowUAV;
260260
bool useReadbackHeap;
261-
uint32_t initialState; // VkBufferUsageFlags or D3D12_RESOURCE_STATES
262261
const char* debugName;
263262
};
264263

include/NrcD3d12.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,15 @@ class Context
139139
NRC_DECLSPEC const Buffers* GetBuffers() const;
140140

141141
/**
142-
* Returns pointer to the device used to initialize this NRC Context
142+
* Returns pointer to the device used to initialize this NRC Context.
143143
*/
144144
NRC_DECLSPEC ID3D12Device5* GetDevice() const;
145145

146+
/**
147+
* Returns the initial resource state decided based on type and usage.
148+
*/
149+
NRC_DECLSPEC D3D12_RESOURCE_STATES GetInitialBufferState(AllocationInfo const& allocationInfo) const;
150+
146151
protected:
147152
// Prevent user code from being able to instanciate this class
148153
Context()

include/NrcHelpers.hlsli

+4-7
Original file line numberDiff line numberDiff line change
@@ -447,11 +447,11 @@ NrcPathVertex NrcUnpackPathVertex(const NrcPackedPathVertex packed)
447447
}
448448

449449
/** Increments the specified counter by one
450-
\param[in] counterData Counter buffer to update.
450+
\param[in] countersData Counter buffer to update.
451451
\param[in] counterID ID (index) of the counter to update.
452452
\return Original counter value before the increment.
453453
*/
454-
uint NrcIncrementCounter(RWByteAddressBuffer counterData, NrcCounter counter)
454+
uint NrcIncrementCounter(NRC_RW_STRUCTURED_BUFFER(uint) countersData, NrcCounter counter)
455455
{
456456
#if 1 //< Optimized version with one InterlockedAdd per warp. This can be always on
457457

@@ -460,17 +460,14 @@ uint NrcIncrementCounter(RWByteAddressBuffer counterData, NrcCounter counter)
460460
uint originalValue;
461461
if (WaveIsFirstLane())
462462
{
463-
counterData.InterlockedAdd(((uint)counter) * 4, laneCount, originalValue);
463+
InterlockedAdd(countersData[(uint)counter], laneCount, originalValue);
464464
}
465465
originalValue = WaveReadLaneFirst(originalValue); // Broadcast to all active threads
466466
return originalValue + laneOffset;
467-
468467
#else //< Reference implementation with one InterlockedAdd per thread
469-
470468
uint originalValue;
471-
counterData.InterlockedAdd(counterID * 4, 1, originalValue);
469+
InterlockedAdd(countersData[(uint)counter], 1, originalValue);
472470
return originalValue;
473-
474471
#endif
475472
}
476473

include/NrcStructures.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,12 @@ struct NrcSurfaceAttributes
288288
*/
289289
struct NrcBuffers
290290
{
291-
RWStructuredBuffer<NrcPackedQueryPathInfo> queryPathInfo;
292-
RWStructuredBuffer<NrcPackedTrainingPathInfo> trainingPathInfo;
293-
RWStructuredBuffer<NrcPackedPathVertex> trainingPathVertices;
294-
RWStructuredBuffer<NrcRadianceParams> queryRadianceParams;
295-
RWByteAddressBuffer countersData;
296-
#if NRC_DEBUG_BUFFERS
297-
RWStructuredBuffer<NrcDebugTrainingPathInfo> debugTrainingPathInfo;
291+
#if !NRC_USE_CUSTOM_BUFFER_ACCESSORS
292+
NRC_RW_STRUCTURED_BUFFER(NrcPackedQueryPathInfo) queryPathInfo;
293+
NRC_RW_STRUCTURED_BUFFER(NrcPackedTrainingPathInfo) trainingPathInfo;
294+
NRC_RW_STRUCTURED_BUFFER(NrcPackedPathVertex) trainingPathVertices;
295+
NRC_RW_STRUCTURED_BUFFER(NrcRadianceParams) queryRadianceParams;
296+
NRC_RW_STRUCTURED_BUFFER(uint) countersData;
298297
#endif
299298
};
300299

include/NrcVk.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@ class Context
169169
NRC_DECLSPEC const Buffers* GetBuffers() const;
170170

171171
/**
172-
* Returns pointer to the device used to initialize this NRC Context
172+
* Returns pointer to the device used to initialize this NRC Context.
173173
*/
174174
NRC_DECLSPEC VkDevice GetDevice() const;
175175

176+
/**
177+
* Returns resource usage flags.
178+
*/
179+
NRC_DECLSPEC VkBufferUsageFlags GetBufferUsageFlags(AllocationInfo const& allocationInfo) const;
180+
176181
protected:
177182
// Prevent user code from being able to instanciate this class
178183
Context()

lib/NRC_D3D12.lib

608 Bytes
Binary file not shown.

lib/NRC_Vulkan.lib

474 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)