xref: /aosp_15_r20/external/mesa3d/src/amd/vulkan/bvh/morton.comp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1/*
2 * Copyright © 2022 Konstantin Seurer
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#version 460
8
9#extension GL_GOOGLE_include_directive : require
10
11#extension GL_EXT_shader_explicit_arithmetic_types_int8 : require
12#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
13#extension GL_EXT_shader_explicit_arithmetic_types_int32 : require
14#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
15#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
16#extension GL_EXT_scalar_block_layout : require
17#extension GL_EXT_buffer_reference : require
18#extension GL_EXT_buffer_reference2 : require
19
20layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
21
22#include "build_interface.h"
23
24layout(push_constant) uniform CONSTS {
25   morton_args args;
26};
27
28uint32_t
29morton_component(uint32_t x)
30{
31   x = (x * 0x00000101u) & 0x0F00F00Fu;
32   x = (x * 0x00000011u) & 0xC30C30C3u;
33   x = (x * 0x00000005u) & 0x49249249u;
34   return x;
35}
36
37uint32_t
38morton_code(uint32_t x, uint32_t y, uint32_t z)
39{
40   return (morton_component(x) << 2) | (morton_component(y) << 1) | morton_component(z);
41}
42
43uint32_t
44lbvh_key(float x01, float y01, float z01)
45{
46   return morton_code(uint32_t(x01 * 255.0), uint32_t(y01 * 255.0), uint32_t(z01 * 255.0)) << 8;
47}
48
49void
50main(void)
51{
52   uint32_t global_id = gl_GlobalInvocationID.x;
53
54   REF(key_id_pair) key_id = INDEX(key_id_pair, args.ids, global_id);
55
56   uint32_t id = DEREF(key_id).id;
57
58   uint32_t key;
59   if (id != RADV_BVH_INVALID_NODE) {
60      radv_aabb bounds = DEREF(REF(radv_ir_node)OFFSET(args.bvh, ir_id_to_offset(id))).aabb;
61      vec3 center = (bounds.min + bounds.max) * 0.5;
62
63      radv_aabb bvh_bounds;
64      bvh_bounds.min.x = from_emulated_float(DEREF(args.header).min_bounds[0]);
65      bvh_bounds.min.y = from_emulated_float(DEREF(args.header).min_bounds[1]);
66      bvh_bounds.min.z = from_emulated_float(DEREF(args.header).min_bounds[2]);
67      bvh_bounds.max.x = from_emulated_float(DEREF(args.header).max_bounds[0]);
68      bvh_bounds.max.y = from_emulated_float(DEREF(args.header).max_bounds[1]);
69      bvh_bounds.max.z = from_emulated_float(DEREF(args.header).max_bounds[2]);
70
71      vec3 normalized_center = (center - bvh_bounds.min) / (bvh_bounds.max - bvh_bounds.min);
72
73      key = lbvh_key(normalized_center.x, normalized_center.y, normalized_center.z);
74   } else {
75      /* Move null instances to the end to avoid mixing null instances with active instances. This
76       * way, we can skip early during traversal.
77       */
78      key = 0xFFFFFFFF;
79   }
80   DEREF(key_id).key = key;
81}
82