Debian Codegraph @

src/panfrost/vulkan/panvk_vX_nir_lower_input_attachment_loads.c

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 *
4 * Derived from tu_shader.c which is:
5 * Copyright © 2019 Google LLC
6 *
7 * Also derived from anv_pipeline.c which is
8 * Copyright © 2015 Intel Corporation
9 *
10 * SPDX-License-Identifier: MIT
11 */
12
13 #include "panvk_device.h"
14 #include "panvk_shader.h"
15
16 #include "vk_graphics_state.h"
17
18 #include "nir.h"
19 #include "nir_builder.h"
20 #include "pan_nir.h"
21
22 struct panvk_lower_input_attachment_load_ctx {
23 uint32_t ro_color_mask;
24 uint32_t input_attachment_read;
25 };
26
27 static bool
28 collect_frag_writes(nir_builder *b, nir_intrinsic_instr *intr, void *data)
29 {
30 if (intr->intrinsic != nir_intrinsic_store_deref)
31 return false;
32
33 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
34
35 if (deref->modes != nir_var_shader_out)
36 return false;
37
38 nir_variable *var = nir_deref_instr_get_variable(deref);
39 assert(var);
40
41 if (var->data.location < FRAG_RESULT_DATA0 ||
42 var->data.location > FRAG_RESULT_DATA7)
43 return false;
44
45 uint32_t *written_mask = data;
46
47 *written_mask |= BITFIELD_BIT(var->data.location - FRAG_RESULT_DATA0);
48 return true;
49 }
50
51 static uint32_t
52 readonly_color_mask(nir_shader *nir,
53 const struct vk_graphics_pipeline_state *state)
54 {
55 if (!state || !state->ial || !state->cal)
56 return 0;
57
58 uint32_t in_mask = 0, out_mask = 0;
59
60 for (uint32_t i = 0; i < ARRAY_SIZE(state->ial->color_map); i++) {
61 if (i >= state->ial->color_attachment_count)
62 break;
63
64 if (state->ial->color_map[i] != MESA_VK_ATTACHMENT_UNUSED)
65 in_mask |= BITFIELD_BIT(i);
66 }
67
68 NIR_PASS(_, nir, nir_shader_intrinsics_pass, collect_frag_writes,
69 nir_metadata_all, &out_mask);
70
71 for (uint32_t i = 0; i < ARRAY_SIZE(state->cal->color_map); i++) {
72 if (state->ial->color_map[i] == MESA_VK_ATTACHMENT_UNUSED)
73 out_mask &= ~BITFIELD_BIT(i);
74 }
75
76 return in_mask & ~out_mask;
77 }
78
79 static bool
80 lower_input_attachment_load(nir_builder *b, nir_intrinsic_instr *intr,
81 void *data)
82 {
83 struct panvk_lower_input_attachment_load_ctx *ctx = data;
84
85 if (intr->intrinsic != nir_intrinsic_image_deref_load &&
86 intr->intrinsic != nir_intrinsic_image_deref_sparse_load)
87 return false;
88
89 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
90 enum glsl_sampler_dim image_dim = glsl_get_sampler_dim(deref->type);
91 if (image_dim != GLSL_SAMPLER_DIM_SUBPASS &&
92 image_dim != GLSL_SAMPLER_DIM_SUBPASS_MS)
93 return false;
94
95 b->cursor = nir_before_instr(&intr->instr);
96
97 uint32_t index_imm = 0, range = 1;
98 nir_def *index_ssa = NULL;
99 if (deref->deref_type == nir_deref_type_array) {
100 nir_deref_instr *parent = nir_deref_instr_parent(deref);
101 if (nir_src_is_const(deref->arr.index)) {
102 index_imm = nir_src_as_uint(deref->arr.index);
103 } else {
104 index_ssa = deref->arr.index.ssa;
105 range = glsl_array_size(parent->type);
106 }
107 deref = parent;
108 }
109
110 assert(deref->deref_type == nir_deref_type_var);
111 nir_variable *var = deref->var;
112
113 const unsigned base_idx =
114 var->data.index != NIR_VARIABLE_NO_INDEX ? var->data.index + 1 : 0;
115 index_imm += base_idx;
116 index_ssa = index_ssa ?
117 nir_iadd_imm(b, index_ssa, base_idx) : nir_imm_int(b, index_imm);
118
119 nir_alu_type dest_type = nir_intrinsic_dest_type(intr);
120
121 /* Zero means variable array. */
122 range = range == 0 ? 9 - index_imm : range;
123 ctx->input_attachment_read |= BITFIELD_RANGE(index_imm, range);
124
125 nir_def *target = nir_load_input_attachment_target_pan(b, index_ssa);
126 nir_def *load_img, *load_output;
127
128 nir_push_if(b, nir_ine_imm(b, target, ~0));
129 {
130 nir_def *is_color_att = nir_ilt_imm(b, target, 8);
131 nir_def *load_color, *load_zs;
132 nir_io_semantics iosem = {0};
133 iosem.fb_fetch_output = true;
134 iosem.fb_fetch_output_coherent = !!(nir_intrinsic_access(intr) & ACCESS_COHERENT);
135
136 nir_push_if(b, is_color_att);
137 {
138 nir_def *conversion =
139 nir_load_input_attachment_conv_pan(b, index_ssa);
140 nir_def *is_read_only =
141 nir_i2b(b, nir_iand_imm(b, nir_ishl(b, nir_imm_int(b, 1), target),
142 ctx->ro_color_mask));
143 nir_def *load_ro_color, *load_rw_color;
144
145 iosem.location = FRAG_RESULT_DATA0;
146 nir_push_if(b, is_read_only);
147 {
148 load_ro_color = nir_load_tile_res_pan(
149 b, intr->def.num_components, intr->def.bit_size,
150 pan_nir_tile_rt_sample(b, target, intr->src[2].ssa),
151 pan_nir_tile_default_coverage(b),
152 conversion, .dest_type = dest_type,
153 .access = nir_intrinsic_access(intr), .io_semantics = iosem);
154 }
155 nir_push_else(b, NULL);
156 {
157 load_rw_color = nir_load_tile_pan(
158 b, intr->def.num_components, intr->def.bit_size,
159 pan_nir_tile_rt_sample(b, target, intr->src[2].ssa),
160 pan_nir_tile_default_coverage(b),
161 conversion, .dest_type = dest_type,
162 .access = nir_intrinsic_access(intr), .io_semantics = iosem);
163 }
164 nir_pop_if(b, NULL);
165 load_color = nir_if_phi(b, load_ro_color, load_rw_color);
166 }
167 nir_push_else(b, NULL);
168 {
169 #if PAN_ARCH < 9
170 /* On v7, we need to pass the depth format around. If we use a
171 * conversion of zero, like we do on v9+, the GPU reports an
172 * INVALID_INSTR_ENC. */
173 struct mali_internal_conversion_packed stencil_conv;
174
175 pan_pack(&stencil_conv, INTERNAL_CONVERSION, cfg) {
176 cfg.register_format = MALI_REGISTER_FILE_FORMAT_U32;
177 cfg.memory_format = GENX(pan_dithered_format_from_pipe_format)(
178 PIPE_FORMAT_S8_UINT, false);
179 }
180
181 nir_def *conversion =
182 dest_type == nir_type_uint32
183 ? nir_imm_int(b, stencil_conv.opaque[0])
184 : nir_load_input_attachment_conv_pan(b, index_ssa);
185 #else
186 nir_def *conversion = nir_imm_int(b, 0);
187 #endif
188
189 iosem.location = dest_type == nir_type_float32 ? FRAG_RESULT_DEPTH
190 : FRAG_RESULT_STENCIL;
191 target = nir_imm_int(b, 0);
192 load_zs = nir_load_tile_pan(
193 b, intr->def.num_components, intr->def.bit_size,
194 pan_nir_tile_location_sample(b, iosem.location, intr->src[2].ssa),
195 pan_nir_tile_default_coverage(b),
196 conversion, .dest_type = dest_type,
197 .access = nir_intrinsic_access(intr), .io_semantics = iosem);
198
199 /* If we loaded the stencil value, the upper 24 bits might contain
200 * garbage, hence the masking done here. */
201 if (iosem.location == FRAG_RESULT_STENCIL)
202 load_zs = nir_iand_imm(b, load_zs, BITFIELD_MASK(8));
203 }
204 nir_pop_if(b, NULL);
205
206 load_output = nir_if_phi(b, load_color, load_zs);
207 }
208 nir_push_else(b, NULL);
209 {
210 nir_instr *load_clone = nir_instr_clone(b->shader, &intr->instr);
211 nir_builder_instr_insert(b, load_clone);
212 load_img = &nir_instr_as_intrinsic(load_clone)->def;
213 }
214 nir_pop_if(b, NULL);
215
216 nir_def_replace(&intr->def, nir_if_phi(b, load_output, load_img));
217
218 return true;
219 }
220
221 bool
222 panvk_per_arch(nir_lower_input_attachment_loads)(
223 nir_shader *nir,
224 const struct vk_graphics_pipeline_state *state,
225 uint32_t *input_attachment_read_out)
226 {
227 bool progress = false;
228 struct panvk_lower_input_attachment_load_ctx ia_load_ctx = {
229 .ro_color_mask = readonly_color_mask(nir, state),
230 };
231
232 NIR_PASS(progress, nir, nir_shader_intrinsics_pass,
233 lower_input_attachment_load, nir_metadata_none,
234 &ia_load_ctx);
235
236 if (input_attachment_read_out)
237 *input_attachment_read_out = ia_load_ctx.input_attachment_read;
238
239 /* Lower the remaining input attachment loads. */
240 struct nir_input_attachment_options lower_input_attach_opts = { };
241 NIR_PASS(progress, nir, nir_lower_input_attachments,
242 &lower_input_attach_opts);
243
244 return progress;
245 }