Skip to content

Commit

Permalink
Add VoxelChunks vulkan raytracing demo
Browse files Browse the repository at this point in the history
This demonstrates how to use multiple BLASes in a single TLAS
in an efficient way by only allocating one VkBuffer for acceleration
structures and scratch areas to build all BLASes with a single
vkCmdBuildAccelerationStructuresKHR() call.

Additionally, it uses GL_EXT_buffer_reference to use buffer
addresses in the closest-hit shader for reading vertex/index data
for each sub-mesh/chunk.
  • Loading branch information
httpdigest committed Mar 3, 2022
1 parent d40661e commit 3ef47ae
Show file tree
Hide file tree
Showing 4 changed files with 2,300 additions and 0 deletions.
45 changes: 45 additions & 0 deletions res/org/lwjgl/demo/vulkan/raytracing/voxelchunks/closesthit.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable
#extension GL_EXT_buffer_reference : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable

struct hitPayload
{
vec3 col;
float t;
vec3 n;
uint mat;
};
layout(location = 0) rayPayloadInEXT hitPayload payload;

struct InstanceDesc {
uint64_t vertexAddress;
uint64_t indexAddress;
};
layout(buffer_reference) buffer PositionsAndTypes {uint pt[]; };
layout(buffer_reference, scalar) buffer Indices {uvec3 i[]; };
layout(binding = 3, set = 0) buffer InstancesDescs { InstanceDesc id[]; } instancesDescs;
layout(binding = 4, set = 0) buffer Materials { uint m[]; } materials;

void main(void) {
InstanceDesc instanceDesc = instancesDescs.id[gl_InstanceCustomIndexEXT];
Indices indices = Indices(instanceDesc.indexAddress);
PositionsAndTypes positionsAndTypes = PositionsAndTypes(instanceDesc.vertexAddress);
uvec3 ind = indices.i[gl_PrimitiveID];
uint pt0 = positionsAndTypes.pt[ind.x],
pt1 = positionsAndTypes.pt[ind.y],
pt2 = positionsAndTypes.pt[ind.z];
vec3 p0 = vec3(pt0 & 0xFFu, pt0 >> 8u & 0xFFu, pt0 >> 16u & 0xFFu),
p1 = vec3(pt1 & 0xFFu, pt1 >> 8u & 0xFFu, pt1 >> 16u & 0xFFu),
p2 = vec3(pt2 & 0xFFu, pt2 >> 8u & 0xFFu, pt2 >> 16u & 0xFFu);
uint type = pt0 >> 24u & 0xFFu;
payload.col = unpackUnorm4x8(materials.m[type]).rgb;
payload.n = normalize(cross(p1-p0, p2-p0));
payload.mat = type;
payload.t = gl_RayTmaxEXT;
}
31 changes: 31 additions & 0 deletions res/org/lwjgl/demo/vulkan/raytracing/voxelchunks/raygen.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable

struct hitPayload
{
vec3 col;
float t;
vec3 n;
uint mat;
};
layout(location = 0) rayPayloadEXT hitPayload 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 {
vec3 corners[4];
mat4 viewInverse;
} cam;

void main(void) {
vec2 px = vec2(gl_LaunchIDEXT.xy) + vec2(0.5);
vec2 p = px / vec2(gl_LaunchSizeEXT.xy);
vec3 origin = cam.viewInverse[3].xyz;
vec3 target = mix(mix(cam.corners[0], cam.corners[2], p.y), mix(cam.corners[1], cam.corners[3], p.y), p.x);
vec4 direction = cam.viewInverse * vec4(target.xyz, 0.0);
traceRayEXT(acc, 0, 0xFF, 0, 0, 0, origin, 1E-3, direction.xyz, 1E+4, 0);
imageStore(image, ivec2(gl_LaunchIDEXT), vec4(payload.n, 1.0));
}
20 changes: 20 additions & 0 deletions res/org/lwjgl/demo/vulkan/raytracing/voxelchunks/raymiss.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable

struct hitPayload
{
vec3 col;
float t;
vec3 n;
uint mat;
};
layout(location = 0) rayPayloadInEXT hitPayload payload;

void main(void) {
payload.mat = 0u;
payload.col = vec3(0.8, 0.9, 1.0);
}
Loading

0 comments on commit 3ef47ae

Please sign in to comment.