Debian Codegraph @

src/asahi/lib/agx_helpers.h

raw
1 /*
2 * Copyright 2023 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #pragma once
7
8 #include <stdbool.h>
9 #include "asahi/compiler/agx_compile.h"
10 #include "asahi/layout/layout.h"
11 #include "agx_abi.h"
12 #include "agx_pack.h"
13 #include "agx_ppp.h"
14 #include "libagx_shaders.h"
15
16 #define AGX_MAX_OCCLUSION_QUERIES (32768)
17 #define AGX_MAX_VIEWPORTS (16)
18
19 static inline enum agx_sampler_states
20 agx_translate_sampler_state_count(unsigned count, bool extended)
21 {
22 assert(count <= 17 && "max 17 sampler state registers supported");
23
24 if (count == 0) {
25 return AGX_SAMPLER_STATES_0;
26 } else if (extended) {
27 if (count <= 8)
28 return AGX_SAMPLER_STATES_8_EXTENDED;
29 else
30 return AGX_SAMPLER_STATES_16_EXTENDED;
31 } else {
32 if (count <= 4)
33 return AGX_SAMPLER_STATES_4_COMPACT;
34 else if (count <= 8)
35 return AGX_SAMPLER_STATES_8_COMPACT;
36 else if (count <= 12)
37 return AGX_SAMPLER_STATES_12_COMPACT;
38 else
39 return AGX_SAMPLER_STATES_16_COMPACT;
40 }
41 }
42
43 static void
44 agx_pack_txf_sampler(struct agx_sampler_packed *out)
45 {
46 agx_pack(out, SAMPLER, cfg) {
47 /* Allow mipmapping. This is respected by txf, weirdly. */
48 cfg.minimum_lod = 0.0;
49 cfg.maximum_lod = INFINITY;
50 cfg.mip_filter = AGX_MIP_FILTER_NEAREST;
51
52 /* Out-of-bounds reads must return 0 */
53 cfg.wrap_s = AGX_WRAP_CLAMP_TO_BORDER;
54 cfg.wrap_t = AGX_WRAP_CLAMP_TO_BORDER;
55 cfg.wrap_r = AGX_WRAP_CLAMP_TO_BORDER;
56 cfg.border_colour = AGX_BORDER_COLOUR_TRANSPARENT_BLACK;
57 }
58 }
59
60 /* Channels agree for RGBA but are weird for force 0/1 */
61
62 static inline enum agx_channel
63 agx_channel_from_pipe(enum pipe_swizzle in)
64 {
65 STATIC_ASSERT((enum agx_channel)PIPE_SWIZZLE_X == AGX_CHANNEL_R);
66 STATIC_ASSERT((enum agx_channel)PIPE_SWIZZLE_Y == AGX_CHANNEL_G);
67 STATIC_ASSERT((enum agx_channel)PIPE_SWIZZLE_Z == AGX_CHANNEL_B);
68 STATIC_ASSERT((enum agx_channel)PIPE_SWIZZLE_W == AGX_CHANNEL_A);
69 STATIC_ASSERT((enum agx_channel)PIPE_SWIZZLE_0 & 0x4);
70 STATIC_ASSERT((enum agx_channel)PIPE_SWIZZLE_1 & 0x4);
71 STATIC_ASSERT((enum agx_channel)PIPE_SWIZZLE_NONE & 0x4);
72
73 if ((in & 0x4) == 0)
74 return (enum agx_channel)in;
75 else if (in == PIPE_SWIZZLE_1)
76 return AGX_CHANNEL_1;
77 else
78 return AGX_CHANNEL_0;
79 }
80
81 static inline enum agx_layout
82 agx_translate_layout(enum ail_tiling tiling)
83 {
84 switch (tiling) {
85 case AIL_TILING_GPU:
86 return AGX_LAYOUT_GPU;
87 case AIL_TILING_TWIDDLED:
88 return AGX_LAYOUT_TWIDDLED;
89 case AIL_TILING_LINEAR:
90 return AGX_LAYOUT_LINEAR;
91 }
92
93 UNREACHABLE("Invalid tiling");
94 }
95
96 static inline enum agx_zls_tiling
97 agx_translate_zls_tiling(enum ail_tiling tiling)
98 {
99 switch (tiling) {
100 case AIL_TILING_GPU:
101 return AGX_ZLS_TILING_GPU;
102 case AIL_TILING_TWIDDLED:
103 return AGX_ZLS_TILING_TWIDDLED;
104 default:
105 UNREACHABLE("Invalid ZLS tiling");
106 }
107 }
108
109 struct agx_zls {
110 bool z_load, z_store;
111 bool s_load, s_store;
112 };
113
114 static inline void
115 agx_pack_zls_control(struct agx_zls_control_packed *packed,
116 const struct ail_layout *z, const struct ail_layout *s,
117 struct agx_zls *args)
118 {
119 agx_pack(packed, ZLS_CONTROL, cfg) {
120 if (z) {
121 cfg.z_store = args->z_store;
122 cfg.z_load = args->z_load;
123 cfg.z_load_compress = cfg.z_store_compress = z->compressed;
124 cfg.z_load_tiling = cfg.z_store_tiling =
125 agx_translate_zls_tiling(z->tiling);
126
127 if (z->format == PIPE_FORMAT_Z16_UNORM) {
128 cfg.z_format = AGX_ZLS_FORMAT_16;
129 } else {
130 cfg.z_format = AGX_ZLS_FORMAT_32F;
131 }
132 }
133
134 if (s) {
135 cfg.s_load = args->s_load;
136 cfg.s_store = args->s_store;
137 cfg.s_load_compress = cfg.s_store_compress = s->compressed;
138 cfg.s_load_tiling = cfg.s_store_tiling =
139 agx_translate_zls_tiling(s->tiling);
140 }
141 }
142 }
143
144 static enum agx_sample_count
145 agx_translate_sample_count(unsigned samples)
146 {
147 switch (samples) {
148 case 2:
149 return AGX_SAMPLE_COUNT_2;
150 case 4:
151 return AGX_SAMPLE_COUNT_4;
152 default:
153 UNREACHABLE("Invalid sample count");
154 }
155 }
156
157 static enum agx_conservative_depth
158 agx_translate_depth_layout(enum gl_frag_depth_layout layout)
159 {
160 switch (layout) {
161 case FRAG_DEPTH_LAYOUT_ANY:
162 return AGX_CONSERVATIVE_DEPTH_ANY;
163 case FRAG_DEPTH_LAYOUT_LESS:
164 return AGX_CONSERVATIVE_DEPTH_LESS;
165 case FRAG_DEPTH_LAYOUT_GREATER:
166 return AGX_CONSERVATIVE_DEPTH_GREATER;
167 case FRAG_DEPTH_LAYOUT_UNCHANGED:
168 return AGX_CONSERVATIVE_DEPTH_UNCHANGED;
169 default:
170 UNREACHABLE("depth layout should have been canonicalized");
171 }
172 }
173
174 static void
175 agx_pack_fragment_face_2(struct agx_fragment_face_2_packed *out,
176 enum agx_object_type object_type,
177 struct agx_shader_info *info)
178 {
179 agx_pack(out, FRAGMENT_FACE_2, cfg) {
180 /* These act like disables, ANDed in the hardware. Setting them like this
181 * means the draw-time flag is used.
182 */
183 cfg.disable_depth_write = true;
184 cfg.depth_function = AGX_ZS_FUNC_ALWAYS;
185
186 cfg.object_type = object_type;
187 cfg.conservative_depth =
188 info ? agx_translate_depth_layout(info->depth_layout)
189 : AGX_CONSERVATIVE_DEPTH_UNCHANGED;
190 }
191 }
192
193 static void
194 agx_ppp_fragment_face_2(struct agx_ppp_update *ppp,
195 enum agx_object_type object_type,
196 struct agx_shader_info *info)
197 {
198 struct agx_fragment_face_2_packed packed;
199 agx_pack_fragment_face_2(&packed, object_type, info);
200 agx_ppp_push_packed(ppp, &packed, FRAGMENT_FACE_2);
201 }
202
203 static inline uint32_t
204 agx_pack_line_width(float line_width)
205 {
206 /* Line width is packed in a 4:4 fixed point format */
207 unsigned line_width_fixed = ((unsigned)(line_width * 16.0f)) - 1;
208
209 /* Clamp to maximum line width */
210 return MIN2(line_width_fixed, 0xFF);
211 }
212
213 /*
214 * Despite having both a layout *and* a flag that I only see Metal use with null
215 * textures, AGX doesn't seem to have "real" null textures. Instead we need to
216 * bind an arbitrary address and throw away the results to read all 0's.
217 * Accordingly, the caller must pass some address that lives at least as long as
218 * the texture descriptor itself.
219 */
220 static void
221 agx_set_null_texture(struct agx_texture_packed *tex)
222 {
223 agx_pack(tex, TEXTURE, cfg) {
224 cfg.layout = AGX_LAYOUT_TWIDDLED;
225 cfg.channels = AGX_CHANNELS_R8;
226 cfg.type = AGX_TEXTURE_TYPE_UNORM /* don't care */;
227 cfg.swizzle_r = AGX_CHANNEL_0;
228 cfg.swizzle_g = AGX_CHANNEL_0;
229 cfg.swizzle_b = AGX_CHANNEL_0;
230 cfg.swizzle_a = AGX_CHANNEL_0;
231 cfg.address = AGX_ZERO_PAGE_ADDRESS;
232 }
233 }
234
235 static void
236 agx_set_null_pbe(struct agx_pbe_packed *pbe)
237 {
238 agx_pack(pbe, PBE, cfg) {
239 cfg.width = 1;
240 cfg.height = 1;
241 cfg.levels = 1;
242 cfg.layout = AGX_LAYOUT_TWIDDLED;
243 cfg.channels = AGX_CHANNELS_R8;
244 cfg.type = AGX_TEXTURE_TYPE_UNORM /* don't care */;
245 cfg.swizzle_r = AGX_CHANNEL_R;
246 cfg.swizzle_g = AGX_CHANNEL_R;
247 cfg.swizzle_b = AGX_CHANNEL_R;
248 cfg.swizzle_a = AGX_CHANNEL_R;
249 cfg.buffer = AGX_SCRATCH_PAGE_ADDRESS;
250 }
251 }
252
253 /*
254 * Determine the maximum vertex/divided instance index. For robustness,
255 * the index will be clamped to this before reading (if soft fault is
256 * disabled).
257 *
258 * Index i accesses up to (exclusive) offset:
259 *
260 * src_offset + (i * stride) + elsize_B
261 *
262 * so we require
263 *
264 * src_offset + (i * stride) + elsize_B <= size
265 *
266 * <==>
267 *
268 * i <= floor((size - src_offset - elsize_B) / stride)
269 */
270 static inline uint32_t
271 agx_calculate_vbo_clamp(uint64_t vbuf, enum pipe_format format, uint32_t size_B,
272 uint32_t stride_B, uint32_t offset_B,
273 uint64_t *vbuf_out)
274 {
275 unsigned elsize_B = util_format_get_blocksize(format);
276 unsigned subtracted_B = offset_B + elsize_B;
277
278 /* If at least one index is valid, determine the max. Otherwise, direct reads
279 * to zero.
280 */
281 if (size_B >= subtracted_B) {
282 *vbuf_out = vbuf + offset_B;
283
284 /* If stride is zero, do not clamp, everything is valid. */
285 if (stride_B)
286 return ((size_B - subtracted_B) / stride_B);
287 else
288 return UINT32_MAX;
289 } else {
290 *vbuf_out = AGX_ZERO_PAGE_ADDRESS;
291 return 0;
292 }
293 }
294
295 static struct libagx_decompress_args
296 agx_fill_decompress_args(struct ail_layout *layout, unsigned layer,
297 unsigned level, uint64_t ptr, uint64_t images)
298 {
299 return (struct libagx_decompress_args){
300 .images = images,
301 .tile_uncompressed = ail_tile_mode_uncompressed(layout->format),
302 .metadata = ptr + layout->metadata_offset_B +
303 layout->level_offsets_compressed_B[level] +
304 (layer * layout->compression_layer_stride_B),
305 .metadata_layer_stride_tl = layout->compression_layer_stride_B / 8,
306 .metadata_width_tl = ail_metadata_width_tl(layout, level),
307 .metadata_height_tl = ail_metadata_height_tl(layout, level),
308 };
309 }
310
311 #undef libagx_decompress
312 #define libagx_decompress(context, grid, barrier, layout, layer, level, ptr, \
313 images) \
314 libagx_decompress_struct( \
315 context, grid, barrier, \
316 agx_fill_decompress_args(layout, layer, level, ptr, images), \
317 util_logbase2(layout->sample_count_sa))
318
319 struct agx_border_packed;
320
321 void agx_pack_border(struct agx_border_packed *out, const uint32_t in[4],
322 enum pipe_format format);