Skip to content

Commit

Permalink
Refactor and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
httpdigest committed Mar 27, 2021
1 parent 747b1fb commit b6c48ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ layout(location = 1) flat out uint type;

const vec3 N[6] = vec3[6](vec3(-1,0,0), vec3(1,0,0), vec3(0,-1,0), vec3(0,1,0), vec3(0,0,-1), vec3(0,0,1));

#define OFFSET_SCALE 3E-4

vec3 offset() {
uint s = typeAndSide >> 8u;
vec3 r = vec3(gl_VertexIndex & 1, gl_VertexIndex >> 1 & 1, 0.5) * 2.0 - vec3(1.0);
return s < 2u ? r.zxy : s < 4u ? r.yzx : r.xyz;
}

void main(void) {
out_normal = N[typeAndSide >> 8u];
type = typeAndSide & 0xFFu;
Expand Down
20 changes: 11 additions & 9 deletions src/org/lwjgl/demo/vulkan/raytracing/HybridMagicaVoxel.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,12 @@ private static Swapchain createSwapchain() {

private static int determineDepthFormat() {
try (MemoryStack stack = stackPush()) {
int[] depthFormats = { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT };
// prefer any 32-bit signed float depth, and then 24-bit integer depth
int[] depthFormats = {
VK_FORMAT_D32_SFLOAT,
VK_FORMAT_D32_SFLOAT_S8_UINT,
VK_FORMAT_D24_UNORM_S8_UINT
};
VkFormatProperties formatProps = VkFormatProperties
.mallocStack(stack);
int depthFormat = -1;
Expand Down Expand Up @@ -1286,7 +1291,7 @@ private static Pipeline createRasterPipeline() throws IOException {
.sType(VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO)
.depthTestEnable(true)
.depthWriteEnable(true)
.depthCompareOp(VK_COMPARE_OP_GREATER_OR_EQUAL)
.depthCompareOp(VK_COMPARE_OP_GREATER_OR_EQUAL) // <- we use reverse depth
.back(stencil -> stencil
.failOp(VK_STENCIL_OP_KEEP)
.passOp(VK_STENCIL_OP_KEEP)
Expand Down Expand Up @@ -2181,15 +2186,12 @@ private static VkCommandBuffer[] createRayTracingCommandBuffers() {

private static VkCommandBuffer[] createRasterCommandBuffers() {
try (MemoryStack stack = stackPush()) {
VkClearValue.Buffer clearValues = VkClearValue
.callocStack(2, stack);
clearValues.apply(0, v -> v.color().uint32(0, 0).uint32(1, 0).uint32(2, 0).uint32(3, 0))
.apply(1, v -> v.depthStencil().depth(0.0f).stencil(0));
VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo
.callocStack(stack)
.sType(VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO)
.renderPass(renderPass)
.pClearValues(clearValues)
.pClearValues(VkClearValue
.callocStack(2, stack)) // <- all zeroes, we use reverse depth
.renderArea(a -> a.extent().set(swapchain.width, swapchain.height));
VkViewport.Buffer viewport = VkViewport
.callocStack(1, stack)
Expand Down Expand Up @@ -2248,8 +2250,8 @@ private static void update(float dt) {
projMatrix.scaling(1, -1, 1)
.perspective(toRadians(45.0f),
(float) windowAndCallbacks.width / windowAndCallbacks.height,
1000.0f,
0.1f,
1000.0f, // <- near plane (we use reverse depth)
0.1f, // <- far plane (we use reverse depth)
true);
projMatrix.invert(invProjMatrix);
projMatrix.mul(viewMatrix, mvpMatrix).scale(POSITION_SCALE);
Expand Down

0 comments on commit b6c48ef

Please sign in to comment.