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_replace(&intrin->def, is_helper);
65 return true;
66 }
67 default:
68 return false;
69 }
70 }
71
72 static bool
has_is_helper_invocation(nir_shader * shader)73 has_is_helper_invocation(nir_shader *shader)
74 {
75 nir_foreach_function_impl(impl, shader) {
76 nir_foreach_block_safe(block, impl) {
77 nir_foreach_instr_safe(instr, block) {
78 if (instr->type != nir_instr_type_intrinsic)
79 continue;
80
81 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
82 if (intrin->intrinsic == nir_intrinsic_is_helper_invocation)
83 return true;
84 }
85 }
86 }
87
88 return false;
89 }
90
91 bool
nir_lower_is_helper_invocation(nir_shader * shader)92 nir_lower_is_helper_invocation(nir_shader *shader)
93 {
94 if (shader->info.stage != MESA_SHADER_FRAGMENT)
95 return false;
96
97 if (!has_is_helper_invocation(shader))
98 return false;
99
100 nir_function_impl *entrypoint = nir_shader_get_entrypoint(shader);
101
102 nir_builder b = nir_builder_at(nir_before_impl(entrypoint));
103
104 nir_variable *is_helper = nir_local_variable_create(entrypoint,
105 glsl_bool_type(),
106 "gl_IsHelperInvocationEXT");
107
108 nir_def *started_as_helper = shader->options->lower_helper_invocation ? nir_build_lowered_load_helper_invocation(&b) : nir_load_helper_invocation(&b, 1);
109
110 nir_deref_instr *is_helper_deref = nir_build_deref_var(&b, is_helper);
111 nir_store_deref(&b, is_helper_deref, started_as_helper, 1);
112
113 return nir_shader_intrinsics_pass(shader, lower_load_and_store_is_helper,
114 nir_metadata_control_flow,
115 is_helper_deref);
116 }
117