From 150b51086872e992b38d5cdc92988c22500ebec4 Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Thu, 8 May 2025 20:08:57 +0200 Subject: [PATCH] Add slang shaders for ray query sample --- shaders/slang/rayquery/scene.slang | 81 ++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 shaders/slang/rayquery/scene.slang diff --git a/shaders/slang/rayquery/scene.slang b/shaders/slang/rayquery/scene.slang new file mode 100644 index 00000000..5b061c62 --- /dev/null +++ b/shaders/slang/rayquery/scene.slang @@ -0,0 +1,81 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; + float2 UV; + float3 Color; + float3 Normal; +} + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Normal; + float3 Color; + float3 ViewVec; + float3 LightVec; + float3 WorldPos; +} + +struct UBO +{ + float4x4 projection; + float4x4 view; + float4x4 model; + float3 lightPos; +}; +ConstantBuffer ubo; + +RaytracingAccelerationStructure accelStruct; + +#define ambient 0.1 + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.Color = input.Color; + output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0)))); + float4 pos = mul(ubo.model, float4(input.Pos, 1.0)); + output.WorldPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz; + output.Normal = mul((float4x3)ubo.model, input.Normal).xyz; + output.LightVec = normalize(ubo.lightPos - input.Pos); + output.ViewVec = -pos.xyz; + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + float3 N = normalize(input.Normal); + float3 L = normalize(input.LightVec); + float3 V = normalize(input.ViewVec); + float3 R = normalize(-reflect(L, N)); + float3 diffuse = max(dot(N, L), ambient) * input.Color; + + float4 color = float4(diffuse, 1.0); + + RayDesc rayDesc; + rayDesc.Origin = input.WorldPos; + rayDesc.Direction = L; + rayDesc.TMin = 0.01; + rayDesc.TMax = 1000.0; + + RayQuery rayQuery; + rayQuery.TraceRayInline(accelStruct, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 0xFF, rayDesc); + + // Traverse the acceleration structure and store information about the first intersection (if any) + rayQuery.Proceed(); + + // If the intersection has hit a triangle, the fragment is shadowed + if (rayQuery.CommittedStatus() == COMMITTED_TRIANGLE_HIT) { + color *= 0.1; + } + + return color; +}