Debian Codegraph @

src/asahi/libagx/draws.cl

raw
1 /*
2 * Copyright 2024 Valve Corporation
3 * SPDX-License-Identifier: MIT
4 */
5 #include "asahi/lib/agx_abi.h"
6 #include "compiler/libcl/libcl_vk.h"
7 #include "poly/geometry.h"
8 #include "agx_pack.h"
9 #include "libagx_dgc.h"
10
11 /*
12 * To implement drawIndirectCount generically, we dispatch a kernel to
13 * clone-and-patch the indirect buffer, predicating out draws as appropriate.
14 */
15 KERNEL(32)
16 libagx_predicate_indirect(global uint32_t *out, constant uint32_t *in,
17 constant uint32_t *draw_count, uint32_t stride_el,
18 uint indexed__2)
19 {
20 uint draw = cl_global_id.x;
21 uint words = indexed__2 ? 5 : 4;
22 bool enabled = draw < *draw_count;
23 out += draw * words;
24 in += draw * stride_el;
25
26 /* Copy enabled draws, zero predicated draws. */
27 for (uint i = 0; i < words; ++i) {
28 out[i] = enabled ? in[i] : 0;
29 }
30 }
31
32 /*
33 * Indexing/offseting is in software if necessary so we strip all
34 * indexing/offset information.
35 */
36 KERNEL(1)
37 libagx_draw_without_adj(global VkDrawIndirectCommand *out,
38 global VkDrawIndirectCommand *in,
39 global struct poly_vertex_params *vp,
40 uint64_t index_buffer, uint64_t index_buffer_range_el,
41 int index_size_B, enum mesa_prim prim)
42 {
43 *out = (VkDrawIndirectCommand){
44 .vertexCount = libagx_remap_adj_count(in->vertexCount, prim),
45 .instanceCount = in->instanceCount,
46 };
47
48 /* TODO: Deduplicate */
49 if (index_size_B) {
50 uint offs = in->firstVertex;
51
52 vp->index_buffer = poly_index_buffer(index_buffer, index_buffer_range_el,
53 offs, index_size_B);
54
55 vp->index_buffer_range_el =
56 poly_index_buffer_range_el(index_buffer_range_el, offs);
57 }
58 }
59
60 /* Precondition: len must be < the group size */
61 static void
62 libagx_memcpy_small(global uchar *dst, constant uchar *src, uint len, uint tid)
63 {
64 if (tid < len) {
65 dst[tid] = src[tid];
66 }
67 }
68
69 static void
70 libagx_memcpy_aligned_uint4(global uint *dst, constant uint *src, uint len,
71 uint tid, uint group_size)
72 {
73 for (uint i = tid; i < len; i += group_size) {
74 vstore4(vload4(i, src), i, dst);
75 }
76 }
77
78 static void
79 libagx_memcpy_to_aligned(global uint *dst, constant uchar *src, uint len,
80 uint tid, uint group_size)
81 {
82 /* Copy a few bytes at the start */
83 uint start_unaligned = ((uintptr_t)src) & 3;
84 if (start_unaligned) {
85 uint need = 4 - start_unaligned;
86 libagx_memcpy_small((global uchar *)dst, src, need, tid);
87 src += need;
88 len -= need;
89 }
90
91 /* Copy a few bytes at the end */
92 uint end_unaligned = len & 0xf;
93 len -= end_unaligned;
94 libagx_memcpy_small(((global uchar *)dst) + len, src + len, end_unaligned,
95 tid);
96
97 /* Now both src and dst are word-aligned, and len is 16-aligned */
98 libagx_memcpy_aligned_uint4(dst, (constant uint *)src, len / 16, tid,
99 group_size);
100 }
101
102 /* Precondition: len must be < the group size */
103 static void
104 libagx_memset_small(global uchar *dst, uchar b, int len, uint tid)
105 {
106 if (tid < len) {
107 dst[tid] = b;
108 }
109 }
110
111 /*
112 * AGX does not implement robustBufferAccess2 semantics for
113 * index buffers, where out-of-bounds indices read as zero. When we
114 * dynamically detect index buffer overread (this if-statement), we need
115 * to clone the index buffer and zero-extend it to get robustness.
116 *
117 * We do this dynamically (generating a VDM draw to consume the result) to avoid
118 * expensive allocations & memcpys in the happy path where no out-of-bounds
119 * access occurs. Otherwise we could use a hardware indirect draw, rather than
120 * generating VDM words directly in shader.
121 *
122 * TODO: Handle multiple draws in parallel.
123 */
124 KERNEL(32)
125 libagx_draw_robust_index(global uint32_t *vdm, global struct poly_heap *heap,
126 constant VkDrawIndexedIndirectCommand *cmd,
127 uint64_t in_buf_ptr, uint32_t in_buf_range_B,
128 ushort restart, enum agx_primitive topology,
129 enum agx_index_size index_size__3)
130 {
131 uint tid = get_sub_group_local_id();
132 bool first = tid == 0;
133 enum agx_index_size index_size = index_size__3;
134
135 struct agx_draw draw = agx_draw_indexed(
136 cmd->indexCount, cmd->instanceCount, cmd->firstIndex, cmd->vertexOffset,
137 cmd->firstInstance, in_buf_ptr, in_buf_range_B, index_size, restart);
138
139 if (agx_indices_to_B(cmd->firstIndex, index_size) >= in_buf_range_B) {
140 /* If the entire draw is out-of-bounds, skip it. We handle this specially
141 * for both performance and to avoid integer wrapping issues in the main
142 * code path (where cmd->firstIndex could get treated as a negative).
143 */
144 draw.index_buffer = AGX_ZERO_PAGE_ADDRESS;
145 draw.index_buffer_range_B = 4;
146 draw.start = 0;
147 } else if (agx_direct_draw_overreads_indices(draw)) {
148 constant void *in_buf = (constant void *)agx_draw_index_buffer(draw);
149 uint in_size_el = agx_draw_index_range_el(draw);
150 uint in_size_B = agx_indices_to_B(in_size_el, index_size);
151
152 /* After a small number of zeroes at the end, extra zeroes cannot change
153 * rendering since they will duplicate the same degenerate primitive many
154 * times. Therefore we clamp the number of zeroes we need to extend with.
155 * This makes the memset constant time.
156 */
157 draw.b.count[0] = min(draw.b.count[0], in_size_el + 32);
158
159 uint out_size_el = draw.b.count[0];
160 uint out_size_B = agx_indices_to_B(out_size_el, index_size);
161
162 /* Allocate memory for the shadow index buffer */
163 global uchar *padded;
164 if (first) {
165 padded = poly_heap_alloc(heap, out_size_B);
166 }
167 padded = (global uchar *)sub_group_broadcast((uintptr_t)padded, 0);
168
169 draw.index_buffer = (uintptr_t)padded;
170 draw.index_buffer_range_B = out_size_B;
171 draw.start = 0;
172
173 /* Clone the index buffer. The destination is aligned as a post-condition
174 * of poly_heap_alloc_nonatomic.
175 */
176 libagx_memcpy_to_aligned((global uint *)padded, in_buf, in_size_B, tid,
177 32);
178
179 /* Extend with up to 32 zeroes with a small memset */
180 libagx_memset_small(padded + in_size_B, 0, out_size_B - in_size_B, tid);
181 }
182
183 if (first) {
184 agx_vdm_draw(vdm, 0, draw, topology);
185 }
186 }