• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright © 2022 Konstantin Seurer
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#version 460
25
26#extension GL_GOOGLE_include_directive : require
27
28#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
29#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
30#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require
31#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
32#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
33#extension GL_EXT_scalar_block_layout : require
34#extension GL_EXT_buffer_reference : require
35#extension GL_EXT_buffer_reference2 : require
36
37#include "vk_build_interface.h"
38
39layout(local_size_x_id = SUBGROUP_SIZE_ID, local_size_y = 1, local_size_z = 1) in;
40
41layout(push_constant) uniform CONSTS {
42   morton_args args;
43};
44
45uint32_t
46morton_component(uint32_t x)
47{
48   x = (x * 0x00000101u) & 0x0F00F00Fu;
49   x = (x * 0x00000011u) & 0xC30C30C3u;
50   x = (x * 0x00000005u) & 0x49249249u;
51   return x;
52}
53
54uint32_t
55morton_code(uint32_t x, uint32_t y, uint32_t z)
56{
57   return (morton_component(x) << 2) | (morton_component(y) << 1) | morton_component(z);
58}
59
60uint32_t
61lbvh_key(float x01, float y01, float z01)
62{
63   return morton_code(uint32_t(x01 * 255.0), uint32_t(y01 * 255.0), uint32_t(z01 * 255.0)) << 8;
64}
65
66void
67main(void)
68{
69   uint32_t global_id = gl_GlobalInvocationID.x;
70
71   REF(key_id_pair) key_id = INDEX(key_id_pair, args.ids, global_id);
72
73   uint32_t id = DEREF(key_id).id;
74
75   uint32_t key;
76   if (id != VK_BVH_INVALID_NODE) {
77      vk_aabb bounds = DEREF(REF(vk_ir_node)OFFSET(args.bvh, ir_id_to_offset(id))).aabb;
78      vec3 center = (bounds.min + bounds.max) * 0.5;
79
80      vk_aabb bvh_bounds;
81      bvh_bounds.min.x = from_emulated_float(DEREF(args.header).min_bounds[0]);
82      bvh_bounds.min.y = from_emulated_float(DEREF(args.header).min_bounds[1]);
83      bvh_bounds.min.z = from_emulated_float(DEREF(args.header).min_bounds[2]);
84      bvh_bounds.max.x = from_emulated_float(DEREF(args.header).max_bounds[0]);
85      bvh_bounds.max.y = from_emulated_float(DEREF(args.header).max_bounds[1]);
86      bvh_bounds.max.z = from_emulated_float(DEREF(args.header).max_bounds[2]);
87
88      vec3 normalized_center = (center - bvh_bounds.min) / (bvh_bounds.max - bvh_bounds.min);
89
90      key = lbvh_key(normalized_center.x, normalized_center.y, normalized_center.z);
91   } else {
92      /* Move null instances to the end to avoid mixing null instances with active instances. This
93       * way, we can skip early during traversal.
94       */
95      key = 0xFFFFFFFF;
96   }
97   DEREF(key_id).key = key;
98}
99