Skip to content

Commit

Permalink
Add demo rendering MagicaVoxel model with reflective windows
Browse files Browse the repository at this point in the history
  • Loading branch information
httpdigest committed Mar 21, 2021
1 parent a2272e3 commit 0dfcff3
Show file tree
Hide file tree
Showing 6 changed files with 2,057 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable

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

layout(binding = 3, set = 0) buffer PositionsAndTypes { uint16_t pt[]; } positionsAndTypes;
layout(binding = 4, set = 0) buffer Indices { uint16_t i[]; } indices;
layout(binding = 5, set = 0) buffer Materials { uint m[]; } materials;

const vec3 normals[6] = vec3[6](vec3(-1.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0),
vec3(0.0, -1.0, 0.0), vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, -1.0), vec3(0.0, 0.0, 1.0));

void main(void) {
uint index = uint(indices.i[3u * gl_PrimitiveID]);
uint typeAndSide = positionsAndTypes.pt[4u * index + 3u];
uint type = typeAndSide & 0xFFu;
uint side = typeAndSide >> 8u & 0xFFu;
vec3 col = unpackUnorm4x8(materials.m[type]).rgb;
payload.col = col;
payload.mat = type ;
payload.n = normals[side];
payload.t = gl_RayTmaxEXT;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
#version 460
#extension GL_EXT_ray_tracing : enable

#define WINDOW_MATERIAL_INDEX 79

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 {
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 = 1E-3;
float tMax = 1E+4;
for (int i = 0; i < 2; i++) {
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)
);
if (payload.mat == WINDOW_MATERIAL_INDEX) {
origin += payload.t * direction.xyz + payload.n * 1E-5;
direction.xyz = reflect(direction.xyz, payload.n);
} else {
break;
}
}
imageStore(image, ivec2(gl_LaunchIDEXT), vec4(payload.col, 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
*/
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ void main(void) {
);

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

/*
Expand Down
Loading

0 comments on commit 0dfcff3

Please sign in to comment.