Debian Codegraph @

src/asahi/vulkan/hk_cmd_buffer.c

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * Copyright 2024 Alyssa Rosenzweig
4 * Copyright 2022-2023 Collabora Ltd. and Red Hat Inc.
5 * SPDX-License-Identifier: MIT
6 */
7 #include "hk_cmd_buffer.h"
8
9 #include "agx_abi.h"
10 #include "agx_bo.h"
11 #include "agx_device.h"
12 #include "agx_linker.h"
13 #include "agx_tilebuffer.h"
14 #include "agx_usc.h"
15 #include "hk_buffer.h"
16 #include "hk_cmd_pool.h"
17 #include "hk_descriptor_set.h"
18 #include "hk_descriptor_set_layout.h"
19 #include "hk_device.h"
20 #include "hk_device_memory.h"
21 #include "hk_entrypoints.h"
22 #include "hk_image.h"
23 #include "hk_image_view.h"
24 #include "hk_physical_device.h"
25 #include "hk_shader.h"
26
27 #include "libagx_dgc.h"
28 #include "pool.h"
29 #include "shader_enums.h"
30 #include "vk_pipeline_layout.h"
31 #include "vk_synchronization.h"
32
33 #include "util/list.h"
34 #include "util/macros.h"
35 #include "util/u_dynarray.h"
36 #include "vulkan/vulkan_core.h"
37
38 static void
39 hk_descriptor_state_fini(struct hk_cmd_buffer *cmd,
40 struct hk_descriptor_state *desc)
41 {
42 struct hk_cmd_pool *pool = hk_cmd_buffer_pool(cmd);
43
44 for (unsigned i = 0; i < HK_MAX_SETS; i++) {
45 vk_free(&pool->vk.alloc, desc->push[i]);
46 desc->push[i] = NULL;
47 }
48 }
49
50 static void
51 hk_free_resettable_cmd_buffer(struct hk_cmd_buffer *cmd)
52 {
53 struct hk_cmd_pool *pool = hk_cmd_buffer_pool(cmd);
54 struct hk_device *dev = hk_cmd_pool_device(pool);
55
56 hk_descriptor_state_fini(cmd, &cmd->state.gfx.descriptors);
57 hk_descriptor_state_fini(cmd, &cmd->state.cs.descriptors);
58
59 hk_cmd_pool_free_bo_list(pool, &cmd->uploader.main.bos);
60 hk_cmd_pool_free_usc_bo_list(pool, &cmd->uploader.usc.bos);
61
62 list_for_each_entry_safe(struct hk_cs, it, &cmd->control_streams, node) {
63 list_del(&it->node);
64 hk_cs_destroy(it);
65 }
66
67 util_dynarray_foreach(&cmd->large_bos, struct agx_bo *, bo) {
68 agx_bo_unreference(&dev->dev, *bo);
69 }
70
71 util_dynarray_clear(&cmd->large_bos);
72 }
73
74 static void
75 hk_destroy_cmd_buffer(struct vk_command_buffer *vk_cmd_buffer)
76 {
77 struct hk_cmd_buffer *cmd =
78 container_of(vk_cmd_buffer, struct hk_cmd_buffer, vk);
79 struct hk_cmd_pool *pool = hk_cmd_buffer_pool(cmd);
80
81 util_dynarray_fini(&cmd->large_bos);
82 hk_free_resettable_cmd_buffer(cmd);
83 vk_command_buffer_finish(&cmd->vk);
84 vk_free(&pool->vk.alloc, cmd);
85 }
86
87 static VkResult
88 hk_create_cmd_buffer(struct vk_command_pool *vk_pool,
89 VkCommandBufferLevel level,
90 struct vk_command_buffer **cmd_buffer_out)
91 {
92 struct hk_cmd_pool *pool = container_of(vk_pool, struct hk_cmd_pool, vk);
93 struct hk_device *dev = hk_cmd_pool_device(pool);
94 struct hk_cmd_buffer *cmd;
95 VkResult result;
96
97 cmd = vk_zalloc(&pool->vk.alloc, sizeof(*cmd), 8,
98 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
99 if (cmd == NULL)
100 return vk_error(dev, VK_ERROR_OUT_OF_HOST_MEMORY);
101
102 result =
103 vk_command_buffer_init(&pool->vk, &cmd->vk, &hk_cmd_buffer_ops, level);
104 if (result != VK_SUCCESS) {
105 vk_free(&pool->vk.alloc, cmd);
106 return result;
107 }
108
109 cmd->large_bos = UTIL_DYNARRAY_INIT;
110
111 cmd->vk.dynamic_graphics_state.vi = &cmd->state.gfx._dynamic_vi;
112 cmd->vk.dynamic_graphics_state.ms.sample_locations =
113 &cmd->state.gfx._dynamic_sl;
114
115 list_inithead(&cmd->uploader.main.bos);
116 list_inithead(&cmd->uploader.usc.bos);
117 list_inithead(&cmd->control_streams);
118
119 *cmd_buffer_out = &cmd->vk;
120
121 return VK_SUCCESS;
122 }
123
124 static void
125 hk_reset_cmd_buffer(struct vk_command_buffer *vk_cmd_buffer,
126 UNUSED VkCommandBufferResetFlags flags)
127 {
128 struct hk_cmd_buffer *cmd =
129 container_of(vk_cmd_buffer, struct hk_cmd_buffer, vk);
130
131 vk_command_buffer_reset(&cmd->vk);
132 hk_free_resettable_cmd_buffer(cmd);
133
134 cmd->uploader.main.map = NULL;
135 cmd->uploader.main.base = 0;
136 cmd->uploader.main.offset = 0;
137 cmd->uploader.usc.map = NULL;
138 cmd->uploader.usc.base = 0;
139 cmd->uploader.usc.offset = 0;
140
141 cmd->current_cs.gfx = NULL;
142 cmd->current_cs.cs = NULL;
143 cmd->current_cs.post_gfx = NULL;
144 cmd->current_cs.pre_gfx = NULL;
145
146 assert(!cmd->in_meta);
147 cmd->geom_index_buffer = 0;
148 cmd->geom_index_count = 0;
149 cmd->geom_instance_count = 0;
150 cmd->uses_heap = false;
151
152 memset(&cmd->state, 0, sizeof(cmd->state));
153 }
154
155 const struct vk_command_buffer_ops hk_cmd_buffer_ops = {
156 .create = hk_create_cmd_buffer,
157 .reset = hk_reset_cmd_buffer,
158 .destroy = hk_destroy_cmd_buffer,
159 };
160
161 static VkResult
162 hk_cmd_buffer_alloc_bo(struct hk_cmd_buffer *cmd, bool usc,
163 struct hk_cmd_bo **bo_out)
164 {
165 VkResult result = hk_cmd_pool_alloc_bo(hk_cmd_buffer_pool(cmd), usc, bo_out);
166 if (result != VK_SUCCESS)
167 return result;
168
169 if (usc)
170 list_addtail(&(*bo_out)->link, &cmd->uploader.usc.bos);
171 else
172 list_addtail(&(*bo_out)->link, &cmd->uploader.main.bos);
173
174 return VK_SUCCESS;
175 }
176
177 struct agx_ptr
178 hk_pool_alloc_internal(struct hk_cmd_buffer *cmd, uint32_t size,
179 uint32_t alignment, bool usc)
180 {
181 struct hk_device *dev = hk_cmd_buffer_device(cmd);
182 struct hk_uploader *uploader =
183 usc ? &cmd->uploader.usc : &cmd->uploader.main;
184
185 /* Specially handle large allocations owned by the command buffer, e.g. used
186 * for statically allocated vertex output buffers with geometry shaders.
187 */
188 if (size > HK_CMD_BO_SIZE) {
189 uint32_t flags = usc ? AGX_BO_LOW_VA : 0;
190 struct agx_bo *bo =
191 agx_bo_create(&dev->dev, size, flags, 0, "Large pool allocation");
192
193 util_dynarray_append(&cmd->large_bos, bo);
194 return (struct agx_ptr){
195 .gpu = bo->va->addr,
196 .cpu = agx_bo_map(bo),
197 };
198 }
199
200 assert(size <= HK_CMD_BO_SIZE);
201 assert(alignment > 0);
202
203 uint32_t offset = align(uploader->offset, alignment);
204
205 assert(offset <= HK_CMD_BO_SIZE);
206 if (uploader->map != NULL && size <= HK_CMD_BO_SIZE - offset) {
207 uploader->offset = offset + size;
208
209 return (struct agx_ptr){
210 .gpu = uploader->base + offset,
211 .cpu = uploader->map + offset,
212 };
213 }
214
215 struct hk_cmd_bo *bo;
216 VkResult result = hk_cmd_buffer_alloc_bo(cmd, usc, &bo);
217 if (unlikely(result != VK_SUCCESS)) {
218 vk_command_buffer_set_error(&cmd->vk, result);
219 return (struct agx_ptr){0};
220 }
221
222 /* Pick whichever of the current upload BO and the new BO will have more
223 * room left to be the BO for the next upload. If our upload size is
224 * bigger than the old offset, we're better off burning the whole new
225 * upload BO on this one allocation and continuing on the current upload
226 * BO.
227 */
228 if (uploader->map == NULL || size < uploader->offset) {
229 uploader->map = agx_bo_map(bo->bo);
230 uploader->base = bo->bo->va->addr;
231 uploader->offset = size;
232 }
233
234 return (struct agx_ptr){
235 .gpu = bo->bo->va->addr,
236 .cpu = bo->map,
237 };
238 }
239
240 uint64_t
241 hk_pool_upload(struct hk_cmd_buffer *cmd, const void *data, uint32_t size,
242 uint32_t alignment)
243 {
244 struct agx_ptr T = hk_pool_alloc(cmd, size, alignment);
245 if (unlikely(T.cpu == NULL))
246 return 0;
247
248 memcpy(T.cpu, data, size);
249 return T.gpu;
250 }
251
252 VKAPI_ATTR VkResult VKAPI_CALL
253 hk_BeginCommandBuffer(VkCommandBuffer commandBuffer,
254 const VkCommandBufferBeginInfo *pBeginInfo)
255 {
256 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
257 hk_reset_cmd_buffer(&cmd->vk, 0);
258
259 perf_debug(cmd, "Begin command buffer");
260 hk_cmd_buffer_begin_compute(cmd, pBeginInfo);
261 hk_cmd_buffer_begin_graphics(cmd, pBeginInfo);
262
263 return VK_SUCCESS;
264 }
265
266 /*
267 * Merge adjacent compute control streams. Except for reading timestamps, there
268 * is no reason to submit two CDM streams back-to-back in the same command
269 * buffer. However, it is challenging to avoid constructing such sequences due
270 * to the gymnastics required to reorder compute around graphics. Merging at
271 * EndCommandBuffer is cheap O(# of control streams) and lets us get away with
272 * the sloppiness.
273 */
274 static void
275 merge_control_streams(struct hk_cmd_buffer *cmd)
276 {
277 struct hk_cs *last = NULL;
278
279 list_for_each_entry_safe(struct hk_cs, cs, &cmd->control_streams, node) {
280 if (cs->type == HK_CS_CDM && last && last->type == HK_CS_CDM &&
281 !last->timestamp.end.handle) {
282
283 hk_cs_merge_cdm(last, cs);
284 list_del(&cs->node);
285 hk_cs_destroy(cs);
286 } else {
287 last = cs;
288 }
289 }
290 }
291
292 VKAPI_ATTR VkResult VKAPI_CALL
293 hk_EndCommandBuffer(VkCommandBuffer commandBuffer)
294 {
295 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
296
297 assert(cmd->current_cs.gfx == NULL && cmd->current_cs.pre_gfx == NULL &&
298 "must end rendering before ending the command buffer");
299
300 perf_debug(cmd, "End command buffer");
301 hk_cmd_buffer_end_compute(cmd);
302 hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.post_gfx);
303
304 struct hk_device *dev = hk_cmd_buffer_device(cmd);
305 if (likely(!(dev->dev.debug & AGX_DBG_NOMERGE))) {
306 merge_control_streams(cmd);
307 }
308
309 /* We cannot terminate CDM control streams until after merging, since merging
310 * needs to append stream links late. Now that we've merged, insert all the
311 * missing stream terminates.
312 */
313 list_for_each_entry(struct hk_cs, cs, &cmd->control_streams, node) {
314 if (cs->type == HK_CS_CDM) {
315 cs->current = agx_cdm_terminate(cs->current);
316 }
317 }
318
319 return vk_command_buffer_get_record_result(&cmd->vk);
320 }
321
322 VKAPI_ATTR void VKAPI_CALL
323 hk_CmdPipelineBarrier2(VkCommandBuffer commandBuffer,
324 const VkDependencyInfo *pDependencyInfo)
325 {
326 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
327 struct hk_device *dev = hk_cmd_buffer_device(cmd);
328
329 if (HK_PERF(dev, NOBARRIER))
330 return;
331
332 perf_debug(cmd, "Pipeline barrier");
333
334 /* The big hammer. We end both compute and graphics batches. Ending compute
335 * here is necessary to properly handle graphics->compute dependencies.
336 *
337 * XXX: perf. */
338 hk_cmd_buffer_end_compute(cmd);
339 hk_cmd_buffer_end_graphics(cmd);
340 }
341
342 void
343 hk_cmd_bind_shaders(struct vk_command_buffer *vk_cmd, uint32_t stage_count,
344 const mesa_shader_stage *stages,
345 struct vk_shader **const shaders)
346 {
347 struct hk_cmd_buffer *cmd = container_of(vk_cmd, struct hk_cmd_buffer, vk);
348
349 for (uint32_t i = 0; i < stage_count; i++) {
350 struct hk_api_shader *shader =
351 container_of(shaders[i], struct hk_api_shader, vk);
352
353 if (stages[i] == MESA_SHADER_COMPUTE || stages[i] == MESA_SHADER_KERNEL)
354 hk_cmd_bind_compute_shader(cmd, shader);
355 else
356 hk_cmd_bind_graphics_shader(cmd, stages[i], shader);
357 }
358 }
359
360 static void
361 hk_bind_descriptor_sets(UNUSED struct hk_cmd_buffer *cmd,
362 struct hk_descriptor_state *desc,
363 const VkBindDescriptorSetsInfoKHR *info)
364 {
365 VK_FROM_HANDLE(vk_pipeline_layout, pipeline_layout, info->layout);
366
367 /* Fro the Vulkan 1.3.275 spec:
368 *
369 * "When binding a descriptor set (see Descriptor Set Binding) to
370 * set number N...
371 *
372 * If, additionally, the previously bound descriptor set for set
373 * N was bound using a pipeline layout not compatible for set N,
374 * then all bindings in sets numbered greater than N are
375 * disturbed."
376 *
377 * This means that, if some earlier set gets bound in such a way that
378 * it changes set_dynamic_buffer_start[s], this binding is implicitly
379 * invalidated.
380 */
381 uint8_t dyn_buffer_start =
382 pipeline_layout->dynamic_descriptor_offset[info->firstSet];
383
384 uint32_t next_dyn_offset = 0;
385 for (uint32_t i = 0; i < info->descriptorSetCount; ++i) {
386 unsigned s = i + info->firstSet;
387 VK_FROM_HANDLE(hk_descriptor_set, set, info->pDescriptorSets[i]);
388
389 if (desc->sets[s] != set) {
390 if (set != NULL) {
391 desc->root.sets[s] = hk_descriptor_set_addr(set);
392 desc->set_sizes[s] = set->size;
393 } else {
394 desc->root.sets[s] = 0;
395 desc->set_sizes[s] = 0;
396 }
397 desc->sets[s] = set;
398 desc->sets_dirty |= BITFIELD_BIT(s);
399
400 /* Binding descriptors invalidates push descriptors */
401 desc->push_dirty &= ~BITFIELD_BIT(s);
402 }
403
404 desc->root.set_dynamic_buffer_start[s] = dyn_buffer_start;
405
406 if (pipeline_layout->set_layouts[s] != NULL) {
407 const struct hk_descriptor_set_layout *set_layout =
408 vk_to_hk_descriptor_set_layout(pipeline_layout->set_layouts[s]);
409
410 if (set != NULL && set_layout->vk.dynamic_descriptor_count > 0) {
411 for (uint32_t j = 0; j < set_layout->vk.dynamic_descriptor_count; j++) {
412 struct hk_buffer_address addr = set->dynamic_buffers[j];
413 addr.base_addr += info->pDynamicOffsets[next_dyn_offset + j];
414 desc->root.dynamic_buffers[dyn_buffer_start + j] = addr;
415 }
416 next_dyn_offset += set->layout->vk.dynamic_descriptor_count;
417 }
418
419 dyn_buffer_start += set_layout->vk.dynamic_descriptor_count;
420 } else {
421 assert(set == NULL);
422 }
423 }
424 assert(dyn_buffer_start <= HK_MAX_DYNAMIC_BUFFERS);
425 assert(next_dyn_offset <= info->dynamicOffsetCount);
426
427 desc->root_dirty = true;
428 }
429
430 VKAPI_ATTR void VKAPI_CALL
431 hk_CmdBindDescriptorSets2KHR(
432 VkCommandBuffer commandBuffer,
433 const VkBindDescriptorSetsInfoKHR *pBindDescriptorSetsInfo)
434 {
435 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
436
437 if (pBindDescriptorSetsInfo->stageFlags & VK_SHADER_STAGE_ALL_GRAPHICS) {
438 hk_bind_descriptor_sets(cmd, &cmd->state.gfx.descriptors,
439 pBindDescriptorSetsInfo);
440 }
441
442 if (pBindDescriptorSetsInfo->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT) {
443 hk_bind_descriptor_sets(cmd, &cmd->state.cs.descriptors,
444 pBindDescriptorSetsInfo);
445 }
446 }
447
448 static void
449 hk_push_constants(UNUSED struct hk_cmd_buffer *cmd,
450 struct hk_descriptor_state *desc,
451 const VkPushConstantsInfoKHR *info)
452 {
453 memcpy(desc->root.push + info->offset, info->pValues, info->size);
454 desc->root_dirty = true;
455 }
456
457 VKAPI_ATTR void VKAPI_CALL
458 hk_CmdPushConstants2KHR(VkCommandBuffer commandBuffer,
459 const VkPushConstantsInfoKHR *pPushConstantsInfo)
460 {
461 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
462
463 if (pPushConstantsInfo->stageFlags & VK_SHADER_STAGE_ALL_GRAPHICS)
464 hk_push_constants(cmd, &cmd->state.gfx.descriptors, pPushConstantsInfo);
465
466 if (pPushConstantsInfo->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT)
467 hk_push_constants(cmd, &cmd->state.cs.descriptors, pPushConstantsInfo);
468 }
469
470 static struct hk_push_descriptor_set *
471 hk_cmd_push_descriptors(struct hk_cmd_buffer *cmd,
472 struct hk_descriptor_state *desc, uint32_t set)
473 {
474 assert(set < HK_MAX_SETS);
475 if (unlikely(desc->push[set] == NULL)) {
476 desc->push[set] =
477 vk_zalloc(&cmd->vk.pool->alloc, sizeof(*desc->push[set]), 8,
478 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
479 if (unlikely(desc->push[set] == NULL)) {
480 vk_command_buffer_set_error(&cmd->vk, VK_ERROR_OUT_OF_HOST_MEMORY);
481 return NULL;
482 }
483 }
484
485 /* Pushing descriptors replaces whatever sets are bound */
486 desc->sets[set] = NULL;
487 desc->push_dirty |= BITFIELD_BIT(set);
488
489 return desc->push[set];
490 }
491
492 static void
493 hk_push_descriptor_set(struct hk_cmd_buffer *cmd,
494 struct hk_descriptor_state *desc,
495 const VkPushDescriptorSetInfoKHR *info)
496 {
497 VK_FROM_HANDLE(vk_pipeline_layout, pipeline_layout, info->layout);
498
499 struct hk_push_descriptor_set *push_set =
500 hk_cmd_push_descriptors(cmd, desc, info->set);
501 if (unlikely(push_set == NULL))
502 return;
503
504 struct hk_descriptor_set_layout *set_layout =
505 vk_to_hk_descriptor_set_layout(pipeline_layout->set_layouts[info->set]);
506
507 hk_push_descriptor_set_update(push_set, set_layout,
508 info->descriptorWriteCount,
509 info->pDescriptorWrites);
510 }
511
512 VKAPI_ATTR void VKAPI_CALL
513 hk_CmdPushDescriptorSet2KHR(
514 VkCommandBuffer commandBuffer,
515 const VkPushDescriptorSetInfoKHR *pPushDescriptorSetInfo)
516 {
517 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
518
519 if (pPushDescriptorSetInfo->stageFlags & VK_SHADER_STAGE_ALL_GRAPHICS) {
520 hk_push_descriptor_set(cmd, &cmd->state.gfx.descriptors,
521 pPushDescriptorSetInfo);
522 }
523
524 if (pPushDescriptorSetInfo->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT) {
525 hk_push_descriptor_set(cmd, &cmd->state.cs.descriptors,
526 pPushDescriptorSetInfo);
527 }
528 }
529
530 void
531 hk_cmd_buffer_flush_push_descriptors(struct hk_cmd_buffer *cmd,
532 struct hk_descriptor_state *desc)
533 {
534 u_foreach_bit(set_idx, desc->push_dirty) {
535 struct hk_push_descriptor_set *push_set = desc->push[set_idx];
536 uint64_t push_set_addr = hk_pool_upload(
537 cmd, push_set->data, sizeof(push_set->data), HK_MIN_UBO_ALIGNMENT);
538
539 desc->root.sets[set_idx] = push_set_addr;
540 desc->set_sizes[set_idx] = sizeof(push_set->data);
541 }
542
543 desc->root_dirty = true;
544 desc->push_dirty = 0;
545 }
546
547 VKAPI_ATTR void VKAPI_CALL
548 hk_CmdPushDescriptorSetWithTemplate2KHR(
549 VkCommandBuffer commandBuffer, const VkPushDescriptorSetWithTemplateInfoKHR
550 *pPushDescriptorSetWithTemplateInfo)
551 {
552 VK_FROM_HANDLE(hk_cmd_buffer, cmd, commandBuffer);
553 VK_FROM_HANDLE(vk_descriptor_update_template, template,
554 pPushDescriptorSetWithTemplateInfo->descriptorUpdateTemplate);
555 VK_FROM_HANDLE(vk_pipeline_layout, pipeline_layout,
556 pPushDescriptorSetWithTemplateInfo->layout);
557
558 struct hk_descriptor_state *desc =
559 hk_get_descriptors_state(cmd, template->bind_point);
560 struct hk_push_descriptor_set *push_set = hk_cmd_push_descriptors(
561 cmd, desc, pPushDescriptorSetWithTemplateInfo->set);
562 if (unlikely(push_set == NULL))
563 return;
564
565 struct hk_descriptor_set_layout *set_layout = vk_to_hk_descriptor_set_layout(
566 pipeline_layout->set_layouts[pPushDescriptorSetWithTemplateInfo->set]);
567
568 hk_push_descriptor_set_update_template(
569 push_set, set_layout, template,
570 pPushDescriptorSetWithTemplateInfo->pData);
571 }
572
573 uint64_t
574 hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd,
575 VkPipelineBindPoint bind_point)
576 {
577 struct hk_descriptor_state *desc = hk_get_descriptors_state(cmd, bind_point);
578 struct hk_root_descriptor_table *root = &desc->root;
579
580 struct agx_ptr root_ptr = hk_pool_alloc(cmd, sizeof(*root), 8);
581 if (!root_ptr.gpu)
582 return 0;
583
584 root->root_desc_addr = root_ptr.gpu;
585
586 memcpy(root_ptr.cpu, root, sizeof(*root));
587 return root_ptr.gpu;
588 }
589
590 void
591 hk_usc_upload_spilled_rt_descs(struct agx_usc_builder *b,
592 struct hk_cmd_buffer *cmd)
593 {
594 struct hk_rendering_state *render = &cmd->state.gfx.render;
595
596 /* Upload texture/PBE descriptors for each render target so we can clear
597 * spilled render targets.
598 */
599 struct agx_ptr descs =
600 hk_pool_alloc(cmd, AGX_TEXTURE_LENGTH * 2 * render->color_att_count, 64);
601 struct agx_texture_packed *desc = descs.cpu;
602 if (!desc)
603 return;
604
605 for (unsigned i = 0; i < render->color_att_count; ++i) {
606 struct hk_image_view *iview = render->color_att[i].iview;
607 if (!iview) {
608 /* XXX: probably should emit a null descriptor here...? */
609 continue;
610 }
611
612 memcpy(&desc[(i * 2) + 0], &iview->planes[0].emrt_texture, sizeof(*desc));
613 memcpy(&desc[(i * 2) + 1], &iview->planes[0].emrt_pbe, sizeof(*desc));
614 }
615
616 desc = descs.cpu;
617
618 /* Bind the base as u0_u1 for bindless access */
619 agx_usc_uniform(b, 0, 4, hk_pool_upload(cmd, &descs.gpu, 8, 8));
620 }
621
622 void
623 hk_reserve_scratch(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
624 struct hk_shader *s)
625 {
626 struct hk_device *dev = hk_cmd_buffer_device(cmd);
627 uint32_t max_scratch_size =
628 MAX2(s->b.info.scratch_size, s->b.info.preamble_scratch_size);
629
630 /* Not scratch but this is the most convenient place for this... */
631 cs->uses_sampler_heap |= s->b.info.uses_sampler_heap;
632
633 if (max_scratch_size == 0)
634 return;
635
636 unsigned preamble_size = (s->b.info.preamble_scratch_size > 0) ? 1 : 0;
637
638 /* Note: this uses the hardware stage, not the software stage */
639 hk_device_alloc_scratch(dev, s->b.info.stage, max_scratch_size);
640 perf_debug(cmd, "Reserving %u (%u) bytes of scratch for stage %s",
641 s->b.info.scratch_size, s->b.info.preamble_scratch_size,
642 _mesa_shader_stage_to_abbrev(s->b.info.stage));
643
644 switch (s->b.info.stage) {
645 case MESA_SHADER_FRAGMENT:
646 cs->scratch.fs.main = true;
647 cs->scratch.fs.preamble = MAX2(cs->scratch.fs.preamble, preamble_size);
648 break;
649 case MESA_SHADER_VERTEX:
650 cs->scratch.vs.main = true;
651 cs->scratch.vs.preamble = MAX2(cs->scratch.vs.preamble, preamble_size);
652 break;
653 default:
654 cs->scratch.cs.main = true;
655 cs->scratch.cs.preamble = MAX2(cs->scratch.cs.preamble, preamble_size);
656 break;
657 }
658 }
659
660 uint32_t
661 hk_upload_usc_words(struct hk_cmd_buffer *cmd, struct hk_shader *s,
662 struct hk_linked_shader *linked)
663 {
664 struct hk_device *dev = hk_cmd_buffer_device(cmd);
665
666 mesa_shader_stage sw_stage = s->info.stage;
667
668 unsigned constant_push_ranges = DIV_ROUND_UP(s->b.info.rodata.size_16, 64);
669 unsigned push_ranges = 2;
670 unsigned stage_ranges = 3;
671
672 size_t usc_size =
673 agx_usc_size(constant_push_ranges + push_ranges + stage_ranges + 4);
674 struct agx_ptr t = hk_pool_usc_alloc(cmd, usc_size, 64);
675 if (!t.cpu)
676 return 0;
677
678 struct agx_usc_builder b = agx_usc_builder(t.cpu, usc_size);
679
680 uint64_t root_ptr;
681
682 if (sw_stage == MESA_SHADER_COMPUTE) {
683 root_ptr = hk_cmd_buffer_upload_root(cmd, VK_PIPELINE_BIND_POINT_COMPUTE);
684 } else {
685 root_ptr = cmd->state.gfx.root;
686 }
687
688 static_assert(offsetof(struct hk_root_descriptor_table, root_desc_addr) == 0,
689 "self-reflective");
690
691 unsigned root_unif = 0;
692 if (sw_stage == MESA_SHADER_VERTEX) {
693 unsigned count =
694 DIV_ROUND_UP(BITSET_LAST_BIT(s->info.vs.attrib_components_read), 4);
695
696 if (count) {
697 agx_usc_uniform(
698 &b, AGX_ABI_VUNI_VBO_BASE(0), 4 * count,
699 root_ptr + hk_root_descriptor_offset(draw.attrib_base));
700
701 agx_usc_uniform(
702 &b, AGX_ABI_VUNI_VBO_CLAMP(count, 0), 2 * count,
703 root_ptr + hk_root_descriptor_offset(draw.attrib_clamps));
704 }
705
706 if (cmd->state.gfx.draw_params) {
707 agx_usc_uniform(&b, AGX_ABI_VUNI_FIRST_VERTEX(count), 4,
708 cmd->state.gfx.draw_params);
709 }
710
711 if (cmd->state.gfx.draw_id_ptr) {
712 agx_usc_uniform(&b, AGX_ABI_VUNI_DRAW_ID(count), 1,
713 cmd->state.gfx.draw_id_ptr);
714 }
715
716 if (linked->sw_indexing) {
717 agx_usc_uniform(
718 &b, AGX_ABI_VUNI_VERTEX_PARAMS(count), 4,
719 root_ptr + hk_root_descriptor_offset(draw.vertex_params));
720 }
721
722 root_unif = AGX_ABI_VUNI_COUNT_VK(count);
723 } else if (sw_stage == MESA_SHADER_FRAGMENT) {
724 if (agx_tilebuffer_spills(&cmd->state.gfx.render.tilebuffer)) {
725 hk_usc_upload_spilled_rt_descs(&b, cmd);
726 }
727
728 if (cmd->state.gfx.uses_blend_constant) {
729 agx_usc_uniform(
730 &b, 4, 8,
731 root_ptr + hk_root_descriptor_offset(draw.blend_constant));
732 }
733
734 /* The SHARED state is baked into linked->usc for non-fragment shaders. We
735 * don't pass around the information to bake the tilebuffer layout.
736 *
737 * TODO: We probably could with some refactor.
738 */
739 agx_usc_push_packed(&b, SHARED, &cmd->state.gfx.render.tilebuffer.usc);
740 root_unif = AGX_ABI_FUNI_ROOT;
741 }
742
743 /* Address for the root and each set */
744 agx_usc_uniform(&b, root_unif, 4 * (1 + s->info.set_count), root_ptr);
745
746 agx_usc_push_blob(&b, linked->usc.data, linked->usc.size);
747 return agx_usc_addr(&dev->dev, t.gpu);
748 }
749
750 void
751 hk_dispatch_precomp(struct hk_cmd_buffer *cmd, struct agx_grid grid,
752 enum agx_barrier barrier, enum libagx_program idx,
753 void *data, size_t data_size)
754 {
755 struct hk_device *dev = hk_cmd_buffer_device(cmd);
756 struct agx_precompiled_shader *prog = agx_get_precompiled(&dev->bg_eot, idx);
757
758 struct hk_cs **target = (barrier & AGX_POSTGFX) ? &cmd->current_cs.post_gfx
759 : (barrier & AGX_PREGFX) ? &cmd->current_cs.pre_gfx
760 : &cmd->current_cs.cs;
761
762 struct hk_cs *cs = hk_cmd_buffer_get_cs_general(cmd, target, true);
763 if (!cs)
764 return;
765
766 struct agx_ptr t = hk_pool_usc_alloc(cmd, agx_usc_size(15), 64);
767 uint64_t uploaded_data = hk_pool_upload(cmd, data, data_size, 4);
768
769 agx_usc_words_precomp(t.cpu, &prog->b, uploaded_data, data_size);
770
771 hk_dispatch_with_usc_launch(dev, cs, prog->b.launch,
772 agx_usc_addr(&dev->dev, t.gpu), grid,
773 prog->b.workgroup);
774 }
775
776 void
777 hk_cs_init_graphics(struct hk_cmd_buffer *cmd, struct hk_cs *cs)
778 {
779 struct hk_rendering_state *render = &cmd->state.gfx.render;
780 uint8_t *map = cs->current;
781
782 cs->tib = render->tilebuffer;
783
784 /* Assume this is not the first control stream of the render pass, so
785 * initially use the partial background/EOT program and ZLS control.
786 * hk_BeginRendering/hk_EndRendering will override.
787 */
788 cs->cr = render->cr;
789 cs->cr.bg.main = render->cr.bg.partial;
790 cs->cr.eot.main = render->cr.eot.partial;
791 cs->cr.zls_control = render->cr.zls_control_partial;
792
793 /* Barrier to enforce GPU-CPU coherency, in case this batch is back to back
794 * with another that caused stale data to be cached and the CPU wrote to it
795 * in the meantime.
796 */
797 agx_push(map, VDM_BARRIER, cfg) {
798 cfg.usc_cache_inval = true;
799 }
800
801 struct AGX_PPP_HEADER present = {
802 .w_clamp = true,
803 .occlusion_query_2 = true,
804 .output_unknown = true,
805 .varying_word_2 = true,
806 .viewport_count = 1, /* irrelevant */
807 };
808
809 size_t size = agx_ppp_update_size(&present);
810 struct agx_ptr T = hk_pool_alloc(cmd, size, AGX_PPP_HEADER_ALIGN);
811 if (!T.cpu)
812 return;
813
814 struct agx_ppp_update ppp = agx_new_ppp_update(T, size, &present);
815
816 /* clang-format off */
817 agx_ppp_push(&ppp, W_CLAMP, cfg) cfg.w_clamp = 1e-10;
818 agx_ppp_push(&ppp, FRAGMENT_OCCLUSION_QUERY_2, cfg);
819 agx_ppp_push(&ppp, OUTPUT_UNKNOWN, cfg);
820 agx_ppp_push(&ppp, VARYING_2, cfg);
821 /* clang-format on */
822
823 agx_ppp_fini(&map, &ppp);
824 cs->current = map;
825
826 cs->scissor = UTIL_DYNARRAY_INIT;
827 cs->depth_bias = UTIL_DYNARRAY_INIT;
828
829 /* All graphics state must be reemited in each control stream */
830 hk_cmd_buffer_dirty_all(cmd);
831 }
832
833 void
834 hk_ensure_cs_has_space(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
835 size_t space)
836 {
837 bool vdm = cs->type == HK_CS_VDM;
838
839 size_t link_length =
840 vdm ? AGX_VDM_STREAM_LINK_LENGTH : AGX_CDM_STREAM_LINK_LENGTH;
841
842 /* Assert that we have space for a link tag */
843 assert((cs->current + link_length) <= cs->end && "Encoder overflowed");
844
845 /* Always leave room for a link tag, in case we run out of space later,
846 * plus padding because VDM apparently overreads?
847 *
848 * 0x200 is not enough. 0x400 seems to work. 0x800 for safety.
849 */
850 space += link_length + 0x800;
851
852 /* If there is room in the command buffer, we're done */
853 if (likely((cs->end - cs->current) >= space))
854 return;
855
856 /* Otherwise, we need to allocate a new command buffer. We use memory owned
857 * by the batch to simplify lifetime management for the BO.
858 */
859 size_t size = 65536;
860 struct agx_ptr T = hk_pool_alloc(cmd, size, 256);
861
862 /* Jump from the old control stream to the new control stream */
863 agx_cs_jump(cs->current, T.gpu, vdm);
864
865 /* Swap out the control stream */
866 cs->current = T.cpu;
867 cs->end = cs->current + size;
868 cs->chunk = T;
869 cs->stream_linked = true;
870 }
871
872 static void
873 clear_attachment_as_image(struct hk_cmd_buffer *cmd,
874 struct hk_rendering_state *render,
875 struct hk_attachment *att, unsigned aspect)
876 {
877 struct hk_image_view *view = att->iview;
878 if (!att->clear || !view || !(view->vk.aspects & aspect))
879 return;
880
881 const uint32_t layer_count = render->view_mask
882 ? util_last_bit(render->view_mask)
883 : render->layer_count;
884
885 const VkImageSubresourceRange range = {
886 .layerCount = layer_count,
887 .levelCount = 1,
888 .baseArrayLayer = view->vk.base_array_layer,
889 .baseMipLevel = view->vk.base_mip_level,
890 .aspectMask = view->vk.aspects & aspect,
891 };
892
893 assert(util_bitcount(range.aspectMask) == 1);
894 VkFormat format = att->vk_format;
895
896 if (aspect == VK_IMAGE_ASPECT_DEPTH_BIT) {
897 format = vk_format_depth_only(format);
898 } else if (aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
899 format = vk_format_stencil_only(format);
900 }
901
902 struct hk_image *image = container_of(view->vk.image, struct hk_image, vk);
903 hk_clear_image(cmd, image, vk_format_to_pipe_format(format),
904 att->clear_colour, &range, false /* partially clear 3D */);
905 }
906
907 void
908 hk_optimize_empty_vdm(struct hk_cmd_buffer *cmd)
909 {
910 struct hk_cs *cs = cmd->current_cs.gfx;
911 struct hk_rendering_state *render = &cmd->state.gfx.render;
912
913 for (unsigned i = 0; i < render->color_att_count; ++i) {
914 clear_attachment_as_image(cmd, render, &render->color_att[i], ~0);
915 }
916
917 clear_attachment_as_image(cmd, render, &render->depth_att,
918 VK_IMAGE_ASPECT_DEPTH_BIT);
919
920 clear_attachment_as_image(cmd, render, &render->stencil_att,
921 VK_IMAGE_ASPECT_STENCIL_BIT);
922
923 /* Remove the VDM control stream from the command buffer, now that it is
924 * replaced by equivalent other operations.
925 */
926 list_del(&cs->node);
927 hk_cs_destroy(cs);
928 }