-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add demo rendering MagicaVoxel model with reflective windows
- Loading branch information
1 parent
a2272e3
commit f4a3bce
Showing
6 changed files
with
2,035 additions
and
9 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
res/org/lwjgl/demo/vulkan/raytracing/reflectivemagicavoxel/closesthit.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
67 changes: 67 additions & 0 deletions
67
res/org/lwjgl/demo/vulkan/raytracing/reflectivemagicavoxel/raygen.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
20 changes: 20 additions & 0 deletions
20
res/org/lwjgl/demo/vulkan/raytracing/reflectivemagicavoxel/raymiss.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.