Debian Codegraph @

src/asahi/vulkan/hk_cmd_buffer.h

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
8 #pragma once
9
10 #include "util/macros.h"
11
12 #include "util/list.h"
13 #include "agx_abi.h"
14 #include "agx_helpers.h"
15 #include "agx_linker.h"
16 #include "agx_pack.h"
17 #include "agx_tilebuffer.h"
18 #include "agx_uvs.h"
19 #include "libagx_dgc.h"
20 #include "pool.h"
21 #include "shader_enums.h"
22
23 #include "hk_private.h"
24 #include "hk_shader.h"
25
26 #include "hk_cmd_pool.h"
27 #include "hk_descriptor_set.h"
28
29 #include "asahi/lib/agx_nir_lower_vbo.h"
30 #include "util/u_dynarray.h"
31 #include "vulkan/vulkan_core.h"
32
33 #include "vk_command_buffer.h"
34
35 #include <stdio.h>
36
37 struct hk_buffer;
38 struct hk_cmd_bo;
39 struct hk_cmd_pool;
40 struct hk_image_view;
41 struct hk_push_descriptor_set;
42 struct hk_shader;
43 struct hk_linked_shader;
44 struct agx_usc_builder;
45 struct vk_shader;
46
47 /** Root descriptor table. */
48 struct hk_root_descriptor_table {
49 /* Address of this descriptor itself. Must be first for reflection. */
50 uint64_t root_desc_addr;
51
52 /* Descriptor set base addresses. Must follow root_desc_addr to match our
53 * push layout.
54 */
55 uint64_t sets[HK_MAX_SETS];
56
57 union {
58 struct {
59 uint32_t view_index;
60 uint32_t ppp_multisamplectl;
61
62 /* Vertex input state */
63 uint64_t attrib_base[AGX_MAX_VBUFS];
64 uint32_t attrib_clamps[AGX_MAX_VBUFS];
65 uint32_t attrib_strides[AGX_MAX_VBUFS];
66
67 /* Mask of outputs flowing VS->TCS, VS->GS, or TES->GS . */
68 uint64_t vertex_outputs;
69
70 /* Address of vertex param buffer if geom/tess is used, else 0 */
71 uint64_t vertex_params;
72
73 /* Address of tessellation param buffer if tessellation used, else 0 */
74 uint64_t tess_params;
75
76 /* Address of geometry param buffer if GS is used, else 0 */
77 uint64_t geometry_params;
78
79 /* Pipeline statistics queries. This is a base address with flags. */
80 uint64_t pipeline_stats;
81 VkQueryPipelineStatisticFlags pipeline_stats_flags;
82
83 float blend_constant[4];
84 uint16_t no_epilog_discard;
85 uint16_t _pad1;
86 uint16_t api_sample_mask;
87 uint16_t _pad2;
88 uint16_t force_never_in_shader;
89 uint16_t _pad3;
90 uint16_t provoking;
91 uint16_t _pad4;
92
93 /* True if there is an API geometry shader. If false, there may still
94 * be a geometry shader in use (notably for transform feedback) but it
95 * should not contribute to pipeline statistics.
96 */
97 uint16_t api_gs;
98 uint16_t _pad5;
99
100 uint16_t rasterization_stream;
101 uint16_t _pad6;
102
103 /* Mapping from varying slots written by the last vertex stage to UVS
104 * indices. This mapping must be compatible with the fragment shader.
105 */
106 uint8_t uvs_index[VARYING_SLOT_MAX];
107 } draw;
108 struct {
109 uint64_t group_count_addr;
110 uint32_t base_group[3];
111 } cs;
112 };
113
114 /* Client push constants */
115 uint8_t push[HK_MAX_PUSH_SIZE];
116
117 /* Dynamic buffer bindings */
118 struct hk_buffer_address dynamic_buffers[HK_MAX_DYNAMIC_BUFFERS];
119
120 /* Start index in dynamic_buffers where each set starts */
121 uint8_t set_dynamic_buffer_start[HK_MAX_SETS];
122 };
123
124 /* helper macro for computing root descriptor byte offsets */
125 #define hk_root_descriptor_offset(member) \
126 offsetof(struct hk_root_descriptor_table, member)
127
128 struct hk_descriptor_state {
129 bool root_dirty;
130 struct hk_root_descriptor_table root;
131
132 uint32_t set_sizes[HK_MAX_SETS];
133 struct hk_descriptor_set *sets[HK_MAX_SETS];
134 uint32_t sets_dirty;
135
136 struct hk_push_descriptor_set *push[HK_MAX_SETS];
137 uint32_t push_dirty;
138 };
139
140 struct hk_attachment {
141 VkFormat vk_format;
142 struct hk_image_view *iview;
143
144 VkResolveModeFlagBits resolve_mode;
145 struct hk_image_view *resolve_iview;
146
147 bool clear;
148 uint32_t clear_colour[4];
149 };
150
151 struct hk_bg_eot {
152 uint64_t usc;
153 struct agx_counts_packed counts;
154 };
155
156 struct hk_render_registers {
157 uint32_t width, height, layers;
158 uint32_t zls_width, zls_height;
159 uint32_t isp_bgobjdepth;
160 uint32_t isp_bgobjvals;
161 struct agx_zls_control_packed zls_control, zls_control_partial;
162 uint32_t depth_dimensions;
163 bool process_empty_tiles;
164 enum u_tristate dbias_is_int;
165
166 struct {
167 uint32_t dimensions;
168 uint64_t buffer, meta;
169 uint32_t stride, meta_stride;
170 } depth;
171
172 struct {
173 uint64_t buffer, meta;
174 uint32_t stride, meta_stride;
175 } stencil;
176
177 struct {
178 struct hk_bg_eot main;
179 struct hk_bg_eot partial;
180 } bg;
181
182 struct {
183 struct hk_bg_eot main;
184 struct hk_bg_eot partial;
185 } eot;
186 };
187
188 struct hk_rendering_state {
189 VkRenderingFlagBits flags;
190
191 VkRect2D area;
192 uint32_t layer_count;
193 uint32_t view_mask;
194
195 uint32_t color_att_count;
196 struct hk_attachment color_att[HK_MAX_RTS];
197 struct hk_attachment depth_att;
198 struct hk_attachment stencil_att;
199
200 struct agx_tilebuffer_layout tilebuffer;
201 struct hk_render_registers cr;
202 };
203
204 struct hk_index_buffer_state {
205 struct hk_addr_range buffer;
206 enum agx_index_size size;
207 uint32_t restart;
208 };
209
210 /* Dirty tracking bits for state not tracked by vk_dynamic_graphics_state or
211 * shaders_dirty.
212 */
213 enum hk_dirty {
214 HK_DIRTY_VB = BITFIELD_BIT(0),
215 HK_DIRTY_OCCLUSION = BITFIELD_BIT(1),
216 HK_DIRTY_PROVOKING = BITFIELD_BIT(2),
217 HK_DIRTY_VARYINGS = BITFIELD_BIT(3),
218 };
219
220 struct hk_graphics_state {
221 struct hk_rendering_state render;
222 struct hk_descriptor_state descriptors;
223
224 enum hk_dirty dirty;
225
226 uint64_t root;
227 uint64_t draw_params;
228 uint64_t draw_id_ptr;
229
230 uint32_t shaders_dirty;
231 struct hk_api_shader *shaders[MESA_SHADER_MESH + 1];
232
233 /* Vertex buffers */
234 struct hk_addr_range vb[AGX_MAX_VBUFS];
235
236 /* Transform feedback buffers */
237 struct hk_addr_range xfb[4];
238
239 /* Is transform feedback enabled? */
240 bool xfb_enabled;
241
242 /* Internal transform feedback offset vec4.
243 *
244 * TODO: Strictly could be global.
245 */
246 uint64_t xfb_offsets;
247
248 /* Pointer to the GPU memory backing active transform feedback queries,
249 * per-stream. Zero if no query is bound.
250 */
251 uint64_t xfb_query[4];
252
253 struct hk_index_buffer_state index;
254 enum agx_primitive topology;
255 enum agx_object_type object_type;
256
257 /* Provoking vertex 0, 1, or 2. Usually 0 or 2 for FIRST/LAST. 1 can only be
258 * set for tri fans.
259 */
260 uint8_t provoking;
261
262 struct {
263 enum agx_visibility_mode mode;
264
265 /* If enabled, index of the current occlusion query in the occlusion heap.
266 * There can only be one active at a time (hardware contraint).
267 */
268 uint16_t index;
269 } occlusion;
270
271 /* Fast linked shader data structures */
272 uint64_t varyings;
273 struct agx_varyings_vs linked_varyings;
274
275 uint32_t linked_dirty;
276 struct hk_linked_shader *linked[MESA_SHADER_STAGES];
277 bool generate_primitive_id;
278
279 /* Whether blend constants are required by the active blend state */
280 bool uses_blend_constant;
281
282 /* Tessellation state */
283 struct {
284 uint64_t out_draws;
285 uint64_t grids;
286 struct hk_tess_info info;
287 enum mesa_prim prim;
288 } tess;
289
290 /* Needed by vk_command_buffer::dynamic_graphics_state */
291 struct vk_vertex_input_state _dynamic_vi;
292 struct vk_sample_locations_state _dynamic_sl;
293 };
294
295 struct hk_compute_state {
296 struct hk_descriptor_state descriptors;
297 struct hk_api_shader *shader;
298 };
299
300 struct hk_cmd_push {
301 void *map;
302 uint64_t addr;
303 uint32_t range;
304 bool no_prefetch;
305 };
306
307 struct hk_scratch_req {
308 bool main;
309 bool preamble;
310 };
311
312 /*
313 * Represents a firmware timestamp request. Handle is a kernel timestamp object
314 * handle, not a GEM handle.
315 *
316 * The kernel/firmware uses the handle/offset_B to write. We use the address to
317 * read the results back. We could deduplicate this, but this is convenient.
318 */
319 struct agx_timestamp_req {
320 uint64_t addr;
321 uint32_t handle;
322 uint32_t offset_B;
323 };
324
325 /*
326 * hk_cs represents a single control stream, to be enqueued either to the
327 * CDM or VDM for compute/3D respectively.
328 */
329 enum hk_cs_type {
330 HK_CS_CDM,
331 HK_CS_VDM,
332 };
333
334 struct hk_cs {
335 struct list_head node;
336
337 /* Parent command buffer. Convenience. */
338 struct hk_cmd_buffer *cmd;
339
340 /* Data master */
341 enum hk_cs_type type;
342
343 /* Address of the root control stream for the job */
344 uint64_t addr;
345
346 /* Fat pointer to the start of the current chunk of the control stream */
347 struct agx_ptr chunk;
348
349 /* Start pointer of the root control stream */
350 void *start;
351
352 /* Current pointer within the control stream */
353 void *current;
354
355 /* End pointer of the current chunk of the control stream */
356 void *end;
357
358 /* Whether there is more than just the root chunk */
359 bool stream_linked;
360
361 /* Whether the sampler heap is required. Although we always must maintain the
362 * heap for correctness, it's often not necessary since we can push lots of
363 * samplers (especially for GL/DX11-era engines).
364 */
365 bool uses_sampler_heap;
366
367 /* Scratch requirements */
368 struct {
369 union {
370 struct hk_scratch_req vs;
371 struct hk_scratch_req cs;
372 };
373
374 struct hk_scratch_req fs;
375 } scratch;
376
377 /* Immediate writes, type libagx_imm_write. These all happen in parallel at
378 * the end of the control stream. This accelerates queries. Implies CDM.
379 */
380 struct util_dynarray imm_writes;
381
382 /* Statistics */
383 struct {
384 uint32_t calls, cmds, flushes, merged;
385 } stats;
386
387 /* Timestamp writes. Currently just compute end / fragment end. We could
388 * flesh this out later if we want finer info. (We will, but it's not
389 * required for conformance.)
390 */
391 struct {
392 struct agx_timestamp_req end;
393 } timestamp;
394
395 /* Remaining state is for graphics only, ignored for compute */
396 struct agx_tilebuffer_layout tib;
397
398 struct util_dynarray scissor, depth_bias;
399 uint64_t uploaded_scissor, uploaded_zbias;
400
401 /* We can only set ppp_multisamplectl once per batch. has_sample_locations
402 * tracks if we've committed to a set of sample locations yet. vk_meta
403 * operations do not set has_sample_locations since they don't care and it
404 * would interfere with the app-provided samples.
405 *
406 */
407 bool has_sample_locations;
408 uint32_t ppp_multisamplectl;
409
410 struct hk_render_registers cr;
411
412 /* Active restart index if one is set. Zero if there is no restart index set
413 * yet, since Vulkan does not allow zero restart indices (unlike OpenGL).
414 * This is used in place of dirty tracking, because dirty tracking
415 * restart indices is complicated and just checking the saved value is cheap.
416 */
417 uint32_t restart_index;
418 };
419
420 /*
421 * Helper to merge two compute control streams, concatenating the second control
422 * stream to the first one. Must sync with hk_cs.
423 */
424 static inline void
425 hk_cs_merge_cdm(struct hk_cs *a, const struct hk_cs *b)
426 {
427 assert(a->type == HK_CS_CDM && b->type == HK_CS_CDM);
428 assert(a->cmd == b->cmd);
429 assert(!a->timestamp.end.handle);
430
431 agx_cdm_jump(a->current, b->addr);
432 a->current = b->current;
433 a->stream_linked = true;
434
435 a->uses_sampler_heap |= b->uses_sampler_heap;
436 a->scratch.cs.main |= b->scratch.cs.main;
437 a->scratch.cs.preamble |= b->scratch.cs.preamble;
438
439 a->timestamp = b->timestamp;
440
441 a->stats.calls += b->stats.calls;
442 a->stats.cmds += b->stats.cmds;
443 a->stats.flushes += b->stats.flushes;
444 a->stats.merged++;
445 }
446
447 static inline uint64_t
448 hk_cs_current_addr(struct hk_cs *cs)
449 {
450 return cs->chunk.gpu + ((uint8_t *)cs->current - (uint8_t *)cs->chunk.cpu);
451 }
452
453 struct hk_uploader {
454 /** List of hk_cmd_bo */
455 struct list_head bos;
456
457 /* Current addresses */
458 uint8_t *map;
459 uint64_t base;
460 uint32_t offset;
461 };
462
463 struct hk_cmd_buffer {
464 struct vk_command_buffer vk;
465
466 struct {
467 struct hk_graphics_state gfx;
468 struct hk_compute_state cs;
469 } state;
470
471 struct {
472 struct hk_uploader main, usc;
473 } uploader;
474
475 /* List of all recorded control streams */
476 struct list_head control_streams;
477
478 /* Current recorded control stream */
479 struct {
480 /* VDM stream for 3D */
481 struct hk_cs *gfx;
482
483 /* CDM stream for compute */
484 struct hk_cs *cs;
485
486 /* CDM stream that executes immediately before the current graphics
487 * control stream. Used for geometry shading, tessellation, etc.
488 */
489 struct hk_cs *pre_gfx;
490
491 /* CDM stream that will execute after the current graphics control stream
492 * finishes. Used for queries.
493 */
494 struct hk_cs *post_gfx;
495 } current_cs;
496
497 /* Are we currently inside a vk_meta operation? This alters sample location
498 * behaviour.
499 */
500 bool in_meta;
501
502 /* XXX: move me?
503 *
504 * Indirect draw generated by the indirect GS translator.
505 */
506 uint64_t geom_index_buffer;
507 uint32_t geom_index_count;
508 uint32_t geom_instance_count;
509
510 /* Does the command buffer use the geometry heap? */
511 bool uses_heap;
512
513 /* Owned large BOs */
514 struct util_dynarray large_bos;
515 };
516
517 VK_DEFINE_HANDLE_CASTS(hk_cmd_buffer, vk.base, VkCommandBuffer,
518 VK_OBJECT_TYPE_COMMAND_BUFFER)
519
520 extern const struct vk_command_buffer_ops hk_cmd_buffer_ops;
521
522 static inline struct hk_cmd_pool *
523 hk_cmd_buffer_pool(struct hk_cmd_buffer *cmd)
524 {
525 return (struct hk_cmd_pool *)cmd->vk.pool;
526 }
527
528 /*
529 * The hardware vertex shader is supplied by the last geometry stage. The
530 * geometry pipeline is vertex->tess->geometry so we search backwards.
531 */
532 static inline struct hk_shader *
533 hk_bound_hw_vs(struct hk_graphics_state *gfx)
534 {
535 struct hk_api_shader *vs = gfx->shaders[MESA_SHADER_VERTEX];
536 struct hk_api_shader *tes = gfx->shaders[MESA_SHADER_TESS_EVAL];
537 struct hk_api_shader *gs = gfx->shaders[MESA_SHADER_GEOMETRY];
538
539 if (gs)
540 return &gs->variants[HK_GS_VARIANT_RAST];
541 else if (tes)
542 return &tes->variants[HK_VS_VARIANT_HW];
543 else
544 return &vs->variants[HK_VS_VARIANT_HW];
545 }
546
547 static inline struct hk_shader *
548 hk_bound_sw_vs(struct hk_graphics_state *gfx)
549 {
550 struct hk_api_shader *vs = gfx->shaders[MESA_SHADER_VERTEX];
551 struct hk_shader *hw_vs = hk_bound_hw_vs(gfx);
552
553 if (hw_vs == &vs->variants[HK_VS_VARIANT_HW])
554 return hw_vs;
555 else
556 return &vs->variants[HK_VS_VARIANT_SW];
557 }
558
559 static inline struct hk_shader *
560 hk_bound_sw_vs_before_gs(struct hk_graphics_state *gfx)
561 {
562 struct hk_api_shader *vs = gfx->shaders[MESA_SHADER_VERTEX];
563 struct hk_api_shader *tes = gfx->shaders[MESA_SHADER_TESS_EVAL];
564 struct hk_api_shader *api = tes ?: vs;
565
566 return &api->variants[HK_VS_VARIANT_SW];
567 }
568
569 struct agx_ptr hk_pool_alloc_internal(struct hk_cmd_buffer *cmd, uint32_t size,
570 uint32_t alignment, bool usc);
571
572 uint64_t hk_pool_upload(struct hk_cmd_buffer *cmd, const void *data,
573 uint32_t size, uint32_t alignment);
574
575 static inline struct agx_ptr
576 hk_pool_alloc(struct hk_cmd_buffer *cmd, uint32_t size, uint32_t alignment)
577 {
578 return hk_pool_alloc_internal(cmd, size, alignment, false);
579 }
580
581 static inline struct agx_ptr
582 hk_pool_usc_alloc(struct hk_cmd_buffer *cmd, uint32_t size, uint32_t alignment)
583 {
584 return hk_pool_alloc_internal(cmd, size, alignment, true);
585 }
586
587 void hk_cs_init_graphics(struct hk_cmd_buffer *cmd, struct hk_cs *cs);
588 uint32_t hk_default_sample_positions(unsigned nr_samples);
589
590 static inline struct hk_cs *
591 hk_cmd_buffer_get_cs_general(struct hk_cmd_buffer *cmd, struct hk_cs **ptr,
592 bool compute)
593 {
594 if ((*ptr) == NULL) {
595 /* Allocate root control stream */
596 size_t initial_size = 65536;
597 struct agx_ptr root = hk_pool_alloc(cmd, initial_size, 1024);
598 if (!root.cpu)
599 return NULL;
600
601 /* Allocate hk_cs for the new stream */
602 struct hk_cs *cs = malloc(sizeof(*cs));
603 *cs = (struct hk_cs){
604 .cmd = cmd,
605 .type = compute ? HK_CS_CDM : HK_CS_VDM,
606 .addr = root.gpu,
607 .start = root.cpu,
608 .chunk = root,
609 .current = root.cpu,
610 .end = root.cpu + initial_size,
611 };
612
613 list_inithead(&cs->node);
614
615 bool before_gfx = (ptr == &cmd->current_cs.pre_gfx);
616
617 /* Insert into the command buffer. We usually append to the end of the
618 * command buffer, except for pre-graphics streams which go right before
619 * the graphics workload. (This implies a level of out-of-order processing
620 * that's allowed by Vulkan and required for efficient
621 * geometry/tessellation shaders.)
622 */
623 if (before_gfx && cmd->current_cs.gfx) {
624 list_addtail(&cs->node, &cmd->current_cs.gfx->node);
625 } else {
626 list_addtail(&cs->node, &cmd->control_streams);
627 }
628
629 *ptr = cs;
630
631 if (!compute)
632 hk_cs_init_graphics(cmd, cs);
633 }
634
635 assert(*ptr != NULL);
636 return *ptr;
637 }
638
639 static inline struct hk_cs *
640 hk_cmd_buffer_get_cs(struct hk_cmd_buffer *cmd, bool compute)
641 {
642 struct hk_cs **ptr = compute ? &cmd->current_cs.cs : &cmd->current_cs.gfx;
643 return hk_cmd_buffer_get_cs_general(cmd, ptr, compute);
644 }
645
646 void hk_ensure_cs_has_space(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
647 size_t space);
648
649 static inline uint64_t
650 hk_cs_alloc_for_indirect(struct hk_cs *cs, size_t size_B)
651 {
652 hk_ensure_cs_has_space(cs->cmd, cs, size_B);
653
654 uint64_t addr = hk_cs_current_addr(cs);
655 cs->current += size_B;
656 return addr;
657 }
658
659 static void
660 hk_cmd_buffer_dirty_all(struct hk_cmd_buffer *cmd)
661 {
662 struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state;
663 struct hk_graphics_state *gfx = &cmd->state.gfx;
664
665 vk_dynamic_graphics_state_dirty_all(dyn);
666 gfx->dirty = ~0;
667 gfx->shaders_dirty = ~0;
668 gfx->linked_dirty = ~0;
669 gfx->descriptors.root_dirty = true;
670 }
671
672 static inline void
673 hk_cs_destroy(struct hk_cs *cs)
674 {
675 if (cs->type == HK_CS_VDM) {
676 util_dynarray_fini(&cs->scissor);
677 util_dynarray_fini(&cs->depth_bias);
678 } else {
679 util_dynarray_fini(&cs->imm_writes);
680 }
681
682 free(cs);
683 }
684
685 void hk_dispatch_imm_writes(struct hk_cmd_buffer *cmd, struct hk_cs *cs);
686
687 static void
688 hk_cmd_buffer_end_compute_internal(struct hk_cmd_buffer *cmd,
689 struct hk_cs **ptr)
690 {
691 if (*ptr) {
692 struct hk_cs *cs = *ptr;
693
694 /* This control stream may write immediates as it ends. Queue the writes
695 * now that we're done emitting everything else.
696 */
697 if (cs->imm_writes.size) {
698 hk_dispatch_imm_writes(cmd, cs);
699 }
700 }
701
702 *ptr = NULL;
703 }
704
705 static void
706 hk_cmd_buffer_end_compute(struct hk_cmd_buffer *cmd)
707 {
708 hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.cs);
709 }
710
711 void hk_optimize_empty_vdm(struct hk_cmd_buffer *cmd);
712
713 static void
714 hk_cmd_buffer_end_graphics(struct hk_cmd_buffer *cmd)
715 {
716 struct hk_cs *cs = cmd->current_cs.gfx;
717
718 if (cs && cs->stats.cmds == 0) {
719 hk_optimize_empty_vdm(cmd);
720 } else if (cs) {
721 /* Scissor and depth bias arrays are staged to dynamic arrays on the
722 * CPU. When we end the control stream, they're done growing and are
723 * ready for upload.
724 */
725 cs->uploaded_scissor =
726 hk_pool_upload(cmd, cs->scissor.data, cs->scissor.size, 64);
727
728 cs->uploaded_zbias =
729 hk_pool_upload(cmd, cs->depth_bias.data, cs->depth_bias.size, 64);
730
731 /* TODO: maybe free scissor/depth_bias now? */
732 cs->current = agx_vdm_terminate(cs->current);
733 }
734
735 cmd->current_cs.gfx = NULL;
736
737 hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.pre_gfx);
738 hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.post_gfx);
739
740 assert(cmd->current_cs.pre_gfx == NULL);
741 assert(cmd->current_cs.post_gfx == NULL);
742
743 /* We just flushed out the heap use. If we want to use it again, we'll need
744 * to queue a free for it again.
745 */
746 cmd->uses_heap = false;
747 }
748
749 static inline uint64_t
750 hk_pipeline_stat_addr(struct hk_cmd_buffer *cmd,
751 VkQueryPipelineStatisticFlagBits stat)
752 {
753 struct hk_root_descriptor_table *root = &cmd->state.gfx.descriptors.root;
754 VkQueryPipelineStatisticFlags flags = root->draw.pipeline_stats_flags;
755
756 if (flags & stat) {
757 assert(!cmd->in_meta && "queries paused for meta");
758 assert(util_bitcount(stat) == 1 && "by construction");
759
760 /* Prefix sum to determine the compacted index in the query pool */
761 uint32_t index = util_bitcount(flags & (stat - 1));
762
763 return root->draw.pipeline_stats + (sizeof(uint64_t) * index);
764 } else {
765 /* Query disabled */
766 return AGX_SCRATCH_PAGE_ADDRESS;
767 }
768 }
769
770 static inline bool
771 hk_stat_enabled(uint64_t addr)
772 {
773 return addr != AGX_SCRATCH_PAGE_ADDRESS;
774 }
775
776 void hk_cmd_buffer_begin_graphics(struct hk_cmd_buffer *cmd,
777 const VkCommandBufferBeginInfo *pBeginInfo);
778 void hk_cmd_buffer_begin_compute(struct hk_cmd_buffer *cmd,
779 const VkCommandBufferBeginInfo *pBeginInfo);
780
781 void hk_cmd_invalidate_graphics_state(struct hk_cmd_buffer *cmd);
782 void hk_cmd_invalidate_compute_state(struct hk_cmd_buffer *cmd);
783
784 void hk_cmd_bind_shaders(struct vk_command_buffer *vk_cmd, uint32_t stage_count,
785 const mesa_shader_stage *stages,
786 struct vk_shader **const shaders);
787
788 void hk_cmd_bind_graphics_shader(struct hk_cmd_buffer *cmd,
789 const mesa_shader_stage stage,
790 struct hk_api_shader *shader);
791
792 void hk_cmd_bind_compute_shader(struct hk_cmd_buffer *cmd,
793 struct hk_api_shader *shader);
794
795 void hk_cmd_bind_vertex_buffer(struct hk_cmd_buffer *cmd, uint32_t vb_idx,
796 struct hk_addr_range addr_range);
797
798 static inline struct hk_descriptor_state *
799 hk_get_descriptors_state(struct hk_cmd_buffer *cmd,
800 VkPipelineBindPoint bind_point)
801 {
802 switch (bind_point) {
803 case VK_PIPELINE_BIND_POINT_GRAPHICS:
804 return &cmd->state.gfx.descriptors;
805 case VK_PIPELINE_BIND_POINT_COMPUTE:
806 return &cmd->state.cs.descriptors;
807 default:
808 UNREACHABLE("Unhandled bind point");
809 }
810 };
811
812 void hk_cmd_buffer_flush_push_descriptors(struct hk_cmd_buffer *cmd,
813 struct hk_descriptor_state *desc);
814
815 void hk_meta_resolve_rendering(struct hk_cmd_buffer *cmd,
816 const VkRenderingInfo *pRenderingInfo);
817
818 uint64_t hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd,
819 VkPipelineBindPoint bind_point);
820
821 void hk_reserve_scratch(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
822 struct hk_shader *s);
823
824 uint32_t hk_upload_usc_words(struct hk_cmd_buffer *cmd, struct hk_shader *s,
825 struct hk_linked_shader *linked);
826
827 void hk_usc_upload_spilled_rt_descs(struct agx_usc_builder *b,
828 struct hk_cmd_buffer *cmd);
829
830 void hk_cdm_cache_flush(struct hk_device *dev, struct hk_cs *cs);
831
832 void hk_dispatch_with_usc_launch(struct hk_device *dev, struct hk_cs *cs,
833 struct agx_cdm_launch_word_0_packed launch,
834 uint32_t usc, struct agx_grid grid,
835 struct agx_workgroup local_size);
836
837 void hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs,
838 struct agx_shader_info *info, uint32_t usc,
839 struct agx_grid grid,
840 struct agx_workgroup local_size);
841
842 static inline void
843 hk_dispatch_with_local_size(struct hk_cmd_buffer *cmd, struct hk_cs *cs,
844 struct hk_shader *s, struct agx_grid grid,
845 struct agx_workgroup local_size)
846 {
847 if (agx_is_shader_empty(&s->b))
848 return;
849
850 struct hk_device *dev = hk_cmd_buffer_device(cmd);
851 uint32_t usc = hk_upload_usc_words(cmd, s, s->only_linked);
852
853 hk_reserve_scratch(cmd, cs, s);
854 hk_dispatch_with_usc(dev, cs, &s->b.info, usc, grid, local_size);
855 }
856
857 void hk_dispatch_precomp(struct hk_cmd_buffer *cmd, struct agx_grid grid,
858 enum agx_barrier barrier, enum libagx_program idx,
859 void *data, size_t data_size);
860
861 #define MESA_DISPATCH_PRECOMP hk_dispatch_precomp
862
863 void hk_queue_write(struct hk_cmd_buffer *cmd, uint64_t address, uint32_t value,
864 bool after_gfx);
865
866 void agx_fill_velem_keys(const struct vk_vertex_input_state *vi,
867 uint64_t attribs_read, struct agx_velem_key *keys);
868
869 struct agx_robustness hk_prolog_robustness(struct hk_device *dev);