xref: /aosp_15_r20/external/mesa3d/src/amd/vulkan/nir/radv_nir_lower_viewport_to_zero.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2016 Red Hat.
3  * Copyright © 2016 Bas Nieuwenhuizen
4  * Copyright © 2023 Valve Corporation
5  *
6  * SPDX-License-Identifier: MIT
7  */
8 
9 #include "nir.h"
10 #include "nir_builder.h"
11 #include "radv_nir.h"
12 
13 bool
radv_nir_lower_viewport_to_zero(nir_shader * nir)14 radv_nir_lower_viewport_to_zero(nir_shader *nir)
15 {
16    nir_function_impl *impl = nir_shader_get_entrypoint(nir);
17    bool progress = false;
18 
19    nir_builder b = nir_builder_create(impl);
20 
21    /* There should be only one deref load for VIEWPORT after lower_io_to_temporaries. */
22    nir_foreach_block (block, impl) {
23       nir_foreach_instr (instr, block) {
24          if (instr->type != nir_instr_type_intrinsic)
25             continue;
26 
27          nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
28          if (intr->intrinsic != nir_intrinsic_load_deref)
29             continue;
30 
31          nir_variable *var = nir_intrinsic_get_var(intr, 0);
32          if (var->data.mode != nir_var_shader_in || var->data.location != VARYING_SLOT_VIEWPORT)
33             continue;
34 
35          b.cursor = nir_before_instr(instr);
36 
37          nir_def_rewrite_uses(&intr->def, nir_imm_zero(&b, 1, 32));
38          progress = true;
39          break;
40       }
41       if (progress)
42          break;
43    }
44 
45    if (progress)
46       nir_metadata_preserve(impl, nir_metadata_control_flow);
47    else
48       nir_metadata_preserve(impl, nir_metadata_all);
49 
50    return progress;
51 }
52