Skip to content

Commit bb2fd06

Browse files
author
devsh
committed
do shadows without any closest hit shaders, one miss shader instead
1 parent 699f263 commit bb2fd06

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

71_RayTracingPipeline/app_resources/common.hlsl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,10 @@ struct RayLight
170170

171171
struct [raypayload] OcclusionPayload
172172
{
173-
float32_t attenuation : read(caller) : write(caller, anyhit);
173+
// TODO: will this break DXC? Tbh should come from push constant or some autoexposure feedback
174+
// NBL_CONSTEXPR_STATIC_INLINE float32_t MinAttenuation = 1.f/1024.f;
175+
176+
float32_t attenuation : read(caller,anyhit,miss) : write(caller,anyhit,miss);
174177
};
175178

176179
struct MaterialId
@@ -210,7 +213,7 @@ struct [raypayload] PrimaryPayload
210213
using generator_t = nbl::hlsl::random::Pcg;
211214

212215
float32_t3 worldNormal : read(caller) : write(closesthit);
213-
float32_t rayDistance : read(caller) : write(closesthit, miss);
216+
float32_t rayDistance : read(caller) : write(closesthit,miss);
214217
generator_t pcg : read(anyhit) : write(caller,anyhit);
215218
MaterialId materialId : read(caller) : write(closesthit);
216219

71_RayTracingPipeline/app_resources/raytrace.rgen.hlsl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,15 @@ void main()
9797
rayDesc.TMin = 0.01;
9898
rayDesc.TMax = cLight.outLightDistance;
9999

100-
uint32_t shadowRayFlags = RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH;
101100
OcclusionPayload occlusionPayload;
102-
occlusionPayload.attenuation = 1;
101+
// negative means its a hit, the miss shader will flip it back around to positive
102+
occlusionPayload.attenuation = -1.f;
103+
// abuse of miss shader to mean "not hit shader" solves us having to call closest hit shaders
104+
uint32_t shadowRayFlags = RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER;
103105
TraceRay(topLevelAS, shadowRayFlags, 0xFF, ERT_OCCLUSION, 0, EMT_OCCLUSION, rayDesc, occlusionPayload);
104106

