Skip to content

Commit

Permalink
Added sample for VK_KHR_ray_tracing_position_fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaWillems committed Mar 16, 2024
1 parent a0940b2 commit 85018e6
Show file tree
Hide file tree
Showing 8 changed files with 661 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) 2016-2024, Sascha Willems
# SPDX-License-Identifier: MIT

# Function for building single example
function(buildExample EXAMPLE_NAME)
SET(EXAMPLE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE_NAME})
Expand Down Expand Up @@ -138,6 +141,7 @@ set(EXAMPLES
raytracinggltf
raytracingintersection
raytracingreflections
raytracingpositionfetch
raytracingsbtdata
raytracingshadows
raytracingtextures
Expand Down
557 changes: 557 additions & 0 deletions examples/raytracingpositionfetch/raytracingpositionfetch.cpp

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions shaders/glsl/raytracingpositionfetch/closesthit.rchit
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.
15 changes: 15 additions & 0 deletions shaders/glsl/raytracingpositionfetch/miss.rmiss
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 added shaders/glsl/raytracingpositionfetch/miss.rmiss.spv
Binary file not shown.
39 changes: 39 additions & 0 deletions shaders/glsl/raytracingpositionfetch/raygen.rgen
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.

0 comments on commit 85018e6

Please sign in to comment.