Skip to content

Commit

Permalink
Add KHR ray tracing demo
Browse files Browse the repository at this point in the history
  • Loading branch information
httpdigest committed Mar 20, 2021
1 parent 0c3b5c4 commit a2272e3
Show file tree
Hide file tree
Showing 5 changed files with 1,697 additions and 522 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable

layout(location = 0) rayPayloadInEXT bool payload;

void main(void) {
payload = true;
}
54 changes: 54 additions & 0 deletions res/org/lwjgl/demo/vulkan/raytracing/simpletriangle/raygen.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable

layout(location = 0) rayPayloadEXT bool payload;
layout(binding = 0, set = 0) uniform accelerationStructureEXT acc;
layout(binding = 1, set = 0, rgba8) uniform image2D image;
layout(binding = 2, set = 0) uniform Camera {
mat4 projInverse;
mat4 viewInverse;
} cam;

void main(void) {
vec2 px = vec2(gl_LaunchIDEXT.xy) + vec2(0.5);
vec2 ndc = (px / vec2(gl_LaunchSizeEXT.xy)) * 2.0 - vec2(1.0);
vec3 origin = cam.viewInverse[3].xyz;
vec4 target = cam.projInverse * vec4(ndc, 0.0, 1.0);
vec4 direction = cam.viewInverse * vec4(normalize(target.xyz), 0.0);
uint rayFlags = gl_RayFlagsOpaqueEXT | gl_RayFlagsCullBackFacingTrianglesEXT;
float tMin = 0.1;
float tMax = 100.0;
traceRayEXT(
acc, // acceleration structure
rayFlags, // rayFlags
0xFF, // cullMask
0, // sbtRecordOffset // <- see comment [1] below
0, // sbtRecordStride // <- see comment [1] below
0, // missIndex
origin, // ray origin
tMin, // ray min range
direction.xyz, // ray direction
tMax, // ray max range
0 // payload (location = 0)
);

imageStore(image, ivec2(gl_LaunchIDEXT),
payload
? vec4(0.5, 0.6, 0.7, 1.0)
: vec4(0.2, 0.3, 0.4, 1.0));
}

/*
* [1]: The formula to determine the hit shader binding table record to invoke is based on the sbt offset and stride
* given to vkCmdTraceRaysKHR() call (sbt.offset and sbt.stride) as well the BLAS instance's SBT offset
* (VkAccelerationStructureInstanceKHR.instanceShaderBindingTableRecordOffset) and the geometryIndex inside one BLAS
* as well as the sbtRecordOffset and sbtRecordStride arguments to traceRayEXT(), in the following way:
*
* hitShaderAddress = sbt.offset + sbt.stride * ( instance.sbtOffset + geometryIndex * sbtRecordStride + sbtRecordOffset )
*
* Reference: https://vulkan.lunarg.com/doc/view/1.2.135.0/windows/chunked_spec/chap35.html#_hit_shaders
*/
12 changes: 12 additions & 0 deletions res/org/lwjgl/demo/vulkan/raytracing/simpletriangle/raymiss.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable

layout(location = 0) rayPayloadInEXT bool payload;

void main(void) {
payload = false;
}
Loading

0 comments on commit a2272e3

Please sign in to comment.