105107
attenuation = occlusionPayload.attenuation;
106-
if (occlusionPayload.attenuation > 0.0001)
108+
if (occlusionPayload.attenuation > 1.f/1024.f)
107109
{
108110
const float32_t3 diffuse = computeDiffuse(material, cLight.outLightDir, worldNormal);
109111
const float32_t3 specular = computeSpecular(material, camDirection, cLight.outLightDir, worldNormal);
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "common.hlsl"
2+
#include "nbl/builtin/hlsl/spirv_intrinsics/raytracing.hlsl"
23

34
[[vk::push_constant]] SPushConstants pc;
45

@@ -9,9 +10,11 @@ void main(inout OcclusionPayload payload, in BuiltInTriangleIntersectionAttribut
910
const STriangleGeomInfo geom = vk::RawBufferLoad < STriangleGeomInfo > (pc.triangleGeomInfoBuffer + instID * sizeof(STriangleGeomInfo));
1011
const Material material = nbl::hlsl::_static_cast<Material>(geom.material);
1112

12-
payload.attenuation = (1.f-material.alpha) * payload.attenuation;
13-
// arbitrary constant
14-
// if (payload.attenuation < 1.f/1024.f)
15-
// TerminateRay();
13+
const float attenuation = (1.f-material.alpha) * payload.attenuation;
14+
// DXC cogegens weird things in the presence of termination instructions
15+
payload.attenuation = attenuation;
16+
// arbitrary constant, whatever you want the smallest attenuation to be. Remember until miss, the attenuatio is negative
17+
if (attenuation > -1.f/1024.f)
18+
AcceptHitAndEndSearch();
1619
IgnoreHit();
1720
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "common.hlsl"
2+
3+
[shader("miss")]
4+
void main(inout OcclusionPayload payload)
5+
{
6+
// make positive
7+
payload.attenuation = -payload.attenuation;
8+
}

71_RayTracingPipeline/app_resources/raytrace_shadow_triangle.rchit.hlsl

Lines changed: 0 additions & 7 deletions
This file was deleted.

71_RayTracingPipeline/main.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class RaytracingPipelineApp final : public examples::SimpleWindowedApplication,
163163
const auto anyHitShaderColorPayload = loadCompileAndCreateShader("app_resources/raytrace.rahit.hlsl");
164164
const auto anyHitShaderShadowPayload = loadCompileAndCreateShader("app_resources/raytrace_shadow.rahit.hlsl");
165165
const auto missShader = loadCompileAndCreateShader("app_resources/raytrace.rmiss.hlsl");
166-
const auto shadowClosestHitShader = loadCompileAndCreateShader("app_resources/raytrace_shadow_triangle.rchit.hlsl");
166+
const auto missShadowShader = loadCompileAndCreateShader("app_resources/raytrace_shadow.rmiss.hlsl");
167167
const auto directionalLightCallShader = loadCompileAndCreateShader("app_resources/light_directional.rcall.hlsl");
168168
const auto pointLightCallShader = loadCompileAndCreateShader("app_resources/light_point.rcall.hlsl");
169169
const auto spotLightCallShader = loadCompileAndCreateShader("app_resources/light_spot.rcall.hlsl");
@@ -323,7 +323,7 @@ class RaytracingPipelineApp final : public examples::SimpleWindowedApplication,
323323
{
324324
RTDS_RAYGEN,
325325
RTDS_MISS,
326-
RTDS_CLOSEST_HIT_SHADOW,
326+
RTDS_MISS_SHADOW,
327327
RTDS_CLOSEST_HIT,
328328
RTDS_SPHERE_CLOSEST_HIT,
329329
RTDS_ANYHIT_PRIMARY,
@@ -338,7 +338,7 @@ class RaytracingPipelineApp final : public examples::SimpleWindowedApplication,
338338
IGPUShader::SSpecInfo shaders[RTDS_COUNT];
339339
shaders[RTDS_RAYGEN] = {.shader = raygenShader.get()};
340340
shaders[RTDS_MISS] = {.shader = missShader.get()};
341-
shaders[RTDS_CLOSEST_HIT_SHADOW] = { .shader = shadowClosestHitShader.get() };
341+
shaders[RTDS_MISS_SHADOW] = { .shader = missShadowShader.get() };
342342
shaders[RTDS_CLOSEST_HIT] = {.shader = closestHitShader.get()};
343343
shaders[RTDS_SPHERE_CLOSEST_HIT] = {.shader = proceduralClosestHitShader.get()};
344344
shaders[RTDS_ANYHIT_PRIMARY] = {.shader = anyHitShaderColorPayload.get()};
@@ -351,17 +351,17 @@ class RaytracingPipelineApp final : public examples::SimpleWindowedApplication,
351351
params.layout = pipelineLayout.get();
352352
params.shaders = std::span(shaders);
353353
using RayTracingFlags = IGPURayTracingPipeline::SCreationParams::FLAGS;
354-
params.flags = core::bitflag(RayTracingFlags::NO_NULL_INTERSECTION_SHADERS) |
355-
RayTracingFlags::NO_NULL_ANY_HIT_SHADERS |
356-
RayTracingFlags::NO_NULL_CLOSEST_HIT_SHADERS;
354+
params.flags = core::bitflag(RayTracingFlags::NO_NULL_MISS_SHADERS) |
355+
RayTracingFlags::NO_NULL_INTERSECTION_SHADERS |
356+
RayTracingFlags::NO_NULL_ANY_HIT_SHADERS;
357357

358358
auto& shaderGroups = params.shaderGroups;
359359

360360
shaderGroups.raygen = { .index = RTDS_RAYGEN };
361361

362362
IRayTracingPipelineBase::SGeneralShaderGroup missGroups[EMT_COUNT];
363363
missGroups[EMT_PRIMARY] = { .index = RTDS_MISS };
364-
missGroups[EMT_OCCLUSION] = { .index = IGPURayTracingPipeline::SGeneralShaderGroup::Unused };
364+
missGroups[EMT_OCCLUSION] = { .index = RTDS_MISS_SHADOW };
365365
shaderGroups.misses = missGroups;
366366

367367
auto getHitGroupIndex = [](E_GEOM_TYPE geomType, E_RAY_TYPE rayType)
@@ -374,7 +374,7 @@ class RaytracingPipelineApp final : public examples::SimpleWindowedApplication,
374374
.anyHit = RTDS_ANYHIT_PRIMARY,
375375
};
376376
hitGroups[getHitGroupIndex(EGT_TRIANGLES, ERT_OCCLUSION)] = {
377-
.closestHit = RTDS_CLOSEST_HIT_SHADOW,
377+
.closestHit = IGPURayTracingPipeline::SGeneralShaderGroup::Unused,
378378
.anyHit = RTDS_ANYHIT_SHADOW,
379379
};
380380
hitGroups[getHitGroupIndex(EGT_PROCEDURAL, ERT_PRIMARY)] = {
@@ -383,7 +383,7 @@ class RaytracingPipelineApp final : public examples::SimpleWindowedApplication,
383383
.intersection = RTDS_INTERSECTION,
384384
};
385385
hitGroups[getHitGroupIndex(EGT_PROCEDURAL, ERT_OCCLUSION)] = {
386-
.closestHit = RTDS_CLOSEST_HIT_SHADOW,
386+
.closestHit = IGPURayTracingPipeline::SGeneralShaderGroup::Unused,
387387
.anyHit = RTDS_ANYHIT_SHADOW,
388388
.intersection = RTDS_INTERSECTION,
389389
};

0 commit comments

Comments
 (0)