Skip to content

Commit bcef62e

Browse files
Ray Query opaque shadows
1 parent 28a788f commit bcef62e

File tree

3 files changed

+58
-17
lines changed

3 files changed

+58
-17
lines changed

attachments/38_ray_tracing.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ class HelloTriangleApplication {
195195
vk::KHRCreateRenderpass2ExtensionName,
196196
vk::KHRAccelerationStructureExtensionName,
197197
vk::KHRBufferDeviceAddressExtensionName,
198-
vk::KHRDeferredHostOperationsExtensionName
198+
vk::KHRDeferredHostOperationsExtensionName,
199+
vk::KHRRayQueryExtensionName
199200
};
200201

201202
void initWindow() {
@@ -372,7 +373,8 @@ class HelloTriangleApplication {
372373
vk::PhysicalDeviceVulkan12Features,
373374
vk::PhysicalDeviceVulkan13Features,
374375
vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT,
375-
vk::PhysicalDeviceAccelerationStructureFeaturesKHR>();
376+
vk::PhysicalDeviceAccelerationStructureFeaturesKHR,
377+
vk::PhysicalDeviceRayQueryFeaturesKHR>();
376378
bool supportsRequiredFeatures = features.template get<vk::PhysicalDeviceFeatures2>().features.samplerAnisotropy &&
377379
features.template get<vk::PhysicalDeviceVulkan13Features>().dynamicRendering &&
378380
features.template get<vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT>().extendedDynamicState &&
@@ -382,7 +384,8 @@ class HelloTriangleApplication {
382384
features.template get<vk::PhysicalDeviceVulkan12Features>().runtimeDescriptorArray &&
383385
features.template get<vk::PhysicalDeviceVulkan12Features>().shaderSampledImageArrayNonUniformIndexing &&
384386
features.template get<vk::PhysicalDeviceVulkan12Features>().bufferDeviceAddress &&
385-
features.template get<vk::PhysicalDeviceAccelerationStructureFeaturesKHR>().accelerationStructure;
387+
features.template get<vk::PhysicalDeviceAccelerationStructureFeaturesKHR>().accelerationStructure &&
388+
features.template get<vk::PhysicalDeviceRayQueryFeaturesKHR>().rayQuery;
386389

387390
return supportsVulkan1_3 && supportsGraphics && supportsAllRequiredExtensions && supportsRequiredFeatures;
388391
} );
@@ -447,14 +450,15 @@ class HelloTriangleApplication {
447450
// query for Vulkan 1.3 features
448451
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan12Features,
449452
vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT,
450-
vk::PhysicalDeviceAccelerationStructureFeaturesKHR> featureChain = {
451-
{.features = {.samplerAnisotropy = true } }, // vk::PhysicalDeviceFeatures2
453+
vk::PhysicalDeviceAccelerationStructureFeaturesKHR, vk::PhysicalDeviceRayQueryFeaturesKHR> featureChain = {
454+
{.features = {.samplerAnisotropy = true } }, // vk::PhysicalDeviceFeatures2
452455
{.shaderSampledImageArrayNonUniformIndexing = true, .descriptorBindingSampledImageUpdateAfterBind = true,
453456
.descriptorBindingPartiallyBound = true, .descriptorBindingVariableDescriptorCount = true,
454-
.runtimeDescriptorArray = true, .bufferDeviceAddress = true }, // vk::PhysicalDeviceVulkan12Features
455-
{.synchronization2 = true, .dynamicRendering = true }, // vk::PhysicalDeviceVulkan13Features
456-
{.extendedDynamicState = true }, // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
457-
{.accelerationStructure = true }, // vk::PhysicalDeviceAccelerationStructureFeaturesKHR
457+
.runtimeDescriptorArray = true, .bufferDeviceAddress = true }, // vk::PhysicalDeviceVulkan12Features
458+
{.synchronization2 = true, .dynamicRendering = true }, // vk::PhysicalDeviceVulkan13Features
459+
{.extendedDynamicState = true }, // vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT
460+
{.accelerationStructure = true }, // vk::PhysicalDeviceAccelerationStructureFeaturesKHR
461+
{.rayQuery = true } // vk::PhysicalDeviceRayQueryFeaturesKHR
458462
};
459463

460464
// create a Device
@@ -1002,14 +1006,10 @@ class HelloTriangleApplication {
10021006
vk::AccelerationStructureGeometryDataKHR geomData(trianglesData);
10031007
vk::AccelerationStructureGeometryKHR blasGeometry{
10041008
.geometryType = vk::GeometryTypeKHR::eTriangles,
1005-
.geometry = geomData
1009+
.geometry = geomData,
1010+
.flags = vk::GeometryFlagBitsKHR::eOpaque
10061011
};
10071012

1008-
if (!submesh.alphaCut)
1009-
{
1010-
blasGeometry.flags = vk::GeometryFlagBitsKHR::eOpaque;
1011-
}
1012-
10131013
vk::AccelerationStructureBuildRangeInfoKHR blasRangeInfo{
10141014
.primitiveCount = static_cast<uint32_t>(submesh.indexCount / 3),
10151015
.primitiveOffset = 0,

attachments/38_ray_tracing.slang

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct VSOutput
2222
float3 fragColor;
2323
float2 fragTexCoord;
2424
float3 fragNormal;
25+
float3 worldPos;
2526
};
2627

2728
[shader("vertex")]
@@ -31,6 +32,7 @@ VSOutput vertMain(VSInput input) {
3132
output.fragColor = input.inColor;
3233
output.fragTexCoord = input.inTexCoord;
3334
output.fragNormal = input.inNormal;
35+
output.worldPos = mul(ubo.model, float4(input.inPosition, 1.0)).xyz;
3436
return output;
3537
}
3638

@@ -46,7 +48,46 @@ struct PushConstant {
4648
[push_constant]
4749
PushConstant pc;
4850

51+
static const float3 lightDir = float3(-6.0, 0.0, 6.0);
52+
53+
// Small epsilon to avoid self-intersection
54+
static const float EPSILON = 0.01;
55+
56+
bool in_shadow(float3 P)
57+
{
58+
// Build the shadow ray from the world position toward the light
59+
RayDesc shadowRayDesc;
60+
shadowRayDesc.Origin = P;
61+
shadowRayDesc.Direction = normalize(lightDir);
62+
shadowRayDesc.TMin = EPSILON;
63+
shadowRayDesc.TMax = 1e4;
64+
65+
// Initialize a ray-query for shadows
66+
RayQuery<RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES |
67+
RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH> sq;
68+
let rayFlags = RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES |
69+
RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH;
70+
71+
sq.TraceRayInline(accelerationStructure, rayFlags, 0xFF, shadowRayDesc);
72+
73+
sq.Proceed();
74+
75+
// If the shadow ray intersects an opaque triangle, we consider the pixel in shadow
76+
bool hit = (sq.CommittedStatus() == COMMITTED_TRIANGLE_HIT);
77+
78+
return hit;
79+
}
80+
4981
[shader("fragment")]
5082
float4 fragMain(VSOutput vertIn) : SV_TARGET {
51-
return textures[pc.materialIndex].Sample(textureSampler, vertIn.fragTexCoord);
83+
float4 baseColor = textures[pc.materialIndex].Sample(textureSampler, vertIn.fragTexCoord);
84+
85+
bool inShadow = in_shadow(vertIn.worldPos);
86+
87+
// Darken if in shadow
88+
if (inShadow) {
89+
baseColor.rgb *= 0.2;
90+
}
91+
92+
return baseColor;
5293
}

attachments/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function (add_slang_shader_target TARGET)
8989
)
9090
add_custom_command (
9191
OUTPUT ${SHADERS_DIR}/slang.spv
92-
COMMAND ${SLANGC_EXECUTABLE} ${SHADER_SOURCES} -target spirv -profile spirv_1_4 -emit-spirv-directly -fvk-use-entrypoint-name ${ENTRY_POINTS} -o slang.spv
92+
COMMAND ${SLANGC_EXECUTABLE} ${SHADER_SOURCES} -target spirv -profile spirv_1_4+spvRayQueryKHR -emit-spirv-directly -fvk-use-entrypoint-name ${ENTRY_POINTS} -o slang.spv
9393
WORKING_DIRECTORY ${SHADERS_DIR}
9494
DEPENDS ${SHADERS_DIR} ${SHADER_SOURCES}
9595
COMMENT "Compiling Slang Shaders"

0 commit comments

Comments
 (0)