Debian Codegraph @

src/asahi/libagx/libagx_dgc.h

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #pragma once
7
8 #include "compiler/libcl/libcl.h"
9 #include "compiler/shader_enums.h"
10 #include "agx_pack.h"
11
12 #define agx_push(ptr, T, cfg) \
13 for (unsigned _loop = 0; _loop < 1; \
14 ++_loop, ptr = (GLOBAL void *)(((uintptr_t)ptr) + AGX_##T##_LENGTH)) \
15 agx_pack(ptr, T, cfg)
16
17 #define agx_push_packed(ptr, src, T) \
18 static_assert(sizeof(src) == AGX_##T##_LENGTH); \
19 memcpy(ptr, &src, sizeof(src)); \
20 ptr = (GLOBAL void *)(((uintptr_t)ptr) + sizeof(src));
21
22 static inline enum agx_index_size
23 agx_translate_index_size(uint8_t size_B)
24 {
25 /* Index sizes are encoded logarithmically */
26 static_assert(__builtin_ctz(1) == AGX_INDEX_SIZE_U8);
27 static_assert(__builtin_ctz(2) == AGX_INDEX_SIZE_U16);
28 static_assert(__builtin_ctz(4) == AGX_INDEX_SIZE_U32);
29
30 assert((size_B == 1) || (size_B == 2) || (size_B == 4));
31 return __builtin_ctz(size_B);
32 }
33
34 static inline unsigned
35 agx_indices_to_B(unsigned x, enum agx_index_size size)
36 {
37 return x << size;
38 }
39
40 static inline uint8_t
41 agx_index_size_to_B(enum agx_index_size size)
42 {
43 return agx_indices_to_B(1, size);
44 }
45
46 struct agx_workgroup {
47 uint32_t x, y, z;
48 };
49
50 static inline struct agx_workgroup
51 agx_workgroup(uint32_t x, uint32_t y, uint32_t z)
52 {
53 return (struct agx_workgroup){.x = x, .y = y, .z = z};
54 }
55
56 static inline unsigned
57 agx_workgroup_threads(struct agx_workgroup wg)
58 {
59 return wg.x * wg.y * wg.z;
60 }
61
62 struct agx_grid {
63 enum agx_cdm_mode mode;
64 union {
65 uint32_t count[3];
66 uint64_t ptr;
67 };
68 };
69
70 static struct agx_grid
71 agx_3d(uint32_t x, uint32_t y, uint32_t z)
72 {
73 return (struct agx_grid){.mode = AGX_CDM_MODE_DIRECT, .count = {x, y, z}};
74 }
75
76 static struct agx_grid
77 agx_1d(uint32_t x)
78 {
79 return agx_3d(x, 1, 1);
80 }
81
82 static struct agx_grid
83 agx_2d(uint32_t x, uint32_t y)
84 {
85 return agx_3d(x, y, 1);
86 }
87
88 static struct agx_grid
89 agx_grid_indirect(uint64_t ptr)
90 {
91 return (struct agx_grid){.mode = AGX_CDM_MODE_INDIRECT_GLOBAL, .ptr = ptr};
92 }
93
94 static struct agx_grid
95 agx_grid_indirect_local(uint64_t ptr)
96 {
97 return (struct agx_grid){.mode = AGX_CDM_MODE_INDIRECT_LOCAL, .ptr = ptr};
98 }
99
100 static inline bool
101 agx_is_indirect(struct agx_grid grid)
102 {
103 return grid.mode != AGX_CDM_MODE_DIRECT;
104 }
105
106 enum agx_barrier {
107 /* No barrier/cache operations needed */
108 AGX_BARRIER_NONE = 0,
109
110 /* Catch-all for all defined barriers. Because we have not yet
111 * reverse-engineered the finer details here, this is the only barrier we
112 * have....
113 */
114 AGX_BARRIER_ALL = (1 << 0),
115
116 /* Specifies ordering requirements for the compute job to happen before/after
117 * the concurrent enqueued graphics on a graphical command buffer.
118 */
119 AGX_PREGFX = (1 << 1),
120 AGX_POSTGFX = (1 << 2),
121 };
122
123 struct agx_draw {
124 struct agx_grid b;
125 uint64_t index_buffer;
126 uint32_t index_buffer_range_B;
127 uint32_t start;
128 uint32_t index_bias;
129 uint32_t start_instance;
130
131 /* Primitive restart enabled. If true, implies indexed */
132 bool restart;
133 enum agx_index_size index_size;
134
135 /* TODO: Optimize this boolean. We can't just check if index_buffer != 0
136 * because that breaks with null index buffers.
137 */
138 bool indexed;
139 };
140
141 static inline struct agx_draw
142 agx_draw_indirect(uint64_t ptr)
143 {
144 return (struct agx_draw){.b = agx_grid_indirect(ptr)};
145 }
146
147 static inline struct agx_draw
148 agx_draw_indexed(uint32_t index_count, uint32_t instance_count,
149 uint32_t first_index, uint32_t index_bias,
150 uint32_t first_instance, uint64_t buf, uint32_t range_B,
151 enum agx_index_size index_size, bool restart)
152 {
153 return (struct agx_draw){
154 .b = agx_3d(index_count, instance_count, 1),
155 .index_buffer = buf,
156 .index_buffer_range_B = range_B,
157 .start = first_index,
158 .index_bias = index_bias,
159 .start_instance = first_instance,
160 .index_size = index_size,
161 .restart = restart,
162 .indexed = true,
163 };
164 }
165
166 static inline struct agx_draw
167 agx_draw_indexed_indirect(uint64_t ptr, uint64_t buf, uint32_t range_B,
168 enum agx_index_size index_size, bool restart)
169 {
170 return (struct agx_draw){
171 .b = agx_grid_indirect(ptr),
172 .index_buffer = buf,
173 .index_buffer_range_B = range_B,
174 .index_size = index_size,
175 .restart = restart,
176 .indexed = true,
177 };
178 }
179
180 static inline unsigned
181 agx_draw_index_range_B(struct agx_draw d)
182 {
183 uint range_B = d.index_buffer_range_B;
184 if (!agx_is_indirect(d.b))
185 range_B -= agx_indices_to_B(d.start, d.index_size);
186
187 return range_B;
188 }
189
190 static inline unsigned
191 agx_draw_index_range_el(struct agx_draw d)
192 {
193 assert(d.indexed);
194 return agx_draw_index_range_B(d) >> d.index_size;
195 }
196
197 static inline uint64_t
198 agx_draw_index_buffer(struct agx_draw d)
199 {
200 assert(d.indexed);
201
202 uint64_t ib = d.index_buffer;
203 if (!agx_is_indirect(d.b))
204 ib += agx_indices_to_B(d.start, d.index_size);
205
206 return ib;
207 }
208
209 static bool
210 agx_direct_draw_overreads_indices(struct agx_draw d)
211 {
212 uint32_t range_B = agx_indices_to_B(d.start + d.b.count[0], d.index_size);
213 return range_B > d.index_buffer_range_B;
214 }
215
216 enum agx_chip {
217 AGX_CHIP_G13G,
218 AGX_CHIP_G13X,
219 AGX_CHIP_G14G,
220 AGX_CHIP_G14X,
221 };
222
223 static inline GLOBAL uint32_t *
224 agx_cdm_launch(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_grid grid,
225 struct agx_workgroup wg,
226 struct agx_cdm_launch_word_0_packed launch, uint32_t usc)
227 {
228 #ifndef __OPENCL_VERSION__
229 struct agx_cdm_launch_word_0_packed mode;
230 agx_pack(&mode, CDM_LAUNCH_WORD_0, cfg) {
231 cfg.mode = grid.mode;
232 }
233
234 agx_merge(launch, mode, CDM_LAUNCH_WORD_0);
235 #endif
236
237 agx_push_packed(out, launch, CDM_LAUNCH_WORD_0);
238
239 agx_push(out, CDM_LAUNCH_WORD_1, cfg) {
240 cfg.pipeline = usc;
241 }
242
243 if (chip == AGX_CHIP_G14X) {
244 agx_push(out, CDM_UNK_G14X, cfg)
245 ;
246 }
247
248 if (agx_is_indirect(grid)) {
249 agx_push(out, CDM_INDIRECT, cfg) {
250 cfg.address_hi = grid.ptr >> 32;
251 cfg.address_lo = grid.ptr;
252 }
253 } else {
254 agx_push(out, CDM_GLOBAL_SIZE, cfg) {
255 cfg.x = grid.count[0];
256 cfg.y = grid.count[1];
257 cfg.z = grid.count[2];
258 }
259 }
260
261 if (grid.mode != AGX_CDM_MODE_INDIRECT_LOCAL) {
262 agx_push(out, CDM_LOCAL_SIZE, cfg) {
263 cfg.x = wg.x;
264 cfg.y = wg.y;
265 cfg.z = wg.z;
266 }
267 }
268
269 return out;
270 }
271
272 static inline GLOBAL uint32_t *
273 agx_vdm_draw(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_draw draw,
274 enum agx_primitive topology)
275 {
276 uint64_t ib = draw.indexed ? agx_draw_index_buffer(draw) : 0;
277
278 agx_push(out, INDEX_LIST, cfg) {
279 cfg.primitive = topology;
280
281 if (agx_is_indirect(draw.b)) {
282 cfg.indirect_buffer_present = true;
283 } else {
284 cfg.instance_count_present = true;
285 cfg.index_count_present = true;
286 cfg.start_present = true;
287 }
288
289 if (draw.indexed) {
290 cfg.restart_enable = draw.restart;
291 cfg.index_buffer_hi = ib >> 32;
292 cfg.index_size = draw.index_size;
293
294 cfg.index_buffer_present = true;
295 cfg.index_buffer_size_present = true;
296 }
297 }
298
299 if (draw.indexed) {
300 agx_push(out, INDEX_LIST_BUFFER_LO, cfg) {
301 cfg.buffer_lo = ib;
302 }
303 }
304
305 if (agx_is_indirect(draw.b)) {
306 agx_push(out, INDEX_LIST_INDIRECT_BUFFER, cfg) {
307 cfg.address_hi = draw.b.ptr >> 32;
308 cfg.address_lo = draw.b.ptr & BITFIELD_MASK(32);
309 }
310 } else {
311 agx_push(out, INDEX_LIST_COUNT, cfg) {
312 cfg.count = draw.b.count[0];
313 }
314
315 agx_push(out, INDEX_LIST_INSTANCES, cfg) {
316 cfg.count = draw.b.count[1];
317 }
318
319 agx_push(out, INDEX_LIST_START, cfg) {
320 cfg.start = draw.indexed ? draw.index_bias : draw.start;
321 }
322 }
323
324 if (draw.indexed) {
325 agx_push(out, INDEX_LIST_BUFFER_SIZE, cfg) {
326 cfg.size = align(agx_draw_index_range_B(draw), 4);
327 }
328 }
329
330 return out;
331 }
332
333 static inline uint32_t
334 agx_vdm_draw_size(enum agx_chip chip, struct agx_draw draw)
335 {
336 uint32_t size = AGX_INDEX_LIST_LENGTH;
337
338 if (agx_is_indirect(draw.b)) {
339 size += AGX_INDEX_LIST_INDIRECT_BUFFER_LENGTH;
340 } else {
341 size += AGX_INDEX_LIST_COUNT_LENGTH;
342 size += AGX_INDEX_LIST_INSTANCES_LENGTH;
343 size += AGX_INDEX_LIST_START_LENGTH;
344 }
345
346 if (draw.indexed) {
347 size += AGX_INDEX_LIST_BUFFER_LO_LENGTH;
348 size += AGX_INDEX_LIST_BUFFER_SIZE_LENGTH;
349 }
350
351 return size;
352 }
353
354 static inline GLOBAL uint32_t *
355 agx_vdm_barrier(GLOBAL uint32_t *out, enum agx_chip chip)
356 {
357 agx_push(out, VDM_BARRIER, cfg) {
358 cfg.unk_4 = (chip == AGX_CHIP_G13X);
359 cfg.unk_5 = true;
360 cfg.unk_6 = true;
361 cfg.unk_8 = true;
362 cfg.unk_11 = true;
363 cfg.unk_20 = true;
364 cfg.unk_24 = (chip == AGX_CHIP_G13X) || (chip == AGX_CHIP_G14X);
365 cfg.unk_26 = (chip == AGX_CHIP_G13X);
366 }
367
368 return out;
369 }
370
371 static inline GLOBAL uint32_t *
372 agx_cdm_barrier(GLOBAL uint32_t *out, enum agx_chip chip)
373 {
374 agx_push(out, CDM_BARRIER, cfg) {
375 cfg.unk_5 = true;
376 cfg.unk_6 = true;
377 cfg.unk_8 = true;
378 // cfg.unk_11 = true;
379 // cfg.unk_20 = true;
380 // cfg.unk_24 = true; if clustered?
381 if (chip == AGX_CHIP_G13X) {
382 cfg.unk_4 = true;
383 // cfg.unk_26 = true;
384 }
385
386 /* With multiple launches in the same CDM stream, we can get cache
387 * coherency (? or sync?) issues. We hit this with blits, which need - in
388 * between dispatches - need the PBE cache to be flushed and the texture
389 * cache to be invalidated. Until we know what bits mean what exactly,
390 * let's just set these after every launch to be safe. We can revisit in
391 * the future when we figure out what the bits mean.
392 */
393 cfg.unk_0 = true;
394 cfg.unk_1 = true;
395 cfg.unk_2 = true;
396 cfg.usc_cache_inval = true;
397 cfg.unk_4 = true;
398 cfg.unk_5 = true;
399 cfg.unk_6 = true;
400 cfg.unk_7 = true;
401 cfg.unk_8 = true;
402 cfg.unk_9 = true;
403 cfg.unk_10 = true;
404 cfg.unk_11 = true;
405 cfg.unk_12 = true;
406 cfg.unk_13 = true;
407 cfg.unk_14 = true;
408 cfg.unk_15 = true;
409 cfg.unk_16 = true;
410 cfg.unk_17 = true;
411 cfg.unk_18 = true;
412 cfg.unk_19 = true;
413 }
414
415 return out;
416 }
417
418 static inline GLOBAL uint32_t *
419 agx_vdm_return(GLOBAL uint32_t *out)
420 {
421 agx_push(out, VDM_BARRIER, cfg) {
422 cfg.returns = true;
423 }
424
425 return out;
426 }
427
428 static inline GLOBAL uint32_t *
429 agx_cdm_return(GLOBAL uint32_t *out)
430 {
431 agx_push(out, CDM_STREAM_RETURN, cfg)
432 ;
433
434 return out;
435 }
436
437 static inline GLOBAL uint32_t *
438 agx_cdm_terminate(GLOBAL uint32_t *out)
439 {
440 agx_push(out, CDM_STREAM_TERMINATE, _)
441 ;
442
443 return out;
444 }
445
446 static inline GLOBAL uint32_t *
447 agx_vdm_terminate(GLOBAL uint32_t *out)
448 {
449 agx_push(out, VDM_STREAM_TERMINATE, _)
450 ;
451
452 return out;
453 }
454
455 static inline GLOBAL uint32_t *
456 agx_cdm_jump(GLOBAL uint32_t *out, uint64_t target)
457 {
458 agx_push(out, CDM_STREAM_LINK, cfg) {
459 cfg.target_lo = target & BITFIELD_MASK(32);
460 cfg.target_hi = target >> 32;
461 }
462
463 return out;
464 }
465
466 static inline GLOBAL uint32_t *
467 agx_vdm_jump(GLOBAL uint32_t *out, uint64_t target)
468 {
469 agx_push(out, VDM_STREAM_LINK, cfg) {
470 cfg.target_lo = target & BITFIELD_MASK(32);
471 cfg.target_hi = target >> 32;
472 }
473
474 return out;
475 }
476
477 static inline GLOBAL uint32_t *
478 agx_cs_jump(GLOBAL uint32_t *out, uint64_t target, bool vdm)
479 {
480 return vdm ? agx_vdm_jump(out, target) : agx_cdm_jump(out, target);
481 }
482
483 static inline GLOBAL uint32_t *
484 agx_cdm_call(GLOBAL uint32_t *out, uint64_t target)
485 {
486 agx_push(out, CDM_STREAM_LINK, cfg) {
487 cfg.target_lo = target & BITFIELD_MASK(32);
488 cfg.target_hi = target >> 32;
489 cfg.with_return = true;
490 }
491
492 return out;
493 }
494
495 static inline GLOBAL uint32_t *
496 agx_vdm_call(GLOBAL uint32_t *out, uint64_t target)
497 {
498 agx_push(out, VDM_STREAM_LINK, cfg) {
499 cfg.target_lo = target & BITFIELD_MASK(32);
500 cfg.target_hi = target >> 32;
501 cfg.with_return = true;
502 }
503
504 return out;
505 }
506
507 #define AGX_MAX_LINKED_USC_SIZE \
508 (AGX_USC_PRESHADER_LENGTH + AGX_USC_FRAGMENT_PROPERTIES_LENGTH + \
509 AGX_USC_REGISTERS_LENGTH + AGX_USC_SHADER_LENGTH + AGX_USC_SHARED_LENGTH + \
510 AGX_USC_SAMPLER_LENGTH + (AGX_USC_UNIFORM_LENGTH * 9))
511
512 /*
513 * This data structure contains everything needed to dispatch a compute shader
514 * (and hopefully eventually graphics?).
515 *
516 * It is purely flat, no CPU pointers. That makes it suitable for sharing
517 * between CPU and GPU. The intention is that it is packed on the CPU side and
518 * then consumed on either host or device for dispatching work.
519 */
520 struct agx_shader {
521 struct agx_cdm_launch_word_0_packed launch;
522 struct agx_workgroup workgroup;
523
524 struct {
525 uint32_t size;
526 uint8_t data[AGX_MAX_LINKED_USC_SIZE];
527 } usc;
528 };
529
530 /* Opaque structure representing a USC program being constructed */
531 struct agx_usc_builder {
532 GLOBAL uint8_t *head;
533
534 #ifndef NDEBUG
535 uint8_t *begin;
536 size_t size;
537 #endif
538 } PACKED;
539
540 static struct agx_usc_builder
541 agx_usc_builder(GLOBAL void *out, ASSERTED size_t size)
542 {
543 return (struct agx_usc_builder){
544 .head = out,
545
546 #ifndef NDEBUG
547 .begin = out,
548 .size = size,
549 #endif
550 };
551 }
552
553 static bool
554 agx_usc_builder_validate(struct agx_usc_builder *b, size_t size)
555 {
556 #ifndef NDEBUG
557 assert(((b->head - b->begin) + size) <= b->size);
558 #endif
559
560 return true;
561 }
562
563 #define agx_usc_pack(b, struct_name, template) \
564 for (bool it = \
565 agx_usc_builder_validate((b), AGX_USC_##struct_name##_LENGTH); \
566 it; it = false, (b)->head += AGX_USC_##struct_name##_LENGTH) \
567 agx_pack((b)->head, USC_##struct_name, template)
568
569 #define agx_usc_push_blob(b, blob, length) \
570 for (bool it = agx_usc_builder_validate((b), length); it; \
571 it = false, (b)->head += length) \
572 memcpy((b)->head, blob, length);
573
574 #define agx_usc_push_packed(b, struct_name, packed) \
575 agx_usc_push_blob(b, packed.opaque, AGX_USC_##struct_name##_LENGTH);
576
577 static void
578 agx_usc_uniform(struct agx_usc_builder *b, unsigned start_halfs,
579 unsigned size_halfs, uint64_t buffer)
580 {
581 assert((start_halfs + size_halfs) <= (1 << 9) && "uniform file overflow");
582 assert(size_halfs <= 64 && "caller's responsibility to split");
583 assert(size_halfs > 0 && "no empty uniforms");
584
585 if (start_halfs & BITFIELD_BIT(8)) {
586 agx_usc_pack(b, UNIFORM_HIGH, cfg) {
587 cfg.start_halfs = start_halfs & BITFIELD_MASK(8);
588 cfg.size_halfs = size_halfs;
589 cfg.buffer = buffer;
590 }
591 } else {
592 agx_usc_pack(b, UNIFORM, cfg) {
593 cfg.start_halfs = start_halfs;
594 cfg.size_halfs = size_halfs;
595 cfg.buffer = buffer;
596 }
597 }
598 }
599
600 static inline void
601 agx_usc_words_precomp(GLOBAL uint32_t *out, CONST struct agx_shader *s,
602 uint64_t data, unsigned data_size)
603 {
604 /* Map the data directly as uniforms starting at u0 */
605 struct agx_usc_builder b = agx_usc_builder(out, sizeof(s->usc.data));
606 agx_usc_uniform(&b, 0, DIV_ROUND_UP(data_size, 2), data);
607 agx_usc_push_blob(&b, s->usc.data, s->usc.size);
608 }
609
610 /* This prototype is sufficient for sizing the output */
611 static inline unsigned
612 libagx_draw_robust_index_vdm_size()
613 {
614 struct agx_draw draw = agx_draw_indexed(0, 0, 0, 0, 0, 0, 0, 0, 0);
615 return agx_vdm_draw_size(0, draw);
616 }
617
618 static inline unsigned
619 libagx_remap_adj_count(unsigned count, enum mesa_prim prim)
620 {
621 if (prim == MESA_PRIM_TRIANGLE_STRIP_ADJACENCY) {
622 /* Spec gives formula for # of primitives in a tri strip adj */
623 unsigned c4 = count >= 4 ? count - 4 : 0;
624 return 3 * (c4 / 2);
625 } else if (prim == MESA_PRIM_LINE_STRIP_ADJACENCY) {
626 return 2 * (count >= 3 ? count - 3 : 0);
627 } else {
628 /* Adjacency lists just drop half the vertices. */
629 return count / 2;
630 }
631 }