• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2020 Valve Corporation
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 
25 #include "nir.h"
26 #include "nir_builder.h"
27 
28 static bool
nir_lower_discard_to_demote_instr(nir_builder * b,nir_instr * instr,void * data)29 nir_lower_discard_to_demote_instr(nir_builder *b, nir_instr *instr, void *data)
30 {
31    if (instr->type != nir_instr_type_intrinsic)
32       return false;
33 
34    nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
35    switch (intrin->intrinsic) {
36    case nir_intrinsic_discard:
37       intrin->intrinsic = nir_intrinsic_demote;
38       b->shader->info.fs.uses_demote = true;
39       return true;
40    case nir_intrinsic_discard_if:
41       intrin->intrinsic = nir_intrinsic_demote_if;
42       b->shader->info.fs.uses_demote = true;
43       return true;
44    case nir_intrinsic_load_helper_invocation:
45       intrin->intrinsic = nir_intrinsic_is_helper_invocation;
46       return true;
47    default:
48       return false;
49    }
50 }
51 
52 /**
53  * This pass is intended as workaround for game bugs to force correct
54  * derivatives after kill. This lowering is not valid in the general case
55  * as it might change the result of subgroup operations and loop behavior.
56  *
57  * discard() will be lowered as demote() and gl_HelperInvocation
58  * will be lowered as helperInvocationEXT().
59  */
60 bool
nir_lower_discard_to_demote(nir_shader * shader)61 nir_lower_discard_to_demote(nir_shader *shader)
62 {
63    if (shader->info.stage != MESA_SHADER_FRAGMENT)
64       return false;
65 
66    return nir_shader_instructions_pass(shader,
67                                        nir_lower_discard_to_demote_instr,
68                                        nir_metadata_all,
69                                        NULL);
70 }
71