1 /*
2  * Copyright (c) 2020 Intel Corporation
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 #include "brw_nir_rt.h"
25 #include "brw_nir_rt_builder.h"
26 
27 static nir_function_impl *
lower_any_hit_for_intersection(nir_shader *any_hit)28 lower_any_hit_for_intersection(nir_shader *any_hit)
29 {
30    nir_function_impl *impl = nir_shader_get_entrypoint(any_hit);
31 
32    /* Any-hit shaders need three parameters */
33    assert(impl->function->num_params == 0);
34    nir_parameter params[] = {
35       {
36          /* A pointer to a boolean value for whether or not the hit was
37           * accepted.
38           */
39          .num_components = 1,
40          .bit_size = 32,
41       },
42       {
43          /* The hit T value */
44          .num_components = 1,
45          .bit_size = 32,
46       },
47       {
48          /* The hit kind */
49          .num_components = 1,
50          .bit_size = 32,
51       },
52    };
53    impl->function->num_params = ARRAY_SIZE(params);
54    impl->function->params =
55       ralloc_array(any_hit, nir_parameter, ARRAY_SIZE(params));
56    memcpy(impl->function->params, params, sizeof(params));
57 
58    nir_builder build;
59    nir_builder_init(&build, impl);
60    nir_builder *b = &build;
61 
62    b->cursor = nir_before_cf_list(&impl->body);
63 
64    nir_ssa_def *commit_ptr = nir_load_param(b, 0);
65    nir_ssa_def *hit_t = nir_load_param(b, 1);
66    nir_ssa_def *hit_kind = nir_load_param(b, 2);
67 
68    nir_deref_instr *commit =
69       nir_build_deref_cast(b, commit_ptr, nir_var_function_temp,
70                            glsl_bool_type(), 0);
71 
72    nir_foreach_block_safe(block, impl) {
73       nir_foreach_instr_safe(instr, block) {
74          switch (instr->type) {
75          case nir_instr_type_intrinsic: {
76             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
77             switch (intrin->intrinsic) {
78             case nir_intrinsic_ignore_ray_intersection:
79                b->cursor = nir_instr_remove(&intrin->instr);
80                /* We put the newly emitted code inside a dummy if because it's
81                 * going to contain a jump instruction and we don't want to
82                 * deal with that mess here.  It'll get dealt with by our
83                 * control-flow optimization passes.
84                 */
85                nir_store_deref(b, commit, nir_imm_false(b), 0x1);
86                nir_push_if(b, nir_imm_true(b));
87                nir_jump(b, nir_jump_halt);
88                nir_pop_if(b, NULL);
89                break;
90 
91             case nir_intrinsic_terminate_ray:
92                /* The "normal" handling of terminateRay works fine in
93                 * intersection shaders.
94                 */
95                break;
96 
97             case nir_intrinsic_load_ray_t_max:
98                nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
99                                         hit_t);
100                nir_instr_remove(&intrin->instr);
101                break;
102 
103             case nir_intrinsic_load_ray_hit_kind:
104                nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
105                                         hit_kind);
106                nir_instr_remove(&intrin->instr);
107                break;
108 
109             default:
110                break;
111             }
112             break;
113          }
114 
115          default:
116             break;
117          }
118       }
119    }
120 
121    nir_validate_shader(any_hit, "after initial any-hit lowering");
122 
123    nir_lower_returns_impl(impl);
124 
125    nir_validate_shader(any_hit, "after lowering returns");
126 
127    return impl;
128 }
129 
130 void
brw_nir_lower_intersection_shader(nir_shader *intersection, const nir_shader *any_hit, const struct intel_device_info *devinfo)131 brw_nir_lower_intersection_shader(nir_shader *intersection,
132                                   const nir_shader *any_hit,
133                                   const struct intel_device_info *devinfo)
134 {
135    void *dead_ctx = ralloc_context(intersection);
136 
137    nir_function_impl *any_hit_impl = NULL;
138    struct hash_table *any_hit_var_remap = NULL;
139    if (any_hit) {
140       nir_shader *any_hit_tmp = nir_shader_clone(dead_ctx, any_hit);
141       NIR_PASS_V(any_hit_tmp, nir_opt_dce);
142       any_hit_impl = lower_any_hit_for_intersection(any_hit_tmp);
143       any_hit_var_remap = _mesa_pointer_hash_table_create(dead_ctx);
144    }
145 
146    nir_function_impl *impl = nir_shader_get_entrypoint(intersection);
147 
148    nir_builder build;
149    nir_builder_init(&build, impl);
150    nir_builder *b = &build;
151 
152    b->cursor = nir_before_cf_list(&impl->body);
153 
154    nir_ssa_def *t_addr = brw_nir_rt_mem_hit_addr(b, false /* committed */);
155    nir_variable *commit =
156       nir_local_variable_create(impl, glsl_bool_type(), "ray_commit");
157    nir_store_var(b, commit, nir_imm_false(b), 0x1);
158 
159    assert(impl->end_block->predecessors->entries == 1);
160    set_foreach(impl->end_block->predecessors, block_entry) {
161       struct nir_block *block = (void *)block_entry->key;
162       b->cursor = nir_after_block_before_jump(block);
163       nir_push_if(b, nir_load_var(b, commit));
164       {
165          /* Set the "valid" bit in mem_hit */
166          nir_ssa_def *ray_addr = brw_nir_rt_mem_hit_addr(b, false /* committed */);
167          nir_ssa_def *flags_dw_addr = nir_iadd_imm(b, ray_addr, 12);
168          nir_store_global(b, flags_dw_addr, 4,
169             nir_ior(b, nir_load_global(b, flags_dw_addr, 4, 1, 32),
170                        nir_imm_int(b, 1 << 16)), 0x1 /* write_mask */);
171 
172          nir_accept_ray_intersection(b);
173       }
174       nir_push_else(b, NULL);
175       {
176          nir_ignore_ray_intersection(b);
177       }
178       nir_pop_if(b, NULL);
179       break;
180    }
181 
182    nir_foreach_block_safe(block, impl) {
183       nir_foreach_instr_safe(instr, block) {
184          switch (instr->type) {
185          case nir_instr_type_intrinsic: {
186             nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
187             switch (intrin->intrinsic) {
188             case nir_intrinsic_report_ray_intersection: {
189                b->cursor = nir_instr_remove(&intrin->instr);
190                nir_ssa_def *hit_t = nir_ssa_for_src(b, intrin->src[0], 1);
191                nir_ssa_def *hit_kind = nir_ssa_for_src(b, intrin->src[1], 1);
192                nir_ssa_def *min_t = nir_load_ray_t_min(b);
193                nir_ssa_def *max_t = nir_load_global(b, t_addr, 4, 1, 32);
194 
195                /* bool commit_tmp = false; */
196                nir_variable *commit_tmp =
197                   nir_local_variable_create(impl, glsl_bool_type(),
198                                             "commit_tmp");
199                nir_store_var(b, commit_tmp, nir_imm_false(b), 0x1);
200 
201                nir_push_if(b, nir_iand(b, nir_fge(b, hit_t, min_t),
202                                           nir_fge(b, max_t, hit_t)));
203                {
204                   /* Any-hit defaults to commit */
205                   nir_store_var(b, commit_tmp, nir_imm_true(b), 0x1);
206 
207                   if (any_hit_impl != NULL) {
208                      nir_push_if(b, nir_inot(b, nir_load_leaf_opaque_intel(b)));
209                      {
210                         nir_ssa_def *params[] = {
211                            &nir_build_deref_var(b, commit_tmp)->dest.ssa,
212                            hit_t,
213                            hit_kind,
214                         };
215                         nir_inline_function_impl(b, any_hit_impl, params,
216                                                  any_hit_var_remap);
217                      }
218                      nir_pop_if(b, NULL);
219                   }
220 
221                   nir_push_if(b, nir_load_var(b, commit_tmp));
222                   {
223                      nir_store_var(b, commit, nir_imm_true(b), 0x1);
224                      nir_store_global(b, t_addr, 4,
225                                       nir_vec2(b, hit_t, hit_kind),
226                                       0x3);
227                   }
228                   nir_pop_if(b, NULL);
229                }
230                nir_pop_if(b, NULL);
231 
232                nir_ssa_def *accepted = nir_load_var(b, commit_tmp);
233                nir_ssa_def_rewrite_uses(&intrin->dest.ssa,
234                                         accepted);
235                break;
236             }
237 
238             default:
239                break;
240             }
241             break;
242          }
243 
244          default:
245             break;
246          }
247       }
248    }
249    nir_metadata_preserve(impl, nir_metadata_none);
250 
251    /* We did some inlining; have to re-index SSA defs */
252    nir_index_ssa_defs(impl);
253 
254    ralloc_free(dead_ctx);
255 }
256