Added sample for VK_KHR_ray_tracing_position_fetch

This commit is contained in:
Sascha Willems 2024-03-16 10:29:44 +01:00
parent a0940b2a55
commit 85018e6b09
8 changed files with 661 additions and 0 deletions

View file

@ -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);
}

View file

@ -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.

View file

@ -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.