Debian Codegraph @

src/asahi/compiler/agx_compile.h

raw
1 /*
2 * Copyright 2018-2021 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #pragma once
7
8 #include "compiler/nir/nir.h"
9 #include "util/shader_stats.h"
10 #include "util/u_dynarray.h"
11 #include "util/u_tristate.h"
12 #include "shader_enums.h"
13
14 struct agx_cf_binding {
15 /* Base coefficient register */
16 uint8_t cf_base;
17
18 /* Slot being bound */
19 gl_varying_slot slot : 8;
20
21 /* First component bound.
22 *
23 * Must be 2 (Z) or 3 (W) if slot == VARYING_SLOT_POS.
24 */
25 unsigned offset : 2;
26
27 /* Number of components bound */
28 unsigned count : 3;
29
30 /* Is smooth shading enabled? If false, flat shading is used */
31 bool smooth : 1;
32
33 /* Perspective correct interpolation */
34 bool perspective : 1;
35
36 uint8_t pad;
37 };
38
39 /* Conservative bound, * 4 due to offsets (TODO: maybe worth eliminating
40 * coefficient register aliasing?)
41 */
42 #define AGX_MAX_CF_BINDINGS (VARYING_SLOT_MAX * 4)
43
44 struct agx_varyings_fs {
45 /* Number of coefficient registers used */
46 unsigned nr_cf;
47
48 /* Number of coefficient register bindings */
49 unsigned nr_bindings;
50
51 /* Whether gl_FragCoord.z is read */
52 bool reads_z;
53
54 /* Coefficient register bindings */
55 struct agx_cf_binding bindings[AGX_MAX_CF_BINDINGS];
56 };
57
58 union agx_varyings {
59 struct agx_varyings_fs fs;
60 };
61
62 struct agx_interp_info {
63 /* Bit masks indexed by I/O location of flat and linear varyings */
64 uint64_t flat;
65 uint64_t linear;
66 };
67 static_assert(sizeof(struct agx_interp_info) == 16, "packed");
68
69 struct agx_rodata {
70 /* Offset in the binary */
71 uint32_t offset;
72
73 /* Base uniform to map constants */
74 uint16_t base_uniform;
75
76 /* Number of 16-bit constants to map contiguously there */
77 uint16_t size_16;
78 };
79
80 struct agx_shader_info {
81 mesa_shader_stage stage;
82 uint32_t binary_size;
83
84 union agx_varyings varyings;
85
86 /* Number of uniforms */
87 uint16_t push_count;
88
89 /* Local memory allocation in bytes */
90 uint16_t local_size;
91
92 /* Local imageblock allocation in bytes per thread */
93 uint16_t imageblock_stride;
94
95 /* Scratch memory allocation in bytes for main/preamble respectively */
96 unsigned scratch_size, preamble_scratch_size;
97
98 /* Size in bytes of the main sahder */
99 unsigned main_size;
100
101 /* Does the shader have a preamble? If so, it is at offset preamble_offset.
102 * The main shader is at offset main_offset. The preamble is executed first.
103 */
104 bool has_preamble;
105 unsigned preamble_offset, main_offset;
106
107 /* Does the shader read the tilebuffer? */
108 bool reads_tib;
109
110 /* Does the shader require early fragment tests? */
111 bool early_fragment_tests;
112
113 /* Does the shader potentially draw to a nonzero viewport? */
114 bool nonzero_viewport;
115
116 /* Does the shader write layer and/or viewport index? Written together */
117 bool writes_layer_viewport;
118
119 /* Does the shader control the sample mask? */
120 bool writes_sample_mask;
121
122 /* Depth layout, never equal to NONE */
123 enum gl_frag_depth_layout depth_layout;
124
125 /* Based only the compiled shader, should tag writes be disabled? This is set
126 * based on what is outputted. Note if rasterizer discard is used, that needs
127 * to disable tag writes regardless of this flag.
128 */
129 bool tag_write_disable;
130
131 /* Shader is incompatible with triangle merging */
132 bool disable_tri_merging;
133
134 /* Reads draw ID system value */
135 bool uses_draw_id;
136
137 /* Reads base vertex/instance */
138 bool uses_base_param;
139
140 /* Uses txf and hence needs a txf sampler mapped */
141 bool uses_txf;
142
143 /* Potentially uses the sampler heap (conservative) */
144 bool uses_sampler_heap;
145
146 /* Number of texture/sampler state registers pushed by the preamble. */
147 uint8_t texture_state_count, sampler_state_count;
148
149 /* Number of 16-bit registers used by the main shader and preamble
150 * respectively.
151 */
152 uint16_t nr_gprs, nr_preamble_gprs;
153
154 /* Output mask set during driver lowering */
155 uint64_t outputs;
156
157 /* Workgroup size */
158 uint16_t workgroup_size[3];
159
160 /* There may be constants in the binary. The driver must map these to uniform
161 * registers as specified hre.
162 */
163 struct agx_rodata rodata;
164
165 struct agx2_stats stats;
166 };
167
168 struct agx_precompiled_kernel_info {
169 uint32_t preamble_offset, main_offset;
170 uint32_t main_size, binary_size;
171 struct agx_rodata rodata;
172 uint16_t nr_gprs, nr_preamble_gprs;
173 uint16_t push_count;
174 uint16_t workgroup_size[3];
175 uint16_t local_size;
176 uint16_t imageblock_stride;
177 bool uses_txf;
178 };
179
180 static inline struct agx_precompiled_kernel_info
181 agx_compact_kernel_info(struct agx_shader_info *info)
182 {
183 assert(info->has_preamble == (info->nr_preamble_gprs > 0));
184 assert(info->texture_state_count <= 8 && "static maximum, no need to plumb");
185
186 return (struct agx_precompiled_kernel_info){
187 .preamble_offset = info->preamble_offset,
188 .main_offset = info->main_offset,
189 .main_size = info->main_size,
190 .binary_size = info->binary_size,
191 .rodata = info->rodata,
192 .nr_gprs = info->nr_gprs,
193 .nr_preamble_gprs = info->nr_preamble_gprs,
194 .push_count = info->push_count,
195 .workgroup_size = {info->workgroup_size[0], info->workgroup_size[1],
196 info->workgroup_size[2]},
197 .local_size = info->local_size,
198 .imageblock_stride = info->imageblock_stride,
199 .uses_txf = info->uses_txf,
200 };
201 }
202
203 struct agx_shader_part {
204 struct agx_shader_info info;
205 void *binary;
206 };
207
208 static inline bool
209 agx_is_shader_empty(struct agx_shader_part *s)
210 {
211 /* Last instruction is a stop, so if there's one instruction, there is
212 * nothing but a stop. The shader is thus empty.
213 */
214 return (s->info.stats.instrs == 1);
215 }
216
217 #define AGX_MAX_RTS (8)
218
219 enum agx_format {
220 AGX_FORMAT_I8 = 0,
221 AGX_FORMAT_I16 = 1,
222 AGX_FORMAT_I32 = 2,
223 AGX_FORMAT_F16 = 3,
224 AGX_FORMAT_U8NORM = 4,
225 AGX_FORMAT_S8NORM = 5,
226 AGX_FORMAT_U16NORM = 6,
227 AGX_FORMAT_S16NORM = 7,
228 AGX_FORMAT_RGB10A2 = 8,
229 AGX_FORMAT_SRGBA8 = 10,
230 AGX_FORMAT_RG11B10F = 12,
231 AGX_FORMAT_RGB9E5 = 13,
232
233 /* Keep last */
234 AGX_NUM_FORMATS,
235 };
236
237 struct agx_fs_shader_key {
238 /* Normally, access to the tilebuffer must be guarded by appropriate fencing
239 * instructions to ensure correct results in the presence of out-of-order
240 * hardware optimizations. However, specially dispatched clear shaders are
241 * not subject to these conditions and can omit the wait instructions.
242 *
243 * Must (only) be set for special clear shaders.
244 *
245 * Must not be used with sample mask writes (including discards) or
246 * tilebuffer loads (including blending).
247 */
248 bool ignore_tib_dependencies;
249
250 /* When dynamic sample shading is used, the fragment shader is wrapped in a
251 * loop external to the API shader. This bit indicates that we are compiling
252 * inside the sample loop, meaning the execution nesting counter is already
253 * zero and must be preserved.
254 */
255 bool inside_sample_loop;
256
257 /* Base coefficient register. 0 for API shaders but nonzero for FS prolog */
258 uint8_t cf_base;
259 };
260
261 struct agx_device_key {
262 /* Does the target GPU need explicit cluster coherency for atomics?
263 * Only used on G13X.
264 */
265 enum u_tristate needs_g13x_coherency;
266
267 /* Is soft fault enabled? This is technically system-wide policy set by the
268 * kernel, but that's functionally a hardware feature.
269 */
270 bool soft_fault;
271 };
272
273 struct agx_shader_key {
274 /* Device info */
275 struct agx_device_key dev;
276
277 /* Number of reserved preamble slots at the start */
278 unsigned reserved_preamble;
279
280 /* Whether scratch memory is available in the given shader stage */
281 bool has_scratch;
282
283 /* Whether we're compiling the helper program used for scratch allocation.
284 * This has special register allocation requirements.
285 */
286 bool is_helper;
287
288 /* Whether the driver supports uploading constants for this shader. If
289 * false, constants will not be promoted to uniforms.
290 */
291 bool promote_constants;
292
293 /* Similarly whether the driver supports promoting bindless
294 * textures/samplers. Currently this works only if non-bindless
295 * textures/samplers are not used, but none of our drivers mix bindless /
296 * non-bindless usage.
297 */
298 bool promote_textures;
299
300 /* Set if this is a non-monolithic shader that must be linked with additional
301 * shader parts before the program can be used. This suppresses omission of
302 * `stop` instructions, which the linker must insert instead.
303 */
304 bool no_stop;
305
306 /* Set if this is a secondary shader part (prolog or epilog). This prevents
307 * the compiler from allocating uniform registers. For example, this turns
308 * off preambles.
309 */
310 bool secondary;
311
312 union {
313 struct agx_fs_shader_key fs;
314 };
315 };
316
317 uint64_t agx_gather_texcoords(nir_shader *nir);
318
319 void agx_preprocess_nir(nir_shader *nir);
320 bool agx_nir_lower_discard_zs_emit(nir_shader *s);
321 bool agx_nir_lower_sample_mask(nir_shader *s);
322 bool agx_nir_lower_interpolation(nir_shader *s);
323
324 bool agx_nir_lower_cull_distance_vs(struct nir_shader *s);
325 bool agx_nir_lower_cull_distance_fs(struct nir_shader *s,
326 unsigned nr_distances);
327 bool agx_mem_vectorize_cb(unsigned align_mul, unsigned align_offset,
328 unsigned bit_size, unsigned num_components,
329 int64_t hole_size, nir_intrinsic_instr *low,
330 nir_intrinsic_instr *high, void *data);
331
332 void agx_compile_shader_nir(nir_shader *nir, struct agx_shader_key *key,
333 struct agx_shader_part *out);
334
335 struct agx_occupancy {
336 unsigned max_registers;
337 unsigned max_threads;
338 };
339
340 struct agx_occupancy agx_occupancy_for_register_count(unsigned halfregs);
341 unsigned agx_max_registers_for_occupancy(unsigned occupancy);
342
343 static inline unsigned
344 agx_round_registers(unsigned halfregs)
345 {
346 return agx_occupancy_for_register_count(halfregs).max_registers;
347 }
348
349 static const nir_shader_compiler_options agx_nir_options = {
350 .lower_fdiv = true,
351 .fuse_ffma16 = true,
352 .fuse_ffma32 = true,
353 .lower_flrp16 = true,
354 .lower_flrp32 = true,
355 .lower_fpow = true,
356 .lower_fmod = true,
357 .lower_bitfield_extract8 = true,
358 .lower_bitfield_extract16 = true,
359 .lower_bitfield_insert = true,
360 .lower_ifind_msb = true,
361 .lower_find_lsb = true,
362 .lower_uadd_carry = true,
363 .lower_usub_borrow = true,
364 .lower_fisnormal = true,
365 .lower_scmp = true,
366 .lower_isign = true,
367 .lower_fsign = true,
368 .lower_iabs = true,
369 .lower_fminmax_signed_zero = true,
370 .lower_fdph = true,
371 .lower_ffract = true,
372 .lower_pack_half_2x16 = true,
373 .lower_pack_unorm_2x16 = true,
374 .lower_pack_snorm_2x16 = true,
375 .lower_pack_unorm_4x8 = true,
376 .lower_pack_snorm_4x8 = true,
377 .lower_pack_64_2x32 = true,
378 .lower_unpack_half_2x16 = true,
379 .lower_unpack_unorm_2x16 = true,
380 .lower_unpack_snorm_2x16 = true,
381 .lower_unpack_unorm_4x8 = true,
382 .lower_unpack_snorm_4x8 = true,
383 .lower_extract_byte = true,
384 .lower_insert_byte = true,
385 .lower_insert_word = true,
386 .has_cs_global_id = true,
387 .lower_device_index_to_zero = true,
388 .lower_hadd = true,
389 .has_amul = true,
390 .has_isub = true,
391 .has_load_global_bounded = true,
392 .support_16bit_alu = true,
393 .max_unroll_iterations = 32,
394 .lower_uniforms_to_ubo = true,
395 .late_lower_int64 = true,
396 .lower_int64_options =
397 (nir_lower_int64_options) ~(nir_lower_iadd64 | nir_lower_imul_2x32_64),
398 .lower_doubles_options = (nir_lower_doubles_options)(~0),
399 .support_indirect_inputs = BITFIELD_BIT(MESA_SHADER_TESS_CTRL) |
400 BITFIELD_BIT(MESA_SHADER_TESS_EVAL) |
401 BITFIELD_BIT(MESA_SHADER_FRAGMENT),
402 .support_indirect_outputs = (uint8_t)BITFIELD_MASK(MESA_SHADER_STAGES),
403 .max_samples = 4,
404 .lower_fquantize2f16 = true,
405 .compact_arrays = true,
406 .discard_is_demote = true,
407 .scalarize_ddx = true,
408 .io_options = nir_io_always_interpolate_convergent_fs_inputs,
409 };