Debian Codegraph @

src/panfrost/vulkan/panvk_vX_cmd_frame_shaders.c

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "panvk_cmd_frame_shaders.h"
7
8 #include "panvk_cmd_alloc.h"
9 #include "panvk_image_view.h"
10 #include "panvk_meta.h"
11 #include "panvk_shader.h"
12
13 #include "nir_builder.h"
14
15 #include "pan_afbc.h"
16 #include "pan_shader.h"
17 #include "pan_nir.h"
18
19 PRAGMA_DIAGNOSTIC_PUSH
20 PRAGMA_DIAGNOSTIC_ERROR(-Wpadded)
21 struct panvk_frame_shader_key {
22 enum panvk_meta_object_key_type type;
23 struct pan_fb_shader_key key;
24 };
25 PRAGMA_DIAGNOSTIC_POP
26
27 struct panvk_fb_iviews {
28 const struct pan_image_view *rt_iviews[PAN_MAX_RTS];
29 const struct pan_image_view *z, *s;
30 };
31
32 struct panvk_fb_sysvals {
33 struct pan_fb_bbox render_area_px;
34 float clear_depth;
35 uint8_t clear_stencil;
36 uint8_t _pad;
37 uint16_t layer_id;
38 } __attribute__((aligned(FAU_WORD_SIZE)));
39 static_assert(sizeof(struct panvk_fb_sysvals) % FAU_WORD_SIZE == 0,
40 "panvk_fb_sysvals is FAU_WORD-aligned.");
41
42 static uint32_t
43 key_locations_written(const struct panvk_frame_shader_key *key)
44 {
45 uint32_t locations = 0;
46 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++) {
47 if (pan_fb_shader_key_target_written(&key->key.rts[rt]))
48 locations |= BITFIELD_BIT(FRAG_RESULT_DATA0 + rt);
49 }
50
51 if (pan_fb_shader_key_target_written(&key->key.z))
52 locations |= BITFIELD_BIT(FRAG_RESULT_DEPTH);
53
54 if (pan_fb_shader_key_target_written(&key->key.s))
55 locations |= BITFIELD_BIT(FRAG_RESULT_STENCIL);
56
57 return locations;
58 }
59
60 static uint32_t
61 shader_op_to_location_bit(enum pan_fb_shader_op op)
62 {
63 if (op == PAN_FB_SHADER_COPY_Z)
64 return BITFIELD_BIT(FRAG_RESULT_DEPTH);
65 else if (op == PAN_FB_SHADER_COPY_S)
66 return BITFIELD_BIT(FRAG_RESULT_STENCIL);
67
68 if (op < PAN_FB_SHADER_COPY_RT_0 ||
69 op >= PAN_FB_SHADER_COPY_RT_0 + PAN_MAX_RTS)
70 return 0;
71
72 uint8_t rt = op - PAN_FB_SHADER_COPY_RT_0;
73 return BITFIELD_BIT(FRAG_RESULT_DATA0 + rt);
74 }
75
76 static uint32_t
77 key_target_to_locations_read(const struct pan_fb_shader_key_target *target)
78 {
79 return shader_op_to_location_bit(target->in_bounds_op) |
80 shader_op_to_location_bit(target->border_op);
81 }
82
83 static uint32_t
84 key_locations_read(const struct panvk_frame_shader_key *key)
85 {
86 uint32_t locations = key_target_to_locations_read(&key->key.z) |
87 key_target_to_locations_read(&key->key.s);
88 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++)
89 locations |= key_target_to_locations_read(&key->key.rts[rt]);
90
91 return locations;
92 }
93
94 static uint32_t
95 tex_index_for_loc(gl_frag_result loc, uint32_t locations_written)
96 {
97 /* We could compact further than locations_written but this is probably
98 * good enough.
99 */
100 assert(locations_written & BITFIELD_BIT(loc));
101 return util_bitcount(locations_written & BITFIELD_MASK(loc));
102 }
103
104 static bool
105 compact_tex_indices(nir_builder *b, nir_tex_instr *tex,
106 const struct panvk_frame_shader_key *key)
107 {
108 const uint32_t tex_idx =
109 tex_index_for_loc(tex->texture_index, key_locations_written(key));
110
111 #if PAN_ARCH < 9
112 tex->sampler_index = 0;
113 tex->texture_index = tex_idx;
114 #else
115 tex->sampler_index = pan_res_handle(0, 0);
116 tex->texture_index = pan_res_handle(0, tex_idx + 1);
117 #endif
118
119 return true;
120 }
121
122 #ifdef load_sysval
123 #undef load_sysval
124 #endif
125
126 static nir_def *
127 load_sysval(nir_builder *b, unsigned num_components,
128 unsigned bit_size, unsigned offset)
129 {
130 assert((offset * 8) % bit_size == 0);
131 ASSERTED unsigned size = (num_components * bit_size) / 8;
132 assert(offset + size <= sizeof(struct panvk_fb_sysvals));
133
134 return nir_load_push_constant(b, num_components, bit_size,
135 nir_imm_int(b, offset));
136 }
137
138 static bool
139 lower_sysval(nir_builder *b, nir_intrinsic_instr *intr,
140 const struct panvk_frame_shader_key *key)
141 {
142 b->cursor = nir_before_instr(&intr->instr);
143
144 unsigned offset;
145 nir_def *val;
146 switch (intr->intrinsic) {
147 case nir_intrinsic_load_fb_render_area_pan:
148 offset = offsetof(struct panvk_fb_sysvals, render_area_px);
149 val = load_sysval(b, 4, 16, offset);
150 break;
151 case nir_intrinsic_load_clear_value_pan:
152 switch (nir_intrinsic_io_semantics(intr).location) {
153 case FRAG_RESULT_DEPTH:
154 offset = offsetof(struct panvk_fb_sysvals, clear_depth);
155 val = load_sysval(b, 1, 32, offset);
156 break;
157 case FRAG_RESULT_STENCIL:
158 offset = offsetof(struct panvk_fb_sysvals, clear_stencil);
159 val = nir_u2u32(b, load_sysval(b, 1, 8, offset));
160 break;
161 default:
162 UNREACHABLE("Bifrost+ doesn't need to push clear colors");
163 }
164 break;
165 case nir_intrinsic_load_layer_id:
166 /* On v9+, we have real layered rendering */
167 if (PAN_ARCH >= 9)
168 return false;
169
170 offset = offsetof(struct panvk_fb_sysvals, layer_id);
171 val = nir_u2u32(b, load_sysval(b, 1, 16, offset));
172 break;
173 default:
174 return false;
175 }
176
177 nir_def_replace(&intr->def, val);
178
179 return true;
180 }
181
182 static bool
183 lower_instr(nir_builder *b, nir_instr *instr, void *cb_data)
184 {
185 switch (instr->type) {
186 case nir_instr_type_tex:
187 return compact_tex_indices(b, nir_instr_as_tex(instr), cb_data);
188 case nir_instr_type_intrinsic:
189 return lower_sysval(b, nir_instr_as_intrinsic(instr), cb_data);
190 default:
191 return false;
192 }
193 }
194
195 static VkResult
196 get_frame_shader(struct panvk_device *dev,
197 const struct panvk_frame_shader_key *key,
198 struct panvk_internal_shader **shader_out)
199 {
200 struct panvk_physical_device *phys_dev =
201 to_panvk_physical_device(dev->vk.physical);
202 struct panvk_internal_shader *shader;
203 VkShaderEXT shader_handle = (VkShaderEXT)vk_meta_lookup_object(
204 &dev->meta, VK_OBJECT_TYPE_SHADER_EXT, key, sizeof(*key));
205 if (shader_handle != VK_NULL_HANDLE)
206 goto out;
207
208 const struct nir_shader_compiler_options *nir_options =
209 pan_get_nir_shader_compiler_options(PAN_ARCH, false);
210 nir_shader *nir = GENX(pan_get_fb_shader)(&key->key, nir_options);
211
212 NIR_PASS(_, nir, nir_shader_instructions_pass, lower_instr,
213 nir_metadata_control_flow, (void *)key);
214
215 nir_shader_gather_info(nir, nir_shader_get_entrypoint(nir));
216
217 struct pan_compile_inputs inputs = {
218 .gpu_id = phys_dev->kmod.dev->props.gpu_id,
219 .gpu_variant = phys_dev->kmod.dev->props.gpu_variant,
220 .is_blit = true,
221 };
222
223 pan_preprocess_nir(nir, inputs.gpu_id);
224 pan_postprocess_nir(nir, inputs.gpu_id);
225
226 VkResult result = panvk_per_arch(create_internal_shader)(
227 dev, nir, &inputs, &shader);
228 ralloc_free(nir);
229
230 if (result != VK_SUCCESS)
231 return result;
232
233 #if PAN_ARCH >= 9
234 shader->spd = panvk_pool_alloc_desc(&dev->mempools.rw, SHADER_PROGRAM);
235 if (!panvk_priv_mem_check_alloc(shader->spd)) {
236 vk_shader_destroy(&dev->vk, &shader->vk, NULL);
237 return panvk_error(dev, VK_ERROR_OUT_OF_DEVICE_MEMORY);
238 }
239
240 panvk_priv_mem_write_desc(shader->spd, 0, SHADER_PROGRAM, cfg) {
241 cfg.stage = MALI_SHADER_STAGE_FRAGMENT;
242 cfg.fragment_coverage_bitmask_type = MALI_COVERAGE_BITMASK_TYPE_GL;
243 cfg.register_allocation =
244 pan_register_allocation(shader->info.work_reg_count);
245 cfg.binary = panvk_priv_mem_dev_addr(shader->code_mem);
246 cfg.preload.r48_r63 = shader->info.preload >> 48;
247 }
248 #endif
249
250 shader_handle = (VkShaderEXT)vk_meta_cache_object(
251 &dev->vk, &dev->meta, key, sizeof(*key), VK_OBJECT_TYPE_SHADER_EXT,
252 (uint64_t)panvk_internal_shader_to_handle(shader));
253
254 out:
255 shader = panvk_internal_shader_from_handle(shader_handle);
256 *shader_out = shader;
257 return VK_SUCCESS;
258 }
259
260 static VkResult
261 alloc_pre_post_dcds(struct panvk_cmd_buffer *cmdbuf,
262 struct pan_fb_frame_shaders *fs,
263 struct mali_draw_packed **dcds)
264 {
265 if (fs->dcd_pointer)
266 return VK_SUCCESS;
267
268 struct panvk_rendering_state *render = &cmdbuf->state.gfx.render;
269 uint32_t dcd_count = 3 * (PAN_ARCH < 9 ? render->layer_count : 1);
270
271 struct pan_ptr dcd = panvk_cmd_alloc_desc_array(cmdbuf, dcd_count, DRAW);
272 if (!dcd.gpu)
273 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
274
275 fs->dcd_pointer = dcd.gpu;
276 *dcds = dcd.cpu;
277
278 return VK_SUCCESS;
279 }
280
281 static enum mali_register_file_format
282 get_reg_fmt(enum pan_fb_shader_data_type data_type)
283 {
284 switch (data_type) {
285 case PAN_FB_SHADER_DATA_TYPE_F32:
286 return MALI_REGISTER_FILE_FORMAT_F32;
287 case PAN_FB_SHADER_DATA_TYPE_U32:
288 return MALI_REGISTER_FILE_FORMAT_U32;
289 case PAN_FB_SHADER_DATA_TYPE_I32:
290 return MALI_REGISTER_FILE_FORMAT_I32;
291 default:
292 assert(!"Invalid reg type");
293 return MALI_REGISTER_FILE_FORMAT_F32;
294 }
295 }
296
297 static struct panvk_image_view *
298 get_color_attachment_view(struct panvk_cmd_buffer *cmdbuf, uint32_t i)
299 {
300 return cmdbuf->state.gfx.render.color_attachments.preload_iviews[i] != NULL
301 ? cmdbuf->state.gfx.render.color_attachments.preload_iviews[i]
302 : cmdbuf->state.gfx.render.color_attachments.iviews[i];
303 }
304
305 static struct panvk_image_view *
306 get_z_attachment_view(struct panvk_cmd_buffer *cmdbuf)
307 {
308 return cmdbuf->state.gfx.render.z_attachment.preload_iview
309 ? cmdbuf->state.gfx.render.z_attachment.preload_iview
310 : cmdbuf->state.gfx.render.z_attachment.iview;
311 }
312
313 static struct panvk_image_view *
314 get_s_attachment_view(struct panvk_cmd_buffer *cmdbuf)
315 {
316 return cmdbuf->state.gfx.render.s_attachment.preload_iview
317 ? cmdbuf->state.gfx.render.s_attachment.preload_iview
318 : cmdbuf->state.gfx.render.s_attachment.iview;
319 }
320
321 static const struct pan_image_view *
322 get_iview(const struct panvk_fb_iviews *iviews, gl_frag_result location)
323 {
324 switch (location) {
325 default: {
326 assert(location >= FRAG_RESULT_DATA0);
327 unsigned rt = location - FRAG_RESULT_DATA0;
328 assert(rt < PAN_MAX_RTS);
329 return iviews->rt_iviews[rt];
330 }
331
332 case FRAG_RESULT_DEPTH:
333 return iviews->z;
334
335 case FRAG_RESULT_STENCIL:
336 return iviews->s;
337 }
338 }
339
340 static void
341 fill_textures(struct panvk_cmd_buffer *cmdbuf,
342 const struct panvk_fb_iviews *iviews,
343 const struct panvk_frame_shader_key *key,
344 struct mali_texture_packed *textures)
345 {
346 uint32_t locations = key_locations_written(key);
347 uint32_t idx = 0;
348
349 u_foreach_bit(loc, locations) {
350 assert(idx == tex_index_for_loc(loc, locations));
351 const struct pan_image_view *iview = get_iview(iviews, loc);
352 if (iview == NULL) {
353 textures[idx++] = (struct mali_texture_packed){0};
354 continue;
355 }
356
357 struct pan_image_view pview = *iview;
358 if (loc == FRAG_RESULT_DEPTH)
359 pview.format = util_format_get_depth_only(pview.format);
360 else if (loc == FRAG_RESULT_STENCIL)
361 pview.format = util_format_stencil_only(pview.format);
362 #if PAN_ARCH == 7
363 else if (pan_afbc_supports_format(PAN_ARCH, pview.format))
364 GENX(pan_texture_afbc_reswizzle)(&pview);
365 #endif
366
367 const uint32_t tex_payload_size =
368 GENX(pan_texture_estimate_payload_size)(&pview);
369
370 #if PAN_ARCH <= 7
371 const uint32_t tex_payload_align = pan_alignment(SURFACE_WITH_STRIDE);
372 #else
373 const uint32_t tex_payload_align = pan_alignment(NULL_PLANE);
374 #endif
375
376 struct pan_ptr payload = panvk_cmd_alloc_dev_mem(
377 cmdbuf, desc, tex_payload_size, tex_payload_align);
378
379 GENX(pan_sampled_texture_emit)(&pview, &textures[idx++], &payload);
380 }
381
382 assert(idx == util_bitcount(locations));
383 }
384
385 static void
386 fill_bds(const struct pan_fb_layout *fb,
387 const struct panvk_frame_shader_key *key,
388 struct mali_blend_packed *bds)
389 {
390 for (unsigned i = 0; i < fb->rt_count; i++) {
391 pan_pack(&bds[i], BLEND, cfg) {
392 if (!pan_fb_shader_key_target_written(&key->key.rts[i]) ||
393 fb->rt_formats[i] == PIPE_FORMAT_NONE) {
394 cfg.enable = false;
395 cfg.internal.mode = MALI_BLEND_MODE_OFF;
396 continue;
397 }
398
399 cfg.round_to_fb_precision = true;
400 cfg.srgb = util_format_is_srgb(fb->rt_formats[i]);
401 cfg.internal.mode = MALI_BLEND_MODE_OPAQUE;
402 cfg.equation.rgb.a = MALI_BLEND_OPERAND_A_SRC;
403 cfg.equation.rgb.b = MALI_BLEND_OPERAND_B_SRC;
404 cfg.equation.rgb.c = MALI_BLEND_OPERAND_C_ZERO;
405 cfg.equation.alpha.a = MALI_BLEND_OPERAND_A_SRC;
406 cfg.equation.alpha.b = MALI_BLEND_OPERAND_B_SRC;
407 cfg.equation.alpha.c = MALI_BLEND_OPERAND_C_ZERO;
408 cfg.equation.color_mask = 0xf;
409
410 cfg.internal.fixed_function.num_comps = 4;
411 cfg.internal.fixed_function.conversion.memory_format =
412 GENX(pan_dithered_format_from_pipe_format)(fb->rt_formats[i], false);
413 cfg.internal.fixed_function.rt = i;
414 #if PAN_ARCH < 9
415 cfg.internal.fixed_function.conversion.register_format =
416 get_reg_fmt(key->key.rts[i].data_type);
417 #endif
418 }
419 }
420 }
421
422 #if PAN_ARCH < 9
423 static VkResult
424 cmd_emit_dcd(struct panvk_cmd_buffer *cmdbuf,
425 const struct pan_fb_layout *fb,
426 const struct pan_fb_load *load,
427 const struct panvk_fb_iviews *iviews,
428 const struct panvk_frame_shader_key *key,
429 struct pan_fb_frame_shaders *fs,
430 unsigned dcd_idx,
431 struct mali_draw_packed **dcds)
432 {
433 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
434 struct panvk_internal_shader *shader = NULL;
435
436 VkResult result = get_frame_shader(dev, key, &shader);
437 if (result != VK_SUCCESS)
438 return result;
439
440 uint32_t locations_written = key_locations_written(key);
441 bool preload_z = locations_written & BITFIELD_BIT(FRAG_RESULT_DEPTH);
442 bool preload_s = locations_written & BITFIELD_BIT(FRAG_RESULT_STENCIL);
443
444 uint32_t tex_count = util_bitcount(locations_written);
445 uint32_t bd_count = fb->rt_count;
446
447 struct pan_ptr rsd = panvk_cmd_alloc_desc_aggregate(
448 cmdbuf, PAN_DESC(RENDERER_STATE),
449 PAN_DESC_ARRAY(bd_count, BLEND));
450 if (!rsd.cpu)
451 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
452
453 pan_cast_and_pack(rsd.cpu, RENDERER_STATE, cfg) {
454 pan_shader_prepare_rsd(&shader->info,
455 panvk_priv_mem_dev_addr(shader->code_mem), &cfg);
456
457 cfg.shader.texture_count = tex_count;
458 cfg.shader.sampler_count = 1;
459
460 cfg.multisample_misc.sample_mask = 0xFFFF;
461 cfg.multisample_misc.multisample_enable = fb->sample_count > 1;
462 cfg.multisample_misc.evaluate_per_sample = shader->info.fs.sample_shading;
463
464 cfg.multisample_misc.depth_function = MALI_FUNC_ALWAYS;
465 cfg.multisample_misc.depth_write_mask = preload_z;
466
467 cfg.stencil_mask_misc.stencil_enable = preload_s;
468 cfg.stencil_mask_misc.stencil_mask_front = 0xFF;
469 cfg.stencil_mask_misc.stencil_mask_back = 0xFF;
470 cfg.stencil_front.compare_function = MALI_FUNC_ALWAYS;
471 cfg.stencil_front.stencil_fail = MALI_STENCIL_OP_REPLACE;
472 cfg.stencil_front.depth_fail = MALI_STENCIL_OP_REPLACE;
473 cfg.stencil_front.depth_pass = MALI_STENCIL_OP_REPLACE;
474 cfg.stencil_front.mask = 0xFF;
475
476 cfg.stencil_back = cfg.stencil_front;
477
478 if (preload_z || preload_s) {
479 /* Writing Z/S requires late updates */
480 cfg.properties.zs_update_operation = MALI_PIXEL_KILL_FORCE_LATE;
481 cfg.properties.pixel_kill_operation = MALI_PIXEL_KILL_FORCE_LATE;
482 } else {
483 /* Skipping ATEST requires forcing Z/S */
484 cfg.properties.zs_update_operation = MALI_PIXEL_KILL_FORCE_EARLY;
485 cfg.properties.pixel_kill_operation = MALI_PIXEL_KILL_FORCE_EARLY;
486 }
487
488 /* However, while shaders writing Z/S can normally be killed, on v6
489 * for frame shaders it can cause GPU timeouts, so only allow colour
490 * blit shaders to be killed. */
491 cfg.properties.allow_forward_pixel_to_kill =
492 key->type == PANVK_META_OBJECT_KEY_FB_COLOR_PRELOAD_SHADER;
493
494 if (PAN_ARCH == 6)
495 cfg.properties.allow_forward_pixel_to_be_killed =
496 key->type == PANVK_META_OBJECT_KEY_FB_COLOR_PRELOAD_SHADER;
497 }
498
499 fill_bds(fb, key, rsd.cpu + pan_size(RENDERER_STATE));
500
501 struct panvk_batch *batch = cmdbuf->cur_batch;
502
503 /* Align on 32x32 tiles */
504 const struct pan_fb_bbox aligned_bbox =
505 pan_fb_bbox_align(fb->render_area_px, 32, 32);
506
507 struct pan_ptr vpd = panvk_cmd_alloc_desc(cmdbuf, VIEWPORT);
508 if (!vpd.cpu)
509 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
510
511 pan_cast_and_pack(vpd.cpu, VIEWPORT, cfg) {
512 cfg.scissor_minimum_x = aligned_bbox.min_x;
513 cfg.scissor_minimum_y = aligned_bbox.min_y;
514 cfg.scissor_maximum_x = aligned_bbox.max_x;
515 cfg.scissor_maximum_y = aligned_bbox.max_y;
516 }
517
518 struct pan_ptr sampler = panvk_cmd_alloc_desc(cmdbuf, SAMPLER);
519 if (!sampler.cpu)
520 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
521
522 pan_cast_and_pack(sampler.cpu, SAMPLER, cfg) {
523 cfg.seamless_cube_map = false;
524 cfg.normalized_coordinates = false;
525 cfg.clamp_integer_array_indices = false;
526 cfg.minify_nearest = true;
527 cfg.magnify_nearest = true;
528 }
529
530 struct pan_ptr textures =
531 panvk_cmd_alloc_desc_array(cmdbuf, tex_count, TEXTURE);
532 if (!textures.cpu)
533 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
534
535 fill_textures(cmdbuf, iviews, key, textures.cpu);
536
537 result = alloc_pre_post_dcds(cmdbuf, fs, dcds);
538 if (result != VK_SUCCESS)
539 return result;
540
541 struct mali_draw_packed dcd_base;
542
543 /* If we got a preload without any draw, we end up with a NULL TLS
544 * descriptor. Allocate a dummy one (no TLS, no WLS) to get things working. */
545 if (!batch->tls.cpu) {
546 panvk_per_arch(cmd_alloc_tls_desc)(cmdbuf, true);
547 GENX(pan_emit_tls)(&batch->tlsinfo, batch->tls.cpu);
548 }
549
550 pan_pack(&dcd_base, DRAW, cfg) {
551 cfg.thread_storage = batch->tls.gpu;
552 cfg.state = rsd.gpu;
553
554 cfg.viewport = vpd.gpu;
555
556 cfg.textures = textures.gpu;
557 cfg.samplers = sampler.gpu;
558
559 #if PAN_ARCH >= 6
560 /* Until we decide to support FB CRC, we can consider that untouched tiles
561 * should never be written back. */
562 cfg.clean_fragment_write = true;
563 #endif
564 }
565
566 uint32_t layer_count = cmdbuf->state.gfx.render.layer_count;
567 struct pan_ptr faus = panvk_cmd_alloc_dev_mem(
568 cmdbuf, desc, layer_count * sizeof(struct panvk_fb_sysvals), 16);
569 for (uint32_t l = 0; l < layer_count; l++) {
570 const struct panvk_fb_sysvals sysvals = {
571 .render_area_px = fb->render_area_px,
572 .clear_depth = load ? load->z.clear.depth : 0,
573 .clear_stencil = load? load->s.clear.stencil : 0,
574 .layer_id = l,
575 };
576 struct pan_ptr layer_faus = pan_ptr_offset(faus, sizeof(sysvals) * l);
577 memcpy(layer_faus.cpu, &sysvals, sizeof(sysvals));
578
579 struct mali_draw_packed dcd_layer;
580 pan_pack(&dcd_layer, DRAW, cfg) {
581 cfg.push_uniforms = layer_faus.gpu;
582 };
583
584 pan_merge(&dcd_layer, &dcd_base, DRAW);
585 (*dcds)[(l * 3) + dcd_idx] = dcd_layer;
586 }
587
588 return VK_SUCCESS;
589 }
590 #else
591 static VkResult
592 cmd_emit_dcd(struct panvk_cmd_buffer *cmdbuf,
593 const struct pan_fb_layout *fb,
594 const struct pan_fb_load *load,
595 const struct panvk_fb_iviews *iviews,
596 struct panvk_frame_shader_key *key,
597 struct pan_fb_frame_shaders *fs,
598 unsigned dcd_idx,
599 struct mali_draw_packed **dcds)
600 {
601 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
602 struct panvk_internal_shader *shader = NULL;
603
604 VkResult result = get_frame_shader(dev, key, &shader);
605 if (result != VK_SUCCESS)
606 return result;
607
608 uint32_t locations_written = key_locations_written(key);
609 uint32_t locations_read = key_locations_read(key);
610 bool preload_color =
611 locations_written & BITFIELD_RANGE(FRAG_RESULT_DATA0, PAN_MAX_RTS);
612 bool preload_z = locations_written & BITFIELD_BIT(FRAG_RESULT_DEPTH);
613 bool preload_s = locations_written & BITFIELD_BIT(FRAG_RESULT_STENCIL);
614
615 uint32_t bd_count = preload_color ? fb->rt_count : 0;
616 struct pan_ptr bds = panvk_cmd_alloc_desc_array(cmdbuf, bd_count, BLEND);
617 if (bd_count > 0 && !bds.cpu)
618 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
619
620 uint32_t tex_count = util_bitcount(locations_written);
621 uint32_t desc_count = tex_count + 1;
622
623 struct pan_ptr descs = panvk_cmd_alloc_dev_mem(
624 cmdbuf, desc, desc_count * PANVK_DESCRIPTOR_SIZE, PANVK_DESCRIPTOR_SIZE);
625 if (!descs.cpu)
626 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
627
628 struct mali_sampler_packed *sampler = descs.cpu;
629
630 pan_pack(sampler, SAMPLER, cfg) {
631 cfg.seamless_cube_map = false;
632 cfg.normalized_coordinates = false;
633 cfg.clamp_integer_array_indices = false;
634 cfg.minify_nearest = true;
635 cfg.magnify_nearest = true;
636 }
637
638 fill_textures(cmdbuf, iviews, key, descs.cpu + PANVK_DESCRIPTOR_SIZE);
639
640 uint32_t rt_written =
641 (locations_written >> FRAG_RESULT_DATA0) & BITFIELD_MASK(PAN_MAX_RTS);
642 uint32_t rt_read =
643 (locations_read >> FRAG_RESULT_DATA0) & BITFIELD_MASK(PAN_MAX_RTS);
644
645 if (preload_color)
646 fill_bds(fb, key, bds.cpu);
647
648 /* Resource table sizes need to be multiples of 4. We use only one
649 * element here though.
650 */
651 const uint32_t res_table_size = MALI_RESOURCE_TABLE_SIZE_ALIGNMENT;
652 struct pan_ptr res_table =
653 panvk_cmd_alloc_desc_array(cmdbuf, res_table_size, RESOURCE);
654 if (!res_table.cpu)
655 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
656 memset(res_table.cpu, 0, pan_size(RESOURCE) * res_table_size);
657
658 pan_cast_and_pack(res_table.cpu, RESOURCE, cfg) {
659 cfg.address = descs.gpu;
660 cfg.size = desc_count * PANVK_DESCRIPTOR_SIZE;
661 }
662
663 struct pan_ptr zsd = panvk_cmd_alloc_desc(cmdbuf, DEPTH_STENCIL);
664 if (!zsd.cpu)
665 return VK_ERROR_OUT_OF_DEVICE_MEMORY;
666
667 pan_cast_and_pack(zsd.cpu, DEPTH_STENCIL, cfg) {
668 cfg.depth_function = MALI_FUNC_ALWAYS;
669 cfg.depth_write_enable = preload_z;
670
671 if (preload_z)
672 cfg.depth_source = MALI_DEPTH_SOURCE_SHADER;
673
674 cfg.stencil_test_enable = preload_s;
675 cfg.stencil_from_shader = preload_s;
676
677 cfg.front_compare_function = MALI_FUNC_ALWAYS;
678 cfg.front_stencil_fail = MALI_STENCIL_OP_REPLACE;
679 cfg.front_depth_fail = MALI_STENCIL_OP_REPLACE;
680 cfg.front_depth_pass = MALI_STENCIL_OP_REPLACE;
681 cfg.front_write_mask = 0xFF;
682 cfg.front_value_mask = 0xFF;
683
684 cfg.back_compare_function = MALI_FUNC_ALWAYS;
685 cfg.back_stencil_fail = MALI_STENCIL_OP_REPLACE;
686 cfg.back_depth_fail = MALI_STENCIL_OP_REPLACE;
687 cfg.back_depth_pass = MALI_STENCIL_OP_REPLACE;
688 cfg.back_write_mask = 0xFF;
689 cfg.back_value_mask = 0xFF;
690
691 cfg.depth_cull_enable = false;
692 }
693
694 result = alloc_pre_post_dcds(cmdbuf, fs, dcds);
695 if (result != VK_SUCCESS)
696 return result;
697
698 struct pan_ptr faus = panvk_cmd_alloc_dev_mem(
699 cmdbuf, desc, sizeof(struct panvk_fb_sysvals), 16);
700 const struct panvk_fb_sysvals sysvals = {
701 .render_area_px = fb->render_area_px,
702 .clear_depth = load ? load->z.clear.depth : 0,
703 .clear_stencil = load ? load->s.clear.stencil : 0,
704 };
705 memcpy(faus.cpu, &sysvals, sizeof(sysvals));
706
707 pan_pack(&(*dcds)[dcd_idx], DRAW, cfg) {
708 if (preload_z || preload_s) {
709 /* ZS_EMIT requires late update/kill */
710 cfg.flags_0.zs_update_operation = MALI_PIXEL_KILL_FORCE_LATE;
711 cfg.flags_0.pixel_kill_operation = MALI_PIXEL_KILL_FORCE_LATE;
712 } else {
713 /* Skipping ATEST requires forcing Z/S */
714 cfg.flags_0.zs_update_operation = MALI_PIXEL_KILL_FORCE_EARLY;
715 cfg.flags_0.pixel_kill_operation = MALI_PIXEL_KILL_FORCE_EARLY;
716 }
717
718 if (preload_color) {
719 cfg.blend = bds.gpu;
720 cfg.blend_count = bd_count;
721 cfg.flags_1.render_target_mask =
722 cmdbuf->state.gfx.render.bound_attachments &
723 MESA_VK_RP_ATTACHMENT_ANY_COLOR_BITS;
724 } else {
725 cfg.blend_count = 0;
726 }
727
728 /* Only allow color preload shaders to be killed since post-frame
729 * shaders are likely to read from the framebuffer.
730 */
731 cfg.flags_0.allow_forward_pixel_to_kill =
732 key->type == PANVK_META_OBJECT_KEY_FB_COLOR_PRELOAD_SHADER;
733 cfg.flags_0.allow_forward_pixel_to_be_killed = true;
734 cfg.depth_stencil = zsd.gpu;
735 cfg.flags_1.sample_mask = 0xFFFF;
736 cfg.flags_0.multisample_enable = fb->sample_count > 1;
737 cfg.flags_0.evaluate_per_sample = shader->info.fs.sample_shading;
738 cfg.flags_0.clean_fragment_write = true;
739
740 #if PAN_ARCH >= 12
741 cfg.fragment_resources = res_table.gpu | res_table_size;
742 cfg.fragment_shader = panvk_priv_mem_dev_addr(shader->spd);
743 cfg.fragment_fau.pointer = faus.gpu;
744 cfg.fragment_fau.count = sizeof(struct panvk_fb_sysvals) / FAU_WORD_SIZE;
745 cfg.thread_storage = cmdbuf->state.gfx.tsd;
746 #else
747 cfg.maximum_z = 1.0;
748 cfg.shader.resources = res_table.gpu | res_table_size;
749 cfg.shader.shader = panvk_priv_mem_dev_addr(shader->spd);
750 cfg.shader.thread_storage = cmdbuf->state.gfx.tsd;
751 cfg.shader.fau_count = sizeof(struct panvk_fb_sysvals) / FAU_WORD_SIZE;
752 cfg.shader.fau = faus.gpu;
753 #endif
754 cfg.flags_2.write_mask = rt_written;
755 cfg.flags_2.read_mask = rt_read;
756 #if PAN_ARCH >= 11
757 cfg.flags_2.no_shader_depth_read =
758 !(locations_read & BITFIELD_BIT(FRAG_RESULT_DEPTH));
759 cfg.flags_2.no_shader_stencil_read =
760 !(locations_read & BITFIELD_BIT(FRAG_RESULT_STENCIL));
761 #endif
762 }
763
764 return VK_SUCCESS;
765 }
766 #endif
767
768 static bool
769 always_load(const struct pan_fb_load *load,
770 const struct panvk_frame_shader_key *key)
771 {
772 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++) {
773 if (pan_fb_shader_key_target_written(&key->key.rts[rt]) &&
774 load->rts[rt].always)
775 return true;
776 }
777
778 return (pan_fb_shader_key_target_written(&key->key.z) && load->z.always) ||
779 (pan_fb_shader_key_target_written(&key->key.s) && load->s.always);
780 }
781
782 static VkResult
783 cmd_preload_zs_attachments(struct panvk_cmd_buffer *cmdbuf,
784 const struct pan_fb_layout *fb,
785 const struct pan_fb_load *load,
786 struct pan_fb_frame_shaders *fs,
787 struct mali_draw_packed **dcds)
788 {
789 struct panvk_frame_shader_key key = {
790 .type = PANVK_META_OBJECT_KEY_FB_ZS_PRELOAD_SHADER,
791 };
792 if (!GENX(pan_fb_load_shader_key_fill)(&key.key, fb, load, true))
793 return VK_SUCCESS;
794
795 const struct panvk_fb_iviews iviews = {
796 .z = load->z.iview,
797 .s = load->s.iview,
798 };
799
800 const uint32_t dcd_idx = 1;
801 VkResult result = cmd_emit_dcd(cmdbuf, fb, load, &iviews, &key,
802 fs, dcd_idx, dcds);
803 if (result != VK_SUCCESS)
804 return result;
805
806 /* We could use INTERSECT on Valhall too, but EARLY_ZS_ALWAYS has the
807 * advantage of reloading the ZS tile buffer one or more tiles ahead,
808 * making ZS data immediately available for any ZS tests taking place in
809 * other shaders. Thing's haven't been benchmarked to determine what's
810 * preferable (saving bandwidth vs having ZS preloaded earlier), so let's
811 * leave it like that for now.
812 *
813 * On v13+, we don't have EARLY_ZS_ALWAYS instead we use PREPASS_ALWAYS.
814 */
815 #if PAN_ARCH >= 13
816 fs->modes[dcd_idx] = MALI_PRE_POST_FRAME_SHADER_MODE_PREPASS_ALWAYS;
817 #elif PAN_ARCH >= 9
818 fs->modes[dcd_idx] = MALI_PRE_POST_FRAME_SHADER_MODE_EARLY_ZS_ALWAYS;
819 #else
820 /* We could use INTERSECT on Bifrost v7 too, but EARLY_ZS_ALWAYS has the
821 * advantage of reloading the ZS tile buffer one or more tiles ahead,
822 * making ZS data immediately available for any ZS tests taking place in
823 * other shaders. Thing's haven't been benchmarked to determine what's
824 * preferable (saving bandwidth vs having ZS preloaded earlier), so let's
825 * leave it like that for now. HOWEVER, EARLY_ZS_ALWAYS doesn't exist on
826 * 7.0, only on 7.2 and later, so check for that!
827 */
828 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
829 struct panvk_physical_device *pdev =
830 to_panvk_physical_device(dev->vk.physical);
831 unsigned arch_major = PAN_ARCH_MAJOR(pdev->kmod.dev->props.gpu_id);
832 unsigned arch_minor = PAN_ARCH_MINOR(pdev->kmod.dev->props.gpu_id);
833
834 if (arch_major > 7 || (arch_major == 7 && arch_minor >= 2))
835 fs->modes[dcd_idx] = MALI_PRE_POST_FRAME_SHADER_MODE_EARLY_ZS_ALWAYS;
836 else if (always_load(load, &key))
837 fs->modes[dcd_idx] = MALI_PRE_POST_FRAME_SHADER_MODE_ALWAYS;
838 else
839 fs->modes[dcd_idx] = MALI_PRE_POST_FRAME_SHADER_MODE_INTERSECT;
840 #endif
841
842 return VK_SUCCESS;
843 }
844
845 static VkResult
846 cmd_preload_color_attachments(struct panvk_cmd_buffer *cmdbuf,
847 const struct pan_fb_layout *fb,
848 const struct pan_fb_load *load,
849 struct pan_fb_frame_shaders *fs,
850 struct mali_draw_packed **dcds)
851 {
852 struct panvk_frame_shader_key key = {
853 .type = PANVK_META_OBJECT_KEY_FB_COLOR_PRELOAD_SHADER,
854 };
855 if (!GENX(pan_fb_load_shader_key_fill)(&key.key, fb, load, false))
856 return VK_SUCCESS;
857
858 struct panvk_fb_iviews iviews = { };
859 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++)
860 iviews.rt_iviews[rt] = load->rts[rt].iview;
861
862 const uint32_t dcd_idx = 0;
863 VkResult result = cmd_emit_dcd(cmdbuf, fb, load, &iviews, &key,
864 fs, dcd_idx, dcds);
865 if (result != VK_SUCCESS)
866 return result;
867
868 fs->modes[dcd_idx] = always_load(load, &key)
869 ? MALI_PRE_POST_FRAME_SHADER_MODE_ALWAYS
870 : MALI_PRE_POST_FRAME_SHADER_MODE_INTERSECT;
871
872 return VK_SUCCESS;
873 }
874
875 static VkResult
876 cmd_resolve_attachments(struct panvk_cmd_buffer *cmdbuf,
877 const struct pan_fb_layout *fb,
878 const struct pan_fb_resolve *resolve,
879 struct pan_fb_frame_shaders *fs,
880 struct mali_draw_packed **dcds)
881 {
882 if (resolve == NULL)
883 return VK_SUCCESS;
884
885 struct panvk_frame_shader_key key = {
886 .type = PANVK_META_OBJECT_KEY_FB_RESOLVE_SHADER,
887 };
888 if (!GENX(pan_fb_resolve_shader_key_fill)(&key.key, fb, resolve))
889 return VK_SUCCESS;
890
891 struct panvk_fb_iviews iviews = {
892 .z = resolve->z.iview,
893 .s = resolve->s.iview,
894 };
895 for (unsigned rt = 0; rt < PAN_MAX_RTS; rt++)
896 iviews.rt_iviews[rt] = resolve->rts[rt].iview;
897
898 const uint32_t dcd_idx = 2;
899 VkResult result = cmd_emit_dcd(cmdbuf, fb, NULL, &iviews, &key,
900 fs, dcd_idx, dcds);
901 if (result != VK_SUCCESS)
902 return result;
903
904 fs->modes[dcd_idx] = MALI_PRE_POST_FRAME_SHADER_MODE_ALWAYS;
905
906 return VK_SUCCESS;
907 }
908
909 VkResult
910 panvk_per_arch(cmd_get_frame_shaders)(struct panvk_cmd_buffer *cmdbuf,
911 const struct pan_fb_layout *fb,
912 const struct pan_fb_load *load,
913 const struct pan_fb_resolve *resolve,
914 struct pan_fb_frame_shaders *fs_out)
915 {
916 *fs_out = (struct pan_fb_frame_shaders) { .dcd_pointer = 0 };
917 struct mali_draw_packed *dcds = NULL;
918
919 VkResult result = cmd_preload_color_attachments(cmdbuf, fb, load,
920 fs_out, &dcds);
921 if (result != VK_SUCCESS)
922 return result;
923
924 result = cmd_preload_zs_attachments(cmdbuf, fb, load, fs_out, &dcds);
925 if (result != VK_SUCCESS)
926 return result;
927
928 result = cmd_resolve_attachments(cmdbuf, fb, resolve, fs_out, &dcds);
929 if (result != VK_SUCCESS)
930 return result;
931
932 return VK_SUCCESS;
933 }