Skip to content

Commit ca8c232

Browse files
committed
Added "enable RWMC" toggle
1 parent 389248c commit ca8c232

File tree

5 files changed

+39
-75
lines changed

5 files changed

+39
-75
lines changed

31_HLSLPathTracer/app_resources/hlsl/pathtracer.hlsl

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,14 @@ struct Unidirectional
314314
uint32_t base;
315315
};
316316

317-
/**
318-
* @brief Resets all buffers in the cascade to 0 at the given pixel coordinates.
319-
*
320-
* This function writes zero values to every buffer in the cascade
321-
* for the specified 2D pixel location.
322-
*
323-
* @param coords Integer 2D coordinates of the pixel to reset.
324-
* @param cascadeSize number of buffers in the cascade to clear.
325-
*/
326-
void resetCascade(NBL_CONST_REF_ARG(int32_t2) coords, uint32_t cascadeSize)
327-
{
328-
for (int i = 0; i < 6; ++i)
329-
cascade[uint3(coords.x, coords.y, i)] = float32_t4(0.0f, 0.0f, 0.0f, 0.0f);
330-
}
331-
332317
void generateCascade(int32_t2 coords, uint32_t numSamples, uint32_t depth, NBL_CONST_REF_ARG(RWMCCascadeSettings) cascadeSettings, NBL_CONST_REF_ARG(scene_type) scene)
333318
{
319+
// TODO: move `MaxCascadeSize` somewhere else
320+
const static uint32_t MaxCascadeSize = 10u;
321+
float32_t4 cascadeEntry[MaxCascadeSize];
322+
for (int i = 0; i < MaxCascadeSize; ++i)
323+
cascadeEntry[i] = float32_t4(0.0f, 0.0f, 0.0f, 0.0f);
324+
334325
float lowerScale = cascadeSettings.start;
335326
float upperScale = lowerScale * cascadeSettings.base;
336327

@@ -364,8 +355,14 @@ struct Unidirectional
364355
else
365356
higherCascadeLevelWeight = upperScale / luma;
366357

367-
cascade[uint3(coords.x, coords.y, lowerCascadeIndex)] += float32_t4(accumulation * lowerCascadeLevelWeight, 1.0f);
368-
cascade[uint3(coords.x, coords.y, lowerCascadeIndex + 1u)] += float32_t4(accumulation * higherCascadeLevelWeight, 1.0f);
358+
// TODO: odrazu liczyc srednia
359+
cascadeEntry[lowerCascadeIndex] += float32_t4(accumulation * lowerCascadeLevelWeight, 1.0f);
360+
}
361+
362+
for (uint32_t i = 0; i < 6; i++)
363+
{
364+
cascadeEntry[i] /= float(numSamples);
365+
cascade[uint3(coords.x, coords.y, i)] = cascadeEntry[i];
369366
}
370367
}
371368

31_HLSLPathTracer/app_resources/hlsl/render.comp.hlsl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
217217

218218
pathtracer_type pathtracer = pathtracer_type::create(ptCreateParams);
219219

220-
bool useRWMC = true; // TODO: move to push constants if we keep it
221-
if(!useRWMC)
220+
bool useRWMC = bool(pc.useRWMC);
221+
if (!useRWMC)
222222
{
223223
float32_t3 color = pathtracer.getMeasure(pc.sampleCount, pc.depth, scene);
224224
float32_t4 pixCol = float32_t4(color, 1.0);
@@ -227,11 +227,11 @@ void main(uint32_t3 threadID : SV_DispatchThreadID)
227227
else
228228
{
229229
pathtracer_type::RWMCCascadeSettings cascadeSettings;
230-
cascadeSettings.size = 6u;
231-
cascadeSettings.start = 1u;
232-
cascadeSettings.base = 8u;
230+
cascadeSettings.size = pc.rwmcCascadeSize;
231+
cascadeSettings.start = pc.rwmcCascadeStart;
232+
cascadeSettings.base = pc.rwmcCascadeBase;
233233

234-
pathtracer.resetCascade(coords, 6u);
234+
// TODO: template parameter should be
235235
pathtracer.generateCascade(coords, pc.sampleCount, pc.depth, cascadeSettings, scene);
236236
}
237237

31_HLSLPathTracer/app_resources/hlsl/render_common.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct SPushConstants
77
int sampleCount;
88
int depth;
99
uint32_t rwmcCascadeSize;
10+
int useRWMC;
1011
uint32_t rwmcCascadeStart;
1112
uint32_t rwmcCascadeBase;
1213
};

31_HLSLPathTracer/app_resources/hlsl/reweighting.hlsl renamed to 31_HLSLPathTracer/app_resources/hlsl/resolve.comp.hlsl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ float32_t3 RWMCReweight(in RWMCReweightingParameters params, in int32_t2 coord)
117117
next = RWMCSampleCascade(coord, i + 1u, reciprocalBaseI);
118118
}
119119

