Skip to content

Commit 078c30d

Browse files
Add opaque Ray Query reflections
1 parent 92029c5 commit 078c30d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

attachments/38_ray_tracing.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct UniformBufferObject {
8787
alignas(16) glm::mat4 model;
8888
alignas(16) glm::mat4 view;
8989
alignas(16) glm::mat4 proj;
90+
alignas(16) glm::vec3 cameraPos;
9091
};
9192

9293
struct PushConstant {
@@ -1737,6 +1738,7 @@ class HelloTriangleApplication {
17371738
ubo.view = lookAt(eye, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
17381739
ubo.proj = glm::perspective(glm::radians(45.0f), static_cast<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), 0.1f, 10.0f);
17391740
ubo.proj[1][1] *= -1;
1741+
ubo.cameraPos = eye;
17401742

17411743
memcpy(uniformBuffersMapped[currentImage], &ubo, sizeof(ubo));
17421744
}

attachments/38_ray_tracing.slang

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct UniformBuffer {
99
float4x4 model;
1010
float4x4 view;
1111
float4x4 proj;
12+
float3 cameraPos;
1213
};
1314
[[vk::binding(0,0)]]
1415
ConstantBuffer<UniformBuffer> ubo;
@@ -128,18 +129,58 @@ bool in_shadow(float3 P)
128129
return hit;
129130
}
130131

132+
void apply_reflection(float3 P, float3 N, inout float4 baseColor) {
133+
// Build the reflections ray
134+
float3 V = normalize(ubo.cameraPos - P);
135+
float3 R = reflect(-V, N);
136+
137+
RayDesc reflectionRayDesc;
138+
reflectionRayDesc.Origin = P;
139+
reflectionRayDesc.Direction = R;
140+
reflectionRayDesc.TMin = EPSILON;
141+
reflectionRayDesc.TMax = 1e4;
142+
143+
// Initialize a ray-query for reflections
144+
RayQuery<RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES |
145+
RAY_FLAG_FORCE_OPAQUE> rq;
146+
let rayFlags = RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES |
147+
RAY_FLAG_FORCE_OPAQUE;
148+
149+
rq.TraceRayInline(accelerationStructure, rayFlags, 0xFF, reflectionRayDesc);
150+
151+
rq.Proceed();
152+
153+
bool hit = (rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT);
154+
155+
if (hit)
156+
{
157+
uint instanceID = rq.CommittedInstanceID();
158+
uint primIndex = rq.CommittedPrimitiveIndex();
159+
160+
float2 uv = intersection_uv(instanceID, primIndex, rq.CommittedTriangleBarycentrics());
161+
162+
uint materialID = instanceLUTBuffer[instanceID].materialID;
163+
float4 intersectionColor = textures[materialID].Sample(textureSampler, uv);
164+
165+
baseColor.rgb = lerp(baseColor.rgb, intersectionColor.rgb, 0.7);
166+
}
167+
}
168+
131169
[shader("fragment")]
132170
float4 fragMain(VSOutput vertIn) : SV_TARGET {
133171
float4 baseColor = textures[pc.materialIndex].Sample(textureSampler, vertIn.fragTexCoord);
134172

135173
// Alpha test
136174
if (baseColor.a < 0.5) discard;
137175

176+
float3 P = vertIn.worldPos;
177+
float3 N = vertIn.fragNormal;
178+
138179
if (pc.reflective > 0) {
139-
baseColor.rgb = float3(1.0, 0.0, 0.0);
180+
apply_reflection(P, N, baseColor);
140181
}
141182

142-
bool inShadow = in_shadow(vertIn.worldPos);
183+
bool inShadow = in_shadow(P);
143184

144185
// Darken if in shadow
145186
if (inShadow) {

0 commit comments

Comments
 (0)