Debian Codegraph @

src/imagination/vulkan/pvr_arch_cmd_buffer.c

raw
1 /*
2 * Copyright © 2022 Imagination Technologies Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * 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 THE
18 * 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 "pvr_cmd_buffer.h"
25
26 #include <assert.h>
27 #include <limits.h>
28 #include <stdbool.h>
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <vulkan/vulkan.h>
33
34 #include "hwdef/rogue_hw_defs.h"
35 #include "hwdef/rogue_hw_utils.h"
36 #include "pvr_blit.h"
37 #include "pvr_bo.h"
38 #include "pvr_buffer.h"
39 #include "pvr_clear.h"
40 #include "pvr_common.h"
41 #include "pvr_csb.h"
42 #include "pvr_csb_enum_helpers.h"
43 #include "pvr_descriptor_set.h"
44 #include "pvr_device.h"
45 #include "pvr_device_info.h"
46 #include "pvr_entrypoints.h"
47 #include "pvr_formats.h"
48 #include "pvr_framebuffer.h"
49 #include "pvr_hw_pass.h"
50 #include "pvr_image.h"
51 #include "pvr_job_common.h"
52 #include "pvr_job_render.h"
53 #include "pvr_limits.h"
54 #include "pvr_macros.h"
55 #include "pvr_pass.h"
56 #include "pvr_pds.h"
57 #include "pvr_physical_device.h"
58 #include "pvr_pipeline.h"
59 #include "pvr_query.h"
60 #include "pvr_rt_dataset.h"
61 #include "pvr_tex_state.h"
62 #include "pvr_types.h"
63 #include "pvr_usc.h"
64 #include "pvr_winsys.h"
65 #include "util/bitscan.h"
66 #include "util/bitset.h"
67 #include "util/compiler.h"
68 #include "util/list.h"
69 #include "util/macros.h"
70 #include "util/u_dynarray.h"
71 #include "util/u_math.h"
72 #include "util/u_pack_color.h"
73 #include "vk_alloc.h"
74 #include "vk_command_buffer.h"
75 #include "vk_command_pool.h"
76 #include "vk_common_entrypoints.h"
77 #include "vk_format.h"
78 #include "vk_graphics_state.h"
79 #include "vk_log.h"
80 #include "vk_object.h"
81 #include "vk_pipeline_layout.h"
82 #include "vk_synchronization.h"
83 #include "vk_util.h"
84
85 /* Structure used to pass data into pvr_compute_generate_control_stream()
86 * function.
87 */
88 struct pvr_compute_kernel_info {
89 pvr_dev_addr_t indirect_buffer_addr;
90 bool global_offsets_present;
91 uint32_t usc_common_size;
92 uint32_t usc_unified_size;
93 uint32_t pds_temp_size;
94 uint32_t pds_data_size;
95 enum ROGUE_CDMCTRL_USC_TARGET usc_target;
96 bool is_fence;
97 uint32_t pds_data_offset;
98 uint32_t pds_code_offset;
99 enum ROGUE_CDMCTRL_SD_TYPE sd_type;
100 bool usc_common_shared;
101 uint32_t global_size[PVR_WORKGROUP_DIMENSIONS];
102 uint32_t local_size[PVR_WORKGROUP_DIMENSIONS];
103 uint32_t max_instances;
104 };
105
106 static void
107 pvr_dynamic_render_info_destroy(const struct pvr_device *device,
108 struct pvr_cmd_buffer *cmd_buffer,
109 struct pvr_dynamic_render_info *dr_info);
110
111 static void pvr_cmd_buffer_clear_values_free(struct pvr_cmd_buffer *cmd_buffer);
112
113 static void pvr_cmd_buffer_attachments_free(struct pvr_cmd_buffer *cmd_buffer);
114
115 struct pvr_renderpass_hwsetup_render *pvr_arch_pass_info_get_hw_render(
116 const struct pvr_render_pass_info *render_pass_info,
117 uint32_t idx)
118 {
119 if (render_pass_info->dr_info)
120 return &render_pass_info->dr_info->hw_render;
121
122 return &render_pass_info->pass->hw_setup->renders[idx];
123 }
124
125 static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer,
126 struct pvr_sub_cmd *sub_cmd)
127 {
128 bool secondary_cont;
129
130 if (sub_cmd->owned) {
131 switch (sub_cmd->type) {
132 case PVR_SUB_CMD_TYPE_GRAPHICS:
133 secondary_cont = cmd_buffer->vk.level ==
134 VK_COMMAND_BUFFER_LEVEL_SECONDARY &&
135 (cmd_buffer->usage_flags &
136 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT);
137
138 util_dynarray_fini(&sub_cmd->gfx.sec_query_indices);
139 util_dynarray_fini(&sub_cmd->gfx.unbound_deferred_clears);
140 pvr_csb_finish(&sub_cmd->gfx.control_stream);
141 pvr_bo_free(cmd_buffer->device, sub_cmd->gfx.terminate_ctrl_stream);
142 pvr_bo_free(cmd_buffer->device, sub_cmd->gfx.multiview_ctrl_stream);
143 pvr_bo_suballoc_free(sub_cmd->gfx.depth_bias_bo);
144 pvr_bo_suballoc_free(sub_cmd->gfx.scissor_bo);
145 if (sub_cmd->is_dynamic_render && !secondary_cont) {
146 pvr_rstate_entry_remove(cmd_buffer->device, sub_cmd->gfx.rstate);
147 pvr_dynamic_render_info_destroy(cmd_buffer->device,
148 cmd_buffer,
149 sub_cmd->gfx.dr_info);
150 }
151 break;
152
153 case PVR_SUB_CMD_TYPE_COMPUTE:
154 case PVR_SUB_CMD_TYPE_QUERY:
155 pvr_csb_finish(&sub_cmd->compute.control_stream);
156 break;
157
158 case PVR_SUB_CMD_TYPE_TRANSFER:
159 list_for_each_entry_safe (struct pvr_transfer_cmd,
160 transfer_cmd,
161 sub_cmd->transfer.transfer_cmds,
162 link) {
163 list_del(&transfer_cmd->link);
164 vk_free(&cmd_buffer->vk.pool->alloc, transfer_cmd);
165 }
166 break;
167
168 case PVR_SUB_CMD_TYPE_EVENT:
169 if (sub_cmd->event.type == PVR_EVENT_TYPE_WAIT)
170 vk_free(&cmd_buffer->vk.pool->alloc, sub_cmd->event.wait.events);
171 break;
172
173 default:
174 UNREACHABLE("Unsupported sub-command type");
175 }
176 }
177
178 list_del(&sub_cmd->link);
179 vk_free(&cmd_buffer->vk.pool->alloc, sub_cmd);
180 }
181
182 static void pvr_cmd_buffer_free_sub_cmds(struct pvr_cmd_buffer *cmd_buffer)
183 {
184 list_for_each_entry_safe (struct pvr_sub_cmd,
185 sub_cmd,
186 &cmd_buffer->sub_cmds,
187 link) {
188 pvr_cmd_buffer_free_sub_cmd(cmd_buffer, sub_cmd);
189 }
190 }
191
192 static void pvr_cmd_buffer_free_resources(struct pvr_cmd_buffer *cmd_buffer)
193 {
194 pvr_cmd_buffer_attachments_free(cmd_buffer);
195 pvr_cmd_buffer_clear_values_free(cmd_buffer);
196
197 util_dynarray_fini(&cmd_buffer->state.query_indices);
198
199 pvr_cmd_buffer_free_sub_cmds(cmd_buffer);
200
201 list_for_each_entry_safe (struct pvr_suballoc_bo,
202 suballoc_bo,
203 &cmd_buffer->bo_list,
204 link) {
205 list_del(&suballoc_bo->link);
206 pvr_bo_suballoc_free(suballoc_bo);
207 }
208
209 /* At the end of any graphics job, all of these get moved elsewhere */
210 assert(list_is_empty(&cmd_buffer->deferred_clears));
211
212 util_dynarray_fini(&cmd_buffer->deferred_csb_commands);
213 util_dynarray_fini(&cmd_buffer->scissor_array);
214 util_dynarray_fini(&cmd_buffer->depth_bias_array);
215 }
216
217 static void pvr_cmd_buffer_reset(struct vk_command_buffer *vk_cmd_buffer,
218 VkCommandBufferResetFlags flags)
219 {
220 struct pvr_cmd_buffer *cmd_buffer =
221 container_of(vk_cmd_buffer, struct pvr_cmd_buffer, vk);
222
223 /* FIXME: For now we always free all resources as if
224 * VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT was set.
225 */
226 pvr_cmd_buffer_free_resources(cmd_buffer);
227
228 vk_command_buffer_reset(&cmd_buffer->vk);
229
230 memset(&cmd_buffer->state, 0, sizeof(cmd_buffer->state));
231 memset(&cmd_buffer->scissor_words, 0, sizeof(cmd_buffer->scissor_words));
232
233 cmd_buffer->usage_flags = 0;
234 }
235
236 static void pvr_cmd_buffer_destroy(struct vk_command_buffer *vk_cmd_buffer)
237 {
238 struct pvr_cmd_buffer *cmd_buffer =
239 container_of(vk_cmd_buffer, struct pvr_cmd_buffer, vk);
240
241 pvr_cmd_buffer_free_resources(cmd_buffer);
242 vk_command_buffer_finish(&cmd_buffer->vk);
243 vk_free(&cmd_buffer->vk.pool->alloc, cmd_buffer);
244 }
245
246 static const struct vk_command_buffer_ops cmd_buffer_ops = {
247 .reset = pvr_cmd_buffer_reset,
248 .destroy = pvr_cmd_buffer_destroy,
249 };
250
251 static VkResult pvr_cmd_buffer_create(struct pvr_device *device,
252 struct vk_command_pool *pool,
253 VkCommandBufferLevel level,
254 VkCommandBuffer *pCommandBuffer)
255 {
256 struct pvr_cmd_buffer *cmd_buffer;
257 VkResult result;
258
259 cmd_buffer = vk_zalloc(&pool->alloc,
260 sizeof(*cmd_buffer),
261 8U,
262 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
263 if (!cmd_buffer)
264 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
265
266 result =
267 vk_command_buffer_init(pool, &cmd_buffer->vk, &cmd_buffer_ops, level);
268 if (result != VK_SUCCESS) {
269 vk_free(&pool->alloc, cmd_buffer);
270 return result;
271 }
272
273 cmd_buffer->device = device;
274
275 cmd_buffer->depth_bias_array = UTIL_DYNARRAY_INIT;
276 cmd_buffer->scissor_array = UTIL_DYNARRAY_INIT;
277 cmd_buffer->deferred_csb_commands = UTIL_DYNARRAY_INIT;
278
279 list_inithead(&cmd_buffer->deferred_clears);
280 list_inithead(&cmd_buffer->sub_cmds);
281 list_inithead(&cmd_buffer->bo_list);
282
283 *pCommandBuffer = pvr_cmd_buffer_to_handle(cmd_buffer);
284
285 return VK_SUCCESS;
286 }
287
288 VkResult PVR_PER_ARCH(AllocateCommandBuffers)(
289 VkDevice _device,
290 const VkCommandBufferAllocateInfo *pAllocateInfo,
291 VkCommandBuffer *pCommandBuffers)
292 {
293 VK_FROM_HANDLE(vk_command_pool, pool, pAllocateInfo->commandPool);
294 VK_FROM_HANDLE(pvr_device, device, _device);
295 VkResult result = VK_SUCCESS;
296 uint32_t i;
297
298 for (i = 0; i < pAllocateInfo->commandBufferCount; i++) {
299 result = pvr_cmd_buffer_create(device,
300 pool,
301 pAllocateInfo->level,
302 &pCommandBuffers[i]);
303 if (result != VK_SUCCESS)
304 break;
305 }
306
307 if (result != VK_SUCCESS) {
308 while (i--) {
309 VK_FROM_HANDLE(vk_command_buffer, cmd_buffer, pCommandBuffers[i]);
310 pvr_cmd_buffer_destroy(cmd_buffer);
311 }
312
313 for (i = 0; i < pAllocateInfo->commandBufferCount; i++)
314 pCommandBuffers[i] = VK_NULL_HANDLE;
315 }
316
317 return result;
318 }
319
320 static void pvr_cmd_buffer_update_barriers(struct pvr_cmd_buffer *cmd_buffer,
321 enum pvr_sub_cmd_type type)
322 {
323 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
324 uint32_t barriers;
325
326 switch (type) {
327 case PVR_SUB_CMD_TYPE_GRAPHICS:
328 barriers = PVR_PIPELINE_STAGE_GEOM_BIT | PVR_PIPELINE_STAGE_FRAG_BIT;
329 break;
330
331 case PVR_SUB_CMD_TYPE_COMPUTE:
332 barriers = PVR_PIPELINE_STAGE_COMPUTE_BIT;
333 break;
334
335 case PVR_SUB_CMD_TYPE_QUERY:
336 case PVR_SUB_CMD_TYPE_TRANSFER:
337 /* Compute jobs are used for queries but to copy the results we
338 * have to sync with transfer jobs because vkCmdCopyQueryPoolResults() is
339 * deemed as a transfer operation by the spec.
340 */
341 barriers = PVR_PIPELINE_STAGE_TRANSFER_BIT;
342 break;
343
344 case PVR_SUB_CMD_TYPE_EVENT:
345 barriers = 0;
346 break;
347
348 default:
349 UNREACHABLE("Unsupported sub-command type");
350 }
351
352 for (uint32_t i = 0; i < ARRAY_SIZE(state->barriers_needed); i++)
353 state->barriers_needed[i] |= barriers;
354 }
355
356 static VkResult
357 pvr_cmd_buffer_upload_tables(struct pvr_device *device,
358 struct pvr_cmd_buffer *cmd_buffer,
359 struct pvr_sub_cmd_gfx *const sub_cmd)
360 {
361 const uint32_t cache_line_size =
362 pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
363 VkResult result;
364
365 assert(!sub_cmd->depth_bias_bo && !sub_cmd->scissor_bo);
366
367 if (cmd_buffer->depth_bias_array.size > 0) {
368 result =
369 pvr_gpu_upload(device,
370 device->heaps.general_heap,
371 util_dynarray_begin(&cmd_buffer->depth_bias_array),
372 cmd_buffer->depth_bias_array.size,
373 cache_line_size,
374 &sub_cmd->depth_bias_bo);
375 if (result != VK_SUCCESS)
376 return result;
377 }
378
379 if (cmd_buffer->scissor_array.size > 0) {
380 result = pvr_gpu_upload(device,
381 device->heaps.general_heap,
382 util_dynarray_begin(&cmd_buffer->scissor_array),
383 cmd_buffer->scissor_array.size,
384 cache_line_size,
385 &sub_cmd->scissor_bo);
386 if (result != VK_SUCCESS)
387 goto err_free_depth_bias_bo;
388 }
389
390 util_dynarray_clear(&cmd_buffer->depth_bias_array);
391 util_dynarray_clear(&cmd_buffer->scissor_array);
392
393 return VK_SUCCESS;
394
395 err_free_depth_bias_bo:
396 pvr_bo_suballoc_free(sub_cmd->depth_bias_bo);
397 sub_cmd->depth_bias_bo = NULL;
398
399 return result;
400 }
401
402 static VkResult
403 pvr_cmd_buffer_emit_ppp_state(const struct pvr_cmd_buffer *const cmd_buffer,
404 struct pvr_csb *const csb)
405 {
406 const struct pvr_render_state *const rstate =
407 cmd_buffer->state.render_pass_info.rstate;
408
409 assert(csb->stream_type == PVR_CMD_STREAM_TYPE_GRAPHICS ||
410 csb->stream_type == PVR_CMD_STREAM_TYPE_GRAPHICS_DEFERRED);
411
412 pvr_csb_set_relocation_mark(csb);
413
414 pvr_csb_emit (csb, VDMCTRL_PPP_STATE0, state0) {
415 state0.addrmsb = rstate->ppp_state_bo->dev_addr;
416 state0.word_count = rstate->ppp_state_size;
417 }
418
419 pvr_csb_emit (csb, VDMCTRL_PPP_STATE1, state1) {
420 state1.addrlsb = rstate->ppp_state_bo->dev_addr;
421 }
422
423 pvr_csb_clear_relocation_mark(csb);
424
425 return csb->status;
426 }
427
428 VkResult
429 pvr_arch_cmd_buffer_upload_general(struct pvr_cmd_buffer *const cmd_buffer,
430 const void *const data,
431 const size_t size,
432 struct pvr_suballoc_bo **const pvr_bo_out)
433 {
434 struct pvr_device *const device = cmd_buffer->device;
435 const uint32_t cache_line_size =
436 pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
437 struct pvr_suballoc_bo *suballoc_bo;
438 VkResult result;
439
440 result = pvr_gpu_upload(device,
441 device->heaps.general_heap,
442 data,
443 size,
444 cache_line_size,
445 &suballoc_bo);
446 if (result != VK_SUCCESS)
447 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
448
449 list_add(&suballoc_bo->link, &cmd_buffer->bo_list);
450
451 *pvr_bo_out = suballoc_bo;
452
453 return VK_SUCCESS;
454 }
455
456 static VkResult
457 pvr_cmd_buffer_upload_usc(struct pvr_cmd_buffer *const cmd_buffer,
458 const void *const code,
459 const size_t code_size,
460 uint64_t code_alignment,
461 struct pvr_suballoc_bo **const pvr_bo_out)
462 {
463 struct pvr_device *const device = cmd_buffer->device;
464 const uint32_t cache_line_size =
465 pvr_get_slc_cache_line_size(&device->pdevice->dev_info);
466 struct pvr_suballoc_bo *suballoc_bo;
467 VkResult result;
468
469 code_alignment = MAX2(code_alignment, cache_line_size);
470
471 result =
472 pvr_gpu_upload_usc(device, code, code_size, code_alignment, &suballoc_bo);
473 if (result != VK_SUCCESS)
474 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
475
476 list_add(&suballoc_bo->link, &cmd_buffer->bo_list);
477
478 *pvr_bo_out = suballoc_bo;
479
480 return VK_SUCCESS;
481 }
482
483 VkResult
484 pvr_arch_cmd_buffer_upload_pds(struct pvr_cmd_buffer *const cmd_buffer,
485 const uint32_t *data,
486 uint32_t data_size_dwords,
487 uint32_t data_alignment,
488 const uint32_t *code,
489 uint32_t code_size_dwords,
490 uint32_t code_alignment,
491 uint64_t min_alignment,
492 struct pvr_pds_upload *const pds_upload_out)
493 {
494 struct pvr_device *const device = cmd_buffer->device;
495 VkResult result;
496
497 result = pvr_gpu_upload_pds(device,
498 data,
499 data_size_dwords,
500 data_alignment,
501 code,
502 code_size_dwords,
503 code_alignment,
504 min_alignment,
505 pds_upload_out);
506 if (result != VK_SUCCESS)
507 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
508
509 list_add(&pds_upload_out->pvr_bo->link, &cmd_buffer->bo_list);
510
511 return VK_SUCCESS;
512 }
513
514 static inline VkResult
515 pvr_cmd_buffer_upload_pds_data(struct pvr_cmd_buffer *const cmd_buffer,
516 const uint32_t *data,
517 uint32_t data_size_dwords,
518 uint32_t data_alignment,
519 struct pvr_pds_upload *const pds_upload_out)
520 {
521 return pvr_arch_cmd_buffer_upload_pds(cmd_buffer,
522 data,
523 data_size_dwords,
524 data_alignment,
525 NULL,
526 0,
527 0,
528 data_alignment,
529 pds_upload_out);
530 }
531
532 /* pbe_cs_words must be an array of length emit_count with
533 * ROGUE_NUM_PBESTATE_STATE_WORDS entries
534 */
535 static VkResult pvr_sub_cmd_gfx_per_job_fragment_programs_create_and_upload(
536 struct pvr_cmd_buffer *const cmd_buffer,
537 const uint32_t emit_count,
538 const uint32_t *pbe_cs_words,
539 const unsigned *tile_buffer_ids,
540 unsigned pixel_output_width,
541 struct pvr_pds_upload *const pds_upload_out)
542 {
543 struct pvr_pds_event_program pixel_event_program = {
544 /* No data to DMA, just a DOUTU needed. */
545 .num_emit_word_pairs = 0,
546 };
547 const uint32_t staging_buffer_size =
548 PVR_DW_TO_BYTES(cmd_buffer->device->pixel_event_data_size_in_dwords);
549 const VkAllocationCallbacks *const allocator = &cmd_buffer->vk.pool->alloc;
550 struct pvr_device *const device = cmd_buffer->device;
551 const struct pvr_device_tile_buffer_state *tile_buffer_state =
552 &device->tile_buffer_state;
553 struct pvr_suballoc_bo *usc_eot_program = NULL;
554 struct pvr_eot_props props = {
555 .emit_count = emit_count,
556 .shared_words = false,
557 .state_words = pbe_cs_words,
558 };
559 uint32_t *staging_buffer;
560 uint32_t usc_temp_count;
561 pco_shader *eot;
562 VkResult result;
563
564 bool has_tile_buffers = false;
565 for (unsigned u = 0; u < emit_count; ++u) {
566 unsigned tile_buffer_id = tile_buffer_ids[u];
567 if (tile_buffer_id == ~0)
568 continue;
569
570 assert(tile_buffer_id < tile_buffer_state->buffer_count);
571 props.tile_buffer_addrs[u] =
572 tile_buffer_state->buffers[tile_buffer_id]->vma->dev_addr.addr;
573 has_tile_buffers = true;
574 }
575
576 if (has_tile_buffers) {
577 props.num_output_regs = pixel_output_width;
578 props.msaa_samples =
579 cmd_buffer->vk.dynamic_graphics_state.ms.rasterization_samples;
580
581 if (!props.msaa_samples)
582 props.msaa_samples = 1;
583 }
584
585 eot =
586 pvr_usc_eot(device->pdevice->pco_ctx, &props, &device->pdevice->dev_info);
587 usc_temp_count = pco_shader_data(eot)->common.temps;
588
589 result = pvr_cmd_buffer_upload_usc(cmd_buffer,
590 pco_shader_binary_data(eot),
591 pco_shader_binary_size(eot),
592 4,
593 &usc_eot_program);
594
595 ralloc_free(eot);
596
597 if (result != VK_SUCCESS)
598 return result;
599
600 pvr_pds_setup_doutu(&pixel_event_program.task_control,
601 usc_eot_program->dev_addr.addr,
602 usc_temp_count,
603 ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE,
604 false);
605
606 /* TODO: We could skip allocating this and generate directly into the device
607 * buffer thus removing one allocation and memcpy() per job. Would this
608 * speed up things in a noticeable way?
609 */
610 staging_buffer = vk_alloc(allocator,
611 staging_buffer_size,
612 8,
613 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
614 if (!staging_buffer) {
615 result = vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
616 goto err_free_usc_pixel_program;
617 }
618
619 /* Generate the data segment. The code segment was uploaded earlier when
620 * setting up the PDS static heap data.
621 */
622 pvr_pds_generate_pixel_event_data_segment(&pixel_event_program,
623 staging_buffer,
624 &device->pdevice->dev_info);
625
626 result = pvr_cmd_buffer_upload_pds_data(
627 cmd_buffer,
628 staging_buffer,
629 cmd_buffer->device->pixel_event_data_size_in_dwords,
630 4,
631 pds_upload_out);
632
633 vk_free(allocator, staging_buffer);
634
635 return result;
636
637 err_free_usc_pixel_program:
638 list_del(&usc_eot_program->link);
639 pvr_bo_suballoc_free(usc_eot_program);
640
641 return result;
642 }
643
644 static VkResult pvr_sub_cmd_gfx_build_terminate_ctrl_stream(
645 struct pvr_device *const device,
646 const struct pvr_cmd_buffer *const cmd_buffer,
647 struct pvr_sub_cmd_gfx *const gfx_sub_cmd)
648 {
649 struct list_head bo_list;
650 struct pvr_csb csb;
651 VkResult result;
652
653 pvr_csb_init(device, PVR_CMD_STREAM_TYPE_GRAPHICS, &csb);
654
655 result = pvr_cmd_buffer_emit_ppp_state(cmd_buffer, &csb);
656 if (result != VK_SUCCESS)
657 goto err_csb_finish;
658
659 result = pvr_arch_csb_emit_terminate(&csb);
660 if (result != VK_SUCCESS)
661 goto err_csb_finish;
662
663 result = pvr_csb_bake(&csb, &bo_list);
664 if (result != VK_SUCCESS)
665 goto err_csb_finish;
666
667 /* This is a trivial control stream, there's no reason it should ever require
668 * more memory than a single bo can provide.
669 */
670 assert(list_is_singular(&bo_list));
671 gfx_sub_cmd->terminate_ctrl_stream =
672 list_first_entry(&bo_list, struct pvr_bo, link);
673
674 return VK_SUCCESS;
675
676 err_csb_finish:
677 pvr_csb_finish(&csb);
678
679 return result;
680 }
681
682 static VkResult pvr_setup_texture_state_words(
683 struct pvr_device *device,
684 struct pvr_combined_image_sampler_descriptor *descriptor,
685 const struct pvr_image_view *image_view,
686 uint32_t view_index)
687 {
688 const struct pvr_image *image = vk_to_pvr_image(image_view->vk.image);
689 const struct pvr_image_plane *plane = pvr_single_plane_const(image);
690
691 STATIC_ASSERT(sizeof(descriptor->image) ==
692 sizeof(image_view->image_state[PVR_TEXTURE_STATE_SAMPLE]));
693 memcpy(&descriptor->image,
694 &image_view->image_state[PVR_TEXTURE_STATE_SAMPLE],
695 sizeof(descriptor->image));
696
697 const struct ROGUE_TEXSTATE_IMAGE_WORD1 image_word1 = pvr_csb_unpack(
698 &descriptor->image.words[1],
699 TEXSTATE_IMAGE_WORD1);
700
701 pvr_csb_pack (&descriptor->image.words[1],
702 TEXSTATE_IMAGE_WORD1,
703 word1) {
704 word1 = image_word1;
705 word1.texaddr =
706 PVR_DEV_ADDR_OFFSET(word1.texaddr,
707 plane->layer_size * view_index);
708 }
709
710 pvr_csb_pack (&descriptor->sampler.words[0],
711 TEXSTATE_SAMPLER_WORD0,
712 sampler) {
713 sampler.non_normalized_coords = true;
714 sampler.addrmode_v = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
715 sampler.addrmode_u = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
716 sampler.minfilter = ROGUE_TEXSTATE_FILTER_POINT;
717 sampler.magfilter = ROGUE_TEXSTATE_FILTER_POINT;
718 sampler.dadjust = ROGUE_TEXSTATE_DADJUST_ZERO_UINT;
719 }
720
721 pvr_csb_pack (&descriptor->sampler.words[1],
722 TEXSTATE_SAMPLER_WORD1,
723 sampler) {}
724
725 return VK_SUCCESS;
726 }
727
728 static VkResult
729 pvr_load_op_constants_create_and_upload(struct pvr_cmd_buffer *cmd_buffer,
730 const struct pvr_load_op *load_op,
731 uint32_t view_index,
732 pvr_dev_addr_t *const addr_out)
733 {
734 const struct pvr_render_pass_info *render_pass_info =
735 &cmd_buffer->state.render_pass_info;
736 const struct pvr_render_pass *pass = render_pass_info->pass;
737 struct pvr_suballoc_bo *clear_bo;
738 uint32_t attachment_count;
739 bool has_depth_clear;
740 bool has_depth_load;
741 VkResult result;
742
743 struct pvr_combined_image_sampler_descriptor
744 texture_states[PVR_LOAD_OP_CLEARS_LOADS_MAX_RTS];
745 uint32_t texture_count = 0;
746 uint32_t hw_clear_value[PVR_LOAD_OP_CLEARS_LOADS_MAX_RTS *
747 PVR_CLEAR_COLOR_ARRAY_SIZE];
748 uint32_t next_clear_consts = 0;
749 unsigned buffer_size;
750 uint8_t *buffer;
751
752 if (load_op->is_hw_object)
753 attachment_count = load_op->hw_render->color_init_count;
754 else
755 attachment_count = load_op->subpass->color_count;
756
757 for (uint32_t i = 0; i < attachment_count; i++) {
758 struct pvr_image_view *image_view;
759 uint32_t attachment_idx;
760 const VkClearValue *clear_value;
761
762 if (load_op->is_hw_object)
763 attachment_idx = load_op->hw_render->color_init[i].index;
764 else
765 attachment_idx = load_op->subpass->color_attachments[i];
766
767 image_view = render_pass_info->attachments[attachment_idx];
768 clear_value = &render_pass_info->clear_values[attachment_idx];
769
770 assert((load_op->clears_loads_state.rt_load_mask &
771 load_op->clears_loads_state.rt_clear_mask) == 0);
772 if (load_op->clears_loads_state.rt_load_mask & BITFIELD_BIT(i)) {
773 result = pvr_setup_texture_state_words(cmd_buffer->device,
774 &texture_states[texture_count],
775 image_view,
776 view_index);
777 if (result != VK_SUCCESS)
778 return result;
779
780 texture_count++;
781 } else if (load_op->clears_loads_state.rt_clear_mask & BITFIELD_BIT(i)) {
782 const uint32_t accum_fmt_size =
783 pvr_get_pbe_accum_format_size_in_bytes(image_view->vk.format);
784
785 assert(next_clear_consts +
786 vk_format_get_blocksize(image_view->vk.format) <=
787 ARRAY_SIZE(hw_clear_value));
788
789 /* FIXME: do this at the point we store the clear values? */
790 pvr_get_hw_clear_color(image_view->vk.format,
791 clear_value->color,
792 &hw_clear_value[next_clear_consts]);
793
794 next_clear_consts += DIV_ROUND_UP(accum_fmt_size, sizeof(uint32_t));
795 }
796 }
797
798 has_depth_load = false;
799 for (uint32_t i = 0;
800 i < ARRAY_SIZE(load_op->clears_loads_state.dest_vk_format);
801 i++) {
802 switch (load_op->clears_loads_state.dest_vk_format[i]) {
803 case VK_FORMAT_D16_UNORM:
804 case VK_FORMAT_X8_D24_UNORM_PACK32:
805 case VK_FORMAT_D32_SFLOAT:
806 case VK_FORMAT_D24_UNORM_S8_UINT:
807 case VK_FORMAT_D32_SFLOAT_S8_UINT:
808 has_depth_load = true;
809 break;
810
811 default:
812 break;
813 }
814
815 if (has_depth_load)
816 break;
817 }
818
819 has_depth_clear = load_op->clears_loads_state.depth_clear_to_reg != -1;
820
821 assert(!(has_depth_clear && has_depth_load));
822
823 if (has_depth_load) {
824 const struct pvr_render_pass_attachment *attachment;
825 const struct pvr_image_view *image_view;
826
827 assert(load_op->subpass->depth_stencil_attachment !=
828 VK_ATTACHMENT_UNUSED);
829 assert(!load_op->is_hw_object);
830 attachment =
831 &pass->attachments[load_op->subpass->depth_stencil_attachment];
832
833 image_view = render_pass_info->attachments[attachment->index];
834
835 result = pvr_setup_texture_state_words(cmd_buffer->device,
836 &texture_states[texture_count],
837 image_view,
838 view_index);
839 if (result != VK_SUCCESS)
840 return result;
841
842 texture_count++;
843 } else if (has_depth_clear) {
844 const struct pvr_render_pass_attachment *attachment;
845 VkClearValue clear_value;
846
847 assert(load_op->subpass->depth_stencil_attachment !=
848 VK_ATTACHMENT_UNUSED);
849 attachment =
850 &pass->attachments[load_op->subpass->depth_stencil_attachment];
851
852 clear_value = render_pass_info->clear_values[attachment->index];
853
854 assert(next_clear_consts < ARRAY_SIZE(hw_clear_value));
855 hw_clear_value[next_clear_consts++] = fui(clear_value.depthStencil.depth);
856 }
857
858 buffer_size = next_clear_consts * sizeof(hw_clear_value[0]);
859 if (texture_count > 0)
860 buffer_size = ALIGN_POT(buffer_size, 4 * sizeof(uint32_t));
861 unsigned words = buffer_size;
862 buffer_size +=
863 texture_count * sizeof(struct pvr_combined_image_sampler_descriptor);
864
865 unsigned tile_buffer_offset = buffer_size;
866 buffer_size += load_op->num_tile_buffers * sizeof(uint64_t);
867
868 assert(!(buffer_size % sizeof(uint32_t)));
869 assert(buffer_size / sizeof(uint32_t) == load_op->shareds_count);
870
871 result =
872 pvr_arch_cmd_buffer_alloc_mem(cmd_buffer,
873 cmd_buffer->device->heaps.general_heap,
874 buffer_size,
875 &clear_bo);
876 if (result != VK_SUCCESS)
877 return result;
878
879 buffer = (uint8_t *)pvr_bo_suballoc_get_map_addr(clear_bo);
880 memcpy(&buffer[0],
881 hw_clear_value,
882 next_clear_consts * sizeof(hw_clear_value[0]));
883
884 memcpy(&buffer[words],
885 texture_states,
886 texture_count * sizeof(struct pvr_combined_image_sampler_descriptor));
887
888 struct pvr_device *const device = cmd_buffer->device;
889 const struct pvr_device_tile_buffer_state *tile_buffer_state =
890 &device->tile_buffer_state;
891
892 uint32_t *tile_buffers = (uint32_t *)&buffer[tile_buffer_offset];
893 for (unsigned u = 0; u < load_op->num_tile_buffers; ++u) {
894 assert(u < tile_buffer_state->buffer_count);
895 uint64_t tile_buffer_addr =
896 tile_buffer_state->buffers[u]->vma->dev_addr.addr;
897
898 tile_buffers[2 * u] = tile_buffer_addr & 0xffffffff;
899 tile_buffers[2 * u + 1] = tile_buffer_addr >> 32;
900 }
901
902 *addr_out = clear_bo->dev_addr;
903
904 return VK_SUCCESS;
905 }
906
907 static VkResult pvr_load_op_pds_data_create_and_upload(
908 struct pvr_cmd_buffer *cmd_buffer,
909 const struct pvr_load_op *load_op,
910 pvr_dev_addr_t constants_addr,
911 struct pvr_pds_upload *const pds_upload_out)
912 {
913 struct pvr_device *device = cmd_buffer->device;
914 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
915 struct pvr_pds_pixel_shader_sa_program program = { 0 };
916 uint32_t staging_buffer_size;
917 uint32_t *staging_buffer;
918 VkResult result;
919
920 program.num_texture_dma_kicks = 1;
921
922 pvr_csb_pack (&program.texture_dma_address[0],
923 PDSINST_DOUT_FIELDS_DOUTD_SRC0,
924 value) {
925 value.sbase = constants_addr;
926 }
927
928 pvr_csb_pack (&program.texture_dma_control[0],
929 PDSINST_DOUT_FIELDS_DOUTD_SRC1,
930 value) {
931 value.dest = ROGUE_PDSINST_DOUTD_DEST_COMMON_STORE;
932 value.bsize = load_op->shareds_count;
933 }
934
935 pvr_pds_set_sizes_pixel_shader_sa_texture_data(&program, dev_info);
936
937 staging_buffer_size = PVR_DW_TO_BYTES(program.data_size);
938
939 staging_buffer = vk_alloc(&cmd_buffer->vk.pool->alloc,
940 staging_buffer_size,
941 8,
942 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
943 if (!staging_buffer)
944 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
945
946 pvr_pds_generate_pixel_shader_sa_texture_state_data(&program,
947 staging_buffer,
948 dev_info);
949
950 result = pvr_cmd_buffer_upload_pds_data(cmd_buffer,
951 staging_buffer,
952 program.data_size,
953 1,
954 pds_upload_out);
955 if (result != VK_SUCCESS) {
956 vk_free(&cmd_buffer->vk.pool->alloc, staging_buffer);
957 return result;
958 }
959
960 vk_free(&cmd_buffer->vk.pool->alloc, staging_buffer);
961
962 return VK_SUCCESS;
963 }
964
965 /* FIXME: Should this function be specific to the HW background object, in
966 * which case its name should be changed, or should it have the load op
967 * structure passed in?
968 */
969 static VkResult
970 pvr_load_op_data_create_and_upload(struct pvr_cmd_buffer *cmd_buffer,
971 const struct pvr_load_op *load_op,
972 uint32_t view_index,
973 struct pvr_pds_upload *const pds_upload_out)
974 {
975 pvr_dev_addr_t constants_addr;
976 VkResult result;
977
978 result = pvr_load_op_constants_create_and_upload(cmd_buffer,
979 load_op,
980 view_index,
981 &constants_addr);
982 if (result != VK_SUCCESS)
983 return result;
984
985 return pvr_load_op_pds_data_create_and_upload(cmd_buffer,
986 load_op,
987 constants_addr,
988 pds_upload_out);
989 }
990
991 static void pvr_pds_bgnd_pack_state(
992 const struct pvr_load_op *load_op,
993 const struct pvr_pds_upload *load_op_program,
994 uint64_t pds_reg_values[static const ROGUE_NUM_CR_PDS_BGRND_WORDS])
995 {
996 pvr_csb_pack (&pds_reg_values[0], CR_PDS_BGRND0_BASE, value) {
997 value.shader_addr = PVR_DEV_ADDR(load_op->pds_frag_prog.data_offset);
998 value.texunicode_addr =
999 PVR_DEV_ADDR(load_op->pds_tex_state_prog.code_offset);
1000 }
1001
1002 pvr_csb_pack (&pds_reg_values[1], CR_PDS_BGRND1_BASE, value) {
1003 value.texturedata_addr = PVR_DEV_ADDR(load_op_program->data_offset);
1004 }
1005
1006 pvr_csb_pack (&pds_reg_values[2], CR_PDS_BGRND3_SIZEINFO, value) {
1007 value.usc_sharedsize =
1008 DIV_ROUND_UP(load_op->const_shareds_count,
1009 ROGUE_CR_PDS_BGRND3_SIZEINFO_USC_SHAREDSIZE_UNIT_SIZE);
1010 value.pds_texturestatesize = DIV_ROUND_UP(
1011 load_op_program->data_size,
1012 ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_TEXTURESTATESIZE_UNIT_SIZE);
1013 value.pds_tempsize =
1014 DIV_ROUND_UP(load_op->temps_count,
1015 ROGUE_CR_PDS_BGRND3_SIZEINFO_PDS_TEMPSIZE_UNIT_SIZE);
1016 }
1017 }
1018
1019 static inline VkResult pvr_load_op_state_data_create_and_upload_for_view(
1020 struct pvr_cmd_buffer *cmd_buffer,
1021 const struct pvr_load_op *load_op,
1022 uint32_t view_index,
1023 uint64_t pds_reg_values[static const ROGUE_NUM_CR_PDS_BGRND_WORDS])
1024 {
1025 struct pvr_pds_upload load_op_program;
1026 VkResult result;
1027
1028 /* FIXME: Should we free the PDS pixel event data or let it be freed
1029 * when the pool gets emptied?
1030 */
1031 result = pvr_load_op_data_create_and_upload(cmd_buffer,
1032 load_op,
1033 view_index,
1034 &load_op_program);
1035 if (result != VK_SUCCESS)
1036 return result;
1037
1038 pvr_pds_bgnd_pack_state(load_op, &load_op_program, pds_reg_values);
1039
1040 return VK_SUCCESS;
1041 }
1042
1043 static VkResult pvr_load_op_state_data_create_and_upload(
1044 struct pvr_cmd_buffer *cmd_buffer,
1045 const struct pvr_load_op_state *load_op_state,
1046 struct pvr_view_state *view_state)
1047 {
1048 for (uint32_t i = 0; i < load_op_state->load_op_count; i++) {
1049 const struct pvr_load_op *load_op = &load_op_state->load_ops[i];
1050 uint32_t view_index = load_op->view_indices[0];
1051 uint64_t *pds_reg_values;
1052 VkResult result;
1053
1054 pds_reg_values = view_state->view[view_index].pds_bgnd_reg_values;
1055 result =
1056 pvr_load_op_state_data_create_and_upload_for_view(cmd_buffer,
1057 load_op,
1058 view_index,
1059 pds_reg_values);
1060 if (result != VK_SUCCESS)
1061 return result;
1062
1063 pds_reg_values = view_state->view[view_index].pr_pds_bgnd_reg_values;
1064 result =
1065 pvr_load_op_state_data_create_and_upload_for_view(cmd_buffer,
1066 load_op,
1067 view_index,
1068 pds_reg_values);
1069 if (result != VK_SUCCESS)
1070 return result;
1071 }
1072
1073 return VK_SUCCESS;
1074 }
1075
1076 /**
1077 * \brief Calculates the stride in pixels based on the pitch in bytes and pixel
1078 * format.
1079 *
1080 * \param[in] pitch Width pitch in bytes.
1081 * \param[in] vk_format Vulkan image format.
1082 * \return Stride in pixels.
1083 */
1084 static inline uint32_t pvr_stride_from_pitch(uint32_t pitch, VkFormat vk_format)
1085 {
1086 const unsigned int cpp = vk_format_get_blocksize(vk_format);
1087
1088 assert(pitch % cpp == 0);
1089
1090 return pitch / cpp;
1091 }
1092
1093 static void pvr_setup_pbe_state(
1094 const struct pvr_device_info *dev_info,
1095 const struct pvr_render_state *rstate,
1096 uint32_t mrt_index,
1097 const struct usc_mrt_resource *mrt_resource,
1098 const struct pvr_image_view *const iview,
1099 const VkRect2D *render_area,
1100 const bool down_scale,
1101 const uint32_t samples,
1102 uint32_t pbe_cs_words[static const ROGUE_NUM_PBESTATE_STATE_WORDS],
1103 uint64_t pbe_reg_words[static const ROGUE_NUM_PBESTATE_REG_WORDS],
1104 uint32_t view_index)
1105 {
1106 const struct pvr_image *image = pvr_image_view_get_image(iview);
1107 const struct pvr_image_plane *plane = pvr_single_plane_const(image);
1108 uint32_t level_pitch = plane->mip_levels[iview->vk.base_mip_level].pitch;
1109
1110 struct pvr_pbe_surf_params surface_params;
1111 struct pvr_pbe_render_params render_params;
1112 bool with_packed_usc_channel;
1113 const uint8_t *swizzle;
1114 uint32_t position;
1115
1116 /* down_scale should be true when performing a resolve, in which case there
1117 * should be more than one sample.
1118 */
1119 assert((down_scale && samples > 1U) || (!down_scale && samples == 1U));
1120
1121 /* Setup surface parameters. */
1122
1123 if (PVR_HAS_FEATURE(dev_info, usc_f16sop_u8)) {
1124 with_packed_usc_channel = vk_format_is_unorm(iview->vk.format) ||
1125 vk_format_is_snorm(iview->vk.format);
1126 } else {
1127 with_packed_usc_channel = false;
1128 }
1129
1130 swizzle = pvr_get_format_swizzle(iview->vk.format);
1131 memcpy(surface_params.swizzle, swizzle, sizeof(surface_params.swizzle));
1132
1133 pvr_arch_pbe_get_src_format_and_gamma(iview->vk.format,
1134 PVR_PBE_GAMMA_NONE,
1135 with_packed_usc_channel,
1136 &surface_params.source_format,
1137 &surface_params.gamma);
1138
1139 surface_params.is_normalized =
1140 pvr_vk_format_is_fully_normalized(iview->vk.format);
1141 surface_params.pbe_packmode = pvr_arch_get_pbe_packmode(iview->vk.format);
1142 surface_params.nr_components = vk_format_get_nr_components(iview->vk.format);
1143
1144 /* FIXME: Should we have an inline function to return the address of a mip
1145 * level?
1146 */
1147 surface_params.addr = PVR_DEV_ADDR_OFFSET(
1148 image->vma->dev_addr,
1149 plane->layer_size * view_index +
1150 plane->mip_levels[iview->vk.base_mip_level].offset);
1151
1152 if (!iview->vk.storage.z_slice_offset) {
1153 surface_params.addr =
1154 PVR_DEV_ADDR_OFFSET(surface_params.addr,
1155 iview->vk.base_array_layer * plane->layer_size);
1156 }
1157
1158 surface_params.mem_layout = image->memlayout;
1159 surface_params.stride = pvr_stride_from_pitch(level_pitch, iview->vk.format);
1160 surface_params.depth = iview->vk.extent.depth;
1161 surface_params.width = iview->vk.extent.width;
1162 surface_params.height = iview->vk.extent.height;
1163 surface_params.z_only_render = false;
1164 surface_params.down_scale = down_scale;
1165
1166 /* Setup render parameters. */
1167
1168 if (mrt_resource->type == USC_MRT_RESOURCE_TYPE_MEMORY) {
1169 position = mrt_resource->mem.offset_dw;
1170 } else {
1171 assert(mrt_resource->type == USC_MRT_RESOURCE_TYPE_OUTPUT_REG);
1172 assert(mrt_resource->reg.offset == 0);
1173
1174 position = mrt_resource->reg.output_reg;
1175 }
1176
1177 assert(position <= 3 || PVR_HAS_FEATURE(dev_info, eight_output_registers));
1178
1179 switch (position) {
1180 case 0:
1181 case 4:
1182 render_params.source_start = PVR_PBE_STARTPOS_BIT0;
1183 break;
1184 case 1:
1185 case 5:
1186 render_params.source_start = PVR_PBE_STARTPOS_BIT32;
1187 break;
1188 case 2:
1189 case 6:
1190 render_params.source_start = PVR_PBE_STARTPOS_BIT64;
1191 break;
1192 case 3:
1193 case 7:
1194 render_params.source_start = PVR_PBE_STARTPOS_BIT96;
1195 break;
1196 default:
1197 assert(!"Invalid output register");
1198 break;
1199 }
1200
1201 #define PVR_DEC_IF_NOT_ZERO(_v) (((_v) > 0) ? (_v) - 1 : 0)
1202
1203 render_params.min_x_clip = MAX2(0, render_area->offset.x);
1204 render_params.min_y_clip = MAX2(0, render_area->offset.y);
1205 render_params.max_x_clip = MIN2(
1206 rstate->width - 1,
1207 PVR_DEC_IF_NOT_ZERO(render_area->offset.x + render_area->extent.width));
1208 render_params.max_y_clip = MIN2(
1209 rstate->height - 1,
1210 PVR_DEC_IF_NOT_ZERO(render_area->offset.y + render_area->extent.height));
1211
1212 #undef PVR_DEC_IF_NOT_ZERO
1213
1214 render_params.slice = iview->vk.storage.z_slice_offset;
1215
1216 render_params.mrt_index = mrt_index;
1217
1218 pvr_arch_pbe_pack_state(dev_info,
1219 &surface_params,
1220 &render_params,
1221 pbe_cs_words,
1222 pbe_reg_words);
1223 }
1224
1225 static struct pvr_render_target *
1226 pvr_get_render_target(const struct pvr_renderpass_hwsetup_render *hw_render,
1227 const struct pvr_render_state *rstate)
1228 {
1229 uint32_t rt_idx = 0;
1230
1231 switch (hw_render->sample_count) {
1232 case 1:
1233 case 2:
1234 case 4:
1235 case 8:
1236 rt_idx = util_logbase2(hw_render->sample_count);
1237 break;
1238
1239 default:
1240 UNREACHABLE("Unsupported sample count");
1241 break;
1242 }
1243
1244 return &rstate->render_targets[rt_idx];
1245 }
1246
1247 static uint32_t pvr_get_pixel_output_width(
1248 const struct pvr_renderpass_hwsetup_render *hw_render,
1249 const struct pvr_device_info *dev_info)
1250 {
1251 /* Default value based on the maximum value found in all existing cores. The
1252 * maximum is used as this is being treated as a lower bound, making it a
1253 * "safer" choice than the minimum value found in all existing cores.
1254 */
1255 const uint32_t min_output_regs =
1256 PVR_GET_FEATURE_VALUE(dev_info, usc_min_output_registers_per_pix, 2U);
1257 const uint32_t width = MAX2(hw_render->output_regs_count, min_output_regs);
1258
1259 return util_next_power_of_two(width);
1260 }
1261
1262 static inline bool
1263 pvr_ds_attachment_requires_zls(const struct pvr_ds_attachment *attachment)
1264 {
1265 bool zls_used;
1266
1267 zls_used = attachment->load.d || attachment->load.s;
1268 zls_used |= attachment->store.d || attachment->store.s;
1269
1270 return zls_used;
1271 }
1272
1273 /**
1274 * \brief If depth and/or stencil attachment dimensions are not tile-aligned,
1275 * then we may need to insert some additional transfer subcommands.
1276 *
1277 * It's worth noting that we check whether the dimensions are smaller than a
1278 * tile here, rather than checking whether they're tile-aligned - this relies
1279 * on the assumption that we can safely use any attachment with dimensions
1280 * larger than a tile. If the attachment is twiddled, it will be over-allocated
1281 * to the nearest power-of-two (which will be tile-aligned). If the attachment
1282 * is not twiddled, we don't need to worry about tile-alignment at all.
1283 */
1284 static bool pvr_sub_cmd_gfx_requires_ds_subtile_alignment(
1285 const struct pvr_device_info *dev_info,
1286 const struct pvr_render_job *job)
1287 {
1288 const struct pvr_image *const ds_image =
1289 pvr_image_view_get_image(job->ds.iview);
1290 const struct pvr_image_plane *const ds_plane =
1291 pvr_single_plane_const(ds_image);
1292 uint32_t zls_tile_size_x;
1293 uint32_t zls_tile_size_y;
1294
1295 rogue_get_zls_tile_size_xy(dev_info, &zls_tile_size_x, &zls_tile_size_y);
1296
1297 if (ds_plane->physical_extent.width >= zls_tile_size_x &&
1298 ds_plane->physical_extent.height >= zls_tile_size_y) {
1299 return false;
1300 }
1301
1302 /* If we have the zls_subtile feature, we can skip the alignment iff:
1303 * - The attachment is not multisampled, and
1304 * - The depth and stencil attachments are the same.
1305 */
1306 if (PVR_HAS_FEATURE(dev_info, zls_subtile) &&
1307 ds_image->vk.samples == VK_SAMPLE_COUNT_1_BIT &&
1308 job->has_stencil_attachment == job->has_depth_attachment) {
1309 return false;
1310 }
1311
1312 /* No ZLS functions enabled; nothing to do. */
1313 if ((!job->has_depth_attachment && !job->has_stencil_attachment) ||
1314 !pvr_ds_attachment_requires_zls(&job->ds)) {
1315 return false;
1316 }
1317
1318 return true;
1319 }
1320
1321 static VkResult
1322 pvr_sub_cmd_gfx_align_ds_subtiles(struct pvr_cmd_buffer *const cmd_buffer,
1323 struct pvr_sub_cmd_gfx *const gfx_sub_cmd)
1324 {
1325 struct pvr_sub_cmd *const prev_sub_cmd =
1326 container_of(gfx_sub_cmd, struct pvr_sub_cmd, gfx);
1327 struct pvr_ds_attachment *const ds = &gfx_sub_cmd->job.ds;
1328 const struct pvr_image *const ds_image = pvr_image_view_get_image(ds->iview);
1329 const struct pvr_image_plane *const ds_plane =
1330 pvr_single_plane_const(ds_image);
1331 const VkFormat copy_format = pvr_get_raw_copy_format(ds_image->vk.format);
1332
1333 struct pvr_suballoc_bo *buffer;
1334 uint32_t buffer_layer_size;
1335 VkBufferImageCopy2 region;
1336 VkExtent2D zls_tile_size;
1337 VkExtent2D rounded_size;
1338 uint32_t buffer_size;
1339 VkExtent2D scale;
1340 VkResult result;
1341
1342 /* The operations below assume the last command in the buffer was the target
1343 * gfx subcommand. Assert that this is the case.
1344 */
1345 assert(list_last_entry(&cmd_buffer->sub_cmds, struct pvr_sub_cmd, link) ==
1346 prev_sub_cmd);
1347 assert(prev_sub_cmd == cmd_buffer->state.current_sub_cmd);
1348
1349 if (!pvr_ds_attachment_requires_zls(ds))
1350 return VK_SUCCESS;
1351
1352 rogue_get_zls_tile_size_xy(&cmd_buffer->device->pdevice->dev_info,
1353 &zls_tile_size.width,
1354 &zls_tile_size.height);
1355 rogue_get_isp_scale_xy_from_samples(ds_image->vk.samples,
1356 &scale.width,
1357 &scale.height);
1358
1359 rounded_size = (VkExtent2D){
1360 .width = ALIGN_POT(ds_plane->physical_extent.width, zls_tile_size.width),
1361 .height =
1362 ALIGN_POT(ds_plane->physical_extent.height, zls_tile_size.height),
1363 };
1364
1365 buffer_layer_size = vk_format_get_blocksize(ds_image->vk.format) *
1366 rounded_size.width * rounded_size.height * scale.width *
1367 scale.height;
1368
1369 if (ds->iview->vk.layer_count > 1)
1370 buffer_layer_size = ALIGN_POT(buffer_layer_size, ds_image->alignment);
1371
1372 buffer_size = buffer_layer_size * ds->iview->vk.layer_count;
1373
1374 result =
1375 pvr_arch_cmd_buffer_alloc_mem(cmd_buffer,
1376 cmd_buffer->device->heaps.general_heap,
1377 buffer_size,
1378 &buffer);
1379 if (result != VK_SUCCESS)
1380 return result;
1381
1382 region = (VkBufferImageCopy2){
1383 .sType = VK_STRUCTURE_TYPE_BUFFER_IMAGE_COPY_2,
1384 .pNext = NULL,
1385 .bufferOffset = 0,
1386 .bufferRowLength = rounded_size.width,
1387 .bufferImageHeight = 0,
1388 .imageSubresource = {
1389 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT,
1390 .mipLevel = ds->iview->vk.base_mip_level,
1391 .baseArrayLayer = ds->iview->vk.base_array_layer,
1392 .layerCount = ds->iview->vk.layer_count,
1393 },
1394 .imageOffset = { 0 },
1395 .imageExtent = {
1396 .width = ds->iview->vk.extent.width,
1397 .height = ds->iview->vk.extent.height,
1398 .depth = 1,
1399 },
1400 };
1401
1402 if (ds->load.d || ds->load.s) {
1403 struct pvr_sub_cmd *new_sub_cmd;
1404
1405 cmd_buffer->state.current_sub_cmd = NULL;
1406
1407 result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer,
1408 PVR_SUB_CMD_TYPE_TRANSFER);
1409 if (result != VK_SUCCESS)
1410 return result;
1411
1412 new_sub_cmd = cmd_buffer->state.current_sub_cmd;
1413
1414 result = pvr_copy_image_to_buffer_region_format(cmd_buffer,
1415 ds_image,
1416 buffer->dev_addr,
1417 &region,
1418 copy_format,
1419 copy_format);
1420 if (result != VK_SUCCESS)
1421 return result;
1422
1423 new_sub_cmd->transfer.serialize_with_frag = true;
1424
1425 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
1426 if (result != VK_SUCCESS)
1427 return result;
1428
1429 /* Now we have to fiddle with cmd_buffer to place this transfer command
1430 * *before* the target gfx subcommand.
1431 *
1432 * Note the doc for list_move_to() is subtly wrong - item is placed
1433 * directly *after* loc in the list, not "in front of".
1434 */
1435 list_move_to(&new_sub_cmd->link, prev_sub_cmd->link.prev);
1436
1437 cmd_buffer->state.current_sub_cmd = prev_sub_cmd;
1438 }
1439
1440 if (ds->store.d || ds->store.s) {
1441 cmd_buffer->state.current_sub_cmd = NULL;
1442
1443 result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer,
1444 PVR_SUB_CMD_TYPE_TRANSFER);
1445 if (result != VK_SUCCESS)
1446 return result;
1447
1448 result = pvr_copy_buffer_to_image_region_format(cmd_buffer,
1449 buffer->dev_addr,
1450 ds_image,
1451 &region,
1452 copy_format,
1453 copy_format,
1454 0);
1455 if (result != VK_SUCCESS)
1456 return result;
1457
1458 cmd_buffer->state.current_sub_cmd->transfer.serialize_with_frag = true;
1459
1460 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
1461 if (result != VK_SUCCESS)
1462 return result;
1463
1464 cmd_buffer->state.current_sub_cmd = prev_sub_cmd;
1465 }
1466
1467 /* Finally, patch up the target graphics sub_cmd to use the correctly-strided
1468 * buffer.
1469 */
1470 ds->has_alignment_transfers = true;
1471 ds->addr = buffer->dev_addr;
1472 ds->physical_extent = rounded_size;
1473
1474 gfx_sub_cmd->wait_on_previous_transfer = true;
1475
1476 return VK_SUCCESS;
1477 }
1478
1479 struct pvr_emit_state {
1480 uint32_t pbe_cs_words[PVR_MAX_COLOR_ATTACHMENTS]
1481 [ROGUE_NUM_PBESTATE_STATE_WORDS];
1482
1483 uint64_t pbe_reg_words[PVR_MAX_COLOR_ATTACHMENTS]
1484 [ROGUE_NUM_PBESTATE_REG_WORDS];
1485
1486 unsigned tile_buffer_ids[PVR_MAX_COLOR_ATTACHMENTS];
1487
1488 uint32_t emit_count;
1489 };
1490
1491 static void
1492 pvr_setup_emit_state(const struct pvr_device_info *dev_info,
1493 const struct pvr_renderpass_hwsetup_render *hw_render,
1494 struct pvr_render_pass_info *render_pass_info,
1495 uint32_t view_index,
1496 struct pvr_emit_state *emit_state)
1497 {
1498 assert(hw_render->pbe_emits <= PVR_NUM_PBE_EMIT_REGS);
1499
1500 if (hw_render->eot_surface_count == 0) {
1501 emit_state->emit_count = 1;
1502 pvr_csb_pack (&emit_state->pbe_cs_words[0][1],
1503 PBESTATE_STATE_WORD1,
1504 state) {
1505 state.emptytile = true;
1506 }
1507 return;
1508 }
1509
1510 static_assert(USC_MRT_RESOURCE_TYPE_OUTPUT_REG + 1 ==
1511 USC_MRT_RESOURCE_TYPE_MEMORY,
1512 "The loop below needs adjusting.");
1513
1514 emit_state->emit_count = 0;
1515 for (uint32_t resource_type = USC_MRT_RESOURCE_TYPE_OUTPUT_REG;
1516 resource_type <= USC_MRT_RESOURCE_TYPE_MEMORY;
1517 resource_type++) {
1518 for (uint32_t i = 0; i < hw_render->eot_surface_count; i++) {
1519 const struct pvr_render_state *rstate = render_pass_info->rstate;
1520 const struct pvr_renderpass_hwsetup_eot_surface *surface =
1521 &hw_render->eot_surfaces[i];
1522 const struct pvr_image_view *iview =
1523 render_pass_info->attachments[surface->attachment_idx];
1524 const struct usc_mrt_resource *mrt_resource =
1525 &hw_render->eot_setup.mrt_resources[surface->mrt_idx];
1526 uint32_t samples = 1;
1527
1528 if (mrt_resource->type != resource_type)
1529 continue;
1530
1531 if (surface->need_resolve) {
1532 const struct pvr_image_view *resolve_src =
1533 render_pass_info->attachments[surface->src_attachment_idx];
1534
1535 /* Attachments that are the destination of resolve operations must
1536 * be loaded before their next use.
1537 */
1538 render_pass_info->enable_bg_tag = true;
1539 render_pass_info->process_empty_tiles = true;
1540
1541 if (surface->resolve_type != PVR_RESOLVE_TYPE_PBE)
1542 continue;
1543
1544 samples = (uint32_t)resolve_src->vk.image->samples;
1545 }
1546
1547 assert(emit_state->emit_count < ARRAY_SIZE(emit_state->pbe_cs_words));
1548 assert(emit_state->emit_count < ARRAY_SIZE(emit_state->pbe_reg_words));
1549
1550 emit_state->tile_buffer_ids[emit_state->emit_count] =
1551 mrt_resource->type == USC_MRT_RESOURCE_TYPE_MEMORY
1552 ? mrt_resource->mem.tile_buffer
1553 : ~0;
1554
1555 pvr_setup_pbe_state(dev_info,
1556 rstate,
1557 emit_state->emit_count,
1558 mrt_resource,
1559 iview,
1560 &render_pass_info->render_area,
1561 surface->need_resolve,
1562 samples,
1563 emit_state->pbe_cs_words[emit_state->emit_count],
1564 emit_state->pbe_reg_words[emit_state->emit_count],
1565 view_index);
1566 emit_state->emit_count += 1;
1567 }
1568 }
1569
1570 assert(emit_state->emit_count == hw_render->pbe_emits);
1571 }
1572
1573 static inline bool
1574 pvr_is_render_area_tile_aligned(const struct pvr_cmd_buffer *cmd_buffer,
1575 const struct pvr_image_view *iview)
1576 {
1577 const VkRect2D *render_area =
1578 &cmd_buffer->state.render_pass_info.render_area;
1579
1580 return render_area->offset.x == 0 && render_area->offset.y == 0 &&
1581 render_area->extent.height == iview->vk.extent.height &&
1582 render_area->extent.width == iview->vk.extent.width;
1583 }
1584
1585 static VkResult pvr_render_targets_dataset_init(
1586 struct pvr_device *device,
1587 struct pvr_render_state *rstate,
1588 const struct pvr_renderpass_hwsetup_render *hw_render);
1589
1590 static VkResult pvr_sub_cmd_gfx_job_init(const struct pvr_device_info *dev_info,
1591 struct pvr_cmd_buffer *cmd_buffer,
1592 struct pvr_sub_cmd_gfx *sub_cmd)
1593 {
1594 static const VkClearDepthStencilValue default_ds_clear_value = {
1595 .depth = 1.0f,
1596 .stencil = 0xFFFFFFFF,
1597 };
1598
1599 const struct vk_dynamic_graphics_state *dynamic_state =
1600 &cmd_buffer->vk.dynamic_graphics_state;
1601 struct pvr_render_pass_info *render_pass_info =
1602 &cmd_buffer->state.render_pass_info;
1603 const struct pvr_renderpass_hwsetup_render *hw_render =
1604 pvr_arch_pass_info_get_hw_render(render_pass_info,
1605 sub_cmd->hw_render_idx);
1606 struct pvr_render_job *job = &sub_cmd->job;
1607 struct pvr_render_state *rstate = render_pass_info->rstate;
1608 struct pvr_spm_bgobj_state *spm_bgobj_state =
1609 &rstate->spm_bgobj_state_per_render[sub_cmd->hw_render_idx];
1610 struct pvr_spm_eot_state *spm_eot_state =
1611 &rstate->spm_eot_state_per_render[sub_cmd->hw_render_idx];
1612 struct pvr_render_target *render_target;
1613 VkResult result;
1614
1615 /* Unless for barrier_{store,load}, where the index defaults to zero, the
1616 * view index associated with a gfx job is known and set only at submission
1617 * time.
1618 */
1619 job->view_state.view_index = 0;
1620
1621 if (sub_cmd->barrier_store) {
1622 /* Store to the SPM scratch buffer. */
1623
1624 /* The scratch buffer is always needed and allocated to avoid data loss in
1625 * case SPM is hit so set the flag unconditionally.
1626 */
1627 job->requires_spm_scratch_buffer = true;
1628
1629 /* There can only ever be one frag job running on the hardware at any one
1630 * time, and a context switch is not allowed mid-tile, so instead of
1631 * allocating a new scratch buffer we can reuse the SPM scratch buffer to
1632 * perform the store.
1633 * So use the SPM EOT program with the SPM PBE reg words in order to store
1634 * the render to the SPM scratch buffer.
1635 */
1636
1637 memcpy(job->pbe_reg_words,
1638 &spm_eot_state->pbe_reg_words,
1639 sizeof(job->pbe_reg_words));
1640
1641 /* Configure the job view state for a barrier store */
1642 assert(!job->view_state.view_index);
1643
1644 job->view_state.view[0].pds_pixel_event_data_offset =
1645 spm_eot_state->pixel_event_program_data_offset;
1646 job->view_state.force_pds_pixel_event_data_offset_zero = true;
1647 } else {
1648 struct pvr_pds_upload pds_pixel_event_program;
1649 struct pvr_emit_state emit_state = { 0 };
1650 memset(emit_state.tile_buffer_ids,
1651 ~0,
1652 sizeof(emit_state.tile_buffer_ids));
1653
1654 u_foreach_bit (view_idx, hw_render->view_mask) {
1655 pvr_setup_emit_state(dev_info,
1656 hw_render,
1657 render_pass_info,
1658 view_idx,
1659 &emit_state);
1660
1661 unsigned pixel_output_width =
1662 pvr_get_pixel_output_width(hw_render, dev_info);
1663
1664 result = pvr_sub_cmd_gfx_per_job_fragment_programs_create_and_upload(
1665 cmd_buffer,
1666 emit_state.emit_count,
1667 emit_state.pbe_cs_words[0],
1668 emit_state.tile_buffer_ids,
1669 pixel_output_width,
1670 &pds_pixel_event_program);
1671 if (result != VK_SUCCESS)
1672 return result;
1673
1674 /* Configure the job view state */
1675 job->view_state.view[view_idx].pds_pixel_event_data_offset =
1676 pds_pixel_event_program.data_offset;
1677 }
1678
1679 job->z_only_render = !hw_render->eot_surface_count &&
1680 !sub_cmd->frag_has_side_effects &&
1681 !sub_cmd->has_depth_feedback;
1682
1683 memcpy(job->pbe_reg_words,
1684 emit_state.pbe_reg_words,
1685 sizeof(job->pbe_reg_words));
1686 }
1687
1688 if (sub_cmd->barrier_load) {
1689 job->enable_bg_tag = true;
1690 job->process_empty_tiles = true;
1691
1692 /* Load the previously stored render from the SPM scratch buffer. */
1693
1694 STATIC_ASSERT(ARRAY_SIZE(job->view_state.view[0].pds_bgnd_reg_values) ==
1695 ARRAY_SIZE(spm_bgobj_state->pds_reg_values));
1696 typed_memcpy(job->view_state.view[0].pds_bgnd_reg_values,
1697 spm_bgobj_state->pds_reg_values,
1698 ARRAY_SIZE(spm_bgobj_state->pds_reg_values));
1699
1700 STATIC_ASSERT(
1701 ARRAY_SIZE(job->view_state.view[0].pr_pds_bgnd_reg_values) ==
1702 ARRAY_SIZE(spm_bgobj_state->pds_reg_values));
1703 typed_memcpy(job->view_state.view[0].pr_pds_bgnd_reg_values,
1704 spm_bgobj_state->pds_reg_values,
1705 ARRAY_SIZE(spm_bgobj_state->pds_reg_values));
1706
1707 /* Configure the job view state for a barrier load */
1708 assert(!job->view_state.view_index);
1709 job->view_state.force_pds_bgnd_reg_values_zero = true;
1710 } else if (hw_render->load_op_state) {
1711 const struct pvr_load_op_state *load_op_state = hw_render->load_op_state;
1712
1713 /* We always have at least 1 bit set in the view_mask */
1714 assert(load_op_state->load_op_count);
1715
1716 /* Recalculate Background Object(s). */
1717
1718 result = pvr_load_op_state_data_create_and_upload(cmd_buffer,
1719 load_op_state,
1720 &job->view_state);
1721 if (result != VK_SUCCESS)
1722 return result;
1723
1724 job->enable_bg_tag = render_pass_info->enable_bg_tag;
1725 job->process_empty_tiles = render_pass_info->process_empty_tiles;
1726 }
1727
1728 if (!hw_render->requires_frag_pr) {
1729 memcpy(job->pr_pbe_reg_words,
1730 job->pbe_reg_words,
1731 sizeof(job->pbe_reg_words));
1732 job->view_state.use_pds_pixel_event_data_offset = true;
1733 } else {
1734 memcpy(job->pr_pbe_reg_words,
1735 &spm_eot_state->pbe_reg_words,
1736 sizeof(job->pbe_reg_words));
1737 job->pr_pds_pixel_event_data_offset =
1738 spm_eot_state->pixel_event_program_data_offset;
1739 }
1740
1741 render_target = pvr_get_render_target(hw_render, rstate);
1742 job->view_state.rt_datasets = &render_target->rt_dataset[0];
1743
1744 if (cmd_buffer->state.current_sub_cmd->is_dynamic_render &&
1745 !cmd_buffer->state.current_sub_cmd->is_resume) {
1746 result =
1747 pvr_render_targets_dataset_init(cmd_buffer->device, rstate, hw_render);
1748 if (result != VK_SUCCESS) {
1749 vk_command_buffer_set_error(&cmd_buffer->vk, result);
1750 return result;
1751 }
1752 }
1753
1754 job->ctrl_stream_addr = pvr_csb_get_start_address(&sub_cmd->control_stream);
1755
1756 if (sub_cmd->depth_bias_bo)
1757 job->depth_bias_table_addr = sub_cmd->depth_bias_bo->dev_addr;
1758 else
1759 job->depth_bias_table_addr = PVR_DEV_ADDR_INVALID;
1760
1761 if (sub_cmd->scissor_bo)
1762 job->scissor_table_addr = sub_cmd->scissor_bo->dev_addr;
1763 else
1764 job->scissor_table_addr = PVR_DEV_ADDR_INVALID;
1765
1766 job->pixel_output_width = pvr_get_pixel_output_width(hw_render, dev_info);
1767
1768 /* Setup depth/stencil job information. */
1769 if (hw_render->ds_attach_idx != VK_ATTACHMENT_UNUSED) {
1770 struct pvr_image_view *ds_iview =
1771 render_pass_info->attachments[hw_render->ds_attach_idx];
1772 const struct pvr_image *ds_image = pvr_image_view_get_image(ds_iview);
1773 const struct pvr_image_plane *ds_plane = pvr_single_plane_const(ds_image);
1774
1775 job->has_depth_attachment = vk_format_has_depth(ds_image->vk.format);
1776 job->has_stencil_attachment = vk_format_has_stencil(ds_image->vk.format);
1777
1778 if (job->has_depth_attachment || job->has_stencil_attachment) {
1779 uint32_t level_pitch =
1780 ds_plane->mip_levels[ds_iview->vk.base_mip_level].pitch;
1781 const bool render_area_is_tile_aligned =
1782 pvr_is_render_area_tile_aligned(cmd_buffer, ds_iview);
1783 bool store_was_optimised_out = false;
1784 bool d_store = false, s_store = false;
1785 bool d_load = false, s_load = false;
1786
1787 job->ds.iview = ds_iview;
1788 job->ds.addr = ds_image->dev_addr;
1789
1790 job->ds.stride =
1791 pvr_stride_from_pitch(level_pitch, ds_iview->vk.format);
1792 job->ds.height = ds_iview->vk.extent.height;
1793 job->ds.physical_extent = (VkExtent2D){
1794 .width = u_minify(ds_plane->physical_extent.width,
1795 ds_iview->vk.base_mip_level),
1796 .height = u_minify(ds_plane->physical_extent.height,
1797 ds_iview->vk.base_mip_level),
1798 };
1799 job->ds.layer_size = ds_plane->layer_size;
1800
1801 job->ds_clear_value = default_ds_clear_value;
1802
1803 if (hw_render->ds_attach_idx < render_pass_info->clear_value_count) {
1804 const VkClearDepthStencilValue *const clear_values =
1805 &render_pass_info->clear_values[hw_render->ds_attach_idx]
1806 .depthStencil;
1807
1808 if (job->has_depth_attachment)
1809 job->ds_clear_value.depth = clear_values->depth;
1810
1811 if (job->has_stencil_attachment)
1812 job->ds_clear_value.stencil = clear_values->stencil;
1813 }
1814
1815 switch (ds_iview->vk.format) {
1816 case VK_FORMAT_D16_UNORM:
1817 job->ds.zls_format = ROGUE_CR_ZLS_FORMAT_TYPE_16BITINT;
1818 break;
1819
1820 case VK_FORMAT_S8_UINT:
1821 case VK_FORMAT_D32_SFLOAT:
1822 job->ds.zls_format = ROGUE_CR_ZLS_FORMAT_TYPE_F32Z;
1823 break;
1824
1825 case VK_FORMAT_D24_UNORM_S8_UINT:
1826 case VK_FORMAT_X8_D24_UNORM_PACK32:
1827 job->ds.zls_format = ROGUE_CR_ZLS_FORMAT_TYPE_24BITINT;
1828 break;
1829
1830 case VK_FORMAT_D32_SFLOAT_S8_UINT:
1831 job->ds.zls_format = ROGUE_CR_ZLS_FORMAT_TYPE_F64Z;
1832 break;
1833
1834 default:
1835 UNREACHABLE("Unsupported depth stencil format");
1836 }
1837
1838 job->ds.memlayout = ds_image->memlayout;
1839
1840 if (job->has_depth_attachment) {
1841 if (hw_render->depth_store || sub_cmd->barrier_store) {
1842 const bool depth_init_is_clear = hw_render->depth_init ==
1843 VK_ATTACHMENT_LOAD_OP_CLEAR;
1844
1845 d_store = true;
1846
1847 if (hw_render->depth_store && render_area_is_tile_aligned &&
1848 !(sub_cmd->modifies_depth || depth_init_is_clear)) {
1849 d_store = false;
1850 store_was_optimised_out = true;
1851 }
1852 }
1853
1854 if (d_store && !render_area_is_tile_aligned) {
1855 d_load = true;
1856 } else if (hw_render->depth_init == VK_ATTACHMENT_LOAD_OP_LOAD) {
1857 enum pvr_depth_stencil_usage depth_usage = sub_cmd->depth_usage;
1858 d_load = (depth_usage != PVR_DEPTH_STENCIL_USAGE_NEVER);
1859 } else {
1860 d_load = sub_cmd->barrier_load;
1861 }
1862 }
1863
1864 if (job->has_stencil_attachment) {
1865 if (hw_render->stencil_store || sub_cmd->barrier_store) {
1866 const bool stencil_init_is_clear = hw_render->stencil_init ==
1867 VK_ATTACHMENT_LOAD_OP_CLEAR;
1868
1869 s_store = true;
1870
1871 if (hw_render->stencil_store && render_area_is_tile_aligned &&
1872 !(sub_cmd->modifies_stencil || stencil_init_is_clear)) {
1873 s_store = false;
1874 store_was_optimised_out = true;
1875 }
1876 }
1877
1878 if (s_store && !render_area_is_tile_aligned) {
1879 s_load = true;
1880 } else if (hw_render->stencil_init == VK_ATTACHMENT_LOAD_OP_LOAD) {
1881 enum pvr_depth_stencil_usage stencil_usage =
1882 sub_cmd->stencil_usage;
1883 s_load = (stencil_usage != PVR_DEPTH_STENCIL_USAGE_NEVER);
1884 } else {
1885 s_load = sub_cmd->barrier_load;
1886 }
1887 }
1888
1889 job->ds.load.d = d_load;
1890 job->ds.load.s = s_load;
1891 job->ds.store.d = d_store;
1892 job->ds.store.s = s_store;
1893
1894 /* ZLS can't do masked writes for packed depth stencil formats so if
1895 * we store anything, we have to store everything.
1896 */
1897 if ((job->ds.store.d || job->ds.store.s) &&
1898 pvr_zls_format_type_is_packed(job->ds.zls_format)) {
1899 job->ds.store.d = true;
1900 job->ds.store.s = true;
1901
1902 /* In case we are only operating on one aspect of the attachment we
1903 * need to load the unused one in order to preserve its contents due
1904 * to the forced store which might otherwise corrupt it.
1905 */
1906 if (hw_render->depth_init != VK_ATTACHMENT_LOAD_OP_CLEAR)
1907 job->ds.load.d = true;
1908
1909 if (hw_render->stencil_init != VK_ATTACHMENT_LOAD_OP_CLEAR)
1910 job->ds.load.s = true;
1911 }
1912
1913 if (pvr_ds_attachment_requires_zls(&job->ds) ||
1914 store_was_optimised_out) {
1915 job->process_empty_tiles = true;
1916 }
1917
1918 if (pvr_sub_cmd_gfx_requires_ds_subtile_alignment(dev_info, job)) {
1919 result = pvr_sub_cmd_gfx_align_ds_subtiles(cmd_buffer, sub_cmd);
1920 if (result != VK_SUCCESS)
1921 return result;
1922 }
1923 }
1924 } else {
1925 job->has_depth_attachment = false;
1926 job->has_stencil_attachment = false;
1927 job->ds_clear_value = default_ds_clear_value;
1928 }
1929
1930 if (hw_render->ds_attach_idx != VK_ATTACHMENT_UNUSED) {
1931 struct pvr_image_view *iview =
1932 render_pass_info->attachments[hw_render->ds_attach_idx];
1933 const struct pvr_image *image = pvr_image_view_get_image(iview);
1934
1935 /* If the HW render pass has a valid depth/stencil surface, determine the
1936 * sample count from the attachment's image.
1937 */
1938 job->samples = image->vk.samples;
1939 } else if (hw_render->output_regs_count) {
1940 /* If the HW render pass has output registers, we have color attachments
1941 * to write to, so determine the sample count from the count specified for
1942 * every color attachment in this render.
1943 */
1944 job->samples = hw_render->sample_count;
1945 } else if (cmd_buffer->state.gfx_pipeline) {
1946 /* If the HW render pass has no color or depth/stencil attachments, we
1947 * determine the sample count from the count given during pipeline
1948 * creation.
1949 */
1950 job->samples = dynamic_state->ms.rasterization_samples;
1951 } else if (render_pass_info->pass->attachment_count > 0) {
1952 /* If we get here, we have a render pass with subpasses containing no
1953 * attachments. The next best thing is largest of the sample counts
1954 * specified by the render pass attachment descriptions.
1955 */
1956 job->samples = render_pass_info->pass->max_sample_count;
1957 } else {
1958 /* No appropriate framebuffer attachment is available. */
1959 mesa_logw("Defaulting render job sample count to 1.");
1960 job->samples = VK_SAMPLE_COUNT_1_BIT;
1961 }
1962
1963 if (sub_cmd->max_tiles_in_flight ==
1964 PVR_GET_FEATURE_VALUE(dev_info, isp_max_tiles_in_flight, 1U)) {
1965 /* Use the default limit based on the partition store. */
1966 job->max_tiles_in_flight = 0U;
1967 } else {
1968 job->max_tiles_in_flight = sub_cmd->max_tiles_in_flight;
1969 }
1970
1971 job->frag_uses_atomic_ops = sub_cmd->frag_uses_atomic_ops;
1972 job->disable_compute_overlap = false;
1973 job->max_shared_registers = cmd_buffer->state.max_shared_regs;
1974
1975 if (!cmd_buffer->state.current_sub_cmd->is_suspend) {
1976 assert(cmd_buffer->state.current_sub_cmd->type ==
1977 PVR_SUB_CMD_TYPE_GRAPHICS);
1978 job->geometry_terminate = true;
1979 job->run_frag = true;
1980 }
1981
1982 /* TODO: Enable pixel merging when it's safe to do. */
1983 job->disable_pixel_merging = true;
1984
1985 return VK_SUCCESS;
1986 }
1987
1988 static void
1989 pvr_sub_cmd_compute_job_init(const struct pvr_physical_device *pdevice,
1990 struct pvr_cmd_buffer *cmd_buffer,
1991 struct pvr_sub_cmd_compute *sub_cmd)
1992 {
1993 sub_cmd->num_shared_regs = MAX2(cmd_buffer->device->idfwdf_state.usc_shareds,
1994 cmd_buffer->state.max_shared_regs);
1995
1996 cmd_buffer->state.max_shared_regs = 0U;
1997 }
1998
1999 #define PIXEL_ALLOCATION_SIZE_MAX_IN_BLOCKS \
2000 (1024 / ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE)
2001
2002 static uint32_t
2003 pvr_compute_flat_slot_size(const struct pvr_physical_device *pdevice,
2004 uint32_t coeff_regs_count,
2005 bool use_barrier,
2006 uint32_t total_workitems)
2007 {
2008 const struct pvr_device_runtime_info *dev_runtime_info =
2009 &pdevice->dev_runtime_info;
2010 const struct pvr_device_info *dev_info = &pdevice->dev_info;
2011 uint32_t max_workgroups_per_task = ROGUE_CDM_MAX_PACKED_WORKGROUPS_PER_TASK;
2012 uint32_t max_avail_coeff_regs =
2013 dev_runtime_info->cdm_max_local_mem_size_regs;
2014 uint32_t localstore_chunks_count =
2015 DIV_ROUND_UP(PVR_DW_TO_BYTES(coeff_regs_count),
2016 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE);
2017
2018 /* Ensure that we cannot have more workgroups in a slot than the available
2019 * number of coefficients allow us to have.
2020 */
2021 if (coeff_regs_count > 0U) {
2022 /* If the geometry or fragment jobs can overlap with the compute job, or
2023 * if there is a vertex shader already running then we need to consider
2024 * this in calculating max allowed work-groups.
2025 */
2026 if (PVR_HAS_QUIRK(dev_info, 52354) &&
2027 (PVR_HAS_FEATURE(dev_info, compute_overlap) ||
2028 PVR_HAS_FEATURE(dev_info, gs_rta_support))) {
2029 /* Solve for n (number of work-groups per task). All values are in
2030 * size of common store alloc blocks:
2031 *
2032 * n + (2n + 7) * (local_memory_size_max - 1) =
2033 * (coefficient_memory_pool_size) - (7 * pixel_allocation_size_max)
2034 * ==>
2035 * n + 2n * (local_memory_size_max - 1) =
2036 * (coefficient_memory_pool_size) - (7 * pixel_allocation_size_max)
2037 * - (7 * (local_memory_size_max - 1))
2038 * ==>
2039 * n * (1 + 2 * (local_memory_size_max - 1)) =
2040 * (coefficient_memory_pool_size) - (7 * pixel_allocation_size_max)
2041 * - (7 * (local_memory_size_max - 1))
2042 * ==>
2043 * n = ((coefficient_memory_pool_size) -
2044 * (7 * pixel_allocation_size_max) -
2045 * (7 * (local_memory_size_max - 1)) / (1 +
2046 * 2 * (local_memory_size_max - 1)))
2047 */
2048 uint32_t max_common_store_blocks =
2049 DIV_ROUND_UP(max_avail_coeff_regs * 4U,
2050 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE);
2051
2052 /* (coefficient_memory_pool_size) - (7 * pixel_allocation_size_max)
2053 */
2054 max_common_store_blocks -= ROGUE_MAX_OVERLAPPED_PIXEL_TASK_INSTANCES *
2055 PIXEL_ALLOCATION_SIZE_MAX_IN_BLOCKS;
2056
2057 /* - (7 * (local_memory_size_max - 1)) */
2058 max_common_store_blocks -= (ROGUE_MAX_OVERLAPPED_PIXEL_TASK_INSTANCES *
2059 (localstore_chunks_count - 1U));
2060
2061 /* Divide by (1 + 2 * (local_memory_size_max - 1)) */
2062 max_workgroups_per_task = max_common_store_blocks /
2063 (1U + 2U * (localstore_chunks_count - 1U));
2064
2065 max_workgroups_per_task =
2066 MIN2(max_workgroups_per_task,
2067 ROGUE_CDM_MAX_PACKED_WORKGROUPS_PER_TASK);
2068
2069 } else {
2070 max_workgroups_per_task =
2071 MIN2((max_avail_coeff_regs / coeff_regs_count),
2072 max_workgroups_per_task);
2073 }
2074 }
2075
2076 /* max_workgroups_per_task should at least be one. */
2077 assert(max_workgroups_per_task >= 1U);
2078
2079 if (total_workitems >= ROGUE_MAX_INSTANCES_PER_TASK) {
2080 /* In this case, the work group size will have been padded up to the
2081 * next ROGUE_MAX_INSTANCES_PER_TASK so we just set max instances to be
2082 * ROGUE_MAX_INSTANCES_PER_TASK.
2083 */
2084 return ROGUE_MAX_INSTANCES_PER_TASK;
2085 }
2086
2087 /* In this case, the number of instances in the slot must be clamped to
2088 * accommodate whole work-groups only.
2089 */
2090 if (PVR_HAS_QUIRK(dev_info, 49032) || use_barrier) {
2091 max_workgroups_per_task =
2092 MIN2(max_workgroups_per_task,
2093 ROGUE_MAX_INSTANCES_PER_TASK / total_workitems);
2094 return total_workitems * max_workgroups_per_task;
2095 }
2096
2097 return MIN2(total_workitems * max_workgroups_per_task,
2098 ROGUE_MAX_INSTANCES_PER_TASK);
2099 }
2100
2101 static void
2102 pvr_compute_generate_control_stream(struct pvr_csb *csb,
2103 struct pvr_sub_cmd_compute *sub_cmd,
2104 const struct pvr_compute_kernel_info *info)
2105 {
2106 pvr_csb_set_relocation_mark(csb);
2107
2108 /* Compute kernel 0. */
2109 pvr_csb_emit (csb, CDMCTRL_KERNEL0, kernel0) {
2110 kernel0.indirect_present = !!info->indirect_buffer_addr.addr;
2111 kernel0.global_offsets_present = info->global_offsets_present;
2112 kernel0.usc_common_size = info->usc_common_size;
2113 kernel0.usc_unified_size = info->usc_unified_size;
2114 kernel0.pds_temp_size = info->pds_temp_size;
2115 kernel0.pds_data_size = info->pds_data_size;
2116 kernel0.usc_target = info->usc_target;
2117 kernel0.fence = info->is_fence;
2118 }
2119
2120 /* Compute kernel 1. */
2121 pvr_csb_emit (csb, CDMCTRL_KERNEL1, kernel1) {
2122 kernel1.data_addr = PVR_DEV_ADDR(info->pds_data_offset);
2123 kernel1.sd_type = info->sd_type;
2124 kernel1.usc_common_shared = info->usc_common_shared;
2125 }
2126
2127 /* Compute kernel 2. */
2128 pvr_csb_emit (csb, CDMCTRL_KERNEL2, kernel2) {
2129 kernel2.code_addr = PVR_DEV_ADDR(info->pds_code_offset);
2130 }
2131
2132 if (info->indirect_buffer_addr.addr) {
2133 /* Compute kernel 6. */
2134 pvr_csb_emit (csb, CDMCTRL_KERNEL6, kernel6) {
2135 kernel6.indirect_addrmsb = info->indirect_buffer_addr;
2136 }
2137
2138 /* Compute kernel 7. */
2139 pvr_csb_emit (csb, CDMCTRL_KERNEL7, kernel7) {
2140 kernel7.indirect_addrlsb = info->indirect_buffer_addr;
2141 }
2142 } else {
2143 /* Compute kernel 3. */
2144 pvr_csb_emit (csb, CDMCTRL_KERNEL3, kernel3) {
2145 assert(info->global_size[0U] > 0U);
2146 kernel3.workgroup_x = info->global_size[0U] - 1U;
2147 }
2148
2149 /* Compute kernel 4. */
2150 pvr_csb_emit (csb, CDMCTRL_KERNEL4, kernel4) {
2151 assert(info->global_size[1U] > 0U);
2152 kernel4.workgroup_y = info->global_size[1U] - 1U;
2153 }
2154
2155 /* Compute kernel 5. */
2156 pvr_csb_emit (csb, CDMCTRL_KERNEL5, kernel5) {
2157 assert(info->global_size[2U] > 0U);
2158 kernel5.workgroup_z = info->global_size[2U] - 1U;
2159 }
2160 }
2161
2162 /* Compute kernel 8. */
2163 pvr_csb_emit (csb, CDMCTRL_KERNEL8, kernel8) {
2164 if (info->max_instances == ROGUE_MAX_INSTANCES_PER_TASK)
2165 kernel8.max_instances = 0U;
2166 else
2167 kernel8.max_instances = info->max_instances;
2168
2169 assert(info->local_size[0U] > 0U);
2170 kernel8.workgroup_size_x = info->local_size[0U] - 1U;
2171 assert(info->local_size[1U] > 0U);
2172 kernel8.workgroup_size_y = info->local_size[1U] - 1U;
2173 assert(info->local_size[2U] > 0U);
2174 kernel8.workgroup_size_z = info->local_size[2U] - 1U;
2175 }
2176
2177 pvr_csb_clear_relocation_mark(csb);
2178
2179 /* Track the highest amount of shared registers usage in this dispatch.
2180 * This is used by the FW for context switching, so must be large enough
2181 * to contain all the shared registers that might be in use for this compute
2182 * job. Coefficients don't need to be included as the context switch will not
2183 * happen within the execution of a single workgroup, thus nothing needs to
2184 * be preserved.
2185 */
2186 if (info->usc_common_shared) {
2187 sub_cmd->num_shared_regs =
2188 MAX2(sub_cmd->num_shared_regs, info->usc_common_size);
2189 }
2190 }
2191
2192 /* TODO: This can be pre-packed and uploaded directly. Would that provide any
2193 * speed up?
2194 */
2195 static void
2196 pvr_compute_generate_idfwdf(struct pvr_cmd_buffer *cmd_buffer,
2197 struct pvr_sub_cmd_compute *const sub_cmd)
2198 {
2199 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
2200 bool *const is_sw_barrier_required =
2201 &state->current_sub_cmd->compute.pds_sw_barrier_requires_clearing;
2202 const struct pvr_physical_device *pdevice = cmd_buffer->device->pdevice;
2203 struct pvr_csb *csb = &sub_cmd->control_stream;
2204 const struct pvr_pds_upload *program;
2205
2206 if (PVR_NEED_SW_COMPUTE_PDS_BARRIER(&pdevice->dev_info) &&
2207 *is_sw_barrier_required) {
2208 *is_sw_barrier_required = false;
2209 program = &cmd_buffer->device->idfwdf_state.sw_compute_barrier_pds;
2210 } else {
2211 program = &cmd_buffer->device->idfwdf_state.pds;
2212 }
2213
2214 struct pvr_compute_kernel_info info = {
2215 .indirect_buffer_addr = PVR_DEV_ADDR_INVALID,
2216 .global_offsets_present = false,
2217 .usc_common_size = DIV_ROUND_UP(
2218 PVR_DW_TO_BYTES(cmd_buffer->device->idfwdf_state.usc_shareds),
2219 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE),
2220 .usc_unified_size = 0U,
2221 .pds_temp_size = 0U,
2222 .pds_data_size =
2223 DIV_ROUND_UP(PVR_DW_TO_BYTES(program->data_size),
2224 ROGUE_CDMCTRL_KERNEL0_PDS_DATA_SIZE_UNIT_SIZE),
2225 .usc_target = ROGUE_CDMCTRL_USC_TARGET_ALL,
2226 .is_fence = false,
2227 .pds_data_offset = program->data_offset,
2228 .sd_type = ROGUE_CDMCTRL_SD_TYPE_USC,
2229 .usc_common_shared = true,
2230 .pds_code_offset = program->code_offset,
2231 .global_size = { 1U, 1U, 1U },
2232 .local_size = { 1U, 1U, 1U },
2233 };
2234
2235 /* We don't need to pad work-group size for this case. */
2236
2237 info.max_instances =
2238 pvr_compute_flat_slot_size(pdevice,
2239 cmd_buffer->device->idfwdf_state.usc_shareds,
2240 false,
2241 1U);
2242
2243 pvr_compute_generate_control_stream(csb, sub_cmd, &info);
2244 }
2245
2246 /* TODO: This can be pre-packed and uploaded directly. Would that provide any
2247 * speed up?
2248 */
2249 void pvr_arch_compute_generate_fence(struct pvr_cmd_buffer *cmd_buffer,
2250 struct pvr_sub_cmd_compute *const sub_cmd,
2251 bool deallocate_shareds)
2252 {
2253 const struct pvr_pds_upload *program =
2254 &cmd_buffer->device->pds_compute_fence_program;
2255 const struct pvr_physical_device *pdevice = cmd_buffer->device->pdevice;
2256 struct pvr_csb *csb = &sub_cmd->control_stream;
2257
2258 struct pvr_compute_kernel_info info = {
2259 .indirect_buffer_addr = PVR_DEV_ADDR_INVALID,
2260 .global_offsets_present = false,
2261 .usc_common_size = 0U,
2262 .usc_unified_size = 0U,
2263 .pds_temp_size = 0U,
2264 .pds_data_size =
2265 DIV_ROUND_UP(PVR_DW_TO_BYTES(program->data_size),
2266 ROGUE_CDMCTRL_KERNEL0_PDS_DATA_SIZE_UNIT_SIZE),
2267 .usc_target = ROGUE_CDMCTRL_USC_TARGET_ANY,
2268 .is_fence = true,
2269 .pds_data_offset = program->data_offset,
2270 .sd_type = ROGUE_CDMCTRL_SD_TYPE_PDS,
2271 .usc_common_shared = deallocate_shareds,
2272 .pds_code_offset = program->code_offset,
2273 .global_size = { 1U, 1U, 1U },
2274 .local_size = { 1U, 1U, 1U },
2275 };
2276
2277 /* We don't need to pad work-group size for this case. */
2278 /* Here we calculate the slot size. This can depend on the use of barriers,
2279 * local memory, BRN's or other factors.
2280 */
2281 info.max_instances = pvr_compute_flat_slot_size(pdevice, 0U, false, 1U);
2282
2283 pvr_compute_generate_control_stream(csb, sub_cmd, &info);
2284 }
2285
2286 static VkResult
2287 pvr_cmd_buffer_process_deferred_clears(struct pvr_cmd_buffer *cmd_buffer)
2288 {
2289 list_for_each_entry_safe (struct pvr_transfer_cmd,
2290 transfer_cmd,
2291 &cmd_buffer->deferred_clears,
2292 link) {
2293 VkResult result;
2294
2295 list_del(&transfer_cmd->link);
2296 result = pvr_arch_cmd_buffer_add_transfer_cmd(cmd_buffer, transfer_cmd);
2297 if (result != VK_SUCCESS)
2298 return result;
2299
2300 cmd_buffer->state.current_sub_cmd->transfer.serialize_with_frag = true;
2301 }
2302
2303 return VK_SUCCESS;
2304 }
2305
2306 static VkResult
2307 pvr_csb_gfx_build_view_index_ctrl_stream(struct pvr_device *const device,
2308 pvr_dev_addr_t addr,
2309 struct pvr_bo **bo,
2310 uint32_t *stride)
2311 {
2312 struct list_head bo_list;
2313 struct pvr_csb csb;
2314 VkResult result;
2315
2316 pvr_csb_init(device, PVR_CMD_STREAM_TYPE_GRAPHICS, &csb);
2317
2318 for (uint32_t i = 0; i < PVR_MAX_MULTIVIEW; ++i) {
2319 struct pvr_pds_view_index_init_program *program =
2320 &device->view_index_init_info[i];
2321
2322 pvr_csb_set_relocation_mark(&csb);
2323
2324 pvr_csb_emit (&csb, VDMCTRL_PDS_STATE0, state_update0) {
2325 state_update0.block_type = ROGUE_VDMCTRL_BLOCK_TYPE_PDS_STATE_UPDATE;
2326 state_update0.dm_target = ROGUE_VDMCTRL_DM_TARGET_VDM;
2327 state_update0.usc_target = ROGUE_VDMCTRL_USC_TARGET_ALL;
2328 state_update0.usc_common_size = 0;
2329 state_update0.usc_unified_size = 0;
2330 state_update0.pds_temp_size = program->temps_used;
2331 state_update0.pds_data_size = DIV_ROUND_UP(
2332 PVR_DW_TO_BYTES(device->view_index_init_programs[i].data_size),
2333 ROGUE_VDMCTRL_PDS_STATE0_PDS_DATA_SIZE_UNIT_SIZE);
2334 }
2335
2336 pvr_csb_emit (&csb, VDMCTRL_PDS_STATE1, state_update1) {
2337 state_update1.pds_data_addr =
2338 device->view_index_init_programs[i].pvr_bo->dev_addr;
2339 state_update1.sd_type = ROGUE_VDMCTRL_SD_TYPE_PDS;
2340 state_update1.sd_next_type = ROGUE_VDMCTRL_SD_TYPE_USC;
2341 }
2342
2343 pvr_csb_emit (&csb, VDMCTRL_PDS_STATE2, state_update2) {
2344 state_update2.pds_code_addr.addr =
2345 device->view_index_init_programs[i].pvr_bo->dev_addr.addr +
2346 PVR_DW_TO_BYTES(device->view_index_init_programs[i].data_size);
2347 }
2348
2349 pvr_csb_clear_relocation_mark(&csb);
2350
2351 pvr_arch_csb_emit_link(&csb, addr, false);
2352 }
2353
2354 result = pvr_csb_bake(&csb, &bo_list);
2355 if (result != VK_SUCCESS)
2356 goto err_csb_finish;
2357
2358 assert(list_is_singular(&bo_list));
2359 *bo = list_first_entry(&bo_list, struct pvr_bo, link);
2360
2361 /* This needs to be kept in sync with the instructions emitted above. */
2362 *stride = pvr_cmd_length(VDMCTRL_PDS_STATE0) +
2363 pvr_cmd_length(VDMCTRL_PDS_STATE1) +
2364 pvr_cmd_length(VDMCTRL_PDS_STATE2) +
2365 pvr_cmd_length(VDMCTRL_STREAM_LINK0) +
2366 pvr_cmd_length(VDMCTRL_STREAM_LINK1);
2367
2368 return VK_SUCCESS;
2369
2370 err_csb_finish:
2371 pvr_csb_finish(&csb);
2372
2373 return result;
2374 }
2375
2376 VkResult pvr_arch_cmd_buffer_end_sub_cmd(struct pvr_cmd_buffer *cmd_buffer)
2377 {
2378 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
2379 struct pvr_sub_cmd *sub_cmd = state->current_sub_cmd;
2380 struct pvr_device *device = cmd_buffer->device;
2381 const struct pvr_query_pool *query_pool = NULL;
2382 struct pvr_suballoc_bo *query_bo = NULL;
2383 size_t query_indices_size = 0;
2384 bool suspend, dynamic_render;
2385 VkResult result;
2386
2387 /* FIXME: Is this NULL check required because this function is called from
2388 * pvr_resolve_unemitted_resolve_attachments()? See comment about this
2389 * function being called twice in a row in pvr_CmdEndRenderPass().
2390 */
2391 if (!sub_cmd)
2392 return VK_SUCCESS;
2393
2394 if (!sub_cmd->owned) {
2395 state->current_sub_cmd = NULL;
2396 return VK_SUCCESS;
2397 }
2398
2399 switch (sub_cmd->type) {
2400 case PVR_SUB_CMD_TYPE_GRAPHICS: {
2401 struct pvr_sub_cmd_gfx *const gfx_sub_cmd = &sub_cmd->gfx;
2402 const bool secondary_cont =
2403 cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY &&
2404 cmd_buffer->usage_flags &
2405 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
2406
2407 query_indices_size =
2408 util_dynarray_num_elements(&state->query_indices, char);
2409
2410 if (query_indices_size > 0) {
2411 assert(gfx_sub_cmd->query_pool);
2412
2413 if (secondary_cont) {
2414 util_dynarray_append_dynarray(&gfx_sub_cmd->sec_query_indices,
2415 &state->query_indices);
2416 } else {
2417 const void *data = util_dynarray_begin(&state->query_indices);
2418
2419 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
2420 data,
2421 query_indices_size,
2422 &query_bo);
2423 if (result != VK_SUCCESS)
2424 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2425
2426 query_pool = gfx_sub_cmd->query_pool;
2427 }
2428
2429 dynamic_render = sub_cmd->is_dynamic_render;
2430 suspend = sub_cmd->is_suspend;
2431 gfx_sub_cmd->has_query = true;
2432
2433 util_dynarray_clear(&state->query_indices);
2434 }
2435
2436 if (secondary_cont) {
2437 result = pvr_arch_csb_emit_return(&gfx_sub_cmd->control_stream);
2438 if (result != VK_SUCCESS)
2439 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2440
2441 break;
2442 }
2443
2444 /* TODO: Check if the sub_cmd can be skipped based on
2445 * sub_cmd->gfx.empty_cmd flag and if dynamic rendering isn't enabled.
2446 */
2447
2448 /* TODO: Set the state in the functions called with the command buffer
2449 * instead of here.
2450 */
2451
2452 result = pvr_cmd_buffer_upload_tables(device, cmd_buffer, gfx_sub_cmd);
2453 if (result != VK_SUCCESS)
2454 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2455
2456 result = pvr_cmd_buffer_emit_ppp_state(cmd_buffer,
2457 &gfx_sub_cmd->control_stream);
2458 if (result != VK_SUCCESS)
2459 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2460
2461 result = pvr_arch_csb_emit_terminate(&gfx_sub_cmd->control_stream);
2462 if (result != VK_SUCCESS)
2463 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2464
2465 if (gfx_sub_cmd->view_index_wanted) {
2466 result = pvr_csb_gfx_build_view_index_ctrl_stream(
2467 device,
2468 pvr_csb_get_start_address(&gfx_sub_cmd->control_stream),
2469 &gfx_sub_cmd->multiview_ctrl_stream,
2470 &gfx_sub_cmd->multiview_ctrl_stream_stride);
2471 if (result != VK_SUCCESS)
2472 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2473 }
2474
2475 result = pvr_sub_cmd_gfx_job_init(&device->pdevice->dev_info,
2476 cmd_buffer,
2477 gfx_sub_cmd);
2478 if (result != VK_SUCCESS)
2479 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2480
2481 if (pvr_sub_cmd_gfx_requires_split_submit(gfx_sub_cmd)) {
2482 result = pvr_sub_cmd_gfx_build_terminate_ctrl_stream(device,
2483 cmd_buffer,
2484 gfx_sub_cmd);
2485 if (result != VK_SUCCESS)
2486 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2487 }
2488
2489 break;
2490 }
2491
2492 case PVR_SUB_CMD_TYPE_QUERY:
2493 case PVR_SUB_CMD_TYPE_COMPUTE: {
2494 struct pvr_sub_cmd_compute *const compute_sub_cmd = &sub_cmd->compute;
2495
2496 pvr_arch_compute_generate_fence(cmd_buffer, compute_sub_cmd, true);
2497
2498 result = pvr_arch_csb_emit_terminate(&compute_sub_cmd->control_stream);
2499 if (result != VK_SUCCESS)
2500 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2501
2502 pvr_sub_cmd_compute_job_init(device->pdevice,
2503 cmd_buffer,
2504 compute_sub_cmd);
2505 break;
2506 }
2507
2508 case PVR_SUB_CMD_TYPE_TRANSFER:
2509 break;
2510
2511 case PVR_SUB_CMD_TYPE_EVENT:
2512 break;
2513
2514 default:
2515 UNREACHABLE("Unsupported sub-command type");
2516 }
2517
2518 state->current_sub_cmd = NULL;
2519
2520 /* pvr_cmd_buffer_process_deferred_clears() must be called with a NULL
2521 * current_sub_cmd.
2522 *
2523 * We can start a sub_cmd of a different type from the current sub_cmd only
2524 * after having ended the current sub_cmd. However, we can't end the current
2525 * sub_cmd if this depends on starting sub_cmd(s) of a different type. Hence,
2526 * don't try to start transfer sub_cmd(s) with
2527 * pvr_cmd_buffer_process_deferred_clears() until the current hasn't ended.
2528 * Failing to do so we will cause a circular dependency between
2529 * pvr_cmd_buffer_{end,start}_cmd and blow the stack.
2530 */
2531 if (sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS) {
2532 result = pvr_cmd_buffer_process_deferred_clears(cmd_buffer);
2533 if (result != VK_SUCCESS)
2534 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2535 }
2536
2537 if (query_pool) {
2538 struct pvr_query_info query_info;
2539
2540 assert(query_bo);
2541 assert(query_indices_size);
2542
2543 query_info.type = PVR_QUERY_TYPE_AVAILABILITY_WRITE;
2544
2545 /* sizeof(uint32_t) is for the size of single query. */
2546 query_info.availability_write.num_query_indices =
2547 query_indices_size / sizeof(uint32_t);
2548 query_info.availability_write.index_bo = query_bo;
2549
2550 query_info.availability_write.num_queries = query_pool->query_count;
2551 query_info.availability_write.availability_bo =
2552 query_pool->availability_buffer;
2553
2554 query_info.is_dynamic_render = dynamic_render;
2555 query_info.is_suspend = suspend;
2556
2557 /* Insert a barrier after the graphics sub command and before the
2558 * query sub command so that the availability write program waits for the
2559 * fragment shader to complete.
2560 */
2561
2562 result =
2563 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT);
2564 if (result != VK_SUCCESS)
2565 return result;
2566
2567 cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){
2568 .type = PVR_EVENT_TYPE_BARRIER,
2569 .barrier = {
2570 .wait_for_stage_mask = PVR_PIPELINE_STAGE_FRAG_BIT,
2571 .wait_at_stage_mask = PVR_PIPELINE_STAGE_QUERY_BIT,
2572 },
2573 };
2574
2575 return pvr_arch_add_query_program(cmd_buffer, &query_info);
2576 }
2577
2578 return VK_SUCCESS;
2579 }
2580
2581 void pvr_arch_reset_graphics_dirty_state(struct pvr_cmd_buffer *const cmd_buffer,
2582 bool start_geom)
2583 {
2584 struct vk_dynamic_graphics_state *const dynamic_state =
2585 &cmd_buffer->vk.dynamic_graphics_state;
2586
2587 if (start_geom) {
2588 /*
2589 * Initial geometry phase state.
2590 * It's the driver's responsibility to ensure that the state of the
2591 * hardware is correctly initialized at the start of every geometry
2592 * phase. This is required to prevent stale state from a previous
2593 * geometry phase erroneously affecting the next geometry phase.
2594 *
2595 * If a geometry phase does not contain any geometry, this restriction
2596 * can be ignored. If the first draw call in a geometry phase will only
2597 * update the depth or stencil buffers i.e. ISP_TAGWRITEDISABLE is set
2598 * in the ISP State Control Word, the PDS State Pointers
2599 * (TA_PRES_PDSSTATEPTR*) in the first PPP State Update do not need to
2600 * be supplied, since they will never reach the PDS in the fragment
2601 * phase.
2602 */
2603
2604 cmd_buffer->state.emit_header = (struct ROGUE_TA_STATE_HEADER){
2605 .pres_stream_out_size = true,
2606 .pres_ppp_ctrl = true,
2607 .pres_varying_word2 = true,
2608 .pres_varying_word1 = true,
2609 .pres_varying_word0 = true,
2610 .pres_outselects = true,
2611 .pres_wclamp = true,
2612 .pres_viewport = true,
2613 .pres_region_clip = true,
2614 .pres_pds_state_ptr0 = true,
2615 .pres_ispctl_fb = true,
2616 .pres_ispctl = true,
2617 };
2618 } else {
2619 struct ROGUE_TA_STATE_HEADER *const emit_header =
2620 &cmd_buffer->state.emit_header;
2621
2622 emit_header->pres_ppp_ctrl = true;
2623 emit_header->pres_varying_word1 = true;
2624 emit_header->pres_varying_word0 = true;
2625 emit_header->pres_outselects = true;
2626 emit_header->pres_viewport = true;
2627 emit_header->pres_region_clip = true;
2628 emit_header->pres_pds_state_ptr0 = true;
2629 emit_header->pres_ispctl_fb = true;
2630 emit_header->pres_ispctl = true;
2631 }
2632
2633 memset(&cmd_buffer->state.ppp_state,
2634 0U,
2635 sizeof(cmd_buffer->state.ppp_state));
2636
2637 cmd_buffer->state.dirty.vertex_bindings = true;
2638 cmd_buffer->state.dirty.gfx_pipeline_binding = true;
2639
2640 BITSET_SET(dynamic_state->dirty, MESA_VK_DYNAMIC_VP_VIEWPORTS);
2641 BITSET_SET(dynamic_state->dirty, MESA_VK_DYNAMIC_VP_VIEWPORT_COUNT);
2642 }
2643
2644 static inline bool
2645 pvr_cmd_uses_deferred_cs_cmds(const struct pvr_cmd_buffer *const cmd_buffer)
2646 {
2647 const VkCommandBufferUsageFlags deferred_control_stream_flags =
2648 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT |
2649 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
2650
2651 return cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY &&
2652 (cmd_buffer->usage_flags & deferred_control_stream_flags) ==
2653 deferred_control_stream_flags;
2654 }
2655
2656 static inline uint32_t
2657 pvr_render_pass_info_get_view_mask(const struct pvr_render_pass_info *rp_info)
2658 {
2659 const uint32_t hw_render_idx = rp_info->current_hw_subpass;
2660 const struct pvr_renderpass_hwsetup_render *hw_render =
2661 pvr_arch_pass_info_get_hw_render(rp_info, hw_render_idx);
2662
2663 return hw_render->view_mask;
2664 }
2665
2666 VkResult pvr_arch_cmd_buffer_start_sub_cmd(struct pvr_cmd_buffer *cmd_buffer,
2667 enum pvr_sub_cmd_type type)
2668 {
2669 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
2670 struct pvr_device *device = cmd_buffer->device;
2671 struct pvr_sub_cmd *sub_cmd;
2672 VkResult result;
2673
2674 /* Check the current status of the buffer. */
2675 if (vk_command_buffer_has_error(&cmd_buffer->vk))
2676 return vk_command_buffer_get_record_result(&cmd_buffer->vk);
2677
2678 pvr_cmd_buffer_update_barriers(cmd_buffer, type);
2679
2680 /* TODO: Add proper support for joining consecutive event sub_cmd? */
2681 if (state->current_sub_cmd) {
2682 if (state->current_sub_cmd->type == type) {
2683 /* Continue adding to the current sub command. */
2684 return VK_SUCCESS;
2685 }
2686
2687 /* End the current sub command. */
2688 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
2689 if (result != VK_SUCCESS)
2690 return result;
2691 }
2692
2693 sub_cmd = vk_zalloc(&cmd_buffer->vk.pool->alloc,
2694 sizeof(*sub_cmd),
2695 8,
2696 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
2697 if (!sub_cmd) {
2698 return vk_command_buffer_set_error(&cmd_buffer->vk,
2699 VK_ERROR_OUT_OF_HOST_MEMORY);
2700 }
2701
2702 sub_cmd->type = type;
2703 sub_cmd->owned = true;
2704
2705 switch (type) {
2706 case PVR_SUB_CMD_TYPE_GRAPHICS:
2707 sub_cmd->gfx.depth_usage = PVR_DEPTH_STENCIL_USAGE_UNDEFINED;
2708 sub_cmd->gfx.stencil_usage = PVR_DEPTH_STENCIL_USAGE_UNDEFINED;
2709 sub_cmd->gfx.modifies_depth = false;
2710 sub_cmd->gfx.modifies_stencil = false;
2711 sub_cmd->gfx.max_tiles_in_flight =
2712 PVR_GET_FEATURE_VALUE(&device->pdevice->dev_info,
2713 isp_max_tiles_in_flight,
2714 1);
2715 sub_cmd->gfx.rstate = state->render_pass_info.rstate;
2716 sub_cmd->gfx.empty_cmd = true;
2717 sub_cmd->gfx.view_mask =
2718 pvr_render_pass_info_get_view_mask(&state->render_pass_info);
2719 if (state->render_pass_info.dynamic_render) {
2720 sub_cmd->is_suspend = state->render_pass_info.suspend;
2721 sub_cmd->is_resume = state->render_pass_info.resume;
2722 sub_cmd->is_dynamic_render = true;
2723 sub_cmd->gfx.hw_render = state->render_pass_info.hw_render;
2724 sub_cmd->gfx.multiview_enabled =
2725 state->render_pass_info.hw_render->multiview_enabled;
2726 sub_cmd->gfx.hw_render_idx = 0;
2727 sub_cmd->gfx.dr_info = state->render_pass_info.dr_info;
2728 } else {
2729 sub_cmd->gfx.hw_render_idx =
2730 state->render_pass_info.current_hw_subpass;
2731 sub_cmd->gfx.multiview_enabled =
2732 state->render_pass_info.pass
2733 ? state->render_pass_info.pass->multiview_enabled
2734 : false;
2735 }
2736 sub_cmd->gfx.view_index_wanted = sub_cmd->gfx.multiview_enabled;
2737
2738 if (state->vis_test_enabled)
2739 sub_cmd->gfx.query_pool = state->query_pool;
2740
2741 pvr_arch_reset_graphics_dirty_state(cmd_buffer, true);
2742
2743 if (pvr_cmd_uses_deferred_cs_cmds(cmd_buffer)) {
2744 pvr_csb_init(device,
2745 PVR_CMD_STREAM_TYPE_GRAPHICS_DEFERRED,
2746 &sub_cmd->gfx.control_stream);
2747 } else {
2748 pvr_csb_init(device,
2749 PVR_CMD_STREAM_TYPE_GRAPHICS,
2750 &sub_cmd->gfx.control_stream);
2751 }
2752
2753 sub_cmd->gfx.sec_query_indices = UTIL_DYNARRAY_INIT;
2754 sub_cmd->gfx.unbound_deferred_clears = UTIL_DYNARRAY_INIT;
2755 break;
2756
2757 case PVR_SUB_CMD_TYPE_QUERY:
2758 case PVR_SUB_CMD_TYPE_COMPUTE:
2759 pvr_csb_init(device,
2760 PVR_CMD_STREAM_TYPE_COMPUTE,
2761 &sub_cmd->compute.control_stream);
2762 break;
2763
2764 case PVR_SUB_CMD_TYPE_TRANSFER:
2765 sub_cmd->transfer.transfer_cmds = &sub_cmd->transfer.transfer_cmds_priv;
2766 list_inithead(sub_cmd->transfer.transfer_cmds);
2767 break;
2768
2769 case PVR_SUB_CMD_TYPE_EVENT:
2770 break;
2771
2772 default:
2773 UNREACHABLE("Unsupported sub-command type");
2774 }
2775
2776 list_addtail(&sub_cmd->link, &cmd_buffer->sub_cmds);
2777 state->current_sub_cmd = sub_cmd;
2778
2779 return VK_SUCCESS;
2780 }
2781
2782 VkResult
2783 pvr_arch_cmd_buffer_alloc_mem(struct pvr_cmd_buffer *cmd_buffer,
2784 struct pvr_winsys_heap *heap,
2785 uint64_t size,
2786 struct pvr_suballoc_bo **const pvr_bo_out)
2787 {
2788 const uint32_t cache_line_size =
2789 pvr_get_slc_cache_line_size(&cmd_buffer->device->pdevice->dev_info);
2790 struct pvr_suballoc_bo *suballoc_bo;
2791 struct pvr_suballocator *allocator;
2792 VkResult result;
2793
2794 if (heap == cmd_buffer->device->heaps.general_heap)
2795 allocator = &cmd_buffer->device->suballoc_general;
2796 else if (heap == cmd_buffer->device->heaps.pds_heap)
2797 allocator = &cmd_buffer->device->suballoc_pds;
2798 else if (heap == cmd_buffer->device->heaps.transfer_frag_heap)
2799 allocator = &cmd_buffer->device->suballoc_transfer;
2800 else if (heap == cmd_buffer->device->heaps.usc_heap)
2801 allocator = &cmd_buffer->device->suballoc_usc;
2802 else
2803 UNREACHABLE("Unknown heap type");
2804
2805 result =
2806 pvr_bo_suballoc(allocator, size, cache_line_size, false, &suballoc_bo);
2807 if (result != VK_SUCCESS)
2808 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
2809
2810 list_add(&suballoc_bo->link, &cmd_buffer->bo_list);
2811
2812 *pvr_bo_out = suballoc_bo;
2813
2814 return VK_SUCCESS;
2815 }
2816
2817 static void pvr_cmd_bind_compute_pipeline(
2818 const struct pvr_compute_pipeline *const compute_pipeline,
2819 struct pvr_cmd_buffer *const cmd_buffer)
2820 {
2821 cmd_buffer->state.compute_pipeline = compute_pipeline;
2822 cmd_buffer->state.dirty.compute_pipeline_binding = true;
2823 }
2824
2825 static void pvr_cmd_bind_graphics_pipeline(
2826 const struct pvr_graphics_pipeline *const gfx_pipeline,
2827 struct pvr_cmd_buffer *const cmd_buffer)
2828 {
2829 cmd_buffer->state.gfx_pipeline = gfx_pipeline;
2830 cmd_buffer->state.dirty.gfx_pipeline_binding = true;
2831
2832 vk_cmd_set_dynamic_graphics_state(&cmd_buffer->vk,
2833 &gfx_pipeline->dynamic_state);
2834 }
2835
2836 void PVR_PER_ARCH(CmdBindPipeline)(VkCommandBuffer commandBuffer,
2837 VkPipelineBindPoint pipelineBindPoint,
2838 VkPipeline _pipeline)
2839 {
2840 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
2841 VK_FROM_HANDLE(pvr_pipeline, pipeline, _pipeline);
2842
2843 switch (pipelineBindPoint) {
2844 case VK_PIPELINE_BIND_POINT_COMPUTE:
2845 pvr_cmd_bind_compute_pipeline(to_pvr_compute_pipeline(pipeline),
2846 cmd_buffer);
2847 break;
2848
2849 case VK_PIPELINE_BIND_POINT_GRAPHICS:
2850 pvr_cmd_bind_graphics_pipeline(to_pvr_graphics_pipeline(pipeline),
2851 cmd_buffer);
2852 break;
2853
2854 default:
2855 UNREACHABLE("Invalid bind point.");
2856 break;
2857 }
2858 }
2859
2860 #if MESA_DEBUG
2861 static void check_viewport_quirk_70165(const struct pvr_device *device,
2862 const VkViewport *pViewport)
2863 {
2864 const struct pvr_device_info *dev_info = &device->pdevice->dev_info;
2865 float min_vertex_x, max_vertex_x, min_vertex_y, max_vertex_y;
2866 float min_screen_space_value, max_screen_space_value;
2867 float sign_to_unsigned_offset, fixed_point_max;
2868 float guardband_width, guardband_height;
2869
2870 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format)) {
2871 /* Max representable value in 13.4 fixed point format.
2872 * Round-down to avoid precision issues.
2873 * Calculated as (2 ** 13) - 2*(2 ** -4)
2874 */
2875 fixed_point_max = 8192.0f - 2.0f / 16.0f;
2876
2877 if (PVR_HAS_FEATURE(dev_info, screen_size8K)) {
2878 if (pViewport->width <= 4096 && pViewport->height <= 4096) {
2879 guardband_width = pViewport->width / 4.0f;
2880 guardband_height = pViewport->height / 4.0f;
2881
2882 /* 2k of the range is negative */
2883 sign_to_unsigned_offset = 2048.0f;
2884 } else {
2885 guardband_width = 0.0f;
2886 guardband_height = 0.0f;
2887
2888 /* For > 4k renders, the entire range is positive */
2889 sign_to_unsigned_offset = 0.0f;
2890 }
2891 } else {
2892 guardband_width = pViewport->width / 4.0f;
2893 guardband_height = pViewport->height / 4.0f;
2894
2895 /* 2k of the range is negative */
2896 sign_to_unsigned_offset = 2048.0f;
2897 }
2898 } else {
2899 /* Max representable value in 16.8 fixed point format
2900 * Calculated as (2 ** 16) - (2 ** -8)
2901 */
2902 fixed_point_max = 65535.99609375f;
2903 guardband_width = pViewport->width / 4.0f;
2904 guardband_height = pViewport->height / 4.0f;
2905
2906 /* 4k/20k of the range is negative */
2907 sign_to_unsigned_offset = (float)PVR_MAX_NEG_OFFSCREEN_OFFSET;
2908 }
2909
2910 min_screen_space_value = -sign_to_unsigned_offset;
2911 max_screen_space_value = fixed_point_max - sign_to_unsigned_offset;
2912
2913 min_vertex_x = pViewport->x - guardband_width;
2914 max_vertex_x = pViewport->x + pViewport->width + guardband_width;
2915 min_vertex_y = pViewport->y - guardband_height;
2916 max_vertex_y = pViewport->y + pViewport->height + guardband_height;
2917 if (min_vertex_x < min_screen_space_value ||
2918 max_vertex_x > max_screen_space_value ||
2919 min_vertex_y < min_screen_space_value ||
2920 max_vertex_y > max_screen_space_value) {
2921 mesa_logw("Viewport is affected by BRN70165, geometry outside "
2922 "the viewport could be corrupted");
2923 }
2924 }
2925 #endif
2926
2927 void PVR_PER_ARCH(CmdSetViewport)(VkCommandBuffer commandBuffer,
2928 uint32_t firstViewport,
2929 uint32_t viewportCount,
2930 const VkViewport *pViewports)
2931 {
2932 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
2933 const uint32_t total_count = firstViewport + viewportCount;
2934
2935 assert(firstViewport < PVR_MAX_VIEWPORTS && viewportCount > 0);
2936 assert(total_count >= 1 && total_count <= PVR_MAX_VIEWPORTS);
2937
2938 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
2939
2940 #if MESA_DEBUG
2941 if (PVR_HAS_QUIRK(&cmd_buffer->device->pdevice->dev_info, 70165)) {
2942 for (uint32_t viewport = 0; viewport < viewportCount; viewport++) {
2943 check_viewport_quirk_70165(cmd_buffer->device, &pViewports[viewport]);
2944 }
2945 }
2946 #endif
2947
2948 vk_common_CmdSetViewport(commandBuffer,
2949 firstViewport,
2950 viewportCount,
2951 pViewports);
2952 }
2953
2954 void PVR_PER_ARCH(CmdSetDepthBounds)(VkCommandBuffer commandBuffer,
2955 float minDepthBounds,
2956 float maxDepthBounds)
2957 {
2958 mesa_logd("No support for depth bounds testing.");
2959 }
2960
2961 void PVR_PER_ARCH(CmdBindDescriptorSets2KHR)(
2962 VkCommandBuffer commandBuffer,
2963 const VkBindDescriptorSetsInfoKHR *pBindDescriptorSetsInfo)
2964 {
2965 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
2966 VK_FROM_HANDLE(vk_pipeline_layout,
2967 pipeline_layout,
2968 pBindDescriptorSetsInfo->layout);
2969 unsigned dyn_off = 0;
2970 const bool has_dyn_offs = pBindDescriptorSetsInfo->dynamicOffsetCount > 0;
2971
2972 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
2973
2974 struct pvr_descriptor_state *graphics_desc_state =
2975 &cmd_buffer->state.gfx_desc_state;
2976 struct pvr_descriptor_state *compute_desc_state =
2977 &cmd_buffer->state.compute_desc_state;
2978
2979 for (unsigned u = 0; u < pBindDescriptorSetsInfo->descriptorSetCount; ++u) {
2980 VK_FROM_HANDLE(pvr_descriptor_set,
2981 set,
2982 pBindDescriptorSetsInfo->pDescriptorSets[u]);
2983 unsigned desc_set = u + pBindDescriptorSetsInfo->firstSet;
2984
2985 const struct pvr_descriptor_set_layout *set_layout =
2986 vk_to_pvr_descriptor_set_layout(
2987 pipeline_layout->set_layouts[desc_set]);
2988
2989 for (unsigned u = 0; u < set_layout->dynamic_buffer_count; ++u) {
2990 set->dynamic_buffers[u].offset =
2991 has_dyn_offs ? pBindDescriptorSetsInfo->pDynamicOffsets[dyn_off++]
2992 : 0;
2993 }
2994
2995 if (pBindDescriptorSetsInfo->stageFlags & VK_SHADER_STAGE_ALL_GRAPHICS) {
2996 if (graphics_desc_state->sets[desc_set] != set) {
2997 graphics_desc_state->sets[desc_set] = set;
2998 graphics_desc_state->dirty_sets |= BITFIELD_BIT(desc_set);
2999 }
3000 }
3001
3002 if (pBindDescriptorSetsInfo->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT) {
3003 if (compute_desc_state->sets[desc_set] != set) {
3004 compute_desc_state->sets[desc_set] = set;
3005 compute_desc_state->dirty_sets |= BITFIELD_BIT(desc_set);
3006 }
3007 }
3008 }
3009
3010 assert(dyn_off == pBindDescriptorSetsInfo->dynamicOffsetCount);
3011
3012 if (pBindDescriptorSetsInfo->stageFlags & VK_SHADER_STAGE_ALL_GRAPHICS)
3013 cmd_buffer->state.dirty.gfx_desc_dirty = true;
3014
3015 if (pBindDescriptorSetsInfo->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT)
3016 cmd_buffer->state.dirty.compute_desc_dirty = true;
3017 }
3018
3019 void PVR_PER_ARCH(CmdBindVertexBuffers2)(VkCommandBuffer commandBuffer,
3020 uint32_t firstBinding,
3021 uint32_t bindingCount,
3022 const VkBuffer *pBuffers,
3023 const VkDeviceSize *pOffsets,
3024 const VkDeviceSize *pSizes,
3025 const VkDeviceSize *pStrides)
3026 {
3027 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
3028 struct pvr_vertex_binding *const vb = cmd_buffer->state.vertex_bindings;
3029
3030 assert(firstBinding < PVR_MAX_VERTEX_INPUT_BINDINGS &&
3031 bindingCount <= PVR_MAX_VERTEX_INPUT_BINDINGS);
3032
3033 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
3034
3035 for (uint32_t i = 0; i < bindingCount; i++) {
3036 VK_FROM_HANDLE(pvr_buffer, buffer, pBuffers[i]);
3037 const uint64_t size = pSizes ? pSizes[i] : VK_WHOLE_SIZE;
3038
3039 vb[firstBinding + i] = (struct pvr_vertex_binding){
3040 .buffer = buffer,
3041 .offset = pOffsets[i],
3042 .size = vk_buffer_range(&buffer->vk, pOffsets[i], size),
3043 };
3044 }
3045
3046 if (pStrides != NULL) {
3047 vk_cmd_set_vertex_binding_strides(&cmd_buffer->vk,
3048 firstBinding,
3049 bindingCount,
3050 pStrides);
3051 }
3052
3053 cmd_buffer->state.dirty.vertex_bindings = true;
3054 }
3055
3056 void PVR_PER_ARCH(CmdBindIndexBuffer)(VkCommandBuffer commandBuffer,
3057 VkBuffer buffer,
3058 VkDeviceSize offset,
3059 VkIndexType indexType)
3060 {
3061 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
3062 VK_FROM_HANDLE(pvr_buffer, index_buffer, buffer);
3063 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
3064
3065 assert(offset < index_buffer->vk.size);
3066 assert(indexType == VK_INDEX_TYPE_UINT32 ||
3067 indexType == VK_INDEX_TYPE_UINT16 ||
3068 indexType == VK_INDEX_TYPE_UINT8_KHR);
3069
3070 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
3071
3072 state->index_buffer_binding.buffer = index_buffer;
3073 state->index_buffer_binding.offset = offset;
3074 state->index_buffer_binding.type = indexType;
3075 state->dirty.index_buffer_binding = true;
3076 }
3077
3078 static void update_push_constants(struct pvr_push_constants *push_consts,
3079 uint32_t offset,
3080 uint32_t size,
3081 const void *data)
3082 {
3083 memcpy(&push_consts->data[offset], data, size);
3084 push_consts->bytes_updated = MAX2(push_consts->bytes_updated, offset + size);
3085 push_consts->dirty = true;
3086 }
3087
3088 void PVR_PER_ARCH(CmdPushConstants2KHR)(
3089 VkCommandBuffer commandBuffer,
3090 const VkPushConstantsInfoKHR *pPushConstantsInfo)
3091 {
3092 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
3093 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
3094
3095 if (pPushConstantsInfo->stageFlags & VK_SHADER_STAGE_VERTEX_BIT) {
3096 update_push_constants(
3097 &state->push_consts[PVR_STAGE_ALLOCATION_VERTEX_GEOMETRY],
3098 pPushConstantsInfo->offset,
3099 pPushConstantsInfo->size,
3100 pPushConstantsInfo->pValues);
3101 }
3102
3103 if (pPushConstantsInfo->stageFlags & VK_SHADER_STAGE_FRAGMENT_BIT) {
3104 update_push_constants(&state->push_consts[PVR_STAGE_ALLOCATION_FRAGMENT],
3105 pPushConstantsInfo->offset,
3106 pPushConstantsInfo->size,
3107 pPushConstantsInfo->pValues);
3108 }
3109
3110 if (pPushConstantsInfo->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT) {
3111 update_push_constants(&state->push_consts[PVR_STAGE_ALLOCATION_COMPUTE],
3112 pPushConstantsInfo->offset,
3113 pPushConstantsInfo->size,
3114 pPushConstantsInfo->pValues);
3115 }
3116 }
3117
3118 static void pvr_cmd_buffer_attachments_free(struct pvr_cmd_buffer *cmd_buffer)
3119 {
3120 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
3121
3122 if (!state->render_pass_info.attachments) {
3123 assert(!state->render_pass_info.attachment_count);
3124 return;
3125 }
3126
3127 vk_free(&cmd_buffer->vk.pool->alloc, state->render_pass_info.attachments);
3128 state->render_pass_info.attachment_count = 0;
3129 state->render_pass_info.attachments = NULL;
3130 }
3131
3132 static VkResult
3133 pvr_cmd_buffer_attachments_alloc(struct pvr_cmd_buffer *cmd_buffer,
3134 uint32_t attachment_count)
3135 {
3136 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
3137 struct pvr_render_pass_info *info = &state->render_pass_info;
3138
3139 /* Free any previously allocated attachments. */
3140 pvr_cmd_buffer_attachments_free(cmd_buffer);
3141
3142 state->render_pass_info.attachment_count = attachment_count;
3143 if (attachment_count == 0) {
3144 info->attachments = NULL;
3145 return VK_SUCCESS;
3146 }
3147
3148 const size_t size = attachment_count * sizeof(*info->attachments);
3149
3150 info->attachments = vk_zalloc(&cmd_buffer->vk.pool->alloc,
3151 size,
3152 8,
3153 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
3154 if (!info->attachments) {
3155 return vk_command_buffer_set_error(&cmd_buffer->vk,
3156 VK_ERROR_OUT_OF_HOST_MEMORY);
3157 }
3158
3159 return VK_SUCCESS;
3160 }
3161
3162 static VkResult pvr_cmd_buffer_attachments_setup(
3163 struct pvr_cmd_buffer *cmd_buffer,
3164 const struct pvr_render_pass *pass,
3165 const struct pvr_framebuffer *framebuffer,
3166 const VkRenderPassBeginInfo *pRenderPassBeginInfo)
3167 {
3168 const VkRenderPassAttachmentBeginInfo *pImageless =
3169 vk_find_struct_const(pRenderPassBeginInfo->pNext,
3170 RENDER_PASS_ATTACHMENT_BEGIN_INFO);
3171 struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info;
3172
3173 VkResult result;
3174
3175 if (!pImageless)
3176 assert(pass->attachment_count == framebuffer->attachment_count);
3177
3178 result =
3179 pvr_cmd_buffer_attachments_alloc(cmd_buffer, pass->attachment_count);
3180 if (result != VK_SUCCESS)
3181 return result;
3182
3183 for (uint32_t i = 0; i < pass->attachment_count; i++) {
3184 if (pImageless && pImageless->attachmentCount) {
3185 assert(pImageless->attachmentCount == pass->attachment_count);
3186 VK_FROM_HANDLE(pvr_image_view, img_view, pImageless->pAttachments[i]);
3187 info->attachments[i] = img_view;
3188 } else {
3189 info->attachments[i] = framebuffer->attachments[i];
3190 }
3191 }
3192
3193 return result;
3194 }
3195
3196 static inline VkResult pvr_render_targets_datasets_create(
3197 struct pvr_device *device,
3198 struct pvr_render_state *rstate,
3199 const struct pvr_renderpass_hwsetup_render *hw_render,
3200 struct pvr_render_target *render_target)
3201 {
3202 const struct pvr_device_info *const dev_info = &device->pdevice->dev_info;
3203 const uint32_t layers =
3204 PVR_HAS_FEATURE(dev_info, gs_rta_support) ? rstate->layers : 1;
3205
3206 pthread_mutex_lock(&render_target->mutex);
3207
3208 u_foreach_bit (view_idx, hw_render->view_mask) {
3209 struct pvr_rt_dataset *rt_dataset;
3210 VkResult result;
3211
3212 if (render_target->valid_mask & BITFIELD_BIT(view_idx))
3213 continue;
3214
3215 result = pvr_arch_render_target_dataset_create(device,
3216 rstate->width,
3217 rstate->height,
3218 hw_render->sample_count,
3219 layers,
3220 &rt_dataset);
3221 if (result != VK_SUCCESS) {
3222 pvr_render_targets_datasets_destroy(render_target);
3223 pthread_mutex_unlock(&render_target->mutex);
3224 return result;
3225 }
3226
3227 render_target->valid_mask |= BITFIELD_BIT(view_idx);
3228 render_target->rt_dataset[view_idx] = rt_dataset;
3229 }
3230
3231 pthread_mutex_unlock(&render_target->mutex);
3232
3233 return VK_SUCCESS;
3234 }
3235
3236 static VkResult pvr_render_targets_dataset_init(
3237 struct pvr_device *device,
3238 struct pvr_render_state *rstate,
3239 const struct pvr_renderpass_hwsetup_render *hw_render)
3240 {
3241 struct pvr_render_target *render_target =
3242 pvr_get_render_target(hw_render, rstate);
3243
3244 return pvr_render_targets_datasets_create(device,
3245 rstate,
3246 hw_render,
3247 render_target);
3248 }
3249
3250 static VkResult
3251 pvr_render_targets_init_for_render(struct pvr_device *device,
3252 struct pvr_render_pass *pass,
3253 struct pvr_framebuffer *framebuffer)
3254 {
3255 for (uint32_t i = 0; i < pass->hw_setup->render_count; i++) {
3256 const struct pvr_renderpass_hwsetup_render *hw_render =
3257 &pass->hw_setup->renders[i];
3258 VkResult result;
3259
3260 result = pvr_render_targets_dataset_init(device,
3261 framebuffer->rstate,
3262 hw_render);
3263 if (result != VK_SUCCESS)
3264 return result;
3265 }
3266
3267 return VK_SUCCESS;
3268 }
3269
3270 const struct pvr_renderpass_hwsetup_subpass *
3271 pvr_arch_get_hw_subpass(const struct pvr_render_pass *pass,
3272 const uint32_t subpass)
3273 {
3274 const struct pvr_renderpass_hw_map *map =
3275 &pass->hw_setup->subpass_map[subpass];
3276
3277 return &pass->hw_setup->renders[map->render].subpasses[map->subpass];
3278 }
3279
3280 static void
3281 pvr_perform_start_of_render_attachment_clear(struct pvr_cmd_buffer *cmd_buffer,
3282 uint32_t layers,
3283 uint32_t index,
3284 bool is_depth_stencil,
3285 uint32_t *index_list_clear_mask,
3286 bool resume_render)
3287 {
3288 ASSERTED static const VkImageAspectFlags dsc_aspect_flags =
3289 VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT |
3290 VK_IMAGE_ASPECT_COLOR_BIT;
3291 struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info;
3292 const struct pvr_render_pass *pass = info->pass;
3293 const struct pvr_renderpass_hwsetup *hw_setup = pass ? pass->hw_setup : NULL;
3294 const uint32_t hw_render_idx =
3295 hw_setup ? hw_setup->subpass_map[info->subpass_idx].render : 0;
3296 const struct pvr_renderpass_hwsetup_render *hw_render =
3297 pvr_arch_pass_info_get_hw_render(info, hw_render_idx);
3298 VkImageAspectFlags image_aspect;
3299 const struct pvr_image *image;
3300 struct pvr_image_view *iview;
3301 VkFormat vk_format;
3302 uint32_t iview_idx;
3303
3304 if (is_depth_stencil) {
3305 assert(hw_render->ds_attach_idx != VK_ATTACHMENT_UNUSED);
3306 iview_idx = hw_render->ds_attach_idx;
3307 } else {
3308 assert(index != VK_ATTACHMENT_UNUSED);
3309 iview_idx = hw_render->color_init[index].index;
3310 }
3311
3312 assert(iview_idx != VK_ATTACHMENT_UNUSED);
3313
3314 iview = info->attachments[iview_idx];
3315 image = pvr_image_view_get_image(iview);
3316 vk_format = image->vk.format;
3317
3318 if (is_depth_stencil) {
3319 bool stencil_clear;
3320 bool depth_clear;
3321 bool is_stencil;
3322 bool is_depth;
3323
3324 assert(index == 0);
3325
3326 depth_clear = hw_render->depth_init == VK_ATTACHMENT_LOAD_OP_CLEAR;
3327 stencil_clear = hw_render->stencil_init == VK_ATTACHMENT_LOAD_OP_CLEAR;
3328 is_stencil = vk_format_has_stencil(vk_format);
3329 is_depth = vk_format_has_depth(vk_format);
3330
3331 /* Attempt to clear the ds attachment. Do not erroneously discard an
3332 * attachment that has no depth clear but has a stencil attachment.
3333 */
3334 /* if not (a ∧ c) ∨ (b ∧ d) */
3335 if (!((is_depth && depth_clear) || (is_stencil && stencil_clear)))
3336 return;
3337 } else if (hw_render->color_init[index].op != VK_ATTACHMENT_LOAD_OP_CLEAR) {
3338 return;
3339 }
3340
3341 /* FIXME: It would be nice if this function and pvr_sub_cmd_gfx_job_init()
3342 * were doing the same check (even if it's just an assert) to determine if a
3343 * clear is needed.
3344 */
3345 /* If this is single-layer fullscreen, we already do the clears in
3346 * pvr_sub_cmd_gfx_job_init().
3347 */
3348 if (pvr_is_render_area_tile_aligned(cmd_buffer, iview) && layers == 1) {
3349 return;
3350 }
3351
3352 image_aspect = vk_format_aspects(vk_format);
3353 assert((image_aspect & ~dsc_aspect_flags) == 0);
3354
3355 if (image_aspect & VK_IMAGE_ASPECT_DEPTH_BIT &&
3356 hw_render->depth_init != VK_ATTACHMENT_LOAD_OP_CLEAR) {
3357 image_aspect &= ~VK_IMAGE_ASPECT_DEPTH_BIT;
3358 }
3359
3360 if (image_aspect & VK_IMAGE_ASPECT_STENCIL_BIT &&
3361 hw_render->stencil_init != VK_ATTACHMENT_LOAD_OP_CLEAR) {
3362 image_aspect &= ~VK_IMAGE_ASPECT_STENCIL_BIT;
3363 }
3364
3365 if (image_aspect != VK_IMAGE_ASPECT_NONE && !resume_render) {
3366 VkClearAttachment clear_attachment = {
3367 .aspectMask = image_aspect,
3368 .colorAttachment = index,
3369 .clearValue = info->clear_values[iview_idx],
3370 };
3371 VkClearRect rect = {
3372 .rect = info->render_area,
3373 .baseArrayLayer = 0,
3374 /* TODO:
3375 *
3376 * Verify where layers should come from, info->framebuffer
3377 * vs layers (function argument). Aren't they the same?
3378 * If they are, drop the argument altogether.
3379 */
3380 .layerCount = info->rstate->layers,
3381 };
3382
3383 assert(iview_idx < info->clear_value_count);
3384
3385 pvr_clear_attachments_render_init(cmd_buffer, &clear_attachment, &rect);
3386
3387 *index_list_clear_mask |= (1 << index);
3388 }
3389 }
3390
3391 static void
3392 pvr_perform_start_of_render_clears(struct pvr_cmd_buffer *cmd_buffer,
3393 bool resume_render)
3394 {
3395 struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info;
3396 const struct pvr_renderpass_hwsetup_render *hw_render;
3397 const struct pvr_render_pass *pass = info->pass;
3398
3399 if (pass) {
3400 const struct pvr_renderpass_hwsetup *hw_setup = pass->hw_setup;
3401
3402 hw_render =
3403 &hw_setup->renders[hw_setup->subpass_map[info->subpass_idx].render];
3404 } else {
3405 hw_render = pvr_arch_pass_info_get_hw_render(info, 0);
3406 }
3407
3408 /* Mask of attachment clears using index lists instead of background object
3409 * to clear.
3410 */
3411 uint32_t index_list_clear_mask = 0;
3412
3413 for (uint32_t i = 0; i < hw_render->color_init_count; i++) {
3414 pvr_perform_start_of_render_attachment_clear(cmd_buffer,
3415 info->rstate->layers,
3416 i,
3417 false,
3418 &index_list_clear_mask,
3419 resume_render);
3420 }
3421
3422 info->enable_bg_tag = !!hw_render->color_init_count;
3423
3424 /* If we're not using index list for all clears/loads then we need to run
3425 * the background object on empty tiles.
3426 */
3427 if (hw_render->color_init_count &&
3428 index_list_clear_mask != ((1u << hw_render->color_init_count) - 1u)) {
3429 info->process_empty_tiles = true;
3430 } else {
3431 info->process_empty_tiles = false;
3432 }
3433
3434 if (hw_render->ds_attach_idx != VK_ATTACHMENT_UNUSED) {
3435 uint32_t ds_index_list = 0;
3436
3437 pvr_perform_start_of_render_attachment_clear(cmd_buffer,
3438 info->rstate->layers,
3439 0,
3440 true,
3441 &ds_index_list,
3442 resume_render);
3443 }
3444 }
3445
3446 static void pvr_stash_depth_format(struct pvr_cmd_buffer_state *state,
3447 struct pvr_sub_cmd_gfx *const sub_cmd)
3448 {
3449 const struct pvr_renderpass_hwsetup_render *hw_render =
3450 pvr_arch_pass_info_get_hw_render(&state->render_pass_info,
3451 sub_cmd->hw_render_idx);
3452
3453 if (hw_render->ds_attach_idx != VK_ATTACHMENT_UNUSED) {
3454 struct pvr_image_view **iviews = state->render_pass_info.attachments;
3455
3456 state->depth_format = iviews[hw_render->ds_attach_idx]->vk.format;
3457 }
3458 }
3459
3460 static bool pvr_loadops_contain_clear(struct pvr_renderpass_hwsetup *hw_setup)
3461 {
3462 for (uint32_t i = 0; i < hw_setup->render_count; i++) {
3463 struct pvr_renderpass_hwsetup_render *hw_render = &hw_setup->renders[i];
3464 uint32_t render_targets_count = hw_render->init_setup.num_render_targets;
3465
3466 for (uint32_t j = 0;
3467 j < (hw_render->color_init_count * render_targets_count);
3468 j += render_targets_count) {
3469 for (uint32_t k = 0; k < hw_render->init_setup.num_render_targets;
3470 k++) {
3471 if (hw_render->color_init[j + k].op ==
3472 VK_ATTACHMENT_LOAD_OP_CLEAR) {
3473 return true;
3474 }
3475 }
3476 }
3477 if (hw_render->depth_init == VK_ATTACHMENT_LOAD_OP_CLEAR ||
3478 hw_render->stencil_init == VK_ATTACHMENT_LOAD_OP_CLEAR) {
3479 return true;
3480 }
3481 }
3482
3483 return false;
3484 }
3485
3486 static void pvr_cmd_buffer_clear_values_free(struct pvr_cmd_buffer *cmd_buffer)
3487 {
3488 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
3489
3490 if (!state->render_pass_info.clear_values) {
3491 assert(!state->render_pass_info.clear_value_count);
3492 return;
3493 }
3494
3495 vk_free(&cmd_buffer->vk.pool->alloc, state->render_pass_info.clear_values);
3496 state->render_pass_info.clear_value_count = 0;
3497 state->render_pass_info.clear_values = NULL;
3498 }
3499
3500 static VkResult
3501 pvr_cmd_buffer_clear_values_alloc(struct pvr_cmd_buffer *cmd_buffer,
3502 uint32_t clear_value_count)
3503 {
3504 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
3505
3506 /* Free any previously allocated clear values. */
3507 pvr_cmd_buffer_clear_values_free(cmd_buffer);
3508
3509 state->render_pass_info.clear_value_count = clear_value_count;
3510 if (!clear_value_count) {
3511 state->render_pass_info.clear_values = NULL;
3512 return VK_SUCCESS;
3513 }
3514
3515 const size_t size =
3516 clear_value_count * sizeof(*state->render_pass_info.clear_values);
3517
3518 state->render_pass_info.clear_values =
3519 vk_zalloc(&cmd_buffer->vk.pool->alloc,
3520 size,
3521 8,
3522 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
3523 if (!state->render_pass_info.clear_values) {
3524 return vk_command_buffer_set_error(&cmd_buffer->vk,
3525 VK_ERROR_OUT_OF_HOST_MEMORY);
3526 }
3527
3528 return VK_SUCCESS;
3529 }
3530
3531 static VkResult
3532 pvr_cmd_buffer_set_clear_values(struct pvr_cmd_buffer *cmd_buffer,
3533 const VkRenderPassBeginInfo *pRenderPassBegin)
3534 {
3535 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
3536 const size_t size = pRenderPassBegin->clearValueCount *
3537 sizeof(*state->render_pass_info.clear_values);
3538 VkResult result;
3539
3540 result =
3541 pvr_cmd_buffer_clear_values_alloc(cmd_buffer,
3542 pRenderPassBegin->clearValueCount);
3543 if (result != VK_SUCCESS)
3544 return result;
3545
3546 memcpy(state->render_pass_info.clear_values,
3547 pRenderPassBegin->pClearValues,
3548 size);
3549
3550 return result;
3551 }
3552
3553 /**
3554 * \brief Indicates whether to use the large or normal clear state words.
3555 *
3556 * If the current render area can fit within a quarter of the max framebuffer
3557 * that the device is capable of, we can use the normal clear state words,
3558 * otherwise the large clear state words are needed.
3559 *
3560 * The requirement of a quarter of the max framebuffer comes from the index
3561 * count used in the normal clear state words and the vertices uploaded at
3562 * device creation.
3563 *
3564 * \param[in] cmd_buffer The command buffer for the clear.
3565 * \return true if large clear state words are required.
3566 */
3567 static bool
3568 pvr_is_large_clear_required(const struct pvr_cmd_buffer *const cmd_buffer)
3569 {
3570 const struct pvr_device_info *const dev_info =
3571 &cmd_buffer->device->pdevice->dev_info;
3572 const VkRect2D render_area = cmd_buffer->state.render_pass_info.render_area;
3573 const uint32_t vf_max_x = rogue_get_param_vf_max_x(dev_info);
3574 const uint32_t vf_max_y = rogue_get_param_vf_max_x(dev_info);
3575
3576 return (render_area.extent.width > (vf_max_x / 2) - 1) ||
3577 (render_area.extent.height > (vf_max_y / 2) - 1);
3578 }
3579
3580 static void pvr_emit_clear_words(struct pvr_cmd_buffer *const cmd_buffer,
3581 struct pvr_sub_cmd_gfx *const sub_cmd)
3582 {
3583 struct pvr_device *device = cmd_buffer->device;
3584 struct pvr_csb *csb = &sub_cmd->control_stream;
3585 uint32_t vdm_state_size_in_dw;
3586 const uint32_t *vdm_state;
3587 uint32_t *stream;
3588
3589 vdm_state_size_in_dw =
3590 pvr_clear_vdm_state_get_size_in_dw(&device->pdevice->dev_info, 1);
3591
3592 pvr_csb_set_relocation_mark(csb);
3593
3594 stream = pvr_arch_csb_alloc_dwords(csb, vdm_state_size_in_dw);
3595 if (!stream) {
3596 pvr_cmd_buffer_set_error_unwarned(cmd_buffer, csb->status);
3597 return;
3598 }
3599
3600 if (pvr_is_large_clear_required(cmd_buffer))
3601 vdm_state = device->static_clear_state->large_clear_vdm_words;
3602 else
3603 vdm_state = device->static_clear_state->vdm_words;
3604
3605 memcpy(stream, vdm_state, PVR_DW_TO_BYTES(vdm_state_size_in_dw));
3606
3607 pvr_csb_clear_relocation_mark(csb);
3608 }
3609
3610 static VkResult pvr_cs_write_load_op_for_view(struct pvr_cmd_buffer *cmd_buffer,
3611 struct pvr_sub_cmd_gfx *sub_cmd,
3612 struct pvr_load_op *load_op,
3613 uint32_t view_index,
3614 uint32_t isp_userpass)
3615 {
3616 const struct pvr_device *device = cmd_buffer->device;
3617 struct pvr_static_clear_ppp_template template =
3618 device->static_clear_state->ppp_templates[VK_IMAGE_ASPECT_COLOR_BIT];
3619 uint32_t pds_state[PVR_PDS_STATE_LENGTH];
3620 struct pvr_pds_upload shareds_update_program;
3621 struct pvr_suballoc_bo *pvr_bo;
3622 VkResult result;
3623
3624 result = pvr_load_op_data_create_and_upload(cmd_buffer,
3625 load_op,
3626 view_index,
3627 &shareds_update_program);
3628 if (result != VK_SUCCESS)
3629 return result;
3630
3631 template.config.ispctl.upass = isp_userpass;
3632
3633 /* It might look odd that we aren't specifying the code segment's
3634 * address anywhere. This is because the hardware always assumes that the
3635 * data size is 2 128bit words and the code segments starts after that.
3636 */
3637 pvr_csb_pack (&pds_state[PVR_STATIC_CLEAR_PPP_PDS_TYPE_SHADERBASE],
3638 TA_STATE_PDS_SHADERBASE,
3639 shaderbase) {
3640 shaderbase.addr = PVR_DEV_ADDR(load_op->pds_frag_prog.data_offset);
3641 }
3642
3643 pvr_csb_pack (&pds_state[PVR_STATIC_CLEAR_PPP_PDS_TYPE_TEXUNICODEBASE],
3644 TA_STATE_PDS_TEXUNICODEBASE,
3645 texunicodebase) {
3646 texunicodebase.addr =
3647 PVR_DEV_ADDR(load_op->pds_tex_state_prog.code_offset);
3648 }
3649
3650 pvr_csb_pack (&pds_state[PVR_STATIC_CLEAR_PPP_PDS_TYPE_SIZEINFO1],
3651 TA_STATE_PDS_SIZEINFO1,
3652 sizeinfo1) {
3653 /* Dummy coefficient loading program. */
3654 sizeinfo1.pds_varyingsize = 0;
3655
3656 sizeinfo1.pds_texturestatesize = DIV_ROUND_UP(
3657 shareds_update_program.data_size,
3658 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_TEXTURESTATESIZE_UNIT_SIZE);
3659
3660 sizeinfo1.pds_tempsize =
3661 DIV_ROUND_UP(load_op->temps_count,
3662 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_TEMPSIZE_UNIT_SIZE);
3663 }
3664
3665 pvr_csb_pack (&pds_state[PVR_STATIC_CLEAR_PPP_PDS_TYPE_SIZEINFO2],
3666 TA_STATE_PDS_SIZEINFO2,
3667 sizeinfo2) {
3668 sizeinfo2.usc_sharedsize =
3669 DIV_ROUND_UP(load_op->const_shareds_count,
3670 ROGUE_TA_STATE_PDS_SIZEINFO2_USC_SHAREDSIZE_UNIT_SIZE);
3671 }
3672
3673 /* Dummy coefficient loading program. */
3674 pds_state[PVR_STATIC_CLEAR_PPP_PDS_TYPE_VARYINGBASE] = 0;
3675
3676 pvr_csb_pack (&pds_state[PVR_STATIC_CLEAR_PPP_PDS_TYPE_TEXTUREDATABASE],
3677 TA_STATE_PDS_TEXTUREDATABASE,
3678 texturedatabase) {
3679 texturedatabase.addr = PVR_DEV_ADDR(shareds_update_program.data_offset);
3680 }
3681
3682 template.config.pds_state = &pds_state;
3683
3684 pvr_emit_ppp_from_template(&sub_cmd->control_stream, &template, &pvr_bo);
3685 list_add(&pvr_bo->link, &cmd_buffer->bo_list);
3686
3687 pvr_emit_clear_words(cmd_buffer, sub_cmd);
3688
3689 pvr_arch_reset_graphics_dirty_state(cmd_buffer, false);
3690
3691 return VK_SUCCESS;
3692 }
3693
3694 static VkResult pvr_cs_write_load_op(struct pvr_cmd_buffer *cmd_buffer,
3695 struct pvr_sub_cmd_gfx *sub_cmd,
3696 struct pvr_load_op *load_op,
3697 uint32_t isp_userpass)
3698 {
3699 assert(load_op->view_count);
3700
3701 for (uint32_t i = 0; i < load_op->view_count; i++) {
3702 const uint32_t view_index = load_op->view_indices[i];
3703 VkResult result;
3704
3705 result = pvr_cs_write_load_op_for_view(cmd_buffer,
3706 sub_cmd,
3707 load_op,
3708 view_index,
3709 isp_userpass);
3710 if (result != VK_SUCCESS)
3711 return result;
3712 }
3713
3714 return VK_SUCCESS;
3715 }
3716
3717 static inline enum pvr_resolve_op
3718 pvr_resolve_mode_to_op(enum VkResolveModeFlagBits mode)
3719 {
3720 switch (mode) {
3721 default:
3722 UNREACHABLE("invalid resolve mode");
3723 FALLTHROUGH;
3724 case VK_RESOLVE_MODE_AVERAGE_BIT:
3725 return PVR_RESOLVE_BLEND;
3726 case VK_RESOLVE_MODE_SAMPLE_ZERO_BIT:
3727 return PVR_RESOLVE_SAMPLE0;
3728 case VK_RESOLVE_MODE_MIN_BIT:
3729 return PVR_RESOLVE_MIN;
3730 case VK_RESOLVE_MODE_MAX_BIT:
3731 return PVR_RESOLVE_MAX;
3732 }
3733 }
3734
3735 static inline void
3736 pvr_resolve_subresource_layer_init(const struct pvr_image_view *const iview,
3737 VkImageSubresourceLayers *subresource)
3738 {
3739 *subresource = (VkImageSubresourceLayers){
3740 .mipLevel = iview->vk.base_mip_level,
3741 .baseArrayLayer = iview->vk.base_array_layer,
3742 .layerCount = iview->vk.layer_count,
3743 };
3744 }
3745
3746 static inline void
3747 pvr_resolve_offset_init(const struct pvr_render_pass_info *const info,
3748 VkOffset3D *offset)
3749 {
3750 *offset = (VkOffset3D){
3751 .x = info->render_area.offset.x,
3752 .y = info->render_area.offset.y,
3753 };
3754 }
3755
3756 /* clang-format off */
3757 static inline void
3758 pvr_resolve_image_copy_region_init(const struct pvr_image_view *const src_view,
3759 const struct pvr_image_view *const dst_view,
3760 const struct pvr_render_pass_info *const info,
3761 VkImageCopy2 *region)
3762 {
3763 pvr_resolve_subresource_layer_init(src_view, &region->srcSubresource);
3764 pvr_resolve_subresource_layer_init(dst_view, &region->dstSubresource);
3765 pvr_resolve_offset_init(info, &region->srcOffset);
3766 pvr_resolve_offset_init(info, &region->dstOffset);
3767
3768 region->extent = (VkExtent3D){
3769 .width = info->render_area.extent.width,
3770 .height = info->render_area.extent.height,
3771 .depth = 1
3772 };
3773 }
3774 /* clang-format on */
3775
3776 static VkResult
3777 pvr_resolve_unemitted_resolve_attachments(struct pvr_cmd_buffer *cmd_buffer,
3778 struct pvr_render_pass_info *info)
3779 {
3780 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
3781 const struct pvr_renderpass_hwsetup_render *hw_render =
3782 pvr_arch_pass_info_get_hw_render(&state->render_pass_info,
3783 info->current_hw_subpass);
3784
3785 for (uint32_t i = 0U; i < hw_render->eot_surface_count; i++) {
3786 const struct pvr_renderpass_hwsetup_eot_surface *surface =
3787 &hw_render->eot_surfaces[i];
3788 const uint32_t color_attach_idx = surface->src_attachment_idx;
3789 const uint32_t resolve_attach_idx = surface->attachment_idx;
3790 struct pvr_image_view *dst_view;
3791 struct pvr_image_view *src_view;
3792 VkFormat src_format;
3793 VkFormat dst_format;
3794 VkImageCopy2 region;
3795 VkResult result;
3796
3797 if (!surface->need_resolve ||
3798 surface->resolve_type != PVR_RESOLVE_TYPE_TRANSFER) {
3799 continue;
3800 }
3801
3802 dst_view = info->attachments[resolve_attach_idx];
3803 src_view = info->attachments[color_attach_idx];
3804
3805 pvr_resolve_image_copy_region_init(src_view, dst_view, info, &region);
3806 region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
3807 region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
3808
3809 /* TODO: if ERN_46863 is supported, Depth and stencil are sampled
3810 * separately from images with combined depth+stencil. Add logic here to
3811 * handle it using appropriate format from image view.
3812 */
3813 src_format = src_view->vk.image->format;
3814 dst_format = dst_view->vk.image->format;
3815 src_view->vk.image->format = src_view->vk.format;
3816 dst_view->vk.image->format = dst_view->vk.format;
3817
3818 result = pvr_copy_or_resolve_color_image_region(
3819 cmd_buffer,
3820 vk_to_pvr_image(src_view->vk.image),
3821 vk_to_pvr_image(dst_view->vk.image),
3822 &region);
3823
3824 src_view->vk.image->format = src_format;
3825 dst_view->vk.image->format = dst_format;
3826
3827 state->current_sub_cmd->transfer.serialize_with_frag = true;
3828
3829 if (result != VK_SUCCESS)
3830 return result;
3831 }
3832
3833 if (hw_render->stencil_resolve_mode || hw_render->depth_resolve_mode) {
3834 const struct pvr_image_view *dst_view =
3835 info->attachments[hw_render->ds_attach_resolve_idx];
3836 const struct pvr_image_view *src_view =
3837 info->attachments[hw_render->ds_attach_idx];
3838 const VkClearValue *resolve_clear_values =
3839 &state->render_pass_info.clear_values[hw_render->ds_attach_resolve_idx];
3840 const bool both_stencil = vk_format_has_stencil(dst_view->vk.format) &&
3841 vk_format_has_stencil(src_view->vk.format);
3842 const bool both_depth = vk_format_has_depth(dst_view->vk.format) &&
3843 vk_format_has_depth(src_view->vk.format);
3844 struct pvr_render_pass_attachment resolve_attachment;
3845 bool clear_depth = false, clear_stencil = false;
3846 VkClearDepthStencilValue clear_values;
3847 VkImageCopy2 region;
3848
3849 pvr_resolve_image_copy_region_init(src_view, dst_view, info, &region);
3850
3851 if (!info->dr_info) {
3852 resolve_attachment =
3853 info->pass->attachments[hw_render->ds_attach_resolve_idx];
3854
3855 if (both_depth && !hw_render->depth_resolve_mode &&
3856 resolve_attachment.store_op == VK_ATTACHMENT_STORE_OP_STORE &&
3857 resolve_attachment.load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
3858 clear_values.depth = resolve_clear_values->depthStencil.depth;
3859 clear_depth = true;
3860 }
3861
3862 if (both_stencil && !hw_render->stencil_resolve_mode &&
3863 resolve_attachment.stencil_store_op ==
3864 VK_ATTACHMENT_STORE_OP_STORE &&
3865 resolve_attachment.stencil_load_op ==
3866 VK_ATTACHMENT_LOAD_OP_CLEAR) {
3867 clear_values.stencil = resolve_clear_values->depthStencil.stencil;
3868 clear_stencil = true;
3869 }
3870 }
3871
3872 /* Resolve depth and if it can clear stencil */
3873 if (both_depth && hw_render->depth_resolve_mode) {
3874 region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
3875 region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
3876
3877 pvr_copy_or_resolve_depth_stencil_region(
3878 cmd_buffer,
3879 vk_to_pvr_image(src_view->vk.image),
3880 vk_to_pvr_image(dst_view->vk.image),
3881 pvr_resolve_mode_to_op(hw_render->depth_resolve_mode),
3882 clear_stencil,
3883 &clear_values,
3884 &region);
3885
3886 state->current_sub_cmd->transfer.serialize_with_frag = true;
3887 clear_stencil = false;
3888 }
3889
3890 /* Resolve stencil and if it can clear depth */
3891 if (both_stencil && hw_render->stencil_resolve_mode) {
3892 region.srcSubresource.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
3893 region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
3894
3895 pvr_copy_or_resolve_depth_stencil_region(
3896 cmd_buffer,
3897 vk_to_pvr_image(src_view->vk.image),
3898 vk_to_pvr_image(dst_view->vk.image),
3899 pvr_resolve_mode_to_op(hw_render->stencil_resolve_mode),
3900 clear_depth,
3901 &clear_values,
3902 &region);
3903
3904 state->current_sub_cmd->transfer.serialize_with_frag = true;
3905 clear_depth = false;
3906 }
3907
3908 /* Clear what it couldn't be cleared yet */
3909 if (clear_stencil || clear_depth) {
3910 const VkImageAspectFlagBits aspect =
3911 (clear_depth ? VK_IMAGE_ASPECT_DEPTH_BIT : 0) |
3912 (clear_stencil ? VK_IMAGE_ASPECT_STENCIL_BIT : 0);
3913 VkImageSubresourceRange range = (VkImageSubresourceRange){
3914 .aspectMask = aspect,
3915 .baseMipLevel = dst_view->vk.base_mip_level,
3916 .levelCount = 1,
3917 .baseArrayLayer = dst_view->vk.base_array_layer,
3918 .layerCount = dst_view->vk.layer_count,
3919 };
3920
3921 pvr_clear_depth_stencil_image(cmd_buffer,
3922 vk_to_pvr_image(dst_view->vk.image),
3923 &clear_values,
3924 1,
3925 &range);
3926
3927 state->current_sub_cmd->transfer.serialize_with_frag = true;
3928 }
3929 }
3930
3931 return pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
3932 }
3933
3934 void PVR_PER_ARCH(CmdBeginRenderPass2)(
3935 VkCommandBuffer commandBuffer,
3936 const VkRenderPassBeginInfo *pRenderPassBeginInfo,
3937 const VkSubpassBeginInfo *pSubpassBeginInfo)
3938 {
3939 VK_FROM_HANDLE(pvr_framebuffer,
3940 framebuffer,
3941 pRenderPassBeginInfo->framebuffer);
3942 VK_FROM_HANDLE(pvr_render_pass, pass, pRenderPassBeginInfo->renderPass);
3943 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
3944 const struct pvr_renderpass_hwsetup_subpass *hw_subpass;
3945 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
3946 VkResult result;
3947
3948 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
3949
3950 assert(!state->render_pass_info.pass);
3951 assert(cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
3952
3953 /* FIXME: Create a separate function for everything using pass->subpasses,
3954 * look at cmd_buffer_begin_subpass() for example. */
3955 state->render_pass_info.pass = pass;
3956 state->render_pass_info.framebuffer = framebuffer;
3957 state->render_pass_info.rstate = framebuffer->rstate;
3958 state->render_pass_info.subpass_idx = 0;
3959 state->render_pass_info.render_area = pRenderPassBeginInfo->renderArea;
3960 state->render_pass_info.current_hw_subpass = 0;
3961 state->render_pass_info.pipeline_bind_point =
3962 pass->subpasses[0].pipeline_bind_point;
3963 state->render_pass_info.isp_userpass = pass->subpasses[0].isp_userpass;
3964 state->dirty.isp_userpass = true;
3965
3966 result = pvr_cmd_buffer_attachments_setup(cmd_buffer,
3967 pass,
3968 framebuffer,
3969 pRenderPassBeginInfo);
3970 if (result != VK_SUCCESS)
3971 return;
3972
3973 result =
3974 pvr_render_targets_init_for_render(cmd_buffer->device, pass, framebuffer);
3975 if (result != VK_SUCCESS) {
3976 pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
3977 return;
3978 }
3979
3980 result = pvr_cmd_buffer_set_clear_values(cmd_buffer, pRenderPassBeginInfo);
3981 if (result != VK_SUCCESS)
3982 return;
3983
3984 assert(pass->subpasses[0].pipeline_bind_point ==
3985 VK_PIPELINE_BIND_POINT_GRAPHICS);
3986
3987 result =
3988 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_GRAPHICS);
3989 if (result != VK_SUCCESS)
3990 return;
3991
3992 /* Run subpass 0 "soft" background object after the actual background
3993 * object.
3994 */
3995 hw_subpass = pvr_arch_get_hw_subpass(pass, 0);
3996 if (hw_subpass->load_op) {
3997 result = pvr_cs_write_load_op(cmd_buffer,
3998 &cmd_buffer->state.current_sub_cmd->gfx,
3999 hw_subpass->load_op,
4000 0);
4001 if (result != VK_SUCCESS)
4002 return;
4003 }
4004
4005 pvr_perform_start_of_render_clears(cmd_buffer, false);
4006 pvr_stash_depth_format(&cmd_buffer->state,
4007 &cmd_buffer->state.current_sub_cmd->gfx);
4008 }
4009
4010 static inline uint32_t
4011 pvr_get_ds_component_bits(const VkFormat vk_format,
4012 const VkImageAspectFlags ds_aspect)
4013 {
4014 uint32_t component;
4015
4016 if (ds_aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
4017 assert(vk_format_has_stencil(vk_format));
4018 component = 0;
4019 } else {
4020 assert(ds_aspect == VK_IMAGE_ASPECT_DEPTH_BIT);
4021 assert(vk_format_has_depth(vk_format));
4022 component = 1;
4023 }
4024
4025 return vk_format_get_component_bits(vk_format,
4026 UTIL_FORMAT_COLORSPACE_ZS,
4027 component);
4028 }
4029
4030 static inline bool
4031 pvr_can_pbe_resolve_ds_attachment(const struct pvr_device_info *dev_info,
4032 VkFormat vk_format,
4033 uint32_t sample_count,
4034 VkResolveModeFlagBits resolve_mode)
4035 {
4036 if (!PVR_HAS_FEATURE(dev_info, gs_rta_support))
4037 return false;
4038
4039 if (resolve_mode != VK_RESOLVE_MODE_AVERAGE_BIT)
4040 return false;
4041
4042 if (pvr_get_ds_component_bits(vk_format, VK_IMAGE_ASPECT_STENCIL_BIT))
4043 return false;
4044
4045 return pvr_arch_format_is_pbe_downscalable(dev_info, vk_format);
4046 }
4047
4048 static void pvr_dynamic_rendering_output_attachments_cleanup(
4049 const struct pvr_device *device,
4050 const VkAllocationCallbacks *const allocator,
4051 struct pvr_dynamic_render_info *dr_info)
4052 {
4053 if (!dr_info)
4054 return;
4055
4056 pvr_arch_mrt_load_op_state_cleanup(device,
4057 allocator,
4058 dr_info->hw_render.load_op_state);
4059
4060 pvr_arch_destroy_mrt_setup(device, &dr_info->hw_render.eot_setup);
4061 pvr_arch_destroy_mrt_setup(device, &dr_info->hw_render.init_setup);
4062 pvr_arch_destroy_mrt_setup(device, dr_info->mrt_setup);
4063
4064 vk_free2(&device->vk.alloc, allocator, dr_info->hw_render.eot_surfaces);
4065 vk_free2(&device->vk.alloc, allocator, dr_info->hw_render.color_init);
4066 vk_free2(&device->vk.alloc, allocator, dr_info->color_attachments);
4067 }
4068
4069 static VkResult pvr_dynamic_rendering_output_attachments_setup(
4070 struct pvr_cmd_buffer *cmd_buffer,
4071 const VkRenderingInfo *pRenderingInfo,
4072 struct pvr_dynamic_render_info *dr_info,
4073 uint32_t *attach_idx)
4074 {
4075 VkFormat attachment_formats[pRenderingInfo->colorAttachmentCount];
4076 uint32_t mrt_attachment_map[pRenderingInfo->colorAttachmentCount];
4077 uint32_t mrt_count = 0, eot_mrt_count = 0, eot_mrt_idx = 0,
4078 eot_surface_idx = 0, color_idx = 0, pbe_emits = 0,
4079 init_setup_idx = 0;
4080 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
4081 struct pvr_device *device = cmd_buffer->device;
4082 struct usc_mrt_setup mrt_setup;
4083 VkResult result;
4084
4085 dr_info->color_attachment_count = pRenderingInfo->colorAttachmentCount;
4086
4087 if (dr_info->color_attachment_count) {
4088 const uint32_t size =
4089 sizeof(*dr_info->color_attachments) * dr_info->color_attachment_count;
4090
4091 dr_info->color_attachments = vk_zalloc(&device->vk.alloc,
4092 size,
4093 8,
4094 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4095 if (!dr_info->color_attachments) {
4096 return vk_command_buffer_set_error(&cmd_buffer->vk,
4097 VK_ERROR_OUT_OF_HOST_MEMORY);
4098 }
4099 }
4100
4101 for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) {
4102 uint32_t cur_attach_idx = *attach_idx;
4103 struct pvr_render_pass_attachment *attachment =
4104 &dr_info->attachments[cur_attach_idx];
4105 const VkRenderingAttachmentInfo *attachment_info =
4106 &pRenderingInfo->pColorAttachments[i];
4107 const struct pvr_image *image;
4108 struct pvr_image_view *iview;
4109
4110 /* Start off clean */
4111 dr_info->color_attachments[i].index_color = VK_ATTACHMENT_UNUSED;
4112 dr_info->color_attachments[i].index_resolve = VK_ATTACHMENT_UNUSED;
4113 attachment_formats[mrt_count] = VK_FORMAT_UNDEFINED;
4114 mrt_attachment_map[i] = VK_ATTACHMENT_UNUSED;
4115
4116 if (attachment_info->imageView)
4117 iview = pvr_image_view_from_handle(attachment_info->imageView);
4118 else if (attachment_info->resolveImageView)
4119 iview = pvr_image_view_from_handle(attachment_info->resolveImageView);
4120 else
4121 continue;
4122
4123 /* We have an output color attachment */
4124 image = pvr_image_view_get_image(iview);
4125
4126 state->render_pass_info.attachments[cur_attach_idx] = iview;
4127 state->render_pass_info.rstate->height =
4128 MIN2(state->render_pass_info.rstate->height, image->vk.extent.height);
4129 state->render_pass_info.rstate->width =
4130 MIN2(state->render_pass_info.rstate->width, image->vk.extent.width);
4131
4132 mrt_attachment_map[i] = mrt_count;
4133 attachment_formats[mrt_count++] = iview->vk.view_format;
4134
4135 attachment->index = cur_attach_idx;
4136 attachment->vk_format = iview->vk.view_format;
4137 attachment->sample_count = image->vk.samples;
4138 attachment->resolve_target = VK_ATTACHMENT_UNUSED;
4139 attachment->load_op = attachment_info->loadOp;
4140 attachment->store_op = attachment_info->storeOp;
4141 attachment->resolve_mode = attachment_info->resolveMode;
4142 attachment->stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
4143 attachment->stencil_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
4144 attachment->stencil_resolve_mode = VK_RESOLVE_MODE_NONE;
4145 attachment->need_eot = true;
4146 /* TODO: verify logic for is_pbe_downscalable */
4147 attachment->is_pbe_downscalable = pvr_can_pbe_resolve_ds_attachment(
4148 &cmd_buffer->device->pdevice->dev_info,
4149 iview->vk.view_format,
4150 image->vk.samples,
4151 attachment_info->resolveMode);
4152
4153 dr_info->color_attachments[i].index_color = cur_attach_idx;
4154 dr_info->hw_render.sample_count =
4155 MAX2(dr_info->hw_render.sample_count, attachment->sample_count);
4156
4157 *attach_idx += 1;
4158
4159 if (attachment->resolve_mode &&
4160 cur_attach_idx < dr_info->attachment_count) {
4161 const uint32_t resolve_attach_idx = *attach_idx;
4162 struct pvr_render_pass_attachment *resolve_attach =
4163 &dr_info->attachments[resolve_attach_idx];
4164 struct pvr_image_view *resolve_iview =
4165 pvr_image_view_from_handle(attachment_info->resolveImageView);
4166
4167 assert(resolve_iview &&
4168 resolve_attach_idx <= dr_info->attachment_count);
4169
4170 state->render_pass_info.attachments[resolve_attach_idx] =
4171 resolve_iview;
4172
4173 attachment->resolve_target = resolve_attach_idx;
4174
4175 dr_info->color_attachments[i].index_resolve = resolve_attach_idx;
4176 dr_info->hw_render.has_side_effects = true;
4177 dr_info->hw_render.requires_frag_pr = true;
4178 dr_info->hw_render.eot_surface_count++;
4179
4180 resolve_attach->sample_count = image->vk.samples;
4181 resolve_attach->index = resolve_attach_idx;
4182 resolve_attach->vk_format = resolve_iview->vk.view_format;
4183 resolve_attach->load_op = resolve_attach->stencil_load_op =
4184 VK_ATTACHMENT_LOAD_OP_DONT_CARE;
4185 resolve_attach->store_op = resolve_attach->stencil_store_op =
4186 VK_ATTACHMENT_STORE_OP_DONT_CARE;
4187 resolve_attach->resolve_mode = resolve_attach->stencil_resolve_mode =
4188 VK_RESOLVE_MODE_NONE;
4189 resolve_attach->resolve_target = VK_ATTACHMENT_UNUSED;
4190
4191 /* If resolving with a tranfer, then always store the source */
4192 if (!attachment->is_pbe_downscalable)
4193 attachment->store_op = VK_ATTACHMENT_STORE_OP_STORE;
4194
4195 *attach_idx += 1;
4196 }
4197
4198 if (attachment->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
4199 VkClearColorValue *clear_value =
4200 &state->render_pass_info.clear_values[cur_attach_idx].color;
4201
4202 *clear_value = attachment_info->clearValue.color;
4203
4204 if (attachment->store_op == VK_ATTACHMENT_STORE_OP_STORE)
4205 dr_info->hw_render.has_side_effects = true;
4206 }
4207
4208 if (attachment->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR ||
4209 attachment->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
4210 dr_info->hw_render.color_init_count++;
4211 }
4212
4213 if (attachment->store_op == VK_ATTACHMENT_STORE_OP_STORE)
4214 dr_info->hw_render.eot_surface_count++;
4215 else
4216 dr_info->hw_render.requires_frag_pr = true;
4217
4218 if (attachment->store_op == VK_ATTACHMENT_STORE_OP_STORE ||
4219 attachment->resolve_mode) {
4220 eot_mrt_count++;
4221 }
4222 }
4223
4224 if (dr_info->hw_render.color_init_count) {
4225 const uint32_t size = dr_info->hw_render.color_init_count *
4226 sizeof(*dr_info->hw_render.color_init);
4227
4228 dr_info->hw_render.color_init =
4229 vk_zalloc(&device->vk.alloc,
4230 size,
4231 8,
4232 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4233 if (!dr_info->hw_render.color_init) {
4234 result = vk_command_buffer_set_error(&cmd_buffer->vk,
4235 VK_ERROR_OUT_OF_HOST_MEMORY);
4236 goto err_free_color_attachments;
4237 }
4238 }
4239
4240 if (dr_info->hw_render.eot_surface_count) {
4241 const uint32_t size = dr_info->hw_render.eot_surface_count *
4242 sizeof(*dr_info->hw_render.eot_surfaces);
4243
4244 dr_info->hw_render.eot_surfaces =
4245 vk_zalloc(&device->vk.alloc,
4246 size,
4247 8,
4248 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4249 if (!dr_info->hw_render.eot_surfaces) {
4250 result = vk_command_buffer_set_error(&cmd_buffer->vk,
4251 VK_ERROR_OUT_OF_HOST_MEMORY);
4252 goto err_free_hw_render_color_init;
4253 }
4254 }
4255
4256 result = pvr_arch_init_usc_mrt_setup(device,
4257 mrt_count,
4258 attachment_formats,
4259 &mrt_setup);
4260 if (result != VK_SUCCESS) {
4261 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4262 goto err_free_hw_render_eot_surface;
4263 }
4264
4265 dr_info->hw_render.tile_buffers_count = mrt_setup.num_tile_buffers;
4266 dr_info->hw_render.output_regs_count = mrt_setup.num_output_regs;
4267
4268 result =
4269 pvr_arch_mrt_setup_partial_init(cmd_buffer->device,
4270 dr_info->mrt_setup,
4271 pRenderingInfo->colorAttachmentCount,
4272 dr_info->hw_render.output_regs_count,
4273 dr_info->hw_render.tile_buffers_count);
4274 if (result != VK_SUCCESS) {
4275 pvr_arch_destroy_mrt_setup(device, &mrt_setup);
4276 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4277 goto err_free_hw_render_eot_surface;
4278 }
4279
4280 /* We fully initialised only a mrt_count number of render targets,
4281 * now scatter gather them over the whole number of attachments.
4282 * This is necessary because render targets are attachment-indexed
4283 * throughout the driver.
4284 */
4285 for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) {
4286 if (mrt_attachment_map[i] != VK_ATTACHMENT_UNUSED) {
4287 memcpy(&dr_info->mrt_setup->mrt_resources[i],
4288 &mrt_setup.mrt_resources[mrt_attachment_map[i]],
4289 sizeof(mrt_setup.mrt_resources[0]));
4290 }
4291 }
4292
4293 result =
4294 pvr_arch_mrt_setup_partial_init(cmd_buffer->device,
4295 &dr_info->hw_render.init_setup,
4296 dr_info->hw_render.color_init_count,
4297 dr_info->hw_render.output_regs_count,
4298 dr_info->hw_render.tile_buffers_count);
4299 if (result != VK_SUCCESS) {
4300 pvr_arch_destroy_mrt_setup(device, &mrt_setup);
4301 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4302 goto err_finish_mrt_setup;
4303 }
4304
4305 result =
4306 pvr_arch_mrt_setup_partial_init(cmd_buffer->device,
4307 &dr_info->hw_render.eot_setup,
4308 eot_mrt_count,
4309 dr_info->hw_render.output_regs_count,
4310 dr_info->hw_render.tile_buffers_count);
4311 if (result != VK_SUCCESS) {
4312 pvr_arch_destroy_mrt_setup(device, &mrt_setup);
4313 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4314 goto err_finish_mrt_init_setup;
4315 }
4316
4317 for (uint32_t i = 0; i < dr_info->attachment_count; i++) {
4318 struct pvr_render_pass_attachment *attachment = &dr_info->attachments[i];
4319 bool need_eot_setup = false;
4320
4321 if (!attachment->need_eot)
4322 continue;
4323
4324 if (attachment->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR ||
4325 attachment->load_op == VK_ATTACHMENT_LOAD_OP_LOAD) {
4326 dr_info->hw_render.color_init[init_setup_idx].index = i;
4327 dr_info->hw_render.color_init[init_setup_idx].op = attachment->load_op;
4328 dr_info->hw_render.init_setup.mrt_resources[init_setup_idx] =
4329 mrt_setup.mrt_resources[color_idx];
4330 init_setup_idx++;
4331 }
4332
4333 if (attachment->store_op == VK_ATTACHMENT_STORE_OP_STORE ||
4334 (attachment->resolve_mode && !attachment->is_pbe_downscalable)) {
4335 struct pvr_renderpass_hwsetup_eot_surface *surface =
4336 &dr_info->hw_render.eot_surfaces[eot_surface_idx];
4337
4338 surface->src_attachment_idx = VK_ATTACHMENT_UNUSED;
4339 surface->resolve_type = PVR_RESOLVE_TYPE_INVALID;
4340 surface->mrt_idx = eot_mrt_idx;
4341 surface->attachment_idx = i;
4342
4343 need_eot_setup = true;
4344 eot_surface_idx++;
4345 pbe_emits++;
4346 }
4347
4348 if (attachment->resolve_mode) {
4349 struct pvr_renderpass_hwsetup_eot_surface *surface =
4350 &dr_info->hw_render.eot_surfaces[eot_surface_idx]; /* next surface
4351 */
4352
4353 surface->src_attachment_idx = i;
4354 surface->mrt_idx = eot_mrt_idx;
4355 surface->need_resolve = true;
4356 surface->attachment_idx = attachment->resolve_target;
4357
4358 if (attachment->store_op == VK_ATTACHMENT_STORE_OP_DONT_CARE &&
4359 attachment->is_pbe_downscalable) {
4360 surface->resolve_type = PVR_RESOLVE_TYPE_PBE;
4361 pbe_emits++;
4362 } else if (!attachment->is_pbe_downscalable) {
4363 surface->resolve_type = PVR_RESOLVE_TYPE_TRANSFER;
4364 } else {
4365 surface->resolve_type = PVR_RESOLVE_TYPE_INVALID;
4366 }
4367
4368 need_eot_setup = true;
4369 eot_surface_idx++;
4370 }
4371
4372 if (need_eot_setup) {
4373 dr_info->hw_render.eot_setup.mrt_resources[eot_mrt_idx++] =
4374 mrt_setup.mrt_resources[color_idx];
4375 }
4376
4377 color_idx++;
4378 }
4379
4380 assert(dr_info->hw_render.eot_surface_count == eot_surface_idx);
4381 assert(pbe_emits <= PVR_MAX_COLOR_ATTACHMENTS);
4382
4383 for (uint32_t i = 0; i < eot_surface_idx; i++) {
4384 struct pvr_renderpass_hwsetup_eot_surface *surface =
4385 &dr_info->hw_render.eot_surfaces[i];
4386 struct pvr_render_pass_attachment *attachment;
4387
4388 if (!surface->need_resolve ||
4389 surface->resolve_type != PVR_RESOLVE_TYPE_INVALID) {
4390 continue;
4391 }
4392
4393 assert(surface->src_attachment_idx != VK_ATTACHMENT_UNUSED);
4394
4395 attachment = &dr_info->attachments[surface->src_attachment_idx];
4396 if (attachment->resolve_mode) {
4397 if (pbe_emits < PVR_MAX_COLOR_ATTACHMENTS) {
4398 surface->resolve_type = PVR_RESOLVE_TYPE_PBE;
4399 pbe_emits++;
4400 } else {
4401 surface->resolve_type = PVR_RESOLVE_TYPE_TRANSFER;
4402 }
4403 }
4404 }
4405
4406 dr_info->hw_render.pbe_emits = pbe_emits;
4407
4408 pvr_arch_destroy_mrt_setup(device, &mrt_setup);
4409
4410 return VK_SUCCESS;
4411
4412 err_finish_mrt_init_setup:
4413 pvr_arch_destroy_mrt_setup(device, &dr_info->hw_render.init_setup);
4414
4415 err_finish_mrt_setup:
4416 pvr_arch_destroy_mrt_setup(device, dr_info->mrt_setup);
4417
4418 err_free_hw_render_eot_surface:
4419 vk_free(&device->vk.alloc, dr_info->hw_render.eot_surfaces);
4420
4421 err_free_hw_render_color_init:
4422 vk_free(&device->vk.alloc, dr_info->hw_render.color_init);
4423
4424 err_free_color_attachments:
4425 vk_free(&device->vk.alloc, dr_info->color_attachments);
4426
4427 return result;
4428 }
4429
4430 static void
4431 pvr_dynamic_render_info_destroy(const struct pvr_device *device,
4432 struct pvr_cmd_buffer *cmd_buffer,
4433 struct pvr_dynamic_render_info *dr_info)
4434 {
4435 pvr_dynamic_rendering_output_attachments_cleanup(device,
4436 &device->vk.alloc,
4437 dr_info);
4438
4439 pvr_cmd_buffer_clear_values_free(cmd_buffer);
4440 pvr_cmd_buffer_attachments_free(cmd_buffer);
4441
4442 if (dr_info)
4443 vk_free(&device->vk.alloc, dr_info->attachments);
4444
4445 vk_free(&device->vk.alloc, dr_info);
4446 }
4447
4448 static VkResult
4449 pvr_dynamic_render_info_create(struct pvr_cmd_buffer *cmd_buffer,
4450 const VkRenderingInfo *pRenderingInfo,
4451 struct pvr_dynamic_render_info **dr_info_out)
4452 {
4453 const bool has_stencil_resolve_iview =
4454 pRenderingInfo->pStencilAttachment &&
4455 pRenderingInfo->pStencilAttachment->resolveMode &&
4456 pRenderingInfo->pStencilAttachment->resolveImageView;
4457 const bool has_depth_resolve_iview =
4458 pRenderingInfo->pDepthAttachment &&
4459 pRenderingInfo->pDepthAttachment->resolveMode &&
4460 pRenderingInfo->pDepthAttachment->resolveImageView;
4461 const bool has_stencil_iview = pRenderingInfo->pStencilAttachment &&
4462 pRenderingInfo->pStencilAttachment->imageView;
4463 const bool has_depth_iview = pRenderingInfo->pDepthAttachment &&
4464 pRenderingInfo->pDepthAttachment->imageView;
4465 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
4466 struct pvr_device *device = cmd_buffer->device;
4467 struct pvr_dynamic_render_info *dr_info;
4468 struct pvr_render_state *rstate;
4469 struct usc_mrt_setup *mrt_setup;
4470 uint32_t attach_idx = 0;
4471 VkResult result;
4472
4473 VK_MULTIALLOC(ma);
4474 vk_multialloc_add(&ma, &dr_info, __typeof__(*dr_info), 1);
4475 vk_multialloc_add(&ma, &mrt_setup, __typeof__(*mrt_setup), 1);
4476 if (!vk_multialloc_zalloc(&ma,
4477 &device->vk.alloc,
4478 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)) {
4479 return vk_command_buffer_set_error(&cmd_buffer->vk,
4480 VK_ERROR_OUT_OF_HOST_MEMORY);
4481 }
4482
4483 for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) {
4484 const VkRenderingAttachmentInfo *attachment_info =
4485 &pRenderingInfo->pColorAttachments[i];
4486
4487 if (attachment_info->imageView)
4488 dr_info->attachment_count++;
4489
4490 if (attachment_info->resolveMode && attachment_info->resolveImageView)
4491 dr_info->attachment_count++;
4492 }
4493
4494 dr_info->attachment_count += has_depth_iview || has_stencil_iview;
4495 dr_info->attachment_count += has_depth_resolve_iview ||
4496 has_stencil_resolve_iview;
4497 dr_info->mrt_setup = mrt_setup;
4498
4499 rstate = vk_zalloc(&device->vk.alloc,
4500 sizeof(*rstate),
4501 8,
4502 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4503 if (!rstate) {
4504 result = vk_command_buffer_set_error(&cmd_buffer->vk,
4505 VK_ERROR_OUT_OF_HOST_MEMORY);
4506 goto err_free_dr_info;
4507 }
4508
4509 *rstate = (struct pvr_render_state){
4510 .width = UINT32_MAX,
4511 .height = UINT32_MAX,
4512 .width_alignment = 1,
4513 .height_alignment = 1,
4514 };
4515
4516 state->render_pass_info.rstate = rstate;
4517
4518 if (dr_info->attachment_count) {
4519 dr_info->attachments =
4520 vk_zalloc(&device->vk.alloc,
4521 sizeof(*dr_info->attachments) * dr_info->attachment_count,
4522 8,
4523 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4524 if (!dr_info->attachments) {
4525 result = vk_command_buffer_set_error(&cmd_buffer->vk,
4526 VK_ERROR_OUT_OF_HOST_MEMORY);
4527 goto err_free_rstate;
4528 }
4529
4530 result = pvr_cmd_buffer_attachments_alloc(cmd_buffer,
4531 dr_info->attachment_count);
4532 if (result != VK_SUCCESS) {
4533 result = vk_command_buffer_set_error(&cmd_buffer->vk, result);
4534 goto err_free_dr_info_attachments;
4535 }
4536
4537 result = pvr_cmd_buffer_clear_values_alloc(cmd_buffer,
4538 dr_info->attachment_count);
4539 if (result != VK_SUCCESS) {
4540 result = vk_command_buffer_set_error(&cmd_buffer->vk, result);
4541 goto err_free_cmd_buffer_attachments;
4542 }
4543 } else {
4544 pvr_cmd_buffer_clear_values_free(cmd_buffer);
4545 pvr_cmd_buffer_attachments_free(cmd_buffer);
4546 }
4547
4548 dr_info->hw_render = (struct pvr_renderpass_hwsetup_render){
4549 .ds_attach_resolve_idx = VK_ATTACHMENT_UNUSED,
4550 .ds_attach_idx = VK_ATTACHMENT_UNUSED,
4551 .depth_init = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
4552 .stencil_init = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
4553 .multiview_enabled = !!pRenderingInfo->viewMask,
4554 .view_mask = MAX2(1, pRenderingInfo->viewMask),
4555 .sample_count = 1,
4556 };
4557
4558 if (has_depth_iview || has_stencil_iview) {
4559 struct pvr_render_pass_attachment *ds_attach =
4560 &dr_info->attachments[attach_idx];
4561 VkClearDepthStencilValue *clear_value;
4562 const struct pvr_image *image;
4563 struct pvr_image_view *iview;
4564
4565 ds_attach->index = attach_idx;
4566
4567 ds_attach->load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
4568 ds_attach->store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
4569 if (has_depth_iview) {
4570 iview = pvr_image_view_from_handle(
4571 pRenderingInfo->pDepthAttachment->imageView);
4572 ds_attach->is_depth = true;
4573 ds_attach->load_op = pRenderingInfo->pDepthAttachment->loadOp;
4574 ds_attach->store_op = pRenderingInfo->pDepthAttachment->storeOp;
4575 ds_attach->resolve_mode =
4576 pRenderingInfo->pDepthAttachment->resolveMode;
4577 }
4578
4579 ds_attach->stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
4580 ds_attach->stencil_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
4581 if (has_stencil_iview) {
4582 iview = pvr_image_view_from_handle(
4583 pRenderingInfo->pStencilAttachment->imageView);
4584 ds_attach->is_stencil = true;
4585 ds_attach->stencil_load_op =
4586 pRenderingInfo->pStencilAttachment->loadOp;
4587 ds_attach->stencil_store_op =
4588 pRenderingInfo->pStencilAttachment->storeOp;
4589 ds_attach->stencil_resolve_mode =
4590 pRenderingInfo->pStencilAttachment->resolveMode;
4591 }
4592
4593 image = pvr_image_view_get_image(iview);
4594
4595 ds_attach->vk_format = iview->vk.view_format;
4596 ds_attach->sample_count = image->vk.samples;
4597 ds_attach->resolve_target = VK_ATTACHMENT_UNUSED;
4598
4599 state->render_pass_info.attachments[attach_idx] = iview;
4600 state->render_pass_info.rstate->height =
4601 MIN2(state->render_pass_info.rstate->height, image->vk.extent.height);
4602 state->render_pass_info.rstate->width =
4603 MIN2(state->render_pass_info.rstate->width, image->vk.extent.width);
4604
4605 dr_info->hw_render.ds_attach_idx = attach_idx;
4606 dr_info->hw_render.depth_init = ds_attach->load_op;
4607 dr_info->hw_render.depth_store = ds_attach->store_op ==
4608 VK_ATTACHMENT_STORE_OP_STORE;
4609 dr_info->hw_render.depth_resolve_mode = ds_attach->resolve_mode;
4610
4611 dr_info->hw_render.stencil_init = ds_attach->stencil_load_op;
4612 dr_info->hw_render.stencil_store = ds_attach->stencil_store_op ==
4613 VK_ATTACHMENT_STORE_OP_STORE;
4614 dr_info->hw_render.sample_count =
4615 MAX2(dr_info->hw_render.sample_count, ds_attach->sample_count);
4616 dr_info->hw_render.stencil_resolve_mode = ds_attach->stencil_resolve_mode;
4617
4618 clear_value =
4619 &state->render_pass_info.clear_values[attach_idx].depthStencil;
4620
4621 if (ds_attach->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
4622 clear_value->depth =
4623 pRenderingInfo->pDepthAttachment->clearValue.depthStencil.depth;
4624 if (ds_attach->store_op == VK_ATTACHMENT_STORE_OP_STORE)
4625 dr_info->hw_render.has_side_effects = true;
4626 }
4627
4628 if (ds_attach->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
4629 clear_value->stencil =
4630 pRenderingInfo->pStencilAttachment->clearValue.depthStencil.stencil;
4631 if (ds_attach->stencil_store_op == VK_ATTACHMENT_STORE_OP_STORE)
4632 dr_info->hw_render.has_side_effects = true;
4633 }
4634
4635 attach_idx++;
4636
4637 if (ds_attach->resolve_mode || ds_attach->stencil_resolve_mode) {
4638 struct pvr_render_pass_attachment *resolve_ds_attach =
4639 &dr_info->attachments[attach_idx];
4640 struct pvr_image_view *resolve_iview;
4641
4642 ds_attach->resolve_target = attach_idx;
4643
4644 if (has_depth_resolve_iview) {
4645 resolve_iview = pvr_image_view_from_handle(
4646 pRenderingInfo->pDepthAttachment->resolveImageView);
4647 } else if (has_stencil_resolve_iview) {
4648 resolve_iview = pvr_image_view_from_handle(
4649 pRenderingInfo->pStencilAttachment->resolveImageView);
4650 } else {
4651 UNREACHABLE("invalid image view for DS resolve attachment");
4652 }
4653
4654 state->render_pass_info.attachments[attach_idx] = resolve_iview;
4655
4656 dr_info->hw_render.ds_attach_resolve_idx = attach_idx;
4657 dr_info->hw_render.has_side_effects = true;
4658
4659 assert(iview->vk.view_format == resolve_iview->vk.view_format);
4660
4661 resolve_ds_attach->vk_format = ds_attach->vk_format;
4662 resolve_ds_attach->sample_count = 1;
4663 resolve_ds_attach->index = attach_idx;
4664 resolve_ds_attach->resolve_target = VK_ATTACHMENT_UNUSED;
4665 resolve_ds_attach->is_stencil = ds_attach->is_stencil;
4666 resolve_ds_attach->store_op = ds_attach->store_op;
4667 resolve_ds_attach->stencil_store_op = ds_attach->stencil_store_op;
4668 resolve_ds_attach->load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
4669 resolve_ds_attach->stencil_load_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
4670
4671 attach_idx++;
4672 }
4673 }
4674
4675 result = pvr_dynamic_rendering_output_attachments_setup(cmd_buffer,
4676 pRenderingInfo,
4677 dr_info,
4678 &attach_idx);
4679 if (result != VK_SUCCESS) {
4680 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4681 goto err_free_cmd_buffer_clear_values;
4682 }
4683
4684 if (state->render_pass_info.rstate->height == UINT32_MAX) {
4685 state->render_pass_info.rstate->height =
4686 pRenderingInfo->renderArea.offset.y +
4687 pRenderingInfo->renderArea.extent.height;
4688 }
4689
4690 if (state->render_pass_info.rstate->width == UINT32_MAX) {
4691 state->render_pass_info.rstate->width =
4692 pRenderingInfo->renderArea.offset.x +
4693 pRenderingInfo->renderArea.extent.width;
4694 }
4695
4696 *dr_info_out = dr_info;
4697
4698 /* FIXME: When adding support for caching rt datasets and render states,
4699 * remove this and handle it with a proper hash table.
4700 */
4701 pvr_rstate_entry_add(device, rstate);
4702
4703 return VK_SUCCESS;
4704
4705 err_free_cmd_buffer_clear_values:
4706 pvr_cmd_buffer_clear_values_free(cmd_buffer);
4707
4708 err_free_cmd_buffer_attachments:
4709 pvr_cmd_buffer_attachments_free(cmd_buffer);
4710
4711 err_free_dr_info_attachments:
4712 vk_free(&device->vk.alloc, dr_info->attachments);
4713
4714 err_free_rstate:
4715 vk_free(&device->vk.alloc, rstate);
4716
4717 err_free_dr_info:
4718 vk_free(&device->vk.alloc, dr_info);
4719
4720 return result;
4721 }
4722
4723 static inline uint64_t pvr_render_pass_info_get_scratch_buffer_size(
4724 struct pvr_device *device,
4725 const struct pvr_render_pass_info *info)
4726 {
4727 return pvr_arch_spm_scratch_buffer_calc_required_size(
4728 &info->dr_info->hw_render,
4729 1,
4730 info->dr_info->hw_render.sample_count,
4731 info->rstate->width,
4732 info->rstate->height);
4733 }
4734
4735 void PVR_PER_ARCH(CmdBeginRendering)(VkCommandBuffer commandBuffer,
4736 const VkRenderingInfo *pRenderingInfo)
4737 {
4738 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
4739 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
4740 struct pvr_device *const device = cmd_buffer->device;
4741 struct pvr_dynamic_render_info *dr_info = NULL;
4742 struct pvr_sub_cmd_gfx *sub_cmd;
4743 bool resume, suspend;
4744 VkResult result;
4745
4746 /* TODO: Check not in renderpess? */
4747
4748 suspend = pRenderingInfo->flags & VK_RENDERING_SUSPENDING_BIT_KHR;
4749 resume = pRenderingInfo->flags & VK_RENDERING_RESUMING_BIT_KHR;
4750
4751 if (state->current_sub_cmd && resume) {
4752 assert(state->current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS);
4753 assert(state->current_sub_cmd->is_dynamic_render);
4754 assert(state->current_sub_cmd->is_suspend);
4755
4756 if (!suspend)
4757 state->current_sub_cmd->is_suspend = false;
4758
4759 return;
4760 }
4761
4762 result =
4763 pvr_dynamic_render_info_create(cmd_buffer, pRenderingInfo, &dr_info);
4764 if (result != VK_SUCCESS) {
4765 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4766 return;
4767 }
4768
4769 state->render_pass_info.dr_info = dr_info;
4770 state->render_pass_info.hw_render = &dr_info->hw_render;
4771 state->render_pass_info.render_area = (VkRect2D){
4772 .extent = pRenderingInfo->renderArea.extent,
4773 .offset =
4774 (VkOffset2D){
4775 .x = MAX2(0, pRenderingInfo->renderArea.offset.x),
4776 .y = MAX2(0, pRenderingInfo->renderArea.offset.y),
4777 },
4778 };
4779 state->render_pass_info.pipeline_bind_point =
4780 VK_PIPELINE_BIND_POINT_GRAPHICS;
4781 state->render_pass_info.dynamic_render = true;
4782 state->render_pass_info.suspend = suspend;
4783 state->render_pass_info.resume = resume;
4784 state->render_pass_info.current_hw_subpass = -1;
4785
4786 if (dr_info->hw_render.tile_buffers_count) {
4787 result = pvr_device_tile_buffer_ensure_cap(
4788 device,
4789 dr_info->hw_render.tile_buffers_count);
4790 if (result != VK_SUCCESS) {
4791 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4792 goto err_destroy_dynamic_render_info;
4793 }
4794 }
4795
4796 if (pRenderingInfo->pDepthAttachment &&
4797 pRenderingInfo->pDepthAttachment->imageView) {
4798 struct pvr_image_view *iview = pvr_image_view_from_handle(
4799 pRenderingInfo->pDepthAttachment->imageView);
4800 state->depth_format = iview->vk.format;
4801 } else if (pRenderingInfo->pStencilAttachment &&
4802 pRenderingInfo->pStencilAttachment->imageView) {
4803 struct pvr_image_view *iview = pvr_image_view_from_handle(
4804 pRenderingInfo->pStencilAttachment->imageView);
4805 state->depth_format = iview->vk.format;
4806 } else {
4807 state->depth_format = VK_FORMAT_UNDEFINED;
4808 }
4809
4810 if (dr_info->color_attachment_count ||
4811 dr_info->hw_render.ds_attach_idx != VK_ATTACHMENT_UNUSED) {
4812 state->render_pass_info.sample_count = dr_info->hw_render.sample_count;
4813 }
4814
4815 state->render_pass_info.rstate->layers =
4816 !pRenderingInfo->viewMask ? pRenderingInfo->layerCount : 1;
4817 state->render_pass_info.rstate->scratch_buffer_size =
4818 pvr_render_pass_info_get_scratch_buffer_size(device,
4819 &state->render_pass_info);
4820
4821 result = pvr_arch_render_state_setup(device,
4822 NULL,
4823 state->render_pass_info.rstate,
4824 1,
4825 &dr_info->hw_render);
4826 if (result != VK_SUCCESS) {
4827 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4828 goto err_cleanup_tile_buffers;
4829 }
4830
4831 result =
4832 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_GRAPHICS);
4833 if (result != VK_SUCCESS) {
4834 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4835 goto err_cleanup_render_state;
4836 }
4837
4838 sub_cmd = &state->current_sub_cmd->gfx;
4839 sub_cmd->rstate = state->render_pass_info.rstate;
4840 sub_cmd->dr_info = dr_info;
4841 assert(sub_cmd->dr_info);
4842
4843 result = pvr_arch_mrt_load_ops_setup(cmd_buffer,
4844 &cmd_buffer->vk.pool->alloc,
4845 &dr_info->hw_render.load_op_state);
4846 if (result != VK_SUCCESS) {
4847 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4848 goto err_cleanup_render_state;
4849 }
4850
4851 pvr_perform_start_of_render_clears(cmd_buffer, resume);
4852
4853 return;
4854
4855 err_cleanup_render_state:
4856 pvr_render_state_cleanup(device, &device->vk.alloc, state->render_pass_info.rstate);
4857
4858 err_cleanup_tile_buffers:
4859 pvr_device_free_tile_buffer_state(device);
4860
4861 err_destroy_dynamic_render_info:
4862 pvr_dynamic_render_info_destroy(device, cmd_buffer, dr_info);
4863 }
4864
4865 static VkResult
4866 pvr_resolve_unemitted_resolve_attachments(struct pvr_cmd_buffer *cmd_buffer,
4867 struct pvr_render_pass_info *info);
4868
4869 void PVR_PER_ARCH(CmdEndRendering)(VkCommandBuffer commandBuffer)
4870 {
4871 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
4872 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
4873 VkResult result;
4874
4875 if (state->current_sub_cmd && state->current_sub_cmd->is_suspend) {
4876 return;
4877 }
4878
4879 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
4880 if (result != VK_SUCCESS) {
4881 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4882 goto exit_teardown_render;
4883 }
4884
4885 result = pvr_resolve_unemitted_resolve_attachments(cmd_buffer,
4886 &state->render_pass_info);
4887 if (result != VK_SUCCESS) {
4888 vk_command_buffer_set_error(&cmd_buffer->vk, result);
4889 goto exit_teardown_render;
4890 }
4891
4892 exit_teardown_render:
4893 state->render_pass_info.dynamic_render = false;
4894 pvr_cmd_buffer_clear_values_free(cmd_buffer);
4895 pvr_cmd_buffer_attachments_free(cmd_buffer);
4896 }
4897
4898 static void pvr_cmd_buffer_state_from_dynamic_inheritance(
4899 struct pvr_cmd_buffer *cmd_buffer,
4900 const VkCommandBufferInheritanceRenderingInfo *inheritance_info)
4901 {
4902 const bool has_stencil = inheritance_info->stencilAttachmentFormat !=
4903 VK_FORMAT_UNDEFINED;
4904 const bool has_depth = inheritance_info->depthAttachmentFormat !=
4905 VK_FORMAT_UNDEFINED;
4906 VkFormat attachment_formats[inheritance_info->colorAttachmentCount];
4907 uint32_t mrt_attachment_map[inheritance_info->colorAttachmentCount];
4908 struct pvr_device *const device = cmd_buffer->device;
4909 struct pvr_dynamic_render_info *dr_info;
4910 uint32_t attach_idx = 0, mrt_count = 0;
4911 struct usc_mrt_setup mrt_setup, *setup;
4912 VkResult result;
4913
4914 VK_MULTIALLOC(ma);
4915 vk_multialloc_add(&ma, &dr_info, __typeof__(*dr_info), 1);
4916 vk_multialloc_add(&ma, &setup, __typeof__(*setup), 1);
4917 if (!vk_multialloc_zalloc(&ma,
4918 &device->vk.alloc,
4919 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)) {
4920 vk_command_buffer_set_error(&cmd_buffer->vk, VK_ERROR_OUT_OF_HOST_MEMORY);
4921 }
4922
4923 dr_info->hw_render.sample_count = inheritance_info->rasterizationSamples;
4924 dr_info->hw_render.multiview_enabled = !!inheritance_info->viewMask;
4925 dr_info->hw_render.view_mask = MAX2(1, inheritance_info->viewMask);
4926 dr_info->attachment_count = has_depth || has_stencil;
4927 dr_info->mrt_setup = setup;
4928
4929 for (uint32_t i = 0; i < inheritance_info->colorAttachmentCount; i++)
4930 if (inheritance_info->pColorAttachmentFormats[i] != VK_FORMAT_UNDEFINED)
4931 dr_info->attachment_count++;
4932
4933 if (dr_info->attachment_count) {
4934 dr_info->attachments =
4935 vk_zalloc(&cmd_buffer->vk.pool->alloc,
4936 sizeof(*dr_info->attachments) * dr_info->attachment_count,
4937 8,
4938 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4939 if (!dr_info->attachments) {
4940 vk_command_buffer_set_error(&cmd_buffer->vk,
4941 VK_ERROR_OUT_OF_HOST_MEMORY);
4942 goto err_free_dr_info;
4943 }
4944 }
4945
4946 dr_info->color_attachment_count = inheritance_info->colorAttachmentCount;
4947 if (dr_info->color_attachment_count) {
4948 dr_info->color_attachments = vk_zalloc(
4949 &device->vk.alloc,
4950 sizeof(*dr_info->color_attachments) * dr_info->color_attachment_count,
4951 8,
4952 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
4953 if (!dr_info->color_attachments) {
4954 vk_command_buffer_set_error(&cmd_buffer->vk,
4955 VK_ERROR_OUT_OF_HOST_MEMORY);
4956 goto err_free_attachments;
4957 }
4958 }
4959
4960 assert(inheritance_info->stencilAttachmentFormat == VK_FORMAT_UNDEFINED ||
4961 inheritance_info->depthAttachmentFormat == VK_FORMAT_UNDEFINED ||
4962 inheritance_info->depthAttachmentFormat ==
4963 inheritance_info->stencilAttachmentFormat);
4964
4965 dr_info->hw_render.ds_attach_idx = VK_ATTACHMENT_UNUSED;
4966
4967 if (has_depth || has_stencil) {
4968 struct pvr_render_pass_attachment *ds_attachment =
4969 &dr_info->attachments[attach_idx];
4970 const VkFormat vk_format = has_depth
4971 ? inheritance_info->depthAttachmentFormat
4972 : inheritance_info->stencilAttachmentFormat;
4973
4974 dr_info->hw_render.ds_attach_idx = attach_idx;
4975 dr_info->hw_render.stencil_store = has_stencil;
4976 dr_info->hw_render.depth_store = has_depth;
4977
4978 ds_attachment->index = attach_idx;
4979 ds_attachment->vk_format = vk_format;
4980 ds_attachment->is_depth = dr_info->hw_render.depth_store;
4981 ds_attachment->is_stencil = dr_info->hw_render.stencil_store;
4982 ds_attachment->sample_count = inheritance_info->rasterizationSamples;
4983 ds_attachment->resolve_target = VK_ATTACHMENT_UNUSED;
4984 ds_attachment->load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
4985 ds_attachment->store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
4986 ds_attachment->stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
4987 ds_attachment->stencil_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
4988
4989 attach_idx++;
4990 }
4991
4992 for (uint32_t i = 0; i < inheritance_info->colorAttachmentCount; i++) {
4993 struct pvr_render_pass_attachment *attachment;
4994
4995 dr_info->color_attachments[i].index_color = VK_ATTACHMENT_UNUSED;
4996 dr_info->color_attachments[i].index_resolve = VK_ATTACHMENT_UNUSED;
4997 attachment_formats[mrt_count] = VK_FORMAT_UNDEFINED;
4998 mrt_attachment_map[i] = VK_ATTACHMENT_UNUSED;
4999
5000 if (inheritance_info->pColorAttachmentFormats[i] == VK_FORMAT_UNDEFINED)
5001 continue;
5002
5003 dr_info->color_attachments[i].index_color = attach_idx;
5004 mrt_attachment_map[i] = mrt_count;
5005 attachment_formats[mrt_count++] =
5006 inheritance_info->pColorAttachmentFormats[i];
5007
5008 attachment = &dr_info->attachments[attach_idx];
5009 attachment->index = attach_idx;
5010 attachment->vk_format = inheritance_info->pColorAttachmentFormats[i];
5011 attachment->sample_count = inheritance_info->rasterizationSamples;
5012 attachment->resolve_target = VK_ATTACHMENT_UNUSED;
5013 attachment->load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
5014 attachment->store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
5015 attachment->stencil_load_op = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
5016 attachment->stencil_store_op = VK_ATTACHMENT_STORE_OP_DONT_CARE;
5017 attachment->stencil_resolve_mode = VK_RESOLVE_MODE_NONE;
5018 attach_idx++;
5019 }
5020
5021 result = pvr_arch_init_usc_mrt_setup(device,
5022 mrt_count,
5023 attachment_formats,
5024 &mrt_setup);
5025 if (result != VK_SUCCESS) {
5026 vk_command_buffer_set_error(&cmd_buffer->vk, result);
5027 goto err_free_attachments;
5028 }
5029
5030 result =
5031 pvr_arch_mrt_setup_partial_init(cmd_buffer->device,
5032 dr_info->mrt_setup,
5033 inheritance_info->colorAttachmentCount,
5034 mrt_setup.num_output_regs,
5035 mrt_setup.num_tile_buffers);
5036 if (result != VK_SUCCESS) {
5037 pvr_arch_destroy_mrt_setup(device, &mrt_setup);
5038 vk_command_buffer_set_error(&cmd_buffer->vk, result);
5039 goto err_free_attachments;
5040 }
5041
5042 /* We fully initialised only a mrt_count number of render targets,
5043 * now scatter gather them over the whole number of attachments.
5044 * This is necessary because render targets are attachment-indexed
5045 * throughout the driver.
5046 */
5047 for (uint32_t i = 0; i < inheritance_info->colorAttachmentCount; i++) {
5048 if (mrt_attachment_map[i] != VK_ATTACHMENT_UNUSED) {
5049 memcpy(&dr_info->mrt_setup->mrt_resources[i],
5050 &mrt_setup.mrt_resources[mrt_attachment_map[i]],
5051 sizeof(mrt_setup.mrt_resources[0]));
5052 }
5053 }
5054
5055 pvr_arch_destroy_mrt_setup(device, &mrt_setup);
5056
5057 if (dr_info->mrt_setup->num_tile_buffers) {
5058 result = pvr_device_tile_buffer_ensure_cap(
5059 device,
5060 dr_info->mrt_setup->num_tile_buffers);
5061 if (result != VK_SUCCESS) {
5062 vk_command_buffer_set_error(&cmd_buffer->vk, result);
5063 goto err_destroy_mrt_setup;
5064 }
5065 }
5066
5067 cmd_buffer->state.render_pass_info.hw_render = &dr_info->hw_render;
5068 cmd_buffer->state.render_pass_info.dr_info = dr_info;
5069
5070 /* For setting the depth_format, first check stencil and then override */
5071 if (has_stencil)
5072 cmd_buffer->state.depth_format =
5073 inheritance_info->stencilAttachmentFormat;
5074
5075 if (has_depth)
5076 cmd_buffer->state.depth_format = inheritance_info->depthAttachmentFormat;
5077
5078 return;
5079
5080 err_destroy_mrt_setup:
5081 pvr_arch_destroy_mrt_setup(device, dr_info->mrt_setup);
5082
5083 err_free_attachments:
5084 vk_free(&device->vk.alloc, dr_info->attachments);
5085
5086 err_free_dr_info:
5087 vk_free(&device->vk.alloc, dr_info);
5088 }
5089
5090 static inline void pvr_cmd_buffer_state_from_render_pass_inheritance(
5091 struct pvr_cmd_buffer_state *state,
5092 const VkCommandBufferInheritanceInfo *inheritance_info)
5093 {
5094 struct pvr_render_pass *pass =
5095 pvr_render_pass_from_handle(inheritance_info->renderPass);
5096
5097 state->render_pass_info.pass = pass;
5098 state->render_pass_info.framebuffer =
5099 pvr_framebuffer_from_handle(inheritance_info->framebuffer);
5100 state->render_pass_info.subpass_idx = inheritance_info->subpass;
5101 state->render_pass_info.isp_userpass =
5102 pass->subpasses[inheritance_info->subpass].isp_userpass;
5103 }
5104
5105 VkResult
5106 PVR_PER_ARCH(BeginCommandBuffer)(VkCommandBuffer commandBuffer,
5107 const VkCommandBufferBeginInfo *pBeginInfo)
5108 {
5109 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
5110 struct pvr_cmd_buffer_state *state;
5111 VkResult result;
5112
5113 vk_command_buffer_begin(&cmd_buffer->vk, pBeginInfo);
5114
5115 cmd_buffer->usage_flags = pBeginInfo->flags;
5116 state = &cmd_buffer->state;
5117
5118 /* VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT must be ignored for
5119 * primary level command buffers.
5120 *
5121 * From the Vulkan 1.0 spec:
5122 *
5123 * VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT specifies that a
5124 * secondary command buffer is considered to be entirely inside a render
5125 * pass. If this is a primary command buffer, then this bit is ignored.
5126 */
5127 if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
5128 cmd_buffer->usage_flags &=
5129 ~VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
5130 }
5131
5132 if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
5133 if (cmd_buffer->usage_flags &
5134 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT) {
5135 const VkCommandBufferInheritanceRenderingInfo *dyn_inheritance_info =
5136 vk_find_struct_const(pBeginInfo->pInheritanceInfo->pNext,
5137 COMMAND_BUFFER_INHERITANCE_RENDERING_INFO);
5138 if (!pBeginInfo->pInheritanceInfo->renderPass) {
5139 assert(dyn_inheritance_info);
5140 pvr_cmd_buffer_state_from_dynamic_inheritance(cmd_buffer,
5141 dyn_inheritance_info);
5142 } else {
5143 pvr_cmd_buffer_state_from_render_pass_inheritance(
5144 state,
5145 pBeginInfo->pInheritanceInfo);
5146 }
5147
5148 result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer,
5149 PVR_SUB_CMD_TYPE_GRAPHICS);
5150 if (result != VK_SUCCESS)
5151 return result;
5152
5153 state->vis_test_enabled =
5154 pBeginInfo->pInheritanceInfo->occlusionQueryEnable;
5155
5156 if (!pBeginInfo->pInheritanceInfo->renderPass)
5157 state->current_sub_cmd->is_dynamic_render = true;
5158 }
5159
5160 state->dirty.isp_userpass = true;
5161 }
5162
5163 state->query_indices = UTIL_DYNARRAY_INIT;
5164
5165 memset(state->barriers_needed,
5166 0xFF,
5167 sizeof(*state->barriers_needed) * ARRAY_SIZE(state->barriers_needed));
5168
5169 return VK_SUCCESS;
5170 }
5171
5172 VkResult
5173 pvr_arch_cmd_buffer_add_transfer_cmd(struct pvr_cmd_buffer *cmd_buffer,
5174 struct pvr_transfer_cmd *transfer_cmd)
5175 {
5176 struct pvr_sub_cmd_transfer *sub_cmd;
5177 VkResult result;
5178
5179 result =
5180 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_TRANSFER);
5181 if (result != VK_SUCCESS)
5182 return result;
5183
5184 sub_cmd = &cmd_buffer->state.current_sub_cmd->transfer;
5185
5186 list_addtail(&transfer_cmd->link, sub_cmd->transfer_cmds);
5187
5188 return VK_SUCCESS;
5189 }
5190
5191 static VkResult
5192 pvr_setup_vertex_buffers(struct pvr_cmd_buffer *cmd_buffer,
5193 const struct pvr_graphics_pipeline *const gfx_pipeline)
5194 {
5195 const struct pvr_vertex_shader_state *const vertex_state =
5196 &gfx_pipeline->shader_state.vertex;
5197 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
5198 const struct pvr_pds_info *const pds_info = state->pds_shader.info;
5199 struct pvr_suballoc_bo *pvr_bo;
5200 const uint8_t *entries;
5201 uint32_t *dword_buffer;
5202 uint64_t *qword_buffer;
5203 VkResult result;
5204
5205 result = pvr_arch_cmd_buffer_alloc_mem(
5206 cmd_buffer,
5207 cmd_buffer->device->heaps.pds_heap,
5208 PVR_DW_TO_BYTES(pds_info->data_size_in_dwords),
5209 &pvr_bo);
5210 if (result != VK_SUCCESS)
5211 return result;
5212
5213 dword_buffer = (uint32_t *)pvr_bo_suballoc_get_map_addr(pvr_bo);
5214 qword_buffer = (uint64_t *)pvr_bo_suballoc_get_map_addr(pvr_bo);
5215
5216 entries = (uint8_t *)pds_info->entries;
5217
5218 for (uint32_t i = 0; i < pds_info->entry_count; i++) {
5219 const struct pvr_const_map_entry *const entry_header =
5220 (struct pvr_const_map_entry *)entries;
5221
5222 switch (entry_header->type) {
5223 case PVR_PDS_CONST_MAP_ENTRY_TYPE_LITERAL32: {
5224 const struct pvr_const_map_entry_literal32 *const literal =
5225 (struct pvr_const_map_entry_literal32 *)entries;
5226
5227 PVR_WRITE(dword_buffer,
5228 literal->literal_value,
5229 literal->const_offset,
5230 pds_info->data_size_in_dwords);
5231
5232 entries += sizeof(*literal);
5233 break;
5234 }
5235
5236 case PVR_PDS_CONST_MAP_ENTRY_TYPE_DOUTU_ADDRESS: {
5237 const struct pvr_const_map_entry_doutu_address *const doutu_addr =
5238 (struct pvr_const_map_entry_doutu_address *)entries;
5239
5240 const pco_data *const vs_data = &state->gfx_pipeline->vs_data;
5241 const pvr_dev_addr_t exec_addr =
5242 PVR_DEV_ADDR_OFFSET(vertex_state->shader_bo->dev_addr,
5243 vs_data->common.entry_offset);
5244 uint64_t addr = 0ULL;
5245
5246 pvr_set_usc_execution_address64(&addr, exec_addr.addr);
5247
5248 PVR_WRITE(qword_buffer,
5249 addr | doutu_addr->doutu_control,
5250 doutu_addr->const_offset,
5251 pds_info->data_size_in_dwords);
5252
5253 entries += sizeof(*doutu_addr);
5254 break;
5255 }
5256
5257 case PVR_PDS_CONST_MAP_ENTRY_TYPE_BASE_INSTANCE: {
5258 const struct pvr_const_map_entry_base_instance *const base_instance =
5259 (struct pvr_const_map_entry_base_instance *)entries;
5260
5261 PVR_WRITE(dword_buffer,
5262 state->draw_state.base_instance,
5263 base_instance->const_offset,
5264 pds_info->data_size_in_dwords);
5265
5266 entries += sizeof(*base_instance);
5267 break;
5268 }
5269
5270 case PVR_PDS_CONST_MAP_ENTRY_TYPE_BASE_VERTEX: {
5271 const struct pvr_const_map_entry_base_instance *const base_instance =
5272 (struct pvr_const_map_entry_base_instance *)entries;
5273
5274 PVR_WRITE(dword_buffer,
5275 state->draw_state.base_vertex,
5276 base_instance->const_offset,
5277 pds_info->data_size_in_dwords);
5278
5279 entries += sizeof(*base_instance);
5280 break;
5281 }
5282
5283 case PVR_PDS_CONST_MAP_ENTRY_TYPE_VERTEX_ATTRIBUTE_ADDRESS: {
5284 const struct pvr_const_map_entry_vertex_attribute_address
5285 *const attribute =
5286 (struct pvr_const_map_entry_vertex_attribute_address *)entries;
5287 const struct pvr_vertex_binding *const binding =
5288 &state->vertex_bindings[attribute->binding_index];
5289 /* In relation to the Vulkan spec. 22.4. Vertex Input Address
5290 * Calculation:
5291 * Adding binding->offset corresponds to calculating the
5292 * `bufferBindingAddress`. Adding attribute->offset corresponds to
5293 * adding the `attribDesc.offset`. The `effectiveVertexOffset` is
5294 * taken care by the PDS program itself with a DDMAD which will
5295 * multiply the vertex/instance idx with the binding's stride and
5296 * add that to the address provided here.
5297 */
5298 const pvr_dev_addr_t addr =
5299 PVR_DEV_ADDR_OFFSET(binding->buffer->dev_addr,
5300 binding->offset + attribute->offset);
5301
5302 PVR_WRITE(qword_buffer,
5303 addr.addr,
5304 attribute->const_offset,
5305 pds_info->data_size_in_dwords);
5306
5307 entries += sizeof(*attribute);
5308 break;
5309 }
5310
5311 case PVR_PDS_CONST_MAP_ENTRY_TYPE_ROBUST_VERTEX_ATTRIBUTE_ADDRESS: {
5312 const struct pvr_const_map_entry_robust_vertex_attribute_address
5313 *const attribute =
5314 (struct pvr_const_map_entry_robust_vertex_attribute_address *)
5315 entries;
5316 const struct pvr_vertex_binding *const binding =
5317 &state->vertex_bindings[attribute->binding_index];
5318 pvr_dev_addr_t addr;
5319
5320 if (binding->size <
5321 (attribute->offset + attribute->attrib_size_in_bytes)) {
5322 /* Replace with load from robustness buffer when no attribute is in
5323 * range
5324 */
5325 addr = PVR_DEV_ADDR_OFFSET(
5326 cmd_buffer->device->robustness_buffer->vma->dev_addr,
5327 attribute->robustness_buffer_offset);
5328 } else {
5329 addr = PVR_DEV_ADDR_OFFSET(binding->buffer->dev_addr,
5330 binding->offset + attribute->offset);
5331 }
5332
5333 PVR_WRITE(qword_buffer,
5334 addr.addr,
5335 attribute->const_offset,
5336 pds_info->data_size_in_dwords);
5337
5338 entries += sizeof(*attribute);
5339 break;
5340 }
5341
5342 case PVR_PDS_CONST_MAP_ENTRY_TYPE_VERTEX_ATTR_DDMADT_OOB_BUFFER_SIZE: {
5343 const struct pvr_pds_const_map_entry_vertex_attr_ddmadt_oob_buffer_size
5344 *ddmadt_src3 =
5345 (struct pvr_pds_const_map_entry_vertex_attr_ddmadt_oob_buffer_size
5346 *)entries;
5347 const struct pvr_vertex_binding *const binding =
5348 &state->vertex_bindings[ddmadt_src3->binding_index];
5349
5350 const struct vk_dynamic_graphics_state *dynamic_state =
5351 &cmd_buffer->vk.dynamic_graphics_state;
5352 uint32_t stride =
5353 dynamic_state->vi_binding_strides[ddmadt_src3->binding_index];
5354 uint32_t valid_buffer_size =
5355 binding->buffer->vk.size - binding->offset - ddmadt_src3->offset;
5356 uint32_t attrib_size = ddmadt_src3->attrib_size_in_bytes;
5357 uint32_t dma_size = ddmadt_src3->size_in_dwords * 4;
5358 uint64_t control_qword;
5359 uint32_t control_dword;
5360
5361 assert(PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info,
5362 pds_ddmadt));
5363
5364 /* The DMA should cover the whole attribute, but do not overread
5365 * more than the headroom when allocating memory for buffers.
5366 */
5367 assert(dma_size >= attrib_size);
5368 assert(dma_size <= attrib_size + PVR_BUFFER_MEMORY_PADDING_SIZE);
5369
5370 /* DDMADT checks whether the whole DMA request (size indicated by
5371 * the sizes_in_dwords field) is out of the regions set by msize.
5372 * Set the buffer size to the last valid vertex DMA address plus
5373 * the size of one DMA request.
5374 *
5375 * This also ensures at least one valid inbound DDMAD is possible.
5376 * In case the original buffer isn't big enough for one DMA, the
5377 * DDMADT source_address should have been already redirected to the
5378 * robust buffer when setting up ROBUST_VERTEX_ATTRIBUTE_ADDRESS.
5379 *
5380 * See also the pseudocode in pvr_pds_generate_vertex_primary_program()
5381 * before the "LAST DDMAD" mark, which explains how DDMAD(T) works.
5382 */
5383 if (stride == 0) {
5384 /* All DMA requests happen on the same address */
5385 valid_buffer_size = 0;
5386 } else {
5387 uint32_t buffer_tail_size = valid_buffer_size % stride;
5388 valid_buffer_size -= buffer_tail_size;
5389 if (buffer_tail_size < attrib_size && valid_buffer_size != 0) {
5390 /* The buffer tail isn't big enough for a vertex attribute fetch
5391 * operation so the last valid DMA happens on the last whole
5392 * stride.
5393 */
5394 valid_buffer_size -= stride;
5395 }
5396 }
5397 valid_buffer_size += dma_size;
5398
5399 pvr_csb_pack (&control_qword, PDSINST_DDMAD_FIELDS_SRC3, src3) {
5400 src3.test = true;
5401 src3.msize = valid_buffer_size;
5402 }
5403 control_dword = (uint32_t)(control_qword >> 32);
5404
5405 PVR_WRITE(dword_buffer,
5406 control_dword,
5407 ddmadt_src3->const_offset,
5408 pds_info->data_size_in_dwords);
5409
5410 entries += sizeof(*ddmadt_src3);
5411 break;
5412 }
5413 case PVR_PDS_CONST_MAP_ENTRY_TYPE_VERTEX_ATTRIBUTE_MAX_INDEX: {
5414 const struct pvr_const_map_entry_vertex_attribute_max_index *attribute =
5415 (struct pvr_const_map_entry_vertex_attribute_max_index *)entries;
5416 const struct pvr_vertex_binding *const binding =
5417 &state->vertex_bindings[attribute->binding_index];
5418 const uint64_t bound_size = binding->buffer->vk.size - binding->offset;
5419 const uint32_t attribute_end =
5420 attribute->offset + attribute->attrib_size_in_bytes;
5421 const struct vk_dynamic_graphics_state *dynamic_state =
5422 &cmd_buffer->vk.dynamic_graphics_state;
5423 const uint32_t stride =
5424 dynamic_state->vi_binding_strides[attribute->binding_index];
5425 uint32_t max_index;
5426
5427 /* If the stride is 0 then all attributes use the same single
5428 * element from the binding so the index can only be up to 0.
5429 */
5430 if (bound_size < attribute_end || stride == 0) {
5431 max_index = 0;
5432 } else {
5433 max_index = (uint32_t)(bound_size / stride) - 1;
5434
5435 /* There's one last attribute that can fit in. */
5436 if (bound_size % stride >= attribute_end)
5437 max_index++;
5438 }
5439
5440 PVR_WRITE(dword_buffer,
5441 max_index,
5442 attribute->const_offset,
5443 pds_info->data_size_in_dwords);
5444
5445 entries += sizeof(*attribute);
5446 break;
5447 }
5448
5449 case PVR_PDS_CONST_MAP_ENTRY_TYPE_VERTEX_ATTRIBUTE_STRIDE: {
5450 const struct pvr_const_map_entry_vertex_attribute_stride *attribute =
5451 (const struct pvr_const_map_entry_vertex_attribute_stride *)entries;
5452 const struct vk_dynamic_graphics_state *dynamic_state =
5453 &cmd_buffer->vk.dynamic_graphics_state;
5454 const uint32_t stride =
5455 dynamic_state->vi_binding_strides[attribute->binding_index];
5456
5457 PVR_WRITE(dword_buffer,
5458 stride,
5459 attribute->const_offset,
5460 pds_info->data_size_in_dwords);
5461
5462 entries += sizeof(*attribute);
5463 break;
5464 }
5465
5466 default:
5467 UNREACHABLE("Unsupported data section map");
5468 break;
5469 }
5470 }
5471
5472 state->pds_vertex_attrib_offset =
5473 pvr_bo->dev_addr.addr -
5474 cmd_buffer->device->heaps.pds_heap->base_addr.addr;
5475
5476 return VK_SUCCESS;
5477 }
5478
5479 static VkResult pvr_cmd_upload_push_consts(struct pvr_cmd_buffer *cmd_buffer,
5480 enum pvr_stage_allocation stage);
5481
5482 static VkResult pvr_setup_descriptor_mappings(
5483 struct pvr_cmd_buffer *const cmd_buffer,
5484 enum pvr_stage_allocation stage,
5485 const struct pvr_stage_allocation_descriptor_state *descriptor_state,
5486 const pvr_dev_addr_t *const num_worgroups_buff_addr,
5487 uint32_t *const descriptor_data_offset_out)
5488 {
5489 const struct pvr_pds_info *const pds_info = &descriptor_state->pds_info;
5490 const struct pvr_descriptor_state *desc_state;
5491 const pco_data *data;
5492 struct pvr_suballoc_bo *pvr_bo;
5493 const uint8_t *entries;
5494 uint32_t *dword_buffer;
5495 uint64_t *qword_buffer;
5496 VkResult result;
5497
5498 if (!pds_info->data_size_in_dwords)
5499 return VK_SUCCESS;
5500
5501 result = pvr_arch_cmd_buffer_alloc_mem(
5502 cmd_buffer,
5503 cmd_buffer->device->heaps.pds_heap,
5504 PVR_DW_TO_BYTES(pds_info->data_size_in_dwords),
5505 &pvr_bo);
5506 if (result != VK_SUCCESS)
5507 return result;
5508
5509 dword_buffer = (uint32_t *)pvr_bo_suballoc_get_map_addr(pvr_bo);
5510 qword_buffer = (uint64_t *)pvr_bo_suballoc_get_map_addr(pvr_bo);
5511
5512 entries = (uint8_t *)pds_info->entries;
5513
5514 switch (stage) {
5515 case PVR_STAGE_ALLOCATION_VERTEX_GEOMETRY:
5516 desc_state = &cmd_buffer->state.gfx_desc_state;
5517 data = &cmd_buffer->state.gfx_pipeline->vs_data;
5518 break;
5519
5520 case PVR_STAGE_ALLOCATION_FRAGMENT:
5521 desc_state = &cmd_buffer->state.gfx_desc_state;
5522 data = &cmd_buffer->state.gfx_pipeline->fs_data;
5523 break;
5524
5525 case PVR_STAGE_ALLOCATION_COMPUTE:
5526 desc_state = &cmd_buffer->state.compute_desc_state;
5527 data = &cmd_buffer->state.compute_pipeline->cs_data;
5528 break;
5529
5530 default:
5531 UNREACHABLE("Unsupported stage.");
5532 break;
5533 }
5534
5535 for (uint32_t i = 0; i < pds_info->entry_count; i++) {
5536 const struct pvr_const_map_entry *const entry_header =
5537 (struct pvr_const_map_entry *)entries;
5538
5539 switch (entry_header->type) {
5540 case PVR_PDS_CONST_MAP_ENTRY_TYPE_LITERAL32: {
5541 const struct pvr_const_map_entry_literal32 *const literal =
5542 (struct pvr_const_map_entry_literal32 *)entries;
5543
5544 PVR_WRITE(dword_buffer,
5545 literal->literal_value,
5546 literal->const_offset,
5547 pds_info->data_size_in_dwords);
5548
5549 entries += sizeof(*literal);
5550 break;
5551 }
5552
5553 case PVR_PDS_CONST_MAP_ENTRY_TYPE_DESCRIPTOR_SET: {
5554 const struct pvr_const_map_entry_descriptor_set *desc_set_entry =
5555 (struct pvr_const_map_entry_descriptor_set *)entries;
5556 const uint32_t desc_set = desc_set_entry->descriptor_set;
5557 const struct pvr_descriptor_set *descriptor_set;
5558 pvr_dev_addr_t desc_set_addr;
5559
5560 assert(desc_set < PVR_MAX_DESCRIPTOR_SETS);
5561
5562 descriptor_set = desc_state->sets[desc_set];
5563 assert(descriptor_set);
5564
5565 desc_set_addr = descriptor_set->dev_addr;
5566 PVR_WRITE(qword_buffer,
5567 desc_set_addr.addr,
5568 desc_set_entry->const_offset,
5569 pds_info->data_size_in_dwords);
5570
5571 entries += sizeof(*desc_set_entry);
5572 break;
5573 }
5574
5575 case PVR_PDS_CONST_MAP_ENTRY_TYPE_SPECIAL_BUFFER: {
5576 const struct pvr_const_map_entry_special_buffer *special_buff_entry =
5577 (struct pvr_const_map_entry_special_buffer *)entries;
5578
5579 switch (special_buff_entry->buffer_type) {
5580 case PVR_BUFFER_TYPE_DYNAMIC: {
5581 unsigned desc_set = special_buff_entry->data;
5582 const struct pvr_descriptor_set *descriptor_set;
5583 struct pvr_suballoc_bo *dynamic_desc_bo;
5584
5585 assert(desc_set < PVR_MAX_DESCRIPTOR_SETS);
5586
5587 descriptor_set = desc_state->sets[desc_set];
5588 assert(descriptor_set);
5589
5590 result = pvr_arch_cmd_buffer_upload_general(
5591 cmd_buffer,
5592 descriptor_set->dynamic_buffers,
5593 special_buff_entry->size_in_dwords * sizeof(uint32_t),
5594 &dynamic_desc_bo);
5595
5596 if (result != VK_SUCCESS)
5597 return result;
5598
5599 PVR_WRITE(qword_buffer,
5600 dynamic_desc_bo->dev_addr.addr,
5601 special_buff_entry->const_offset,
5602 pds_info->data_size_in_dwords);
5603 break;
5604 }
5605
5606 case PVR_BUFFER_TYPE_PUSH_CONSTS: {
5607 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
5608
5609 /* Handle running with undefined push constants. */
5610 if (!state->push_consts[stage].dev_addr.addr) {
5611 state->push_consts[stage].dirty = true;
5612 assert(!state->push_consts[stage].bytes_updated);
5613 state->push_consts[stage].bytes_updated =
5614 sizeof(state->push_consts[stage].data);
5615
5616 result = pvr_cmd_upload_push_consts(cmd_buffer, stage);
5617
5618 /* Reset. */
5619 state->push_consts[stage].bytes_updated = 0;
5620
5621 if (result != VK_SUCCESS)
5622 return result;
5623 }
5624
5625 PVR_WRITE(qword_buffer,
5626 state->push_consts[stage].dev_addr.addr,
5627 special_buff_entry->const_offset,
5628 pds_info->data_size_in_dwords);
5629 break;
5630 }
5631
5632 case PVR_BUFFER_TYPE_BLEND_CONSTS: {
5633 const struct vk_color_blend_state *cb =
5634 &cmd_buffer->vk.dynamic_graphics_state.cb;
5635
5636 struct pvr_suballoc_bo *blend_consts_bo;
5637 result =
5638 pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5639 cb->blend_constants,
5640 sizeof(cb->blend_constants),
5641 &blend_consts_bo);
5642
5643 if (result != VK_SUCCESS)
5644 return result;
5645
5646 PVR_WRITE(qword_buffer,
5647 blend_consts_bo->dev_addr.addr,
5648 special_buff_entry->const_offset,
5649 pds_info->data_size_in_dwords);
5650 break;
5651 }
5652
5653 case PVR_BUFFER_TYPE_POINT_SAMPLER: {
5654 uint64_t point_sampler_words[ROGUE_NUM_TEXSTATE_SAMPLER_WORDS];
5655 pvr_csb_pack (&point_sampler_words[0],
5656 TEXSTATE_SAMPLER_WORD0,
5657 sampler) {
5658 sampler.addrmode_u = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_BORDER;
5659 sampler.addrmode_v = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_BORDER;
5660 sampler.addrmode_w = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_BORDER;
5661 sampler.dadjust = ROGUE_TEXSTATE_DADJUST_ZERO_UINT;
5662 sampler.minfilter = ROGUE_TEXSTATE_FILTER_POINT;
5663 sampler.magfilter = ROGUE_TEXSTATE_FILTER_POINT;
5664 sampler.maxlod = ROGUE_TEXSTATE_CLAMP_MAX;
5665 sampler.anisoctl = ROGUE_TEXSTATE_ANISOCTL_DISABLED;
5666 }
5667
5668 pvr_csb_pack (&point_sampler_words[1],
5669 TEXSTATE_SAMPLER_WORD1,
5670 sampler) {}
5671
5672 struct pvr_suballoc_bo *point_sampler_bo;
5673 result =
5674 pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5675 point_sampler_words,
5676 sizeof(point_sampler_words),
5677 &point_sampler_bo);
5678
5679 if (result != VK_SUCCESS)
5680 return result;
5681
5682 PVR_WRITE(qword_buffer,
5683 point_sampler_bo->dev_addr.addr,
5684 special_buff_entry->const_offset,
5685 pds_info->data_size_in_dwords);
5686 break;
5687 }
5688
5689 case PVR_BUFFER_TYPE_IA_SAMPLER: {
5690 uint64_t ia_sampler_words[ROGUE_NUM_TEXSTATE_SAMPLER_WORDS];
5691 pvr_csb_pack (&ia_sampler_words[0],
5692 TEXSTATE_SAMPLER_WORD0,
5693 sampler) {
5694 sampler.addrmode_u = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
5695 sampler.addrmode_v = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
5696 sampler.addrmode_w = ROGUE_TEXSTATE_ADDRMODE_CLAMP_TO_EDGE;
5697 sampler.dadjust = ROGUE_TEXSTATE_DADJUST_ZERO_UINT;
5698 sampler.magfilter = ROGUE_TEXSTATE_FILTER_POINT;
5699 sampler.minfilter = ROGUE_TEXSTATE_FILTER_POINT;
5700 sampler.anisoctl = ROGUE_TEXSTATE_ANISOCTL_DISABLED;
5701 sampler.non_normalized_coords = true;
5702 }
5703
5704 pvr_csb_pack (&ia_sampler_words[1],
5705 TEXSTATE_SAMPLER_WORD1,
5706 sampler) {}
5707
5708 struct pvr_suballoc_bo *ia_sampler_bo;
5709 result =
5710 pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5711 ia_sampler_words,
5712 sizeof(ia_sampler_words),
5713 &ia_sampler_bo);
5714
5715 if (result != VK_SUCCESS)
5716 return result;
5717
5718 PVR_WRITE(qword_buffer,
5719 ia_sampler_bo->dev_addr.addr,
5720 special_buff_entry->const_offset,
5721 pds_info->data_size_in_dwords);
5722 break;
5723 }
5724
5725 case PVR_BUFFER_TYPE_FRONT_FACE_OP: {
5726 VkFrontFace front_face =
5727 cmd_buffer->vk.dynamic_graphics_state.rs.front_face;
5728 VkPrimitiveTopology primitive_topology =
5729 cmd_buffer->vk.dynamic_graphics_state.ia.primitive_topology;
5730
5731 uint32_t ff_op;
5732 switch (primitive_topology) {
5733 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
5734 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
5735 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
5736 ff_op = PCO_FRONT_FACE_OP_TRUE;
5737 break;
5738
5739 default:
5740 ff_op = front_face == VK_FRONT_FACE_COUNTER_CLOCKWISE
5741 ? PCO_FRONT_FACE_OP_SWAP
5742 : PCO_FRONT_FACE_OP_NOP;
5743 break;
5744 }
5745
5746 struct pvr_suballoc_bo *ff_op_bo;
5747 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5748 &ff_op,
5749 sizeof(ff_op),
5750 &ff_op_bo);
5751
5752 if (result != VK_SUCCESS)
5753 return result;
5754
5755 PVR_WRITE(qword_buffer,
5756 ff_op_bo->dev_addr.addr,
5757 special_buff_entry->const_offset,
5758 pds_info->data_size_in_dwords);
5759 break;
5760 }
5761
5762 case PVR_BUFFER_TYPE_FS_META: {
5763 uint32_t fs_meta = 0;
5764
5765 if (cmd_buffer->vk.dynamic_graphics_state.ms.alpha_to_one_enable)
5766 fs_meta |= (1 << 0);
5767
5768 fs_meta |= cmd_buffer->vk.dynamic_graphics_state.ms.sample_mask
5769 << 9;
5770 fs_meta |=
5771 cmd_buffer->vk.dynamic_graphics_state.cb.color_write_enables
5772 << 1;
5773
5774 if (cmd_buffer->vk.dynamic_graphics_state.ms
5775 .alpha_to_coverage_enable)
5776 fs_meta |= (1 << 25);
5777
5778 struct pvr_suballoc_bo *fs_meta_bo;
5779 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5780 &fs_meta,
5781 sizeof(fs_meta),
5782 &fs_meta_bo);
5783
5784 if (result != VK_SUCCESS)
5785 return result;
5786
5787 PVR_WRITE(qword_buffer,
5788 fs_meta_bo->dev_addr.addr,
5789 special_buff_entry->const_offset,
5790 pds_info->data_size_in_dwords);
5791 break;
5792 }
5793
5794 case PVR_BUFFER_TYPE_TILE_BUFFERS: {
5795 const struct pvr_device_tile_buffer_state *tile_buffer_state =
5796 &cmd_buffer->device->tile_buffer_state;
5797 const struct pvr_graphics_pipeline *const gfx_pipeline =
5798 cmd_buffer->state.gfx_pipeline;
5799 const pco_data *const fs_data = &gfx_pipeline->fs_data;
5800
5801 unsigned num_tile_buffers =
5802 fs_data->fs.tile_buffers.count / fs_data->fs.tile_buffers.stride;
5803
5804 uint32_t tile_buffer_addrs[PVR_MAX_TILE_BUFFER_COUNT * 2];
5805
5806 for (unsigned u = 0; u < num_tile_buffers; ++u) {
5807 assert(u < tile_buffer_state->buffer_count);
5808 uint64_t tile_buffer_addr =
5809 tile_buffer_state->buffers[u]->vma->dev_addr.addr;
5810
5811 tile_buffer_addrs[2 * u] = tile_buffer_addr & 0xffffffff;
5812 tile_buffer_addrs[2 * u + 1] = tile_buffer_addr >> 32;
5813 }
5814
5815 struct pvr_suballoc_bo *tile_buffer_bo;
5816 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5817 &tile_buffer_addrs,
5818 num_tile_buffers *
5819 sizeof(uint64_t),
5820 &tile_buffer_bo);
5821
5822 if (result != VK_SUCCESS)
5823 return result;
5824
5825 PVR_WRITE(qword_buffer,
5826 tile_buffer_bo->dev_addr.addr,
5827 special_buff_entry->const_offset,
5828 pds_info->data_size_in_dwords);
5829 break;
5830 }
5831
5832 case PVR_BUFFER_TYPE_SPILL_INFO: {
5833 unsigned spill_block_size =
5834 data->common.spilled_temps * sizeof(uint32_t);
5835 spill_block_size = spill_block_size ? spill_block_size
5836 : sizeof(uint32_t);
5837
5838 size_t total_spill_mem_size =
5839 spill_block_size * rogue_get_total_instance_count(
5840 &cmd_buffer->device->pdevice->dev_info);
5841 struct pvr_suballoc_bo *spill_buffer_bo;
5842 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5843 NULL,
5844 total_spill_mem_size,
5845 &spill_buffer_bo);
5846
5847 if (result != VK_SUCCESS)
5848 return result;
5849
5850 uint32_t spill_info[3] = {
5851 [0] = spill_buffer_bo->dev_addr.addr & 0xffffffff,
5852 [1] = spill_buffer_bo->dev_addr.addr >> 32,
5853 [2] = spill_block_size,
5854 };
5855
5856 struct pvr_suballoc_bo *spill_info_bo;
5857 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5858 spill_info,
5859 sizeof(spill_info),
5860 &spill_info_bo);
5861
5862 if (result != VK_SUCCESS)
5863 return result;
5864
5865 PVR_WRITE(qword_buffer,
5866 spill_info_bo->dev_addr.addr,
5867 special_buff_entry->const_offset,
5868 pds_info->data_size_in_dwords);
5869 break;
5870 }
5871
5872 case PVR_BUFFER_TYPE_SCRATCH_INFO: {
5873 assert(data->common.scratch);
5874 unsigned scratch_block_size = data->common.scratch;
5875
5876 size_t total_scratch_mem_size =
5877 scratch_block_size * rogue_get_total_instance_count(
5878 &cmd_buffer->device->pdevice->dev_info);
5879 struct pvr_suballoc_bo *scratch_buffer_bo;
5880 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5881 NULL,
5882 total_scratch_mem_size,
5883 &scratch_buffer_bo);
5884
5885 if (result != VK_SUCCESS)
5886 return result;
5887
5888 uint32_t scratch_info[3] = {
5889 [0] = scratch_buffer_bo->dev_addr.addr & 0xffffffff,
5890 [1] = scratch_buffer_bo->dev_addr.addr >> 32,
5891 [2] = scratch_block_size,
5892 };
5893
5894 struct pvr_suballoc_bo *scratch_info_bo;
5895 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
5896 scratch_info,
5897 sizeof(scratch_info),
5898 &scratch_info_bo);
5899
5900 if (result != VK_SUCCESS)
5901 return result;
5902
5903 PVR_WRITE(qword_buffer,
5904 scratch_info_bo->dev_addr.addr,
5905 special_buff_entry->const_offset,
5906 pds_info->data_size_in_dwords);
5907 break;
5908 }
5909
5910 case PVR_BUFFER_TYPE_SAMPLE_LOCATIONS: {
5911 /* Standard sample locations. */
5912 uint32_t packed_sample_locations[] = {
5913 0x000044cc,
5914 0xeaa26e26,
5915 0x359db759,
5916 0x1ffb71d3,
5917 };
5918
5919 struct pvr_suballoc_bo *sample_locations_bo;
5920 result = pvr_arch_cmd_buffer_upload_general(
5921 cmd_buffer,
5922 &packed_sample_locations,
5923 sizeof(packed_sample_locations),
5924 &sample_locations_bo);
5925
5926 if (result != VK_SUCCESS)
5927 return result;
5928
5929 PVR_WRITE(qword_buffer,
5930 sample_locations_bo->dev_addr.addr,
5931 special_buff_entry->const_offset,
5932 pds_info->data_size_in_dwords);
5933 break;
5934 }
5935
5936 default:
5937 UNREACHABLE("Unsupported special buffer type.");
5938 }
5939
5940 entries += sizeof(*special_buff_entry);
5941 break;
5942 }
5943
5944 default:
5945 UNREACHABLE("Unsupported map entry type.");
5946 }
5947 }
5948
5949 *descriptor_data_offset_out =
5950 pvr_bo->dev_addr.addr -
5951 cmd_buffer->device->heaps.pds_heap->base_addr.addr;
5952
5953 return VK_SUCCESS;
5954 }
5955
5956 static void pvr_compute_update_shared(struct pvr_cmd_buffer *cmd_buffer,
5957 struct pvr_sub_cmd_compute *const sub_cmd)
5958 {
5959 const struct pvr_device *device = cmd_buffer->device;
5960 const struct pvr_physical_device *pdevice = device->pdevice;
5961 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
5962 struct pvr_csb *csb = &sub_cmd->control_stream;
5963 const struct pvr_compute_pipeline *pipeline = state->compute_pipeline;
5964 const uint32_t const_shared_regs = pipeline->cs_data.common.shareds;
5965 struct pvr_compute_kernel_info info;
5966
5967 /* No shared regs, no need to use an allocation kernel. */
5968 if (!const_shared_regs)
5969 return;
5970
5971 /* Accumulate the MAX number of shared registers across the kernels in this
5972 * dispatch. This is used by the FW for context switching, so must be large
5973 * enough to contain all the shared registers that might be in use for this
5974 * compute job. Coefficients don't need to be included as the context switch
5975 * will not happen within the execution of a single workgroup, thus nothing
5976 * needs to be preserved.
5977 */
5978 state->max_shared_regs = MAX2(state->max_shared_regs, const_shared_regs);
5979
5980 info = (struct pvr_compute_kernel_info){
5981 .indirect_buffer_addr = PVR_DEV_ADDR_INVALID,
5982 .sd_type = ROGUE_CDMCTRL_SD_TYPE_NONE,
5983
5984 .usc_target = ROGUE_CDMCTRL_USC_TARGET_ALL,
5985 .usc_common_shared = true,
5986 .usc_common_size =
5987 DIV_ROUND_UP(PVR_DW_TO_BYTES(const_shared_regs),
5988 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE),
5989
5990 .global_size = { 1, 1, 1 },
5991 .local_size = { 1, 1, 1 },
5992 };
5993
5994 /* Sometimes we don't have a secondary program if there were no constants to
5995 * write, but we still need to run a PDS program to accomplish the
5996 * allocation of the local/common store shared registers. Use the
5997 * pre-uploaded empty PDS program in this instance.
5998 */
5999 if (pipeline->descriptor_state.pds_info.code_size_in_dwords) {
6000 uint32_t pds_data_size_in_dwords =
6001 pipeline->descriptor_state.pds_info.data_size_in_dwords;
6002
6003 info.pds_data_offset = state->pds_compute_descriptor_data_offset;
6004 info.pds_data_size =
6005 DIV_ROUND_UP(PVR_DW_TO_BYTES(pds_data_size_in_dwords),
6006 ROGUE_CDMCTRL_KERNEL0_PDS_DATA_SIZE_UNIT_SIZE);
6007
6008 /* Check that we have upload the code section. */
6009 assert(pipeline->descriptor_state.pds_code.code_size);
6010 info.pds_code_offset = pipeline->descriptor_state.pds_code.code_offset;
6011 } else {
6012 const struct pvr_pds_upload *program = &device->pds_compute_empty_program;
6013
6014 info.pds_data_offset = program->data_offset;
6015 info.pds_data_size =
6016 DIV_ROUND_UP(PVR_DW_TO_BYTES(program->data_size),
6017 ROGUE_CDMCTRL_KERNEL0_PDS_DATA_SIZE_UNIT_SIZE);
6018 info.pds_code_offset = program->code_offset;
6019 }
6020
6021 /* We don't need to pad the workgroup size. */
6022
6023 info.max_instances =
6024 pvr_compute_flat_slot_size(pdevice, const_shared_regs, false, 1U);
6025
6026 pvr_compute_generate_control_stream(csb, sub_cmd, &info);
6027 }
6028
6029 void pvr_arch_compute_update_shared_private(
6030 struct pvr_cmd_buffer *cmd_buffer,
6031 struct pvr_sub_cmd_compute *const sub_cmd,
6032 struct pvr_private_compute_pipeline *pipeline)
6033 {
6034 const struct pvr_physical_device *pdevice = cmd_buffer->device->pdevice;
6035 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
6036 const uint32_t const_shared_regs = pipeline->const_shared_regs_count;
6037 struct pvr_csb *csb = &sub_cmd->control_stream;
6038 struct pvr_compute_kernel_info info;
6039
6040 /* No shared regs, no need to use an allocation kernel. */
6041 if (!const_shared_regs)
6042 return;
6043
6044 /* See comment in pvr_compute_update_shared() for details on this. */
6045 state->max_shared_regs = MAX2(state->max_shared_regs, const_shared_regs);
6046
6047 info = (struct pvr_compute_kernel_info){
6048 .indirect_buffer_addr = PVR_DEV_ADDR_INVALID,
6049 .usc_common_size =
6050 DIV_ROUND_UP(PVR_DW_TO_BYTES(const_shared_regs),
6051 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE),
6052 .pds_data_size =
6053 DIV_ROUND_UP(PVR_DW_TO_BYTES(pipeline->pds_shared_update_data_size_dw),
6054 ROGUE_CDMCTRL_KERNEL0_PDS_DATA_SIZE_UNIT_SIZE),
6055 .usc_target = ROGUE_CDMCTRL_USC_TARGET_ALL,
6056 .pds_data_offset = pipeline->pds_shared_update_data_offset,
6057 .pds_code_offset = pipeline->pds_shared_update_code_offset,
6058 .sd_type = ROGUE_CDMCTRL_SD_TYPE_NONE,
6059 .usc_common_shared = true,
6060 .global_size = { 1, 1, 1 },
6061 .local_size = { 1, 1, 1 },
6062 };
6063
6064 /* We don't need to pad the workgroup size. */
6065
6066 info.max_instances =
6067 pvr_compute_flat_slot_size(pdevice, const_shared_regs, false, 1U);
6068
6069 pvr_compute_generate_control_stream(csb, sub_cmd, &info);
6070 }
6071
6072 static uint32_t
6073 pvr_compute_flat_pad_workgroup_size(const struct pvr_physical_device *pdevice,
6074 uint32_t workgroup_size,
6075 uint32_t coeff_regs_count)
6076 {
6077 const struct pvr_device_runtime_info *dev_runtime_info =
6078 &pdevice->dev_runtime_info;
6079 const struct pvr_device_info *dev_info = &pdevice->dev_info;
6080 uint32_t max_avail_coeff_regs =
6081 dev_runtime_info->cdm_max_local_mem_size_regs;
6082 uint32_t coeff_regs_count_aligned =
6083 ALIGN_POT(coeff_regs_count,
6084 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE >> 2U);
6085
6086 /* If the work group size is > ROGUE_MAX_INSTANCES_PER_TASK. We now *always*
6087 * pad the work group size to the next multiple of
6088 * ROGUE_MAX_INSTANCES_PER_TASK.
6089 *
6090 * If we use more than 1/8th of the max coefficient registers then we round
6091 * work group size up to the next multiple of ROGUE_MAX_INSTANCES_PER_TASK
6092 */
6093 /* TODO: See if this can be optimized. */
6094 if (workgroup_size > ROGUE_MAX_INSTANCES_PER_TASK ||
6095 coeff_regs_count_aligned > (max_avail_coeff_regs / 8)) {
6096 assert(workgroup_size < rogue_get_compute_max_work_group_size(dev_info));
6097
6098 return ALIGN_POT(workgroup_size, ROGUE_MAX_INSTANCES_PER_TASK);
6099 }
6100
6101 return workgroup_size;
6102 }
6103
6104 void pvr_arch_compute_update_kernel_private(
6105 struct pvr_cmd_buffer *cmd_buffer,
6106 struct pvr_sub_cmd_compute *const sub_cmd,
6107 struct pvr_private_compute_pipeline *pipeline,
6108 const uint32_t global_workgroup_size[static const PVR_WORKGROUP_DIMENSIONS])
6109 {
6110 const struct pvr_physical_device *pdevice = cmd_buffer->device->pdevice;
6111 struct pvr_csb *csb = &sub_cmd->control_stream;
6112
6113 struct pvr_compute_kernel_info info = {
6114 .indirect_buffer_addr = PVR_DEV_ADDR_INVALID,
6115 .usc_target = ROGUE_CDMCTRL_USC_TARGET_ANY,
6116 .pds_temp_size =
6117 DIV_ROUND_UP(pipeline->pds_temps_used << 2U,
6118 ROGUE_CDMCTRL_KERNEL0_PDS_TEMP_SIZE_UNIT_SIZE),
6119
6120 .pds_data_size =
6121 DIV_ROUND_UP(PVR_DW_TO_BYTES(pipeline->pds_data_size_dw),
6122 ROGUE_CDMCTRL_KERNEL0_PDS_DATA_SIZE_UNIT_SIZE),
6123 .pds_data_offset = pipeline->pds_data_offset,
6124 .pds_code_offset = pipeline->pds_code_offset,
6125
6126 .sd_type = ROGUE_CDMCTRL_SD_TYPE_NONE,
6127
6128 .usc_unified_size =
6129 DIV_ROUND_UP(pipeline->unified_store_regs_count << 2U,
6130 ROGUE_CDMCTRL_KERNEL0_USC_UNIFIED_SIZE_UNIT_SIZE),
6131
6132 /* clang-format off */
6133 .global_size = {
6134 global_workgroup_size[0],
6135 global_workgroup_size[1],
6136 global_workgroup_size[2]
6137 },
6138 /* clang-format on */
6139 };
6140
6141 uint32_t work_size = pipeline->workgroup_size.width *
6142 pipeline->workgroup_size.height *
6143 pipeline->workgroup_size.depth;
6144 uint32_t coeff_regs =
6145 pipeline->coeff_regs_count + pipeline->const_shared_regs_count;
6146
6147 info.usc_common_size =
6148 DIV_ROUND_UP(PVR_DW_TO_BYTES(coeff_regs),
6149 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE);
6150
6151 /* Use a whole slot per workgroup. */
6152 work_size = MAX2(work_size, ROGUE_MAX_INSTANCES_PER_TASK);
6153
6154 if (pipeline->const_shared_regs_count > 0)
6155 info.sd_type = ROGUE_CDMCTRL_SD_TYPE_USC;
6156
6157 work_size =
6158 pvr_compute_flat_pad_workgroup_size(pdevice, work_size, coeff_regs);
6159
6160 info.local_size[0] = work_size;
6161 info.local_size[1] = 1U;
6162 info.local_size[2] = 1U;
6163
6164 info.max_instances =
6165 pvr_compute_flat_slot_size(pdevice, coeff_regs, false, work_size);
6166
6167 pvr_compute_generate_control_stream(csb, sub_cmd, &info);
6168 }
6169
6170 static void pvr_compute_update_kernel(
6171 struct pvr_cmd_buffer *cmd_buffer,
6172 struct pvr_sub_cmd_compute *const sub_cmd,
6173 pvr_dev_addr_t indirect_addr,
6174 const uint32_t global_base_group[static const PVR_WORKGROUP_DIMENSIONS],
6175 const uint32_t global_workgroup_size[static const PVR_WORKGROUP_DIMENSIONS])
6176 {
6177 const struct pvr_physical_device *pdevice = cmd_buffer->device->pdevice;
6178 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
6179 struct pvr_csb *csb = &sub_cmd->control_stream;
6180 const struct pvr_compute_pipeline *pipeline = state->compute_pipeline;
6181 const pco_data *const cs_data = &pipeline->cs_data;
6182 const struct pvr_pds_info *program_info = &pipeline->pds_cs_program_info;
6183 bool uses_wg_id = pipeline->base_workgroup_data_patching_offset != ~0u;
6184 bool uses_num_wgs = pipeline->num_workgroups_data_patching_offset != ~0u;
6185 bool base_group_set = !!global_base_group[0] || !!global_base_group[1] ||
6186 !!global_base_group[2];
6187 uint32_t pds_data_offset = pipeline->pds_cs_program.data_offset;
6188
6189 /* Does the PDS data segment need patching, or can the default be used? */
6190 if ((uses_wg_id && base_group_set) || uses_num_wgs) {
6191 struct pvr_pds_upload pds_data_upload;
6192 uint32_t *pds_data;
6193
6194 /* Upload and patch PDS data segment. */
6195 pvr_cmd_buffer_upload_pds_data(cmd_buffer,
6196 pipeline->pds_cs_data_section,
6197 program_info->data_size_in_dwords,
6198 16,
6199 &pds_data_upload);
6200 pds_data_offset = pds_data_upload.data_offset;
6201 pds_data = pvr_bo_suballoc_get_map_addr(pds_data_upload.pvr_bo);
6202
6203 if (uses_wg_id && base_group_set) {
6204 unsigned offset = pipeline->base_workgroup_data_patching_offset;
6205 for (unsigned u = 0; u < PVR_WORKGROUP_DIMENSIONS; ++u) {
6206 pds_data[offset + u] = global_base_group[u];
6207 }
6208 }
6209
6210 if (uses_num_wgs) {
6211 if (indirect_addr.addr) {
6212 unsigned offset =
6213 pipeline->num_workgroups_indirect_src_patching_offset;
6214
6215 uint64_t *pds_data64 =
6216 pvr_bo_suballoc_get_map_addr(pds_data_upload.pvr_bo);
6217 pds_data64[offset / 2] = indirect_addr.addr;
6218
6219 offset = pipeline->num_workgroups_indirect_src_dma_patching_offset;
6220
6221 pds_data[offset] |=
6222 3 << PVR_ROGUE_PDSINST_DOUT_FIELDS_DOUTD_SRC1_BSIZE_SHIFT;
6223 }
6224
6225 unsigned offset = pipeline->num_workgroups_data_patching_offset;
6226 for (unsigned u = 0; u < PVR_WORKGROUP_DIMENSIONS; ++u) {
6227 pds_data[offset + u] = global_workgroup_size[u];
6228 }
6229 }
6230 }
6231
6232 struct pvr_compute_kernel_info info = {
6233 .indirect_buffer_addr = indirect_addr,
6234 .usc_target = ROGUE_CDMCTRL_USC_TARGET_ANY,
6235 .pds_temp_size =
6236 DIV_ROUND_UP(program_info->temps_required << 2U,
6237 ROGUE_CDMCTRL_KERNEL0_PDS_TEMP_SIZE_UNIT_SIZE),
6238
6239 .pds_data_size =
6240 DIV_ROUND_UP(PVR_DW_TO_BYTES(program_info->data_size_in_dwords),
6241 ROGUE_CDMCTRL_KERNEL0_PDS_DATA_SIZE_UNIT_SIZE),
6242 .pds_data_offset = pds_data_offset,
6243 .pds_code_offset = pipeline->pds_cs_program.code_offset,
6244
6245 .sd_type = ROGUE_CDMCTRL_SD_TYPE_NONE,
6246
6247 .usc_unified_size =
6248 DIV_ROUND_UP(cs_data->common.vtxins << 2U,
6249 ROGUE_CDMCTRL_KERNEL0_USC_UNIFIED_SIZE_UNIT_SIZE),
6250
6251 /* clang-format off */
6252 .global_size = {
6253 global_workgroup_size[0],
6254 global_workgroup_size[1],
6255 global_workgroup_size[2]
6256 },
6257 /* clang-format on */
6258 };
6259
6260 uint32_t work_size = cs_data->cs.workgroup_size[0] *
6261 cs_data->cs.workgroup_size[1] *
6262 cs_data->cs.workgroup_size[2];
6263 uint32_t coeff_regs = cs_data->common.coeffs + cs_data->common.shareds;
6264
6265 info.usc_common_size =
6266 DIV_ROUND_UP(PVR_DW_TO_BYTES(coeff_regs),
6267 ROGUE_CDMCTRL_KERNEL0_USC_COMMON_SIZE_UNIT_SIZE);
6268
6269 /* Use a whole slot per workgroup. */
6270 work_size = MAX2(work_size, ROGUE_MAX_INSTANCES_PER_TASK);
6271
6272 if (cs_data->common.shareds > 0)
6273 info.sd_type = ROGUE_CDMCTRL_SD_TYPE_USC;
6274
6275 work_size =
6276 pvr_compute_flat_pad_workgroup_size(pdevice, work_size, coeff_regs);
6277
6278 info.local_size[0] = work_size;
6279 info.local_size[1] = 1U;
6280 info.local_size[2] = 1U;
6281
6282 info.max_instances =
6283 pvr_compute_flat_slot_size(pdevice, coeff_regs, false, work_size);
6284
6285 pvr_compute_generate_control_stream(csb, sub_cmd, &info);
6286 }
6287
6288 static VkResult pvr_cmd_upload_push_consts(struct pvr_cmd_buffer *cmd_buffer,
6289 enum pvr_stage_allocation stage)
6290 {
6291 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
6292 struct pvr_push_constants *push_consts = &state->push_consts[stage];
6293 struct pvr_suballoc_bo *suballoc_bo;
6294 VkResult result;
6295
6296 if (!push_consts->dirty)
6297 return VK_SUCCESS;
6298
6299 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
6300 push_consts->data,
6301 push_consts->bytes_updated,
6302 &suballoc_bo);
6303 if (result != VK_SUCCESS)
6304 return result;
6305
6306 push_consts->dev_addr = suballoc_bo->dev_addr;
6307 push_consts->dirty = false;
6308
6309 return VK_SUCCESS;
6310 }
6311
6312 static void pvr_cmd_dispatch(
6313 struct pvr_cmd_buffer *const cmd_buffer,
6314 const pvr_dev_addr_t indirect_addr,
6315 const uint32_t base_group[static const PVR_WORKGROUP_DIMENSIONS],
6316 const uint32_t workgroup_size[static const PVR_WORKGROUP_DIMENSIONS])
6317 {
6318 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
6319 const struct pvr_compute_pipeline *compute_pipeline =
6320 state->compute_pipeline;
6321 const pco_data *const cs_data = &compute_pipeline->cs_data;
6322 struct pvr_sub_cmd_compute *sub_cmd;
6323 VkResult result;
6324
6325 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_COMPUTE);
6326
6327 sub_cmd = &state->current_sub_cmd->compute;
6328 sub_cmd->uses_atomic_ops |= cs_data->common.uses.atomics;
6329 sub_cmd->uses_barrier |= cs_data->common.uses.barriers;
6330
6331 if (state->push_consts[PVR_STAGE_ALLOCATION_COMPUTE].dirty) {
6332 result =
6333 pvr_cmd_upload_push_consts(cmd_buffer, PVR_STAGE_ALLOCATION_COMPUTE);
6334 if (result != VK_SUCCESS)
6335 return;
6336
6337 /* Regenerate the PDS program to use the new push consts buffer. */
6338 state->dirty.compute_desc_dirty = true;
6339 }
6340
6341 if (state->dirty.compute_desc_dirty ||
6342 state->dirty.compute_pipeline_binding) {
6343 result = pvr_setup_descriptor_mappings(
6344 cmd_buffer,
6345 PVR_STAGE_ALLOCATION_COMPUTE,
6346 &compute_pipeline->descriptor_state,
6347 NULL,
6348 &state->pds_compute_descriptor_data_offset);
6349 if (result != VK_SUCCESS)
6350 return;
6351 }
6352
6353 pvr_compute_update_shared(cmd_buffer, sub_cmd);
6354 pvr_compute_update_kernel(cmd_buffer,
6355 sub_cmd,
6356 indirect_addr,
6357 base_group,
6358 workgroup_size);
6359 }
6360
6361 void PVR_PER_ARCH(CmdDispatchBase)(VkCommandBuffer commandBuffer,
6362 uint32_t baseGroupX,
6363 uint32_t baseGroupY,
6364 uint32_t baseGroupZ,
6365 uint32_t groupCountX,
6366 uint32_t groupCountY,
6367 uint32_t groupCountZ)
6368 {
6369 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
6370
6371 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
6372
6373 if (!groupCountX || !groupCountY || !groupCountZ)
6374 return;
6375
6376 pvr_cmd_dispatch(cmd_buffer,
6377 PVR_DEV_ADDR_INVALID,
6378 (uint32_t[]){ baseGroupX, baseGroupY, baseGroupZ },
6379 (uint32_t[]){ groupCountX, groupCountY, groupCountZ });
6380 }
6381
6382 void PVR_PER_ARCH(CmdDispatchIndirect)(VkCommandBuffer commandBuffer,
6383 VkBuffer _buffer,
6384 VkDeviceSize offset)
6385 {
6386 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
6387 VK_FROM_HANDLE(pvr_buffer, buffer, _buffer);
6388
6389 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
6390
6391 pvr_cmd_dispatch(cmd_buffer,
6392 PVR_DEV_ADDR_OFFSET(buffer->dev_addr, offset),
6393 (uint32_t[]){ 0, 0, 0 },
6394 (uint32_t[]){ 1, 1, 1 });
6395 }
6396
6397 static void
6398 pvr_update_draw_state(struct pvr_cmd_buffer_state *const state,
6399 const struct pvr_cmd_buffer_draw_state *const draw_state)
6400 {
6401 /* We don't have a state to tell us that base_instance is being used so it
6402 * gets used as a boolean - 0 means we'll use a pds program that skips the
6403 * base instance addition. If the base_instance gets used (and the last
6404 * draw's base_instance was 0) then we switch to the BASE_INSTANCE attrib
6405 * program.
6406 *
6407 * If base_instance changes then we only need to update the data section.
6408 *
6409 * The only draw call state that doesn't really matter is the start vertex
6410 * as that is handled properly in the VDM state in all cases.
6411 */
6412 if ((state->draw_state.draw_indexed != draw_state->draw_indexed) ||
6413 (state->draw_state.draw_indirect != draw_state->draw_indirect) ||
6414 (state->draw_state.base_instance == 0 &&
6415 draw_state->base_instance != 0)) {
6416 state->dirty.draw_variant = true;
6417 } else if (state->draw_state.base_instance != draw_state->base_instance) {
6418 state->dirty.draw_base_instance = true;
6419 }
6420
6421 state->draw_state = *draw_state;
6422 }
6423
6424 static uint32_t pvr_calc_shared_regs_count(
6425 const struct pvr_graphics_pipeline *const gfx_pipeline)
6426 {
6427 uint32_t shared_regs = gfx_pipeline->vs_data.common.shareds;
6428
6429 if (gfx_pipeline->shader_state.fragment.shader_bo) {
6430 uint32_t fragment_regs = gfx_pipeline->fs_data.common.shareds;
6431 shared_regs = MAX2(shared_regs, fragment_regs);
6432 }
6433
6434 return shared_regs;
6435 }
6436
6437 static void
6438 pvr_emit_dirty_pds_state(const struct pvr_cmd_buffer *const cmd_buffer,
6439 struct pvr_sub_cmd_gfx *const sub_cmd,
6440 const uint32_t pds_vertex_descriptor_data_offset)
6441 {
6442 const struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
6443 const struct pvr_stage_allocation_descriptor_state
6444 *const vertex_descriptor_state =
6445 &state->gfx_pipeline->shader_state.vertex.descriptor_state;
6446 const pco_data *const vs_data = &state->gfx_pipeline->vs_data;
6447 struct pvr_csb *const csb = &sub_cmd->control_stream;
6448
6449 if (!vertex_descriptor_state->pds_info.code_size_in_dwords)
6450 return;
6451
6452 pvr_csb_set_relocation_mark(csb);
6453
6454 pvr_csb_emit (csb, VDMCTRL_PDS_STATE0, state0) {
6455 state0.usc_target = ROGUE_VDMCTRL_USC_TARGET_ALL;
6456
6457 state0.usc_common_size =
6458 DIV_ROUND_UP(PVR_DW_TO_BYTES(vs_data->common.shareds),
6459 ROGUE_VDMCTRL_PDS_STATE0_USC_COMMON_SIZE_UNIT_SIZE);
6460
6461 state0.pds_data_size = DIV_ROUND_UP(
6462 PVR_DW_TO_BYTES(vertex_descriptor_state->pds_info.data_size_in_dwords),
6463 ROGUE_VDMCTRL_PDS_STATE0_PDS_DATA_SIZE_UNIT_SIZE);
6464 }
6465
6466 pvr_csb_emit (csb, VDMCTRL_PDS_STATE1, state1) {
6467 state1.pds_data_addr = PVR_DEV_ADDR(pds_vertex_descriptor_data_offset);
6468 state1.sd_type = ROGUE_VDMCTRL_SD_TYPE_NONE;
6469 }
6470
6471 pvr_csb_emit (csb, VDMCTRL_PDS_STATE2, state2) {
6472 state2.pds_code_addr =
6473 PVR_DEV_ADDR(vertex_descriptor_state->pds_code.code_offset);
6474 }
6475
6476 pvr_csb_clear_relocation_mark(csb);
6477 }
6478
6479 static void pvr_setup_output_select(struct pvr_cmd_buffer *const cmd_buffer)
6480 {
6481 const struct pvr_graphics_pipeline *const gfx_pipeline =
6482 cmd_buffer->state.gfx_pipeline;
6483 struct ROGUE_TA_STATE_HEADER *const header = &cmd_buffer->state.emit_header;
6484 struct pvr_ppp_state *const ppp_state = &cmd_buffer->state.ppp_state;
6485 const pco_data *const vs_data = &gfx_pipeline->vs_data;
6486 const pco_data *const fs_data = &gfx_pipeline->fs_data;
6487 uint32_t output_selects;
6488 uint32_t varying[2];
6489
6490 const pco_range *varyings = vs_data->vs.varyings;
6491
6492 const bool has_point_size = varyings[VARYING_SLOT_PSIZ].count > 0;
6493
6494 const bool has_viewport = varyings[VARYING_SLOT_VIEWPORT].count > 0;
6495
6496 const bool has_layer = varyings[VARYING_SLOT_LAYER].count > 0;
6497
6498 const unsigned clip_count = vs_data->vs.clip_count;
6499 const unsigned cull_count = vs_data->vs.cull_count;
6500 const unsigned clip_cull = clip_count + cull_count;
6501
6502 pvr_csb_pack (&output_selects, TA_OUTPUT_SEL, state) {
6503 state.rhw_pres = fs_data->fs.uses.w;
6504 state.tsp_unclamped_z_pres = fs_data->fs.uses.z;
6505
6506 state.vtxsize = vs_data->vs.vtxouts;
6507 state.psprite_size_pres = has_point_size;
6508 state.vpt_tgt_pres = has_viewport;
6509 state.render_tgt_pres = has_layer;
6510
6511 state.plane0 = clip_cull > 0;
6512 state.plane1 = clip_cull > 1;
6513 state.plane2 = clip_cull > 2;
6514 state.plane3 = clip_cull > 3;
6515 state.plane4 = clip_cull > 4;
6516 state.plane5 = clip_cull > 5;
6517 state.plane6 = clip_cull > 6;
6518 state.plane7 = clip_cull > 7;
6519
6520 state.cullplane0 = (clip_cull > 0) && (clip_count < 1);
6521 state.cullplane1 = (clip_cull > 1) && (clip_count < 2);
6522 state.cullplane2 = (clip_cull > 2) && (clip_count < 3);
6523 state.cullplane3 = (clip_cull > 3) && (clip_count < 4);
6524 state.cullplane4 = (clip_cull > 4) && (clip_count < 5);
6525 state.cullplane5 = (clip_cull > 5) && (clip_count < 6);
6526 state.cullplane6 = (clip_cull > 6) && (clip_count < 7);
6527 state.cullplane7 = (clip_cull > 7) && (clip_count < 8);
6528 }
6529
6530 if (ppp_state->output_selects != output_selects) {
6531 ppp_state->output_selects = output_selects;
6532 header->pres_outselects = true;
6533 }
6534
6535 pvr_csb_pack (&varying[0], TA_STATE_VARYING0, varying0) {
6536 varying0.f32_linear = vs_data->vs.f32_smooth;
6537 varying0.f32_flat = vs_data->vs.f32_flat;
6538 varying0.f32_npc = vs_data->vs.f32_npc;
6539 }
6540
6541 if (ppp_state->varying_word[0] != varying[0]) {
6542 ppp_state->varying_word[0] = varying[0];
6543 header->pres_varying_word0 = true;
6544 }
6545
6546 pvr_csb_pack (&varying[1], TA_STATE_VARYING1, varying1) {
6547 varying1.f16_linear = vs_data->vs.f16_smooth;
6548 varying1.f16_flat = vs_data->vs.f16_flat;
6549 varying1.f16_npc = vs_data->vs.f16_npc;
6550 }
6551
6552 if (ppp_state->varying_word[1] != varying[1]) {
6553 ppp_state->varying_word[1] = varying[1];
6554 header->pres_varying_word1 = true;
6555 }
6556 }
6557
6558 static bool
6559 pvr_needs_fs_passthrough(const struct vk_dynamic_graphics_state *dynamic_state)
6560 {
6561 if (dynamic_state->rs.rasterizer_discard_enable)
6562 return false;
6563
6564 uint32_t full_sample_mask =
6565 BITFIELD_MASK(dynamic_state->ms.rasterization_samples);
6566
6567 bool partial_sample_mask =
6568 (dynamic_state->ms.sample_mask & full_sample_mask) != full_sample_mask;
6569
6570 return partial_sample_mask;
6571 }
6572
6573 static void
6574 pvr_setup_isp_faces_and_control(struct pvr_cmd_buffer *const cmd_buffer,
6575 struct ROGUE_TA_STATE_ISPA *const ispa_out)
6576 {
6577 struct ROGUE_TA_STATE_HEADER *const header = &cmd_buffer->state.emit_header;
6578 const struct pvr_fragment_shader_state *const fragment_shader_state =
6579 &cmd_buffer->state.gfx_pipeline->shader_state.fragment;
6580 const struct pvr_render_pass_info *const pass_info =
6581 &cmd_buffer->state.render_pass_info;
6582 struct vk_dynamic_graphics_state *dynamic_state =
6583 &cmd_buffer->vk.dynamic_graphics_state;
6584 struct pvr_ppp_state *const ppp_state = &cmd_buffer->state.ppp_state;
6585 bool skip_fs = fragment_shader_state->is_passthrough &&
6586 !pvr_needs_fs_passthrough(dynamic_state);
6587
6588 const bool rasterizer_discard = dynamic_state->rs.rasterizer_discard_enable;
6589 const uint32_t subpass_idx = pass_info->subpass_idx;
6590 const uint32_t depth_stencil_attachment_idx =
6591 pass_info->pass
6592 ? pass_info->pass->subpasses[subpass_idx].depth_stencil_attachment
6593 : pass_info->dr_info->hw_render.ds_attach_idx;
6594 struct pvr_render_pass_attachment *attachment;
6595
6596 if (pass_info->pass) {
6597 attachment =
6598 depth_stencil_attachment_idx != VK_ATTACHMENT_UNUSED
6599 ? &pass_info->pass->attachments[depth_stencil_attachment_idx]
6600 : NULL;
6601 } else {
6602 attachment =
6603 depth_stencil_attachment_idx != VK_ATTACHMENT_UNUSED
6604 ? &pass_info->dr_info->attachments[depth_stencil_attachment_idx]
6605 : NULL;
6606 }
6607
6608 const enum ROGUE_TA_OBJTYPE obj_type =
6609 pvr_ta_objtype(dynamic_state->ia.primitive_topology);
6610
6611 VkImageAspectFlags ds_aspects =
6612 (!rasterizer_discard && attachment)
6613 ? vk_format_aspects(attachment->vk_format) &
6614 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)
6615 : VK_IMAGE_ASPECT_NONE;
6616
6617 /* Dynamic rendering attachments could be bound with a D+S format but only
6618 * D or S active.
6619 */
6620 if (!pass_info->pass && attachment && !attachment->is_depth)
6621 ds_aspects &= ~VK_IMAGE_ASPECT_DEPTH_BIT;
6622 if (!pass_info->pass && attachment && !attachment->is_stencil)
6623 ds_aspects &= ~VK_IMAGE_ASPECT_STENCIL_BIT;
6624
6625 /* This is deliberately a full copy rather than a pointer because
6626 * vk_optimize_depth_stencil_state() can only be run once against any given
6627 * instance of vk_depth_stencil_state.
6628 */
6629 struct vk_depth_stencil_state ds_state = dynamic_state->ds;
6630
6631 uint32_t ispb_stencil_off;
6632 bool is_two_sided = false;
6633 uint32_t isp_control;
6634
6635 uint32_t line_width;
6636 uint32_t common_a;
6637 uint32_t front_a;
6638 uint32_t front_b;
6639 uint32_t back_a;
6640 uint32_t back_b;
6641
6642 vk_optimize_depth_stencil_state(&ds_state, ds_aspects, true);
6643
6644 /* Convert to 4.4 fixed point format. */
6645 line_width = util_unsigned_fixed(dynamic_state->rs.line.width, 4);
6646
6647 /* Subtract 1 to shift values from range [0=0,256=16] to [0=1/16,255=16].
6648 * If 0 it stays at 0, otherwise we subtract 1.
6649 */
6650 line_width = (!!line_width) * (line_width - 1);
6651
6652 line_width = MIN2(line_width, ROGUE_TA_STATE_ISPA_POINTLINEWIDTH_SIZE_MAX);
6653
6654 /* TODO: Part of the logic in this function is duplicated in another part
6655 * of the code. E.g. the dcmpmode, and sop1/2/3. Could we do this earlier?
6656 */
6657
6658 pvr_csb_pack (&common_a, TA_STATE_ISPA, ispa) {
6659 ispa.pointlinewidth = line_width;
6660
6661 ispa.dcmpmode = pvr_ta_cmpmode(ds_state.depth.compare_op);
6662 ispa.dwritedisable = !ds_state.depth.write_enable;
6663
6664 ispa.passtype = fragment_shader_state->pass_type;
6665
6666 if (dynamic_state->cb.logic_op_enable &&
6667 fragment_shader_state->pass_type == ROGUE_TA_PASSTYPE_OPAQUE)
6668 ispa.passtype = ROGUE_TA_PASSTYPE_TRANSLUCENT;
6669
6670 if (skip_fs)
6671 ispa.passtype = ROGUE_TA_PASSTYPE_OPAQUE;
6672
6673 ispa.objtype = obj_type;
6674
6675 /* Return unpacked ispa structure. dcmpmode, dwritedisable, passtype and
6676 * objtype are needed by pvr_setup_triangle_merging_flag.
6677 */
6678 if (ispa_out)
6679 *ispa_out = ispa;
6680 }
6681
6682 /* TODO: Does this actually represent the ispb control word on stencil off?
6683 * If not, rename the variable.
6684 */
6685 pvr_csb_pack (&ispb_stencil_off, TA_STATE_ISPB, ispb) {
6686 ispb.sop3 = ROGUE_TA_ISPB_STENCILOP_KEEP;
6687 ispb.sop2 = ROGUE_TA_ISPB_STENCILOP_KEEP;
6688 ispb.sop1 = ROGUE_TA_ISPB_STENCILOP_KEEP;
6689 ispb.scmpmode = ROGUE_TA_CMPMODE_ALWAYS;
6690 }
6691
6692 /* FIXME: This logic should be redone and improved. Can we also get rid of
6693 * the front and back variants?
6694 */
6695
6696 front_a = common_a;
6697 back_a = common_a;
6698
6699 if (ds_state.stencil.test_enable) {
6700 uint32_t front_a_sref;
6701 uint32_t back_a_sref;
6702
6703 pvr_csb_pack (&front_a_sref, TA_STATE_ISPA, ispa) {
6704 ispa.sref = ds_state.stencil.front.reference;
6705 }
6706 front_a |= front_a_sref;
6707
6708 pvr_csb_pack (&back_a_sref, TA_STATE_ISPA, ispa) {
6709 ispa.sref = ds_state.stencil.back.reference;
6710 }
6711 back_a |= back_a_sref;
6712
6713 pvr_csb_pack (&front_b, TA_STATE_ISPB, ispb) {
6714 const struct vk_stencil_test_face_state *const front =
6715 &ds_state.stencil.front;
6716
6717 if (ds_state.stencil.write_enable)
6718 ispb.swmask = front->write_mask;
6719
6720 ispb.scmpmask = front->compare_mask;
6721
6722 ispb.sop3 = pvr_ta_stencilop(front->op.pass);
6723 ispb.sop2 = pvr_ta_stencilop(front->op.depth_fail);
6724 ispb.sop1 = pvr_ta_stencilop(front->op.fail);
6725 ispb.scmpmode = pvr_ta_cmpmode(front->op.compare);
6726 }
6727
6728 pvr_csb_pack (&back_b, TA_STATE_ISPB, ispb) {
6729 const struct vk_stencil_test_face_state *const back =
6730 &ds_state.stencil.back;
6731
6732 if (ds_state.stencil.write_enable)
6733 ispb.swmask = back->write_mask;
6734
6735 ispb.scmpmask = back->compare_mask;
6736
6737 ispb.sop3 = pvr_ta_stencilop(back->op.pass);
6738 ispb.sop2 = pvr_ta_stencilop(back->op.depth_fail);
6739 ispb.sop1 = pvr_ta_stencilop(back->op.fail);
6740 ispb.scmpmode = pvr_ta_cmpmode(back->op.compare);
6741 }
6742 } else {
6743 front_b = ispb_stencil_off;
6744 back_b = ispb_stencil_off;
6745 }
6746
6747 if (front_a != back_a || front_b != back_b) {
6748 if (dynamic_state->rs.cull_mode & VK_CULL_MODE_BACK_BIT) {
6749 /* Single face, using front state. */
6750 } else if (dynamic_state->rs.cull_mode & VK_CULL_MODE_FRONT_BIT) {
6751 /* Single face, using back state. */
6752
6753 front_a = back_a;
6754 front_b = back_b;
6755 } else {
6756 /* Both faces. */
6757
6758 header->pres_ispctl_ba = is_two_sided = true;
6759
6760 if (dynamic_state->rs.front_face == VK_FRONT_FACE_COUNTER_CLOCKWISE) {
6761 uint32_t tmp = front_a;
6762
6763 front_a = back_a;
6764 back_a = tmp;
6765
6766 tmp = front_b;
6767 front_b = back_b;
6768 back_b = tmp;
6769 }
6770
6771 /* HW defaults to stencil off. */
6772 if (back_b != ispb_stencil_off) {
6773 header->pres_ispctl_fb = true;
6774 header->pres_ispctl_bb = true;
6775 }
6776 }
6777 }
6778
6779 if (ds_state.stencil.test_enable && front_b != ispb_stencil_off)
6780 header->pres_ispctl_fb = true;
6781
6782 pvr_csb_pack (&isp_control, TA_STATE_ISPCTL, ispctl) {
6783 ispctl.upass = pass_info->isp_userpass;
6784
6785 /* TODO: is shader_bo ever NULL? Figure out what to do. */
6786 ispctl.tagwritedisable = rasterizer_discard ||
6787 !fragment_shader_state->shader_bo || skip_fs;
6788
6789 ispctl.two_sided = is_two_sided;
6790 ispctl.bpres = header->pres_ispctl_fb || header->pres_ispctl_bb;
6791
6792 ispctl.dbenable = !rasterizer_discard &&
6793 dynamic_state->rs.depth_bias.enable &&
6794 obj_type == ROGUE_TA_OBJTYPE_TRIANGLE;
6795 if (!rasterizer_discard && cmd_buffer->state.vis_test_enabled) {
6796 ispctl.vistest = true;
6797 ispctl.visreg = cmd_buffer->state.vis_reg;
6798 }
6799
6800 ispctl.scenable = !rasterizer_discard;
6801
6802 ppp_state->isp.control_struct = ispctl;
6803 }
6804
6805 header->pres_ispctl = true;
6806
6807 ppp_state->isp.control = isp_control;
6808 ppp_state->isp.front_a = front_a;
6809 ppp_state->isp.front_b = front_b;
6810 ppp_state->isp.back_a = back_a;
6811 ppp_state->isp.back_b = back_b;
6812 }
6813
6814 static float
6815 pvr_calculate_final_depth_bias_contant_factor(struct pvr_device_info *dev_info,
6816 VkFormat format,
6817 float depth_bias)
6818 {
6819 /* Information for future modifiers of these depth bias calculations.
6820 * ==================================================================
6821 * Specified depth bias equations scale the specified constant factor by a
6822 * value 'r' that is guaranteed to cause a resolvable difference in depth
6823 * across the entire range of depth values.
6824 * For floating point depth formats 'r' is calculated by taking the maximum
6825 * exponent across the triangle.
6826 * For UNORM formats 'r' is constant.
6827 * Here 'n' is the number of mantissa bits stored in the floating point
6828 * representation (23 for F32).
6829 *
6830 * UNORM Format -> z += dbcf * r + slope
6831 * FLOAT Format -> z += dbcf * 2^(e-n) + slope
6832 *
6833 * HW Variations.
6834 * ==============
6835 * The HW either always performs the F32 depth bias equation (exponent based
6836 * r), or in the case of HW that correctly supports the integer depth bias
6837 * equation for UNORM depth formats, we can select between both equations
6838 * using the ROGUE_CR_ISP_CTL.dbias_is_int flag - this is required to
6839 * correctly perform Vulkan UNORM depth bias (constant r).
6840 *
6841 * if ern42307:
6842 * if DBIAS_IS_INT_EN:
6843 * z += dbcf + slope
6844 * else:
6845 * z += dbcf * 2^(e-n) + slope
6846 * else:
6847 * z += dbcf * 2^(e-n) + slope
6848 *
6849 */
6850
6851 float nudge_factor;
6852
6853 if (PVR_HAS_ENHANCEMENT(dev_info, 42307)) {
6854 switch (format) {
6855 case VK_FORMAT_D16_UNORM:
6856 return depth_bias / (1 << 15);
6857
6858 case VK_FORMAT_D24_UNORM_S8_UINT:
6859 case VK_FORMAT_X8_D24_UNORM_PACK32:
6860 return depth_bias / (1 << 23);
6861
6862 default:
6863 return depth_bias;
6864 }
6865 }
6866
6867 /* The reasoning behind clamping/nudging the value here is because UNORM
6868 * depth formats can have higher precision over our underlying D32F
6869 * representation for some depth ranges.
6870 *
6871 * When the HW scales the depth bias value by 2^(e-n) [The 'r' term'] a depth
6872 * bias of 1 can result in a value smaller than one F32 ULP, which will get
6873 * quantized to 0 - resulting in no bias.
6874 *
6875 * Biasing small values away from zero will ensure that small depth biases of
6876 * 1 still yield a result and overcome Z-fighting.
6877 */
6878 switch (format) {
6879 case VK_FORMAT_D16_UNORM:
6880 depth_bias *= 512.0f;
6881 nudge_factor = 1.0f;
6882 break;
6883
6884 case VK_FORMAT_D24_UNORM_S8_UINT:
6885 case VK_FORMAT_X8_D24_UNORM_PACK32:
6886 depth_bias *= 2.0f;
6887 nudge_factor = 2.0f;
6888 break;
6889
6890 default:
6891 nudge_factor = 0.0f;
6892 break;
6893 }
6894
6895 if (nudge_factor != 0.0f) {
6896 if (depth_bias < 0.0f && depth_bias > -nudge_factor)
6897 depth_bias -= nudge_factor;
6898 else if (depth_bias > 0.0f && depth_bias < nudge_factor)
6899 depth_bias += nudge_factor;
6900 }
6901
6902 return depth_bias;
6903 }
6904
6905 static void pvr_get_viewport_scissor_overlap(const VkViewport *const viewport,
6906 const VkRect2D *const scissor,
6907 VkRect2D *const rect_out)
6908 {
6909 /* TODO: See if we can remove this struct. */
6910 struct pvr_rect {
6911 int32_t x0, y0;
6912 int32_t x1, y1;
6913 };
6914
6915 /* TODO: Worry about overflow? */
6916 const struct pvr_rect scissor_rect = {
6917 .x0 = scissor->offset.x,
6918 .y0 = scissor->offset.y,
6919 .x1 = scissor->offset.x + scissor->extent.width,
6920 .y1 = scissor->offset.y + scissor->extent.height
6921 };
6922 struct pvr_rect viewport_rect = { 0 };
6923
6924 assert(viewport->width >= 0.0f);
6925 assert(scissor_rect.x0 >= 0);
6926 assert(scissor_rect.y0 >= 0);
6927
6928 if (scissor->extent.width == 0 || scissor->extent.height == 0) {
6929 *rect_out = (VkRect2D){ 0 };
6930 return;
6931 }
6932
6933 viewport_rect.x0 = (int32_t)viewport->x;
6934 viewport_rect.x1 = (int32_t)viewport->x + (int32_t)viewport->width;
6935
6936 /* TODO: Is there a mathematical way of doing all this and then clamp at
6937 * the end?
6938 */
6939 /* We flip the y0 and y1 when height is negative. */
6940 viewport_rect.y0 = (int32_t)viewport->y + MIN2(0, (int32_t)viewport->height);
6941 viewport_rect.y1 = (int32_t)viewport->y + MAX2(0, (int32_t)viewport->height);
6942
6943 if (scissor_rect.x1 <= viewport_rect.x0 ||
6944 scissor_rect.y1 <= viewport_rect.y0 ||
6945 scissor_rect.x0 >= viewport_rect.x1 ||
6946 scissor_rect.y0 >= viewport_rect.y1) {
6947 *rect_out = (VkRect2D){ 0 };
6948 return;
6949 }
6950
6951 /* Determine the overlapping rectangle. */
6952 viewport_rect.x0 = MAX2(viewport_rect.x0, scissor_rect.x0);
6953 viewport_rect.y0 = MAX2(viewport_rect.y0, scissor_rect.y0);
6954 viewport_rect.x1 = MIN2(viewport_rect.x1, scissor_rect.x1);
6955 viewport_rect.y1 = MIN2(viewport_rect.y1, scissor_rect.y1);
6956
6957 /* TODO: Is this conversion safe? Is this logic right? */
6958 rect_out->offset.x = (uint32_t)viewport_rect.x0;
6959 rect_out->offset.y = (uint32_t)viewport_rect.y0;
6960 rect_out->extent.height = (uint32_t)(viewport_rect.y1 - viewport_rect.y0);
6961 rect_out->extent.width = (uint32_t)(viewport_rect.x1 - viewport_rect.x0);
6962 }
6963
6964 static inline uint32_t
6965 pvr_get_geom_region_clip_align_size(struct pvr_device_info *const dev_info)
6966 {
6967 /* TODO: This should come from rogue_ppp.xml. */
6968 return 16U + 16U * (!PVR_HAS_FEATURE(dev_info, tile_size_16x16));
6969 }
6970
6971 static void
6972 pvr_setup_isp_depth_bias_scissor_state(struct pvr_cmd_buffer *const cmd_buffer)
6973 {
6974 struct ROGUE_TA_STATE_HEADER *const header = &cmd_buffer->state.emit_header;
6975 struct pvr_ppp_state *const ppp_state = &cmd_buffer->state.ppp_state;
6976 struct vk_dynamic_graphics_state *const dynamic_state =
6977 &cmd_buffer->vk.dynamic_graphics_state;
6978 const struct ROGUE_TA_STATE_ISPCTL *const ispctl =
6979 &ppp_state->isp.control_struct;
6980 struct pvr_device_info *const dev_info =
6981 &cmd_buffer->device->pdevice->dev_info;
6982
6983 if (ispctl->dbenable &&
6984 (BITSET_TEST(dynamic_state->dirty,
6985 MESA_VK_DYNAMIC_RS_DEPTH_BIAS_FACTORS) ||
6986 cmd_buffer->depth_bias_array.size == 0)) {
6987 struct pvr_depth_bias_state depth_bias = {
6988 .constant_factor = pvr_calculate_final_depth_bias_contant_factor(
6989 dev_info,
6990 cmd_buffer->state.depth_format,
6991 dynamic_state->rs.depth_bias.constant_factor),
6992 .slope_factor = dynamic_state->rs.depth_bias.slope_factor,
6993 .clamp = dynamic_state->rs.depth_bias.clamp,
6994 };
6995
6996 ppp_state->depthbias_scissor_indices.depthbias_index =
6997 util_dynarray_num_elements(&cmd_buffer->depth_bias_array,
6998 __typeof__(depth_bias));
6999
7000 util_dynarray_append(&cmd_buffer->depth_bias_array, depth_bias);
7001
7002 header->pres_ispctl_dbsc = true;
7003 }
7004
7005 if (ispctl->scenable) {
7006 const uint32_t region_clip_align_size =
7007 pvr_get_geom_region_clip_align_size(dev_info);
7008 const VkViewport *const viewport = &dynamic_state->vp.viewports[0];
7009 const VkRect2D *const scissor = &dynamic_state->vp.scissors[0];
7010 struct pvr_scissor_words scissor_words;
7011 VkRect2D overlap_rect;
7012 uint32_t height;
7013 uint32_t width;
7014 uint32_t x;
7015 uint32_t y;
7016
7017 /* For region clip. */
7018 uint32_t bottom;
7019 uint32_t right;
7020 uint32_t left;
7021 uint32_t top;
7022
7023 /* We don't support multiple viewport calculations. */
7024 assert(dynamic_state->vp.viewport_count == 1);
7025 /* We don't support multiple scissor calculations. */
7026 assert(dynamic_state->vp.scissor_count == 1);
7027
7028 pvr_get_viewport_scissor_overlap(viewport, scissor, &overlap_rect);
7029
7030 x = overlap_rect.offset.x;
7031 y = overlap_rect.offset.y;
7032 width = overlap_rect.extent.width;
7033 height = overlap_rect.extent.height;
7034
7035 pvr_csb_pack (&scissor_words.w0, IPF_SCISSOR_WORD_0, word0) {
7036 word0.scw0_xmax = x + width;
7037 word0.scw0_xmin = x;
7038 }
7039
7040 pvr_csb_pack (&scissor_words.w1, IPF_SCISSOR_WORD_1, word1) {
7041 word1.scw1_ymax = y + height;
7042 word1.scw1_ymin = y;
7043 }
7044
7045 if (cmd_buffer->scissor_array.size &&
7046 cmd_buffer->scissor_words.w0 == scissor_words.w0 &&
7047 cmd_buffer->scissor_words.w1 == scissor_words.w1) {
7048 return;
7049 }
7050
7051 cmd_buffer->scissor_words = scissor_words;
7052
7053 /* Calculate region clip. */
7054
7055 left = x / region_clip_align_size;
7056 top = y / region_clip_align_size;
7057
7058 /* We prevent right=-1 with the multiplication. */
7059 /* TODO: Is there a better way of doing this? */
7060 if ((x + width) != 0U)
7061 right = DIV_ROUND_UP(x + width, region_clip_align_size) - 1;
7062 else
7063 right = 0;
7064
7065 if ((y + height) != 0U)
7066 bottom = DIV_ROUND_UP(y + height, region_clip_align_size) - 1;
7067 else
7068 bottom = 0U;
7069
7070 /* Setup region clip to clip everything outside what was calculated. */
7071
7072 /* FIXME: Should we mask to prevent writing over other words? */
7073 pvr_csb_pack (&ppp_state->region_clipping.word0, TA_REGION_CLIP0, word0) {
7074 word0.right = right;
7075 word0.left = left;
7076 word0.mode = ROGUE_TA_REGION_CLIP_MODE_OUTSIDE;
7077 }
7078
7079 pvr_csb_pack (&ppp_state->region_clipping.word1, TA_REGION_CLIP1, word1) {
7080 word1.bottom = bottom;
7081 word1.top = top;
7082 }
7083
7084 ppp_state->depthbias_scissor_indices.scissor_index =
7085 util_dynarray_num_elements(&cmd_buffer->scissor_array,
7086 struct pvr_scissor_words);
7087
7088 util_dynarray_append(&cmd_buffer->scissor_array,
7089 cmd_buffer->scissor_words);
7090
7091 header->pres_ispctl_dbsc = true;
7092 header->pres_region_clip = true;
7093 }
7094 }
7095
7096 static void
7097 pvr_setup_triangle_merging_flag(struct pvr_cmd_buffer *const cmd_buffer,
7098 struct ROGUE_TA_STATE_ISPA *ispa)
7099 {
7100 struct ROGUE_TA_STATE_HEADER *const header = &cmd_buffer->state.emit_header;
7101 struct pvr_ppp_state *const ppp_state = &cmd_buffer->state.ppp_state;
7102 uint32_t merge_word;
7103 uint32_t mask;
7104
7105 pvr_csb_pack (&merge_word, TA_STATE_PDS_SIZEINFO2, size_info) {
7106 /* Disable for lines or punch-through or for DWD and depth compare
7107 * always.
7108 */
7109 if (ispa->objtype == ROGUE_TA_OBJTYPE_LINE ||
7110 ispa->passtype == ROGUE_TA_PASSTYPE_PUNCH_THROUGH ||
7111 (ispa->dwritedisable && ispa->dcmpmode == ROGUE_TA_CMPMODE_ALWAYS)) {
7112 size_info.pds_tri_merge_disable = true;
7113 }
7114 }
7115
7116 pvr_csb_pack (&mask, TA_STATE_PDS_SIZEINFO2, size_info) {
7117 size_info.pds_tri_merge_disable = true;
7118 }
7119
7120 merge_word |= ppp_state->pds.size_info2 & ~mask;
7121
7122 if (merge_word != ppp_state->pds.size_info2) {
7123 ppp_state->pds.size_info2 = merge_word;
7124 header->pres_pds_state_ptr0 = true;
7125 }
7126 }
7127
7128 static VkResult
7129 setup_pds_fragment_program(struct pvr_cmd_buffer *const cmd_buffer,
7130 struct pvr_pds_upload *pds_fragment_program)
7131 {
7132 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7133 const struct pvr_fragment_shader_state *const fragment_shader_state =
7134 &state->gfx_pipeline->shader_state.fragment;
7135 const struct vk_dynamic_graphics_state *const dynamic_state =
7136 &cmd_buffer->vk.dynamic_graphics_state;
7137 const struct pvr_pds_kickusc_program *program =
7138 &fragment_shader_state->pds_fragment_program;
7139 uint32_t *pds_fragment_program_buffer =
7140 fragment_shader_state->pds_fragment_program_buffer;
7141
7142 memset(pds_fragment_program, 0, sizeof(*pds_fragment_program));
7143
7144 if (!pds_fragment_program_buffer)
7145 return VK_SUCCESS;
7146
7147 struct ROGUE_PDSINST_DOUTU_SRC0 doutu_src;
7148 ROGUE_PDSINST_DOUTU_SRC0_unpack(
7149 &pds_fragment_program_buffer[program->doutu_offset],
7150 &doutu_src);
7151
7152 /* TODO: VkPipelineMultisampleStateCreateInfo.sampleShadingEnable? */
7153 doutu_src.sample_rate = dynamic_state->ms.rasterization_samples >
7154 VK_SAMPLE_COUNT_1_BIT
7155 ? ROGUE_PDSINST_DOUTU_SAMPLE_RATE_FULL
7156 : ROGUE_PDSINST_DOUTU_SAMPLE_RATE_INSTANCE;
7157
7158 ROGUE_PDSINST_DOUTU_SRC0_pack(
7159 &pds_fragment_program_buffer[program->doutu_offset],
7160 &doutu_src);
7161
7162 /* FIXME: Figure out the define for alignment of 16. */
7163 return pvr_arch_cmd_buffer_upload_pds(
7164 cmd_buffer,
7165 &pds_fragment_program_buffer[0],
7166 program->data_size,
7167 16,
7168 &pds_fragment_program_buffer[program->data_size],
7169 program->code_size,
7170 16,
7171 16,
7172 pds_fragment_program);
7173 }
7174
7175 static VkResult
7176 setup_pds_coeff_program(struct pvr_cmd_buffer *const cmd_buffer,
7177 struct pvr_pds_upload *pds_coeff_program)
7178 {
7179 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7180 const struct pvr_fragment_shader_state *const fragment_shader_state =
7181 &state->gfx_pipeline->shader_state.fragment;
7182 const struct vk_dynamic_graphics_state *const dynamic_state =
7183 &cmd_buffer->vk.dynamic_graphics_state;
7184 const VkProvokingVertexModeEXT provoking_vertex =
7185 dynamic_state->rs.provoking_vertex;
7186 const VkPrimitiveTopology topology = dynamic_state->ia.primitive_topology;
7187 const struct pvr_pds_coeff_loading_program *program =
7188 &fragment_shader_state->pds_coeff_program;
7189 uint32_t *pds_coeff_program_buffer =
7190 fragment_shader_state->pds_coeff_program_buffer;
7191 unsigned i;
7192
7193 memset(pds_coeff_program, 0, sizeof(*pds_coeff_program));
7194
7195 if (!pds_coeff_program_buffer)
7196 return VK_SUCCESS;
7197
7198 BITSET_FOREACH_SET (i, program->flat_iter_mask, PVR_MAXIMUM_ITERATIONS) {
7199 uint32_t off = program->dout_src_offsets[i];
7200 assert(off != ~0u);
7201
7202 struct ROGUE_PDSINST_DOUT_FIELDS_DOUTI_SRC douti_src;
7203 ROGUE_PDSINST_DOUT_FIELDS_DOUTI_SRC_unpack(&pds_coeff_program_buffer[off],
7204 &douti_src);
7205
7206 if (provoking_vertex == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT)
7207 douti_src.shademodel = ROGUE_PDSINST_DOUTI_SHADEMODEL_FLAT_VERTEX2;
7208 else if (topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN)
7209 douti_src.shademodel = ROGUE_PDSINST_DOUTI_SHADEMODEL_FLAT_VERTEX1;
7210 else
7211 douti_src.shademodel = ROGUE_PDSINST_DOUTI_SHADEMODEL_FLAT_VERTEX0;
7212
7213 ROGUE_PDSINST_DOUT_FIELDS_DOUTI_SRC_pack(&pds_coeff_program_buffer[off],
7214 &douti_src);
7215 }
7216
7217 if (program->dout_z_iterator_offset != ~0u) {
7218 struct ROGUE_PDSINST_DOUT_FIELDS_DOUTI_SRC douti_src;
7219 ROGUE_PDSINST_DOUT_FIELDS_DOUTI_SRC_unpack(
7220 &pds_coeff_program_buffer[program->dout_z_iterator_offset],
7221 &douti_src);
7222
7223 douti_src.depthbias = dynamic_state->rs.depth_bias.enable;
7224
7225 ROGUE_PDSINST_DOUT_FIELDS_DOUTI_SRC_pack(
7226 &pds_coeff_program_buffer[program->dout_z_iterator_offset],
7227 &douti_src);
7228 }
7229
7230 /* FIXME: Figure out the define for alignment of 16. */
7231 return pvr_arch_cmd_buffer_upload_pds(
7232 cmd_buffer,
7233 &pds_coeff_program_buffer[0],
7234 program->data_size,
7235 16,
7236 &pds_coeff_program_buffer[program->data_size],
7237 program->code_size,
7238 16,
7239 16,
7240 pds_coeff_program);
7241 }
7242
7243 static VkResult
7244 pvr_setup_fragment_state_pointers(struct pvr_cmd_buffer *const cmd_buffer,
7245 struct pvr_sub_cmd_gfx *const sub_cmd)
7246 {
7247 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7248 const pco_data *const fs_data = &state->gfx_pipeline->fs_data;
7249
7250 const struct pvr_fragment_shader_state *const fragment_shader_state =
7251 &state->gfx_pipeline->shader_state.fragment;
7252 const struct pvr_stage_allocation_descriptor_state *descriptor_shader_state =
7253 &fragment_shader_state->descriptor_state;
7254 const struct pvr_pipeline_stage_state *fragment_state =
7255 &fragment_shader_state->stage_state;
7256 struct pvr_pds_upload pds_fragment_program;
7257 struct pvr_pds_upload pds_coeff_program;
7258 VkResult result;
7259
7260 result = setup_pds_fragment_program(cmd_buffer, &pds_fragment_program);
7261 if (result != VK_SUCCESS)
7262 return result;
7263
7264 result = setup_pds_coeff_program(cmd_buffer, &pds_coeff_program);
7265 if (result != VK_SUCCESS)
7266 return result;
7267
7268 const struct pvr_physical_device *pdevice = cmd_buffer->device->pdevice;
7269 struct ROGUE_TA_STATE_HEADER *const header = &state->emit_header;
7270 struct pvr_ppp_state *const ppp_state = &state->ppp_state;
7271
7272 const uint32_t pds_uniform_size =
7273 DIV_ROUND_UP(descriptor_shader_state->pds_info.data_size_in_dwords,
7274 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_UNIFORMSIZE_UNIT_SIZE);
7275
7276 const uint32_t pds_varying_state_size =
7277 DIV_ROUND_UP(pds_coeff_program.data_size,
7278 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_VARYINGSIZE_UNIT_SIZE);
7279
7280 const uint32_t usc_varying_size =
7281 DIV_ROUND_UP(fs_data->common.coeffs,
7282 ROGUE_TA_STATE_PDS_SIZEINFO1_USC_VARYINGSIZE_UNIT_SIZE);
7283
7284 const uint32_t pds_temp_size =
7285 DIV_ROUND_UP(fragment_state->pds_temps_count,
7286 ROGUE_TA_STATE_PDS_SIZEINFO1_PDS_TEMPSIZE_UNIT_SIZE);
7287
7288 const uint32_t usc_shared_size =
7289 DIV_ROUND_UP(fs_data->common.shareds,
7290 ROGUE_TA_STATE_PDS_SIZEINFO2_USC_SHAREDSIZE_UNIT_SIZE);
7291
7292 const uint32_t max_tiles_in_flight =
7293 pvr_arch_calc_fscommon_size_and_tiles_in_flight(
7294 &pdevice->dev_info,
7295 &pdevice->dev_runtime_info,
7296 usc_shared_size *
7297 ROGUE_TA_STATE_PDS_SIZEINFO2_USC_SHAREDSIZE_UNIT_SIZE,
7298 1);
7299 uint32_t size_info_mask;
7300 uint32_t size_info2;
7301
7302 if (max_tiles_in_flight < sub_cmd->max_tiles_in_flight)
7303 sub_cmd->max_tiles_in_flight = max_tiles_in_flight;
7304
7305 pvr_csb_pack (&ppp_state->pds.pixel_shader_base,
7306 TA_STATE_PDS_SHADERBASE,
7307 shader_base) {
7308 shader_base.addr = PVR_DEV_ADDR(pds_fragment_program.data_offset);
7309 }
7310
7311 if (descriptor_shader_state->pds_code.pvr_bo) {
7312 pvr_csb_pack (&ppp_state->pds.texture_uniform_code_base,
7313 TA_STATE_PDS_TEXUNICODEBASE,
7314 tex_base) {
7315 tex_base.addr =
7316 PVR_DEV_ADDR(descriptor_shader_state->pds_code.code_offset);
7317 }
7318 } else {
7319 ppp_state->pds.texture_uniform_code_base = 0U;
7320 }
7321
7322 pvr_csb_pack (&ppp_state->pds.size_info1, TA_STATE_PDS_SIZEINFO1, info1) {
7323 info1.pds_uniformsize = pds_uniform_size;
7324 info1.pds_texturestatesize = 0U;
7325 info1.pds_varyingsize = pds_varying_state_size;
7326 info1.usc_varyingsize = usc_varying_size;
7327 info1.pds_tempsize = pds_temp_size;
7328 }
7329
7330 pvr_csb_pack (&size_info_mask, TA_STATE_PDS_SIZEINFO2, mask) {
7331 mask.pds_tri_merge_disable = true;
7332 }
7333
7334 ppp_state->pds.size_info2 &= size_info_mask;
7335
7336 pvr_csb_pack (&size_info2, TA_STATE_PDS_SIZEINFO2, info2) {
7337 info2.usc_sharedsize = usc_shared_size;
7338 }
7339
7340 ppp_state->pds.size_info2 |= size_info2;
7341
7342 if (pds_coeff_program.pvr_bo) {
7343 header->pres_pds_state_ptr1 = true;
7344
7345 pvr_csb_pack (&ppp_state->pds.varying_base,
7346 TA_STATE_PDS_VARYINGBASE,
7347 base) {
7348 base.addr = PVR_DEV_ADDR(pds_coeff_program.data_offset);
7349 }
7350 } else {
7351 ppp_state->pds.varying_base = 0U;
7352 }
7353
7354 pvr_csb_pack (&ppp_state->pds.uniform_state_data_base,
7355 TA_STATE_PDS_UNIFORMDATABASE,
7356 base) {
7357 base.addr = PVR_DEV_ADDR(state->pds_fragment_descriptor_data_offset);
7358 }
7359
7360 header->pres_pds_state_ptr0 = true;
7361 header->pres_pds_state_ptr3 = true;
7362
7363 return result;
7364 }
7365
7366 static void pvr_setup_viewport(struct pvr_cmd_buffer *const cmd_buffer)
7367 {
7368 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7369 struct ROGUE_TA_STATE_HEADER *const header = &state->emit_header;
7370 struct vk_dynamic_graphics_state *const dynamic_state =
7371 &cmd_buffer->vk.dynamic_graphics_state;
7372 struct pvr_ppp_state *const ppp_state = &state->ppp_state;
7373
7374 if (ppp_state->viewport_count != dynamic_state->vp.viewport_count) {
7375 ppp_state->viewport_count = dynamic_state->vp.viewport_count;
7376 header->pres_viewport = true;
7377 }
7378
7379 if (dynamic_state->rs.rasterizer_discard_enable) {
7380 /* We don't want to emit any viewport data as it'll just get thrown
7381 * away. It's after the previous condition because we still want to
7382 * stash the viewport_count as it's our trigger for when
7383 * rasterizer discard gets disabled.
7384 */
7385 header->pres_viewport = false;
7386 return;
7387 }
7388
7389 for (uint32_t i = 0; i < ppp_state->viewport_count; i++) {
7390 VkViewport *viewport = &dynamic_state->vp.viewports[i];
7391 uint32_t x_scale = fui(viewport->width * 0.5f);
7392 uint32_t y_scale = fui(viewport->height * 0.5f);
7393 uint32_t z_scale = fui(viewport->maxDepth - viewport->minDepth);
7394 uint32_t x_center = fui(viewport->x + viewport->width * 0.5f);
7395 uint32_t y_center = fui(viewport->y + viewport->height * 0.5f);
7396 uint32_t z_center = fui(viewport->minDepth);
7397
7398 if (ppp_state->viewports[i].a0 != x_center ||
7399 ppp_state->viewports[i].m0 != x_scale ||
7400 ppp_state->viewports[i].a1 != y_center ||
7401 ppp_state->viewports[i].m1 != y_scale ||
7402 ppp_state->viewports[i].a2 != z_center ||
7403 ppp_state->viewports[i].m2 != z_scale) {
7404 ppp_state->viewports[i].a0 = x_center;
7405 ppp_state->viewports[i].m0 = x_scale;
7406 ppp_state->viewports[i].a1 = y_center;
7407 ppp_state->viewports[i].m1 = y_scale;
7408 ppp_state->viewports[i].a2 = z_center;
7409 ppp_state->viewports[i].m2 = z_scale;
7410
7411 header->pres_viewport = true;
7412 }
7413 }
7414 }
7415
7416 static void pvr_setup_ppp_control(struct pvr_cmd_buffer *const cmd_buffer)
7417 {
7418 struct vk_dynamic_graphics_state *const dynamic_state =
7419 &cmd_buffer->vk.dynamic_graphics_state;
7420 const VkProvokingVertexModeEXT provoking_vertex =
7421 dynamic_state->rs.provoking_vertex;
7422 const VkPrimitiveTopology topology = dynamic_state->ia.primitive_topology;
7423 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7424 struct ROGUE_TA_STATE_HEADER *const header = &state->emit_header;
7425 struct pvr_ppp_state *const ppp_state = &state->ppp_state;
7426 uint32_t ppp_control;
7427
7428 pvr_csb_pack (&ppp_control, TA_STATE_PPP_CTRL, control) {
7429 control.drawclippededges = true;
7430 control.wclampen = true;
7431
7432 if (provoking_vertex == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT)
7433 control.flatshade_vtx = ROGUE_TA_FLATSHADE_VTX_VERTEX_2;
7434 else if (topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN)
7435 control.flatshade_vtx = ROGUE_TA_FLATSHADE_VTX_VERTEX_1;
7436 else
7437 control.flatshade_vtx = ROGUE_TA_FLATSHADE_VTX_VERTEX_0;
7438
7439 switch (dynamic_state->rs.depth_clip_enable) {
7440 case VK_MESA_DEPTH_CLIP_ENABLE_FALSE:
7441 control.clip_mode = ROGUE_TA_CLIP_MODE_NO_FRONT_OR_REAR;
7442 break;
7443
7444 case VK_MESA_DEPTH_CLIP_ENABLE_TRUE:
7445 control.clip_mode = ROGUE_TA_CLIP_MODE_FRONT_REAR;
7446 break;
7447
7448 case VK_MESA_DEPTH_CLIP_ENABLE_NOT_CLAMP:
7449 if (dynamic_state->rs.depth_clamp_enable)
7450 control.clip_mode = ROGUE_TA_CLIP_MODE_NO_FRONT_OR_REAR;
7451 else
7452 control.clip_mode = ROGUE_TA_CLIP_MODE_FRONT_REAR;
7453 break;
7454 }
7455
7456 /* +--- FrontIsCCW?
7457 * | +--- Cull Front?
7458 * v v
7459 * 0|0 CULLMODE_CULL_CCW,
7460 * 0|1 CULLMODE_CULL_CW,
7461 * 1|0 CULLMODE_CULL_CW,
7462 * 1|1 CULLMODE_CULL_CCW,
7463 */
7464 switch (dynamic_state->rs.cull_mode) {
7465 case VK_CULL_MODE_BACK_BIT:
7466 case VK_CULL_MODE_FRONT_BIT:
7467 if ((dynamic_state->rs.front_face == VK_FRONT_FACE_COUNTER_CLOCKWISE) ^
7468 (dynamic_state->rs.cull_mode == VK_CULL_MODE_FRONT_BIT)) {
7469 control.cullmode = ROGUE_TA_CULLMODE_CULL_CW;
7470 } else {
7471 control.cullmode = ROGUE_TA_CULLMODE_CULL_CCW;
7472 }
7473
7474 break;
7475
7476 case VK_CULL_MODE_FRONT_AND_BACK:
7477 case VK_CULL_MODE_NONE:
7478 control.cullmode = ROGUE_TA_CULLMODE_NO_CULLING;
7479 break;
7480
7481 default:
7482 UNREACHABLE("Unsupported cull mode!");
7483 }
7484
7485 if (dynamic_state->rs.line.mode ==
7486 VK_LINE_RASTERIZATION_MODE_BRESENHAM_KHR) {
7487 switch (topology) {
7488 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
7489 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
7490 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
7491 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
7492 control.prim_msaa = true;
7493 break;
7494
7495 default:
7496 break;
7497 }
7498 }
7499 }
7500
7501 if (ppp_control != ppp_state->ppp_control) {
7502 ppp_state->ppp_control = ppp_control;
7503 header->pres_ppp_ctrl = true;
7504 }
7505 }
7506
7507 /* Largest valid PPP State update in words = 31
7508 * 1 - Header
7509 * 3 - Stream Out Config words 0, 1 and 2
7510 * 1 - PPP Control word
7511 * 3 - Varying Config words 0, 1 and 2
7512 * 1 - Output Select
7513 * 1 - WClamp
7514 * 6 - Viewport Transform words
7515 * 2 - Region Clip words
7516 * 3 - PDS State for fragment phase (PDSSTATEPTR 1-3)
7517 * 4 - PDS State for fragment phase (PDSSTATEPTR0)
7518 * 6 - ISP Control Words
7519 */
7520 #define PVR_MAX_PPP_STATE_DWORDS 31
7521
7522 static VkResult pvr_emit_ppp_state(struct pvr_cmd_buffer *const cmd_buffer,
7523 struct pvr_sub_cmd_gfx *const sub_cmd)
7524 {
7525 const bool deferred_secondary = pvr_cmd_uses_deferred_cs_cmds(cmd_buffer);
7526 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7527 struct ROGUE_TA_STATE_HEADER *const header = &state->emit_header;
7528 struct pvr_csb *const control_stream = &sub_cmd->control_stream;
7529 struct pvr_ppp_state *const ppp_state = &state->ppp_state;
7530 uint32_t ppp_state_words[PVR_MAX_PPP_STATE_DWORDS];
7531 const bool emit_dbsc = header->pres_ispctl_dbsc;
7532 uint32_t *buffer_ptr = ppp_state_words;
7533 uint32_t dbsc_patching_offset = 0;
7534 uint32_t ppp_state_words_count;
7535 struct pvr_suballoc_bo *pvr_bo;
7536 VkResult result;
7537
7538 #if !defined(NDEBUG)
7539 struct ROGUE_TA_STATE_HEADER emit_mask = *header;
7540 uint32_t packed_emit_mask;
7541
7542 static_assert(pvr_cmd_length(TA_STATE_HEADER) == 1,
7543 "EMIT_MASK_IS_CLEAR assumes 1 dword sized header.");
7544
7545 # define EMIT_MASK_GET(field) (emit_mask.field)
7546 # define EMIT_MASK_SET(field, value) (emit_mask.field = (value))
7547 # define EMIT_MASK_IS_CLEAR \
7548 (pvr_cmd_pack(TA_STATE_HEADER)(&packed_emit_mask, &emit_mask), \
7549 packed_emit_mask == 0)
7550 #else
7551 # define EMIT_MASK_GET(field)
7552 # define EMIT_MASK_SET(field, value)
7553 #endif
7554
7555 header->view_port_count =
7556 (ppp_state->viewport_count == 0) ? 0U : (ppp_state->viewport_count - 1);
7557 header->pres_ispctl_fa = header->pres_ispctl;
7558
7559 /* If deferred_secondary is true then we do a separate state update
7560 * which gets patched in vkCmdExecuteCommands().
7561 */
7562 header->pres_ispctl_dbsc &= !deferred_secondary;
7563
7564 pvr_csb_write_struct(buffer_ptr, TA_STATE_HEADER, header);
7565
7566 static_assert(pvr_cmd_length(TA_STATE_HEADER) == 1,
7567 "Following header check assumes 1 dword sized header.");
7568 /* If the header is empty we exit early and prevent a bo alloc of 0 size. */
7569 if (ppp_state_words[0] == 0)
7570 return VK_SUCCESS;
7571
7572 if (header->pres_ispctl) {
7573 pvr_csb_write_value(buffer_ptr, TA_STATE_ISPCTL, ppp_state->isp.control);
7574
7575 assert(header->pres_ispctl_fa);
7576 /* This is not a mistake. FA, BA have the ISPA format, and FB, BB have the
7577 * ISPB format.
7578 */
7579 pvr_csb_write_value(buffer_ptr, TA_STATE_ISPA, ppp_state->isp.front_a);
7580 EMIT_MASK_SET(pres_ispctl_fa, false);
7581
7582 if (header->pres_ispctl_fb) {
7583 pvr_csb_write_value(buffer_ptr, TA_STATE_ISPB, ppp_state->isp.front_b);
7584 EMIT_MASK_SET(pres_ispctl_fb, false);
7585 }
7586
7587 if (header->pres_ispctl_ba) {
7588 pvr_csb_write_value(buffer_ptr, TA_STATE_ISPA, ppp_state->isp.back_a);
7589 EMIT_MASK_SET(pres_ispctl_ba, false);
7590 }
7591
7592 if (header->pres_ispctl_bb) {
7593 pvr_csb_write_value(buffer_ptr, TA_STATE_ISPB, ppp_state->isp.back_b);
7594 EMIT_MASK_SET(pres_ispctl_bb, false);
7595 }
7596
7597 EMIT_MASK_SET(pres_ispctl, false);
7598 }
7599
7600 if (header->pres_ispctl_dbsc) {
7601 assert(!deferred_secondary);
7602
7603 dbsc_patching_offset = buffer_ptr - ppp_state_words;
7604
7605 pvr_csb_pack (buffer_ptr, TA_STATE_ISPDBSC, ispdbsc) {
7606 ispdbsc.dbindex = ppp_state->depthbias_scissor_indices.depthbias_index;
7607 ispdbsc.scindex = ppp_state->depthbias_scissor_indices.scissor_index;
7608 }
7609 buffer_ptr += pvr_cmd_length(TA_STATE_ISPDBSC);
7610
7611 EMIT_MASK_SET(pres_ispctl_dbsc, false);
7612 }
7613
7614 if (header->pres_pds_state_ptr0) {
7615 pvr_csb_write_value(buffer_ptr,
7616 TA_STATE_PDS_SHADERBASE,
7617 ppp_state->pds.pixel_shader_base);
7618
7619 pvr_csb_write_value(buffer_ptr,
7620 TA_STATE_PDS_TEXUNICODEBASE,
7621 ppp_state->pds.texture_uniform_code_base);
7622
7623 pvr_csb_write_value(buffer_ptr,
7624 TA_STATE_PDS_SIZEINFO1,
7625 ppp_state->pds.size_info1);
7626 pvr_csb_write_value(buffer_ptr,
7627 TA_STATE_PDS_SIZEINFO2,
7628 ppp_state->pds.size_info2);
7629
7630 EMIT_MASK_SET(pres_pds_state_ptr0, false);
7631 }
7632
7633 if (header->pres_pds_state_ptr1) {
7634 pvr_csb_write_value(buffer_ptr,
7635 TA_STATE_PDS_VARYINGBASE,
7636 ppp_state->pds.varying_base);
7637 EMIT_MASK_SET(pres_pds_state_ptr1, false);
7638 }
7639
7640 /* We don't use pds_state_ptr2 (texture state programs) control word, but
7641 * this doesn't mean we need to set it to 0. This is because the hardware
7642 * runs the texture state program only when
7643 * ROGUE_TA_STATE_PDS_SIZEINFO1.pds_texturestatesize is non-zero.
7644 */
7645 assert(pvr_csb_unpack(&ppp_state->pds.size_info1, TA_STATE_PDS_SIZEINFO1)
7646 .pds_texturestatesize == 0);
7647
7648 if (header->pres_pds_state_ptr3) {
7649 pvr_csb_write_value(buffer_ptr,
7650 TA_STATE_PDS_UNIFORMDATABASE,
7651 ppp_state->pds.uniform_state_data_base);
7652 EMIT_MASK_SET(pres_pds_state_ptr3, false);
7653 }
7654
7655 if (header->pres_region_clip) {
7656 pvr_csb_write_value(buffer_ptr,
7657 TA_REGION_CLIP0,
7658 ppp_state->region_clipping.word0);
7659 pvr_csb_write_value(buffer_ptr,
7660 TA_REGION_CLIP1,
7661 ppp_state->region_clipping.word1);
7662
7663 EMIT_MASK_SET(pres_region_clip, false);
7664 }
7665
7666 if (header->pres_viewport) {
7667 const uint32_t viewports = MAX2(1, ppp_state->viewport_count);
7668 EMIT_MASK_SET(view_port_count, viewports);
7669
7670 for (uint32_t i = 0; i < viewports; i++) {
7671 /* These don't have any definitions in the csbgen xml files and none
7672 * will be added.
7673 */
7674 *buffer_ptr++ = ppp_state->viewports[i].a0;
7675 *buffer_ptr++ = ppp_state->viewports[i].m0;
7676 *buffer_ptr++ = ppp_state->viewports[i].a1;
7677 *buffer_ptr++ = ppp_state->viewports[i].m1;
7678 *buffer_ptr++ = ppp_state->viewports[i].a2;
7679 *buffer_ptr++ = ppp_state->viewports[i].m2;
7680
7681 EMIT_MASK_SET(view_port_count, EMIT_MASK_GET(view_port_count) - 1);
7682 }
7683
7684 EMIT_MASK_SET(pres_viewport, false);
7685 }
7686
7687 if (header->pres_wclamp) {
7688 pvr_csb_pack (buffer_ptr, TA_WCLAMP, wclamp) {
7689 wclamp.val = fui(1.0e-13f);
7690 }
7691 buffer_ptr += pvr_cmd_length(TA_WCLAMP);
7692 EMIT_MASK_SET(pres_wclamp, false);
7693 }
7694
7695 if (header->pres_outselects) {
7696 pvr_csb_write_value(buffer_ptr, TA_OUTPUT_SEL, ppp_state->output_selects);
7697 EMIT_MASK_SET(pres_outselects, false);
7698 }
7699
7700 if (header->pres_varying_word0) {
7701 pvr_csb_write_value(buffer_ptr,
7702 TA_STATE_VARYING0,
7703 ppp_state->varying_word[0]);
7704 EMIT_MASK_SET(pres_varying_word0, false);
7705 }
7706
7707 if (header->pres_varying_word1) {
7708 pvr_csb_write_value(buffer_ptr,
7709 TA_STATE_VARYING1,
7710 ppp_state->varying_word[1]);
7711 EMIT_MASK_SET(pres_varying_word1, false);
7712 }
7713
7714 /* We only emit this on the first draw of a render job to prevent us from
7715 * inheriting a non-zero value set elsewhere.
7716 */
7717 if (header->pres_varying_word2) {
7718 pvr_csb_write_value(buffer_ptr, TA_STATE_VARYING2, 0);
7719 EMIT_MASK_SET(pres_varying_word2, false);
7720 }
7721
7722 if (header->pres_ppp_ctrl) {
7723 pvr_csb_write_value(buffer_ptr,
7724 TA_STATE_PPP_CTRL,
7725 ppp_state->ppp_control);
7726 EMIT_MASK_SET(pres_ppp_ctrl, false);
7727 }
7728
7729 /* We only emit this on the first draw of a render job to prevent us from
7730 * inheriting a non-zero value set elsewhere.
7731 */
7732 if (header->pres_stream_out_size) {
7733 pvr_csb_write_value(buffer_ptr, TA_STATE_STREAM_OUT0, 0);
7734 EMIT_MASK_SET(pres_stream_out_size, false);
7735 }
7736
7737 assert(EMIT_MASK_IS_CLEAR);
7738
7739 #undef EMIT_MASK_GET
7740 #undef EMIT_MASK_SET
7741 #if !defined(NDEBUG)
7742 # undef EMIT_MASK_IS_CLEAR
7743 #endif
7744
7745 ppp_state_words_count = buffer_ptr - ppp_state_words;
7746 assert(ppp_state_words_count <= PVR_MAX_PPP_STATE_DWORDS);
7747
7748 result =
7749 pvr_arch_cmd_buffer_alloc_mem(cmd_buffer,
7750 cmd_buffer->device->heaps.general_heap,
7751 PVR_DW_TO_BYTES(ppp_state_words_count),
7752 &pvr_bo);
7753 if (result != VK_SUCCESS)
7754 return result;
7755
7756 memcpy(pvr_bo_suballoc_get_map_addr(pvr_bo),
7757 ppp_state_words,
7758 PVR_DW_TO_BYTES(ppp_state_words_count));
7759
7760 pvr_csb_set_relocation_mark(control_stream);
7761
7762 /* Write the VDM state update into the VDM control stream. */
7763 pvr_csb_emit (control_stream, VDMCTRL_PPP_STATE0, state0) {
7764 state0.word_count = ppp_state_words_count;
7765 state0.addrmsb = pvr_bo->dev_addr;
7766 }
7767
7768 pvr_csb_emit (control_stream, VDMCTRL_PPP_STATE1, state1) {
7769 state1.addrlsb = pvr_bo->dev_addr;
7770 }
7771
7772 pvr_csb_clear_relocation_mark(control_stream);
7773
7774 if (emit_dbsc && cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
7775 struct pvr_deferred_cs_command cmd;
7776
7777 if (deferred_secondary) {
7778 const uint32_t num_dwords = pvr_cmd_length(VDMCTRL_PPP_STATE0) +
7779 pvr_cmd_length(VDMCTRL_PPP_STATE1);
7780 uint32_t *vdm_state;
7781
7782 pvr_csb_set_relocation_mark(control_stream);
7783
7784 vdm_state = pvr_arch_csb_alloc_dwords(control_stream, num_dwords);
7785 if (!vdm_state) {
7786 result = pvr_csb_get_status(control_stream);
7787 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
7788 }
7789
7790 pvr_csb_clear_relocation_mark(control_stream);
7791
7792 cmd = (struct pvr_deferred_cs_command){
7793 .type = PVR_DEFERRED_CS_COMMAND_TYPE_DBSC,
7794 .dbsc = {
7795 .state = ppp_state->depthbias_scissor_indices,
7796 .vdm_state = vdm_state,
7797 },
7798 };
7799 } else {
7800 cmd = (struct pvr_deferred_cs_command){
7801 .type = PVR_DEFERRED_CS_COMMAND_TYPE_DBSC2,
7802 .dbsc2 = {
7803 .state = ppp_state->depthbias_scissor_indices,
7804 .ppp_cs_bo = pvr_bo,
7805 .patch_offset = dbsc_patching_offset,
7806 },
7807 };
7808 }
7809
7810 util_dynarray_append(&cmd_buffer->deferred_csb_commands, cmd);
7811 }
7812
7813 state->emit_header = (struct ROGUE_TA_STATE_HEADER){ 0 };
7814
7815 return VK_SUCCESS;
7816 }
7817
7818 static inline bool pvr_ppp_dynamic_state_isp_faces_and_control_dirty(
7819 const BITSET_WORD *const dynamic_dirty)
7820 {
7821 return BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_DS_DEPTH_COMPARE_OP) ||
7822 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_DS_DEPTH_TEST_ENABLE) ||
7823 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_DS_DEPTH_WRITE_ENABLE) ||
7824 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_DS_STENCIL_COMPARE_MASK) ||
7825 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_DS_STENCIL_REFERENCE) ||
7826 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_DS_STENCIL_WRITE_MASK) ||
7827 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_DS_STENCIL_OP) ||
7828 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_RS_DEPTH_BIAS_ENABLE) ||
7829 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_RS_LINE_WIDTH) ||
7830 BITSET_TEST(dynamic_dirty,
7831 MESA_VK_DYNAMIC_RS_RASTERIZER_DISCARD_ENABLE) ||
7832 BITSET_TEST(dynamic_dirty,
7833 MESA_VK_DYNAMIC_MS_RASTERIZATION_SAMPLES) ||
7834 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_MS_SAMPLE_MASK);
7835 }
7836
7837 static inline bool
7838 pvr_ppp_state_update_required(const struct pvr_cmd_buffer *cmd_buffer)
7839 {
7840 const BITSET_WORD *const dynamic_dirty =
7841 cmd_buffer->vk.dynamic_graphics_state.dirty;
7842 const struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7843 const struct ROGUE_TA_STATE_HEADER *const header = &state->emit_header;
7844
7845 /* For push constants we only need to worry if they are updated for the
7846 * fragment stage since we're only updating the pds programs used in the
7847 * fragment stage.
7848 */
7849
7850 return header->pres_ppp_ctrl || header->pres_ispctl ||
7851 header->pres_ispctl_fb || header->pres_ispctl_ba ||
7852 header->pres_ispctl_bb || header->pres_ispctl_dbsc ||
7853 header->pres_pds_state_ptr0 || header->pres_pds_state_ptr1 ||
7854 header->pres_pds_state_ptr2 || header->pres_pds_state_ptr3 ||
7855 header->pres_region_clip || header->pres_viewport ||
7856 header->pres_wclamp || header->pres_outselects ||
7857 header->pres_varying_word0 || header->pres_varying_word1 ||
7858 header->pres_varying_word2 || header->pres_stream_out_program ||
7859 state->dirty.fragment_descriptors || state->dirty.vis_test ||
7860 state->dirty.gfx_pipeline_binding || state->dirty.isp_userpass ||
7861 state->push_consts[PVR_STAGE_ALLOCATION_FRAGMENT].dirty ||
7862 pvr_ppp_dynamic_state_isp_faces_and_control_dirty(dynamic_dirty) ||
7863 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_RS_DEPTH_BIAS_FACTORS) ||
7864 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_RS_DEPTH_CLAMP_ENABLE) ||
7865 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_VP_SCISSORS) ||
7866 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_VP_SCISSOR_COUNT) ||
7867 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_VP_VIEWPORTS) ||
7868 BITSET_TEST(dynamic_dirty, MESA_VK_DYNAMIC_VP_VIEWPORT_COUNT);
7869 }
7870
7871 static VkResult
7872 pvr_emit_dirty_ppp_state(struct pvr_cmd_buffer *const cmd_buffer,
7873 struct pvr_sub_cmd_gfx *const sub_cmd)
7874 {
7875 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
7876 struct vk_dynamic_graphics_state *const dynamic_state =
7877 &cmd_buffer->vk.dynamic_graphics_state;
7878 const struct pvr_fragment_shader_state *fragment_state =
7879 &state->gfx_pipeline->shader_state.fragment;
7880 VkResult result;
7881
7882 /* TODO: The emit_header will be dirty only if
7883 * pvr_arch_reset_graphics_dirty_state() was called before this (so when
7884 * command buffer begins recording or when it's reset). Otherwise it will
7885 * have been zeroed out by the previous pvr_emit_ppp_state(). We can probably
7886 * set a flag in there and check it here instead of checking the header.
7887 * Check if this is true and implement the flag.
7888 */
7889 if (!pvr_ppp_state_update_required(cmd_buffer))
7890 return VK_SUCCESS;
7891
7892 if (state->dirty.gfx_pipeline_binding) {
7893 struct ROGUE_TA_STATE_ISPA ispa;
7894
7895 pvr_setup_output_select(cmd_buffer);
7896 pvr_setup_isp_faces_and_control(cmd_buffer, &ispa);
7897 pvr_setup_triangle_merging_flag(cmd_buffer, &ispa);
7898 } else if (pvr_ppp_dynamic_state_isp_faces_and_control_dirty(
7899 dynamic_state->dirty) ||
7900 state->dirty.isp_userpass || state->dirty.vis_test) {
7901 pvr_setup_isp_faces_and_control(cmd_buffer, NULL);
7902 }
7903
7904 bool skip_fs = fragment_state->is_passthrough &&
7905 !pvr_needs_fs_passthrough(dynamic_state);
7906 if (!dynamic_state->rs.rasterizer_discard_enable &&
7907 state->dirty.fragment_descriptors &&
7908 state->gfx_pipeline->shader_state.fragment.shader_bo &&
7909 !state->gfx_pipeline->fs_data.common.uses.empty && !skip_fs) {
7910 result = pvr_setup_fragment_state_pointers(cmd_buffer, sub_cmd);
7911 if (result != VK_SUCCESS)
7912 return result;
7913 }
7914
7915 pvr_setup_isp_depth_bias_scissor_state(cmd_buffer);
7916
7917 if (BITSET_TEST(dynamic_state->dirty, MESA_VK_DYNAMIC_VP_VIEWPORTS) ||
7918 BITSET_TEST(dynamic_state->dirty, MESA_VK_DYNAMIC_VP_VIEWPORT_COUNT))
7919 pvr_setup_viewport(cmd_buffer);
7920
7921 pvr_setup_ppp_control(cmd_buffer);
7922
7923 /* The hardware doesn't have an explicit mode for this so we use a
7924 * negative viewport to make sure all objects are culled out early.
7925 */
7926 if (dynamic_state->rs.cull_mode == VK_CULL_MODE_FRONT_AND_BACK) {
7927 /* Shift the viewport out of the guard-band culling everything. */
7928 const uint32_t negative_vp_val = fui(-2.0f);
7929
7930 state->ppp_state.viewports[0].a0 = negative_vp_val;
7931 state->ppp_state.viewports[0].m0 = 0;
7932 state->ppp_state.viewports[0].a1 = negative_vp_val;
7933 state->ppp_state.viewports[0].m1 = 0;
7934 state->ppp_state.viewports[0].a2 = negative_vp_val;
7935 state->ppp_state.viewports[0].m2 = 0;
7936
7937 state->ppp_state.viewport_count = 1;
7938
7939 state->emit_header.pres_viewport = true;
7940 }
7941
7942 result = pvr_emit_ppp_state(cmd_buffer, sub_cmd);
7943 if (result != VK_SUCCESS)
7944 return result;
7945
7946 if (state->gfx_pipeline &&
7947 fragment_state->pass_type == ROGUE_TA_PASSTYPE_DEPTH_FEEDBACK &&
7948 !skip_fs) {
7949 assert(state->current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS);
7950 state->current_sub_cmd->gfx.has_depth_feedback = true;
7951 }
7952
7953 return VK_SUCCESS;
7954 }
7955
7956 void pvr_arch_calculate_vertex_cam_size(const struct pvr_device_info *dev_info,
7957 const uint32_t vs_output_size,
7958 const bool raster_enable,
7959 uint32_t *const cam_size_out,
7960 uint32_t *const vs_max_instances_out)
7961 {
7962 /* First work out the size of a vertex in the UVS and multiply by 4 for
7963 * column ordering.
7964 */
7965 const uint32_t uvs_vertex_vector_size_in_dwords =
7966 (vs_output_size + 1U + raster_enable * 4U) * 4U;
7967 const uint32_t vdm_cam_size =
7968 PVR_GET_FEATURE_VALUE(dev_info, vdm_cam_size, 32U);
7969
7970 /* This is a proxy for 8XE. */
7971 if (PVR_HAS_FEATURE(dev_info, simple_internal_parameter_format) &&
7972 vdm_cam_size < 96U) {
7973 /* Comparisons are based on size including scratch per vertex vector. */
7974 if (uvs_vertex_vector_size_in_dwords < (14U * 4U)) {
7975 *cam_size_out = MIN2(31U, vdm_cam_size - 1U);
7976 *vs_max_instances_out = 16U;
7977 } else if (uvs_vertex_vector_size_in_dwords < (20U * 4U)) {
7978 *cam_size_out = 15U;
7979 *vs_max_instances_out = 16U;
7980 } else if (uvs_vertex_vector_size_in_dwords < (28U * 4U)) {
7981 *cam_size_out = 11U;
7982 *vs_max_instances_out = 12U;
7983 } else if (uvs_vertex_vector_size_in_dwords < (44U * 4U)) {
7984 *cam_size_out = 7U;
7985 *vs_max_instances_out = 8U;
7986 } else if (PVR_HAS_FEATURE(dev_info,
7987 simple_internal_parameter_format_v2) ||
7988 uvs_vertex_vector_size_in_dwords < (64U * 4U)) {
7989 *cam_size_out = 7U;
7990 *vs_max_instances_out = 4U;
7991 } else {
7992 *cam_size_out = 3U;
7993 *vs_max_instances_out = 2U;
7994 }
7995 } else {
7996 /* Comparisons are based on size including scratch per vertex vector. */
7997 if (uvs_vertex_vector_size_in_dwords <= (32U * 4U)) {
7998 /* output size <= 27 + 5 scratch. */
7999 *cam_size_out = MIN2(95U, vdm_cam_size - 1U);
8000 *vs_max_instances_out = 0U;
8001 } else if (uvs_vertex_vector_size_in_dwords <= 48U * 4U) {
8002 /* output size <= 43 + 5 scratch */
8003 *cam_size_out = 63U;
8004 if (PVR_GET_FEATURE_VALUE(dev_info, uvs_vtx_entries, 144U) < 288U)
8005 *vs_max_instances_out = 16U;
8006 else
8007 *vs_max_instances_out = 0U;
8008 } else if (uvs_vertex_vector_size_in_dwords <= 64U * 4U) {
8009 /* output size <= 59 + 5 scratch. */
8010 *cam_size_out = 31U;
8011 if (PVR_GET_FEATURE_VALUE(dev_info, uvs_vtx_entries, 144U) < 288U)
8012 *vs_max_instances_out = 16U;
8013 else
8014 *vs_max_instances_out = 0U;
8015 } else {
8016 *cam_size_out = 15U;
8017 *vs_max_instances_out = 16U;
8018 }
8019 }
8020 }
8021
8022 static void pvr_emit_dirty_vdm_state(struct pvr_cmd_buffer *const cmd_buffer,
8023 struct pvr_sub_cmd_gfx *const sub_cmd)
8024 {
8025 /* FIXME: Assume all state is dirty for the moment. */
8026 struct pvr_device_info *const dev_info =
8027 &cmd_buffer->device->pdevice->dev_info;
8028 ASSERTED const uint32_t max_user_vertex_output_components =
8029 pvr_get_max_user_vertex_output_components(dev_info);
8030 struct ROGUE_VDMCTRL_VDM_STATE0 header = { pvr_cmd_header(
8031 VDMCTRL_VDM_STATE0) };
8032 struct vk_dynamic_graphics_state *const dynamic_state =
8033 &cmd_buffer->vk.dynamic_graphics_state;
8034 const VkProvokingVertexModeEXT provoking_vertex =
8035 dynamic_state->rs.provoking_vertex;
8036 const VkPrimitiveTopology topology = dynamic_state->ia.primitive_topology;
8037 const struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
8038 const pco_data *const vs_data = &state->gfx_pipeline->vs_data;
8039 struct pvr_csb *const csb = &sub_cmd->control_stream;
8040 uint32_t max_instances;
8041 uint32_t cam_size;
8042
8043 /* CAM Calculations and HW state take vertex size aligned to DWORDS. */
8044 assert(vs_data->vs.vtxouts <= max_user_vertex_output_components);
8045
8046 pvr_arch_calculate_vertex_cam_size(dev_info,
8047 vs_data->vs.vtxouts,
8048 true,
8049 &cam_size,
8050 &max_instances);
8051
8052 pvr_csb_set_relocation_mark(csb);
8053
8054 pvr_csb_emit (csb, VDMCTRL_VDM_STATE0, state0) {
8055 state0.cam_size = cam_size;
8056
8057 if (dynamic_state->ia.primitive_restart_enable) {
8058 state0.cut_index_enable = true;
8059 state0.cut_index_present = true;
8060 }
8061
8062 if (provoking_vertex == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT)
8063 state0.flatshade_control = ROGUE_VDMCTRL_FLATSHADE_CONTROL_VERTEX_2;
8064 else if (topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN)
8065 state0.flatshade_control = ROGUE_VDMCTRL_FLATSHADE_CONTROL_VERTEX_1;
8066 else
8067 state0.flatshade_control = ROGUE_VDMCTRL_FLATSHADE_CONTROL_VERTEX_0;
8068
8069 /* If we've bound a different vertex buffer, or this draw-call requires
8070 * a different PDS attrib data-section from the last draw call (changed
8071 * base_instance) then we need to specify a new data section. This is
8072 * also the case if we've switched pipeline or attrib program as the
8073 * data-section layout will be different.
8074 */
8075 state0.vs_data_addr_present =
8076 state->dirty.gfx_pipeline_binding || state->dirty.vertex_bindings ||
8077 state->dirty.draw_base_instance || state->dirty.draw_variant;
8078
8079 /* Need to specify new PDS Attrib program if we've bound a different
8080 * pipeline or we needed a different PDS Attrib variant for this
8081 * draw-call.
8082 */
8083 state0.vs_other_present = state->dirty.gfx_pipeline_binding ||
8084 state->dirty.draw_variant;
8085
8086 /* UVB_SCRATCH_SELECT_ONE with no rasterization is only valid when
8087 * stream output is enabled. We use UVB_SCRATCH_SELECT_FIVE because
8088 * Vulkan doesn't support stream output and the vertex position is
8089 * always emitted to the UVB.
8090 */
8091 state0.uvs_scratch_size_select =
8092 ROGUE_VDMCTRL_UVS_SCRATCH_SIZE_SELECT_FIVE;
8093
8094 header = state0;
8095 }
8096
8097 if (header.cut_index_present) {
8098 pvr_csb_emit (csb, VDMCTRL_VDM_STATE1, state1) {
8099 state1.cut_index =
8100 vk_index_to_restart(state->index_buffer_binding.type);
8101 }
8102 }
8103
8104 if (header.vs_data_addr_present) {
8105 pvr_csb_emit (csb, VDMCTRL_VDM_STATE2, state2) {
8106 state2.vs_pds_data_base_addr =
8107 PVR_DEV_ADDR(state->pds_vertex_attrib_offset);
8108 }
8109 }
8110
8111 if (header.vs_other_present) {
8112 const uint32_t usc_unified_store_size_in_bytes = vs_data->common.vtxins
8113 << 2;
8114
8115 pvr_csb_emit (csb, VDMCTRL_VDM_STATE3, state3) {
8116 state3.vs_pds_code_base_addr =
8117 PVR_DEV_ADDR(state->pds_shader.code_offset);
8118 }
8119
8120 pvr_csb_emit (csb, VDMCTRL_VDM_STATE4, state4) {
8121 state4.vs_output_size = vs_data->vs.vtxouts;
8122 }
8123
8124 pvr_csb_emit (csb, VDMCTRL_VDM_STATE5, state5) {
8125 state5.vs_max_instances = max_instances;
8126 state5.vs_usc_common_size = 0U;
8127 state5.vs_usc_unified_size = DIV_ROUND_UP(
8128 usc_unified_store_size_in_bytes,
8129 ROGUE_VDMCTRL_VDM_STATE5_VS_USC_UNIFIED_SIZE_UNIT_SIZE);
8130 state5.vs_pds_temp_size =
8131 DIV_ROUND_UP(state->pds_shader.info->temps_required << 2,
8132 ROGUE_VDMCTRL_VDM_STATE5_VS_PDS_TEMP_SIZE_UNIT_SIZE);
8133 state5.vs_pds_data_size = DIV_ROUND_UP(
8134 PVR_DW_TO_BYTES(state->pds_shader.info->data_size_in_dwords),
8135 ROGUE_VDMCTRL_VDM_STATE5_VS_PDS_DATA_SIZE_UNIT_SIZE);
8136 }
8137 }
8138
8139 pvr_csb_clear_relocation_mark(csb);
8140 }
8141
8142 static VkResult pvr_validate_draw_state(struct pvr_cmd_buffer *cmd_buffer)
8143 {
8144 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
8145 struct vk_dynamic_graphics_state *const dynamic_state =
8146 &cmd_buffer->vk.dynamic_graphics_state;
8147 const struct pvr_graphics_pipeline *const gfx_pipeline = state->gfx_pipeline;
8148 const pco_data *const vs_data = &gfx_pipeline->vs_data;
8149 const pco_data *const fs_data = &gfx_pipeline->fs_data;
8150 struct pvr_sub_cmd_gfx *sub_cmd;
8151 bool fstencil_writemask_zero;
8152 bool bstencil_writemask_zero;
8153 bool fstencil_keep;
8154 bool bstencil_keep;
8155 VkResult result;
8156
8157 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_GRAPHICS);
8158
8159 sub_cmd = &state->current_sub_cmd->gfx;
8160 sub_cmd->empty_cmd = false;
8161
8162 /* Determine pipeline depth/stencil usage. If a pipeline uses depth or
8163 * stencil testing, those attachments are using their loaded values, and
8164 * the loadOps cannot be optimized out.
8165 */
8166 /* Pipeline uses depth testing. */
8167 if (sub_cmd->depth_usage == PVR_DEPTH_STENCIL_USAGE_UNDEFINED &&
8168 dynamic_state->ds.depth.compare_op != VK_COMPARE_OP_ALWAYS) {
8169 sub_cmd->depth_usage = PVR_DEPTH_STENCIL_USAGE_NEEDED;
8170 }
8171
8172 /* Pipeline uses stencil testing. */
8173 if (sub_cmd->stencil_usage == PVR_DEPTH_STENCIL_USAGE_UNDEFINED &&
8174 (dynamic_state->ds.stencil.front.op.compare != VK_COMPARE_OP_ALWAYS ||
8175 dynamic_state->ds.stencil.back.op.compare != VK_COMPARE_OP_ALWAYS)) {
8176 sub_cmd->stencil_usage = PVR_DEPTH_STENCIL_USAGE_NEEDED;
8177 }
8178
8179 if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info,
8180 compute_overlap)) {
8181 uint32_t coefficient_size =
8182 DIV_ROUND_UP(fs_data->common.coeffs,
8183 ROGUE_TA_STATE_PDS_SIZEINFO1_USC_VARYINGSIZE_UNIT_SIZE);
8184
8185 if (coefficient_size >
8186 ROGUE_TA_STATE_PDS_SIZEINFO1_USC_VARYINGSIZE_MAX_SIZE)
8187 sub_cmd->disable_compute_overlap = true;
8188 }
8189
8190 sub_cmd->frag_uses_atomic_ops |= fs_data->common.uses.atomics;
8191 sub_cmd->frag_has_side_effects |= fs_data->common.uses.side_effects;
8192 sub_cmd->frag_uses_texture_rw |= false;
8193 sub_cmd->vertex_uses_texture_rw |= false;
8194
8195 sub_cmd->view_index_wanted |= vs_data->common.multiview;
8196 sub_cmd->view_index_wanted |= fs_data->common.multiview;
8197
8198 sub_cmd->job.get_vis_results = state->vis_test_enabled;
8199
8200 fstencil_keep =
8201 (dynamic_state->ds.stencil.front.op.fail == VK_STENCIL_OP_KEEP) &&
8202 (dynamic_state->ds.stencil.front.op.pass == VK_STENCIL_OP_KEEP);
8203 bstencil_keep =
8204 (dynamic_state->ds.stencil.back.op.fail == VK_STENCIL_OP_KEEP) &&
8205 (dynamic_state->ds.stencil.back.op.pass == VK_STENCIL_OP_KEEP);
8206 fstencil_writemask_zero = (dynamic_state->ds.stencil.front.write_mask == 0);
8207 bstencil_writemask_zero = (dynamic_state->ds.stencil.back.write_mask == 0);
8208
8209 /* Set stencil modified flag if:
8210 * - Neither front nor back-facing stencil has a fail_op/pass_op of KEEP.
8211 * - Neither front nor back-facing stencil has a write_mask of zero.
8212 */
8213 if (!(fstencil_keep && bstencil_keep) &&
8214 !(fstencil_writemask_zero && bstencil_writemask_zero)) {
8215 sub_cmd->modifies_stencil = true;
8216 }
8217
8218 /* Set depth modified flag if depth write is enabled. */
8219 if (dynamic_state->ds.depth.write_enable)
8220 sub_cmd->modifies_depth = true;
8221
8222 /* If either the data or code changes for pds vertex attribs, regenerate the
8223 * data segment.
8224 */
8225 if (state->dirty.vertex_bindings || state->dirty.gfx_pipeline_binding ||
8226 state->dirty.draw_variant || state->dirty.draw_base_instance) {
8227 enum pvr_pds_vertex_attrib_program_type prog_type;
8228 const struct pvr_pds_attrib_program *program;
8229
8230 if (state->draw_state.draw_indirect)
8231 prog_type = PVR_PDS_VERTEX_ATTRIB_PROGRAM_DRAW_INDIRECT;
8232 else if (state->draw_state.base_instance)
8233 prog_type = PVR_PDS_VERTEX_ATTRIB_PROGRAM_BASE_INSTANCE;
8234 else
8235 prog_type = PVR_PDS_VERTEX_ATTRIB_PROGRAM_BASIC;
8236
8237 program =
8238 &gfx_pipeline->shader_state.vertex.pds_attrib_programs[prog_type];
8239 state->pds_shader.info = &program->info;
8240 state->pds_shader.code_offset = program->program.code_offset;
8241
8242 state->max_shared_regs =
8243 MAX2(state->max_shared_regs, pvr_calc_shared_regs_count(gfx_pipeline));
8244
8245 pvr_setup_vertex_buffers(cmd_buffer, gfx_pipeline);
8246 }
8247
8248 state->dirty.vertex_descriptors = state->dirty.gfx_pipeline_binding;
8249 state->dirty.fragment_descriptors = state->dirty.vertex_descriptors;
8250
8251 if (state->push_consts[PVR_STAGE_ALLOCATION_VERTEX_GEOMETRY].dirty) {
8252 result = pvr_cmd_upload_push_consts(cmd_buffer,
8253 PVR_STAGE_ALLOCATION_VERTEX_GEOMETRY);
8254
8255 state->dirty.vertex_descriptors = true;
8256 }
8257
8258 if (state->push_consts[PVR_STAGE_ALLOCATION_FRAGMENT].dirty) {
8259 result =
8260 pvr_cmd_upload_push_consts(cmd_buffer, PVR_STAGE_ALLOCATION_FRAGMENT);
8261
8262 state->dirty.fragment_descriptors = true;
8263 }
8264
8265 /* Account for dirty descriptor set. */
8266 /* TODO: It could be the case that there are no descriptors for a specific
8267 * stage, or that the update descriptors aren't active for a particular
8268 * stage. In such cases we could avoid regenerating the descriptor PDS
8269 * program.
8270 */
8271 state->dirty.vertex_descriptors |= state->dirty.gfx_desc_dirty;
8272 state->dirty.fragment_descriptors |= state->dirty.gfx_desc_dirty;
8273
8274 if (BITSET_TEST(dynamic_state->dirty, MESA_VK_DYNAMIC_CB_BLEND_CONSTANTS) ||
8275 BITSET_TEST(dynamic_state->dirty, MESA_VK_DYNAMIC_RS_FRONT_FACE) ||
8276 BITSET_TEST(dynamic_state->dirty,
8277 MESA_VK_DYNAMIC_CB_COLOR_WRITE_ENABLES)) {
8278 state->dirty.fragment_descriptors = true;
8279 }
8280
8281 const struct pvr_fragment_shader_state *const fragment_shader_state =
8282 &gfx_pipeline->shader_state.fragment;
8283 bool skip_fs = fragment_shader_state->is_passthrough &&
8284 !pvr_needs_fs_passthrough(dynamic_state);
8285
8286 if (state->dirty.fragment_descriptors && !skip_fs) {
8287 result = pvr_setup_descriptor_mappings(
8288 cmd_buffer,
8289 PVR_STAGE_ALLOCATION_FRAGMENT,
8290 &state->gfx_pipeline->shader_state.fragment.descriptor_state,
8291 NULL,
8292 &state->pds_fragment_descriptor_data_offset);
8293 if (result != VK_SUCCESS) {
8294 mesa_loge("Could not setup fragment descriptor mappings.");
8295 return result;
8296 }
8297 }
8298
8299 if (state->dirty.vertex_descriptors) {
8300 uint32_t pds_vertex_descriptor_data_offset;
8301
8302 result = pvr_setup_descriptor_mappings(
8303 cmd_buffer,
8304 PVR_STAGE_ALLOCATION_VERTEX_GEOMETRY,
8305 &state->gfx_pipeline->shader_state.vertex.descriptor_state,
8306 NULL,
8307 &pds_vertex_descriptor_data_offset);
8308 if (result != VK_SUCCESS) {
8309 mesa_loge("Could not setup vertex descriptor mappings.");
8310 return result;
8311 }
8312
8313 pvr_emit_dirty_pds_state(cmd_buffer,
8314 sub_cmd,
8315 pds_vertex_descriptor_data_offset);
8316 }
8317
8318 pvr_emit_dirty_ppp_state(cmd_buffer, sub_cmd);
8319 pvr_emit_dirty_vdm_state(cmd_buffer, sub_cmd);
8320
8321 vk_dynamic_graphics_state_clear_dirty(dynamic_state);
8322 state->dirty.gfx_desc_dirty = false;
8323 state->dirty.draw_base_instance = false;
8324 state->dirty.draw_variant = false;
8325 state->dirty.fragment_descriptors = false;
8326 state->dirty.gfx_pipeline_binding = false;
8327 state->dirty.isp_userpass = false;
8328 state->dirty.vertex_bindings = false;
8329 state->dirty.vis_test = false;
8330
8331 return VK_SUCCESS;
8332 }
8333
8334 static uint32_t pvr_get_hw_primitive_topology(VkPrimitiveTopology topology)
8335 {
8336 switch (topology) {
8337 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
8338 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_POINT_LIST;
8339 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
8340 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_LINE_LIST;
8341 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
8342 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_LINE_STRIP;
8343 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
8344 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_TRI_LIST;
8345 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
8346 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_TRI_STRIP;
8347 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
8348 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_TRI_FAN;
8349 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
8350 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_LINE_LIST_ADJ;
8351 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
8352 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_LINE_STRIP_ADJ;
8353 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
8354 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_TRI_LIST_ADJ;
8355 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
8356 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_TRI_STRIP_ADJ;
8357 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
8358 return ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_PATCH_LIST;
8359 default:
8360 UNREACHABLE("Undefined primitive topology");
8361 }
8362 }
8363
8364 /* TODO: Rewrite this in terms of ALIGN_POT() and pvr_cmd_length(). */
8365 /* Aligned to 128 bit for PDS loads / stores */
8366 #define DUMMY_VDM_CONTROL_STREAM_BLOCK_SIZE 8
8367
8368 static VkResult
8369 pvr_write_draw_indirect_vdm_stream(struct pvr_cmd_buffer *cmd_buffer,
8370 struct pvr_csb *const csb,
8371 pvr_dev_addr_t idx_buffer_addr,
8372 uint32_t idx_stride,
8373 struct ROGUE_VDMCTRL_INDEX_LIST0 *list_hdr,
8374 struct pvr_buffer *buffer,
8375 VkDeviceSize offset,
8376 uint32_t count,
8377 uint32_t stride)
8378 {
8379 struct pvr_pds_drawindirect_program pds_prog = { 0 };
8380 uint32_t word0;
8381
8382 /* Draw indirect always has index offset and instance count. */
8383 list_hdr->index_offset_present = true;
8384 list_hdr->index_instance_count_present = true;
8385
8386 pvr_cmd_pack(VDMCTRL_INDEX_LIST0)(&word0, list_hdr);
8387
8388 pds_prog.support_base_instance = true;
8389 pds_prog.arg_buffer = buffer->dev_addr.addr + offset;
8390 pds_prog.index_buffer = idx_buffer_addr.addr;
8391 pds_prog.index_block_header = word0;
8392 pds_prog.index_stride = idx_stride;
8393 pds_prog.num_views = 1U;
8394
8395 /* TODO: See if we can pre-upload the code section of all the pds programs
8396 * and reuse them here.
8397 */
8398 /* Generate and upload the PDS programs (code + data). */
8399 for (uint32_t i = 0U; i < count; i++) {
8400 const struct pvr_device_info *dev_info =
8401 &cmd_buffer->device->pdevice->dev_info;
8402 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
8403 struct pvr_suballoc_bo *dummy_bo;
8404 struct pvr_suballoc_bo *pds_bo;
8405 uint32_t *dummy_stream;
8406 uint32_t *pds_base;
8407 uint32_t pds_size;
8408 VkResult result;
8409
8410 /* TODO: Move this outside the loop and allocate all of them in one go? */
8411 result =
8412 pvr_arch_cmd_buffer_alloc_mem(cmd_buffer,
8413 cmd_buffer->device->heaps.general_heap,
8414 DUMMY_VDM_CONTROL_STREAM_BLOCK_SIZE,
8415 &dummy_bo);
8416 if (result != VK_SUCCESS)
8417 return result;
8418
8419 pds_prog.increment_draw_id = (i != 0);
8420 pds_prog.index_list_addr_buffer = dummy_bo->dev_addr.addr;
8421
8422 if (state->draw_state.draw_indexed) {
8423 pvr_pds_generate_draw_elements_indirect(&pds_prog,
8424 0,
8425 PDS_GENERATE_SIZES,
8426 dev_info);
8427 } else {
8428 pvr_pds_generate_draw_arrays_indirect(&pds_prog,
8429 0,
8430 PDS_GENERATE_SIZES,
8431 dev_info);
8432 }
8433
8434 pds_size = PVR_DW_TO_BYTES(pds_prog.program.data_size_aligned +
8435 pds_prog.program.code_size_aligned);
8436
8437 result = pvr_arch_cmd_buffer_alloc_mem(cmd_buffer,
8438 cmd_buffer->device->heaps.pds_heap,
8439 pds_size,
8440 &pds_bo);
8441 if (result != VK_SUCCESS)
8442 return result;
8443
8444 pds_base = pvr_bo_suballoc_get_map_addr(pds_bo);
8445 memcpy(pds_base,
8446 pds_prog.program.code,
8447 PVR_DW_TO_BYTES(pds_prog.program.code_size_aligned));
8448
8449 if (state->draw_state.draw_indexed) {
8450 pvr_pds_generate_draw_elements_indirect(
8451 &pds_prog,
8452 pds_base + pds_prog.program.code_size_aligned,
8453 PDS_GENERATE_DATA_SEGMENT,
8454 dev_info);
8455 } else {
8456 pvr_pds_generate_draw_arrays_indirect(
8457 &pds_prog,
8458 pds_base + pds_prog.program.code_size_aligned,
8459 PDS_GENERATE_DATA_SEGMENT,
8460 dev_info);
8461 }
8462
8463 pvr_csb_set_relocation_mark(csb);
8464
8465 pvr_csb_emit (csb, VDMCTRL_PDS_STATE0, state0) {
8466 state0.usc_target = ROGUE_VDMCTRL_USC_TARGET_ANY;
8467
8468 state0.pds_temp_size =
8469 DIV_ROUND_UP(PVR_DW_TO_BYTES(pds_prog.program.temp_size_aligned),
8470 ROGUE_VDMCTRL_PDS_STATE0_PDS_TEMP_SIZE_UNIT_SIZE);
8471
8472 state0.pds_data_size =
8473 DIV_ROUND_UP(PVR_DW_TO_BYTES(pds_prog.program.data_size_aligned),
8474 ROGUE_VDMCTRL_PDS_STATE0_PDS_DATA_SIZE_UNIT_SIZE);
8475 }
8476
8477 pvr_csb_emit (csb, VDMCTRL_PDS_STATE1, state1) {
8478 const uint32_t data_offset =
8479 pds_bo->dev_addr.addr +
8480 PVR_DW_TO_BYTES(pds_prog.program.code_size_aligned) -
8481 cmd_buffer->device->heaps.pds_heap->base_addr.addr;
8482
8483 state1.pds_data_addr = PVR_DEV_ADDR(data_offset);
8484 state1.sd_type = ROGUE_VDMCTRL_SD_TYPE_PDS;
8485 state1.sd_next_type = ROGUE_VDMCTRL_SD_TYPE_NONE;
8486 }
8487
8488 pvr_csb_emit (csb, VDMCTRL_PDS_STATE2, state2) {
8489 const uint32_t code_offset =
8490 pds_bo->dev_addr.addr -
8491 cmd_buffer->device->heaps.pds_heap->base_addr.addr;
8492
8493 state2.pds_code_addr = PVR_DEV_ADDR(code_offset);
8494 }
8495
8496 pvr_csb_clear_relocation_mark(csb);
8497
8498 /* We don't really need to set the relocation mark since the following
8499 * state update is just one emit but let's be nice and use it.
8500 */
8501 pvr_csb_set_relocation_mark(csb);
8502
8503 /* Sync task to ensure the VDM doesn't start reading the dummy blocks
8504 * before they are ready.
8505 */
8506 pvr_csb_emit (csb, VDMCTRL_INDEX_LIST0, list0) {
8507 list0.primitive_topology = ROGUE_VDMCTRL_PRIMITIVE_TOPOLOGY_TRI_LIST;
8508 }
8509
8510 pvr_csb_clear_relocation_mark(csb);
8511
8512 dummy_stream = pvr_bo_suballoc_get_map_addr(dummy_bo);
8513
8514 /* For indexed draw cmds fill in the dummy's header (as it won't change
8515 * based on the indirect args) and increment by the in-use size of each
8516 * dummy block.
8517 */
8518 if (!state->draw_state.draw_indexed) {
8519 dummy_stream[0] = word0;
8520 dummy_stream += 4;
8521 } else {
8522 dummy_stream += 5;
8523 }
8524
8525 /* clang-format off */
8526 pvr_csb_pack (dummy_stream, VDMCTRL_STREAM_RETURN, word);
8527 /* clang-format on */
8528
8529 pvr_csb_set_relocation_mark(csb);
8530
8531 /* Stream link to the first dummy which forces the VDM to discard any
8532 * prefetched (dummy) control stream.
8533 */
8534 pvr_csb_emit (csb, VDMCTRL_STREAM_LINK0, link) {
8535 link.with_return = true;
8536 link.link_addrmsb = dummy_bo->dev_addr;
8537 }
8538
8539 pvr_csb_emit (csb, VDMCTRL_STREAM_LINK1, link) {
8540 link.link_addrlsb = dummy_bo->dev_addr;
8541 }
8542
8543 pvr_csb_clear_relocation_mark(csb);
8544
8545 /* Point the pds program to the next argument buffer and the next VDM
8546 * dummy buffer.
8547 */
8548 pds_prog.arg_buffer += stride;
8549 }
8550
8551 return VK_SUCCESS;
8552 }
8553
8554 #undef DUMMY_VDM_CONTROL_STREAM_BLOCK_SIZE
8555
8556 static void pvr_emit_vdm_index_list(struct pvr_cmd_buffer *cmd_buffer,
8557 struct pvr_sub_cmd_gfx *const sub_cmd,
8558 VkPrimitiveTopology topology,
8559 uint32_t index_offset,
8560 uint32_t first_index,
8561 uint32_t index_count,
8562 uint32_t instance_count,
8563 struct pvr_buffer *buffer,
8564 VkDeviceSize offset,
8565 uint32_t count,
8566 uint32_t stride)
8567 {
8568 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
8569
8570 const pco_data *const vs_data = &state->gfx_pipeline->vs_data;
8571 struct ROGUE_VDMCTRL_INDEX_LIST0 list_hdr = { pvr_cmd_header(
8572 VDMCTRL_INDEX_LIST0) };
8573 pvr_dev_addr_t index_buffer_addr = PVR_DEV_ADDR_INVALID;
8574 struct pvr_csb *const csb = &sub_cmd->control_stream;
8575 unsigned int index_stride = 0;
8576
8577 list_hdr.primitive_topology = pvr_get_hw_primitive_topology(topology);
8578
8579 /* firstInstance is not handled here in the VDM state, it's implemented as
8580 * an addition in the PDS vertex fetch using
8581 * PVR_PDS_CONST_MAP_ENTRY_TYPE_BASE_INSTANCE entry type.
8582 */
8583
8584 list_hdr.index_count_present = true;
8585
8586 if (instance_count > 1)
8587 list_hdr.index_instance_count_present = true;
8588
8589 if (index_offset)
8590 list_hdr.index_offset_present = true;
8591
8592 if (state->draw_state.draw_indexed) {
8593 list_hdr.index_size =
8594 pvr_vdmctrl_index_size_from_type(state->index_buffer_binding.type);
8595 index_stride = vk_index_type_to_bytes(state->index_buffer_binding.type);
8596
8597 index_buffer_addr = PVR_DEV_ADDR_OFFSET(
8598 state->index_buffer_binding.buffer->dev_addr,
8599 state->index_buffer_binding.offset + first_index * index_stride);
8600
8601 list_hdr.index_addr_present = true;
8602 list_hdr.index_base_addrmsb = index_buffer_addr;
8603 }
8604
8605 list_hdr.degen_cull_enable =
8606 PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info,
8607 vdm_degenerate_culling) &&
8608 !vs_data->common.uses.side_effects;
8609
8610 if (state->draw_state.draw_indirect) {
8611 assert(buffer);
8612 pvr_write_draw_indirect_vdm_stream(cmd_buffer,
8613 csb,
8614 index_buffer_addr,
8615 index_stride,
8616 &list_hdr,
8617 buffer,
8618 offset,
8619 count,
8620 stride);
8621 return;
8622 }
8623
8624 pvr_csb_set_relocation_mark(csb);
8625
8626 pvr_csb_emit (csb, VDMCTRL_INDEX_LIST0, list0) {
8627 list0 = list_hdr;
8628 }
8629
8630 if (list_hdr.index_addr_present) {
8631 pvr_csb_emit (csb, VDMCTRL_INDEX_LIST1, list1) {
8632 list1.index_base_addrlsb = index_buffer_addr;
8633 }
8634 }
8635
8636 if (list_hdr.index_count_present) {
8637 pvr_csb_emit (csb, VDMCTRL_INDEX_LIST2, list2) {
8638 list2.index_count = index_count;
8639 }
8640 }
8641
8642 if (list_hdr.index_instance_count_present) {
8643 pvr_csb_emit (csb, VDMCTRL_INDEX_LIST3, list3) {
8644 list3.instance_count = instance_count - 1;
8645 }
8646 }
8647
8648 if (list_hdr.index_offset_present) {
8649 pvr_csb_emit (csb, VDMCTRL_INDEX_LIST4, list4) {
8650 list4.index_offset = index_offset;
8651 }
8652 }
8653
8654 pvr_csb_clear_relocation_mark(csb);
8655 }
8656
8657 void PVR_PER_ARCH(CmdDraw)(VkCommandBuffer commandBuffer,
8658 uint32_t vertexCount,
8659 uint32_t instanceCount,
8660 uint32_t firstVertex,
8661 uint32_t firstInstance)
8662 {
8663 const struct pvr_cmd_buffer_draw_state draw_state = {
8664 .base_vertex = firstVertex,
8665 .base_instance = firstInstance,
8666 };
8667
8668 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
8669 struct vk_dynamic_graphics_state *const dynamic_state =
8670 &cmd_buffer->vk.dynamic_graphics_state;
8671 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
8672 VkResult result;
8673
8674 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
8675
8676 pvr_update_draw_state(state, &draw_state);
8677
8678 result = pvr_validate_draw_state(cmd_buffer);
8679 if (result != VK_SUCCESS)
8680 return;
8681
8682 /* Write the VDM control stream for the primitive. */
8683 pvr_emit_vdm_index_list(cmd_buffer,
8684 &state->current_sub_cmd->gfx,
8685 dynamic_state->ia.primitive_topology,
8686 firstVertex,
8687 0U,
8688 vertexCount,
8689 instanceCount,
8690 NULL,
8691 0U,
8692 0U,
8693 0U);
8694 }
8695
8696 void PVR_PER_ARCH(CmdDrawIndexed)(VkCommandBuffer commandBuffer,
8697 uint32_t indexCount,
8698 uint32_t instanceCount,
8699 uint32_t firstIndex,
8700 int32_t vertexOffset,
8701 uint32_t firstInstance)
8702 {
8703 const struct pvr_cmd_buffer_draw_state draw_state = {
8704 .base_vertex = vertexOffset,
8705 .base_instance = firstInstance,
8706 .draw_indexed = true,
8707 };
8708
8709 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
8710 struct vk_dynamic_graphics_state *const dynamic_state =
8711 &cmd_buffer->vk.dynamic_graphics_state;
8712 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
8713 VkResult result;
8714
8715 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
8716
8717 pvr_update_draw_state(state, &draw_state);
8718
8719 result = pvr_validate_draw_state(cmd_buffer);
8720 if (result != VK_SUCCESS)
8721 return;
8722
8723 /* Write the VDM control stream for the primitive. */
8724 pvr_emit_vdm_index_list(cmd_buffer,
8725 &state->current_sub_cmd->gfx,
8726 dynamic_state->ia.primitive_topology,
8727 vertexOffset,
8728 firstIndex,
8729 indexCount,
8730 instanceCount,
8731 NULL,
8732 0U,
8733 0U,
8734 0U);
8735 }
8736
8737 void PVR_PER_ARCH(CmdDrawIndexedIndirect)(VkCommandBuffer commandBuffer,
8738 VkBuffer _buffer,
8739 VkDeviceSize offset,
8740 uint32_t drawCount,
8741 uint32_t stride)
8742 {
8743 const struct pvr_cmd_buffer_draw_state draw_state = {
8744 .draw_indirect = true,
8745 .draw_indexed = true,
8746 };
8747
8748 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
8749 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
8750 struct vk_dynamic_graphics_state *const dynamic_state =
8751 &cmd_buffer->vk.dynamic_graphics_state;
8752 VK_FROM_HANDLE(pvr_buffer, buffer, _buffer);
8753 VkResult result;
8754
8755 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
8756
8757 pvr_update_draw_state(state, &draw_state);
8758
8759 result = pvr_validate_draw_state(cmd_buffer);
8760 if (result != VK_SUCCESS)
8761 return;
8762
8763 /* Write the VDM control stream for the primitive. */
8764 pvr_emit_vdm_index_list(cmd_buffer,
8765 &state->current_sub_cmd->gfx,
8766 dynamic_state->ia.primitive_topology,
8767 0U,
8768 0U,
8769 0U,
8770 0U,
8771 buffer,
8772 offset,
8773 drawCount,
8774 stride);
8775 }
8776
8777 void PVR_PER_ARCH(CmdDrawIndirect)(VkCommandBuffer commandBuffer,
8778 VkBuffer _buffer,
8779 VkDeviceSize offset,
8780 uint32_t drawCount,
8781 uint32_t stride)
8782 {
8783 const struct pvr_cmd_buffer_draw_state draw_state = {
8784 .draw_indirect = true,
8785 };
8786
8787 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
8788 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
8789 VK_FROM_HANDLE(pvr_buffer, buffer, _buffer);
8790 struct vk_dynamic_graphics_state *const dynamic_state =
8791 &cmd_buffer->vk.dynamic_graphics_state;
8792 VkResult result;
8793
8794 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
8795
8796 pvr_update_draw_state(state, &draw_state);
8797
8798 result = pvr_validate_draw_state(cmd_buffer);
8799 if (result != VK_SUCCESS)
8800 return;
8801
8802 /* Write the VDM control stream for the primitive. */
8803 pvr_emit_vdm_index_list(cmd_buffer,
8804 &state->current_sub_cmd->gfx,
8805 dynamic_state->ia.primitive_topology,
8806 0U,
8807 0U,
8808 0U,
8809 0U,
8810 buffer,
8811 offset,
8812 drawCount,
8813 stride);
8814 }
8815
8816 void PVR_PER_ARCH(CmdEndRenderPass2)(VkCommandBuffer commandBuffer,
8817 const VkSubpassEndInfo *pSubpassEndInfo)
8818 {
8819 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
8820 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
8821 struct pvr_image_view **attachments;
8822 VkClearValue *clear_values;
8823 VkResult result;
8824
8825 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
8826
8827 assert(state->render_pass_info.pass);
8828 assert(state->render_pass_info.framebuffer);
8829
8830 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
8831 if (result != VK_SUCCESS)
8832 return;
8833
8834 result = pvr_resolve_unemitted_resolve_attachments(cmd_buffer,
8835 &state->render_pass_info);
8836 if (result != VK_SUCCESS)
8837 return;
8838
8839 /* Save the required fields before clearing render_pass_info struct. */
8840 attachments = state->render_pass_info.attachments;
8841 clear_values = state->render_pass_info.clear_values;
8842
8843 memset(&state->render_pass_info, 0, sizeof(state->render_pass_info));
8844
8845 state->render_pass_info.attachments = attachments;
8846 state->render_pass_info.clear_values = clear_values;
8847 }
8848
8849 static VkResult
8850 pvr_execute_deferred_cmd_buffer(struct pvr_cmd_buffer *cmd_buffer,
8851 const struct pvr_cmd_buffer *sec_cmd_buffer)
8852 {
8853 struct vk_dynamic_graphics_state *const dynamic_state =
8854 &cmd_buffer->vk.dynamic_graphics_state;
8855 const uint32_t prim_db_elems =
8856 util_dynarray_num_elements(&cmd_buffer->depth_bias_array,
8857 struct pvr_depth_bias_state);
8858 const uint32_t prim_scissor_elems =
8859 util_dynarray_num_elements(&cmd_buffer->scissor_array,
8860 struct pvr_scissor_words);
8861
8862 util_dynarray_foreach (&sec_cmd_buffer->deferred_csb_commands,
8863 struct pvr_deferred_cs_command,
8864 cmd) {
8865 switch (cmd->type) {
8866 case PVR_DEFERRED_CS_COMMAND_TYPE_DBSC: {
8867 const uint32_t scissor_idx =
8868 prim_scissor_elems + cmd->dbsc.state.scissor_index;
8869 const uint32_t db_idx =
8870 prim_db_elems + cmd->dbsc.state.depthbias_index;
8871 const uint32_t num_dwords =
8872 pvr_cmd_length(TA_STATE_HEADER) + pvr_cmd_length(TA_STATE_ISPDBSC);
8873 struct pvr_suballoc_bo *suballoc_bo;
8874 uint32_t ppp_state[num_dwords];
8875 VkResult result;
8876
8877 pvr_csb_pack (&ppp_state[0], TA_STATE_HEADER, header) {
8878 header.pres_ispctl_dbsc = true;
8879 }
8880
8881 pvr_csb_pack (&ppp_state[1], TA_STATE_ISPDBSC, ispdbsc) {
8882 ispdbsc.dbindex = db_idx;
8883 ispdbsc.scindex = scissor_idx;
8884 }
8885
8886 result = pvr_arch_cmd_buffer_upload_general(cmd_buffer,
8887 &ppp_state[0],
8888 sizeof(ppp_state),
8889 &suballoc_bo);
8890 if (result != VK_SUCCESS)
8891 return result;
8892
8893 pvr_csb_pack (&cmd->dbsc.vdm_state[0], VDMCTRL_PPP_STATE0, state) {
8894 state.word_count = num_dwords;
8895 state.addrmsb = suballoc_bo->dev_addr;
8896 }
8897
8898 pvr_csb_pack (&cmd->dbsc.vdm_state[1], VDMCTRL_PPP_STATE1, state) {
8899 state.addrlsb = suballoc_bo->dev_addr;
8900 }
8901
8902 break;
8903 }
8904
8905 case PVR_DEFERRED_CS_COMMAND_TYPE_DBSC2: {
8906 const uint32_t scissor_idx =
8907 prim_scissor_elems + cmd->dbsc2.state.scissor_index;
8908 const uint32_t db_idx =
8909 prim_db_elems + cmd->dbsc2.state.depthbias_index;
8910
8911 uint32_t *const addr =
8912 (uint32_t *)pvr_bo_suballoc_get_map_addr(cmd->dbsc2.ppp_cs_bo) +
8913 cmd->dbsc2.patch_offset;
8914
8915 assert(pvr_bo_suballoc_get_map_addr(cmd->dbsc2.ppp_cs_bo));
8916
8917 pvr_csb_pack (addr, TA_STATE_ISPDBSC, ispdbsc) {
8918 ispdbsc.dbindex = db_idx;
8919 ispdbsc.scindex = scissor_idx;
8920 }
8921
8922 break;
8923 }
8924
8925 default:
8926 UNREACHABLE("Invalid deferred control stream command type.");
8927 break;
8928 }
8929 }
8930
8931 util_dynarray_append_dynarray(&cmd_buffer->depth_bias_array,
8932 &sec_cmd_buffer->depth_bias_array);
8933
8934 util_dynarray_append_dynarray(&cmd_buffer->scissor_array,
8935 &sec_cmd_buffer->scissor_array);
8936
8937 BITSET_SET(dynamic_state->dirty, MESA_VK_DYNAMIC_RS_DEPTH_BIAS_FACTORS);
8938 cmd_buffer->scissor_words = (struct pvr_scissor_words){ 0 };
8939
8940 return VK_SUCCESS;
8941 }
8942
8943 /* Caller needs to make sure that it ends the current sub_cmd. This function
8944 * only creates a copy of sec_sub_cmd and links it to the cmd_buffer's
8945 * sub_cmd list.
8946 */
8947 static VkResult pvr_execute_sub_cmd(struct pvr_cmd_buffer *cmd_buffer,
8948 struct pvr_sub_cmd *sec_sub_cmd)
8949 {
8950 struct pvr_sub_cmd *primary_sub_cmd =
8951 vk_zalloc(&cmd_buffer->vk.pool->alloc,
8952 sizeof(*primary_sub_cmd),
8953 8,
8954 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
8955 if (!primary_sub_cmd) {
8956 return vk_command_buffer_set_error(&cmd_buffer->vk,
8957 VK_ERROR_OUT_OF_HOST_MEMORY);
8958 }
8959
8960 primary_sub_cmd->type = sec_sub_cmd->type;
8961 primary_sub_cmd->owned = false;
8962 primary_sub_cmd->is_dynamic_render = sec_sub_cmd->is_dynamic_render;
8963 primary_sub_cmd->is_suspend = sec_sub_cmd->is_suspend;
8964 primary_sub_cmd->is_resume = sec_sub_cmd->is_resume;
8965
8966 list_addtail(&primary_sub_cmd->link, &cmd_buffer->sub_cmds);
8967
8968 switch (sec_sub_cmd->type) {
8969 case PVR_SUB_CMD_TYPE_GRAPHICS:
8970 primary_sub_cmd->gfx = sec_sub_cmd->gfx;
8971 break;
8972
8973 case PVR_SUB_CMD_TYPE_QUERY:
8974 case PVR_SUB_CMD_TYPE_COMPUTE:
8975 primary_sub_cmd->compute = sec_sub_cmd->compute;
8976 break;
8977
8978 case PVR_SUB_CMD_TYPE_TRANSFER:
8979 primary_sub_cmd->transfer = sec_sub_cmd->transfer;
8980 break;
8981
8982 case PVR_SUB_CMD_TYPE_EVENT:
8983 primary_sub_cmd->event = sec_sub_cmd->event;
8984 break;
8985
8986 default:
8987 UNREACHABLE("Unsupported sub-command type");
8988 }
8989
8990 return VK_SUCCESS;
8991 }
8992
8993 static VkResult
8994 pvr_execute_graphics_cmd_buffer(struct pvr_cmd_buffer *cmd_buffer,
8995 struct pvr_cmd_buffer *sec_cmd_buffer)
8996 {
8997 const struct pvr_device_info *dev_info =
8998 &cmd_buffer->device->pdevice->dev_info;
8999 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
9000 struct pvr_sub_cmd *primary_sub_cmd = state->current_sub_cmd;
9001 struct pvr_sub_cmd *first_sec_cmd;
9002 VkResult result;
9003
9004 /* Inherited queries are not supported. */
9005 assert(!state->vis_test_enabled);
9006
9007 if (list_is_empty(&sec_cmd_buffer->sub_cmds))
9008 return VK_SUCCESS;
9009
9010 first_sec_cmd =
9011 list_first_entry(&sec_cmd_buffer->sub_cmds, struct pvr_sub_cmd, link);
9012
9013 /* Kick a render if we have a new base address. */
9014 if (primary_sub_cmd->gfx.query_pool && first_sec_cmd->gfx.query_pool &&
9015 primary_sub_cmd->gfx.query_pool != first_sec_cmd->gfx.query_pool) {
9016 state->current_sub_cmd->gfx.barrier_store = true;
9017
9018 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9019 if (result != VK_SUCCESS)
9020 return result;
9021
9022 result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer,
9023 PVR_SUB_CMD_TYPE_GRAPHICS);
9024 if (result != VK_SUCCESS)
9025 return result;
9026
9027 primary_sub_cmd = state->current_sub_cmd;
9028
9029 /* Use existing render setup, but load color attachments from HW
9030 * Background object.
9031 */
9032 primary_sub_cmd->gfx.barrier_load = true;
9033 primary_sub_cmd->gfx.barrier_store = false;
9034 }
9035
9036 list_for_each_entry (struct pvr_sub_cmd,
9037 sec_sub_cmd,
9038 &sec_cmd_buffer->sub_cmds,
9039 link) {
9040 /* Only graphics secondary execution supported within a renderpass. */
9041 assert(sec_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS);
9042
9043 if (!sec_sub_cmd->gfx.empty_cmd)
9044 primary_sub_cmd->gfx.empty_cmd = false;
9045
9046 if (sec_sub_cmd->gfx.query_pool) {
9047 primary_sub_cmd->gfx.query_pool = sec_sub_cmd->gfx.query_pool;
9048
9049 util_dynarray_append_dynarray(&state->query_indices,
9050 &sec_sub_cmd->gfx.sec_query_indices);
9051 }
9052
9053 if (!PVR_HAS_FEATURE(dev_info, gs_rta_support)) {
9054 util_dynarray_foreach (&sec_sub_cmd->gfx.unbound_deferred_clears,
9055 struct pvr_unbound_deferred_clear,
9056 recorded_clear) {
9057 result =
9058 pvr_bind_unbound_deferred_clear(cmd_buffer, recorded_clear);
9059 if (result != VK_SUCCESS)
9060 return result;
9061 }
9062 }
9063
9064 if (pvr_cmd_uses_deferred_cs_cmds(sec_cmd_buffer)) {
9065 /* TODO: In case if secondary buffer is created with
9066 * VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, then we patch the
9067 * stream and copy it to primary stream using pvr_arch_csb_copy below.
9068 * This will need locking if the same secondary command buffer is
9069 * executed in multiple primary buffers at the same time.
9070 */
9071 result = pvr_execute_deferred_cmd_buffer(cmd_buffer, sec_cmd_buffer);
9072 if (result != VK_SUCCESS)
9073 return result;
9074
9075 result = pvr_arch_csb_copy(&primary_sub_cmd->gfx.control_stream,
9076 &sec_sub_cmd->gfx.control_stream);
9077 if (result != VK_SUCCESS)
9078 return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
9079 } else {
9080 result = pvr_execute_deferred_cmd_buffer(cmd_buffer, sec_cmd_buffer);
9081 if (result != VK_SUCCESS)
9082 return result;
9083
9084 pvr_arch_csb_emit_link(
9085 &primary_sub_cmd->gfx.control_stream,
9086 pvr_csb_get_start_address(&sec_sub_cmd->gfx.control_stream),
9087 true);
9088 }
9089
9090 if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info,
9091 compute_overlap)) {
9092 primary_sub_cmd->gfx.job.disable_compute_overlap |=
9093 sec_sub_cmd->gfx.job.disable_compute_overlap;
9094 }
9095
9096 primary_sub_cmd->gfx.job.get_vis_results |=
9097 sec_sub_cmd->gfx.job.get_vis_results;
9098 primary_sub_cmd->gfx.view_index_wanted |=
9099 sec_sub_cmd->gfx.view_index_wanted;
9100
9101 primary_sub_cmd->gfx.max_tiles_in_flight =
9102 MIN2(primary_sub_cmd->gfx.max_tiles_in_flight,
9103 sec_sub_cmd->gfx.max_tiles_in_flight);
9104
9105 /* Pass loaded depth/stencil usage from secondary command buffer. */
9106 if (sec_sub_cmd->gfx.depth_usage == PVR_DEPTH_STENCIL_USAGE_NEEDED)
9107 primary_sub_cmd->gfx.depth_usage = PVR_DEPTH_STENCIL_USAGE_NEEDED;
9108
9109 if (sec_sub_cmd->gfx.stencil_usage == PVR_DEPTH_STENCIL_USAGE_NEEDED)
9110 primary_sub_cmd->gfx.stencil_usage = PVR_DEPTH_STENCIL_USAGE_NEEDED;
9111
9112 /* Pass depth/stencil modification state from secondary command buffer. */
9113 if (sec_sub_cmd->gfx.modifies_depth)
9114 primary_sub_cmd->gfx.modifies_depth = true;
9115
9116 if (sec_sub_cmd->gfx.modifies_stencil)
9117 primary_sub_cmd->gfx.modifies_stencil = true;
9118
9119 if (sec_sub_cmd->gfx.barrier_store) {
9120 struct pvr_sub_cmd *sec_next =
9121 list_entry(sec_sub_cmd->link.next, struct pvr_sub_cmd, link);
9122
9123 /* This shouldn't be the last sub cmd. There should be a barrier load
9124 * subsequent to the barrier store.
9125 */
9126 assert(list_last_entry(&sec_cmd_buffer->sub_cmds,
9127 struct pvr_sub_cmd,
9128 link) != sec_sub_cmd);
9129
9130 /* Kick render to store stencil. */
9131 state->current_sub_cmd->gfx.barrier_store = true;
9132 state->current_sub_cmd->gfx.empty_cmd = false;
9133
9134 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9135 if (result != VK_SUCCESS)
9136 return result;
9137
9138 result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer,
9139 PVR_SUB_CMD_TYPE_GRAPHICS);
9140 if (result != VK_SUCCESS)
9141 return result;
9142
9143 primary_sub_cmd = state->current_sub_cmd;
9144
9145 /* Use existing render setup, but load color attachments from HW
9146 * Background object.
9147 */
9148 primary_sub_cmd->gfx.barrier_load = sec_next->gfx.barrier_load;
9149 primary_sub_cmd->gfx.barrier_store = sec_next->gfx.barrier_store;
9150 primary_sub_cmd->gfx.empty_cmd = false;
9151 }
9152
9153 if (!PVR_HAS_FEATURE(dev_info, gs_rta_support)) {
9154 list_splicetail(&sec_cmd_buffer->deferred_clears,
9155 &cmd_buffer->deferred_clears);
9156 }
9157 }
9158
9159 return VK_SUCCESS;
9160 }
9161
9162 void PVR_PER_ARCH(CmdExecuteCommands)(VkCommandBuffer commandBuffer,
9163 uint32_t commandBufferCount,
9164 const VkCommandBuffer *pCommandBuffers)
9165 {
9166 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
9167 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
9168 struct pvr_cmd_buffer *last_cmd_buffer;
9169 VkResult result;
9170
9171 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
9172
9173 assert(cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY);
9174
9175 /* Reset the CPU copy of the most recent PPP state of the primary command
9176 * buffer.
9177 *
9178 * The next draw call in the primary after CmdExecuteCommands may send
9179 * redundant state, if it all goes in the same geom job.
9180 *
9181 * Can't just copy state from the secondary because the recording state of
9182 * the secondary command buffers would have been deleted at this point.
9183 */
9184 pvr_arch_reset_graphics_dirty_state(cmd_buffer, false);
9185
9186 if (state->current_sub_cmd &&
9187 state->current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS) {
9188 for (uint32_t i = 0; i < commandBufferCount; i++) {
9189 VK_FROM_HANDLE(pvr_cmd_buffer, sec_cmd_buffer, pCommandBuffers[i]);
9190
9191 assert(sec_cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
9192
9193 result = pvr_execute_graphics_cmd_buffer(cmd_buffer, sec_cmd_buffer);
9194 if (result != VK_SUCCESS)
9195 return;
9196 }
9197
9198 last_cmd_buffer =
9199 pvr_cmd_buffer_from_handle(pCommandBuffers[commandBufferCount - 1]);
9200
9201 /* Set barriers from final command secondary command buffer. */
9202 for (uint32_t i = 0; i != PVR_NUM_SYNC_PIPELINE_STAGES; i++) {
9203 state->barriers_needed[i] |=
9204 last_cmd_buffer->state.barriers_needed[i] &
9205 PVR_PIPELINE_STAGE_ALL_GRAPHICS_BITS;
9206 }
9207 } else {
9208 for (uint32_t i = 0; i < commandBufferCount; i++) {
9209 VK_FROM_HANDLE(pvr_cmd_buffer, sec_cmd_buffer, pCommandBuffers[i]);
9210
9211 assert(sec_cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
9212
9213 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9214 if (result != VK_SUCCESS)
9215 return;
9216
9217 list_for_each_entry_safe (struct pvr_sub_cmd,
9218 sec_sub_cmd,
9219 &sec_cmd_buffer->sub_cmds,
9220 link) {
9221 result = pvr_execute_sub_cmd(cmd_buffer, sec_sub_cmd);
9222 if (result != VK_SUCCESS)
9223 return;
9224 }
9225 }
9226
9227 last_cmd_buffer =
9228 pvr_cmd_buffer_from_handle(pCommandBuffers[commandBufferCount - 1]);
9229
9230 memcpy(state->barriers_needed,
9231 last_cmd_buffer->state.barriers_needed,
9232 sizeof(state->barriers_needed));
9233 }
9234 }
9235
9236 static void pvr_insert_transparent_obj(struct pvr_cmd_buffer *const cmd_buffer,
9237 struct pvr_sub_cmd_gfx *const sub_cmd)
9238 {
9239 struct pvr_device *const device = cmd_buffer->device;
9240 /* Yes we want a copy. The user could be recording multiple command buffers
9241 * in parallel so writing the template in place could cause problems.
9242 */
9243 struct pvr_static_clear_ppp_template clear =
9244 device->static_clear_state->ppp_templates[VK_IMAGE_ASPECT_COLOR_BIT];
9245 uint32_t pds_state[PVR_PDS_STATE_LENGTH] = { 0 };
9246 struct pvr_csb *csb = &sub_cmd->control_stream;
9247 struct pvr_suballoc_bo *ppp_bo;
9248
9249 assert(clear.requires_pds_state);
9250
9251 /* Patch the template. */
9252
9253 pvr_csb_pack (&pds_state[PVR_STATIC_CLEAR_PPP_PDS_TYPE_SHADERBASE],
9254 TA_STATE_PDS_SHADERBASE,
9255 shaderbase) {
9256 shaderbase.addr = PVR_DEV_ADDR(device->nop_program.pds.data_offset);
9257 }
9258
9259 clear.config.pds_state = &pds_state;
9260
9261 clear.config.ispctl.upass = cmd_buffer->state.render_pass_info.isp_userpass;
9262
9263 /* Emit PPP state from template. */
9264
9265 pvr_emit_ppp_from_template(csb, &clear, &ppp_bo);
9266 list_add(&ppp_bo->link, &cmd_buffer->bo_list);
9267
9268 /* Emit VDM state. */
9269
9270 pvr_emit_clear_words(cmd_buffer, sub_cmd);
9271
9272 /* Reset graphics state. */
9273 pvr_arch_reset_graphics_dirty_state(cmd_buffer, false);
9274 }
9275
9276 static inline struct pvr_render_subpass *
9277 pvr_get_current_subpass(const struct pvr_cmd_buffer_state *const state)
9278 {
9279 const uint32_t subpass_idx = state->render_pass_info.subpass_idx;
9280
9281 return &state->render_pass_info.pass->subpasses[subpass_idx];
9282 }
9283
9284 void PVR_PER_ARCH(CmdNextSubpass2)(VkCommandBuffer commandBuffer,
9285 const VkSubpassBeginInfo *pSubpassBeginInfo,
9286 const VkSubpassEndInfo *pSubpassEndInfo)
9287 {
9288 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
9289 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
9290 struct pvr_render_pass_info *rp_info = &state->render_pass_info;
9291 const struct pvr_renderpass_hwsetup_subpass *hw_subpass;
9292 struct pvr_renderpass_hwsetup_render *next_hw_render;
9293 const struct pvr_render_pass *pass = rp_info->pass;
9294 const struct pvr_renderpass_hw_map *current_map;
9295 const struct pvr_renderpass_hw_map *next_map;
9296 struct pvr_load_op *hw_subpass_load_op;
9297 VkResult result;
9298
9299 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
9300
9301 current_map = &pass->hw_setup->subpass_map[rp_info->subpass_idx];
9302 next_map = &pass->hw_setup->subpass_map[rp_info->subpass_idx + 1];
9303 next_hw_render = &pass->hw_setup->renders[next_map->render];
9304
9305 if (current_map->render != next_map->render) {
9306 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9307 if (result != VK_SUCCESS)
9308 return;
9309
9310 result = pvr_resolve_unemitted_resolve_attachments(cmd_buffer, rp_info);
9311 if (result != VK_SUCCESS)
9312 return;
9313
9314 rp_info->current_hw_subpass = next_map->render;
9315
9316 result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer,
9317 PVR_SUB_CMD_TYPE_GRAPHICS);
9318 if (result != VK_SUCCESS)
9319 return;
9320
9321 rp_info->enable_bg_tag = false;
9322 rp_info->process_empty_tiles = false;
9323
9324 /* If this subpass contains any load ops the HW Background Object must be
9325 * run to do the clears/loads.
9326 */
9327 if (next_hw_render->color_init_count > 0) {
9328 rp_info->enable_bg_tag = true;
9329
9330 if (pass->multiview_enabled) {
9331 /* TODO: A more optimized fix would be to selectively enable empty
9332 * tile processing for renders depending on whether the render is
9333 * the first use of a particular view in a renderpass. This would be
9334 * a much larger and more invasive change but could be considered if
9335 * empty tile processing in multiview workloads becomes a
9336 * performance issue.
9337 */
9338 rp_info->process_empty_tiles = true;
9339 } else {
9340 for (uint32_t i = 0; i < next_hw_render->color_init_count; i++) {
9341 /* Empty tiles need to be cleared too. */
9342 if (next_hw_render->color_init[i].op ==
9343 VK_ATTACHMENT_LOAD_OP_CLEAR) {
9344 rp_info->process_empty_tiles = true;
9345 break;
9346 }
9347 }
9348 }
9349 }
9350
9351 /* Set isp_userpass to zero for new hw_render. This will be used to set
9352 * ROGUE_CR_ISP_CTL::upass_start.
9353 */
9354 rp_info->isp_userpass = 0;
9355 }
9356
9357 hw_subpass = &next_hw_render->subpasses[next_map->subpass];
9358 hw_subpass_load_op = hw_subpass->load_op;
9359
9360 if (hw_subpass_load_op) {
9361 result = pvr_cs_write_load_op(cmd_buffer,
9362 &state->current_sub_cmd->gfx,
9363 hw_subpass_load_op,
9364 rp_info->isp_userpass);
9365 }
9366
9367 /* Pipelines are created for a particular subpass so unbind but leave the
9368 * vertex and descriptor bindings intact as they are orthogonal to the
9369 * subpass.
9370 */
9371 state->gfx_pipeline = NULL;
9372
9373 /* User-pass spawn is 4 bits so if the driver has to wrap it, it will emit a
9374 * full screen transparent object to flush all tags up until now, then the
9375 * user-pass spawn value will implicitly be reset to 0 because
9376 * pvr_render_subpass::isp_userpass values are stored ANDed with
9377 * ROGUE_CR_ISP_CTL_UPASS_START_SIZE_MAX.
9378 */
9379 /* If hw_subpass_load_op is valid then pvr_write_load_op_control_stream
9380 * has already done a full-screen transparent object.
9381 */
9382 if (rp_info->isp_userpass == ROGUE_CR_ISP_CTL_UPASS_START_SIZE_MAX &&
9383 !hw_subpass_load_op) {
9384 pvr_insert_transparent_obj(cmd_buffer, &state->current_sub_cmd->gfx);
9385 }
9386
9387 rp_info->subpass_idx++;
9388
9389 rp_info->isp_userpass = pass->subpasses[rp_info->subpass_idx].isp_userpass;
9390 state->dirty.isp_userpass = true;
9391
9392 rp_info->pipeline_bind_point =
9393 pass->subpasses[rp_info->subpass_idx].pipeline_bind_point;
9394
9395 pvr_stash_depth_format(state, &state->current_sub_cmd->gfx);
9396 }
9397
9398 static bool
9399 pvr_stencil_has_self_dependency(const struct pvr_cmd_buffer_state *const state)
9400 {
9401 const struct pvr_render_subpass *const current_subpass =
9402 pvr_get_current_subpass(state);
9403 const struct pvr_render_attachment *const input_attachments =
9404 current_subpass->input_attachments;
9405
9406 if (current_subpass->depth_stencil_attachment == VK_ATTACHMENT_UNUSED)
9407 return false;
9408
9409 /* We only need to check the current software subpass as we don't support
9410 * merging to/from a subpass with self-dep stencil.
9411 */
9412
9413 for (uint32_t i = 0; i < current_subpass->input_count; i++) {
9414 if (input_attachments[i].attachment_idx ==
9415 current_subpass->depth_stencil_attachment) {
9416 return input_attachments[i].aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT;
9417 }
9418 }
9419
9420 return false;
9421 }
9422
9423 static bool pvr_is_stencil_store_load_needed(
9424 const struct pvr_cmd_buffer *const cmd_buffer,
9425 VkPipelineStageFlags2 vk_src_stage_mask,
9426 VkPipelineStageFlags2 vk_dst_stage_mask,
9427 uint32_t memory_barrier_count,
9428 const VkMemoryBarrier2 *const memory_barriers,
9429 uint32_t image_barrier_count,
9430 const VkImageMemoryBarrier2 *const image_barriers)
9431 {
9432 const struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
9433 const uint32_t fragment_test_stages =
9434 VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
9435 VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
9436 const struct pvr_render_pass *const pass = state->render_pass_info.pass;
9437 const struct pvr_renderpass_hwsetup_render *hw_render;
9438 struct pvr_image_view **const attachments =
9439 state->render_pass_info.attachments;
9440 const struct pvr_image_view *attachment;
9441 uint32_t hw_render_idx;
9442
9443 if (!pass)
9444 return false;
9445
9446 hw_render_idx = state->current_sub_cmd->gfx.hw_render_idx;
9447 hw_render =
9448 pvr_arch_pass_info_get_hw_render(&state->render_pass_info, hw_render_idx);
9449
9450 if (hw_render->ds_attach_idx == VK_ATTACHMENT_UNUSED)
9451 return false;
9452
9453 if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
9454 attachment = attachments[hw_render->ds_attach_idx];
9455 } else {
9456 assert(!attachments);
9457 attachment = NULL;
9458 }
9459
9460 if (!(vk_src_stage_mask & fragment_test_stages) &&
9461 vk_dst_stage_mask & VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT)
9462 return false;
9463
9464 for (uint32_t i = 0; i < memory_barrier_count; i++) {
9465 const uint32_t stencil_write_bit =
9466 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
9467 const uint32_t input_attachment_read_bit =
9468 VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
9469
9470 if (!(memory_barriers[i].srcAccessMask & stencil_write_bit))
9471 continue;
9472
9473 if (!(memory_barriers[i].dstAccessMask & input_attachment_read_bit))
9474 continue;
9475
9476 return pvr_stencil_has_self_dependency(state);
9477 }
9478
9479 for (uint32_t i = 0; i < image_barrier_count; i++) {
9480 VK_FROM_HANDLE(pvr_image, image, image_barriers[i].image);
9481 const uint32_t stencil_bit = VK_IMAGE_ASPECT_STENCIL_BIT;
9482
9483 if (!(image_barriers[i].subresourceRange.aspectMask & stencil_bit))
9484 continue;
9485
9486 if (attachment && image != vk_to_pvr_image(attachment->vk.image))
9487 continue;
9488
9489 if (!vk_format_has_stencil(image->vk.format))
9490 continue;
9491
9492 return pvr_stencil_has_self_dependency(state);
9493 }
9494
9495 return false;
9496 }
9497
9498 static VkResult
9499 pvr_cmd_buffer_insert_mid_frag_barrier_event(struct pvr_cmd_buffer *cmd_buffer,
9500 uint32_t src_stage_mask,
9501 uint32_t dst_stage_mask)
9502 {
9503 struct pvr_sub_cmd *prev_sub_cmd = cmd_buffer->state.current_sub_cmd;
9504 VkResult result;
9505
9506 assert(cmd_buffer->state.current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS);
9507
9508 cmd_buffer->state.current_sub_cmd->gfx.empty_cmd = false;
9509
9510 /* Submit graphics job to store stencil. */
9511 cmd_buffer->state.current_sub_cmd->gfx.barrier_store = true;
9512
9513 pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9514 result =
9515 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT);
9516 if (result != VK_SUCCESS)
9517 return result;
9518
9519 cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){
9520 .type = PVR_EVENT_TYPE_BARRIER,
9521 .barrier = {
9522 .wait_for_stage_mask = src_stage_mask,
9523 .wait_at_stage_mask = dst_stage_mask,
9524 },
9525 };
9526 pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9527 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_GRAPHICS);
9528
9529 cmd_buffer->state.current_sub_cmd->gfx.dr_info = prev_sub_cmd->gfx.dr_info;
9530 prev_sub_cmd->gfx.dr_info = NULL;
9531 assert(cmd_buffer->state.current_sub_cmd->gfx.dr_info ==
9532 cmd_buffer->state.render_pass_info.dr_info);
9533
9534 /* Use existing render setup, but load color attachments from HW BGOBJ */
9535 cmd_buffer->state.current_sub_cmd->gfx.barrier_load = true;
9536 cmd_buffer->state.current_sub_cmd->gfx.barrier_store = false;
9537 cmd_buffer->state.current_sub_cmd->gfx.empty_cmd = false;
9538
9539 cmd_buffer->state.current_sub_cmd->is_dynamic_render =
9540 prev_sub_cmd->is_dynamic_render;
9541 cmd_buffer->state.current_sub_cmd->is_suspend = prev_sub_cmd->is_suspend;
9542
9543 return VK_SUCCESS;
9544 }
9545
9546 static VkResult
9547 pvr_cmd_buffer_insert_barrier_event(struct pvr_cmd_buffer *cmd_buffer,
9548 uint32_t src_stage_mask,
9549 uint32_t dst_stage_mask)
9550 {
9551 VkResult result;
9552
9553 result =
9554 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT);
9555 if (result != VK_SUCCESS)
9556 return result;
9557
9558 cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){
9559 .type = PVR_EVENT_TYPE_BARRIER,
9560 .barrier = {
9561 .wait_for_stage_mask = src_stage_mask,
9562 .wait_at_stage_mask = dst_stage_mask,
9563 },
9564 };
9565
9566 return pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9567 }
9568
9569 /* This is just enough to handle vkCmdPipelineBarrier().
9570 * TODO: Complete?
9571 */
9572 void PVR_PER_ARCH(CmdPipelineBarrier2)(VkCommandBuffer commandBuffer,
9573 const VkDependencyInfo *pDependencyInfo)
9574 {
9575 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
9576 struct pvr_cmd_buffer_state *const state = &cmd_buffer->state;
9577 const struct pvr_render_pass *const render_pass =
9578 state->render_pass_info.pass;
9579 VkPipelineStageFlags vk_src_stage_mask = 0U;
9580 VkPipelineStageFlags vk_dst_stage_mask = 0U;
9581 bool is_stencil_store_load_needed;
9582 uint32_t required_stage_mask = 0U;
9583 uint32_t src_stage_mask;
9584 uint32_t dst_stage_mask;
9585 bool is_barrier_needed;
9586 VkResult result;
9587
9588 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
9589
9590 for (uint32_t i = 0; i < pDependencyInfo->memoryBarrierCount; i++) {
9591 vk_src_stage_mask |= pDependencyInfo->pMemoryBarriers[i].srcStageMask;
9592 vk_dst_stage_mask |= pDependencyInfo->pMemoryBarriers[i].dstStageMask;
9593 }
9594
9595 for (uint32_t i = 0; i < pDependencyInfo->bufferMemoryBarrierCount; i++) {
9596 vk_src_stage_mask |=
9597 pDependencyInfo->pBufferMemoryBarriers[i].srcStageMask;
9598 vk_dst_stage_mask |=
9599 pDependencyInfo->pBufferMemoryBarriers[i].dstStageMask;
9600 }
9601
9602 for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; i++) {
9603 vk_src_stage_mask |=
9604 pDependencyInfo->pImageMemoryBarriers[i].srcStageMask;
9605 vk_dst_stage_mask |=
9606 pDependencyInfo->pImageMemoryBarriers[i].dstStageMask;
9607 }
9608
9609 src_stage_mask = pvr_stage_mask_src(vk_src_stage_mask);
9610 dst_stage_mask = pvr_stage_mask_dst(vk_dst_stage_mask);
9611
9612 for (uint32_t stage = 0U; stage != PVR_NUM_SYNC_PIPELINE_STAGES; stage++) {
9613 if (!(dst_stage_mask & BITFIELD_BIT(stage)))
9614 continue;
9615
9616 required_stage_mask |= state->barriers_needed[stage];
9617 }
9618
9619 src_stage_mask &= required_stage_mask;
9620 for (uint32_t stage = 0U; stage != PVR_NUM_SYNC_PIPELINE_STAGES; stage++) {
9621 if (!(dst_stage_mask & BITFIELD_BIT(stage)))
9622 continue;
9623
9624 state->barriers_needed[stage] &= ~src_stage_mask;
9625 }
9626
9627 if (src_stage_mask == 0 || dst_stage_mask == 0) {
9628 is_barrier_needed = false;
9629 } else if (src_stage_mask == PVR_PIPELINE_STAGE_GEOM_BIT &&
9630 dst_stage_mask == PVR_PIPELINE_STAGE_FRAG_BIT) {
9631 /* This is implicit so no need to barrier. */
9632 is_barrier_needed = false;
9633 } else if (src_stage_mask == dst_stage_mask &&
9634 util_bitcount(src_stage_mask) == 1) {
9635 struct pvr_sub_cmd *const current_sub_cmd = state->current_sub_cmd;
9636
9637 switch (src_stage_mask) {
9638 case PVR_PIPELINE_STAGE_FRAG_BIT:
9639 is_barrier_needed = false;
9640
9641 if (!render_pass)
9642 break;
9643
9644 assert(current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS);
9645
9646 /* Flush all fragment work up to this point. */
9647 pvr_insert_transparent_obj(cmd_buffer, &current_sub_cmd->gfx);
9648 break;
9649
9650 case PVR_PIPELINE_STAGE_COMPUTE_BIT:
9651 is_barrier_needed = false;
9652
9653 if (!current_sub_cmd ||
9654 current_sub_cmd->type != PVR_SUB_CMD_TYPE_COMPUTE) {
9655 break;
9656 }
9657
9658 /* Multiple dispatches can be merged into a single job. When back to
9659 * back dispatches have a sequential dependency (Compute -> compute
9660 * pipeline barrier) we need to do the following.
9661 * - Dispatch a kernel which fences all previous memory writes and
9662 * flushes the MADD cache.
9663 * - Issue a compute fence which ensures all previous tasks emitted
9664 * by the compute data master are completed before starting
9665 * anything new.
9666 */
9667
9668 /* Issue Data Fence, Wait for Data Fence (IDFWDF) makes the PDS wait
9669 * for data.
9670 */
9671 pvr_compute_generate_idfwdf(cmd_buffer, &current_sub_cmd->compute);
9672
9673 pvr_arch_compute_generate_fence(cmd_buffer,
9674 &current_sub_cmd->compute,
9675 false);
9676 break;
9677
9678 default:
9679 is_barrier_needed = false;
9680 break;
9681 };
9682 } else {
9683 is_barrier_needed = true;
9684 }
9685
9686 is_stencil_store_load_needed =
9687 pvr_is_stencil_store_load_needed(cmd_buffer,
9688 vk_src_stage_mask,
9689 vk_dst_stage_mask,
9690 pDependencyInfo->memoryBarrierCount,
9691 pDependencyInfo->pMemoryBarriers,
9692 pDependencyInfo->imageMemoryBarrierCount,
9693 pDependencyInfo->pImageMemoryBarriers);
9694
9695 if (is_stencil_store_load_needed) {
9696 assert(render_pass);
9697 result = pvr_cmd_buffer_insert_mid_frag_barrier_event(cmd_buffer,
9698 src_stage_mask,
9699 dst_stage_mask);
9700 if (result != VK_SUCCESS)
9701 mesa_loge("Failed to insert mid frag barrier event.");
9702 } else if (is_barrier_needed) {
9703 result = pvr_cmd_buffer_insert_barrier_event(cmd_buffer,
9704 src_stage_mask,
9705 dst_stage_mask);
9706 if (result != VK_SUCCESS)
9707 mesa_loge("Failed to insert pipeline barrier event.");
9708 }
9709 }
9710
9711 void PVR_PER_ARCH(CmdResetEvent2)(VkCommandBuffer commandBuffer,
9712 VkEvent _event,
9713 VkPipelineStageFlags2 stageMask)
9714 {
9715 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
9716 VK_FROM_HANDLE(pvr_event, event, _event);
9717 VkResult result;
9718
9719 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
9720
9721 result =
9722 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT);
9723 if (result != VK_SUCCESS)
9724 return;
9725
9726 cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){
9727 .type = PVR_EVENT_TYPE_RESET,
9728 .set_reset = {
9729 .event = event,
9730 .wait_for_stage_mask = pvr_stage_mask_src(stageMask),
9731 },
9732 };
9733
9734 pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9735 }
9736
9737 void PVR_PER_ARCH(CmdSetEvent2)(VkCommandBuffer commandBuffer,
9738 VkEvent _event,
9739 const VkDependencyInfo *pDependencyInfo)
9740 {
9741 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
9742 VK_FROM_HANDLE(pvr_event, event, _event);
9743 VkResult result;
9744
9745 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
9746
9747 result =
9748 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT);
9749 if (result != VK_SUCCESS)
9750 return;
9751
9752 VkPipelineStageFlags2 stage_mask =
9753 vk_collect_dependency_info_src_stages(pDependencyInfo);
9754
9755 cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){
9756 .type = PVR_EVENT_TYPE_SET,
9757 .set_reset = {
9758 .event = event,
9759 .wait_for_stage_mask = pvr_stage_mask_dst(stage_mask),
9760 },
9761 };
9762
9763 pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9764 }
9765
9766 void PVR_PER_ARCH(CmdWaitEvents2)(VkCommandBuffer commandBuffer,
9767 uint32_t eventCount,
9768 const VkEvent *pEvents,
9769 const VkDependencyInfo *pDependencyInfos)
9770 {
9771 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
9772 struct pvr_event **events_array;
9773 uint32_t *stage_masks;
9774 VkResult result;
9775
9776 PVR_CHECK_COMMAND_BUFFER_BUILDING_STATE(cmd_buffer);
9777
9778 VK_MULTIALLOC(ma);
9779 vk_multialloc_add(&ma, &events_array, __typeof__(*events_array), eventCount);
9780 vk_multialloc_add(&ma, &stage_masks, __typeof__(*stage_masks), eventCount);
9781
9782 if (!vk_multialloc_alloc(&ma,
9783 &cmd_buffer->vk.pool->alloc,
9784 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT)) {
9785 vk_command_buffer_set_error(&cmd_buffer->vk, VK_ERROR_OUT_OF_HOST_MEMORY);
9786 return;
9787 }
9788
9789 result =
9790 pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_EVENT);
9791 if (result != VK_SUCCESS) {
9792 vk_free(&cmd_buffer->vk.pool->alloc, events_array);
9793 return;
9794 }
9795
9796 memcpy(events_array, pEvents, sizeof(*events_array) * eventCount);
9797
9798 for (uint32_t i = 0; i < eventCount; i++) {
9799 const VkDependencyInfo *info = &pDependencyInfos[i];
9800 VkPipelineStageFlags2 mask = 0;
9801
9802 for (uint32_t j = 0; j < info->memoryBarrierCount; j++)
9803 mask |= info->pMemoryBarriers[j].dstStageMask;
9804
9805 for (uint32_t j = 0; j < info->bufferMemoryBarrierCount; j++)
9806 mask |= info->pBufferMemoryBarriers[j].dstStageMask;
9807
9808 for (uint32_t j = 0; j < info->imageMemoryBarrierCount; j++)
9809 mask |= info->pImageMemoryBarriers[j].dstStageMask;
9810
9811 stage_masks[i] = pvr_stage_mask_dst(mask);
9812 }
9813
9814 cmd_buffer->state.current_sub_cmd->event = (struct pvr_sub_cmd_event){
9815 .type = PVR_EVENT_TYPE_WAIT,
9816 .wait = {
9817 .count = eventCount,
9818 .events = events_array,
9819 .wait_at_stage_masks = stage_masks,
9820 },
9821 };
9822
9823 pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9824 }
9825
9826 void PVR_PER_ARCH(CmdWriteTimestamp2)(VkCommandBuffer commandBuffer,
9827 VkPipelineStageFlags2 stage,
9828 VkQueryPool queryPool,
9829 uint32_t query)
9830 {
9831 UNREACHABLE("Timestamp queries are not supported.");
9832 }
9833
9834 VkResult PVR_PER_ARCH(EndCommandBuffer)(VkCommandBuffer commandBuffer)
9835 {
9836 VK_FROM_HANDLE(pvr_cmd_buffer, cmd_buffer, commandBuffer);
9837 struct pvr_cmd_buffer_state *state = &cmd_buffer->state;
9838 VkResult result;
9839
9840 if (vk_command_buffer_has_error(&cmd_buffer->vk))
9841 return vk_command_buffer_end(&cmd_buffer->vk);
9842
9843 /* TODO: We should be freeing all the resources, allocated for recording,
9844 * here.
9845 */
9846 result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer);
9847 if (result != VK_SUCCESS)
9848 pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result);
9849
9850 util_dynarray_fini(&state->query_indices);
9851
9852 return vk_command_buffer_end(&cmd_buffer->vk);
9853 }