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
34static bool
35ssa_def_is_source_depth(nir_ssa_def *def)
36{
37   nir_ssa_scalar scalar = nir_ssa_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
50bool
51nir_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            continue;
68
69         nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
70         if (!nir_deref_mode_is(deref, nir_var_shader_out))
71            continue;
72
73         nir_variable *var = nir_deref_instr_get_variable(deref);
74         if (var->data.location != FRAG_RESULT_DEPTH)
75            continue;
76
77         /* We found a write to gl_FragDepth */
78         if (store_intrin) {
79            /* This isn't the only write: give up on this optimization */
80            goto end;
81         } else {
82            if (ssa_def_is_source_depth(intrin->src[1].ssa)) {
83               /* We're writing gl_FragCoord.z in gl_FragDepth: remember
84                * intrin so we can try to remove it later. */
85               store_intrin = intrin;
86            } else {
87               /* We're writing something else: give up. */
88               goto end;
89            }
90         }
91      }
92   }
93
94   if (store_intrin) {
95      /* Found a single store to gl_FragDepth, and it writes gl_FragCoord.z to it.
96       * Remove it since that's the implicit value of gl_FragDepth.
97       */
98      nir_instr_remove(&store_intrin->instr);
99
100      nir_metadata_preserve(impl, nir_metadata_block_index |
101                                  nir_metadata_dominance |
102                                  nir_metadata_loop_analysis |
103                                  nir_metadata_instr_index);
104      progress = true;
105   }
106
107end:
108   if (!progress)
109      nir_shader_preserve_all_metadata(shader);
110   return progress;
111}
112