xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_opt_fragdepth.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2021 Advanced Micro Devices, Inc.
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 "nir.h"
25 #include "nir_builder.h"
26 
27 /**
28  * This pass removes no-op assignment to gl_FragDepth.
29  *
30  * gl_FragDepth implicit value is gl_FragCoord.z, so if a shader only assign
31  * this value to gl_FragDepth, the store instruction is removed.
32  */
33 
34 static bool
ssa_def_is_source_depth(nir_def * def)35 ssa_def_is_source_depth(nir_def *def)
36 {
37    nir_scalar scalar = nir_scalar_resolved(def, 0);
38    nir_instr *instr = scalar.def->parent_instr;
39    if (instr->type != nir_instr_type_intrinsic)
40       return false;
41 
42    nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
43    if (intrin->intrinsic != nir_intrinsic_load_frag_coord)
44       return false;
45 
46    /* Depth is gl_FragCoord.z */
47    return scalar.comp == 2;
48 }
49 
50 bool
nir_opt_fragdepth(nir_shader * shader)51 nir_opt_fragdepth(nir_shader *shader)
52 {
53    bool progress = false;
54    nir_intrinsic_instr *store_intrin = NULL;
55 
56    if (shader->info.stage != MESA_SHADER_FRAGMENT)
57       goto end;
58 
59    nir_function_impl *impl = nir_shader_get_entrypoint(shader);
60    nir_foreach_block(block, impl) {
61       nir_foreach_instr(instr, block) {
62          if (instr->type != nir_instr_type_intrinsic)
63             continue;
64 
65          nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
66          if (intrin->intrinsic != nir_intrinsic_store_deref &&
67              intrin->intrinsic != nir_intrinsic_store_output)
68             continue;
69 
70          unsigned data_src;
71 
72          if (intrin->intrinsic == nir_intrinsic_store_deref) {
73             nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
74             if (!nir_deref_mode_is(deref, nir_var_shader_out))
75                continue;
76 
77             nir_variable *var = nir_deref_instr_get_variable(deref);
78             if (var->data.location != FRAG_RESULT_DEPTH)
79                continue;
80 
81             data_src = 1;
82          } else {
83             nir_io_semantics sem = nir_intrinsic_io_semantics(intrin);
84 
85             if (sem.location != FRAG_RESULT_DEPTH)
86                continue;
87 
88             data_src = 0;
89          }
90 
91          /* We found a write to gl_FragDepth */
92          if (store_intrin) {
93             /* This isn't the only write: give up on this optimization */
94             goto end;
95          } else {
96             if (ssa_def_is_source_depth(intrin->src[data_src].ssa)) {
97                /* We're writing gl_FragCoord.z in gl_FragDepth: remember
98                 * intrin so we can try to remove it later. */
99                store_intrin = intrin;
100             } else {
101                /* We're writing something else: give up. */
102                goto end;
103             }
104          }
105       }
106    }
107 
108    if (store_intrin) {
109       /* Found a single store to gl_FragDepth, and it writes gl_FragCoord.z to it.
110        * Remove it since that's the implicit value of gl_FragDepth.
111        */
112       nir_instr_remove(&store_intrin->instr);
113 
114       nir_metadata_preserve(impl, nir_metadata_control_flow |
115                                      nir_metadata_loop_analysis |
116                                      nir_metadata_instr_index);
117       progress = true;
118    }
119 
120 end:
121    if (!progress)
122       nir_shader_preserve_all_metadata(shader);
123    return progress;
124 }
125