• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  * Copyright © 2023 Valve Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23  * IN THE SOFTWARE.
24  */
25 
26 #include "nir.h"
27 #include "nir_builder.h"
28 #include "radv_nir.h"
29 
30 static nir_variable *
find_layer_out_var(nir_shader * nir)31 find_layer_out_var(nir_shader *nir)
32 {
33    nir_variable *var = nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_LAYER);
34    if (var != NULL)
35       return var;
36 
37    var = nir_variable_create(nir, nir_var_shader_out, glsl_int_type(), "layer id");
38    var->data.location = VARYING_SLOT_LAYER;
39    var->data.interpolation = INTERP_MODE_NONE;
40 
41    return var;
42 }
43 
44 bool
radv_nir_export_multiview(nir_shader * nir)45 radv_nir_export_multiview(nir_shader *nir)
46 {
47    nir_function_impl *impl = nir_shader_get_entrypoint(nir);
48    bool progress = false;
49 
50    nir_builder b = nir_builder_create(impl);
51 
52    /* This pass is not suitable for mesh shaders, because it can't know the mapping between API mesh
53     * shader invocations and output primitives. Needs to be handled in ac_nir_lower_ngg.
54     */
55    assert(nir->info.stage == MESA_SHADER_VERTEX || nir->info.stage == MESA_SHADER_TESS_EVAL ||
56           nir->info.stage == MESA_SHADER_GEOMETRY);
57 
58    /* Iterate in reverse order since there should be only one deref store to POS after
59     * lower_io_to_temporaries for vertex shaders and inject the layer there. For geometry shaders,
60     * the layer is injected right before every emit_vertex_with_counter.
61     */
62    nir_variable *layer = NULL;
63    nir_foreach_block_reverse (block, impl) {
64       nir_foreach_instr_reverse (instr, block) {
65          if (instr->type != nir_instr_type_intrinsic)
66             continue;
67 
68          if (nir->info.stage == MESA_SHADER_GEOMETRY) {
69             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
70             if (intr->intrinsic != nir_intrinsic_emit_vertex_with_counter)
71                continue;
72 
73             b.cursor = nir_before_instr(instr);
74          } else {
75             nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
76             if (intr->intrinsic != nir_intrinsic_store_deref)
77                continue;
78 
79             nir_variable *var = nir_intrinsic_get_var(intr, 0);
80             if (var->data.mode != nir_var_shader_out || var->data.location != VARYING_SLOT_POS)
81                continue;
82 
83             b.cursor = nir_after_instr(instr);
84          }
85 
86          if (!layer)
87             layer = find_layer_out_var(nir);
88 
89          nir_store_var(&b, layer, nir_load_view_index(&b), 1);
90 
91          /* Update outputs_written to reflect that the pass added a new output. */
92          nir->info.outputs_written |= BITFIELD64_BIT(VARYING_SLOT_LAYER);
93 
94          progress = true;
95          if (nir->info.stage == MESA_SHADER_VERTEX)
96             break;
97       }
98       if (nir->info.stage == MESA_SHADER_VERTEX && progress)
99          break;
100    }
101 
102    if (progress)
103       nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance);
104    else
105       nir_metadata_preserve(impl, nir_metadata_all);
106 
107    return progress;
108 }
109