Skip to content

Commit 3d206fd

Browse files
committed
RWMC setup
1 parent 3400a2a commit 3d206fd

File tree

5 files changed

+231
-44
lines changed

5 files changed

+231
-44
lines changed

31_HLSLPathTracer/app_resources/hlsl/pathtracer.hlsl

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,32 +265,37 @@ struct Unidirectional
265265
// #endif
266266
}
267267

268+
measure_type getSingleSampleMeasure(uint32_t sampleID, uint32_t depth, NBL_CONST_REF_ARG(scene_type) scene)
269+
{
270+
vector3_type uvw = rand3d(0u, sampleID, randGen.rng()); // TODO: take from scramblebuf?
271+
ray_type ray = rayGen.generate(uvw);
272+
273+
// bounces
274+
bool hit = true;
275+
bool rayAlive = true;
276+
for (int d = 1; (d <= depth) && hit && rayAlive; d += 2)
277+
{
278+
ray.intersectionT = numeric_limits<scalar_type>::max;
279+
ray.objectID = intersector_type::traceRay(ray, scene);
280+
281+
hit = ray.objectID.id != -1;
282+
if (hit)
283+
rayAlive = closestHitProgram(1, sampleID, ray, scene);
284+
}
285+
if (!hit)
286+
missProgram(ray);
287+
288+
return ray.payload.accumulation;
289+
}
290+
268291
// Li
269292
measure_type getMeasure(uint32_t numSamples, uint32_t depth, NBL_CONST_REF_ARG(scene_type) scene)
270293
{
271294
measure_type Li = (measure_type)0.0;
272295
scalar_type meanLumaSq = 0.0;
273296
for (uint32_t i = 0; i < numSamples; i++)
274297
{
275-
vector3_type uvw = rand3d(0u, i, randGen.rng()); // TODO: take from scramblebuf?
276-
ray_type ray = rayGen.generate(uvw);
277-
278-
// bounces
279-
bool hit = true;
280-
bool rayAlive = true;
281-
for (int d = 1; (d <= depth) && hit && rayAlive; d += 2)
282-
{
283-
ray.intersectionT = numeric_limits<scalar_type>::max;
284-
ray.objectID = intersector_type::traceRay(ray, scene);
285-
286-
hit = ray.objectID.id != -1;
287-
if (hit)
288-
rayAlive = closestHitProgram(1, i, ray, scene);
289-
}
290-
if (!hit)
291-
missProgram(ray);
292-
293-
measure_type accumulation = ray.payload.accumulation;
298+
measure_type accumulation = getSingleSampleMeasure(i, depth, scene);
294299
scalar_type rcpSampleSize = 1.0 / (i + 1);
295300
Li += (accumulation - Li) * rcpSampleSize;
296301

@@ -302,6 +307,22 @@ struct Unidirectional
302307
return Li;
303308
}
304309

310+
void generateCascades(int32_t2 coords, uint32_t numSamples, uint32_t depth, NBL_CONST_REF_ARG(scene_type) scene)
311+
{
312+
measure_type Li = (measure_type)0.0;
313+
scalar_type meanLumaSq = 0.0;
314+
for (uint32_t i = 0; i < numSamples; i++)
315+
{
316+
measure_type accumulation = getSingleSampleMeasure(i, depth, scene);
317+
scalar_type rcpSampleSize = 1.0 / (i + 1);
318+
Li += (accumulation - Li) * rcpSampleSize;
319+
}
320+
321+
cascade[uint3(coords.x, coords.y, 0)] = float4(Li.r, 0.0f, 0.0f, 0.0f);
322+
cascade[uint3(coords.x, coords.y, 1)] = float4(0.0f, Li.g, 0.0f, 0.0f);
323+
cascade[uint3(coords.x, coords.y, 2)] = float4(0.0f, 0.0f, Li.b, 0.0f);
324+
}
325+
305326
NBL_CONSTEXPR_STATIC_INLINE uint32_t MAX_DEPTH_LOG2 = 4u;
306327
NBL_CONSTEXPR_STATIC_INLINE uint32_t MAX_SAMPLES_LOG2 = 10u;
307328

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

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

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

220-
float32_t3 color = pathtracer.getMeasure(pc.sampleCount, pc.depth, scene);
221-
float32_t4 pixCol = float32_t4(color, 1.0);
222-
outImage[coords] = pixCol;
220+
bool useRWMC = true; // TODO: move to push constants if we keep it
221+
if(!useRWMC)
222+
{
223+
float32_t3 color = pathtracer.getMeasure(pc.sampleCount, pc.depth, scene);
224+
float32_t4 pixCol = float32_t4(color, 1.0);
225+
outImage[coords] = pixCol;
226+
}
227+
else
228+
{
229+
pathtracer.generateCascades(coords, pc.sampleCount, pc.depth, scene);
230+
}
223231

224232
#ifdef PERSISTENT_WORKGROUPS
225233
}

31_HLSLPathTracer/app_resources/hlsl/render_common.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ struct SPushConstants
1919
[[vk::combinedImageSampler]][[vk::binding(2, 2)]] SamplerState scrambleSampler;
2020

2121
[[vk::image_format("rgba16f")]][[vk::binding(0, 0)]] RWTexture2D<float32_t4> outImage;
22+
[[vk::image_format("rgba16f")]][[vk::binding(1, 0)]] RWTexture2DArray<float32_t4> cascade;
2223

2324
#endif
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
2+
3+
[[vk::image_format("rgba16f")]] [[vk::binding(0, 0)]] RWTexture2D<float32_t4> outImage;
4+
[[vk::image_format("rgba16f")]] [[vk::binding(1, 0)]] RWTexture2DArray<float32_t4> cascade;
5+
6+
using namespace nbl;
7+
using namespace hlsl;
8+
9+
NBL_CONSTEXPR uint32_t WorkgroupSize = 512;
10+
NBL_CONSTEXPR uint32_t MAX_DEPTH_LOG2 = 4;
11+
NBL_CONSTEXPR uint32_t MAX_SAMPLES_LOG2 = 10;
12+
13+
int32_t2 getCoordinates()
14+
{
15+
uint32_t width, height;
16+
outImage.GetDimensions(width, height);
17+
return int32_t2(glsl::gl_GlobalInvocationID().x % width, glsl::gl_GlobalInvocationID().x / width);
18+
}
19+
20+
float calculateLumaRec709(float32_t4 color)
21+
{
22+
return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
23+
}
24+
25+
[numthreads(WorkgroupSize, 1, 1)]
26+
void main(uint32_t3 threadID : SV_DispatchThreadID)
27+
{
28+
const int32_t2 coords = getCoordinates();
29+
30+
float r = cascade.Load(uint3(coords, 0)).r;
31+
float g = cascade.Load(uint3(coords, 1)).g;
32+
float b = cascade.Load(uint3(coords, 2)).b;
33+
float32_t4 color = float32_t4(r, g, b, 1.0f);
34+
float luma = calculateLumaRec709(color);
35+
float32_t4 output = float32_t4(luma, luma, luma, 1.0f);
36+
37+
outImage[coords] = output;
38+
}

0 commit comments

Comments
 (0)