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