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