1 /* 2 * Copyright © 2021 Google 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 #ifndef RADV_RT_COMMON_H 25 #define RADV_RT_COMMON_H 26 27 #include "nir/nir.h" 28 #include "nir/nir_builder.h" 29 #include "vk_nir_convert_ycbcr.h" 30 31 #include "compiler/spirv/spirv.h" 32 33 #include "radv_private.h" 34 35 nir_def *build_addr_to_node(nir_builder *b, nir_def *addr); 36 37 nir_def *nir_build_vec3_mat_mult(nir_builder *b, nir_def *vec, nir_def *matrix[], bool translation); 38 39 void nir_build_wto_matrix_load(nir_builder *b, nir_def *instance_addr, nir_def **out); 40 41 nir_def *radv_load_vertex_position(struct radv_device *device, nir_builder *b, nir_def *instance_addr, 42 nir_def *primitive_id, uint32_t index); 43 44 struct radv_ray_traversal_args; 45 46 struct radv_ray_flags { 47 nir_def *force_opaque; 48 nir_def *force_not_opaque; 49 nir_def *terminate_on_first_hit; 50 nir_def *no_cull_front; 51 nir_def *no_cull_back; 52 nir_def *no_cull_opaque; 53 nir_def *no_cull_no_opaque; 54 nir_def *no_skip_triangles; 55 nir_def *no_skip_aabbs; 56 }; 57 58 struct radv_leaf_intersection { 59 nir_def *node_addr; 60 nir_def *primitive_id; 61 nir_def *geometry_id_and_flags; 62 nir_def *opaque; 63 }; 64 65 typedef void (*radv_aabb_intersection_cb)(nir_builder *b, struct radv_leaf_intersection *intersection, 66 const struct radv_ray_traversal_args *args); 67 68 struct radv_triangle_intersection { 69 struct radv_leaf_intersection base; 70 71 nir_def *t; 72 nir_def *frontface; 73 nir_def *barycentrics; 74 }; 75 76 typedef void (*radv_triangle_intersection_cb)(nir_builder *b, struct radv_triangle_intersection *intersection, 77 const struct radv_ray_traversal_args *args, 78 const struct radv_ray_flags *ray_flags); 79 80 typedef void (*radv_rt_stack_store_cb)(nir_builder *b, nir_def *index, nir_def *value, 81 const struct radv_ray_traversal_args *args); 82 83 typedef nir_def *(*radv_rt_stack_load_cb)(nir_builder *b, nir_def *index, const struct radv_ray_traversal_args *args); 84 85 struct radv_ray_traversal_vars { 86 /* For each accepted hit, tmax will be set to the t value. This allows for automatic intersection 87 * culling. 88 */ 89 nir_deref_instr *tmax; 90 91 /* Those variables change when entering and exiting BLASes. */ 92 nir_deref_instr *origin; 93 nir_deref_instr *dir; 94 nir_deref_instr *inv_dir; 95 96 /* The base address of the current TLAS/BLAS. */ 97 nir_deref_instr *bvh_base; 98 99 /* stack is the current stack pointer/index. top_stack is the pointer/index that marks the end of 100 * traversal for the current BLAS/TLAS. stack_low_watermark is the low watermark of the short 101 * stack. 102 */ 103 nir_deref_instr *stack; 104 nir_deref_instr *top_stack; 105 nir_deref_instr *stack_low_watermark; 106 107 nir_deref_instr *current_node; 108 109 /* The node visited in the previous iteration. This is used in backtracking to jump to its parent 110 * and then find the child after the previously visited node. 111 */ 112 nir_deref_instr *previous_node; 113 114 /* When entering an instance these are the instance node and the root node of the BLAS */ 115 nir_deref_instr *instance_top_node; 116 nir_deref_instr *instance_bottom_node; 117 118 /* Information about the current instance used for culling. */ 119 nir_deref_instr *instance_addr; 120 nir_deref_instr *sbt_offset_and_flags; 121 122 /* Statistics. Iteration count in the low 16 bits, candidate instance counts in the high 16 bits. */ 123 nir_deref_instr *iteration_instance_count; 124 }; 125 126 struct radv_ray_traversal_args { 127 nir_def *root_bvh_base; 128 nir_def *flags; 129 nir_def *cull_mask; 130 nir_def *origin; 131 nir_def *tmin; 132 nir_def *dir; 133 134 struct radv_ray_traversal_vars vars; 135 136 /* The increment/decrement used for radv_ray_traversal_vars::stack, and how many entries are 137 * available. stack_base is the base address of the stack. */ 138 uint32_t stack_stride; 139 uint32_t stack_entries; 140 uint32_t stack_base; 141 142 bool ignore_cull_mask; 143 144 radv_rt_stack_store_cb stack_store_cb; 145 radv_rt_stack_load_cb stack_load_cb; 146 147 radv_aabb_intersection_cb aabb_cb; 148 radv_triangle_intersection_cb triangle_cb; 149 150 void *data; 151 }; 152 153 /* For the initialization of instance_bottom_node. Explicitly different than RADV_BVH_INVALID_NODE 154 * or any real node, to ensure we never exit an instance when we're not in one. */ 155 #define RADV_BVH_NO_INSTANCE_ROOT 0xfffffffeu 156 157 /* Builds the ray traversal loop and returns whether traversal is incomplete, similar to 158 * rayQueryProceedEXT. Traversal will only be considered incomplete, if one of the specified 159 * callbacks breaks out of the traversal loop. 160 */ 161 nir_def *radv_build_ray_traversal(struct radv_device *device, nir_builder *b, 162 const struct radv_ray_traversal_args *args); 163 164 #endif 165