1 /*
2 * Copyright © 2021 Igalia S.L.
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_builder.h"
26
27 /* Some hardware doesn't have a way to check if invocation was demoted,
28 * in such case we have to track it ourselves.
29 * OpIsHelperInvocationEXT is specified as:
30 *
31 * "An invocation is currently a helper invocation if it was originally
32 * invoked as a helper invocation or if it has been demoted to a helper
33 * invocation by OpDemoteToHelperInvocationEXT."
34 *
35 * Therefore we:
36 * - Set gl_IsHelperInvocationEXT = gl_HelperInvocation
37 * - Add "gl_IsHelperInvocationEXT = true" right before each demote
38 * - Add "gl_IsHelperInvocationEXT = gl_IsHelperInvocationEXT || condition"
39 * right before each demote_if
40 */
41
42 static bool
lower_load_and_store_is_helper(nir_builder * b,nir_intrinsic_instr * intrin,void * data)43 lower_load_and_store_is_helper(nir_builder *b,
44 nir_intrinsic_instr *intrin, void *data)
45 {
46 nir_deref_instr *is_helper_deref = (nir_deref_instr *)data;
47
48 switch (intrin->intrinsic) {
49 case nir_intrinsic_demote: {
50 b->cursor = nir_before_instr(&intrin->instr);
51 nir_store_deref(b, is_helper_deref, nir_imm_true(b), 1);
52 return true;
53 }
54 case nir_intrinsic_demote_if: {
55 b->cursor = nir_before_instr(&intrin->instr);
56 nir_def *current_is_helper = nir_load_deref(b, is_helper_deref);
57 nir_def *updated_is_helper = nir_ior(b, current_is_helper, intrin->src[0].ssa);
58 nir_store_deref(b, is_helper_deref, updated_is_helper, 1);
59 return true;
60 }
61 case nir_intrinsic_is_helper_invocation: {
62 b->cursor = nir_before_instr(&intrin->instr);
63 nir_def *is_helper = nir_load_deref(b, is_helper_deref);
64 nir_def_rewrite_uses(&intrin->def, is_helper);
65 nir_instr_remove(&intrin->instr);
66 return true;
67 }
68 default:
69 return false;
70 }
71 }
72
73 static bool
has_is_helper_invocation(nir_shader * shader)74 has_is_helper_invocation(nir_shader *shader)
75 {
76 nir_foreach_function_impl(impl, shader) {
77 nir_foreach_block_safe(block, impl) {
78 nir_foreach_instr_safe(instr, block) {
79 if (instr->type != nir_instr_type_intrinsic)
80 continue;
81
82 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
83 if (intrin->intrinsic == nir_intrinsic_is_helper_invocation)
84 return true;
85 }
86 }
87 }
88
89 return false;
90 }
91
92 bool
nir_lower_is_helper_invocation(nir_shader * shader)93 nir_lower_is_helper_invocation(nir_shader *shader)
94 {
95 if (shader->info.stage != MESA_SHADER_FRAGMENT)
96 return false;
97
98 if (!has_is_helper_invocation(shader))
99 return false;
100
101 nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader);
102
103 nir_builder b = nir_builder_at(nir_before_impl(entrypoint));
104
105 nir_variable *is_helper = nir_local_variable_create(entrypoint,
106 glsl_bool_type(),
107 "gl_IsHelperInvocationEXT");
108
109 nir_def *started_as_helper = shader->options->lower_helper_invocation ? nir_build_lowered_load_helper_invocation(&b) : nir_load_helper_invocation(&b, 1);
110
111 nir_deref_instr *is_helper_deref = nir_build_deref_var(&b, is_helper);
112 nir_store_deref(&b, is_helper_deref, started_as_helper, 1);
113
114 return nir_shader_intrinsics_pass(shader, lower_load_and_store_is_helper,
115 nir_metadata_block_index |
116 nir_metadata_dominance,
117 is_helper_deref);
118 }
119