120-
121120
float reliability = 1.f;
122121
// sample counting-based reliability estimation
123122
if (params.reciprocalKappa <= 1.f)
@@ -179,25 +178,17 @@ float32_t3 sumCascade(in const int32_t2 coords)
179178
accumulation += float32_t3(cascadeLevel.r, cascadeLevel.g, cascadeLevel.b);
180179
}
181180

182-
accumulation /= 32.0f;
183-
184181
return accumulation;
185182
}
186183

187184
[numthreads(WorkgroupSize, 1, 1)]
188185
void main(uint32_t3 threadID : SV_DispatchThreadID)
189186
{
190-
// TODO: remove, ideally shader should not be called at all when we don't use RWMC
191-
bool useRWMC = true;
192-
if (!useRWMC)
193-
return;
194-
195187
const int32_t2 coords = getCoordinates();
196188
//float32_t3 color = sumCascade(coords);
197189

198190
RWMCReweightingParameters reweightingParameters = computeReweightingParameters(pc.cascadeCount, pc.base, pc.sampleCount, pc.minReliableLuma, pc.kappa);
199191
float32_t3 color = RWMCReweight(reweightingParameters, coords);
200-
color /= pc.sampleCount;
201192

202193
outImage[coords] = float32_t4(color, 1.0f);
203194
}

31_HLSLPathTracer/main.cpp

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct PTPushConstant
2222
int sampleCount;
2323
int depth;
2424
const uint32_t rwmcCascadeSize = CascadeSize;
25+
int useRWMC;
2526
uint32_t rwmcCascadeStart;
2627
uint32_t rwmcCascadeBase;
2728
};
@@ -74,7 +75,7 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
7475
static inline std::array<std::string, E_LIGHT_GEOMETRY::ELG_COUNT> PTGLSLShaderPaths = { "app_resources/glsl/litBySphere.comp", "app_resources/glsl/litByTriangle.comp", "app_resources/glsl/litByRectangle.comp" };
7576
static inline std::string PTHLSLShaderPath = "app_resources/hlsl/render.comp.hlsl";
7677
static inline std::array<std::string, E_LIGHT_GEOMETRY::ELG_COUNT> PTHLSLShaderVariants = { "SPHERE_LIGHT", "TRIANGLE_LIGHT", "RECTANGLE_LIGHT" };
77-
static inline std::string ReweightingShaderPath = "app_resources/hlsl/reweighting.hlsl";
78+
static inline std::string ReweightingShaderPath = "app_resources/hlsl/resolve.comp.hlsl";
7879
static inline std::string PresentShaderPath = "app_resources/hlsl/present.frag.hlsl";
7980

8081
const char* shaderNames[E_LIGHT_GEOMETRY::ELG_COUNT] = {
@@ -1096,6 +1097,12 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
10961097

10971098
ImGui::Text("X: %f Y: %f", io.MousePos.x, io.MousePos.y);
10981099

1100+
ImGui::Text("\nRWMC settings:");
1101+
ImGui::Checkbox("Enable RWMC", &useRWMC);
1102+
ImGui::SliderFloat("base", &rwmcPushConstants.base, 1.0f, 32.0f);
1103+
ImGui::SliderFloat("minReliableLuma", &rwmcPushConstants.minReliableLuma, 0.1f, 32.0f);
1104+
ImGui::SliderFloat("kappa", &rwmcPushConstants.kappa, 0.1f, 32.0f);
1105+
10991106
ImGui::End();
11001107
}
11011108
);
@@ -1151,9 +1158,6 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
11511158
return m_device->updateDescriptorSets(writes, {});
11521159
}
11531160

1154-
// TODO: DON'T DO THAT! tansition layout once at the initialization stage
1155-
bool cascadeLayoutTransitioned = false;
1156-
11571161
inline void workLoopBody() override
11581162
{
11591163
// framesInFlight: ensuring safe execution of command buffers and acquires, `framesInFlight` only affect semaphore waits, don't use this to index your resources because it can change with swapchain recreation.
@@ -1192,6 +1196,7 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
11921196
// disregard surface/swapchain transformation for now
11931197
const auto viewProjectionMatrix = m_camera.getConcatenatedMatrix();
11941198
viewProjectionMatrix.getInverseTransform(pc.invMVP);
1199+
pc.useRWMC = useRWMC ? 1 : 0;
11951200
pc.sampleCount = spp;
11961201
pc.depth = depth;
11971202

@@ -1229,19 +1234,17 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
12291234
cmdbuf->pipelineBarrier(E_DEPENDENCY_FLAGS::EDF_NONE, { .imgBarriers = imgBarriers });
12301235
}
12311236

