Added samples for ray queries and callable ray tracing shaders

This commit is contained in:
Sascha Willems 2020-11-23 12:25:49 +01:00
parent 08be260685
commit f79c9705b4
22 changed files with 1391 additions and 33 deletions

View file

@ -0,0 +1,37 @@
#version 460
#extension GL_EXT_ray_tracing : enable
#extension GL_EXT_ray_query : enable
layout (binding = 2, set = 0) uniform accelerationStructureEXT topLevelAS;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 inViewVec;
layout (location = 3) in vec3 inLightVec;
layout (location = 4) in vec3 inWorldPos;
layout (location = 0) out vec4 outFragColor;
#define ambient 0.1
void main()
{
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec);
vec3 R = normalize(-reflect(L, N));
vec3 diffuse = max(dot(N, L), ambient) * inColor;
outFragColor = vec4(diffuse, 1.0);
rayQueryEXT rayQuery;
rayQueryInitializeEXT(rayQuery, topLevelAS, gl_RayFlagsTerminateOnFirstHitEXT, 0xFF, inWorldPos, 0.01, L, 1000.0);
// Start the ray traversal, rayQueryProceedEXT returns false if the traversal is complete
while (rayQueryProceedEXT(rayQuery)) { }
// If the intersection has hit a triangle, the fragment is shadowed
if (rayQueryGetIntersectionTypeEXT(rayQuery, true) == gl_RayQueryCommittedIntersectionTriangleEXT ) {
outFragColor *= 0.1;
}
}

Binary file not shown.

View file

@ -0,0 +1,33 @@
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inNormal;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 view;
mat4 model;
vec3 lightPos;
} ubo;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec3 outViewVec;
layout (location = 3) out vec3 outLightVec;
layout (location = 4) out vec3 outWorldPos;
void main()
{
outColor = inColor;
outNormal = inNormal;
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0);
vec4 pos = ubo.model * vec4(inPos, 1.0);
outWorldPos = vec3(ubo.model * vec4(inPos, 1.0));
outNormal = mat3(ubo.model) * inNormal;
outLightVec = normalize(ubo.lightPos - inPos);
outViewVec = -pos.xyz;
}

Binary file not shown.

View file

@ -0,0 +1,11 @@
#version 460 core
#extension GL_EXT_ray_tracing : enable
layout(location = 0) callableDataInEXT vec3 outColor;
void main()
{
// Generate a checker board pattern
vec2 pos = vec2(gl_LaunchIDEXT / 8);
outColor = vec3(mod(pos.x + mod(pos.y, 2.0), 2.0));
}

View file

@ -0,0 +1,9 @@
#version 460 core
#extension GL_EXT_ray_tracing : enable
layout(location = 0) callableDataInEXT vec3 outColor;
void main()
{
outColor = vec3(0.0, 1.0, 0.0);
}

View file

@ -0,0 +1,13 @@
#version 460 core
#extension GL_EXT_ray_tracing : enable
layout(location = 0) callableDataInEXT vec3 outColor;
void main()
{
// Generate a line pattern
vec2 pos = vec2(gl_LaunchIDEXT / 8);
outColor = vec3(mod(pos.y, 2.0));
// outColor = vec3(0.0, 0.0, 1.0);
}

View file

@ -0,0 +1,18 @@
#version 460
#extension GL_EXT_ray_tracing : require
#extension GL_EXT_nonuniform_qualifier : enable
layout(location = 0) rayPayloadInEXT vec3 hitValue;
layout(location = 0) callableDataEXT vec3 outColor;
hitAttributeEXT vec3 attribs;
layout(binding = 0, set = 0) uniform accelerationStructureEXT topLevelAS;
void main()
{
// Execute the callable shader indexed by the current geometry being hit
// For our sample this means that the first callable shader in the SBT is invoked for the first triangle, the second callable shader for the second triangle, etc.
executeCallableEXT(gl_GeometryIndexEXT, 0);
hitValue = outColor;
}

View file

@ -0,0 +1,9 @@
#version 460
#extension GL_EXT_ray_tracing : require
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,32 @@
#version 460
#extension GL_EXT_ray_tracing : require
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;
} cam;
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 = cam.viewInverse * vec4(0,0,0,1);
vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1) ;
vec4 direction = cam.viewInverse*vec4(normalize(target.xyz / target.w), 0) ;
uint rayFlags = gl_RayFlagsOpaqueEXT;
uint cullMask = 0xff;
float tmin = 0.001;
float tmax = 10000.0;
traceRayEXT(topLevelAS, rayFlags, cullMask, 0, 0, 0, origin.xyz, tmin, direction.xyz, tmax, 0);
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(hitValue, 0.0));
}

Binary file not shown.

View file

@ -0,0 +1,9 @@
#version 460
#extension GL_EXT_ray_tracing : require
layout(location = 2) rayPayloadInEXT bool shadowed;
void main()
{
shadowed = false;
}

Binary file not shown.