Skip to content

Commit 699f263

Browse files
author
devsh
committed
clean up the code a bit more, address comments in #173
1 parent 0905053 commit 699f263

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

71_RayTracingPipeline/app_resources/common.hlsl

+13-14
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ struct Material
5353
{
5454
return alpha < 1.0;
5555
}
56+
57+
bool alphaTest(const float32_t xi) NBL_CONST_MEMBER_FUNC
58+
{
59+
return xi > alpha;
60+
}
5661
};
5762

5863
struct MaterialPacked
@@ -67,6 +72,11 @@ struct MaterialPacked
6772
{
6873
return alpha != MAX_UNORM_10;
6974
}
75+
76+
bool alphaTest(const uint32_t xi) NBL_CONST_MEMBER_FUNC
77+
{
78+
return (xi>>22) > alpha;
79+
}
7080
};
7181

7282
struct SProceduralGeomInfo
@@ -198,14 +208,6 @@ struct MaterialId
198208
struct [raypayload] PrimaryPayload
199209
{
200210
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-
*/
209211

210212
float32_t3 worldNormal : read(caller) : write(closesthit);
211213
float32_t rayDistance : read(caller) : write(closesthit, miss);
@@ -335,12 +337,9 @@ float32_t3 fetchVertexNormal(int instID, int primID, STriangleGeomInfo geom, flo
335337
case OT_ICOSPHERE:
336338
default:
337339
{
338-
n0 = normalize(vk::RawBufferLoad <
339-
float3 > (normalVertexBufferAddress + i0 * vertexStride));
340-
n1 = normalize(vk::RawBufferLoad <
341-
float3 > (normalVertexBufferAddress + i1 * vertexStride));
342-
n2 = normalize(vk::RawBufferLoad <
343-
float3 > (normalVertexBufferAddress + i2 * vertexStride));
340+
n0 = vk::RawBufferLoad < float3 > (normalVertexBufferAddress + i0 * vertexStride);
341+
n1 = vk::RawBufferLoad < float3 > (normalVertexBufferAddress + i1 * vertexStride);
342+
n2 = vk::RawBufferLoad < float3 > (normalVertexBufferAddress + i2 * vertexStride);
344343
}
345344
}
346345

71_RayTracingPipeline/app_resources/raytrace.rahit.hlsl

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ void main(inout PrimaryPayload payload, in BuiltInTriangleIntersectionAttributes
88
const int instID = InstanceID();
99
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo));
1010

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)
11+
const uint32_t bitpattern = payload.pcg();
12+
if (geom.material.alphaTest(bitpattern))
1513
IgnoreHit();
1614
}

71_RayTracingPipeline/app_resources/raytrace.rgen.hlsl

+6-3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ void main()
6565
}
6666

6767
const float32_t3 worldPosition = pc.camPos + (camDirection * rayDistance);
68+
69+
// make sure to call with least live state
70+
RayLight cLight;
71+
cLight.inHitPosition = worldPosition;
72+
CallShader(pc.light.type, cLight);
73+
6874
const float32_t3 worldNormal = payload.worldNormal;
6975

7076
Material material;
@@ -80,9 +86,6 @@ void main()
8086
const MaterialPacked materialPacked = vk::RawBufferLoad<MaterialPacked>(pc.triangleGeomInfoBuffer + materialId.getMaterialIndex() * sizeof(STriangleGeomInfo));
8187
material = nbl::hlsl::_static_cast<Material>(materialPacked);
8288
}
83-
RayLight cLight;
84-
cLight.inHitPosition = worldPosition;
85-
CallShader(pc.light.type, cLight);
8689

8790
float32_t attenuation = 1;
8891

71_RayTracingPipeline/app_resources/raytrace.rint.hlsl

+2-8
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@ float32_t hitSphere(SProceduralGeomInfo s, Ray r)
1818
float32_t c = dot(oc, oc) - s.radius * s.radius;
1919
float32_t discriminant = b * b - 4 * a * c;
2020

21-
if (discriminant < 0)
22-
{
23-
return -1.0;
24-
}
25-
else
26-
{
27-
return (-b - sqrt(discriminant)) / (2.0 * a);
28-
}
21+
// return whatever, if the discriminant is negative, it will produce a NaN, and NaN will compare false
22+
return (-b - sqrt(discriminant)) / (2.0 * a);
2923
}
3024

3125
[shader("intersection")]

0 commit comments

Comments
 (0)