Skip to content

Commit 0905053

Browse files
author
devsh
committed
Correct Alpha handling in RT Pipeline example, also work around microsoft/DirectXShaderCompiler#6464
1 parent 6766420 commit 0905053

File tree

5 files changed

+28
-15
lines changed

5 files changed

+28
-15
lines changed

71_RayTracingPipeline/app_resources/common.hlsl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
55
#include "nbl/builtin/hlsl/cpp_compat/basic.h"
6+
#include "nbl/builtin/hlsl/random/pcg.hlsl"
67

78
NBL_CONSTEXPR uint32_t WorkgroupSize = 16;
89
NBL_CONSTEXPR uint32_t MAX_UNORM_10 = 1023;
@@ -196,10 +197,20 @@ struct MaterialId
196197

197198
struct [raypayload] PrimaryPayload
198199
{
199-
float32_t3 worldNormal : read(caller) : write(closesthit);
200-
float32_t rayDistance : read(caller) : write(closesthit, miss);
201-
float32_t alphaThreshold : read(closesthit, anyhit) : write(caller);
202-
MaterialId materialId : read(caller) : write(closesthit);
200+
using generator_t = nbl::hlsl::random::Pcg;
201+
/* bugged out by https://github.com/microsoft/DirectXShaderCompiler/issues/6464
202+
bool nextDiscard(const float32_t alpha)
203+
{
204+
const uint32_t bitpattern = pcg();
205+
const float32_t xi = (float32_t(bitpattern)+0.5f)/float32_t(0xFFFFFFFF);
206+
return xi > alpha;
207+
}
208+
*/
209+
210+
float32_t3 worldNormal : read(caller) : write(closesthit);
211+
float32_t rayDistance : read(caller) : write(closesthit, miss);
212+
generator_t pcg : read(anyhit) : write(caller,anyhit);
213+
MaterialId materialId : read(caller) : write(closesthit);
203214

204215
};
205216

71_RayTracingPipeline/app_resources/raytrace.rahit.hlsl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes
77
{
88
const int instID = InstanceID();
99
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo));
10-
const Material material = nbl::hlsl::_static_cast<Material>(geom.material);
11-
12-
if (material.alpha > payload.alphaThreshold)
13-
{
10+
11+
// Should have been a method of the payload but https://github.com/microsoft/DirectXShaderCompiler/issues/6464 stops it
12+
// alpha is quantized to 10 bits
13+
const uint32_t bitpattern = payload.pcg()>>22;
14+
if (bitpattern > geom.material.alpha)
1415
IgnoreHit();
15-
}
1616
}

71_RayTracingPipeline/app_resources/raytrace.rgen.hlsl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include "nbl/builtin/hlsl/jit/device_capabilities.hlsl"
44
#include "nbl/builtin/hlsl/random/xoroshiro.hlsl"
5-
#include "nbl/builtin/hlsl/random/pcg.hlsl"
65

76
#include "nbl/builtin/hlsl/glsl_compat/core.hlsl"
87
#include "nbl/builtin/hlsl/spirv_intrinsics/raytracing.hlsl"
@@ -28,8 +27,8 @@ void main()
2827
const uint32_t3 launchSize = DispatchRaysDimensions();
2928
const uint32_t2 coords = launchID.xy;
3029

31-
const uint32_t seed1 = nbl::hlsl::Pcg::construct(pc.frameCounter)();
32-
const uint32_t seed2 = nbl::hlsl::Pcg::construct(launchID.y * launchSize.x + launchID.x)();
30+
const uint32_t seed1 = nbl::hlsl::random::Pcg::create(pc.frameCounter)();
31+
const uint32_t seed2 = nbl::hlsl::random::Pcg::create(launchID.y * launchSize.x + launchID.x)();
3332
nbl::hlsl::Xoroshiro64StarStar rnd = nbl::hlsl::Xoroshiro64StarStar::construct(uint32_t2(seed1, seed2));
3433

3534
float32_t3 hitValues = float32_t3(0, 0, 0);
@@ -55,7 +54,7 @@ void main()
5554
rayDesc.TMax = 10000.0;
5655

5756
PrimaryPayload payload;
58-
payload.alphaThreshold = nextRandomUnorm(rnd);
57+
payload.pcg = PrimaryPayload::generator_t::create(rnd());
5958
TraceRay(topLevelAS, RAY_FLAG_NONE, 0xff, ERT_PRIMARY, 0, EMT_PRIMARY, rayDesc, payload);
6059

6160
const float32_t rayDistance = payload.rayDistance;

71_RayTracingPipeline/app_resources/raytrace_shadow.rahit.hlsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ void main(inout OcclusionPayload payload, in BuiltInTriangleIntersectionAttribut
99
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo));
1010
const Material material = nbl::hlsl::_static_cast<Material>(geom.material);
1111

12-
payload.attenuation = material.alpha * payload.attenuation;
12+
payload.attenuation = (1.f-material.alpha) * payload.attenuation;
13+
// arbitrary constant
14+
// if (payload.attenuation < 1.f/1024.f)
15+
// TerminateRay();
1316
IgnoreHit();
1417
}

71_RayTracingPipeline/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ class RaytracingPipelineApp final : public examples::SimpleWindowedApplication,
11301130
.diffuse = {0.2, 0.8, 0.2},
11311131
.specular = {0.8, 0.8, 0.8},
11321132
.shininess = 1.0f,
1133-
.alpha = 0.8,
1133+
.alpha = 0.2,
11341134
},
11351135
.transform = getTranslationMatrix(5.0f, 1.0f, 0),
11361136
},

0 commit comments

Comments
 (0)