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 bool
radv_nir_lower_viewport_to_zero(nir_shader * nir)31 radv_nir_lower_viewport_to_zero(nir_shader *nir)
32 {
33 nir_function_impl *impl = nir_shader_get_entrypoint(nir);
34 bool progress = false;
35
36 nir_builder b = nir_builder_create(impl);
37
38 /* There should be only one deref load for VIEWPORT after lower_io_to_temporaries. */
39 nir_foreach_block (block, impl) {
40 nir_foreach_instr (instr, block) {
41 if (instr->type != nir_instr_type_intrinsic)
42 continue;
43
44 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
45 if (intr->intrinsic != nir_intrinsic_load_deref)
46 continue;
47
48 nir_variable *var = nir_intrinsic_get_var(intr, 0);
49 if (var->data.mode != nir_var_shader_in || var->data.location != VARYING_SLOT_VIEWPORT)
50 continue;
51
52 b.cursor = nir_before_instr(instr);
53
54 nir_def_rewrite_uses(&intr->def, nir_imm_zero(&b, 1, 32));
55 progress = true;
56 break;
57 }
58 if (progress)
59 break;
60 }
61
62 if (progress)
63 nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance);
64 else
65 nir_metadata_preserve(impl, nir_metadata_all);
66
67 return progress;
68 }
69