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#extension GL_KHR_shader_subgroup_vote : require 37#extension GL_KHR_shader_subgroup_arithmetic : require 38#extension GL_KHR_shader_subgroup_ballot : require 39 40layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; 41 42#include "build_interface.h" 43 44layout(push_constant) uniform CONSTS { 45 leaf_args args; 46}; 47 48void 49main(void) 50{ 51 uint32_t global_id = gl_GlobalInvocationID.x; 52 uint32_t primitive_id = args.geom_data.first_id + global_id; 53 54 REF(key_id_pair) id_ptr = INDEX(key_id_pair, args.ids, primitive_id); 55 uint32_t src_offset = global_id * args.geom_data.stride; 56 57 uint32_t dst_stride; 58 uint32_t node_type; 59 if (args.geom_data.geometry_type == VK_GEOMETRY_TYPE_TRIANGLES_KHR) { 60 dst_stride = SIZEOF(radv_bvh_triangle_node); 61 node_type = radv_ir_node_triangle; 62 } else if (args.geom_data.geometry_type == VK_GEOMETRY_TYPE_AABBS_KHR) { 63 dst_stride = SIZEOF(radv_bvh_aabb_node); 64 node_type = radv_ir_node_aabb; 65 } else { 66 dst_stride = SIZEOF(radv_bvh_instance_node); 67 node_type = radv_ir_node_instance; 68 } 69 70 uint32_t dst_offset = primitive_id * dst_stride; 71 VOID_REF dst_ptr = OFFSET(args.bvh, dst_offset); 72 73 radv_aabb bounds; 74 bool is_active; 75 if (args.geom_data.geometry_type == VK_GEOMETRY_TYPE_TRIANGLES_KHR) { 76 is_active = build_triangle(bounds, dst_ptr, args.geom_data, global_id); 77 } else if (args.geom_data.geometry_type == VK_GEOMETRY_TYPE_AABBS_KHR) { 78 VOID_REF src_ptr = OFFSET(args.geom_data.data, src_offset); 79 is_active = build_aabb(bounds, src_ptr, dst_ptr, args.geom_data.geometry_id, global_id); 80 } else { 81 VOID_REF src_ptr = OFFSET(args.geom_data.data, src_offset); 82 /* arrayOfPointers */ 83 if (args.geom_data.stride == 8) { 84 src_ptr = DEREF(REF(VOID_REF)(src_ptr)); 85 } 86 87 is_active = build_instance(bounds, src_ptr, dst_ptr, global_id); 88 } 89 90#if ALWAYS_ACTIVE 91 if (!is_active && args.geom_data.geometry_type != VK_GEOMETRY_TYPE_INSTANCES_KHR) { 92 bounds.min = vec3(0.0); 93 bounds.max = vec3(0.0); 94 is_active = true; 95 } 96#endif 97 98 if (is_active) { 99 REF(radv_ir_node) ir_node = INDEX(radv_ir_node, args.ir, primitive_id); 100 DEREF(ir_node).aabb = bounds; 101 } 102 103 uint32_t ir_offset = primitive_id * SIZEOF(radv_ir_node); 104 DEREF(id_ptr).id = is_active ? pack_ir_node_id(ir_offset, node_type) : RADV_BVH_INVALID_NODE; 105 106 uvec4 ballot = subgroupBallot(is_active); 107 if (subgroupElect()) 108 atomicAdd(DEREF(args.header).active_leaf_count, subgroupBallotBitCount(ballot)); 109 110 atomicMin(DEREF(args.header).min_bounds[0], to_emulated_float(bounds.min.x)); 111 atomicMin(DEREF(args.header).min_bounds[1], to_emulated_float(bounds.min.y)); 112 atomicMin(DEREF(args.header).min_bounds[2], to_emulated_float(bounds.min.z)); 113 atomicMax(DEREF(args.header).max_bounds[0], to_emulated_float(bounds.max.x)); 114 atomicMax(DEREF(args.header).max_bounds[1], to_emulated_float(bounds.max.y)); 115 atomicMax(DEREF(args.header).max_bounds[2], to_emulated_float(bounds.max.z)); 116} 117