Skip to content

Commit 4131b94

Browse files
committed
Add VoxelChunks vulkan raytracing demo
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.
1 parent d40661e commit 4131b94

File tree

4 files changed

+2274
-0
lines changed

4 files changed

+2274
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright LWJGL. All rights reserved.
3+
* License terms: https://www.lwjgl.org/license
4+
*/
5+
#version 460
6+
#extension GL_EXT_ray_tracing : enable
7+
#extension GL_EXT_buffer_reference : enable
8+
#extension GL_EXT_scalar_block_layout : enable
9+
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable
10+
11+
struct hitPayload
12+
{
13+
vec3 col;
14+
float t;
15+
vec3 n;
16+
uint mat;
17+
};
18+
layout(location = 0) rayPayloadInEXT hitPayload payload;
19+
20+
struct InstanceDesc {
21+
uint64_t vertexAddress;
22+
uint64_t indexAddress;
23+
};
24+
layout(buffer_reference) buffer PositionsAndTypes {uint pt[]; };
25+
layout(buffer_reference, scalar) buffer Indices {uvec3 i[]; };
26+
layout(binding = 3, set = 0) buffer InstancesDescs { InstanceDesc id[]; } instancesDescs;
27+
layout(binding = 4, set = 0) buffer Materials { uint m[]; } materials;
28+
29+
void main(void) {
30+
InstanceDesc instanceDesc = instancesDescs.id[gl_InstanceCustomIndexEXT];
31+
Indices indices = Indices(instanceDesc.indexAddress);
32+
PositionsAndTypes positionsAndTypes = PositionsAndTypes(instanceDesc.vertexAddress);
33+
uvec3 ind = indices.i[gl_PrimitiveID];
34+
uint pt0 = positionsAndTypes.pt[ind.x],
35+
pt1 = positionsAndTypes.pt[ind.y],
36+
pt2 = positionsAndTypes.pt[ind.z];
37+
vec3 p0 = vec3(pt0 & 0xFFu, pt0 >> 8u & 0xFFu, pt0 >> 16u & 0xFFu),
38+
p1 = vec3(pt1 & 0xFFu, pt1 >> 8u & 0xFFu, pt1 >> 16u & 0xFFu),
39+
p2 = vec3(pt2 & 0xFFu, pt2 >> 8u & 0xFFu, pt2 >> 16u & 0xFFu);
40+
uint type = pt0 >> 24u & 0xFFu;
41+
payload.col = unpackUnorm4x8(materials.m[type]).rgb;
42+
payload.n = normalize(cross(p1-p0, p2-p0));
43+
payload.mat = type;
44+
payload.t = gl_RayTmaxEXT;
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright LWJGL. All rights reserved.
3+
* License terms: https://www.lwjgl.org/license
4+
*/
5+
#version 460
6+
#extension GL_EXT_ray_tracing : enable
7+
8+
struct hitPayload
9+
{
10+
vec3 col;
11+
float t;
12+
vec3 n;
13+
uint mat;
14+
};
15+
layout(location = 0) rayPayloadEXT hitPayload payload;
16+
layout(binding = 0, set = 0) uniform accelerationStructureEXT acc;
17+
layout(binding = 1, set = 0, rgba8) uniform image2D image;
18+
layout(binding = 2, set = 0) uniform Camera {
19+
vec3 corners[4];
20+
mat4 viewInverse;
21+
} cam;
22+
23+
void main(void) {
24+
vec2 px = vec2(gl_LaunchIDEXT.xy) + vec2(0.5);
25+
vec2 p = px / vec2(gl_LaunchSizeEXT.xy);
26+
vec3 origin = cam.viewInverse[3].xyz;
27+
vec3 target = mix(mix(cam.corners[0], cam.corners[2], p.y), mix(cam.corners[1], cam.corners[3], p.y), p.x);
28+
vec4 direction = cam.viewInverse * vec4(target.xyz, 0.0);
29+
traceRayEXT(acc, 0, 0xFF, 0, 0, 0, origin, 1E-3, direction.xyz, 1E+4, 0);
30+
imageStore(image, ivec2(gl_LaunchIDEXT), vec4(payload.n, 1.0));
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright LWJGL. All rights reserved.
3+
* License terms: https://www.lwjgl.org/license
4+
*/
5+
#version 460
6+
#extension GL_EXT_ray_tracing : enable
7+
8+
struct hitPayload
9+
{
10+
vec3 col;
11+
float t;
12+
vec3 n;
13+
uint mat;
14+
};
15+
layout(location = 0) rayPayloadInEXT hitPayload payload;
16+
17+
void main(void) {
18+
payload.mat = 0u;
19+
payload.col = vec3(0.8, 0.9, 1.0);
20+
}

0 commit comments

Comments
 (0)