2
* Copyright © 2021 Advanced Micro Devices, Inc.
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:
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
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
25
#include "nir_builder.h"
28
* This pass removes no-op assignment to gl_FragDepth.
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.
35
ssa_def_is_source_depth(nir_ssa_def *def)
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)
42
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
43
if (intrin->intrinsic != nir_intrinsic_load_frag_coord)
46
/* Depth is gl_FragCoord.z */
47
return scalar.comp == 2;
51
nir_opt_fragdepth(nir_shader *shader)
53
bool progress = false;
54
nir_intrinsic_instr *store_intrin = NULL;
56
if (shader->info.stage != MESA_SHADER_FRAGMENT)
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)
65
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
66
if (intrin->intrinsic != nir_intrinsic_store_deref)
69
nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
70
if (!nir_deref_mode_is(deref, nir_var_shader_out))
73
nir_variable *var = nir_deref_instr_get_variable(deref);
74
if (var->data.location != FRAG_RESULT_DEPTH)
77
/* We found a write to gl_FragDepth */
79
/* This isn't the only write: give up on this optimization */
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;
87
/* We're writing something else: give up. */
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.
98
nir_instr_remove(&store_intrin->instr);
100
nir_metadata_preserve(impl, nir_metadata_block_index |
101
nir_metadata_dominance |
102
nir_metadata_loop_analysis |
103
nir_metadata_instr_index);
109
nir_shader_preserve_all_metadata(shader);