1232-
// TODO: remove! we want to transition cascade layout only once right after its creation
1233-
if (!cascadeLayoutTransitioned)
1237+
// transit m_cascadeView layout to GENERAL, block until previous shader is done with reading from cascade
1238+
if (useRWMC)
12341239
{
1235-
cascadeLayoutTransitioned = true;
1236-
12371240
const IGPUCommandBuffer::SImageMemoryBarrier<IGPUCommandBuffer::SOwnershipTransferBarrier> cascadeBarrier[] = {
12381241
{
12391242
.barrier = {
12401243
.dep = {
1241-
.srcStageMask = PIPELINE_STAGE_FLAGS::NONE,
1244+
.srcStageMask = PIPELINE_STAGE_FLAGS::COMPUTE_SHADER_BIT,
12421245
.srcAccessMask = ACCESS_FLAGS::NONE,
12431246
.dstStageMask = PIPELINE_STAGE_FLAGS::COMPUTE_SHADER_BIT,
1244-
.dstAccessMask = ACCESS_FLAGS::SHADER_WRITE_BITS
1247+
.dstAccessMask = ACCESS_FLAGS::NONE
12451248
}
12461249
},
12471250
.image = m_cascadeView->getCreationParameters().image.get(),
@@ -1250,7 +1253,7 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
12501253
.baseMipLevel = 0u,
12511254
.levelCount = 1u,
12521255
.baseArrayLayer = 0u,
1253-
.layerCount = 6u
1256+
.layerCount = CascadeSize
12541257
},
12551258
.oldLayout = IImage::LAYOUT::UNDEFINED,
12561259
.newLayout = IImage::LAYOUT::GENERAL
@@ -1298,16 +1301,15 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
12981301
.baseMipLevel = 0u,
12991302
.levelCount = 1u,
13001303
.baseArrayLayer = 0u,
1301-
.layerCount = 6u
1302-
},
1303-
.oldLayout = IImage::LAYOUT::GENERAL,
1304-
.newLayout = IImage::LAYOUT::GENERAL
1304+
.layerCount = CascadeSize
1305+
}
13051306
}
13061307
};
13071308
cmdbuf->pipelineBarrier(E_DEPENDENCY_FLAGS::EDF_NONE, { .imgBarriers = cascadeBarrier });
13081309
}
13091310

13101311
// reweighting
1312+
if(useRWMC)
13111313
{
13121314
IGPUComputePipeline* pipeline;
13131315
if (usePersistentWorkGroups)
@@ -1354,34 +1356,6 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
13541356
cmdbuf->pipelineBarrier(E_DEPENDENCY_FLAGS::EDF_NONE, { .imgBarriers = imgBarriers });
13551357
}
13561358

1357-
// m_cascadeView synchronization - wait for previous compute shader to zero-out the cascade
1358-
// TODO: create this and every other barrier once outside of the loop?
1359-
{
1360-
const IGPUCommandBuffer::SImageMemoryBarrier<IGPUCommandBuffer::SOwnershipTransferBarrier> cascadeBarrier[] = {
1361-
{
1362-
.barrier = {
1363-
.dep = {
1364-
.srcStageMask = PIPELINE_STAGE_FLAGS::COMPUTE_SHADER_BIT,
1365-
.srcAccessMask = ACCESS_FLAGS::SHADER_WRITE_BITS,
1366-
.dstStageMask = PIPELINE_STAGE_FLAGS::COMPUTE_SHADER_BIT,
1367-
.dstAccessMask = ACCESS_FLAGS::SHADER_WRITE_BITS
1368-
}
1369-
},
1370-
.image = m_cascadeView->getCreationParameters().image.get(),
1371-
.subresourceRange = {
1372-
.aspectMask = IImage::EAF_COLOR_BIT,
1373-
.baseMipLevel = 0u,
1374-
.levelCount = 1u,
1375-
.baseArrayLayer = 0u,
1376-
.layerCount = 6u
1377-
},
1378-
.oldLayout = IImage::LAYOUT::GENERAL,
1379-
.newLayout = IImage::LAYOUT::GENERAL
1380-
}
1381-
};
1382-
cmdbuf->pipelineBarrier(E_DEPENDENCY_FLAGS::EDF_NONE, { .imgBarriers = cascadeBarrier });
1383-
}
1384-
13851359
// TODO: tone mapping and stuff
13861360
}
13871361

@@ -1640,6 +1614,7 @@ class HLSLComputePathtracer final : public examples::SimpleWindowedApplication,
16401614
int spp = 32;
16411615
int depth = 3;
16421616
bool usePersistentWorkGroups = false;
1617+
bool useRWMC = false;
16431618
RWMCPushConstants rwmcPushConstants;
16441619
PTPushConstant pc;
16451620

0 commit comments

Comments
 (0)