• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Intel Corporation
3  * Copyright © 2020 Valve Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "nir_builder.h"
26 #include "nir_control_flow.h"
27 
28 /**
29  * This file implements an optimization for multiview. Some GPU's have a
30  * special mode which allows the vertex shader (or last stage in the geometry
31  * pipeline) to create multiple primitives in different layers of the
32  * framebuffer at once by writing multiple copies of gl_Position. The
33  * assumption is that in most uses of multiview, the only use of gl_ViewIndex
34  * is to change the position to implement the parallax effect, and other
35  * varyings will be the same between the different views. We put the body of
36  * the original vertex shader in a loop, writing to a different copy of
37  * gl_Position each loop iteration, and then let other optimizations clean up
38  * the mess.
39  */
40 
41 static bool
shader_writes_to_memory(nir_shader * shader)42 shader_writes_to_memory(nir_shader *shader)
43 {
44    /* With multiview, we would need to ensure that memory writes happen either
45     * once or once per view. Since combination of multiview and memory writes
46     * is not expected, we'll just skip this optimization in this case.
47     */
48 
49    nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader);
50 
51    nir_foreach_block(block, entrypoint) {
52       nir_foreach_instr(instr, block) {
53          if (instr->type != nir_instr_type_intrinsic)
54             continue;
55          nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
56 
57          switch (intrin->intrinsic) {
58          case nir_intrinsic_deref_atomic:
59          case nir_intrinsic_deref_atomic_swap:
60          case nir_intrinsic_store_ssbo:
61          case nir_intrinsic_ssbo_atomic:
62          case nir_intrinsic_ssbo_atomic_swap:
63          case nir_intrinsic_store_shared:
64          case nir_intrinsic_store_shared2_amd:
65          case nir_intrinsic_shared_atomic:
66          case nir_intrinsic_shared_atomic_swap:
67          case nir_intrinsic_task_payload_atomic:
68          case nir_intrinsic_task_payload_atomic_swap:
69          case nir_intrinsic_image_deref_store:
70          case nir_intrinsic_image_deref_atomic:
71          case nir_intrinsic_image_deref_atomic_swap:
72             return true;
73 
74          default:
75             /* Keep walking. */
76             break;
77          }
78       }
79    }
80 
81    return false;
82 }
83 
84 bool
nir_shader_uses_view_index(nir_shader * shader)85 nir_shader_uses_view_index(nir_shader *shader)
86 {
87    nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader);
88 
89    nir_foreach_block(block, entrypoint) {
90       nir_foreach_instr(instr, block) {
91          if (instr->type != nir_instr_type_intrinsic)
92             continue;
93 
94          nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
95          if (intrin->intrinsic == nir_intrinsic_load_view_index)
96             return true;
97       }
98    }
99 
100    return false;
101 }
102 
103 static bool
shader_only_position_uses_view_index(nir_shader * shader)104 shader_only_position_uses_view_index(nir_shader *shader)
105 {
106    nir_shader *shader_no_position = nir_shader_clone(NULL, shader);
107    nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader_no_position);
108 
109    /* Remove the store position from a cloned shader. */
110    nir_foreach_block(block, entrypoint) {
111       nir_foreach_instr_safe(instr, block) {
112          if (instr->type != nir_instr_type_intrinsic)
113             continue;
114 
115          nir_intrinsic_instr *store = nir_instr_as_intrinsic(instr);
116          if (store->intrinsic != nir_intrinsic_store_deref)
117             continue;
118 
119          nir_variable *var = nir_intrinsic_get_var(store, 0);
120          if (var->data.location != VARYING_SLOT_POS)
121             continue;
122 
123          nir_instr_remove(&store->instr);
124       }
125    }
126 
127    /* Clean up shader so unused load_view_index intrinsics are removed. */
128    bool progress;
129    do {
130       progress = false;
131       progress |= nir_opt_dead_cf(shader_no_position);
132 
133       /* Peephole select will drop if-blocks that have then and else empty,
134        * which will remove the usage of an SSA in the condition.
135        */
136       progress |= nir_opt_peephole_select(shader_no_position, 0, false, false);
137 
138       progress |= nir_opt_dce(shader_no_position);
139    } while (progress);
140 
141    bool uses_view_index = nir_shader_uses_view_index(shader_no_position);
142 
143    ralloc_free(shader_no_position);
144    return !uses_view_index;
145 }
146 
147 /* Return true if it's safe to call nir_lower_multiview() on this vertex
148  * shader. Note that this only handles driver-agnostic checks, i.e. things
149  * which would make nir_lower_multiview() incorrect. Any driver-specific
150  * checks, e.g. for sufficient varying space or performance considerations,
151  * should be handled in the driver.
152  *
153  * Note that we don't handle the more complex checks needed for lowering
154  * pipelines with geometry or tessellation shaders.
155  */
156 
157 bool
nir_can_lower_multiview(nir_shader * shader)158 nir_can_lower_multiview(nir_shader *shader)
159 {
160    bool writes_position = false;
161    nir_foreach_shader_out_variable(var, shader) {
162       if (var->data.location == VARYING_SLOT_POS) {
163          writes_position = true;
164          break;
165       }
166    }
167 
168    /* Don't bother handling this edge case. */
169    if (!writes_position)
170       return false;
171 
172    return !shader_writes_to_memory(shader) &&
173           shader_only_position_uses_view_index(shader);
174 }
175 
176 /**
177  * The lowering. Call with the last active geometry stage.
178  */
179 
180 bool
nir_lower_multiview(nir_shader * shader,uint32_t view_mask)181 nir_lower_multiview(nir_shader *shader, uint32_t view_mask)
182 {
183    assert(shader->info.stage != MESA_SHADER_FRAGMENT);
184    int view_count = util_bitcount(view_mask);
185 
186    nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader);
187 
188    /* Update position to refer to an array. */
189    nir_variable *pos_var = NULL;
190    nir_foreach_shader_out_variable(var, shader) {
191       if (var->data.location == VARYING_SLOT_POS) {
192          assert(var->type == glsl_vec4_type());
193          var->type = glsl_array_type(glsl_vec4_type(), view_count, 0);
194          var->data.per_view = true;
195          shader->info.per_view_outputs |= VARYING_BIT_POS;
196          pos_var = var;
197          break;
198       }
199    }
200 
201    assert(pos_var);
202 
203    nir_cf_list body;
204    nir_cf_list_extract(&body, &entrypoint->body);
205 
206    nir_builder b = nir_builder_at(nir_after_impl(entrypoint));
207 
208    /* Loop Index will go from 0 to view_count. */
209    nir_variable *loop_index_var =
210       nir_local_variable_create(entrypoint, glsl_uint_type(), "loop_index");
211    nir_deref_instr *loop_index_deref = nir_build_deref_var(&b, loop_index_var);
212    nir_store_deref(&b, loop_index_deref, nir_imm_int(&b, 0), 1);
213 
214    /* Array of view index values that are active in the loop.  Note that the
215     * loop index only matches the view index if there are no gaps in the
216     * view_mask.
217     */
218    nir_variable *view_index_var = nir_local_variable_create(
219       entrypoint, glsl_array_type(glsl_uint_type(), view_count, 0), "view_index");
220    nir_deref_instr *view_index_deref = nir_build_deref_var(&b, view_index_var);
221    {
222       int array_position = 0;
223       uint32_t view_mask_temp = view_mask;
224       while (view_mask_temp) {
225          uint32_t view_index = u_bit_scan(&view_mask_temp);
226          nir_store_deref(&b, nir_build_deref_array_imm(&b, view_index_deref, array_position),
227                          nir_imm_int(&b, view_index), 1);
228          array_position++;
229       }
230    }
231 
232    /* Create the equivalent of
233     *
234     *    while (true):
235     *       if (loop_index >= view_count):
236     *          break
237     *
238     *       view_index = active_indices[loop_index]
239     *       pos_deref = &pos[loop_index]
240     *
241     *       # Placeholder for the body to be reinserted.
242     *
243     *       loop_index += 1
244     *
245     * Later both `view_index` and `pos_deref` will be used to rewrite the
246     * original shader body.
247     */
248 
249    nir_loop *loop = nir_push_loop(&b);
250 
251    nir_def *loop_index = nir_load_deref(&b, loop_index_deref);
252    nir_def *cmp = nir_ige_imm(&b, loop_index, view_count);
253    nir_if *loop_check = nir_push_if(&b, cmp);
254    nir_jump(&b, nir_jump_break);
255    nir_pop_if(&b, loop_check);
256 
257    nir_def *view_index =
258       nir_load_deref(&b, nir_build_deref_array(&b, view_index_deref, loop_index));
259    nir_deref_instr *pos_deref =
260       nir_build_deref_array(&b, nir_build_deref_var(&b, pos_var), loop_index);
261 
262    nir_store_deref(&b, loop_index_deref, nir_iadd_imm(&b, loop_index, 1), 1);
263    nir_pop_loop(&b, loop);
264 
265    /* Reinsert the body. */
266    b.cursor = nir_after_instr(&pos_deref->instr);
267    nir_cf_reinsert(&body, b.cursor);
268 
269    nir_foreach_block(block, entrypoint) {
270       nir_foreach_instr_safe(instr, block) {
271          if (instr->type != nir_instr_type_intrinsic)
272             continue;
273 
274          nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
275 
276          switch (intrin->intrinsic) {
277          case nir_intrinsic_load_view_index: {
278             nir_def_rewrite_uses(&intrin->def, view_index);
279             break;
280          }
281 
282          case nir_intrinsic_store_deref: {
283             nir_variable *var = nir_intrinsic_get_var(intrin, 0);
284             if (var == pos_var) {
285                nir_deref_instr *old_deref = nir_src_as_deref(intrin->src[0]);
286 
287                nir_src_rewrite(&intrin->src[0], &pos_deref->def);
288 
289                /* Remove old deref since it has the wrong type. */
290                nir_deref_instr_remove_if_unused(old_deref);
291             }
292             break;
293          }
294 
295          case nir_intrinsic_load_deref:
296             if (nir_intrinsic_get_var(intrin, 0) == pos_var) {
297                unreachable("Should have lowered I/O to temporaries "
298                            "so no load_deref on position output is expected.");
299             }
300             break;
301 
302          case nir_intrinsic_copy_deref:
303             unreachable("Should have lowered copy_derefs at this point");
304             break;
305 
306          default:
307             /* Do nothing. */
308             break;
309          }
310       }
311    }
312 
313    nir_metadata_preserve(entrypoint, nir_metadata_none);
314    return true;
315 }
316