1 /* 2 * Copyright © 2021 Google 3 * Copyright © 2023 Valve Corporation 4 * SPDX-License-Identifier: MIT 5 */ 6 7 #ifndef LVP_NIR_RAY_TRACING_H 8 #define LVP_NIR_RAY_TRACING_H 9 10 #include "nir/nir.h" 11 #include "nir/nir_builder.h" 12 13 nir_def *lvp_mul_vec3_mat(nir_builder *b, nir_def *vec, nir_def *matrix[], bool translation); 14 15 void lvp_load_wto_matrix(nir_builder *b, nir_def *instance_addr, nir_def **out); 16 17 nir_def *lvp_load_vertex_position(nir_builder *b, nir_def *instance_addr, 18 nir_def *primitive_id, uint32_t index); 19 20 struct lvp_ray_traversal_args; 21 22 struct lvp_ray_flags { 23 nir_def *force_opaque; 24 nir_def *force_not_opaque; 25 nir_def *terminate_on_first_hit; 26 nir_def *no_cull_front; 27 nir_def *no_cull_back; 28 nir_def *no_cull_opaque; 29 nir_def *no_cull_no_opaque; 30 nir_def *no_skip_triangles; 31 nir_def *no_skip_aabbs; 32 }; 33 34 struct lvp_leaf_intersection { 35 nir_def *node_addr; 36 nir_def *primitive_id; 37 nir_def *geometry_id_and_flags; 38 nir_def *opaque; 39 }; 40 41 typedef void (*lvp_aabb_intersection_cb)(nir_builder *b, struct lvp_leaf_intersection *intersection, 42 const struct lvp_ray_traversal_args *args, 43 const struct lvp_ray_flags *ray_flags); 44 45 struct lvp_triangle_intersection { 46 struct lvp_leaf_intersection base; 47 48 nir_def *t; 49 nir_def *frontface; 50 nir_def *barycentrics; 51 }; 52 53 typedef void (*lvp_triangle_intersection_cb)(nir_builder *b, 54 struct lvp_triangle_intersection *intersection, 55 const struct lvp_ray_traversal_args *args, 56 const struct lvp_ray_flags *ray_flags); 57 58 struct lvp_ray_traversal_vars { 59 /* For each accepted hit, tmax will be set to the t value. This allows for automatic intersection 60 * culling. 61 */ 62 nir_deref_instr *tmax; 63 64 /* Those variables change when entering and exiting BLASes. */ 65 nir_deref_instr *origin; 66 nir_deref_instr *dir; 67 nir_deref_instr *inv_dir; 68 69 /* The base address of the current TLAS/BLAS. */ 70 nir_deref_instr *bvh_base; 71 72 nir_deref_instr *current_node; 73 74 nir_deref_instr *stack_base; 75 nir_deref_instr *stack_ptr; 76 nir_deref_instr *stack; 77 78 /* Information about the current instance used for culling. */ 79 nir_deref_instr *instance_addr; 80 nir_deref_instr *sbt_offset_and_flags; 81 }; 82 83 struct lvp_ray_traversal_args { 84 nir_def *root_bvh_base; 85 nir_def *flags; 86 nir_def *cull_mask; 87 nir_def *origin; 88 nir_def *tmin; 89 nir_def *dir; 90 91 struct lvp_ray_traversal_vars vars; 92 93 lvp_aabb_intersection_cb aabb_cb; 94 lvp_triangle_intersection_cb triangle_cb; 95 96 void *data; 97 }; 98 99 /* Builds the ray traversal loop and returns whether traversal is incomplete, similar to 100 * rayQueryProceedEXT. Traversal will only be considered incomplete, if one of the specified 101 * callbacks breaks out of the traversal loop. 102 */ 103 nir_def *lvp_build_ray_traversal(nir_builder *b, const struct lvp_ray_traversal_args *args); 104 105 #endif 106