-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sample for VK_KHR_ray_tracing_position_fetch
- Loading branch information
1 parent
a0940b2
commit 85018e6
Showing
8 changed files
with
661 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
557 changes: 557 additions & 0 deletions
557
examples/raytracingpositionfetch/raytracingpositionfetch.cpp
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* Copyright (c) 2024, Sascha Willems | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
*/ | ||
|
||
#version 460 | ||
#extension GL_EXT_ray_tracing : enable | ||
#extension GL_EXT_nonuniform_qualifier : enable | ||
// This extension is required for fetching position data in the closes hit shader | ||
#extension GL_EXT_ray_tracing_position_fetch : require | ||
|
||
layout(location = 0) rayPayloadInEXT vec3 hitValue; | ||
hitAttributeEXT vec2 attribs; | ||
|
||
layout(binding = 2, set = 0) uniform UBO | ||
{ | ||
mat4 viewInverse; | ||
mat4 projInverse; | ||
vec4 lightPos; | ||
} ubo; | ||
|
||
void main() | ||
{ | ||
// We need the barycentric coordinates to calculate data for the current position | ||
const vec3 barycentricCoords = vec3(1.0 - attribs.x - attribs.y, attribs.x, attribs.y); | ||
|
||
// With VK_KHR_ray_tracing_position_fetch we can access the vertices for the hit triangle in the shader | ||
vec3 vertexPos0 = gl_HitTriangleVertexPositionsEXT[0]; | ||
vec3 vertexPos1 = gl_HitTriangleVertexPositionsEXT[1]; | ||
vec3 vertexPos2 = gl_HitTriangleVertexPositionsEXT[2]; | ||
vec3 currentPos = vertexPos0 * barycentricCoords.x + vertexPos1 * barycentricCoords.y + vertexPos2 * barycentricCoords.z; | ||
|
||
// Calcualte the normal from above values | ||
vec3 normal = normalize(cross(vertexPos1 - vertexPos0, vertexPos2 - vertexPos0)); | ||
normal = normalize(vec3(normal * gl_WorldToObjectEXT)); | ||
|
||
// Visualize the normal | ||
hitValue = normal; | ||
|
||
// Basic lighting | ||
vec3 lightDir = normalize(ubo.lightPos.xyz - currentPos); | ||
float diffuse = max(dot(normal, lightDir), 0.0); | ||
|
||
hitValue = vec3(0.1 + diffuse); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* Copyright (c) 2024, Sascha Willems | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
*/ | ||
|
||
#version 460 | ||
#extension GL_EXT_ray_tracing : enable | ||
|
||
layout(location = 0) rayPayloadInEXT vec3 hitValue; | ||
|
||
void main() | ||
{ | ||
hitValue = vec3(0.0, 0.0, 0.2); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* Copyright (c) 2024, Sascha Willems | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
*/ | ||
|
||
#version 460 | ||
#extension GL_EXT_ray_tracing : enable | ||
|
||
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS; | ||
layout(binding = 1, set = 0, rgba8) uniform image2D image; | ||
layout(binding = 2, set = 0) uniform CameraProperties | ||
{ | ||
mat4 viewInverse; | ||
mat4 projInverse; | ||
vec4 lightPos; | ||
} ubo; | ||
|
||
layout(location = 0) rayPayloadEXT vec3 hitValue; | ||
|
||
void main() | ||
{ | ||
const vec2 pixelCenter = vec2(gl_LaunchIDEXT.xy) + vec2(0.5); | ||
const vec2 inUV = pixelCenter/vec2(gl_LaunchSizeEXT.xy); | ||
vec2 d = inUV * 2.0 - 1.0; | ||
|
||
vec4 origin = ubo.viewInverse * vec4(0,0,0,1); | ||
vec4 target = ubo.projInverse * vec4(d.x, d.y, 1, 1) ; | ||
vec4 direction = ubo.viewInverse*vec4(normalize(target.xyz), 0) ; | ||
|
||
float tmin = 0.001; | ||
float tmax = 10000.0; | ||
|
||
hitValue = vec3(0.0); | ||
|
||
traceRayEXT(topLevelAS, gl_RayFlagsOpaqueEXT, 0xff, 0, 0, 0, origin.xyz, tmin, direction.xyz, tmax, 0); | ||
|
||
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(hitValue, 0.0)); | ||
} |
Binary file not shown.