From eff1994b730cdd63029f31f13cc49ebecf10233b Mon Sep 17 00:00:00 2001 From: kieroid Date: Thu, 3 Dec 2026 01:58:10 +0800 Subject: [PATCH] asahi: Target the A12 GPU (G11) Adapt the asahi compiler, command streams, layout, Gallium driver and Honeykrisp to Apple's A12 GPU (AGX G11P): the G11 stop instruction, threadgroup memory through the device load/store encoding, no SIMD reductions or G13 cache barriers, single-sampled tilebuffers, the G11 CDM block types and one-word USC shader/register/shared-memory words (cmdbuf.xml), a USC heap that keeps address 0 free, and the device name "Apple A12 (G11x)". AGX_G11 (genxml/agx_g11.h) selects G11 at build time. [Orchard: from rzxvx/Project-Cascadia@566808a devices/a12/mesa/mesa-0001-asahi-g11.patch (badapple 61bd066f), applied onto our fork's main; the only conflict, agx_open_device()'s USC heap hunk, keeps our separate capture heap (b467490b84d) after the G11 USC heap; every other hunk applies unchanged.] Assisted-by: LLM --- src/asahi/compiler/agx_compile.c | 357 +++++++- src/asahi/compiler/agx_compile.h | 20 +- src/asahi/compiler/agx_compiler.h | 4 +- src/asahi/compiler/agx_insert_waits.c | 5 + src/asahi/compiler/agx_lower_spill.c | 49 +- .../compiler/agx_lower_uniform_sources.c | 4 +- src/asahi/compiler/agx_nir.h | 1 + .../compiler/agx_nir_lower_discard_zs_emit.c | 4 + src/asahi/compiler/agx_nir_lower_texture.c | 262 +++++- src/asahi/compiler/agx_nir_opt_preamble.c | 2 +- src/asahi/compiler/agx_nir_texture.h | 6 + src/asahi/compiler/agx_opcodes.py | 2 +- .../compiler/agx_opt_compact_constants.c | 6 +- src/asahi/compiler/agx_pack.c | 242 ++++- src/asahi/compiler/agx_register_allocate.c | 214 ++++- src/asahi/drm-shim/asahi_noop.c | 16 +- src/asahi/genxml/agx_g11.h | 15 + src/asahi/genxml/agx_pack_header.h | 2 + src/asahi/genxml/cmdbuf.xml | 121 ++- src/asahi/isa/AGX2.xml | 2 +- src/asahi/layout/layout.c | 13 + src/asahi/layout/tiling.cc | 37 +- src/asahi/lib/agx_bg_eot.c | 22 +- src/asahi/lib/agx_device.c | 20 +- src/asahi/lib/agx_helpers.h | 130 +++ src/asahi/lib/agx_linker.c | 13 + src/asahi/lib/agx_linker.h | 9 + src/asahi/lib/agx_nir_lower_tilebuffer.c | 153 +++- src/asahi/lib/agx_nir_lower_uvs.c | 22 +- src/asahi/lib/agx_nir_prolog_epilog.c | 3 + src/asahi/lib/agx_ppp.h | 26 +- src/asahi/lib/agx_tilebuffer.c | 50 + src/asahi/lib/agx_tilebuffer.h | 34 +- src/asahi/lib/agx_usc.h | 48 + src/asahi/lib/decode.c | 68 +- src/asahi/libagx/libagx_dgc.h | 70 ++ src/asahi/libagx/texture.cl | 46 +- src/asahi/vulkan/hk_cmd_buffer.c | 223 ++++- src/asahi/vulkan/hk_cmd_buffer.h | 75 +- src/asahi/vulkan/hk_cmd_dispatch.c | 4 + src/asahi/vulkan/hk_cmd_draw.c | 642 ++++++++++++- src/asahi/vulkan/hk_cmd_meta.c | 16 +- src/asahi/vulkan/hk_descriptor_set.c | 6 + src/asahi/vulkan/hk_descriptor_set.h | 6 +- src/asahi/vulkan/hk_format.c | 6 + src/asahi/vulkan/hk_image.c | 49 +- src/asahi/vulkan/hk_image_view.c | 113 ++- src/asahi/vulkan/hk_image_view.h | 1 + src/asahi/vulkan/hk_nir_lower_descriptors.c | 27 + src/asahi/vulkan/hk_physical_device.c | 87 +- src/asahi/vulkan/hk_private.h | 1 + src/asahi/vulkan/hk_queue.c | 10 +- src/asahi/vulkan/hk_sampler.c | 4 + src/asahi/vulkan/hk_shader.c | 619 ++++++++++++- src/asahi/vulkan/hk_shader.h | 18 + src/compiler/nir/nir_opt_shrink_vectors.c | 15 +- src/drm-shim/device.c | 13 +- src/gallium/drivers/asahi/agx_batch.c | 2 + src/gallium/drivers/asahi/agx_blit.c | 4 +- src/gallium/drivers/asahi/agx_disk_cache.c | 2 + .../drivers/asahi/agx_nir_lower_bindings.c | 11 + .../drivers/asahi/agx_nir_lower_sysvals.c | 2 + src/gallium/drivers/asahi/agx_pipe.c | 82 +- src/gallium/drivers/asahi/agx_state.c | 861 +++++++++++++++--- src/gallium/drivers/asahi/agx_state.h | 44 +- src/gallium/include/pipe/p_defines.h | 2 + .../winsys/kmsro/drm/kmsro_drm_winsys.c | 5 +- src/mesa/state_tracker/st_pbo.c | 2 +- src/util/u_thread.c | 10 + src/vulkan/runtime/vk_meta.h | 2 + src/vulkan/runtime/vk_meta_blit_resolve.c | 25 +- 71 files changed, 4742 insertions(+), 345 deletions(-) create mode 100644 src/asahi/genxml/agx_g11.h diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index e131bcf0721..a6cd71a7afc 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -21,6 +21,7 @@ #include "agx_compiler.h" #include "agx_debug.h" #include "agx_nir.h" +#include "agx_nir_texture.h" #include "agx_opcodes.h" #include "glsl_types.h" #include "nir.h" @@ -686,8 +687,12 @@ agx_emit_local_store_pixel(agx_builder *b, nir_intrinsic_instr *instr) agx_index collected = agx_emit_collect(b, compact_count, compacted); agx_index coords = explicit ? agx_src_index(&instr->src[2]) : agx_null(); + /* G11 tilebuffers are single-sampled, see agx_build_tilebuffer_layout */ + agx_index samples = + AGX_G11 ? agx_immediate(0xff) : agx_src_index(&instr->src[1]); + b->shader->out->tag_write_disable = false; - return agx_st_tile(b, collected, agx_src_index(&instr->src[1]), coords, + return agx_st_tile(b, collected, samples, coords, agx_format_for_pipe(nir_intrinsic_format(instr)), nir_intrinsic_write_mask(instr), nir_intrinsic_base(instr), explicit); @@ -730,7 +735,9 @@ agx_emit_local_load_pixel(agx_builder *b, agx_index dest, agx_wait_pixel_mask(b, 0x8); unsigned nr_comps = instr->def.num_components; - agx_ld_tile_to(b, dest, agx_src_index(&instr->src[0]), agx_null(), + agx_ld_tile_to(b, dest, + AGX_G11 ? agx_immediate(0xff) : agx_src_index(&instr->src[0]), + agx_null(), agx_format_for_pipe(nir_intrinsic_format(instr)), BITFIELD_MASK(nr_comps), nir_intrinsic_base(instr), false); agx_emit_cached_split(b, dest, nr_comps); @@ -977,7 +984,7 @@ translate_atomic_opcode(nir_atomic_op op) static agx_index agx_local_base(nir_src src) { - if (nir_src_is_const(src) && nir_src_as_uint(src) == 0) + if (!AGX_G11 && nir_src_is_const(src) && nir_src_as_uint(src) == 0) return agx_zero(); else return agx_src_index(&src); @@ -1106,6 +1113,18 @@ agx_expand_tex_to(agx_builder *b, nir_def *def, agx_index src, bool masked) return mask; } +/* + * G11 has no texture_load, so loads sample at the pixel centre through the + * txf sampler, which takes pixel coordinates there. + */ +static agx_index +agx_g11_pixel_centre(agx_builder *b, agx_index c) +{ + agx_index f = agx_temp(b->shader, AGX_SIZE_32); + agx_convert_to(b, f, agx_immediate(AGX_CONVERT_S32_TO_F), c, AGX_ROUND_RTE); + return agx_fadd(b, f, agx_immediate_f(0.5f)); +} + static agx_instr * agx_emit_image_load(agx_builder *b, agx_index dst, nir_intrinsic_instr *intr) { @@ -1173,6 +1192,16 @@ agx_emit_image_load(agx_builder *b, agx_index dst, nir_intrinsic_instr *intr) lod = agx_zero(); } + if (AGX_G11) { + /* Images have one level */ + lod_mode = AGX_LOD_MODE_AUTO_LOD; + lod = agx_zero(); + + for (unsigned i = 0; i < glsl_get_sampler_dim_coordinate_components(dim); + ++i) + coord[i] = agx_g11_pixel_centre(b, coord[i]); + } + agx_index coords = agx_emit_collect(b, coord_comps, coord); agx_index tmp = agx_vec_temp(b->shader, dst.size, sparse ? 8 : 4); @@ -1501,6 +1530,18 @@ agx_emit_intrinsic(agx_builder *b, nir_intrinsic_instr *instr) b, dst, instr, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X); case nir_intrinsic_load_local_invocation_index: + if (AGX_G11) { + /* G11 has no thread index register (Apple's G11 code never reads + * one): build it from the position and size of the threadgroup. + */ + agx_index x = agx_get_sr(b, 32, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X); + agx_index y = agx_get_sr(b, 32, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X + 1); + agx_index z = agx_get_sr(b, 32, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X + 2); + agx_index wx = agx_get_sr(b, 32, AGX_SR_THREADS_PER_THREADGROUP_X); + agx_index wy = agx_get_sr(b, 32, AGX_SR_THREADS_PER_THREADGROUP_X + 1); + agx_index yz = agx_imad(b, z, wy, y, 0); + return agx_imad_to(b, dst, yz, wx, x, 0); + } return agx_get_sr_to(b, dst, AGX_SR_THREAD_INDEX_IN_THREADGROUP); case nir_intrinsic_load_layer_id: @@ -1520,7 +1561,9 @@ agx_emit_intrinsic(agx_builder *b, nir_intrinsic_instr *instr) /* Pull out all the big hammers to make cross-workgroup memory * barriers work. */ - if (nir_intrinsic_memory_scope(instr) >= SCOPE_QUEUE_FAMILY) { + /* G11 kills the thread on all of these but memory_barrier. */ + if (!AGX_G11 && + nir_intrinsic_memory_scope(instr) >= SCOPE_QUEUE_FAMILY) { agx_unknown_barrier_1(b); agx_memory_barrier_2(b); @@ -1567,7 +1610,10 @@ agx_emit_intrinsic(agx_builder *b, nir_intrinsic_instr *instr) case nir_intrinsic_fence_mem_to_tex_agx: { /* Flush out the atomic to main memory... Found experimentally... */ agx_memory_barrier(b); - agx_memory_barrier_2(b); + + /* G11 kills the thread on memory_barrier_2 and the texture flush */ + if (!AGX_G11) + agx_memory_barrier_2(b); /* TODO: Which ones do we actually need? */ agx_image_barrier_1(b); @@ -1576,14 +1622,20 @@ agx_emit_intrinsic(agx_builder *b, nir_intrinsic_instr *instr) agx_image_barrier_4(b); /* Flush out the texture cache */ - agx_flush_memory_to_texture(b); + if (!AGX_G11) + agx_flush_memory_to_texture(b); return NULL; } case nir_intrinsic_fence_pbe_to_tex_pixel_agx: { agx_image_barrier_1(b); agx_image_barrier_2(b); - agx_flush_memory_to_texture(b); + + /* G11 ends the thread there, which drops the image writes after it + * when the fence lands in a per-sample loop + */ + if (!AGX_G11) + agx_flush_memory_to_texture(b); agx_image_barrier_3(b); return NULL; } @@ -2260,18 +2312,31 @@ agx_emit_tex(agx_builder *b, nir_tex_instr *instr) case nir_tex_src_texture_offset: texture = index; break; - case nir_tex_src_sampler_handle: - b->shader->out->uses_sampler_heap = true; + case nir_tex_src_sampler_handle: { nir_intrinsic_instr *intr = nir_src_as_intrinsic(instr->src[i].src); - if (intr && intr->intrinsic == nir_intrinsic_bindless_sampler_agx) { + bool bindless = + intr && intr->intrinsic == nir_intrinsic_bindless_sampler_agx; + + /* G11 samples from state slots; Vulkan's bindless handle only + * serves descriptor reads (hk_g11_lower_handles) + */ + if (AGX_G11 && bindless) + break; + + b->shader->out->uses_sampler_heap = true; + if (bindless) { /* Heap offset */ sampler = agx_src_index(&intr->src[1]); } else { sampler = index; } break; + } case nir_tex_src_texture_handle: + if (AGX_G11) + break; + texture = agx_translate_bindless_handle(b, &instr->src[i].src, &bindless); break; @@ -2352,6 +2417,20 @@ agx_emit_tex(agx_builder *b, nir_tex_instr *instr) else if (!agx_is_null(compare)) compare_offset = compare; + if (AGX_G11 && instr->op == nir_texop_txf) { + unsigned n = agx_channels(coords); + agx_index c[4]; + assert(n <= 4); + + agx_emit_split(b, c, coords, n); + for (unsigned i = 0; + i < glsl_get_sampler_dim_coordinate_components(instr->sampler_dim); + ++i) + c[i] = agx_g11_pixel_centre(b, c[i]); + + coords = agx_emit_collect(b, n, c); + } + agx_index tmp = agx_vec_temp(b->shader, dst.size, instr->is_sparse ? 8 : 4); agx_instr *I = agx_texture_sample_to( b, tmp, coords, lod, bindless, texture, sampler, compare_offset, @@ -2359,6 +2438,18 @@ agx_emit_tex(agx_builder *b, nir_tex_instr *instr) !agx_is_null(packed_offset), !agx_is_null(compare), instr->op == nir_texop_lod, agx_gather_for_nir(instr), instr->is_sparse); + /* G11 only binds samplers for shaders that use them */ + b->shader->out->sampler_state_count = + MAX2(b->shader->out->sampler_state_count, 1); + + /* A fetch above level 0 samples through the driver's txf-lod sampler + * (g11_txf_to_txl), which Vulkan binds with the txf one + */ + int handle = nir_tex_instr_src_index(instr, nir_tex_src_sampler_handle); + if (AGX_G11 && handle >= 0 && nir_src_is_const(instr->src[handle].src) && + nir_src_as_uint(instr->src[handle].src) == AGX_G11_TXF_LOD_SAMPLER) + b->shader->out->uses_txf = true; + if (instr->op == nir_texop_txf || instr->op == nir_texop_txf_ms) { I->op = AGX_OPCODE_TEXTURE_LOAD; b->shader->out->uses_txf = true; @@ -2853,6 +2944,205 @@ set_speculate(nir_builder *b, nir_instr *instr, UNUSED void *_) return false; } +/* + * G11 lowering, late so that nothing re-forms what it splits: + * + * - G11 flushes half denormals wherever an f16 value meets the hardware, but + * f16 <-> f32 conversions must be exact. Handle the denormal range with + * integer and f32 arithmetic and keep the hardware conversion for the rest. + * - G11 has no srsqrt, so sqrt is x * rsqrt(x) with 0 and inf passed through. + * - Nor roundeven: round floor(x) up when the rest is over a half, or a half + * with floor(x) odd. Nor trunc: floor(|x|) with x's sign, which NIR's own + * lowering loses for -0. agx_nir_options lowers ceil to -floor(-x). + */ +/* f16 -> f32 that keeps half denormals: sign | 0 | mantissa is + * +-mantissa * 2^-24, the rest is the hardware conversion. + */ +static nir_def * +exact_f2f32(nir_builder *b, nir_def *x) +{ + nir_def *bits = nir_u2u32(b, x); + nir_def *den = nir_fmul_imm(b, nir_u2f32(b, nir_iand_imm(b, bits, 0x3ff)), + 0x1p-24); + den = nir_bcsel(b, nir_test_mask(b, bits, 0x8000), nir_fneg(b, den), den); + return nir_bcsel(b, nir_ieq_imm(b, nir_iand_imm(b, bits, 0x7c00), 0), den, + nir_f2f32(b, x)); +} + +/* + * 8-bit normalized values are 0 or at least 1/255 apart, so flushing half + * denormals changes none of them: conversions that only feed 8-bit + * normalized tile or image stores, or read such a tile, keep the hardware's. + */ +static bool +g11_unorm8_access(nir_instr *instr, bool load) +{ + if (instr->type != nir_instr_type_intrinsic) + return false; + + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + switch (intr->intrinsic) { + case nir_intrinsic_load_local_pixel_agx: + if (!load) + return false; + break; + case nir_intrinsic_store_local_pixel_agx: + case nir_intrinsic_bindless_image_store: + case nir_intrinsic_image_store: + if (load) + return false; + break; + default: + return false; + } + + const struct util_format_description *desc = + util_format_description(nir_intrinsic_format(intr)); + return desc->channel[0].size == 8 && desc->channel[0].normalized && + desc->colorspace != UTIL_FORMAT_COLORSPACE_SRGB; +} + +static bool +g11_only_unorm8_stores(nir_def *def) +{ + nir_foreach_use_including_if(src, def) { + if (nir_src_is_if(src)) + return false; + + nir_instr *use = nir_src_use_instr(src); + if (use->type == nir_instr_type_alu && + nir_op_is_vec(nir_instr_as_alu(use)->op)) { + if (!g11_only_unorm8_stores(&nir_instr_as_alu(use)->def)) + return false; + } else if (!g11_unorm8_access(use, false)) { + return false; + } + } + + return true; +} + +static bool +lower_g11_alu(nir_builder *b, nir_alu_instr *alu, UNUSED void *data) +{ + nir_def *x = alu->src[0].src.ssa; + bool to_f32 = alu->op == nir_op_f2f32 && x->bit_size == 16; + + /* f16 compares see denormals as zero, which breaks libclc's rtp and rtn + * conversions: compare in f32 instead. + */ + if (nir_alu_instr_is_comparison(alu) && x->bit_size == 16 && + nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[0]) == + nir_type_float) { + b->cursor = nir_before_instr(&alu->instr); + nir_def *srcs[2]; + for (unsigned i = 0; i < 2; ++i) + srcs[i] = exact_f2f32(b, nir_ssa_for_alu_src(b, alu, i)); + nir_def_replace(&alu->def, nir_build_alu2(b, alu->op, srcs[0], srcs[1])); + return true; + } + + if (alu->op == nir_op_fround_even) { + b->cursor = nir_before_instr(&alu->instr); + x = nir_ssa_for_alu_src(b, alu, 0); + unsigned sz = x->bit_size; + nir_def *f = nir_ffloor(b, x); + nir_def *d = nir_fadd(b, x, nir_fneg(b, f)); + nir_def *h = nir_fmul_imm(b, f, 0.5); + nir_def *odd = nir_fneu(b, nir_ffloor(b, h), h); + nir_def *up = nir_ior(b, nir_flt(b, nir_imm_floatN_t(b, 0.5, sz), d), + nir_iand(b, nir_feq_imm(b, d, 0.5), odd)); + nir_def *r = nir_fadd(b, f, nir_bcsel(b, up, nir_imm_floatN_t(b, 1.0, sz), + nir_imm_floatN_t(b, 0.0, sz))); + /* A zero result keeps x's sign */ + nir_def *sign = nir_iand_imm(b, x, 1ull << (sz - 1)); + nir_def_replace(&alu->def, + nir_bcsel(b, nir_feq_imm(b, r, 0.0), sign, r)); + return true; + } + + if (alu->op == nir_op_ftrunc) { + b->cursor = nir_before_instr(&alu->instr); + x = nir_ssa_for_alu_src(b, alu, 0); + nir_def *sign = nir_iand_imm(b, x, 1ull << (x->bit_size - 1)); + nir_def_replace(&alu->def, nir_ior(b, nir_ffloor(b, nir_fabs(b, x)), sign)); + return true; + } + + if (alu->op == nir_op_fsqrt) { + b->cursor = nir_before_instr(&alu->instr); + x = nir_ssa_for_alu_src(b, alu, 0); + nir_def *edge = nir_ior(b, nir_feq_imm(b, x, 0.0), + nir_feq_imm(b, x, INFINITY)); + nir_def_replace(&alu->def, + nir_bcsel(b, edge, x, nir_fmul(b, x, nir_frsq(b, x)))); + return true; + } + + if (to_f32 && (g11_unorm8_access(nir_def_instr(x), true) || + g11_only_unorm8_stores(&alu->def))) + return false; + + if (to_f32) { + b->cursor = nir_before_instr(&alu->instr); + nir_def_replace(&alu->def, exact_f2f32(b, nir_ssa_for_alu_src(b, alu, 0))); + return true; + } + + if (alu->op != nir_op_f2f16 && alu->op != nir_op_f2f16_rtne) + return false; + + if (g11_only_unorm8_stores(&alu->def)) + return false; + + b->cursor = nir_after_instr(&alu->instr); + x = nir_ssa_for_alu_src(b, alu, 0); + + /* + * Below 2^-14 the result is |x| in units of 2^-24, rounded to even, + * with x's sign: the 24-bit mantissa shifted right by 126 - exponent. + * Integer only, since G11 has no round instruction. Shifts clamp at + * 31, where the mantissa rounds to 0. + */ + nir_def *e = nir_iand_imm(b, nir_ushr_imm(b, x, 23), 0xff); + nir_def *mant = nir_ior_imm(b, nir_iand_imm(b, x, 0x7fffff), 0x800000); + nir_def *s = nir_isub(b, nir_imm_int(b, 126), e); + s = nir_bcsel(b, nir_ult_imm(b, s, 31), s, nir_imm_int(b, 31)); + nir_def *q = nir_ushr(b, mant, s); + nir_def *rem = + nir_iand(b, mant, nir_iadd_imm(b, nir_ishl(b, nir_imm_int(b, 1), s), -1)); + nir_def *half = nir_ishl(b, nir_imm_int(b, 1), nir_iadd_imm(b, s, -1)); + nir_def *up = nir_ior(b, nir_ult(b, half, rem), + nir_iand(b, nir_ieq(b, rem, half), nir_test_mask(b, q, 1))); + q = nir_iadd(b, q, nir_bcsel(b, up, nir_imm_int(b, 1), nir_imm_int(b, 0))); + nir_def *sign = nir_ushr_imm(b, nir_iand_imm(b, x, 0x80000000), 16); + nir_def *den = nir_u2u16(b, nir_ior(b, q, sign)); + nir_def *res = + nir_bcsel(b, nir_flt_imm(b, nir_fabs(b, x), 0x1p-14), den, &alu->def); + + nir_def_rewrite_uses_after(&alu->def, res); + return true; +} + +/* Vulkan's bindless textures leave textures_used empty */ +static bool +agx_g11_has_textures(nir_shader *nir) +{ + if (!BITSET_IS_EMPTY(nir->info.textures_used)) + return true; + + nir_foreach_function_impl(impl, nir) { + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_tex) + return true; + } + } + } + + return false; +} + static void agx_optimize_nir(nir_shader *nir, bool soft_fault, uint16_t *preamble_size, uint8_t *ts_count, uint8_t *ss_count) @@ -2900,7 +3190,9 @@ agx_optimize_nir(nir_shader *nir, bool soft_fault, uint16_t *preamble_size, .lower_subgroup_masks = true, }; - NIR_PASS(progress, nir, nir_opt_uniform_atomics, true); + /* G11 has no SIMD reductions (simd_iadd kills the thread). */ + if (!AGX_G11) + NIR_PASS(progress, nir, nir_opt_uniform_atomics, true); /* Lower addressing modes. The sooner we do this, the sooner we get rid of * amul/aadd instructions and can let nir_opt_algebraic do its job. But we @@ -2968,7 +3260,23 @@ agx_optimize_nir(nir_shader *nir, bool soft_fault, uint16_t *preamble_size, /* Force preamble generation for internal shaders since hk's meta copies * depend on it for correctness with imgwblk intrinsics. */ - if (preamble_size && + /* + * G11 fragment preambles go stale: in a run of draws, some cores shade + * with an earlier draw's preamble results (dEQP-GLES3 shaders.matrix + * uniform cases, varying regions of the image). + * + * ponytail: no fragment preambles on G11, uniform math runs per pixel; + * find what Apple sets to rerun them if that ever shows up in a profile + * + * A G11 preamble that samples drops a triangle now and then (dEQP-GLES3 + * uniform_api struct_in_array sampler2D_samplerCube_both, 9 in 100 + * runs), so shaders with textures get none either. + */ + bool g11_no_preamble = + AGX_G11 && (nir->info.stage == MESA_SHADER_FRAGMENT || + agx_g11_has_textures(nir)); + + if (preamble_size && !g11_no_preamble && (!(agx_compiler_debug & AGX_DBG_NOPREAMBLE) || nir->info.internal)) { unsigned sizes[] = { *preamble_size, @@ -3034,6 +3342,10 @@ agx_optimize_nir(nir_shader *nir, bool soft_fault, uint16_t *preamble_size, * do it after fusing constant shifts. Constant folding will clean up. */ NIR_PASS(_, nir, agx_nir_lower_algebraic_late); + if (AGX_G11) { + NIR_PASS(_, nir, nir_shader_alu_pass, lower_g11_alu, + nir_metadata_control_flow, NULL); + } NIR_PASS(_, nir, agx_nir_fuse_selects); NIR_PASS(_, nir, nir_opt_constant_folding); NIR_PASS(_, nir, nir_opt_combine_barriers, NULL, NULL); @@ -3412,7 +3724,8 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl, AGX_PASS(ctx, agx_ra); agx_lower_64bit_postra(ctx); - if (ctx->scratch_size_B > 0) { + /* G11 spills to threadgroup memory, see agx_lower_spill.c */ + if (ctx->scratch_size_B > 0 && !AGX_G11) { /* Apple always allocate 40 more bytes in the entrypoint and align to 4. */ uint64_t stack_size = align(DIV_ROUND_UP(ctx->scratch_size_B, 4) + 10, 4); @@ -3502,7 +3815,8 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl, */ bool dump_shaders = agx_should_dump(nir, AGX_DBG_SHADERS); #ifndef NDEBUG - bool selftest = !dump_shaders; + /* The ISA description does not know the G11 threadgroup encoding. */ + bool selftest = !dump_shaders && !AGX_G11; #else bool selftest = false; #endif @@ -3512,7 +3826,7 @@ agx_compile_function_nir(nir_shader *nir, nir_function_impl *impl, bool errors = agx2_disassemble(binary->data + offset, binary->size - offset, fp); - if (errors) { + if (errors && !AGX_G11) { /* In a self-test, disassemble again to get something the user can look * at to figure out what went wrong. */ @@ -3592,9 +3906,16 @@ agx_preprocess_nir(nir_shader *nir) NIR_PASS(_, nir, nir_lower_vars_to_ssa); - /* Lower large arrays to scratch and small arrays to csel */ - NIR_PASS(_, nir, nir_lower_vars_to_scratch, 256, - glsl_get_natural_size_align_bytes, glsl_get_word_size_align_bytes); + /* + * Lower large arrays to scratch and small arrays to csel. G11 has scratch + * only for compute (in global memory); the G13 stack instructions graphics + * would get hang it, so keep every array in registers there. + */ + if (!AGX_G11 || nir->info.stage == MESA_SHADER_COMPUTE || + nir->info.stage == MESA_SHADER_KERNEL) + NIR_PASS(_, nir, nir_lower_vars_to_scratch, 256, + glsl_get_natural_size_align_bytes, + glsl_get_word_size_align_bytes); NIR_PASS(_, nir, nir_lower_indirect_derefs_to_if_else_trees, nir_var_function_temp, ~0); NIR_PASS(_, nir, nir_split_var_copies); diff --git a/src/asahi/compiler/agx_compile.h b/src/asahi/compiler/agx_compile.h index 4b104232b76..ebe0c7b1d2b 100644 --- a/src/asahi/compiler/agx_compile.h +++ b/src/asahi/compiler/agx_compile.h @@ -5,6 +5,7 @@ #pragma once +#include "asahi/genxml/agx_g11.h" #include "compiler/nir/nir.h" #include "util/shader_stats.h" #include "util/u_dynarray.h" @@ -77,6 +78,11 @@ struct agx_rodata { uint16_t size_16; }; +/* Top of the G11 spill area: the 32 KB window less the 16 bytes that + * agx_usc_shader_g11 adds to the threadgroup memory size. + */ +#define AGX_G11_SPILL_TOP (32768 - 16) + struct agx_shader_info { mesa_shader_stage stage; uint32_t binary_size; @@ -95,6 +101,11 @@ struct agx_shader_info { /* Scratch memory allocation in bytes for main/preamble respectively */ unsigned scratch_size, preamble_scratch_size; + /* G11: bytes of threadgroup memory per thread for register spills, at the + * top of the window, see agx_lower_spill.c. + */ + unsigned spill_stride; + /* Size in bytes of the main sahder */ unsigned main_size; @@ -368,6 +379,8 @@ static const nir_shader_compiler_options agx_nir_options = { .lower_fminmax_signed_zero = true, .lower_fdph = true, .lower_ffract = true, + /* G11 has floor but no ceil; trunc and roundeven are in lower_g11_alu. */ + .lower_fceil = AGX_G11, .lower_pack_half_2x16 = true, .lower_pack_unorm_2x16 = true, .lower_pack_snorm_2x16 = true, @@ -395,10 +408,13 @@ static const nir_shader_compiler_options agx_nir_options = { .lower_int64_options = (nir_lower_int64_options) ~(nir_lower_iadd64 | nir_lower_imul_2x32_64), .lower_doubles_options = (nir_lower_doubles_options)(~0), + /* G11's iter and st_vary take immediate indices only */ .support_indirect_inputs = BITFIELD_BIT(MESA_SHADER_TESS_CTRL) | BITFIELD_BIT(MESA_SHADER_TESS_EVAL) | - BITFIELD_BIT(MESA_SHADER_FRAGMENT), - .support_indirect_outputs = (uint8_t)BITFIELD_MASK(MESA_SHADER_STAGES), + (AGX_G11 ? 0 : BITFIELD_BIT(MESA_SHADER_FRAGMENT)), + .support_indirect_outputs = + (uint8_t)(BITFIELD_MASK(MESA_SHADER_STAGES) & + ~(AGX_G11 ? BITFIELD_BIT(MESA_SHADER_VERTEX) : 0)), .max_samples = 4, .lower_fquantize2f16 = true, .compact_arrays = true, diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 45460934cf0..ee265af1e62 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -6,6 +6,7 @@ #pragma once +#include "asahi/genxml/agx_g11.h" #include "asahi/isa/agx_minifloat.h" #include "compiler/nir/nir.h" #include "util/bitset.h" @@ -24,7 +25,8 @@ extern "C" { #define AGX_NUM_REGS (256) /* u0-u255 inclusive, as pairs of 16-bits */ -#define AGX_NUM_UNIFORMS (512) +/* G11 uniforms past 255 need an encoding we do not know */ +#define AGX_NUM_UNIFORMS (AGX_G11 ? 256 : 512) /* Semi-arbitrary limit for spill slot allocation */ #define AGX_NUM_MODELED_REGS_LOG2 (11) diff --git a/src/asahi/compiler/agx_insert_waits.c b/src/asahi/compiler/agx_insert_waits.c index 75bfd0092d7..f11a26abdc0 100644 --- a/src/asahi/compiler/agx_insert_waits.c +++ b/src/asahi/compiler/agx_insert_waits.c @@ -15,6 +15,11 @@ static bool instr_is_async(agx_instr *I) { + /* G11 threadgroup loads and atomics use the device memory encodings. */ + if (AGX_G11 && + (I->op == AGX_OPCODE_LOCAL_LOAD || I->op == AGX_OPCODE_LOCAL_ATOMIC)) + return true; + return agx_opcodes_info[I->op].immediates & AGX_IMMEDIATE_SCOREBOARD; } diff --git a/src/asahi/compiler/agx_lower_spill.c b/src/asahi/compiler/agx_lower_spill.c index d5ab85e747f..d57aa3e17f7 100644 --- a/src/asahi/compiler/agx_lower_spill.c +++ b/src/asahi/compiler/agx_lower_spill.c @@ -47,16 +47,63 @@ spill_fill(agx_builder *b, agx_instr *I, enum agx_size size, unsigned channels, "RA allocates enough scratch"); /* Emit the spill/fill */ - if (I->dest[0].memory) { + if (AGX_G11) { + /* r3l holds this thread's spill base, r3h the address of the access */ + agx_index addr = agx_register(7, AGX_SIZE_16); + agx_mov_imm_to(b, addr, stack_offs_B); + agx_iadd_to(b, addr, agx_register(6, AGX_SIZE_16), addr, 0); + + if (I->dest[0].memory) + agx_local_store(b, reg, addr, agx_zero(), format, mask); + else + agx_local_load_to(b, reg, addr, agx_zero(), format, mask); + } else if (I->dest[0].memory) { agx_stack_store(b, reg, agx_immediate(stack_offs_B), format, mask); } else { agx_stack_load_to(b, reg, agx_immediate(stack_offs_B), format, mask); } } +/* + * G11 kills the thread on stack_load/stack_store, so spill to threadgroup + * memory instead: each thread gets spill_stride bytes, stacked down + * from AGX_G11_SPILL_TOP. Nothing is live at entry, so r1-r3 (reserved by RA + * when spilling) are free to compute the base, + * r3l = TOP - (local_index + 1) * stride. + */ +static void +g11_spill_base(agx_context *ctx) +{ + assert(ctx->stage == MESA_SHADER_COMPUTE && !ctx->is_preamble); + assert(ctx->has_spill_pcopy_reserved); + + unsigned stride = align(ctx->scratch_size_B, 16); + agx_index r1 = agx_register(2, AGX_SIZE_32); + agx_index r2 = agx_register(4, AGX_SIZE_32); + agx_index r3 = agx_register(6, AGX_SIZE_32); + agx_builder b = agx_init_builder(ctx, agx_before_block(agx_start_block(ctx))); + + agx_get_sr_to(&b, r1, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X + 2); + agx_get_sr_to(&b, r2, AGX_SR_THREADS_PER_THREADGROUP_X + 1); + agx_get_sr_to(&b, r3, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X + 1); + agx_imad_to(&b, r1, r1, r2, r3, 0); + agx_get_sr_to(&b, r2, AGX_SR_THREADS_PER_THREADGROUP_X); + agx_get_sr_to(&b, r3, AGX_SR_THREAD_POSITION_IN_THREADGROUP_X); + agx_imad_to(&b, r1, r1, r2, r3, 0); + agx_mov_imm_to(&b, r2, (uint32_t)-stride); + agx_mov_imm_to(&b, r3, AGX_G11_SPILL_TOP - stride); + agx_imad_to(&b, r3, r1, r2, r3, 0); + + ctx->max_reg = MAX2(ctx->max_reg, 7); + ctx->out->spill_stride = stride; +} + void agx_lower_spill(agx_context *ctx) { + if (AGX_G11) + g11_spill_base(ctx); + agx_foreach_instr_global_safe(ctx, I) { if (I->op != AGX_OPCODE_MOV || (!I->dest[0].memory && !I->src[0].memory)) continue; diff --git a/src/asahi/compiler/agx_lower_uniform_sources.c b/src/asahi/compiler/agx_lower_uniform_sources.c index 3aeb3a4095d..b3073c5d933 100644 --- a/src/asahi/compiler/agx_lower_uniform_sources.c +++ b/src/asahi/compiler/agx_lower_uniform_sources.c @@ -41,9 +41,9 @@ agx_instr_accepts_uniform(enum agx_opcode op, unsigned src_index, case AGX_OPCODE_ATOMIC: return src_index == 1 && !high; case AGX_OPCODE_LOCAL_LOAD: - return src_index == 0; + return !AGX_G11 && src_index == 0; case AGX_OPCODE_LOCAL_STORE: - return src_index == 1; + return !AGX_G11 && src_index == 1; case AGX_OPCODE_IMAGE_WRITE: return src_index == 3; case AGX_OPCODE_BLOCK_IMAGE_STORE: diff --git a/src/asahi/compiler/agx_nir.h b/src/asahi/compiler/agx_nir.h index c03715027e5..32dbfc54a81 100644 --- a/src/asahi/compiler/agx_nir.h +++ b/src/asahi/compiler/agx_nir.h @@ -26,4 +26,5 @@ bool agx_nir_lower_fminmax(struct nir_shader *s); bool agx_nir_lower_texture_early(struct nir_shader *s, bool support_lod_bias); bool agx_nir_lower_texture(struct nir_shader *s); bool agx_nir_lower_multisampled_image_store(struct nir_shader *s); +bool agx_nir_lower_g11_layered_image_store(struct nir_shader *s); bool agx_nir_needs_texture_crawl(struct nir_instr *instr); diff --git a/src/asahi/compiler/agx_nir_lower_discard_zs_emit.c b/src/asahi/compiler/agx_nir_lower_discard_zs_emit.c index 07bf42aa714..ed46c9fe812 100644 --- a/src/asahi/compiler/agx_nir_lower_discard_zs_emit.c +++ b/src/asahi/compiler/agx_nir_lower_discard_zs_emit.c @@ -54,6 +54,10 @@ lower_zs_emit(nir_block *block, bool force_early_z) */ if (z) value = nir_f2f32(&b, value); + + /* G11 does not clamp the depth a shader writes to [0, 1] */ + if (z && AGX_G11) + value = nir_fsat(&b, value); else value = nir_u2u16(&b, value); diff --git a/src/asahi/compiler/agx_nir_lower_texture.c b/src/asahi/compiler/agx_nir_lower_texture.c index 616d0b0e9d1..239fa1ed7e0 100644 --- a/src/asahi/compiler/agx_nir_lower_texture.c +++ b/src/asahi/compiler/agx_nir_lower_texture.c @@ -8,6 +8,7 @@ #include "compiler/nir/nir.h" #include "compiler/nir/nir_builder.h" +#include "asahi/genxml/agx_g11.h" #include "agx_nir.h" #include "agx_nir_texture.h" #include "glsl_types.h" @@ -50,10 +51,29 @@ fence_image(struct nir_builder *b, nir_intrinsic_instr *intr, void *data) } } +/* + * G11 has no bindless texture state, so its textures stay in state registers + * and descriptors are found from the index (see agx_nir_lower_bindings). + */ +static nir_def * +descriptor_for_index(nir_builder *b, nir_def *index) +{ + return nir_load_from_texture_handle_agx( + b, nir_load_texture_handle_agx(b, nir_u2u32(b, index))); +} + static nir_def * texture_descriptor_ptr(nir_builder *b, nir_tex_instr *tex) { int handle_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_handle); + + if (AGX_G11 && handle_idx < 0) { + int offs_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_offset); + return descriptor_for_index(b, offs_idx >= 0 + ? tex->src[offs_idx].src.ssa + : nir_imm_int(b, tex->texture_index)); + } + assert(handle_idx >= 0 && "must be bindless"); return nir_load_from_texture_handle_agx(b, tex->src[handle_idx].src.ssa); } @@ -201,6 +221,63 @@ lower_buffer_texture(nir_builder *b, nir_tex_instr *tex) * requires the index to be a clamped integer. Lower tex_src_coord into * tex_src_backend1 for array textures by type-converting and clamping. */ +/* + * G11 loads sample in pixel coordinates, which ignore mip levels, so a txf + * above level 0 becomes a txl at the texel centre in normalized coordinates, + * through AGX_G11_TXF_LOD_SAMPLER (mip nearest, normalized). + */ +static void +g11_txf_to_txl(nir_builder *b, nir_tex_instr *tex) +{ + int lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_lod); + nir_def *lod = nir_u2u32(b, tex->src[lod_idx].src.ssa); + nir_def *coord = nir_steal_tex_src(tex, nir_tex_src_coord); + nir_def *offset = nir_steal_tex_src(tex, nir_tex_src_offset); + nir_def *size = nir_build_texture_query(b, tex, nir_texop_txs, + tex->coord_components, + nir_type_int32, false, true); + unsigned spatial = tex->coord_components - (tex->is_array ? 1 : 0); + nir_def *c[4]; + + for (unsigned i = 0; i < tex->coord_components; ++i) { + nir_def *x = nir_i2i32(b, nir_channel(b, coord, i)); + + if (i < spatial) { + if (offset) + x = nir_iadd(b, x, nir_i2i32(b, nir_channel(b, offset, i))); + + nir_def *n = nir_imax(b, nir_ushr(b, nir_channel(b, size, i), lod), + nir_imm_int(b, 1)); + c[i] = nir_fdiv(b, nir_fadd_imm(b, nir_i2f32(b, x), 0.5f), + nir_i2f32(b, n)); + } else { + c[i] = nir_i2f32(b, x); + } + } + + /* The sampler clamps the level and the layer: a level or layer out of + * bounds moves the texel out of bounds instead, to read the zero border + */ + nir_def *levels = nir_build_texture_query(b, tex, nir_texop_query_levels, 1, + nir_type_int32, false, false); + nir_def *valid = nir_ult(b, lod, levels); + + if (tex->is_array) { + unsigned l = tex->coord_components - 1; + valid = nir_iand(b, valid, nir_ult(b, nir_i2i32(b, nir_channel(b, coord, l)), + nir_channel(b, size, l))); + } + + c[0] = nir_bcsel(b, valid, c[0], nir_imm_float(b, -1.0f)); + + tex->op = nir_texop_txl; + tex->sampler_index = AGX_G11_TXF_LOD_SAMPLER - 1; /* +1 below for txf */ + lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_lod); + nir_src_rewrite(&tex->src[lod_idx].src, nir_i2f16(b, lod)); /* 16-bit, as legalized */ + nir_tex_instr_add_src(tex, nir_tex_src_coord, + nir_vec(b, c, tex->coord_components)); +} + static bool lower_regular_texture(nir_builder *b, nir_instr *instr, UNUSED void *data) { @@ -220,6 +297,51 @@ lower_regular_texture(nir_builder *b, nir_instr *instr, UNUSED void *data) if (nir_tex_instr_src_index(tex, nir_tex_src_backend1) >= 0) return false; + /* Vulkan on G11 has no multisampled images (hk_CreateImage), so arrays + * only come with null descriptors: read sample 0 + */ + if (AGX_G11 && tex->op == nir_texop_txf_ms && tex->is_array) { + nir_steal_tex_src(tex, nir_tex_src_ms_index); + tex->op = nir_texop_txf; + tex->sampler_dim = GLSL_SAMPLER_DIM_2D; + } + + /* A sample per layer for colour, per channel of a one-layer array for + * depth and stencil: see agx_g11_ms_layers + */ + if (AGX_G11 && tex->op == nir_texop_txf_ms) { + nir_def *xy = nir_steal_tex_src(tex, nir_tex_src_coord); + nir_def *s = nir_i2iN(b, nir_steal_tex_src(tex, nir_tex_src_ms_index), + xy->bit_size); + + tex->op = nir_texop_txf; + tex->sampler_dim = GLSL_SAMPLER_DIM_2D; + tex->is_array = true; + tex->coord_components = 3; + + nir_def *layers = nir_channel( + b, nir_build_texture_query(b, tex, nir_texop_txs, 3, nir_type_int32, + false, true), + 2); + nir_def *per_layer = nir_ugt_imm(b, nir_u2u32(b, layers), 1); + + nir_tex_instr_add_src( + tex, nir_tex_src_coord, + nir_vec3(b, nir_channel(b, xy, 0), nir_channel(b, xy, 1), + nir_bcsel(b, per_layer, s, nir_imm_intN_t(b, 0, s->bit_size)))); + + b->cursor = nir_after_instr(&tex->instr); + nir_def *res = nir_bcsel( + b, per_layer, &tex->def, + nir_replicate(b, nir_vector_extract(b, &tex->def, s), + tex->def.num_components)); + nir_def_rewrite_uses_after(&tex->def, res); + b->cursor = nir_before_instr(&tex->instr); + } + + if (AGX_G11 && tex->op == nir_texop_txf && has_nonzero_lod(tex)) + g11_txf_to_txl(b, tex); + /* Get the coordinates */ nir_def *coord = nir_steal_tex_src(tex, nir_tex_src_coord); nir_def *ms_idx = nir_steal_tex_src(tex, nir_tex_src_ms_index); @@ -417,6 +539,13 @@ txs_for_image(nir_builder *b, nir_intrinsic_instr *intr, return res; } +/* See lower_g11_layered_store */ +static bool +agx_g11_layered_store(enum glsl_sampler_dim dim, bool array) +{ + return array || dim == GLSL_SAMPLER_DIM_3D || dim == GLSL_SAMPLER_DIM_CUBE; +} + static nir_def * image_texel_address(nir_builder *b, nir_intrinsic_instr *intr, bool return_index) @@ -441,6 +570,14 @@ image_texel_address(nir_builder *b, nir_intrinsic_instr *intr, (dim == GLSL_SAMPLER_DIM_CUBE) || (dim == GLSL_SAMPLER_DIM_3D); + /* G11 layered images have a buffer-like PBE, see lower_g11_layered_store */ + if (AGX_G11 && !return_index && + agx_g11_layered_store(dim, nir_intrinsic_image_array(intr))) { + return libagx_g11_layered_address( + b, desc_address, nir_pad_vec4(b, nir_u2u32(b, coord)), + nir_imm_bool(b, dim == GLSL_SAMPLER_DIM_1D)); + } + if (dim == GLSL_SAMPLER_DIM_BUF && return_index) { return nir_channel(b, coord, 0); } else if (dim == GLSL_SAMPLER_DIM_BUF) { @@ -528,8 +665,12 @@ lower_image_load_robustness(nir_builder *b, nir_intrinsic_instr *intr) nir_def *layer = nir_channel(b, coord, lidx); /* image_load is effectively the same as txf, reuse the txf lower */ + nir_def *desc = intr->intrinsic == nir_intrinsic_image_load + ? descriptor_for_index(b, intr->src[0].ssa) + : nir_load_from_texture_handle_agx(b, intr->src[0].ssa); + nir_def *replaced = libagx_lower_txf_robustness( - b, nir_load_from_texture_handle_agx(b, intr->src[0].ssa), + b, desc, nir_imm_bool(b, false /* lower LOD */), lod, nir_imm_bool(b, false /* lower min LOD */), lod, nir_imm_bool(b, true /* lower layer */), layer, nir_channel(b, coord, 0)); @@ -635,16 +776,26 @@ lower_robustness(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) nir_def *deref = intr->src[0].ssa; nir_def *coord = intr->src[1].ssa; + bool store = intr->intrinsic == nir_intrinsic_image_deref_store; + bool g11_layered = AGX_G11 && store && agx_g11_layered_store(dim, array); + if (dim != GLSL_SAMPLER_DIM_BUF && - !(dim == GLSL_SAMPLER_DIM_MS && - intr->intrinsic == nir_intrinsic_image_deref_store)) + !(dim == GLSL_SAMPLER_DIM_MS && store) && !g11_layered) return false; /* Bounds check the coordinate */ nir_def *size = - nir_image_deref_size(b, size_components, 32, deref, nir_imm_int(b, 0), + nir_image_deref_size(b, size_components - (dim == GLSL_SAMPLER_DIM_CUBE && !array), + 32, deref, nir_imm_int(b, 0), .image_dim = dim, .image_array = array); - nir_def *oob = nir_bany(b, nir_uge(b, coord, size)); + /* Cube sizes count cubes, not faces */ + if (dim == GLSL_SAMPLER_DIM_CUBE && !array) + size = nir_vec3(b, nir_channel(b, size, 0), nir_channel(b, size, 1), + nir_imm_int(b, 6)); + else if (dim == GLSL_SAMPLER_DIM_CUBE) + size = nir_vector_insert_imm(b, size, nir_imul_imm(b, nir_channel(b, size, 2), 6), 2); + + nir_def *oob = nir_bany(b, nir_uge(b, nir_trim_vector(b, coord, size_components), size)); /* Bounds check the sample */ if (dim == GLSL_SAMPLER_DIM_MS) { @@ -663,7 +814,7 @@ lower_robustness(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) * This ensures the resulting hardware coordinate is definitely * out-of-bounds, giving hardware-level robustness2 behaviour. */ - unsigned c = size_components - 1; + unsigned c = g11_layered ? 0 : size_components - 1; nir_def *r = nir_bcsel(b, oob, nir_imm_int(b, 0xFFF0), nir_channel(b, coord, c)); @@ -706,6 +857,59 @@ agx_nir_lower_texture_early(nir_shader *s, bool support_lod_bias) return progress; } +/* + * G11 has no LOD query: compute the LOD from coordinate derivatives. The + * sampler's LOD bias and clamps are not applied. + */ +static bool +g11_lower_lod_query(nir_builder *b, nir_tex_instr *tex, UNUSED void *data) +{ + if (tex->op != nir_texop_lod) + return false; + + b->cursor = nir_before_instr(&tex->instr); + bool cube = tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE; + unsigned n = tex->coord_components; /* no layer for LOD queries */ + int c = nir_tex_instr_src_index(tex, nir_tex_src_coord); + nir_def *p = nir_trim_vector(b, tex->src[c].src.ssa, n); + + nir_def *size = nir_i2f32( + b, nir_build_texture_query(b, tex, nir_texop_txs, + (cube ? 2 : n) + tex->is_array, + nir_type_int32, false, true)); + + if (cube) { + /* Face coordinates span [-1, 1] over the face width */ + nir_def *m = nir_fmax(b, nir_fabs(b, nir_channel(b, p, 0)), + nir_fmax(b, nir_fabs(b, nir_channel(b, p, 1)), + nir_fabs(b, nir_channel(b, p, 2)))); + p = nir_fmul(b, p, nir_frcp(b, m)); + size = nir_fmul_imm(b, nir_replicate(b, nir_channel(b, size, 0), 3), 0.5); + } else { + size = nir_trim_vector(b, size, n); + } + + nir_def *dx = nir_fmul(b, nir_ddx(b, p), size); + nir_def *dy = nir_fmul(b, nir_ddy(b, p), size); + nir_def *rho2 = nir_fmax(b, nir_fdot(b, dx, dx), nir_fdot(b, dy, dy)); + nir_def *lod = nir_fmul_imm(b, nir_flog2(b, rho2), 0.5); + + nir_def *levels = nir_i2f32( + b, nir_build_texture_query(b, tex, nir_texop_query_levels, 1, + nir_type_int32, false, false)); + nir_def *level = + nir_fmin(b, nir_fmax(b, lod, nir_imm_float(b, 0.0)), + nir_fadd_imm(b, levels, -1.0)); + + /* Null descriptors have no size and query zero */ + nir_def *res = nir_vec2(b, level, lod); + res = nir_bcsel(b, nir_feq_imm(b, nir_channel(b, size, 0), 0.0), + nir_imm_vec2(b, 0.0, 0.0), res); + + nir_def_replace(&tex->def, nir_f2fN(b, res, tex->def.bit_size)); + return true; +} + bool agx_nir_lower_texture(nir_shader *s) { @@ -743,6 +947,10 @@ agx_nir_lower_texture(nir_shader *s) * 16-bit multisample indices) but before lowering queries (as the lowering * generates txs for array textures). */ + if (AGX_G11) + NIR_PASS(progress, s, nir_shader_tex_pass, g11_lower_lod_query, + nir_metadata_control_flow, NULL); + NIR_PASS(progress, s, nir_shader_instructions_pass, lower_regular_texture, nir_metadata_none, NULL); NIR_PASS(progress, s, nir_shader_instructions_pass, lower_tex_crawl, @@ -773,6 +981,48 @@ lower_multisampled_store(nir_builder *b, nir_intrinsic_instr *intr, return true; } +/* + * G11's image write to arrays, 3D and cubes ends the thread. Write them as 2D + * through a buffer-like PBE (hk_image_view.c) at the texel's index. An + * out-of-bounds write has x = 0xFFF0 from lower_robustness. + */ +static bool +lower_g11_layered_store(nir_builder *b, nir_intrinsic_instr *intr, + UNUSED void *data) +{ + b->cursor = nir_before_instr(&intr->instr); + + if (intr->intrinsic != nir_intrinsic_bindless_image_store || + !agx_g11_layered_store(nir_intrinsic_image_dim(intr), + nir_intrinsic_image_array(intr))) + return false; + + nir_def *coord = nir_u2u32(b, intr->src[1].ssa); + nir_def *desc = nir_load_from_texture_handle_agx(b, intr->src[0].ssa); + bool is_1d = nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_1D; + nir_def *index = libagx_g11_layered_index(b, desc, nir_pad_vec4(b, coord), + nir_imm_bool(b, is_1d)); + nir_def *coord2d = coords_for_buffer_texture(b, index); + + nir_src_rewrite(&intr->src[1], nir_pad_vector(b, coord2d, 4)); + nir_intrinsic_set_image_dim(intr, GLSL_SAMPLER_DIM_2D); + nir_intrinsic_set_image_array(intr, false); + + /* The 2D write is not clipped to the image, skip it */ + nir_instr_remove(&intr->instr); + nir_push_if(b, nir_ult_imm(b, nir_channel(b, coord, 0), 0x4000)); + nir_builder_instr_insert(b, &intr->instr); + nir_pop_if(b, NULL); + return true; +} + +bool +agx_nir_lower_g11_layered_image_store(nir_shader *s) +{ + return nir_shader_intrinsics_pass(s, lower_g11_layered_store, + nir_metadata_none, NULL); +} + bool agx_nir_lower_multisampled_image_store(nir_shader *s) { diff --git a/src/asahi/compiler/agx_nir_opt_preamble.c b/src/asahi/compiler/agx_nir_opt_preamble.c index 3eb80e95f7f..c8740d886c3 100644 --- a/src/asahi/compiler/agx_nir_opt_preamble.c +++ b/src/asahi/compiler/agx_nir_opt_preamble.c @@ -318,7 +318,7 @@ static const nir_opt_preamble_options preamble_options = { * hot constants so we don't end up rematerializing all over the place. * 480 seems to be a sweetspot, based on a few minutes of shader-db. */ - .preamble_storage_size[nir_preamble_class_general] = 480, + .preamble_storage_size[nir_preamble_class_general] = AGX_G11 ? 224 : 480, /* We have at least 32 texture state registers. TODO: check for more? */ .preamble_storage_size[nir_preamble_class_image] = 32, diff --git a/src/asahi/compiler/agx_nir_texture.h b/src/asahi/compiler/agx_nir_texture.h index d132c6707de..b9cf66204d5 100644 --- a/src/asahi/compiler/agx_nir_texture.h +++ b/src/asahi/compiler/agx_nir_texture.h @@ -8,6 +8,12 @@ /* Texture backend flags */ #define AGX_TEXTURE_FLAG_NO_CLAMP (1 << 0) +/* + * G11: texelFetch above level 0, which pixel coordinates cannot address (see + * g11_txf_to_txl), samples through this hardware sampler, normalized. + */ +#define AGX_G11_TXF_LOD_SAMPLER 15 + /* Indicates that the sampler should be overriden to clamp to 0 instead of 1 */ #define AGX_TEXTURE_FLAG_CLAMP_TO_0 (1 << 1) diff --git a/src/asahi/compiler/agx_opcodes.py b/src/asahi/compiler/agx_opcodes.py index f5afa4623e9..7cd478ae2df 100644 --- a/src/asahi/compiler/agx_opcodes.py +++ b/src/asahi/compiler/agx_opcodes.py @@ -378,7 +378,7 @@ def subgroup_op(name, opc): op("ldcf", (0xA1, 8, _), srcs = 1, imms = [CHANNELS]) op("st_vary", None, dests = 0, srcs = 2, can_eliminate = False) op("no_varyings", (0x80000051, 4, _), dests = 0, can_eliminate = False) -op("stop", (0x88, 2, _), dests = 0, can_eliminate = False, +op("stop", (0x0108, 2, _), dests = 0, can_eliminate = False, schedule_class = "invalid") op("trap", (0x08, 2, _), dests = 0, can_eliminate = False, schedule_class = "invalid") diff --git a/src/asahi/compiler/agx_opt_compact_constants.c b/src/asahi/compiler/agx_opt_compact_constants.c index 6baad35e8f6..309b8258f30 100644 --- a/src/asahi/compiler/agx_opt_compact_constants.c +++ b/src/asahi/compiler/agx_opt_compact_constants.c @@ -41,7 +41,11 @@ agx_opt_compact_constants(agx_context *ctx) /* Try to compact to f16 */ uint16_t compact = _mesa_float_to_half(uif(I->imm)); - if (I->imm == fui(_mesa_half_to_float(compact))) { + /* G11 flushes half denormal sources to zero */ + bool denorm = !(compact & 0x7c00) && (compact & 0x3ff); + + if (I->imm == fui(_mesa_half_to_float(compact)) && + !(AGX_G11 && denorm)) { I->dest[0].size = AGX_SIZE_16; I->imm = compact; BITSET_SET(replaced, v); diff --git a/src/asahi/compiler/agx_pack.c b/src/asahi/compiler/agx_pack.c index 6236c3cf4f3..d1e888ff410 100644 --- a/src/asahi/compiler/agx_pack.c +++ b/src/asahi/compiler/agx_pack.c @@ -553,6 +553,26 @@ agx_pack_alu(struct util_dynarray *emission, agx_instr *I) } } +/* G11 texture dimension, bits 9:8 of samples and writes. Arrays add a + * coordinate. + * + * Multisampled accesses are not known: Mesa never needs them, see + * agx_g11_ms_layers. + */ +static const uint8_t agx_g11_dims[] = { + [AGX_DIM_1D] = 0, [AGX_DIM_1D_ARRAY] = 0, [AGX_DIM_2D] = 1, + [AGX_DIM_2D_ARRAY] = 1, [AGX_DIM_2D_MS] = 1, [AGX_DIM_3D] = 2, + [AGX_DIM_CUBE] = 3, [AGX_DIM_CUBE_ARRAY] = 3, + [AGX_DIM_2D_MS_ARRAY] = 1, +}; + +static bool +agx_g11_array(enum agx_dim dim) +{ + return dim == AGX_DIM_1D_ARRAY || dim == AGX_DIM_2D_ARRAY || + dim == AGX_DIM_CUBE_ARRAY; +} + static void agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, agx_instr *I, enum u_tristate needs_g13x_coherency) @@ -577,6 +597,31 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, pack_assert(I, I->explicit_coords == (coords.type == AGX_INDEX_REGISTER)); unsigned C = I->explicit_coords ? coords.value : 0; + if (AGX_G11) { + /* + * From Apple's G11 fragment shaders lined up with the same shaders + * built for G13: opcode 0x09 / 0x79, the format in 9:7 (no + * conversions past S16NORM), the register in 16-bit halves at + * 15:10, the byte offset << 4 in byte 2, bit 49 for explicit + * coordinates (register in byte 4), bit 51 for loads, the mask in + * 55:52 and the sample mask in byte 7. The format sets the register + * size: 32-bit for I32, else 16-bit. + */ + pack_assert(I, I->format < AGX_FORMAT_RGB10A2); + pack_assert(I, (D >> 2) < 64 && I->pixel_offset < 16 && C < 0x100); + pack_assert(I, !St); + uint64_t raw = (load ? 0x79 : 0x09) | ((uint64_t)I->format << 7) | + ((uint64_t)(D >> 2) << 10) | + ((uint64_t)I->pixel_offset << 20) | + ((uint64_t)C << 32) | + (I->explicit_coords ? BITFIELD64_BIT(49) : 0) | + (load ? BITFIELD64_BIT(51) : 0) | + ((uint64_t)I->mask << 52) | ((uint64_t)S << 56); + + memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); + break; + } + uint64_t raw = agx_opcodes_info[I->op].encoding.exact | ((uint64_t)((D >> 1) & BITFIELD_MASK(7)) << 8) | (St << 22) | ((uint64_t)(I->format) << 24) | @@ -605,6 +650,22 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, ((T & BITFIELD_MASK(6)) << 9) | ((S & 0xff) << 16) | ((T >> 6) << 24) | ((S >> 8) << 26); + /* + * G11 moves the operands up as for zs_emit: T a bit, S two with its + * immediate flag in bit 17 (Apple's RenderBox discard, c1fe0603: + * immediate 255 and immediate 1). Where the high bits of an immediate + * S go is unknown, but at most 4 samples exist so the low ones do. + */ + if (AGX_G11) { + bool St = I->src[1].type == AGX_INDEX_IMMEDIATE; + unsigned Sv = I->src[1].value & (St ? 0xf : ~0); + + pack_assert(I, Sv < 64); + raw = 0xc1 | (Tt ? BITFIELD_BIT(9) : 0) | + ((T & BITFIELD_MASK(6)) << 10) | (St ? BITFIELD_BIT(17) : 0) | + (Sv << 18) | ((T >> 6) << 24); + } + unsigned size = 4; memcpy(util_dynarray_grow_bytes(emission, 1, size), &raw, size); break; @@ -662,6 +723,32 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, bool kill = false; // TODO: optimize bool forward = false; // TODO: optimize + if (AGX_G11) { + /* + * From Apple's G11 fragment shaders lined up with the same shaders + * built for G13 (QuartzCore, SiriUICore, MentalHealthUI): iter 0x11, + * iterproj 0x51 and ldcf 0x91, G13's opcodes minus 0x10 (0x21 is + * the texture sample on G11). The destination at bits 15:8, cf at + * 21:16, channels at 23:22, W's cf at 29:24 (always 0 in Apple's). + * Bit 31 adds G13's upper word: sample index and interpolation. + * Centroid never shows up in Apple's code, so its value is G13's. + * The upper word also holds the destination's high bits at 56, as + * on G13 (Apple's iter to r55). G13's 0x61 iterproj runs as a sample + * of a junk address and wedges the GPU. + */ + pack_assert(I, src_I.type == AGX_INDEX_IMMEDIATE); + pack_assert(I, D < 0x400 && cf_I < 64 && cf_J < 64); + bool L = interp != AGX_INTERPOLATION_CENTER || D >= 0x100; + uint64_t raw = (flat ? 0x91 : perspective ? 0x51 : 0x11) | + ((D & 0xff) << 8) | (cf_I << 16) | (channels << 22) | + (cf_J << 24) | (L ? BITFIELD_BIT(31) : 0) | + ((uint64_t)sample_index.value << 32) | + ((uint64_t)interp << 48) | ((uint64_t)(D >> 8) << 56); + + memcpy(util_dynarray_grow_bytes(emission, 1, L ? 8 : 4), &raw, L ? 8 : 4); + break; + } + uint64_t raw = 0x21 | (flat ? (1 << 7) : 0) | (perspective ? (1 << 6) : 0) | ((D & 0xFF) << 7) | (1ull << 15) | /* XXX */ @@ -688,6 +775,26 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, pack_assert(I, value.type == AGX_INDEX_REGISTER); pack_assert(I, value.size == AGX_SIZE_32); + if (AGX_G11) { + /* + * Apple's G11 vertex shaders: opcode 0x01, bit 8 set, the source + * register in 16-bit halves at bits 15:10 (ds.g11p@0x11540 stores + * x, y, z, w from r0, r2, r3, r1 as 0x00, 0x08, 0x0c, 0x04 in bits + * 15:9), the immediate index flag at bit 22. The G13 form is not + * an instruction there and the TA job never ends. The high bits of + * both sit where G13 has them: Apple's st_vary from r37 sets bit 24. + */ + pack_assert(I, index_src.type == AGX_INDEX_IMMEDIATE); + uint64_t raw = 0x01 | (I->last ? (1 << 7) : 0) | (1 << 8) | + ((value.value & 0x3F) << 10) | + (((uint64_t)(index_src.value & 0x3F)) << 16) | + (1 << 22) | ((value.value >> 6) << 24) | + ((index_src.value >> 6) << 26) | (0x8u << 28); + + memcpy(util_dynarray_grow_bytes(emission, 1, 4), &raw, 4); + break; + } + uint64_t raw = 0x11 | (I->last ? (1 << 7) : 0) | ((value.value & 0x3F) << 9) | (((uint64_t)(index_src.value & 0x3F)) << 16) | @@ -779,6 +886,27 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, unsigned O = agx_pack_local_index(I, is_load ? I->src[1] : I->src[2], &Ot); + if (AGX_G11) { + /* G11 has no separate threadgroup opcodes: it uses the device + * load/store layout with address space 2 in bits 28-29 and a + * 16-bit register base holding the byte address. + */ + pack_assert(I, At == 0 && Ot); + uint64_t raw = + (is_load ? 0x05 : 0x45) | ((I->format & BITFIELD_MASK(3)) << 7) | + ((R & BITFIELD_MASK(6)) << 10) | ((A & BITFIELD_MASK(4)) << 16) | + ((O & BITFIELD_MASK(4)) << 20) | (1 << 24) | (2 << 28) | + (I->scoreboard << 30) | + (((uint64_t)((O >> 4) & BITFIELD_MASK(4))) << 32) | + (((uint64_t)((A >> 4) & BITFIELD_MASK(4))) << 36) | + (((uint64_t)((R >> 6) & BITFIELD_MASK(2))) << 40) | + BITFIELD64_BIT(47) | (((uint64_t)(I->format >> 3)) << 48) | + (((uint64_t)Rt) << 49) | (((uint64_t)I->mask) << 52) | + (((uint64_t)(O >> 8)) << 56); + memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); + break; + } + uint64_t raw = agx_opcodes_info[I->op].encoding.exact | (Rt ? BITFIELD64_BIT(8) : 0) | ((R & BITFIELD_MASK(6)) << 9) | (L ? BITFIELD64_BIT(15) : 0) | @@ -827,6 +955,31 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, } case AGX_OPCODE_LOCAL_ATOMIC: { + if (AGX_G11) { + /* The device atomic with address space 2 in bits 28-29 and the + * byte address in a 16-bit register, as Apple's G11 code has it. + */ + bool Rt, Ot; + unsigned At; + unsigned R = agx_pack_atomic_dest(I, I->dest[0], &Rt); + unsigned S = agx_pack_atomic_source(I, I->src[0]); + unsigned A = agx_pack_local_base(I, I->src[1], &At); + unsigned O = agx_pack_local_index(I, I->src[2], &Ot); + + pack_assert(I, At == 0 && Ot); + uint64_t raw = + 0x15 | (((uint64_t)I->atomic_opc) << 6) | + ((R & BITFIELD_MASK(6)) << 10) | ((A & BITFIELD_MASK(4)) << 16) | + ((O & BITFIELD_MASK(4)) << 20) | (1 << 24) | (2 << 28) | + (I->scoreboard << 30) | (1u << 31) | + (((uint64_t)((O >> 4) & BITFIELD_MASK(4))) << 32) | + (((uint64_t)((A >> 4) & BITFIELD_MASK(4))) << 36) | + (((uint64_t)(R >> 6)) << 40) | (Rt ? BITFIELD64_BIT(47) : 0) | + (((uint64_t)S) << 48) | (((uint64_t)(O >> 8)) << 56); + memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); + break; + } + bool L = true; /* TODO: Don't force */ unsigned At; @@ -872,6 +1025,43 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, unsigned O = agx_pack_sample_compare_offset(I, I->src[5]); unsigned D = agx_pack_lod(I, I->src[1], &lod_mode); + if (AGX_G11) { + /* + * From Apple's G11 kernels lined up with the same kernels built for + * G13. Ten bytes, always a sample: loads come as samples at pixel + * centres through the txf sampler. + */ + pack_assert(I, Tt == 0 && T < 16 && !St && S < 16 && U == 0); + pack_assert(I, !Cs && (C & 1) == 0 && C < 128 && R < 256); + pack_assert(I, !I->query_lod); + pack_assert(I, !I->gather && !I->sparse && lod_mode < 8); + + bool array = agx_g11_array(I->dim); + uint64_t raw = + 0x21 | ((I->dim == AGX_DIM_3D || array) ? 0x80 : 0) | + (agx_g11_dims[I->dim] << 8) | + ((R & BITFIELD_MASK(6)) << 10) | (T << 16) | (S << 20) | + (Rt ? BITFIELD_BIT(24) : 0) | (((C >> 1) & BITFIELD_MASK(5)) << 25) | + (((uint64_t)I->scoreboard) << 31) | (((uint64_t)I->mask) << 32) | + (((uint64_t)lod_mode) << 40) | (0x18ull << 43) | + (((uint64_t)D & BITFIELD_MASK(8)) << 48) | BITFIELD64_BIT(63); + uint16_t raw2 = (R >> 6) | ((C >> 6) << 2) | BITFIELD_BIT(13); + + /* Texel offsets (bit 78, as in Apple's kernels) and depth compares + * (bit 79, found on the phone) take G13's operand register in place + * of the high register bits. + */ + if (I->offset || I->shadow) { + pack_assert(I, R < 64 && C < 64); + raw2 = O | BITFIELD_BIT(13) | (I->offset ? BITFIELD_BIT(14) : 0) | + (I->shadow ? BITFIELD_BIT(15) : 0); + } + + memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); + memcpy(util_dynarray_grow_bytes(emission, 1, 2), &raw2, 2); + break; + } + unsigned q1 = I->shadow; unsigned q2 = I->query_lod ? 2 : 0; unsigned q3 = 0xc; // XXX @@ -963,6 +1153,46 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, unsigned coherency = I->coherent ? 0xf : 0x9; + if (AGX_G11) { + /* + * Apple's kernels write with LOD 0, data in 32-bit registers. The + * texture state and the coordinate register (halves) are split in + * nibbles: state 23:20 and 35:32, coordinates 19:16 and 39:36, found + * by writing through each state on the phone. + */ + pack_assert(I, Tt == 0 && T < 256 && U == 0 && C < 256); + pack_assert(I, Rt && !Cs && Dt && D == 0); + + /* Arrays take the sample's 0x80 and two-byte tail with three + * coordinates. Without them a write to an array writes nothing, + * with them it writes and ends the thread (Vulkan avoids them, see + * lower_g11_layered_store). + */ + bool three = I->dim == AGX_DIM_3D || agx_g11_array(I->dim); + uint64_t raw = 0x31 | (three ? 0x80 : 0) | (agx_g11_dims[I->dim] << 8) | + ((R & BITFIELD_MASK(6)) << 10) | + ((C & 0xf) << 16) | ((T & 0xf) << 20) | (0x09u << 24) | + (((uint64_t)T >> 4) << 32) | (((uint64_t)C >> 4) << 36) | + (((uint64_t)coherency) << 44) | + (((uint64_t)I->dim) << 48); + + if (three) { + uint16_t raw2 = (R >> 6) | ((C >> 6) << 2) | (3 << 4); + + raw |= BITFIELD64_BIT(63); + memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); + memcpy(util_dynarray_grow_bytes(emission, 1, 2), &raw2, 2); + break; + } + + /* ponytail: Apple's kernels only write from r0-r31, so the high data + * register bits are a guess at G13's place. + */ + raw |= ((uint64_t)R >> 6) << 56; + memcpy(util_dynarray_grow_bytes(emission, 1, 8), &raw, 8); + break; + } + uint64_t raw = agx_opcodes_info[I->op].encoding.exact | (Rt ? (1 << 8) : 0) | ((R & BITFIELD_MASK(6)) << 9) | ((C & BITFIELD_MASK(6)) << 16) | (Ct ? (1 << 22) : 0) | @@ -1047,10 +1277,16 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, pack_assert(I, I->zs >= 1 && I->zs <= 3); + /* + * G11 moves S up a bit and T up two (Apple's RenderBox and QuartzCore + * depth writes: 41fe0823 and 41fe1023 where G13 has 417f0223 and + * 417f0423). + */ + unsigned s_shift = AGX_G11 ? 1 : 0, t_shift = AGX_G11 ? 2 : 0; uint32_t word0 = agx_opcodes_info[I->op].encoding.exact | - ((S.type == AGX_INDEX_IMMEDIATE) ? (1 << 8) : 0) | - ((S.value & BITFIELD_MASK(6)) << 9) | - ((T.value & BITFIELD_MASK(6)) << 16) | + ((S.type == AGX_INDEX_IMMEDIATE) ? (1 << (8 + s_shift)) : 0) | + ((S.value & BITFIELD_MASK(6)) << (9 + s_shift)) | + ((T.value & BITFIELD_MASK(6)) << (16 + t_shift)) | ((T.value >> 6) << 26) | ((S.value >> 6) << 24) | (I->zs << 29); diff --git a/src/asahi/compiler/agx_register_allocate.c b/src/asahi/compiler/agx_register_allocate.c index 87a0335a4a7..033f0083f7f 100644 --- a/src/asahi/compiler/agx_register_allocate.c +++ b/src/asahi/compiler/agx_register_allocate.c @@ -1042,6 +1042,186 @@ try_kill_early_sources(struct ra_ctx *rctx, const agx_instr *I, } } +/* + * G11 samples, image writes and tile accesses encode their data and + * coordinate registers in six bits (16-bit halves), so those must sit in + * r0-r31. Whether source s + * (-1 for the destination) is such an operand. + */ +#define G11_LOW_REGS 64 + +static bool +g11_low_operand(const agx_instr *I, int s) +{ + if (!AGX_G11) + return false; + + switch (I->op) { + case AGX_OPCODE_TEXTURE_SAMPLE: + case AGX_OPCODE_TEXTURE_LOAD: + case AGX_OPCODE_IMAGE_LOAD: + return s == -1 || s == 0; + case AGX_OPCODE_LD_TILE: + return s == -1; + case AGX_OPCODE_IMAGE_WRITE: + case AGX_OPCODE_ST_TILE: + return s == 0; + default: + return false; + } +} + +/* + * Move whatever lives in [base, base + n) to free registers elsewhere, + * recording the copies. Fails, changing nothing, if there is no room. + */ +static bool +g11_evict_range(struct ra_ctx *rctx, unsigned base, unsigned n, + struct util_dynarray *copies) +{ + BITSET_WORD *used = rctx->used_regs[RA_GPR]; + unsigned vars[G11_LOW_REGS], homes[G11_LOW_REGS], nr = 0; + bool ok = true; + + /* Occupants fully inside the range, since vectors are aligned to size */ + for (unsigned r = base; r < base + n; ++r) { + if (!BITSET_TEST(used, r)) + continue; + + unsigned var = rctx->reg_to_ssa[r]; + unsigned k = rctx->ncomps[var]; + if (rctx->ssa_to_reg[var] != r || k > n) + return false; + + vars[nr++] = var; + r += k - 1; + } + + /* Find homes outside the range, reserving as we go */ + BITSET_SET_COUNT(used, base, n); + + for (unsigned i = 0; i < nr && ok; ++i) { + ok = find_regs_simple(rctx, RA_GPR, rctx->ncomps[vars[i]], + rctx->ncomps[vars[i]], &homes[i]); + if (ok) + BITSET_SET_COUNT(used, homes[i], rctx->ncomps[vars[i]]); + else + nr = i; + } + + BITSET_CLEAR_COUNT(used, base, n); + + for (unsigned i = 0; i < nr; ++i) { + unsigned k = rctx->ncomps[vars[i]]; + + if (ok) { + insert_copy(rctx, copies, homes[i], vars[i]); + BITSET_CLEAR_COUNT(used, rctx->ssa_to_reg[vars[i]], k); + BITSET_SET_COUNT(used, homes[i], k); + set_ssa_to_reg(rctx, vars[i], homes[i]); + } else { + BITSET_CLEAR_COUNT(used, homes[i], k); + } + } + + return ok; +} + +/* + * Find an aligned range of n registers in r0-r31 avoiding the reserved + * registers and those in pinned, emptying it. Ranges with fewer occupants are + * tried first, up to max_used of them. Returns ~0 if impossible. + */ +static unsigned +g11_low_range(struct ra_ctx *rctx, unsigned n, const BITSET_WORD *pinned, + struct util_dynarray *copies, unsigned max_used) +{ + unsigned top = MIN2(G11_LOW_REGS, rctx->bound[RA_GPR]); + + for (unsigned want = 0; want <= MIN2(n, max_used); ++want) { + for (unsigned base = 0; base + n <= top; base += n) { + if (base < reserved_size(rctx->shader) || + BITSET_TEST_RANGE(pinned, base, base + n - 1)) + continue; + + unsigned nr_used = 0; + for (unsigned r = base; r < base + n; ++r) + nr_used += BITSET_TEST(rctx->used_regs[RA_GPR], r); + + if (nr_used == want && g11_evict_range(rctx, base, n, copies)) + return base; + } + } + + return ~0; +} + +static void +g11_emit_copies(struct ra_ctx *rctx, agx_instr *I, struct util_dynarray *copies) +{ + agx_builder b = agx_init_builder(rctx->shader, agx_before_instr(I)); + agx_emit_parallel_copies( + &b, copies->data, util_dynarray_num_elements(copies, struct agx_copy)); + util_dynarray_fini(copies); +} + +/* + * Move the low-register sources of a G11 sample or image write into r0-r31 + * before it. ponytail: if there is no room the operand stays high and reads + * the wrong register + */ +static void +g11_lower_sources(struct ra_ctx *rctx, agx_instr *I, BITSET_WORD *pinned) +{ + struct util_dynarray copies = UTIL_DYNARRAY_INIT; + + agx_foreach_ssa_src(I, s) { + if (!g11_low_operand(I, s) || I->src[s].memory) + continue; + + unsigned var = I->src[s].value; + unsigned old = rctx->ssa_to_reg[var], n = rctx->ncomps[var]; + + if (old + n <= G11_LOW_REGS) { + BITSET_SET_COUNT(pinned, old, n); + continue; + } + + unsigned reg = g11_low_range(rctx, n, pinned, &copies, n); + if (reg == ~0) + continue; + + insert_copy(rctx, &copies, reg, var); + BITSET_CLEAR_COUNT(rctx->used_regs[RA_GPR], old, n); + BITSET_SET_COUNT(rctx->used_regs[RA_GPR], reg, n); + BITSET_SET_COUNT(pinned, reg, n); + set_ssa_to_reg(rctx, var, reg); + } + + g11_emit_copies(rctx, I, &copies); +} + +/* + * Pick a destination in r0-r31 for a G11 sample, freeing one if needed. With + * sources killed early their registers count as free but are still read, so + * then only an empty range will do: copies could land in them. + */ +static unsigned +g11_low_dest(struct ra_ctx *rctx, agx_instr *I, unsigned reg, unsigned n, + const BITSET_WORD *pinned) +{ + if (reg + n <= G11_LOW_REGS) + return reg; + + struct util_dynarray copies = UTIL_DYNARRAY_INIT; + unsigned low = g11_low_range(rctx, n, pinned, &copies, + rctx->early_killed ? 0 : n); + + g11_emit_copies(rctx, I, &copies); + + return low == ~0 ? reg : low; +} + /** Assign registers to SSA values in a block. */ static void @@ -1123,6 +1303,11 @@ agx_ra_assign_local(struct ra_ctx *rctx) continue; } + BITSET_DECLARE(pinned, AGX_NUM_REGS) = {0}; + bool g11_low = g11_low_operand(I, -1) || g11_low_operand(I, 0); + if (g11_low) + g11_lower_sources(rctx, I, pinned); + /* Search for regions of contiguous killed sources to early-kill. */ rctx->early_killed = false; @@ -1155,7 +1340,22 @@ agx_ra_assign_local(struct ra_ctx *rctx) if (I->op == AGX_OPCODE_PHI && I->dest[d].has_reg) continue; - assign_regs(rctx, I->dest[d], pick_regs(rctx, I, d)); + unsigned reg = pick_regs(rctx, I, d); + + if (g11_low && g11_low_operand(I, -1)) { + BITSET_SET_COUNT(rctx->used_regs[RA_GPR], reg, + rctx->ncomps[I->dest[d].value]); + unsigned low = g11_low_dest(rctx, I, reg, + rctx->ncomps[I->dest[d].value], pinned); + BITSET_CLEAR_COUNT(rctx->used_regs[RA_GPR], reg, + rctx->ncomps[I->dest[d].value]); + if (low != reg) + BITSET_CLEAR_COUNT(rctx->used_regs[RA_GPR], low, + rctx->ncomps[I->dest[d].value]); + reg = low; + } + + assign_regs(rctx, I->dest[d], reg); } /* Free late-killed sources */ @@ -1451,8 +1651,11 @@ agx_ra(agx_context *ctx) enum agx_size *sizes = calloc(ctx->alloc, sizeof(enum agx_size)); BITSET_WORD *visited = BITSET_CALLOC(ctx->alloc); unsigned max_ncomps = 1; + bool g11_texture = false; agx_foreach_instr_global(ctx, I) { + g11_texture |= g11_low_operand(I, 0) || g11_low_operand(I, -1); + /* Record collects/phis so we can coalesce when assigning */ if (I->op == AGX_OPCODE_COLLECT || I->op == AGX_OPCODE_PHI || I->op == AGX_OPCODE_EXPORT || I->op == AGX_OPCODE_SPLIT) { @@ -1497,6 +1700,15 @@ agx_ra(agx_context *ctx) max_regs = ROUND_DOWN_TO(max_regs, reg_file_alignment); + /* + * G11 samples and image writes reach r0-r31 only: the sample tail holds + * the offset or compare register, not high register bits, and none of + * Apple's G11 samples go above r21. Keep such shaders in r0-r31, bigger + * ones move the operands down (g11_lower_sources). + */ + if (g11_texture && demand <= 64) + max_regs = MIN2(max_regs, 64); + /* Or, we can bound tightly for debugging */ if (agx_compiler_debug & AGX_DBG_DEMAND) max_regs = ALIGN_POT(MAX2(demand, 12), reg_file_alignment); diff --git a/src/asahi/drm-shim/asahi_noop.c b/src/asahi/drm-shim/asahi_noop.c index bfbd9c0394c..01e051eb562 100644 --- a/src/asahi/drm-shim/asahi_noop.c +++ b/src/asahi/drm-shim/asahi_noop.c @@ -9,12 +9,20 @@ #include "drm-shim/drm_shim.h" #include "drm-uapi/asahi_drm.h" +/* What the A12's agx kernel driver reports. */ static const struct drm_asahi_params_global params = { - .gpu_generation = 13, - .gpu_variant = 'G', - .gpu_revision = 0, + .gpu_generation = 11, + .gpu_variant = 'P', + .gpu_revision = 0x11, + .chip_id = 0x8020, + .num_dies = 1, + .num_clusters_total = 1, + .num_cores_per_cluster = 4, + .core_masks = {0xf}, .vm_start = 0x4000, - .vm_end = 0x7fffff8000ull, + .vm_end = 0x8000000000ull, + .vm_kernel_min_size = 0x4000, + .max_commands_per_submission = 64, .command_timestamp_frequency_hz = 1000000000, }; diff --git a/src/asahi/genxml/agx_g11.h b/src/asahi/genxml/agx_g11.h new file mode 100644 index 00000000000..7bb3f097adf --- /dev/null +++ b/src/asahi/genxml/agx_g11.h @@ -0,0 +1,15 @@ +/* + * Copyright 2026 kiki + * SPDX-License-Identifier: MIT + */ + +#pragma once + +/* + * Target the A12 GPU (G11). In the compiler: the stop instruction is 0x0801, + * threadgroup memory goes through the device load/store encoding, and there + * are no SIMD reductions or G13 cache barriers. In command streams: CDM + * block types sit in bits 31:30, and the USC shader, register and shared + * memory words are one word. cmdbuf.xml has the G11 encodings. + */ +#define AGX_G11 1 diff --git a/src/asahi/genxml/agx_pack_header.h b/src/asahi/genxml/agx_pack_header.h index 2f238f291c3..1c3294f95e0 100644 --- a/src/asahi/genxml/agx_pack_header.h +++ b/src/asahi/genxml/agx_pack_header.h @@ -8,6 +8,8 @@ #pragma once +#include "asahi/genxml/agx_g11.h" + #include "compiler/libcl/libcl.h" #include "util/bitpack_helpers.h" diff --git a/src/asahi/genxml/cmdbuf.xml b/src/asahi/genxml/cmdbuf.xml index c2979f9d58f..6dd0b714f97 100644 --- a/src/asahi/genxml/cmdbuf.xml +++ b/src/asahi/genxml/cmdbuf.xml @@ -221,24 +221,27 @@ + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -280,25 +283,26 @@ + - - - - - - - - - - - - - + + + + + + + + + + + + + + + - - @@ -637,17 +641,19 @@ + - + - + - - + + @@ -664,20 +670,22 @@ + - - - + + + - - - + + @@ -705,6 +713,17 @@ + + + + + + + + + + @@ -715,11 +734,12 @@ + - + @@ -996,12 +1016,15 @@ + - - - + + + @@ -1020,6 +1043,8 @@ + + diff --git a/src/asahi/isa/AGX2.xml b/src/asahi/isa/AGX2.xml index f3e6733725f..2fd210d1c28 100644 --- a/src/asahi/isa/AGX2.xml +++ b/src/asahi/isa/AGX2.xml @@ -1103,7 +1103,7 @@ - 0000000010001000 + 0000000100001000 diff --git a/src/asahi/layout/layout.c b/src/asahi/layout/layout.c index 4ff45296afe..a271a258f4f 100644 --- a/src/asahi/layout/layout.c +++ b/src/asahi/layout/layout.c @@ -3,6 +3,7 @@ * SPDX-License-Identifier: MIT */ +#include "asahi/genxml/agx_g11.h" #include "layout.h" #include "util/format/u_format.h" #include "util/u_math.h" @@ -35,6 +36,18 @@ ail_initialize_linear(struct ail_layout *layout) struct ail_tile ail_get_max_tile_size(unsigned blocksize_B) { + /* G11 has the oblong tiles the other way up (AGXMetalA12 + * Texture::sumLevelSizesTiled) + */ + if (AGX_G11) { + switch (blocksize_B) { + case 2: return (struct ail_tile){64, 128}; + case 8: return (struct ail_tile){32, 64}; + case 32: return (struct ail_tile){16, 32}; + default: break; + } + } + /* clang-format off */ switch (blocksize_B) { case 1: return (struct ail_tile) { 128, 128 }; diff --git a/src/asahi/layout/tiling.cc b/src/asahi/layout/tiling.cc index 084d7270793..1a0f52992b7 100644 --- a/src/asahi/layout/tiling.cc +++ b/src/asahi/layout/tiling.cc @@ -9,6 +9,7 @@ #include #include #include +#include "asahi/genxml/agx_g11.h" #include "util/macros.h" #include "layout.h" @@ -55,6 +56,20 @@ ail_space_mask(unsigned x) return MOD_POT(0x55555555, x * x); } +/* Spread the low bits of x over the set bits of mask */ +static uint32_t +ail_deposit(uint32_t x, uint32_t mask) +{ + uint32_t out = 0; + + for (uint32_t bit = 1; mask; bit <<= 1, mask &= mask - 1) { + if (x & bit) + out |= mask & -mask; + } + + return out; +} + template void memcpy_small(void *_tiled, void *_linear, const struct ail_layout *tiled_layout, @@ -74,12 +89,26 @@ memcpy_small(void *_tiled, void *_linear, const struct ail_layout *tiled_layout, struct ail_tile tile_size = tiled_layout->tilesize_el[level]; unsigned tile_area_el = tile_size.width_el * tile_size.height_el; unsigned tiles_per_row = DIV_ROUND_UP(stride_el, tile_size.width_el); - unsigned y_offs_el = ail_space_bits(MOD_POT(sy_el, tile_size.height_el)) - << 1; - unsigned x_offs_start_el = - ail_space_bits(MOD_POT(sx_el, tile_size.width_el)); unsigned space_mask_x = ail_space_mask(tile_size.width_el); unsigned space_mask_y = ail_space_mask(tile_size.height_el) << 1; + + /* G11 interleaves with Y in the low bit (checked on the phone), any extra + * bits of the long side of oblong tiles staying on top. + */ + if (AGX_G11) { + unsigned side = MIN2(tile_size.width_el, tile_size.height_el); + unsigned top = 2 * util_logbase2(side); + + space_mask_y = ail_space_mask(side) | + ((tile_size.height_el / side - 1) << top); + space_mask_x = (ail_space_mask(side) << 1) | + ((tile_size.width_el / side - 1) << top); + } + + unsigned y_offs_el = + ail_deposit(MOD_POT(sy_el, tile_size.height_el), space_mask_y); + unsigned x_offs_start_el = + ail_deposit(MOD_POT(sx_el, tile_size.width_el), space_mask_x); unsigned log2_tile_width_el = util_logbase2(tile_size.width_el); unsigned log2_tile_height_el = util_logbase2(tile_size.height_el); diff --git a/src/asahi/lib/agx_bg_eot.c b/src/asahi/lib/agx_bg_eot.c index 1cb8fab4775..3e1d5a94e39 100644 --- a/src/asahi/lib/agx_bg_eot.c +++ b/src/asahi/lib/agx_bg_eot.c @@ -123,7 +123,8 @@ agx_build_background_shader(struct agx_bg_eot_cache *cache, unsigned nr = util_format_get_nr_components(key->tib.logical_format[rt]); bool msaa = key->tib.nr_samples > 1; - bool layered = key->tib.layered; + /* G11 binds the render target's first layer, see hk_g11_layer_cs */ + bool layered = key->tib.layered && !AGX_G11; assert(nr > 0); nir_store_output( @@ -131,7 +132,9 @@ agx_build_background_shader(struct agx_bg_eot_cache *cache, nir_imm_int(&b, 0), .write_mask = BITFIELD_MASK(nr), .src_type = nir_type_uint32, .io_semantics.location = FRAG_RESULT_DATA0 + rt, - .io_semantics.num_slots = 1); + .io_semantics.num_slots = 1, + /* G11: a reload only fills the tilebuffer, see agx_nir_lower_tilebuffer */ + .io_semantics.fb_fetch_output = key->op[rt] == AGX_BG_LOAD); b.shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DATA0 + rt); } @@ -257,8 +260,12 @@ agx_get_precompiled_locked(struct agx_bg_eot_cache *cache, unsigned program) /* Bake launch */ agx_pack(&p->b.launch, CDM_LAUNCH_WORD_0, cfg) { - cfg.texture_state_register_count = 8 /* same encoding as 0 */; - cfg.sampler_state_register_count = 1; + /* G11 loads the counted states from USC address 0 when the USC has no + * texture or sampler words, and precompiled kernels push none. + */ + cfg.texture_state_register_count = AGX_G11 ? 0 : 8 /* same as 0 */; + cfg.sampler_state_register_count = + AGX_G11 ? AGX_SAMPLER_STATES_0 : AGX_SAMPLER_STATES_4_COMPACT; cfg.uniform_register_count = info->push_count; cfg.preshader_register_count = info->nr_preamble_gprs; } @@ -269,9 +276,13 @@ agx_get_precompiled_locked(struct agx_bg_eot_cache *cache, unsigned program) agx_usc_immediates(&b, &info->rodata, p->ptr); - if (info->uses_txf) + if (info->uses_txf && !AGX_G11) agx_usc_push_packed(&b, SAMPLER, cache->dev->txf_sampler); +#if AGX_G11 + agx_usc_shader_g11(&b, agx_usc_addr(cache->dev, p->ptr + info->main_offset), + info->nr_gprs, info->local_size); +#else agx_usc_shared(&b, info->local_size, info->imageblock_stride, 0); agx_usc_pack(&b, SHADER, cfg) { @@ -283,6 +294,7 @@ agx_get_precompiled_locked(struct agx_bg_eot_cache *cache, unsigned program) cfg.register_count = info->nr_gprs; cfg.spill_size = 0; } +#endif if (info->nr_preamble_gprs) { agx_usc_pack(&b, PRESHADER, cfg) { diff --git a/src/asahi/lib/agx_device.c b/src/asahi/lib/agx_device.c index 04d21f857c1..7a31dbfb4a3 100644 --- a/src/asahi/lib/agx_device.c +++ b/src/asahi/lib/agx_device.c @@ -541,7 +541,7 @@ agx_open_device(void *memctx, struct agx_device *dev) } assert(params_size >= sizeof(dev->params)); - assert(dev->params.gpu_generation >= 13); + assert(dev->params.gpu_generation >= (AGX_G11 ? 11 : 13)); const char *variant = " Unknown"; switch (dev->params.gpu_variant) { case 'G': @@ -557,10 +557,15 @@ agx_open_device(void *memctx, struct agx_device *dev) variant = " Ultra"; break; } - snprintf(dev->name, sizeof(dev->name), "Apple M%d%s (G%d%c %02X)", - dev->params.gpu_generation - 12, variant, - dev->params.gpu_generation, dev->params.gpu_variant, - dev->params.gpu_revision + 0xA0); + if (dev->params.gpu_generation == 11) { + snprintf(dev->name, sizeof(dev->name), "Apple A12 (G11%c %02X)", + dev->params.gpu_variant, dev->params.gpu_revision); + } else { + snprintf(dev->name, sizeof(dev->name), "Apple M%d%s (G%d%c %02X)", + dev->params.gpu_generation - 12, variant, + dev->params.gpu_generation, dev->params.gpu_variant, + dev->params.gpu_revision + 0xA0); + } /* We need a large chunk of VA space carved out for robustness. Hardware * loads can shift an i32 by up to 2, for a total shift of 4. If the base @@ -647,7 +652,10 @@ agx_open_device(void *memctx, struct agx_device *dev) simple_mtx_init(&dev->vma_lock, mtx_plain); util_vma_heap_init(&dev->main_heap, user_start, user_size); - util_vma_heap_init(&dev->usc_heap, dev->shader_base, shader_size); + /* G11: keep USC address 0 free, jobs with bad state fetch from there. */ + uint64_t usc_skip = AGX_G11 ? 16384 : 0; + util_vma_heap_init(&dev->usc_heap, dev->shader_base + usc_skip, + shader_size - usc_skip); /* Capture heap overlap with main heap and is allocated in reverse */ util_vma_heap_init(&dev->capture_heap, user_start, user_size); diff --git a/src/asahi/lib/agx_helpers.h b/src/asahi/lib/agx_helpers.h index bf7dfb9a6da..818b19fcb91 100644 --- a/src/asahi/lib/agx_helpers.h +++ b/src/asahi/lib/agx_helpers.h @@ -11,6 +11,7 @@ #include "agx_abi.h" #include "agx_pack.h" #include "agx_ppp.h" +#include "agx_uvs.h" #include "libagx_shaders.h" #define AGX_MAX_OCCLUSION_QUERIES (32768) @@ -54,6 +55,24 @@ agx_pack_txf_sampler(struct agx_sampler_packed *out) cfg.wrap_t = AGX_WRAP_CLAMP_TO_BORDER; cfg.wrap_r = AGX_WRAP_CLAMP_TO_BORDER; cfg.border_colour = AGX_BORDER_COLOUR_TRANSPARENT_BLACK; + + /* G11 loads sample at pixel centres, see agx_g11_pixel_centre */ + cfg.pixel_coordinates = AGX_G11; + } +} + +/* G11 AGX_G11_TXF_LOD_SAMPLER, see agx_nir_texture.h */ +static inline void +agx_pack_txf_lod_sampler(struct agx_sampler_packed *out) +{ + agx_pack(out, SAMPLER, cfg) { + cfg.minimum_lod = 0.0; + cfg.maximum_lod = INFINITY; + cfg.mip_filter = AGX_MIP_FILTER_NEAREST; + cfg.wrap_s = AGX_WRAP_CLAMP_TO_BORDER; + cfg.wrap_t = AGX_WRAP_CLAMP_TO_BORDER; + cfg.wrap_r = AGX_WRAP_CLAMP_TO_BORDER; + cfg.border_colour = AGX_BORDER_COLOUR_TRANSPARENT_BLACK; } } @@ -139,6 +158,117 @@ agx_pack_zls_control(struct agx_zls_control_packed *packed, agx_translate_zls_tiling(s->tiling); } } + + /* AGXMetalA12's Framebuffer sets bits 62 and 63 with any stencil */ + if (AGX_G11 && s) + packed->opaque[1] |= BITFIELD_RANGE(30, 2); +} + +/* + * G11 has no partial renders (the kernel does not set them up), so a render + * whose geometry fills the 8 MB tiler heap hangs the TA. Drivers end the + * render, and split draws, before that. Measured: 14 B per point and 32 B + * per tiny triangle, plus about 0.6 B per 32x32 tile a primitive covers. + * + * ponytail: a CPU estimate against a fixed budget, with indirect draws at a + * flat cost; firmware partial renders are the real fix + */ +#define AGX_G11_HEAP_BUDGET (6u << 20) +#define AGX_G11_HEAP_INDIRECT (AGX_G11_HEAP_BUDGET / 64) + +static inline uint64_t +agx_g11_heap_cost(uint64_t vertices, unsigned outputs) +{ + return vertices * (4 * outputs + 8); +} + +/* Most vertices in one piece of a split draw, a multiple of 6 for any list */ +static inline uint32_t +agx_g11_split_max(unsigned instances, unsigned outputs) +{ + uint64_t per = agx_g11_heap_cost(MAX2(instances, 1), outputs); + return MAX2((AGX_G11_HEAP_BUDGET / 2) / per / 6 * 6, 6); +} + +/* G11 PBE strides keep the level count in the low bits */ +#define AGX_PBE_STRIDE_BIAS (AGX_G11 ? 16 : 4) + +/* + * G11 vertex state token header (VDMEncoderGen2_1::VsStateToken): 0x8040 + * rasterization off, 2:1 the provoking vertex, and a class for the vertex + * output count. + */ +static inline uint32_t +agx_g11_vs_header(bool discard, unsigned provoking, unsigned outputs) +{ + uint32_t class = outputs <= 8 ? 0x4000 + : outputs <= 24 ? 0x2000 + : outputs <= 32 ? 0x1800 + : outputs <= 64 ? 0x1000 + : outputs <= 128 ? 0x800 + : 0x400; + + return 0x48000000 | (discard ? 0x8040 : 0) | (provoking << 1) | class; +} + +/* + * G11 output select. Bits 31:24 are the point size's output word plus one: at + * 4 points take position.w as their size, at 0 they are not drawn. + */ +static inline uint32_t +agx_g11_osel(const struct agx_unlinked_uvs_layout *uvs, uint32_t fs_osel) +{ + uint32_t osel = uvs->osel.opaque[0] | fs_osel | BITFIELD_BIT(16); + + /* Clip distances need it too: the first plane is read from there. + * + * ponytail: only the first plane clips; where G11 reads the others is + * not known + */ + if (uvs->group_offs[UVS_LAYER_VIEWPORT] > uvs->group_offs[UVS_PSIZ] || + uvs->size > uvs->group_offs[UVS_CLIP_DIST]) + osel |= (uvs->group_offs[UVS_PSIZ] + 1) << 24; + + return osel; +} + +/* + * G11 indexed draws follow a restart token (SampledRenderContext:: + * drawIndexedPrimitives): the vertex state header as class 0x50, bit 5 to + * enable, then the index. + */ +static inline uint8_t * +agx_g11_restart(uint8_t *out, uint32_t vs_header, bool restart, uint32_t index) +{ + uint32_t *w = (uint32_t *)out; + + w[0] = 0x50000000 | (vs_header & 0x7ffffdf) | (restart ? BITFIELD_BIT(5) : 0); + w[1] = restart ? index : ~0; + return out + 8; +} + +/* G11 pipeline word register count: 8s in 5:2, 0 for 128 */ +static inline uint32_t +agx_g11_regs(unsigned nr_gprs) +{ + return (DIV_ROUND_UP(nr_gprs, 16) & 15) << 2; +} + +/* + * Counts words (VDM vertex word 0, PPP fragment word 0, background/end of + * tile): the generic packer cannot express a count of 0, and G11 then loads + * state from USC address 0 (a preshader, or texture and sampler states the + * pipeline does not provide). Apple writes 0 for unused fields; compute + * clears the preshader count the same way in agx_cdm_launch(). + */ +static inline void +agx_g11_clear_counts(uint32_t *counts, bool textures, bool samplers) +{ + *counts &= ~BITFIELD_RANGE(12, 4); + if (!textures) + *counts &= ~BITFIELD_RANGE(4, 5); + if (!samplers) + *counts &= ~BITFIELD_RANGE(9, 3); } static enum agx_sample_count diff --git a/src/asahi/lib/agx_linker.c b/src/asahi/lib/agx_linker.c index 445b939f777..ed4010a121b 100644 --- a/src/asahi/lib/agx_linker.c +++ b/src/asahi/lib/agx_linker.c @@ -40,6 +40,12 @@ static const uint8_t sample_loop_header[] = { 0x62, 0x04, 0x01, 0x00, }; +#if AGX_G11 +/* G11 stop is 0x0801; pad with more */ +#define STOP \ + 0x01, 0x08, 0x01, 0x08, 0x01, 0x08, 0x01, 0x08, 0x01, 0x08, \ + 0x01, 0x08, 0x01, 0x08, 0x01, 0x08, 0x01, 0x08, +#else #define STOP \ /* stop */ \ 0x88, 0x00, \ @@ -47,6 +53,7 @@ static const uint8_t sample_loop_header[] = { /* trap */ \ 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, \ 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, +#endif static const uint8_t stop[] = {STOP}; @@ -153,6 +160,8 @@ agx_fast_link(struct agx_linked_shader *linked, struct agx_device *dev, } if (nr_samples_shaded > 1) { + /* The loop is G13 code */ + assert(!AGX_G11 && "G11 sample shading"); assert(sample_loop_footer[SAMPLE_LOOP_FOOTER_COUNT_PATCH_OFFS] == 0); /* Make a stack copy of the footer so we can efficiently patch it */ @@ -181,6 +190,8 @@ agx_fast_link(struct agx_linked_shader *linked, struct agx_device *dev, assert(offset == size); + linked->nr_gprs = nr_gprs; + agx_pack(&linked->shader, USC_SHADER, cfg) { cfg.code = agx_usc_addr(dev, linked->bo->va->addr); cfg.unk_2 = fragment ? 2 : 3; @@ -212,6 +223,8 @@ agx_fast_link(struct agx_linked_shader *linked, struct agx_device *dev, linked->no_op = true; } + linked->reads_tib = reads_tib; + agx_pack(&linked->fragment_control, FRAGMENT_CONTROL, cfg) { cfg.tag_write_disable = tag_write_disable; cfg.disable_tri_merging = disable_tri_merging; diff --git a/src/asahi/lib/agx_linker.h b/src/asahi/lib/agx_linker.h index 419dccf1b9b..7ace67f2d2a 100644 --- a/src/asahi/lib/agx_linker.h +++ b/src/asahi/lib/agx_linker.h @@ -30,6 +30,12 @@ struct agx_linked_shader { /* Whether the program is a no-op and can be skipped. */ bool no_op; + /* Whether the fragment program reads the render targets */ + bool reads_tib; + + /* Register count, for G11's combined shader word */ + unsigned nr_gprs; + /* Coefficient register bindings */ struct agx_varyings_fs cf; @@ -257,6 +263,9 @@ struct agx_fs_epilog_key { enum pipe_format rt_formats[8]; uint8_t nr_samples; bool force_small_tile; + + /* G11: keep the tilebuffer copy of render targets, see agx_tilebuffer.c */ + bool g11_tib; }; void agx_nir_vs_prolog(struct nir_builder *b, const void *key_); diff --git a/src/asahi/lib/agx_nir_lower_tilebuffer.c b/src/asahi/lib/agx_nir_lower_tilebuffer.c index e20a3caf6be..02e467740f2 100644 --- a/src/asahi/lib/agx_nir_lower_tilebuffer.c +++ b/src/asahi/lib/agx_nir_lower_tilebuffer.c @@ -68,6 +68,17 @@ store_tilebuffer(nir_builder *b, struct agx_tilebuffer_layout *tib, value = nir_u2u16(b, value); } + /* G11 tile accesses take 16-bit registers below I32, and store F16 as + * I16: its F16 does not round trip. + */ + if (AGX_G11 && format != PIPE_FORMAT_R32_UINT && value->bit_size == 32) { + value = util_format_is_pure_integer(logical_format) ? nir_u2u16(b, value) + : nir_f2f16(b, value); + } + + if (AGX_G11 && format == PIPE_FORMAT_R16_FLOAT) + format = PIPE_FORMAT_R16_UINT; + if (!samples) samples = nir_imm_intN_t(b, ALL_SAMPLES, 16); @@ -103,6 +114,21 @@ load_tilebuffer(nir_builder *b, struct agx_tilebuffer_layout *tib, unsigned comps = util_format_get_nr_components(logical_format); bool f16 = (format == PIPE_FORMAT_R16_FLOAT); + /* G11 tile accesses take 16-bit registers below I32 */ + if (AGX_G11 && format != PIPE_FORMAT_R32_UINT && bit_size == 32) { + bool integer = util_format_is_pure_integer(logical_format); + nir_def *res = nir_load_local_pixel_agx( + b, MIN2(load_comps, comps), 16, nir_imm_intN_t(b, ALL_SAMPLES, 16), + .base = agx_tilebuffer_offset_B(tib, rt), + .format = f16 ? PIPE_FORMAT_R16_UINT : format); + + res = integer ? nir_u2u32(b, res) : nir_f2f32(b, res); + if (f16) + res = nir_fsat_to_format(b, res, logical_format); + res = nir_sign_extend_if_sint(b, res, logical_format); + return nir_pad_vector(b, res, load_comps); + } + /* Don't load with F16 */ if (f16) format = PIPE_FORMAT_R16_UINT; @@ -166,11 +192,89 @@ image_coords(nir_builder *b) return nir_vector_insert_imm(b, xy__, nir_load_layer_id(b), 2); } +/* + * G11 has no bindless texture state: reach the render target's descriptors + * through their state registers (texture at base + 2 rt, PBE after it). + * They are bound as 2D (see agx_upload_spilled_rt_descriptors), and a G11 + * access with another dimension does nothing. The image write takes 32-bit + * data. + * + * Multisampled render targets are 2D arrays of a layer per sample + * (agx_g11_ms_layers): read as arrays, written as one tall 2D image, a layer + * under the other. + * + * ponytail: one layer, single-sampled writes rely on the PBE's bounds + */ +static nir_def * +g11_rt_coords(nir_builder *b) +{ + return nir_pad_vec4(b, nir_u2u32(b, nir_load_pixel_coord(b))); +} + +static nir_def * +g11_rt_sample(nir_builder *b, unsigned nr_samples, nir_def *samples, + nir_def **cond) +{ + nir_def *sample; + dim_for_rt(b, nr_samples, &sample); + + nir_def *coverage = nir_load_sample_mask(b); + if (samples != NULL) + coverage = nir_iand(b, coverage, nir_u2u32(b, samples)); + + *cond = nir_ine_imm( + b, nir_ubitfield_extract(b, coverage, nir_u2u32(b, sample), nir_imm_int(b, 1)), + 0); + return nir_u2u32(b, sample); +} + static void store_memory(nir_builder *b, unsigned bindless_base, unsigned nr_samples, enum pipe_format format, unsigned rt, nir_def *value, nir_def *samples) { + if (AGX_G11) { + if (value->bit_size == 16) { + value = util_format_is_pure_sint(format) ? nir_i2i32(b, value) + : util_format_is_pure_uint(format) ? nir_u2u32(b, value) + : nir_f2f32(b, value); + } + + nir_def *coords = g11_rt_coords(b); + nir_def *cond = NULL; + + if (nr_samples > 1) { + nir_def *sample = g11_rt_sample(b, nr_samples, samples, &cond); + nir_def *h = nir_channel( + b, + nir_txs(b, .texture_index = bindless_base + (2 * rt), + .dim = GLSL_SAMPLER_DIM_2D, .is_array = true, + .dest_type = nir_type_int32), + 1); + + /* Background programs run for the whole tile, past the bottom edge + * into the next sample's layer + */ + nir_def *y = nir_channel(b, coords, 1); + cond = nir_iand(b, cond, nir_ult(b, y, h)); + coords = nir_vector_insert_imm( + b, coords, nir_iadd(b, y, nir_imul(b, sample, h)), 1); + } + + nir_begin_invocation_interlock(b); + if (cond) + nir_push_if(b, cond); + + nir_image_store(b, nir_imm_intN_t(b, bindless_base + (2 * rt) + 1, 16), + coords, nir_undef(b, 1, 32), value, + nir_imm_intN_t(b, 0, 16), .image_dim = GLSL_SAMPLER_DIM_2D, + .format = format, .src_type = nir_type_uint32); + + if (cond) + nir_pop_if(b, NULL); + return; + } + nir_def *image = handle_for_rt(b, bindless_base, rt, true); nir_def *tex_image = handle_for_rt(b, bindless_base, rt, false); nir_def *zero = nir_imm_intN_t(b, 0, 16); @@ -230,6 +334,39 @@ load_memory(nir_builder *b, unsigned bindless_base, unsigned nr_samples, uint8_t comps, uint8_t bit_size, unsigned rt, enum pipe_format format) { + if (AGX_G11) { + nir_def *coords = g11_rt_coords(b); + + /* Background programs run for the whole tile: clamp to the image, a + * linear one ends right after its last row (read fault on 1D images). + * They are internal and lower the txs later. A GL shader reading the + * framebuffer is in bounds and gets no texture lowering after this. + */ + if (b->shader->info.internal) { + nir_def *size = nir_txs(b, .texture_index = bindless_base + (2 * rt), + .dim = GLSL_SAMPLER_DIM_2D, + .is_array = nr_samples > 1, + .dest_type = nir_type_int32); + nir_def *max = nir_iadd_imm(b, nir_trim_vector(b, size, 2), -1); + coords = nir_pad_vec4( + b, nir_umin(b, nir_trim_vector(b, coords, 2), max)); + } + + if (nr_samples > 1) { + nir_def *sample; + dim_for_rt(b, nr_samples, &sample); + coords = nir_vector_insert_imm(b, coords, nir_u2u32(b, sample), 2); + } + + nir_begin_invocation_interlock(b); + return nir_image_load(b, comps, bit_size, + nir_imm_int(b, bindless_base + (2 * rt)), coords, + nir_undef(b, 1, 32), nir_imm_int(b, 0), + .image_dim = GLSL_SAMPLER_DIM_2D, + .image_array = nr_samples > 1, .format = format, + .access = ACCESS_IN_BOUNDS); + } + nir_def *image = handle_for_rt(b, bindless_base, rt, false); nir_def *zero = nir_imm_intN_t(b, 0, 16); nir_def *lod = zero; @@ -314,7 +451,16 @@ pass(nir_builder *b, nir_intrinsic_instr *intr, void *data) /* Trim to format as required by hardware */ value = nir_trim_vector(b, intr->src[0].ssa, comps); - if (tib->spilled[rt]) { + /* G11 keeps a copy in the tilebuffer for blending */ + if (tib->g11_tib[rt]) { + store_tilebuffer(b, tib, format, logical_format, rt, value, + ctx->write_samples, write_mask); + } + + /* Background reloads (fb_fetch_output, agx_bg_eot.c) only fill G11's + * tilebuffer: writing memory back would race the draws. + */ + if (tib->spilled[rt] && !(tib->g11_tib[rt] && sem.fb_fetch_output)) { store_memory(b, ctx->bindless_base, tib->nr_samples, logical_format, rt, value, ctx->write_samples); ctx->any_memory_stores = true; @@ -331,6 +477,11 @@ pass(nir_builder *b, nir_intrinsic_instr *intr, void *data) */ if (logical_format == PIPE_FORMAT_NONE) { def = nir_undef(b, intr->num_components, bit_size); + } else if (tib->g11_tib[rt]) { + *(ctx->translucent) = true; + + def = load_tilebuffer(b, tib, intr->num_components, bit_size, rt, + format, logical_format); } else if (tib->spilled[rt]) { *(ctx->translucent) = true; diff --git a/src/asahi/lib/agx_nir_lower_uvs.c b/src/asahi/lib/agx_nir_lower_uvs.c index 75d3c0d00ca..166a8d2203e 100644 --- a/src/asahi/lib/agx_nir_lower_uvs.c +++ b/src/asahi/lib/agx_nir_lower_uvs.c @@ -129,6 +129,17 @@ write_layer_viewport_sysval(struct ctx *ctx) nir_imm_int(&b, ctx->layout->group_offs[UVS_LAYER_VIEWPORT])); } +static bool +drop_layer(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + if (intr->intrinsic != nir_intrinsic_store_output || + nir_intrinsic_io_semantics(intr).location != VARYING_SLOT_LAYER) + return false; + + nir_instr_remove(&intr->instr); + return true; +} + static bool gather_components(nir_builder *b, nir_intrinsic_instr *intr, void *data) { @@ -160,6 +171,15 @@ agx_nir_lower_uvs(nir_shader *s, struct agx_unlinked_uvs_layout *layout) NIR_PASS(progress, s, nir_lower_io_to_scalar, nir_var_shader_out, NULL, NULL); + /* G11 layered rendering is not known and the kernel takes one layer, so + * everything lands on layer 0. + */ + if (AGX_G11) { + NIR_PASS(progress, s, nir_shader_intrinsics_pass, drop_layer, + nir_metadata_control_flow, NULL); + s->info.outputs_written &= ~VARYING_BIT_LAYER; + } + /* Determine the unlinked UVS layout */ NIR_PASS(progress, s, nir_shader_intrinsics_pass, gather_components, nir_metadata_control_flow, layout); @@ -310,7 +330,7 @@ agx_link_varyings_vs_fs(void *out, struct agx_varyings_vs *vs, unsigned provoking_vertex, uint8_t sprite_coord_enable, bool *generate_primitive_id) { - assert(fs->nr_bindings > 0); + assert(fs->nr_bindings > 0 || AGX_G11); *generate_primitive_id = false; diff --git a/src/asahi/lib/agx_nir_prolog_epilog.c b/src/asahi/lib/agx_nir_prolog_epilog.c index f523e4439e1..ee78e301dd9 100644 --- a/src/asahi/lib/agx_nir_prolog_epilog.c +++ b/src/asahi/lib/agx_nir_prolog_epilog.c @@ -426,6 +426,9 @@ agx_nir_fs_epilog(nir_builder *b, const void *key_) if (key->force_small_tile) tib.tile_size = 16 * 16; + if (!key->g11_tib) + agx_tilebuffer_drop_g11_tib(&tib); + bool force_translucent = false; nir_lower_blend_options opts = { .scalar_blend_const = true, diff --git a/src/asahi/lib/agx_ppp.h b/src/asahi/lib/agx_ppp.h index 2a7f710ef05..5095e20dd4c 100644 --- a/src/asahi/lib/agx_ppp.h +++ b/src/asahi/lib/agx_ppp.h @@ -41,7 +41,8 @@ agx_ppp_update_size(struct AGX_PPP_HEADER *present) size += present->viewport_count * AGX_REGION_CLIP_LENGTH; if (present->viewport) { - size += AGX_VIEWPORT_CONTROL_LENGTH + + /* G11 has no viewport control word (AGXMetalA12 setViewports) */ + size += (AGX_G11 ? 0 : AGX_VIEWPORT_CONTROL_LENGTH) + (present->viewport_count * AGX_VIEWPORT_LENGTH); } @@ -144,6 +145,29 @@ agx_ppp_fini(uint8_t **out, struct agx_ppp_update *ppp) *out += AGX_PPP_STATE_LENGTH; } +/* + * G11 takes PPP state in blocks of one state group each, as AGXMetalA12's + * RenderContext::encodeAndEmitRenderState emits it. Header bits: + * fragment control, front face, front stencil, back face, back stencil 0-4, + * depth bias/scissor 5, fragment shader 7-8, region clip 10, viewport 11, + * viewport count 15:12, W clamp 16, output select 17, varying counts 32 + * and 16 18-19, cull 21. T holds count words. + */ +static inline void +agx_g11_ppp_emit(uint8_t **out, struct agx_ptr T, const uint32_t *words, + unsigned count) +{ + memcpy(T.cpu, words, count * 4); + + agx_pack(*out, PPP_STATE, cfg) { + cfg.pointer_hi = T.gpu >> 32; + cfg.pointer_lo = (uint32_t)T.gpu; + cfg.size_words = count; + } + + *out += AGX_PPP_STATE_LENGTH; +} + static void agx_ppp_push_merged_blobs(struct agx_ppp_update *ppp, size_t length, void *src1_, void *src2_) diff --git a/src/asahi/lib/agx_tilebuffer.c b/src/asahi/lib/agx_tilebuffer.c index f79180d2674..80fe4ef05c9 100644 --- a/src/asahi/lib/agx_tilebuffer.c +++ b/src/asahi/lib/agx_tilebuffer.c @@ -5,6 +5,7 @@ #include "agx_tilebuffer.h" #include +#include #include "util/format/u_format.h" #include "layout.h" @@ -21,6 +22,24 @@ /* Minimum tile size in pixels, architectural. */ #define MIN_TILE_SIZE_PX (16 * 16) +/* + * Formats G11's tile accesses handle, see agx_pack.c. F16 goes through as I16. + * With I32 even plain clears came out wrong; Apple's G11 blending only uses + * I16 and U8NORM. + */ +static bool +agx_g11_tib_format(enum pipe_format format) +{ + switch ((enum ail_isa_format)format) { + case AIL_ISA_FORMAT_I16: + case AIL_ISA_FORMAT_F16: + case AIL_ISA_FORMAT_U8NORM: + return true; + default: + return false; + } +} + /* Select the largest tile size that fits */ static uint16_t agx_select_tile_size(unsigned px_size_B) @@ -85,6 +104,20 @@ agx_build_tilebuffer_layout(const enum pipe_format *formats, uint8_t nr_cbufs, bool fits = (new_offset_B <= MAX_BYTES_PER_SAMPLE) && (ALIGN_POT(new_offset_B, 8) * MIN_TILE_SIZE_PX * nr_samples) <= MAX_BYTES_PER_TILE; + /* + * G11: keep render targets in memory. The block store encoding is not + * known, so background and fragment programs write with image stores + * and the end of tile program does nothing. Single-sampled targets + * that ld_tile can convert also go in the tilebuffer, which blends + * read. + */ + if (AGX_G11) { + tib.spilled[rt] = true; + fits &= nr_samples == 1 && offset_B < 16 && + agx_g11_tib_format(phys); + tib.g11_tib[rt] = fits; + } + if (fits) { tib._offset_B[rt] = offset_B; offset_B = new_offset_B; @@ -108,6 +141,23 @@ agx_build_tilebuffer_layout(const enum pipe_format *formats, uint8_t nr_cbufs, return tib; } +/* + * G11: keep render targets in memory only, without the tilebuffer copy. The + * copy costs every fragment a tile store, so GL turns it on only once a draw + * reads its render targets (agx_draw_vbo). + */ +void +agx_tilebuffer_drop_g11_tib(struct agx_tilebuffer_layout *tib) +{ + if (!agx_g11_tib_size_B(tib)) + return; + + memset(tib->g11_tib, 0, sizeof(tib->g11_tib)); + tib->sample_size_B = 0; + tib->tile_size = agx_select_tile_size(0); + agx_tilebuffer_pack_usc(tib); +} + /* * With attachmentless rendering in Vulkan, the sample count may not known until * draw-time. It's convenient to construct an agx_tilebuffer_layout anyway when diff --git a/src/asahi/lib/agx_tilebuffer.h b/src/asahi/lib/agx_tilebuffer.h index 49017f51309..c78baa98abe 100644 --- a/src/asahi/lib/agx_tilebuffer.h +++ b/src/asahi/lib/agx_tilebuffer.h @@ -35,6 +35,11 @@ struct agx_tilebuffer_layout { /* Which render targets are spilled. */ bool spilled[AGX_MAX_RENDER_TARGETS]; + /* G11: spilled render targets also kept in the tilebuffer, for blending. + * Fragment programs write both, see agx_nir_lower_tilebuffer.c. + */ + bool g11_tib[AGX_MAX_RENDER_TARGETS]; + /* Offset into the sample of each render target. If a render target is * spilled, its offset is UNDEFINED. Use agx_tilebuffer_offset_B to access. */ @@ -74,11 +79,36 @@ static inline uint8_t agx_tilebuffer_offset_B(struct agx_tilebuffer_layout *layout, unsigned rt) { assert(rt < AGX_MAX_RENDER_TARGETS); - assert(!layout->spilled[rt] && "precondition"); + assert((!layout->spilled[rt] || layout->g11_tib[rt]) && "precondition"); return layout->_offset_B[rt]; } +/* G11 bytes per pixel in the tilebuffer, 0 when only memory is used */ +static inline unsigned +agx_g11_tib_size_B(const struct agx_tilebuffer_layout *layout) +{ + for (unsigned rt = 0; rt < AGX_MAX_RENDER_TARGETS; ++rt) { + if (layout->g11_tib[rt]) + return layout->sample_size_B; + } + + return 0; +} + +/* G11: whether every render target is also in the tilebuffer */ +static inline bool +agx_g11_tib_all(const struct agx_tilebuffer_layout *layout) +{ + for (unsigned rt = 0; rt < AGX_MAX_RENDER_TARGETS; ++rt) { + if (layout->logical_format[rt] != PIPE_FORMAT_NONE && + !layout->g11_tib[rt]) + return false; + } + + return true; +} + static inline bool agx_tilebuffer_spills(struct agx_tilebuffer_layout *layout) { @@ -94,6 +124,8 @@ struct agx_tilebuffer_layout agx_build_tilebuffer_layout(const enum pipe_format *formats, uint8_t nr_cbufs, uint8_t nr_samples, bool layered); +void agx_tilebuffer_drop_g11_tib(struct agx_tilebuffer_layout *tib); + void agx_tilebuffer_set_samples(struct agx_tilebuffer_layout *tib, unsigned nr_samples); diff --git a/src/asahi/lib/agx_usc.h b/src/asahi/lib/agx_usc.h index 123208a9d82..5394d98162b 100644 --- a/src/asahi/lib/agx_usc.h +++ b/src/asahi/lib/agx_usc.h @@ -65,6 +65,54 @@ agx_usc_shared(struct agx_usc_builder *b, uint16_t local_size, } } +#if AGX_G11 +/* G11: the shader with its registers and threadgroup memory, in one word. */ +static inline void +agx_usc_shader_g11(struct agx_usc_builder *b, uint32_t code, unsigned nr_gprs, + unsigned shared_size) +{ + agx_usc_pack(b, SHADER_G11, cfg) { + cfg.code = code; + cfg.register_count = nr_gprs; + cfg.uses_shared_memory = shared_size > 0; + cfg.bytes_per_threadgroup = shared_size ? shared_size + 16 : 65536; + } +} + +/* + * G11 fragment programs describe the tilebuffer in the same word, as Metal's + * LoadShader::setFragmentImageBlockData does: bytes per pixel in 8-byte units + * at 35:32, the tile layout at 31:26 (47 for 32x32 tiles, 63 for 32x16, 54 + * for 16x16), and the tile's bytes in 256-byte units at 23:16 with bit 8. + * sample_size_B is 0 without a tilebuffer. + * + * ponytail: single sample only, as agx_build_tilebuffer_layout allows + */ +static inline void +agx_usc_shader_g11_frag(struct agx_usc_builder *b, uint32_t code, + unsigned nr_gprs, unsigned sample_size_B, + unsigned tile_size) +{ + void *word = b->head; + uint64_t w; + + agx_usc_shader_g11(b, code, nr_gprs, 0); + memcpy(&w, word, 8); + w &= ~(BITFIELD64_BIT(8) | BITFIELD64_RANGE(16, 20)); + + unsigned layout = tile_size == 32 * 32 ? 47 : tile_size == 32 * 16 ? 63 : 54; + unsigned size_B = ALIGN_POT(sample_size_B, 8) * tile_size; + + w |= (uint64_t)layout << 26; + if (size_B) { + w |= BITFIELD64_BIT(8) | ((uint64_t)DIV_ROUND_UP(size_B, 256) << 16) | + ((uint64_t)((sample_size_B / 8) & 0xf) << 32); + } + + memcpy(word, &w, 8); +} +#endif + static inline void agx_usc_immediates(struct agx_usc_builder *b, const struct agx_rodata *ro, uint64_t base_addr) diff --git a/src/asahi/lib/decode.c b/src/asahi/lib/decode.c index 3d1ccb0eda0..76c83086a41 100644 --- a/src/asahi/lib/decode.c +++ b/src/asahi/lib/decode.c @@ -27,7 +27,8 @@ static void agx_disassemble(void *_code, size_t maxlen, FILE *fp) { bool errors = agx2_disassemble(_code, maxlen, fp); - assert(!errors); + /* The ISA description lacks G11's threadgroup memory encodings. */ + assert(!errors || AGX_G11); } FILE *agxdecode_dump_stream; @@ -309,6 +310,20 @@ agxdecode_usc(struct agxdecode_ctx *ctx, const uint8_t *map, return AGX_USC_##name##_LENGTH; \ } +#if AGX_G11 + if ((map[0] & 0xf) == 0xf) { + agx_unpack(agxdecode_dump_stream, map, USC_SHADER_G11, ctrl); + DUMP_UNPACKED(USC_SHADER_G11, ctrl, "Shader\n"); + + void *buf; + size_t size = + agxdecode_grab_mapped(ctx, decode_usc(ctx, ctrl.code), &buf); + + agx_disassemble(buf, size, agxdecode_dump_stream); + return AGX_USC_SHADER_G11_LENGTH; + } +#endif + switch (type) { case AGX_USC_CONTROL_NO_PRESHADER: { DUMP_CL(USC_NO_PRESHADER, map, "No preshader"); @@ -441,6 +456,24 @@ agxdecode_record(struct agxdecode_ctx *ctx, uint64_t va, size_t size, agxdecode_fetch_gpu_array(ctx, va, buf); + if (AGX_G11) { + fprintf(agxdecode_dump_stream, "PPP %zu words:", size / 4); + for (unsigned i = 0; i < size / 4; ++i) + fprintf(agxdecode_dump_stream, " %08x", ((uint32_t *)buf)[i]); + fprintf(agxdecode_dump_stream, "\n"); + + /* G11 blocks (agx_g11_ppp): follow the fragment shader block */ + uint32_t *w = (uint32_t *)buf; + if (size == 20 && w[0] == 0x180) { + uint32_t samplers = (w[1] >> 9) & 7; + agxdecode_stateful(ctx, decode_usc(ctx, w[2] & ~0x3fu), + "Fragment pipeline", agxdecode_usc, verbose, params, + &samplers); + } + + return; + } + agx_unpack(agxdecode_dump_stream, map, PPP_HEADER, hdr); map += AGX_PPP_HEADER_LENGTH; @@ -475,8 +508,10 @@ agxdecode_record(struct agxdecode_ctx *ctx, uint64_t va, size_t size, return; } - DUMP_CL(VIEWPORT_CONTROL, map, "Viewport control"); - map += AGX_VIEWPORT_CONTROL_LENGTH; + if (!AGX_G11) { + DUMP_CL(VIEWPORT_CONTROL, map, "Viewport control"); + map += AGX_VIEWPORT_CONTROL_LENGTH; + } for (unsigned i = 0; i < hdr.viewport_count; ++i) { DUMP_CL(VIEWPORT, map, "Viewport"); @@ -654,6 +689,22 @@ agxdecode_vdm(struct agxdecode_ctx *ctx, const uint8_t *map, uint64_t *link, } case AGX_VDM_BLOCK_TYPE_VDM_STATE_UPDATE: { + /* G11's restart token is class 0x50, its vertex state token 0x48 */ + if (AGX_G11 && (*(const uint32_t *)map >> 27) == 0xa) { + const uint32_t *w = (const uint32_t *)map; + fprintf(agxdecode_dump_stream, "G11 restart %08x %08x\n", w[0], w[1]); + return 8; + } + + if (AGX_G11) { + const uint32_t *w = (const uint32_t *)map; + fprintf(agxdecode_dump_stream, "G11 vertex state %08x %08x %08x %08x\n", + w[0], w[1], w[2], w[3]); + agxdecode_stateful(ctx, decode_usc(ctx, w[2] & ~0x3fu), "Pipeline", + agxdecode_usc, verbose, params, NULL); + return 16; + } + size_t length = AGX_VDM_STATE_LENGTH; agx_unpack(agxdecode_dump_stream, map, VDM_STATE, hdr); map += AGX_VDM_STATE_LENGTH; @@ -700,6 +751,17 @@ agxdecode_vdm(struct agxdecode_ctx *ctx, const uint8_t *map, uint64_t *link, } case AGX_VDM_BLOCK_TYPE_INDEX_LIST: { + if (AGX_G11) { + const uint32_t *w = (const uint32_t *)map; + unsigned n = 1 + util_bitcount(w[0] & BITFIELD_RANGE(25, 4)) + + ((w[0] & BITFIELD_BIT(23)) ? 2 : 0); + fprintf(agxdecode_dump_stream, "G11 draw:"); + for (unsigned i = 0; i < n; ++i) + fprintf(agxdecode_dump_stream, " %08x", w[i]); + fprintf(agxdecode_dump_stream, "\n"); + return n * 4; + } + size_t length = AGX_INDEX_LIST_LENGTH; agx_unpack(agxdecode_dump_stream, map, INDEX_LIST, hdr); DUMP_UNPACKED(INDEX_LIST, hdr, "Index List\n"); diff --git a/src/asahi/libagx/libagx_dgc.h b/src/asahi/libagx/libagx_dgc.h index d8266b66311..45270bab020 100644 --- a/src/asahi/libagx/libagx_dgc.h +++ b/src/asahi/libagx/libagx_dgc.h @@ -234,6 +234,15 @@ agx_cdm_launch(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_grid grid, agx_merge(launch, mode, CDM_LAUNCH_WORD_0); #endif +#if AGX_G11 + /* Leave the preshader count clear, as Apple's driver does. The mode sits + * one bit up, in 29:28 (ComputeProgramVariant's launch templates: 1, + * 0x10000001 and 0x20000001 for direct, indirect global and local). + */ + launch.opaque[0] = + (launch.opaque[0] & BITFIELD_MASK(12)) | ((uint32_t)grid.mode << 28); +#endif + agx_push_packed(out, launch, CDM_LAUNCH_WORD_0); agx_push(out, CDM_LAUNCH_WORD_1, cfg) { @@ -269,10 +278,56 @@ agx_cdm_launch(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_grid grid, return out; } +#if AGX_G11 +/* + * G11 draw header, from Apple's PrimitiveTypeToVDMCTRLType tables: the + * presence bits sit elsewhere (count, instances, start 25-27, index buffer + * 28, indirect 23; no index buffer size). The index buffer's low word comes + * first (SampledRenderContext::drawIndexedPrimitives). Non-indexed draws + * set index size 2 as Apple does. Restart goes in a token before the draw. + */ +static inline GLOBAL uint32_t * +agx_vdm_draw_g11(GLOBAL uint32_t *out, struct agx_draw draw, + enum agx_primitive topology) +{ + uint64_t ib = draw.indexed ? agx_draw_index_buffer(draw) : 0; + uint32_t hdr = (3u << 29) | ((uint32_t)topology << 8); + + if (agx_is_indirect(draw.b)) + hdr |= 1u << 23; + else + hdr |= 7u << 25; + + if (draw.indexed) { + hdr |= (1u << 28) | (uint32_t)(ib >> 32) | + ((uint32_t)draw.index_size << 17); + } else { + hdr |= 2u << 17; + } + + *(out++) = hdr; + if (draw.indexed) + *(out++) = ib; + if (agx_is_indirect(draw.b)) { + *(out++) = draw.b.ptr >> 32; + *(out++) = draw.b.ptr & BITFIELD_MASK(32); + } else { + *(out++) = draw.b.count[0]; + *(out++) = draw.b.count[1]; + *(out++) = draw.indexed ? draw.index_bias : draw.start; + } + + return out; +} +#endif + static inline GLOBAL uint32_t * agx_vdm_draw(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_draw draw, enum agx_primitive topology) { +#if AGX_G11 + return agx_vdm_draw_g11(out, draw, topology); +#endif uint64_t ib = draw.indexed ? agx_draw_index_buffer(draw) : 0; agx_push(out, INDEX_LIST, cfg) { @@ -333,6 +388,10 @@ agx_vdm_draw(GLOBAL uint32_t *out, enum agx_chip chip, struct agx_draw draw, static inline uint32_t agx_vdm_draw_size(enum agx_chip chip, struct agx_draw draw) { +#if AGX_G11 + /* Must match agx_vdm_draw_g11 exactly: a gap would be parsed as words */ + return 4 + (draw.indexed ? 4 : 0) + (agx_is_indirect(draw.b) ? 8 : 12); +#endif uint32_t size = AGX_INDEX_LIST_LENGTH; if (agx_is_indirect(draw.b)) { @@ -371,6 +430,16 @@ agx_vdm_barrier(GLOBAL uint32_t *out, enum agx_chip chip) static inline GLOBAL uint32_t * agx_cdm_barrier(GLOBAL uint32_t *out, enum agx_chip chip) { +#if AGX_G11 + /* 0xc0000160, as Apple's G11 driver emits between launches. */ + agx_push(out, CDM_BARRIER, cfg) { + cfg.unk_5 = true; + cfg.unk_6 = true; + cfg.unk_8 = true; + } + return out; +#endif + agx_push(out, CDM_BARRIER, cfg) { cfg.unk_5 = true; cfg.unk_6 = true; @@ -581,6 +650,7 @@ agx_usc_uniform(struct agx_usc_builder *b, unsigned start_halfs, assert((start_halfs + size_halfs) <= (1 << 9) && "uniform file overflow"); assert(size_halfs <= 64 && "caller's responsibility to split"); assert(size_halfs > 0 && "no empty uniforms"); + assert(!(AGX_G11 && (start_halfs & BITFIELD_BIT(8))) && "no G11 high uniforms"); if (start_halfs & BITFIELD_BIT(8)) { agx_usc_pack(b, UNIFORM_HIGH, cfg) { diff --git a/src/asahi/libagx/texture.cl b/src/asahi/libagx/texture.cl index f6bf75a8cae..090b2b0cac9 100644 --- a/src/asahi/libagx/texture.cl +++ b/src/asahi/libagx/texture.cl @@ -32,12 +32,8 @@ libagx_txs(constant struct agx_texture_packed *ptr, uint16_t lod, uint3 size = (uint3)(d.width, d.height, d.depth); lod += d.first_level; - /* Linear 2D arrays are special. - * - * TODO: Optimize this, since linear 2D arrays aren't needed for APIs and - * this just gets used internally for blits. - */ - if (is_2d && is_array && d.layout == AGX_LAYOUT_LINEAR) + /* Linear arrays are special. G11 images use them, 1D included. */ + if ((is_1d || is_2d) && is_array && d.layout == AGX_LAYOUT_LINEAR) size.z = d.depth_linear; /* 1D Arrays have their second component as the layer count */ @@ -133,7 +129,15 @@ libagx_twiddle_coordinates(ushort2 coord, uint16_t tile_w_px, uint32_t tile_mask = upsample(tile_mask_vec.y, tile_mask_vec.x); uint32_t coord_xy = upsample(coord.y, coord.x); ushort2 offs_px = as_ushort2(coord_xy & tile_mask); +#if AGX_G11 + /* Y in the low bit, extra bits of oblong tiles on top (ail tiling.cc) */ + uint side = min(tile_w_px, tile_h_px); + uint32_t offset_within_tile_px = + nir_interleave(offs_px.y & (side - 1), offs_px.x & (side - 1)) + + ((offs_px.x | offs_px.y) / side) * side * side; +#else uint32_t offset_within_tile_px = nir_interleave(offs_px.x, offs_px.y); +#endif /* Get the coordinates of the corner of the tile */ ushort2 tile_px = as_ushort2(coord_xy & ~tile_mask); @@ -204,6 +208,36 @@ libagx_image_texel_address(constant const struct agx_pbe_packed *ptr, (uint64_t)total_sa * bytes_per_sample_B; } +/* + * Texel index for G11 writes to arrays, 3D and cubes, which go through a 2D + * buffer-like PBE (hk_image_view.c). Log2 of the texel size is in bits 145:147. + */ +uint +libagx_g11_layered_index(constant const struct agx_pbe_packed *ptr, uint4 coord, + bool is_1d) +{ + agx_unpack(NULL, ptr, PBE, d); + + uint log2_B = (((constant uint *)ptr)[4] >> 17) & 7; + uint px = is_1d ? coord.x + : libagx_twiddle_coordinates(convert_ushort2(coord.xy), + d.tile_width_sw, d.tile_height_sw, + d.aligned_width_msaa_sw); + + return px + coord[is_1d ? 1 : 2] * (d.layer_stride_sw >> log2_B); +} + +uint64_t +libagx_g11_layered_address(constant const struct agx_pbe_packed *ptr, + uint4 coord, bool is_1d) +{ + agx_unpack(NULL, ptr, PBE, d); + + uint log2_B = (((constant uint *)ptr)[4] >> 17) & 7; + uint index = libagx_g11_layered_index(ptr, coord, is_1d); + return d.buffer + ((uint64_t)index << log2_B); +} + uint64_t libagx_buffer_texel_address(constant const struct agx_pbe_packed *ptr, uint4 coord, uint bytes_per_pixel_B) diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index b527e97a886..f4ff25543a6 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -5,6 +5,7 @@ * SPDX-License-Identifier: MIT */ #include "hk_cmd_buffer.h" +#include "agx_nir_texture.h" #include "agx_abi.h" #include "agx_bo.h" @@ -306,8 +307,9 @@ hk_EndCommandBuffer(VkCommandBuffer commandBuffer) hk_cmd_buffer_end_compute(cmd); hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.post_gfx); + /* G11's CDM stream link is not known, so its streams stay apart */ struct hk_device *dev = hk_cmd_buffer_device(cmd); - if (likely(!(dev->dev.debug & AGX_DBG_NOMERGE))) { + if (likely(!(dev->dev.debug & AGX_DBG_NOMERGE)) && !AGX_G11) { merge_control_streams(cmd); } @@ -592,6 +594,41 @@ hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd, return root_ptr.gpu; } +/* Add to the 36-bit, 16 byte unit address at bit start of a descriptor */ +static void +hk_g11_add_address(void *desc, unsigned start, uint64_t offset_B) +{ + uint64_t *w = (uint64_t *)desc + 1; + unsigned shift = start - 64; + uint64_t mask = BITFIELD64_MASK(36) << shift; + uint64_t addr = (*w & mask) + ((offset_B >> 4) << shift); + + *w = (*w & ~mask) | (addr & mask); +} + +/* + * Texture and PBE descriptors of a spilled render target, at the layer of + * the G11 render being recorded (hk_g11_layer_cs). Each layer is a render of + * its own, so the descriptors stay 2D. + */ +void +hk_rt_descs(struct hk_cmd_buffer *cmd, unsigned rt, + struct agx_texture_packed out[2]) +{ + struct hk_rendering_state *render = &cmd->state.gfx.render; + struct hk_image_view *iview = render->color_att[rt].iview; + + memcpy(&out[0], &iview->planes[0].emrt_texture, sizeof(out[0])); + memcpy(&out[1], &iview->planes[0].emrt_pbe, sizeof(out[1])); + + if (AGX_G11 && render->g11_layer) { + uint64_t offset_B = render->g11_layer * render->g11_rt_stride_B[rt]; + + hk_g11_add_address(&out[0], 66, offset_B); + hk_g11_add_address(&out[1], 64, offset_B); + } +} + void hk_usc_upload_spilled_rt_descs(struct agx_usc_builder *b, struct hk_cmd_buffer *cmd) @@ -614,12 +651,24 @@ hk_usc_upload_spilled_rt_descs(struct agx_usc_builder *b, continue; } - memcpy(&desc[(i * 2) + 0], &iview->planes[0].emrt_texture, sizeof(*desc)); - memcpy(&desc[(i * 2) + 1], &iview->planes[0].emrt_pbe, sizeof(*desc)); + hk_rt_descs(cmd, i, &desc[i * 2]); } desc = descs.cpu; + /* G11 has no bindless access: bind them as texture state from 0, read + * through the txf sampler (agx_nir_lower_tilebuffer.c) + */ + if (AGX_G11) { + agx_usc_pack(b, TEXTURE, cfg) { + cfg.start = 0; + cfg.count = 2 * render->color_att_count; + cfg.buffer = descs.gpu; + } + + return; + } + /* Bind the base as u0_u1 for bindless access */ agx_usc_uniform(b, 0, 4, hk_pool_upload(cmd, &descs.gpu, 8, 8)); } @@ -662,6 +711,120 @@ hk_reserve_scratch(struct hk_cmd_buffer *cmd, struct hk_cs *cs, } } +#if AGX_G11 +struct hk_g11_counts +hk_g11_counts(struct hk_cmd_buffer *cmd, struct hk_shader *s, bool spilled) +{ + unsigned rts = spilled ? 2 * cmd->state.gfx.render.color_att_count : 0; + + return (struct hk_g11_counts){ + .textures = s->info.g11_nr_tex + rts, + .samplers = + (s->info.g11_nr_samp || s->b.info.uses_txf || rts) ? 16 : 0, + }; +} + +/* Descriptor in a bound set, or NULL past its end (unrolled arrays) */ +static const void * +hk_g11_desc(struct hk_descriptor_state *desc, struct hk_g11_state st, + unsigned size_B) +{ + struct hk_descriptor_set *set = desc->sets[st.set]; + const uint8_t *base = set ? set->mapped_ptr + : desc->push[st.set] ? desc->push[st.set]->data + : NULL; + unsigned end = set ? set->size : HK_PUSH_DESCRIPTOR_SET_SIZE; + + return base && st.offset_B + size_B <= end ? base + st.offset_B : NULL; +} + +/* + * G11 has no bindless textures: copy the descriptors the shader uses (see + * hk_g11_lower_handles) into its texture and sampler state, spilled render + * targets after the textures. Sampler 0 is the txf sampler and 15 the + * txf-lod one, as in the GL driver. Vulkan forbids updating a bound set, so + * copying at record time is safe without update-after-bind. + */ +static void +hk_g11_bind_state(struct hk_cmd_buffer *cmd, struct agx_usc_builder *b, + struct hk_shader *s, struct hk_descriptor_state *desc, + bool spilled) +{ + struct hk_rendering_state *render = &cmd->state.gfx.render; + struct hk_g11_counts n = hk_g11_counts(cmd, s, spilled); + + if (n.textures) { + struct agx_ptr T = hk_pool_alloc(cmd, AGX_TEXTURE_LENGTH * n.textures, 64); + if (!T.cpu) + return; + + struct agx_texture_packed *out = T.cpu; + memset(out, 0, AGX_TEXTURE_LENGTH * n.textures); + + /* Unwritten descriptors are zero and would read address 0, which + * wedges G11: they get the null texture instead + */ + static const struct agx_texture_packed zero; + for (unsigned i = 0; i < s->info.g11_nr_tex; ++i) { + const void *d = + hk_g11_desc(desc, s->info.g11_tex[i], AGX_TEXTURE_LENGTH); + if (d && memcmp(d, &zero, AGX_TEXTURE_LENGTH)) + memcpy(&out[i], d, AGX_TEXTURE_LENGTH); + else + agx_set_null_texture(&out[i]); + } + + for (unsigned rt = 0; spilled && rt < render->color_att_count; ++rt) { + struct hk_image_view *iview = render->color_att[rt].iview; + struct agx_texture_packed *o = &out[s->info.g11_nr_tex + 2 * rt]; + + if (iview) { + hk_rt_descs(cmd, rt, o); + } else { + agx_set_null_texture(o); + agx_set_null_pbe((struct agx_pbe_packed *)(o + 1)); + } + } + + agx_usc_pack(b, TEXTURE, cfg) { + cfg.start = 0; + cfg.count = n.textures; + cfg.buffer = T.gpu; + } + + /* The epilog reads spilled render target sizes from these descriptors + * through u0_u1, as on G13 (hk_usc_upload_spilled_rt_descs) + */ + if (spilled) + agx_usc_uniform(b, 0, 4, hk_pool_upload(cmd, &T.gpu, 8, 8)); + } + + if (n.samplers) { + struct agx_ptr T = hk_pool_alloc(cmd, AGX_SAMPLER_LENGTH * 16, 64); + if (!T.cpu) + return; + + struct agx_sampler_packed *out = T.cpu; + memset(out, 0, AGX_SAMPLER_LENGTH * 16); + agx_pack_txf_sampler(&out[0]); + agx_pack_txf_lod_sampler(&out[AGX_G11_TXF_LOD_SAMPLER]); + + for (unsigned i = 0; i < s->info.g11_nr_samp; ++i) { + const void *d = + hk_g11_desc(desc, s->info.g11_samp[i], AGX_SAMPLER_LENGTH); + if (d) + memcpy(&out[1 + i], d, AGX_SAMPLER_LENGTH); + } + + agx_usc_pack(b, SAMPLER, cfg) { + cfg.start = 0; + cfg.count = 16; + cfg.buffer = T.gpu; + } + } +} +#endif + uint32_t hk_upload_usc_words(struct hk_cmd_buffer *cmd, struct hk_shader *s, struct hk_linked_shader *linked) @@ -675,7 +838,7 @@ hk_upload_usc_words(struct hk_cmd_buffer *cmd, struct hk_shader *s, unsigned stage_ranges = 3; size_t usc_size = - agx_usc_size(constant_push_ranges + push_ranges + stage_ranges + 4); + agx_usc_size(constant_push_ranges + push_ranges + stage_ranges + 6); struct agx_ptr t = hk_pool_usc_alloc(cmd, usc_size, 64); if (!t.cpu) return 0; @@ -726,7 +889,9 @@ hk_upload_usc_words(struct hk_cmd_buffer *cmd, struct hk_shader *s, root_unif = AGX_ABI_VUNI_COUNT_VK(count); } else if (sw_stage == MESA_SHADER_FRAGMENT) { - if (agx_tilebuffer_spills(&cmd->state.gfx.render.tilebuffer)) { + /* G11 binds them with the textures below */ + if (agx_tilebuffer_spills(&cmd->state.gfx.render.tilebuffer) && + !AGX_G11) { hk_usc_upload_spilled_rt_descs(&b, cmd); } @@ -741,13 +906,31 @@ hk_upload_usc_words(struct hk_cmd_buffer *cmd, struct hk_shader *s, * * TODO: We probably could with some refactor. */ - agx_usc_push_packed(&b, SHARED, &cmd->state.gfx.render.tilebuffer.usc); + /* G11 has it in the shader word, which depends on the render */ + struct agx_tilebuffer_layout *tib = &cmd->state.gfx.render.tilebuffer; + if (AGX_G11) { + agx_usc_shader_g11_frag( + &b, agx_usc_addr(&dev->dev, linked->b.bo->va->addr), + linked->b.nr_gprs, agx_g11_tib_size_B(tib), tib->tile_size); + } else { + agx_usc_push_packed(&b, SHARED, &tib->usc); + } root_unif = AGX_ABI_FUNI_ROOT; } /* Address for the root and each set */ agx_usc_uniform(&b, root_unif, 4 * (1 + s->info.set_count), root_ptr); +#if AGX_G11 + hk_g11_bind_state( + cmd, &b, s, + hk_get_descriptors_state(cmd, sw_stage == MESA_SHADER_COMPUTE + ? VK_PIPELINE_BIND_POINT_COMPUTE + : VK_PIPELINE_BIND_POINT_GRAPHICS), + sw_stage == MESA_SHADER_FRAGMENT && + agx_tilebuffer_spills(&cmd->state.gfx.render.tilebuffer)); +#endif + agx_usc_push_blob(&b, linked->usc.data, linked->usc.size); return agx_usc_addr(&dev->dev, t.gpu); } @@ -795,6 +978,20 @@ hk_cs_init_graphics(struct hk_cmd_buffer *cmd, struct hk_cs *cs) cs->cr.eot.main = render->cr.eot.partial; cs->cr.zls_control = render->cr.zls_control_partial; + /* A layer of a G11 layered render, see hk_g11_layer_cs */ + if (render->g11_layers) { + unsigned l = render->g11_layer; + + if (cs->cr.depth.buffer) + cs->cr.depth.buffer += l * render->g11_z_stride_B; + + if (cs->cr.stencil.buffer) + cs->cr.stencil.buffer += l * render->g11_s_stride_B; + + cs->cr.layers = 1; + render->g11_cs[l] = cs; + } + /* Barrier to enforce GPU-CPU coherency, in case this batch is back to back * with another that caused stale data to be cached and the CPU wrote to it * in the meantime. @@ -803,6 +1000,15 @@ hk_cs_init_graphics(struct hk_cmd_buffer *cmd, struct hk_cs *cs) cfg.usc_cache_inval = true; } + /* G11 sends the W clamp with every draw, see hk_g11_flush_ppp_state */ + if (AGX_G11) { + cs->current = map; + cs->scissor = UTIL_DYNARRAY_INIT; + cs->depth_bias = UTIL_DYNARRAY_INIT; + hk_cmd_buffer_dirty_all(cmd); + return; + } + struct AGX_PPP_HEADER present = { .w_clamp = true, .occlusion_query_2 = true, @@ -860,7 +1066,12 @@ hk_ensure_cs_has_space(struct hk_cmd_buffer *cmd, struct hk_cs *cs, /* Otherwise, we need to allocate a new command buffer. We use memory owned * by the batch to simplify lifetime management for the BO. + * + * ponytail: G11's CDM link is not known (VDM's works), so a compute stream + * past 64 KiB (about 20 dispatches) breaks; end the stream there instead + * if a command buffer needs that many */ + assert(!(AGX_G11 && !vdm) && "G11 CDM stream link"); size_t size = 65536; struct agx_ptr T = hk_pool_alloc(cmd, size, 256); diff --git a/src/asahi/vulkan/hk_cmd_buffer.h b/src/asahi/vulkan/hk_cmd_buffer.h index 6a06060a963..f7db4d4a483 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.h +++ b/src/asahi/vulkan/hk_cmd_buffer.h @@ -100,6 +100,11 @@ struct hk_root_descriptor_table { uint16_t rasterization_stream; uint16_t _pad6; + /* G11: layer of the render this draw is recorded into, see + * hk_g11_layer_cs + */ + uint32_t g11_layer; + /* Mapping from varying slots written by the last vertex stage to UVS * indices. This mapping must be compatible with the fragment shader. */ @@ -201,6 +206,17 @@ struct hk_rendering_state { struct agx_tilebuffer_layout tilebuffer; struct hk_render_registers cr; + + /* G11 has no layered rendering: a layered or multiview render is one + * render per layer, each draw recorded into all of them (hk_g11_layer_cs). + * g11_layers is 0 otherwise. + */ + uint32_t g11_layers, g11_layer; + uint64_t g11_rt_stride_B[HK_MAX_RTS], g11_z_stride_B, g11_s_stride_B; + struct hk_cs *g11_cs[HK_G11_MAX_LAYERS]; + + /* G11: render targets have a tilebuffer copy, see hk_g11_tib_on */ + bool g11_tib; }; struct hk_index_buffer_state { @@ -269,6 +285,9 @@ struct hk_graphics_state { uint16_t index; } occlusion; + /* G11 vertex state token header, repeated before indexed draws */ + uint32_t g11_vs_header; + /* Fast linked shader data structures */ uint64_t varyings; struct agx_varyings_vs linked_varyings; @@ -359,6 +378,9 @@ struct hk_cs { /* Whether there is more than just the root chunk */ bool stream_linked; + /* Estimated G11 tiler heap use, see agx_g11_heap_cost */ + uint64_t g11_heap_B; + /* Whether the sampler heap is required. Although we always must maintain the * heap for correctness, it's often not necessary since we can push lots of * samplers (especially for GL/DX11-era engines). @@ -712,12 +734,18 @@ hk_cmd_buffer_end_compute(struct hk_cmd_buffer *cmd) void hk_optimize_empty_vdm(struct hk_cmd_buffer *cmd); static void -hk_cmd_buffer_end_graphics(struct hk_cmd_buffer *cmd) +hk_cmd_buffer_end_graphics_cs(struct hk_cmd_buffer *cmd, struct hk_cs *cs) { - struct hk_cs *cs = cmd->current_cs.gfx; - - if (cs && cs->stats.cmds == 0) { + /* An empty layer of a G11 layered render still clears in its background + * program: replacing it with image clears would land after the other + * layers' renders. + */ + if (cs && cs->stats.cmds == 0 && !cmd->state.gfx.render.g11_layers) { + cmd->current_cs.gfx = cs; hk_optimize_empty_vdm(cmd); + } else if (cs && cs->stats.cmds == 0 && !cs->cr.process_empty_tiles) { + list_del(&cs->node); + hk_cs_destroy(cs); } else if (cs) { /* Scissor and depth bias arrays are staged to dynamic arrays on the * CPU. When we end the control stream, they're done growing and are @@ -732,7 +760,22 @@ hk_cmd_buffer_end_graphics(struct hk_cmd_buffer *cmd) /* TODO: maybe free scissor/depth_bias now? */ cs->current = agx_vdm_terminate(cs->current); } +} + +static void +hk_cmd_buffer_end_graphics(struct hk_cmd_buffer *cmd) +{ + struct hk_rendering_state *render = &cmd->state.gfx.render; + + /* End every layer's render, see hk_g11_layer_cs */ + for (unsigned l = 0; l < render->g11_layers; ++l) { + if (render->g11_cs[l] && render->g11_cs[l] != cmd->current_cs.gfx) + hk_cmd_buffer_end_graphics_cs(cmd, render->g11_cs[l]); + + render->g11_cs[l] = NULL; + } + hk_cmd_buffer_end_graphics_cs(cmd, cmd->current_cs.gfx); cmd->current_cs.gfx = NULL; hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.pre_gfx); @@ -796,6 +839,17 @@ void hk_cmd_bind_compute_shader(struct hk_cmd_buffer *cmd, void hk_cmd_bind_vertex_buffer(struct hk_cmd_buffer *cmd, uint32_t vb_idx, struct hk_addr_range addr_range); +#if AGX_G11 +/* Texture and sampler state a G11 shader binds, see hk_g11_bind_state */ +struct hk_g11_counts { + unsigned textures, samplers; +}; + +struct hk_shader; +struct hk_g11_counts hk_g11_counts(struct hk_cmd_buffer *cmd, + struct hk_shader *s, bool spilled); +#endif + static inline struct hk_descriptor_state * hk_get_descriptors_state(struct hk_cmd_buffer *cmd, VkPipelineBindPoint bind_point) @@ -816,6 +870,9 @@ void hk_cmd_buffer_flush_push_descriptors(struct hk_cmd_buffer *cmd, void hk_meta_resolve_rendering(struct hk_cmd_buffer *cmd, const VkRenderingInfo *pRenderingInfo); +void hk_rt_descs(struct hk_cmd_buffer *cmd, unsigned rt, + struct agx_texture_packed out[2]); + uint64_t hk_cmd_buffer_upload_root(struct hk_cmd_buffer *cmd, VkPipelineBindPoint bind_point); @@ -852,7 +909,17 @@ hk_dispatch_with_local_size(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint32_t usc = hk_upload_usc_words(cmd, s, s->only_linked); hk_reserve_scratch(cmd, cs, s); + +#if AGX_G11 + /* Texture and sampler state per dispatch, see hk_g11_bind_state */ + struct agx_shader_info info = s->b.info; + struct hk_g11_counts n = hk_g11_counts(cmd, s, false); + info.texture_state_count = n.textures; + info.sampler_state_count = n.samplers; + hk_dispatch_with_usc(dev, cs, &info, usc, grid, local_size); +#else hk_dispatch_with_usc(dev, cs, &s->b.info, usc, grid, local_size); +#endif } void hk_dispatch_precomp(struct hk_cmd_buffer *cmd, struct agx_grid grid, diff --git a/src/asahi/vulkan/hk_cmd_dispatch.c b/src/asahi/vulkan/hk_cmd_dispatch.c index 7cb08bb1d3c..9f56d29d320 100644 --- a/src/asahi/vulkan/hk_cmd_dispatch.c +++ b/src/asahi/vulkan/hk_cmd_dispatch.c @@ -79,6 +79,10 @@ hk_dispatch_with_usc(struct hk_device *dev, struct hk_cs *cs, agx_translate_sampler_state_count(info->sampler_state_count, false); cfg.uniform_register_count = info->push_count; cfg.preshader_register_count = info->nr_preamble_gprs; + + /* G11 loads no samplers without textures, see hk_fast_link */ + if (AGX_G11 && !info->texture_state_count) + cfg.sampler_state_register_count = 0; } hk_dispatch_with_usc_launch(dev, cs, launch, usc, grid, local_size); diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 417d4b3bb3b..f2bba244b1b 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -200,6 +200,7 @@ hk_cmd_buffer_begin_graphics(struct hk_cmd_buffer *cmd, * hk_set_view_index will dirty the root each draw. */ cmd->state.gfx.descriptors.root.draw.view_index = 0; + cmd->state.gfx.descriptors.root.draw.g11_layer = 0; cmd->state.gfx.descriptors.root_dirty = true; } @@ -265,6 +266,34 @@ is_attachment_stored(const VkRenderingAttachmentInfo *att) att->resolveMode != VK_RESOLVE_MODE_NONE; } +/* + * G11 has no layered rendering. A layered or multiview render is one render + * per layer, at that layer of the attachments (hk_rt_descs, hk_cs_init_graphics + * for depth and stencil). Every draw is recorded into each layer's control + * stream with the layer in the root table: the vertex shader drops primitives + * of other layers and gl_Layer reads it in the fragment shader. + * + * ponytail: every draw costs one pass per layer + */ +static struct hk_cs * +hk_g11_layer_cs(struct hk_cmd_buffer *cmd, unsigned layer) +{ + struct hk_rendering_state *render = &cmd->state.gfx.render; + struct hk_root_descriptor_table *root = &cmd->state.gfx.descriptors.root; + + render->g11_layer = layer; + root->draw.g11_layer = layer; + root->draw.view_index = render->view_mask ? layer : 0; + cmd->state.gfx.descriptors.root_dirty = true; + + if (cmd->current_cs.gfx != render->g11_cs[layer]) { + cmd->current_cs.gfx = render->g11_cs[layer]; + hk_cmd_buffer_dirty_all(cmd); + } + + return hk_cmd_buffer_get_cs(cmd, false /* compute */); +} + static struct hk_bg_eot hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, bool store, bool partial_render, bool incomplete_render_area) @@ -276,7 +305,8 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, struct agx_bg_eot_key key = {.tib = render->tilebuffer}; static_assert(AGX_BG_EOT_NONE == 0, "default initializer"); - key.tib.layered = (render->cr.layers > 1); + /* G11 renders each layer on its own, see hk_g11_layer_cs */ + key.tib.layered = (render->cr.layers > 1) && !AGX_G11; bool needs_textures_for_spilled_rts = agx_tilebuffer_spills(&render->tilebuffer) && !partial_render && !store; @@ -288,9 +318,9 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, /* Partial render programs exist only to store/load the tilebuffer to * main memory. When render targets are already spilled to main memory, - * there's nothing to do. + * there's nothing to do, except to reload G11's tilebuffer copy. */ - if (key.tib.spilled[i] && (partial_render || store)) + if (key.tib.spilled[i] && (store || (partial_render && !key.tib.g11_tib[i]))) continue; if (store) { @@ -315,8 +345,10 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, */ load |= incomplete_render_area; - /* Don't read back spilled render targets, they're already in memory */ - load &= !key.tib.spilled[i]; + /* Don't read back spilled render targets, they're already in memory. + * G11 copies them into the tilebuffer for blending. + */ + load &= !key.tib.spilled[i] || key.tib.g11_tib[i]; /* This is a very frustrating corner case. From the spec: * @@ -346,7 +378,7 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, /* Don't apply clears for spilled render targets when we clear the * render area explicitly after. */ - if (key.tib.spilled[i] && incomplete_render_area) + if (key.tib.spilled[i] && !key.tib.g11_tib[i] && incomplete_render_area) continue; if (load) @@ -412,6 +444,10 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, if (needs_textures_for_spilled_rts) { hk_usc_upload_spilled_rt_descs(&b, cmd); uniforms = MAX2(uniforms, 4); + + /* G11 binds them as texture state */ + if (AGX_G11) + nr_tex = MAX2(nr_tex, 2 * render->color_att_count); } if (uses_txf) { @@ -421,7 +457,10 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, /* For attachmentless rendering, we don't know the sample count until * draw-time. But we have trivial bg/eot programs in that case too. */ - if (key.tib.nr_samples >= 1) { + if (AGX_G11) { + /* In the shader word below */ + key.tib.nr_samples = MAX2(key.tib.nr_samples, 1); + } else if (key.tib.nr_samples >= 1) { agx_usc_push_packed(&b, SHARED, &key.tib.usc); } else { assert(key.tib.sample_size_B == 0); @@ -435,6 +474,12 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, /* XXX: locking? */ struct agx_bg_eot_shader *shader = agx_get_bg_eot_shader(&dev->bg_eot, &key); +#if AGX_G11 + agx_usc_shader_g11_frag(&b, agx_usc_addr(&dev->dev, shader->ptr + + shader->info.main_offset), + shader->info.nr_gprs, agx_g11_tib_size_B(&key.tib), + key.tib.tile_size); +#else agx_usc_pack(&b, SHADER, cfg) { cfg.code = agx_usc_addr(&dev->dev, shader->ptr); cfg.unk_2 = 0; @@ -442,6 +487,7 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, agx_usc_pack(&b, REGISTERS, cfg) cfg.register_count = shader->info.nr_gprs; +#endif if (shader->info.has_preamble) { agx_usc_pack(&b, PRESHADER, cfg) { @@ -463,6 +509,14 @@ hk_build_bg_eot(struct hk_cmd_buffer *cmd, const VkRenderingInfo *info, agx_translate_sampler_state_count(uses_txf ? 1 : 0, false); } + if (AGX_G11) { + agx_g11_clear_counts((uint32_t *)&ret.counts, nr_tex, uses_txf); + + /* The tilebuffer words written, see hk_g11_flush_ppp_state */ + if (!store && agx_g11_tib_size_B(&key.tib)) + *(uint32_t *)&ret.counts |= BITFIELD_MASK(17) << 15; + } + return ret; } @@ -583,6 +637,20 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, render->cr.layers = layer_count; + if (AGX_G11 && layer_count > 1) { + render->g11_layers = MIN2(layer_count, HK_G11_MAX_LAYERS); + + for (uint32_t i = 0; i < render->color_att_count; i++) { + struct hk_image_view *iview = render->color_att[i].iview; + if (iview) { + struct hk_image *image = + container_of(iview->vk.image, struct hk_image, vk); + render->g11_rt_stride_B[i] = + image->planes[iview->planes[0].image_plane].layout.layer_stride_B; + } + } + } + /* Choose a tilebuffer layout given the framebuffer key */ enum pipe_format formats[HK_MAX_RTS] = {0}; for (unsigned i = 0; i < render->color_att_count; ++i) { @@ -616,6 +684,14 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, (render->view_mask && render->view_mask != BITFIELD64_MASK(render->cr.layers)); + /* G11 turns the tilebuffer copy on at the first read (hk_g11_tib_on). + * Layered renders record every layer's stream up front and keep it, and + * so do renders that clear part of the image with draws. + */ + render->g11_tib = render->g11_layers != 0 || incomplete_render_area; + if (!render->g11_tib) + agx_tilebuffer_drop_g11_tib(&render->tilebuffer); + perf_debug(cmd, "Rendering %ux%ux%u@%u %s%s", render->cr.width, render->cr.height, render->cr.layers, render->tilebuffer.nr_samples, @@ -668,6 +744,7 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, unsigned stride_pages = z_layout->layer_stride_B / AIL_PAGESIZE; render->cr.depth.stride = ((stride_pages - 1) << 14) | 1; + render->g11_z_stride_B = z_layout->layer_stride_B; assert(z_layout->tiling != AIL_TILING_LINEAR && "must tile"); @@ -719,6 +796,7 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, "Page aligned S layers"); unsigned stride_pages = s_layout->layer_stride_B / AIL_PAGESIZE; render->cr.stencil.stride = ((stride_pages - 1) << 14) | 1; + render->g11_s_stride_B = s_layout->layer_stride_B; if (s_layout->compressed) { render->cr.stencil.meta = @@ -750,6 +828,7 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, * hk_set_view_index will dirty the root each draw. */ cmd->state.gfx.descriptors.root.draw.view_index = 0; + cmd->state.gfx.descriptors.root.draw.g11_layer = 0; cmd->state.gfx.descriptors.root_dirty = true; if (render->flags & VK_RENDERING_RESUMING_BIT) @@ -759,12 +838,32 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, * the clears. Create it and swap in the clear. */ assert(!cmd->current_cs.gfx && "not already in a render pass"); - struct hk_cs *cs = hk_cmd_buffer_get_cs(cmd, false /* compute */); - if (!cs) - return; + struct hk_cs *cs = NULL; + + /* Each layer of a G11 layered render clears in its own render. Layer 0 + * is first in the command buffer, so compute queued before the current + * render runs before all of them. + */ + for (unsigned l = 0; l < render->g11_layers; ++l) { + cs = hk_g11_layer_cs(cmd, l); + if (!cs) + return; - cs->cr.bg.main = render->cr.bg.main; - cs->cr.zls_control = render->cr.zls_control; + cs->cr.bg.main = hk_build_bg_eot(cmd, pRenderingInfo, false, false, + incomplete_render_area); + cs->cr.zls_control = render->cr.zls_control; + } + + if (render->g11_layers) { + cs = hk_g11_layer_cs(cmd, 0); + } else { + cs = hk_cmd_buffer_get_cs(cmd, false /* compute */); + if (!cs) + return; + + cs->cr.bg.main = render->cr.bg.main; + cs->cr.zls_control = render->cr.zls_control; + } /* Reordering barrier for post-gfx, in case we had any. */ hk_cmd_buffer_end_compute_internal(cmd, &cmd->current_cs.post_gfx); @@ -899,6 +998,9 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, * fast clearing and then storing. */ cs->cr.process_empty_tiles = resolved_clear; + + for (unsigned l = 0; l < render->g11_layers; ++l) + render->g11_cs[l]->cr.process_empty_tiles = resolved_clear; } } @@ -917,6 +1019,11 @@ hk_CmdEndRendering2KHR(VkCommandBuffer commandBuffer, cs->cr.eot.main = render->cr.eot.main; } + for (unsigned l = 0; l < render->g11_layers; ++l) { + if (render->g11_cs[l]) + render->g11_cs[l]->cr.eot.main = render->cr.eot.main; + } + perf_debug(cmd, "End rendering"); hk_cmd_buffer_end_graphics(cmd); @@ -2041,7 +2148,8 @@ hk_stencil_test_enabled(struct hk_cmd_buffer *cmd) render->stencil_att.vk_format != VK_FORMAT_UNDEFINED; } -static void +/* Returns G11's depth bias/scissor word, which it emits on its own */ +static uint32_t hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) { const struct vk_dynamic_graphics_state *dyn = @@ -2089,16 +2197,18 @@ hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) } /* Upload a hardware scissor for each viewport, whether there's a - * corresponding API scissor or not. + * corresponding API scissor or not. G11's are G13's without the Z range. */ - unsigned index = cs->scissor.size / AGX_SCISSOR_LENGTH; - struct agx_scissor_packed *scissors = - util_dynarray_grow_bytes(&cs->scissor, count, AGX_SCISSOR_LENGTH); + unsigned scissor_size = AGX_G11 ? 8 : AGX_SCISSOR_LENGTH; + unsigned index = cs->scissor.size / scissor_size; + uint8_t *scissors = + util_dynarray_grow_bytes(&cs->scissor, count, scissor_size); for (unsigned i = 0; i < count; ++i) { const VkViewport *vp = &dyn->vp.viewports[i]; + struct agx_scissor_packed scissor; - agx_pack(scissors + i, SCISSOR, cfg) { + agx_pack(&scissor, SCISSOR, cfg) { cfg.min_x = minx[i]; cfg.min_y = miny[i]; cfg.max_x = maxx[i]; @@ -2118,11 +2228,22 @@ hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) cfg.max_z = 1.0; } } + + memcpy(scissors + (i * scissor_size), &scissor, scissor_size); + } + + uint32_t depth_bias_scissor; + agx_pack(&depth_bias_scissor, DEPTH_BIAS_SCISSOR, cfg) { + cfg.scissor = index; + + /* Use the current depth bias, we allocate linearly */ + unsigned count = cs->depth_bias.size / AGX_DEPTH_BIAS_LENGTH; + cfg.depth_bias = count ? count - 1 : 0; } /* Upload state */ struct AGX_PPP_HEADER present = { - .depth_bias_scissor = true, + .depth_bias_scissor = !AGX_G11, .region_clip = true, .viewport = true, .viewport_count = count, @@ -2131,21 +2252,17 @@ hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) size_t size = agx_ppp_update_size(&present); struct agx_ptr T = hk_pool_alloc(cmd, size, AGX_PPP_HEADER_ALIGN); if (!T.cpu) - return; + return depth_bias_scissor; struct agx_ppp_update ppp = agx_new_ppp_update(T, size, &present); - agx_ppp_push(&ppp, DEPTH_BIAS_SCISSOR, cfg) { - cfg.scissor = index; - - /* Use the current depth bias, we allocate linearly */ - unsigned count = cs->depth_bias.size / AGX_DEPTH_BIAS_LENGTH; - cfg.depth_bias = count ? count - 1 : 0; - }; + if (!AGX_G11) + agx_ppp_push_packed(&ppp, &depth_bias_scissor, DEPTH_BIAS_SCISSOR); for (unsigned i = 0; i < count; ++i) { agx_ppp_push(&ppp, REGION_CLIP, cfg) { - cfg.enable = true; + /* AGXMetalA12 leaves the region clip disabled */ + cfg.enable = !AGX_G11; cfg.min_x = minx[i] / 32; cfg.min_y = miny[i] / 32; cfg.max_x = DIV_ROUND_UP(MAX2(maxx[i], 1), 32); @@ -2153,8 +2270,10 @@ hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) } } - agx_ppp_push(&ppp, VIEWPORT_CONTROL, cfg) - ; + if (!AGX_G11) { + agx_ppp_push(&ppp, VIEWPORT_CONTROL, cfg) + ; + } /* Upload viewports */ for (unsigned i = 0; i < count; ++i) { @@ -2172,6 +2291,7 @@ hk_flush_vp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint8_t **out) } agx_ppp_fini(out, &ppp); + return depth_bias_scissor; } static enum agx_object_type @@ -2634,6 +2754,240 @@ agx_fill_velem_keys(const struct vk_vertex_input_state *vi, } } +#if AGX_G11 +/* See agx_g11_ppp_emit */ +static void +hk_g11_ppp(struct hk_cmd_buffer *cmd, uint8_t **out, const uint32_t *words, + unsigned count) +{ + struct agx_ptr T = hk_pool_alloc(cmd, count * 4, 4); + if (T.cpu) + agx_g11_ppp_emit(out, T, words, count); +} + +static uint32_t +hk_g11_stencil(struct vk_stencil_test_face_state s, bool enabled) +{ + uint32_t w; + agx_pack(&w, FRAGMENT_STENCIL, cfg) { + cfg.compare = enabled ? translate_compare_op(s.op.compare) + : AGX_ZS_FUNC_ALWAYS; + cfg.write_mask = enabled ? s.write_mask : 0xFF; + cfg.read_mask = enabled ? s.compare_mask : 0xFF; + cfg.depth_pass = + enabled ? translate_stencil_op(s.op.pass) : AGX_STENCIL_OP_KEEP; + cfg.depth_fail = + enabled ? translate_stencil_op(s.op.depth_fail) : AGX_STENCIL_OP_KEEP; + cfg.stencil_fail = + enabled ? translate_stencil_op(s.op.fail) : AGX_STENCIL_OP_KEEP; + } + return w; +} + +/* + * G11's vertex state is a fixed four word token (Metal's + * VDMEncoderGen2_1::VsStateToken), see the GL driver's agx_encode_state. + * The varying counts go first, in their own PPP block. + */ +static void +hk_g11_flush_vs(struct hk_cmd_buffer *cmd, struct hk_shader *hw_vs, + struct hk_linked_shader *linked, bool varyings_dirty, + uint8_t **out) +{ + struct hk_graphics_state *gfx = &cmd->state.gfx; + unsigned outputs = hw_vs->info.uvs.size; + + if (varyings_dirty) { + uint32_t counts[3] = {BITFIELD_RANGE(18, 2)}; + + memcpy(&counts[1], &gfx->linked_varyings.counts_32, 4); + memcpy(&counts[2], &gfx->linked_varyings.counts_16, 4); + hk_g11_ppp(cmd, out, counts, 3); + } + + uint32_t *w = (uint32_t *)*out; + + w[0] = agx_g11_vs_header(hk_rast_discard(cmd), gfx->provoking, outputs); + gfx->g11_vs_header = w[0]; + + struct hk_g11_counts n = hk_g11_counts(cmd, hw_vs, false); + agx_pack(&w[1], VDM_STATE_VERTEX_SHADER_WORD_0, cfg) { + cfg.uniform_register_count = hw_vs->b.info.push_count; + cfg.texture_state_register_count = n.textures; + cfg.sampler_state_register_count = + agx_translate_sampler_state_count(n.samplers, false); + } + agx_g11_clear_counts(&w[1], n.textures, n.samplers); + + w[2] = hk_upload_usc_words(cmd, hw_vs, linked) | + agx_g11_regs(linked->b.nr_gprs); + w[3] = outputs & 0xff; + *out += 16; +} + +/* + * G11 draw state in AGXMetalA12's blocks and order: the fragment shader, + * output select, fragment state, viewport, W clamp with cull, and depth + * bias/scissor (the GL driver's agx_g11_encode_ppp). + */ +static void +hk_g11_flush_ppp_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, + uint8_t **out, bool vp_dirty) +{ + const struct hk_rendering_state *render = &cmd->state.gfx.render; + struct vk_dynamic_graphics_state *dyn = &cmd->vk.dynamic_graphics_state; + struct hk_graphics_state *gfx = &cmd->state.gfx; + struct hk_shader *hw_vs = hk_bound_hw_vs(gfx); + struct hk_shader *fs = hk_only_variant(gfx->shaders[MESA_SHADER_FRAGMENT]); + struct hk_linked_shader *linked_fs = gfx->linked[MESA_SHADER_FRAGMENT]; + uint32_t fs_control = linked_fs->b.fragment_control.opaque[0]; + + bool hw_vs_dirty = IS_SHADER_DIRTY(VERTEX) || IS_SHADER_DIRTY(TESS_EVAL) || + IS_SHADER_DIRTY(GEOMETRY); + bool fs_dirty = IS_SHADER_DIRTY(FRAGMENT); + bool linked_fs_dirty = IS_LINKED_DIRTY(FRAGMENT); + bool varyings_dirty = gfx->dirty & HK_DIRTY_VARYINGS; + + if ((fs_dirty || linked_fs_dirty || varyings_dirty || + gfx->descriptors.root_dirty) && + !linked_fs->b.no_op) { + + hk_reserve_scratch(cmd, cs, fs); + + struct hk_g11_counts n = hk_g11_counts( + cmd, fs, agx_tilebuffer_spills(&cmd->state.gfx.render.tilebuffer)); + + uint32_t counts; + agx_pack(&counts, FRAGMENT_SHADER_WORD_0, cfg) { + cfg.uniform_register_count = fs->b.info.push_count; + cfg.texture_state_register_count = n.textures; + cfg.sampler_state_register_count = + agx_translate_sampler_state_count(n.samplers, false); + } + agx_g11_clear_counts(&counts, n.textures, n.samplers); + + /* The tilebuffer words the shader writes, from bit 15, see + * agx_g11_encode_ppp + */ + if (agx_g11_tib_size_B(&render->tilebuffer)) + counts |= BITFIELD_MASK(17) << 15; + + uint32_t words[] = { + BITFIELD_BIT(7) | BITFIELD_BIT(8), + counts, + hk_upload_usc_words(cmd, fs, linked_fs) | + agx_g11_regs(linked_fs->b.nr_gprs), + gfx->varyings, + linked_fs->b.cf.nr_bindings & 0x7f, + }; + + hk_g11_ppp(cmd, out, words, ARRAY_SIZE(words)); + } + + if (hw_vs_dirty || linked_fs_dirty || varyings_dirty) { + uint32_t osel = + agx_g11_osel(&hw_vs->info.uvs, linked_fs->b.osel.opaque[0]); + hk_g11_ppp(cmd, out, (uint32_t[]){BITFIELD_BIT(17), osel}, 2); + } + + bool face_dirty = + IS_DIRTY(DS_DEPTH_TEST_ENABLE) || IS_DIRTY(DS_DEPTH_WRITE_ENABLE) || + IS_DIRTY(DS_DEPTH_COMPARE_OP) || IS_DIRTY(DS_STENCIL_REFERENCE) || + IS_DIRTY(RS_LINE_WIDTH) || IS_DIRTY(RS_POLYGON_MODE) || fs_dirty || + IS_DIRTY(DS_STENCIL_OP) || IS_DIRTY(DS_STENCIL_COMPARE_MASK) || + IS_DIRTY(DS_STENCIL_WRITE_MASK) || IS_DIRTY(DS_STENCIL_TEST_ENABLE) || + IS_DIRTY(IA_PRIMITIVE_TOPOLOGY) || IS_DIRTY(RS_DEPTH_BIAS_ENABLE) || + linked_fs_dirty || (gfx->dirty & HK_DIRTY_OCCLUSION); + + if (face_dirty) { + bool stencil = hk_stencil_test_enabled(cmd); + + uint32_t control; + agx_pack(&control, FRAGMENT_CONTROL, cfg) { + cfg.visibility_mode = gfx->occlusion.mode; + cfg.stencil_test_enable = stencil; + cfg.two_sided_stencil = stencil; + cfg.depth_bias_enable = dyn->rs.depth_bias.enable && + gfx->object_type == AGX_OBJECT_TYPE_TRIANGLE; + cfg.scissor_enable = true; + cfg.disable_tri_merging = gfx->object_type != AGX_OBJECT_TYPE_TRIANGLE; + } + + control &= ~BITFIELD_BIT(9); + control |= fs_control & (BITFIELD_BIT(21) | BITFIELD_BIT(26)); + if (gfx->occlusion.mode != AGX_VISIBILITY_MODE_NONE) + control |= (gfx->occlusion.index * 2) & BITFIELD_MASK(14); + + bool has_z = render->depth_att.vk_format != VK_FORMAT_UNDEFINED; + bool z_test = has_z && dyn->ds.depth.test_enable; + enum agx_zs_func zf = + (z_test && !gfx->descriptors.root.draw.force_never_in_shader) + ? translate_compare_op(dyn->ds.depth.compare_op) + : AGX_ZS_FUNC_ALWAYS; + + unsigned type = gfx->object_type; + if (type == AGX_OBJECT_TYPE_TRIANGLE && + dyn->rs.polygon_mode != VK_POLYGON_MODE_FILL) + type = dyn->rs.polygon_mode == VK_POLYGON_MODE_LINE ? 5 : 6; + + /* Stencil reference 7:0, line width 15:8, depth compare 19:17, shader + * writes depth 20, depth write off 21, pass type 26:24, object type + * 31:28 + */ + uint32_t face = (agx_pack_line_width(dyn->rs.line.width) << 8) | + (zf << 17) | + (!(z_test && dyn->ds.depth.write_enable) ? BITFIELD_BIT(21) + : 0) | + (((fs_control >> 29) & 7) << 24) | (type << 28); + + if (fs->b.info.depth_layout != FRAG_DEPTH_LAYOUT_UNCHANGED) + face |= BITFIELD_BIT(20); + + hk_g11_ppp(cmd, out, + (uint32_t[]){ + BITFIELD_MASK(5), + control, + face | dyn->ds.stencil.front.reference, + hk_g11_stencil(dyn->ds.stencil.front, stencil), + face | dyn->ds.stencil.back.reference, + hk_g11_stencil(dyn->ds.stencil.back, stencil), + }, + 6); + } + + uint32_t depth_bias_scissor = 0; + if (vp_dirty) + depth_bias_scissor = hk_flush_vp_state(cmd, cs, out); + + if (IS_DIRTY(RS_CULL_MODE) || IS_DIRTY(RS_RASTERIZER_DISCARD_ENABLE) || + IS_DIRTY(RS_FRONT_FACE) || IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || + IS_DIRTY(RS_DEPTH_CLAMP_ENABLE) || IS_DIRTY(RS_LINE_MODE) || + IS_DIRTY(IA_PRIMITIVE_TOPOLOGY) || (gfx->dirty & HK_DIRTY_PROVOKING) || + varyings_dirty) { + + uint32_t cull; + agx_pack(&cull, CULL, cfg) { + cfg.cull_front = dyn->rs.cull_mode & VK_CULL_MODE_FRONT_BIT; + cfg.cull_back = dyn->rs.cull_mode & VK_CULL_MODE_BACK_BIT; + cfg.front_face_ccw = dyn->rs.front_face != VK_FRONT_FACE_CLOCKWISE; + cfg.flat_shading_vertex = translate_ppp_vertex(gfx->provoking); + cfg.rasterizer_discard = hk_rast_discard(cmd); + cfg.depth_clip = vk_rasterization_state_depth_clip_enable(&dyn->rs); + cfg.depth_clamp = !cfg.depth_clip; + } + + /* No cull 2: clamp W is cull bit 5 */ + hk_g11_ppp(cmd, out, + (uint32_t[]){BITFIELD_BIT(16) | BITFIELD_BIT(21), fui(1e-20), + cull | BITFIELD_BIT(5)}, + 3); + } + + if (vp_dirty) + hk_g11_ppp(cmd, out, (uint32_t[]){BITFIELD_BIT(5), depth_bias_scissor}, 2); +} +#endif + static void hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, uint32_t draw_id, struct agx_draw draw) @@ -2892,6 +3246,8 @@ hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, key.epilog.link.already_ran_zs |= nontrivial_force_early; + key.epilog.g11_tib = cmd->state.gfx.render.g11_tib; + struct hk_rendering_state *render = &cmd->state.gfx.render; for (uint32_t i = 0; i < render->color_att_count; i++) { key.epilog.rt_formats[i] = @@ -3055,7 +3411,9 @@ hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, if ((gfx->dirty & HK_DIRTY_PROVOKING) || vgt_dirty || linked_fs_dirty) { unsigned bindings = linked_fs->b.cf.nr_bindings; - if (bindings) { + + /* G11 reads the binding header even without bindings */ + if (bindings || AGX_G11) { size_t linkage_size = AGX_CF_BINDING_HEADER_LENGTH + (bindings * AGX_CF_BINDING_LENGTH); @@ -3067,6 +3425,10 @@ hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, t.cpu, &gfx->linked_varyings, hw_vs->info.uvs.user_size, &linked_fs->b.cf, gfx->provoking, 0, &gfx->generate_primitive_id); + /* AGXMetalA12 leaves the varying table empty without inputs */ + if (AGX_G11 && !bindings) + memset(t.cpu, 0, linkage_size); + gfx->varyings = agx_usc_addr(&dev->dev, t.gpu); } else { gfx->varyings = 0; @@ -3123,6 +3485,11 @@ hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, /* TODO: Do less often? */ hk_reserve_scratch(cmd, cs, hw_vs); +#if AGX_G11 + hk_g11_flush_vs(cmd, hw_vs, + (hw_vs == sw_vs) ? linked_vs : hw_vs->only_linked, + gfx->dirty & HK_DIRTY_VARYINGS, &out); +#else agx_push(out, VDM_STATE, cfg) { cfg.vertex_shader_word_0_present = true; cfg.vertex_shader_word_1_present = true; @@ -3150,6 +3517,7 @@ hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, /* Pad up to a multiple of 8 bytes */ memset(out, 0, 4); out += 4; +#endif } if (IS_DIRTY(RS_DEPTH_BIAS_FACTORS)) { @@ -3171,16 +3539,25 @@ hk_flush_dynamic_state(struct hk_cmd_buffer *cmd, struct hk_cs *cs, } /* Hardware viewport/scissor state is entangled with depth bias. */ + UNUSED bool vp_dirty = false; if (IS_DIRTY(RS_DEPTH_BIAS_FACTORS) || IS_DIRTY(VP_SCISSORS) || IS_DIRTY(VP_SCISSOR_COUNT) || IS_DIRTY(VP_VIEWPORTS) || IS_DIRTY(VP_VIEWPORT_COUNT) || IS_DIRTY(VP_DEPTH_CLIP_NEGATIVE_ONE_TO_ONE) || IS_DIRTY(RS_DEPTH_CLIP_ENABLE) || IS_DIRTY(RS_DEPTH_CLAMP_ENABLE)) { +#if AGX_G11 + vp_dirty = true; +#else hk_flush_vp_state(cmd, cs, &out); +#endif } +#if AGX_G11 + hk_g11_flush_ppp_state(cmd, cs, &out, vp_dirty); +#else hk_flush_ppp_state(cmd, cs, &out); +#endif cs->current = out; vk_dynamic_graphics_state_clear_dirty(dyn); @@ -3385,7 +3762,8 @@ hk_flush_gfx_state(struct hk_cmd_buffer *cmd, uint32_t draw_id, if (desc->push_dirty) hk_cmd_buffer_flush_push_descriptors(cmd, desc); - if (draw.restart || gfx->shaders[MESA_SHADER_GEOMETRY]) + /* G11 takes the restart index in a token before each draw (hk_draw) */ + if (!AGX_G11 && (draw.restart || gfx->shaders[MESA_SHADER_GEOMETRY])) hk_flush_index(cmd, cs); hk_flush_dynamic_state(cmd, cs, draw_id, draw); @@ -3445,7 +3823,15 @@ hk_CmdBindVertexBuffers2(VkCommandBuffer commandBuffer, uint32_t firstBinding, static bool hk_set_view_index(struct hk_cmd_buffer *cmd, uint32_t view_idx) { - if (cmd->state.gfx.render.view_mask) { + struct hk_rendering_state *render = &cmd->state.gfx.render; + + if (render->view_mask && !(render->view_mask & BITFIELD_BIT(view_idx))) + return false; + + if (render->g11_layers) + return hk_g11_layer_cs(cmd, view_idx) != NULL; + + if (render->view_mask) { cmd->state.gfx.descriptors.root.draw.view_index = view_idx; cmd->state.gfx.descriptors.root_dirty = true; } @@ -3453,13 +3839,24 @@ hk_set_view_index(struct hk_cmd_buffer *cmd, uint32_t view_idx) return true; } +static unsigned +hk_view_count(struct hk_cmd_buffer *cmd) +{ + struct hk_rendering_state *render = &cmd->state.gfx.render; + + return render->g11_layers ? render->g11_layers + : render->view_mask ? util_last_bit(render->view_mask) + : 1; +} + /* * Iterator macro to duplicate a draw for each enabled view (when multiview is * enabled, else always view 0). Along with hk_lower_multiview, this forms the - * world's worst multiview lowering. + * world's worst multiview lowering. G11 also goes over every layer of a + * layered render. */ #define hk_foreach_view(cmd) \ - u_foreach_bit(view_idx, cmd->state.gfx.render.view_mask ?: 1) \ + for (unsigned view_idx = 0; view_idx < hk_view_count(cmd); ++view_idx) \ if (hk_set_view_index(cmd, view_idx)) static void @@ -3509,6 +3906,138 @@ hk_ia_update(struct hk_cmd_buffer *cmd, struct agx_draw draw, } } +#if AGX_G11 +static unsigned +hk_g11_outputs(struct hk_cmd_buffer *cmd) +{ + struct hk_graphics_state *gfx = &cmd->state.gfx; + return gfx->shaders[MESA_SHADER_VERTEX] ? hk_bound_hw_vs(gfx)->info.uvs.size + : 64; +} + +/* End the render before G11's tiler heap fills, see agx_g11_heap_cost */ +static void +hk_g11_heap_reserve(struct hk_cmd_buffer *cmd, struct agx_draw draw) +{ + uint64_t cost = + agx_is_indirect(draw.b) + ? AGX_G11_HEAP_INDIRECT + : agx_g11_heap_cost((uint64_t)draw.b.count[0] * draw.b.count[1], + hk_g11_outputs(cmd)); + struct hk_cs *cs = cmd->current_cs.gfx; + + if (cs && cs->g11_heap_B && cs->g11_heap_B + cost > AGX_G11_HEAP_BUDGET) { + perf_debug(cmd, "Splitting control stream for the G11 tiler heap"); + hk_cmd_buffer_end_graphics(cmd); + } + + cs = hk_cmd_buffer_get_cs(cmd, false /* compute */); + if (cs) + cs->g11_heap_B += cost; +} +#endif + +static void hk_draw(struct hk_cmd_buffer *cmd, uint16_t draw_id, + struct agx_draw draw_); + +#if AGX_G11 +/* + * The tilebuffer copy costs every fragment a tile store, so a render starts + * without it (agx_tilebuffer_drop_g11_tib) and the first draw that reads its + * render targets turns it on: a new stream, whose partial background program + * reloads the copy from memory. Returns false if no target can have one. + */ +static bool +hk_g11_tib_on(struct hk_cmd_buffer *cmd) +{ + struct hk_rendering_state *render = &cmd->state.gfx.render; + VkRenderingAttachmentInfo atts[HK_MAX_RTS] = {0}; + struct agx_tilebuffer_layout tib = agx_build_tilebuffer_layout( + render->tilebuffer.logical_format, render->color_att_count, + render->tilebuffer.nr_samples, true); + + if (!agx_g11_tib_size_B(&tib)) + return false; + + for (unsigned i = 0; i < render->color_att_count; ++i) { + atts[i] = (VkRenderingAttachmentInfo){ + .sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO, + .imageView = hk_image_view_to_handle(render->color_att[i].iview), + .storeOp = VK_ATTACHMENT_STORE_OP_STORE, + }; + } + + const VkRenderingInfo info = { + .sType = VK_STRUCTURE_TYPE_RENDERING_INFO, + .colorAttachmentCount = render->color_att_count, + .pColorAttachments = atts, + }; + + perf_debug(cmd, "Splitting control stream for the G11 tilebuffer copy"); + if (cmd->current_cs.gfx) + hk_cmd_buffer_end_graphics(cmd); + + render->tilebuffer = tib; + render->g11_tib = true; + render->cr.bg.partial = hk_build_bg_eot(cmd, &info, false, true, false); + render->cr.eot.main = hk_build_bg_eot(cmd, &info, true, false, false); + render->cr.eot.partial = hk_build_bg_eot(cmd, &info, true, true, false); + hk_cmd_buffer_dirty_all(cmd); + return true; +} +#endif + +#if AGX_G11 +/* Split a direct draw too big for G11's tiler heap into list or strip pieces */ +static bool +hk_g11_split_draw(struct hk_cmd_buffer *cmd, uint16_t draw_id, + struct agx_draw draw) +{ + VkPrimitiveTopology topo = + cmd->vk.dynamic_graphics_state.ia.primitive_topology; + unsigned overlap; + + switch (topo) { + case VK_PRIMITIVE_TOPOLOGY_POINT_LIST: + case VK_PRIMITIVE_TOPOLOGY_LINE_LIST: + case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST: + overlap = 0; + break; + case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP: + overlap = 1; + break; + case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP: + overlap = 2; + break; + default: + return false; + } + + if (agx_is_indirect(draw.b) || draw.restart || + cmd->state.gfx.shaders[MESA_SHADER_GEOMETRY] || + cmd->state.gfx.shaders[MESA_SHADER_TESS_EVAL]) + return false; + + uint32_t max = agx_g11_split_max(draw.b.count[1], hk_g11_outputs(cmd)); + uint32_t left = draw.b.count[0]; + + if (left <= max) + return false; + + while (left > overlap) { + struct agx_draw piece = draw; + uint32_t n = MIN2(left, max); + + piece.b.count[0] = n; + hk_draw(cmd, draw_id, piece); + draw.start += n - overlap; + left -= n - overlap; + } + + return true; +} +#endif + static void hk_draw(struct hk_cmd_buffer *cmd, uint16_t draw_id, struct agx_draw draw_) { @@ -3523,6 +4052,11 @@ hk_draw(struct hk_cmd_buffer *cmd, uint16_t draw_id, struct agx_draw draw_) draw_.restart = dyn->ia.primitive_restart_enable && draw_.indexed; draw_.index_size = cmd->state.gfx.index.size; +#if AGX_G11 + if (hk_g11_split_draw(cmd, draw_id, draw_)) + return; +#endif + uint64_t stat_ia_verts = hk_pipeline_stat_addr( cmd, VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT); @@ -3546,7 +4080,37 @@ hk_draw(struct hk_cmd_buffer *cmd, uint16_t draw_id, struct agx_draw draw_) hk_foreach_view(cmd) { struct agx_draw draw = draw_; +#if AGX_G11 + hk_g11_heap_reserve(cmd, draw); +#endif struct hk_cs *cs = hk_flush_gfx_state(cmd, draw_id, draw); +#if AGX_G11 + /* G11 blends render targets that are only in memory through the + * texture unit, which does not see what earlier draws in the render + * wrote (agx_draw_vbo does the same). Start a new render, which has a + * clean texture cache. Targets also in the tilebuffer are read there. + * + * ponytail: one render per such draw, slow, and overlap within one draw + * is still wrong + */ + struct hk_linked_shader *g11_fs = + cmd->state.gfx.linked[MESA_SHADER_FRAGMENT]; + + if (cs && g11_fs && g11_fs->b.reads_tib && + !cmd->state.gfx.render.g11_tib && hk_g11_tib_on(cmd)) { + hk_g11_heap_reserve(cmd, draw); + cs = hk_flush_gfx_state(cmd, draw_id, draw); + g11_fs = cmd->state.gfx.linked[MESA_SHADER_FRAGMENT]; + } + + if (cs && cs->stats.calls && g11_fs && g11_fs->b.reads_tib && + !agx_g11_tib_all(&cmd->state.gfx.render.tilebuffer)) { + perf_debug(cmd, "Splitting control stream for a G11 blend"); + hk_cmd_buffer_end_graphics(cmd); + hk_g11_heap_reserve(cmd, draw); + cs = hk_flush_gfx_state(cmd, draw_id, draw); + } +#endif /* If we failed to allocate a control stream, we've already lost the * device. Just drop the draw so we don't crash. */ @@ -3627,6 +4191,14 @@ hk_draw(struct hk_cmd_buffer *cmd, uint16_t draw_id, struct agx_draw draw_) } enum agx_primitive topology = cmd->state.gfx.topology; + +#if AGX_G11 + if (draw.indexed) { + out = agx_g11_restart(out, cmd->state.gfx.g11_vs_header, draw.restart, + dyn->ia.primitive_restart_index); + } +#endif + if (needs_idx_robust) { assert(!geom && !tess && !adj); perf_debug(cmd, "lowering robust index buffer"); diff --git a/src/asahi/vulkan/hk_cmd_meta.c b/src/asahi/vulkan/hk_cmd_meta.c index abe4eb7d9c4..039c19d3465 100644 --- a/src/asahi/vulkan/hk_cmd_meta.c +++ b/src/asahi/vulkan/hk_cmd_meta.c @@ -62,6 +62,7 @@ hk_device_init_meta(struct hk_device *dev) return result; dev->meta.use_gs_for_layer = false; + dev->meta.blit_per_layer = AGX_G11; dev->meta.use_stencil_export = true; dev->meta.use_rect_list_pipeline = true; dev->meta.cmd_bind_map_buffer = hk_cmd_bind_map_buffer; @@ -289,6 +290,11 @@ canonical_format_pipe(enum pipe_format fmt) #undef CASE enum agx_channels channels = ail_pixel_format[fmt].channels; + + /* G11 writes R9G9B9E5 wrong, copy the bits */ + if (AGX_G11 && channels == AGX_CHANNELS_R9G9B9E5) + return PIPE_FORMAT_R32_UINT; + assert(channels < ARRAY_SIZE(map) && "all valid channels handled"); assert(map[channels] != PIPE_FORMAT_NONE && "all valid channels handled"); return map[channels]; @@ -976,9 +982,9 @@ hk_meta_copy_buffer_to_image2(struct vk_command_buffer *cmd, .dst_format = canonical_format_pipe( hk_format_to_pipe_format(aspect_format(image->format, aspect))), - /* TODO: MSAA path */ - .block_based = - (image->image_type != VK_IMAGE_TYPE_1D) && image->samples == 1, + /* TODO: MSAA path. G11's block store is not known. */ + .block_based = (image->image_type != VK_IMAGE_TYPE_1D) && + image->samples == 1 && !AGX_G11, }; VkPipelineLayout pipeline_layout; @@ -1138,9 +1144,9 @@ hk_meta_copy_image2(struct vk_command_buffer *cmd, struct vk_meta_device *meta, .dst_format = canonical_format_pipe(hk_format_to_pipe_format( aspect_format(dst_image->format, dst_aspect_mask))), - /* TODO: MSAA path */ + /* TODO: MSAA path. G11's block store is not known. */ .block_based = (dst_image->image_type != VK_IMAGE_TYPE_1D) && - dst_image->samples == 1, + dst_image->samples == 1 && !AGX_G11, }; assert(key.nr_samples == src_image->samples); diff --git a/src/asahi/vulkan/hk_descriptor_set.c b/src/asahi/vulkan/hk_descriptor_set.c index 09ec77486cc..befe991731f 100644 --- a/src/asahi/vulkan/hk_descriptor_set.c +++ b/src/asahi/vulkan/hk_descriptor_set.c @@ -81,6 +81,7 @@ write_sampled_image_view_desc(struct hk_descriptor_set *set, desc[plane].min_lod_fp16 = _mesa_float_to_half(min_lod); desc[plane].min_lod_uint16 = min_lod; + desc[plane].border_map = view->planes[plane].border_map; } } @@ -117,6 +118,11 @@ write_sampled_image_view_desc(struct hk_descriptor_set *set, desc[plane].sampler = sampler->planes[sampler_plane].hw->key; desc[plane].lod_bias_fp16 = sampler->lod_bias_fp16; desc[plane].clamp_0_sampler_index_or_negative = -1; + + /* See g11_lower_border */ + if (AGX_G11) + memcpy(desc[plane].border, sampler->custom_border.uint32, + sizeof(desc[plane].border)); } if (sampler->has_border) { diff --git a/src/asahi/vulkan/hk_descriptor_set.h b/src/asahi/vulkan/hk_descriptor_set.h index 54f9fc2e8cf..039c53a1f70 100644 --- a/src/asahi/vulkan/hk_descriptor_set.h +++ b/src/asahi/vulkan/hk_descriptor_set.h @@ -37,7 +37,11 @@ struct hk_sampled_image_descriptor { /* This is only used for txf, it can be separate from the other stuff */ uint16_t min_lod_uint16; - uint16_t pad; + + /* G11 border emulation: for each channel, 3 bits of the border component + * it reads, or 4 for constant 0 and 5 for constant 1 (g11_lower_border) + */ + uint16_t border_map; /* TODO: This should probably be a heap! */ uint32_t border[4]; uint8_t pad2[4]; diff --git a/src/asahi/vulkan/hk_format.c b/src/asahi/vulkan/hk_format.c index 28c2b4952f4..425e64ed7cd 100644 --- a/src/asahi/vulkan/hk_format.c +++ b/src/asahi/vulkan/hk_format.c @@ -34,6 +34,12 @@ hk_modifier_features(const struct agx_device *dev, uint64_t mod, return 0; } + /* G11 shares linear images only, as its GL driver does (the display scans + * nothing else, and other layouts are not shared with compositors) + */ + if (AGX_G11 && mod != DRM_FORMAT_MOD_LINEAR) + return 0; + /* Don't advertise compression for the uncompressable */ if (mod == DRM_FORMAT_MOD_APPLE_GPU_TILED_COMPRESSED && !hk_can_compress_format(dev, vk_format)) diff --git a/src/asahi/vulkan/hk_image.c b/src/asahi/vulkan/hk_image.c index 29bfaa1c327..9bbc10ebcf4 100644 --- a/src/asahi/vulkan/hk_image.c +++ b/src/asahi/vulkan/hk_image.c @@ -54,6 +54,20 @@ hk_get_image_plane_format_features(struct hk_physical_device *pdev, if (!util_is_power_of_two_nonzero(util_format_get_blocksize(p_format))) return 0; + /* A12 has no BC decoding (sampling it hangs) and no ASTC HDR, as in + * agx_is_format_supported + */ + if (AGX_G11) { + const struct util_format_description *d = + util_format_description(p_format); + + if (d->layout == UTIL_FORMAT_LAYOUT_S3TC || + d->layout == UTIL_FORMAT_LAYOUT_RGTC || + d->layout == UTIL_FORMAT_LAYOUT_BPTC || + util_format_is_astc_hdr(p_format)) + return 0; + } + /* Linear block-compressed images are all sorts of problematic, not sure * if AGX even supports them. Don't try. */ @@ -67,7 +81,12 @@ hk_get_image_plane_format_features(struct hk_physical_device *pdev, /* We can sample integer formats but it doesn't make sense to linearly * filter them. */ - if (!util_format_is_pure_integer(p_format)) { + if (!util_format_is_pure_integer(p_format) && + !(AGX_G11 && util_format_is_float(p_format) && + util_format_get_component_bits(p_format, UTIL_FORMAT_COLORSPACE_RGB, + 0) == 32 && + !util_format_is_depth_or_stencil(p_format))) { + /* G11 does not filter 32-bit float colour */ features |= VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_FILTER_LINEAR_BIT; } @@ -226,8 +245,8 @@ hk_can_compress(const struct agx_device *dev, VkFormat format, unsigned plane, format = (plane == 0) ? VK_FORMAT_D32_SFLOAT : VK_FORMAT_S8_UINT; } - /* Allow disabling compression for debugging */ - if (dev->debug & AGX_DBG_NOCOMPRESS) + /* Allow disabling compression for debugging. G11's is not known. */ + if ((dev->debug & AGX_DBG_NOCOMPRESS) || AGX_G11) return false; /* TODO: Handle compression with sparse. This should be doable but it's a bit @@ -460,7 +479,7 @@ hk_GetPhysicalDeviceImageFormatProperties2( maxMipLevels = 1; VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT; - if (pImageFormatInfo->tiling == VK_IMAGE_TILING_OPTIMAL && + if (!AGX_G11 && pImageFormatInfo->tiling == VK_IMAGE_TILING_OPTIMAL && pImageFormatInfo->type == VK_IMAGE_TYPE_2D && ycbcr_info == NULL && (features & (VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)) && @@ -727,6 +746,17 @@ hk_map_tiling(struct hk_device *dev, const VkImageCreateInfo *info, return AIL_TILING_LINEAR; case VK_IMAGE_TILING_OPTIMAL: + /* G11 is best tested on linear, as the GL driver picks it. Cube views + * of linear images can't be described, and sparse binds whole pages. + */ + if (AGX_G11 && info->mipLevels == 1 && info->samples == 1 && + info->imageType == VK_IMAGE_TYPE_2D && + !(info->flags & (VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT | + VK_IMAGE_CREATE_SPARSE_BINDING_BIT)) && + !vk_format_is_depth_or_stencil(info->format) && + !vk_format_is_compressed(info->format)) + return AIL_TILING_LINEAR; + return AIL_TILING_GPU; case VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT: @@ -760,11 +790,12 @@ static uint32_t modifier_get_score(uint64_t mod) { switch (mod) { + /* G11 shares linear images only, see hk_modifier_features */ case DRM_FORMAT_MOD_APPLE_GPU_TILED_COMPRESSED: - return 10; + return AGX_G11 ? 0 : 10; case DRM_FORMAT_MOD_APPLE_GPU_TILED: - return 5; + return AGX_G11 ? 0 : 5; case DRM_FORMAT_MOD_LINEAR: return 1; @@ -1000,6 +1031,12 @@ hk_CreateImage(VkDevice _device, const VkImageCreateInfo *pCreateInfo, return wsi_common_create_swapchain_image(&pdev->wsi_device, pCreateInfo, pImage); + /* G11 advertises one sample; its texture state has no multisampled + * array dimension to describe more + */ + if (AGX_G11 && pCreateInfo->samples > 1) + return vk_error(dev, VK_ERROR_FEATURE_NOT_PRESENT); + image = vk_zalloc2(&dev->vk.alloc, pAllocator, sizeof(*image), 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); if (!image) diff --git a/src/asahi/vulkan/hk_image_view.c b/src/asahi/vulkan/hk_image_view.c index 27e13e73832..3e2eb7762a2 100644 --- a/src/asahi/vulkan/hk_image_view.c +++ b/src/asahi/vulkan/hk_image_view.c @@ -53,6 +53,15 @@ static enum agx_texture_dimension translate_image_view_type(VkImageViewType view_type, bool msaa, bool layered, enum hk_desc_usage usage) { + /* G11 writes arrays with a longer form that ends the thread, so spilled + * render targets are bound as 2D at their first layer (GL driver's + * agx_upload_spilled_rt_descriptors). + * + * ponytail: single-sampled, one layer + */ + if (AGX_G11 && usage == HK_DESC_USAGE_EMRT) + return AGX_TEXTURE_DIMENSION_2D; + if (usage == HK_DESC_USAGE_EMRT || usage == HK_DESC_USAGE_INPUT || (usage == HK_DESC_USAGE_LAYERED_BG_EOT && layered)) { return msaa ? AGX_TEXTURE_DIMENSION_2D_ARRAY_MULTISAMPLED @@ -257,6 +266,28 @@ pack_texture(struct hk_image_view *view, unsigned view_plane, util_format_compose_swizzles(format_swizzle, view_swizzle, out_swizzle); + /* See g11_lower_border. Depth and stencil borders keep the swizzle's + * constants, colour borders do not. + */ + if (usage == HK_DESC_USAGE_SAMPLED) { + bool zs = util_format_is_depth_or_stencil(p_format); + uint16_t map = 0; + for (unsigned c = 0; c < 4; ++c) { + unsigned code = out_swizzle[c] == PIPE_SWIZZLE_0 ? 4 + : out_swizzle[c] == PIPE_SWIZZLE_1 && zs ? 4 + : out_swizzle[c] == PIPE_SWIZZLE_1 ? 5 + : view_swizzle[c]; + map |= code << (3 * c); + } + + view->planes[view_plane].border_map = map; + } + + /* See translate_image_view_type */ + bool g11_2d = AGX_G11 && usage == HK_DESC_USAGE_EMRT; + if (g11_2d) + layers = 1; + agx_pack(out, TEXTURE, cfg) { cfg.dimension = translate_image_view_type( view->vk.view_type, view->vk.image->samples > 1, layers > 1, usage); @@ -337,7 +368,7 @@ pack_texture(struct hk_image_view *view, unsigned view_plane, (layer * layout->compression_layer_stride_B); } - if (layout->tiling == AIL_TILING_LINEAR && + if (layout->tiling == AIL_TILING_LINEAR && !g11_2d && (hk_image_view_type_is_array(view->vk.view_type))) { cfg.depth_linear = layers; @@ -392,6 +423,11 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, ? image->vk.extent.depth : view->vk.layer_count; + /* See translate_image_view_type */ + bool g11_2d = AGX_G11 && usage == HK_DESC_USAGE_EMRT; + if (g11_2d) + layers = 1; + agx_pack(out, PBE, cfg) { cfg.dimension = translate_image_view_type(view->vk.view_type, msaa, layers > 1, usage); @@ -414,7 +450,8 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, } cfg.buffer = base_addr + ail_get_layer_offset_B(layout, layer); - cfg.unk_mipmapped = layout->levels > 1; + /* Apple sets it on G11 too, but image writes then drop level 0 */ + cfg.unk_mipmapped = layout->levels > 1 && !AGX_G11; if (msaa & !eot) { /* Multisampled images are bound like buffer textures, with @@ -431,7 +468,7 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, cfg.layout = AGX_LAYOUT_LINEAR; cfg.width = AGX_TEXTURE_BUFFER_WIDTH; cfg.height = DIV_ROUND_UP(size_px, cfg.width); - cfg.stride = (cfg.width * blocksize_B) - 4; + cfg.stride = (cfg.width * blocksize_B) - AGX_PBE_STRIDE_BIAS; cfg.layers = 1; cfg.levels = 1; @@ -457,7 +494,7 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, cfg.level = level; } - if (layout->tiling == AIL_TILING_LINEAR && + if (layout->tiling == AIL_TILING_LINEAR && !g11_2d && (hk_image_view_type_is_array(view->vk.view_type))) { cfg.depth_linear = layers; @@ -471,7 +508,8 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, cfg.levels = image->vk.mip_levels; if (layout->tiling == AIL_TILING_LINEAR) { - cfg.stride = ail_get_linear_stride_B(layout, level) - 4; + cfg.stride = + ail_get_linear_stride_B(layout, level) - AGX_PBE_STRIDE_BIAS; assert(cfg.levels == 1); } else { cfg.page_aligned_layers = layout->page_aligned_layers; @@ -479,6 +517,15 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, if (image->vk.samples > 1) cfg.samples = agx_translate_sample_count(image->vk.samples); + + /* G11 image writes take level 0, so point at the level itself */ + if (AGX_G11 && cfg.level) { + cfg.buffer += ail_get_level_offset_B(layout, cfg.level); + cfg.width = u_minify(cfg.width, cfg.level); + cfg.height = u_minify(cfg.height, cfg.level); + cfg.level = 0; + cfg.levels = 1; + } } if (layout->compressed && usage != HK_DESC_USAGE_EMRT) { @@ -516,6 +563,62 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, } } }; + + /* G11 writes arrays, 3D and cubes as 2D through this buffer-like PBE, see + * lower_g11_layered_store. The sideband gives the texel index. + */ + VkImageViewType vt = view->vk.view_type; + if (AGX_G11 && usage == HK_DESC_USAGE_STORAGE && !msaa && + (hk_image_view_type_is_array(vt) || vt == VK_IMAGE_VIEW_TYPE_3D || + vt == VK_IMAGE_VIEW_TYPE_CUBE)) { + + unsigned blocksize_B = util_format_get_blocksize(p_format); + bool linear = layout->tiling == AIL_TILING_LINEAR; + unsigned size_px = + (layout->size_B - ail_get_layer_level_B(layout, layer, level)) / + blocksize_B; + struct ail_tile tile = layout->tilesize_el[level]; + + assert(util_is_power_of_two_nonzero(blocksize_B)); + assert(util_is_aligned(layout->layer_stride_B, 128)); + + agx_pack(out, PBE, cfg) { + cfg.dimension = AGX_TEXTURE_DIMENSION_2D; + cfg.layout = AGX_LAYOUT_LINEAR; + cfg.channels = ail_pixel_format[p_format].channels; + cfg.type = ail_pixel_format[p_format].type; + cfg.srgb = util_format_is_srgb(p_format); + + for (unsigned i = 0; i < desc->nr_channels; ++i) { + if (desc->swizzle[i] == 0) + cfg.swizzle_r = i; + else if (desc->swizzle[i] == 1) + cfg.swizzle_g = i; + else if (desc->swizzle[i] == 2) + cfg.swizzle_b = i; + else if (desc->swizzle[i] == 3) + cfg.swizzle_a = i; + } + + cfg.buffer = base_addr + ail_get_layer_level_B(layout, layer, level); + cfg.width = AGX_TEXTURE_BUFFER_WIDTH; + cfg.height = DIV_ROUND_UP(size_px, cfg.width); + cfg.stride = (cfg.width * blocksize_B) - AGX_PBE_STRIDE_BIAS; + cfg.layers = 1; + cfg.levels = 1; + + cfg.aligned_width_msaa_sw = + linear ? ail_get_linear_stride_B(layout, level) / blocksize_B + : align(util_format_get_nblocksx( + layout->format, u_minify(layout->width_px, level)), + tile.width_el); + cfg.tile_width_sw = linear ? 1 : tile.width_el; + cfg.tile_height_sw = linear ? 1 : tile.height_el; + cfg.layer_stride_sw = layout->layer_stride_B; + } + + out->opaque[4] |= util_logbase2(blocksize_B) << 17; + } } static VkResult diff --git a/src/asahi/vulkan/hk_image_view.h b/src/asahi/vulkan/hk_image_view.h index a4f5546d571..b7998e19f9e 100644 --- a/src/asahi/vulkan/hk_image_view.h +++ b/src/asahi/vulkan/hk_image_view.h @@ -22,6 +22,7 @@ struct hk_image_view { uint8_t plane_count; struct { uint8_t image_plane; + uint16_t border_map; struct agx_texture_packed ia, sampled, ro_storage, background, layered_background, emrt_texture; diff --git a/src/asahi/vulkan/hk_nir_lower_descriptors.c b/src/asahi/vulkan/hk_nir_lower_descriptors.c index 7de70007f1c..b6bb9ba60fa 100644 --- a/src/asahi/vulkan/hk_nir_lower_descriptors.c +++ b/src/asahi/vulkan/hk_nir_lower_descriptors.c @@ -573,6 +573,13 @@ try_lower_intrin(nir_builder *b, nir_intrinsic_instr *intrin, case nir_intrinsic_load_view_index: return lower_sysval_to_root_table(b, intrin, draw.view_index); + case nir_intrinsic_load_layer_id: + if (!AGX_G11) + return false; + + /* G11 renders each layer on its own, see hk_g11_layer_cs */ + return lower_sysval_to_root_table(b, intrin, draw.g11_layer); + case nir_intrinsic_image_deref_load: case nir_intrinsic_image_deref_sparse_load: case nir_intrinsic_image_deref_store: @@ -663,6 +670,26 @@ lower_tex(nir_builder *b, nir_tex_instr *tex, return true; } + /* Sampler word and border map for g11_lower_border */ + if (AGX_G11 && tex->op == nir_texop_sampler_descriptor_amd) { + nir_def *samp = load_resource_deref_desc( + b, 2, 32, nir_src_as_deref(nir_src_for_ssa(sampler)), + plane_offset_B + offsetof(struct hk_sampled_image_descriptor, sampler), + ctx); + + nir_def *map = load_resource_deref_desc( + b, 1, 16, nir_src_as_deref(nir_src_for_ssa(texture)), + plane_offset_B + + offsetof(struct hk_sampled_image_descriptor, border_map), + ctx); + + nir_def_replace(&tex->def, + nir_vec4(b, nir_channel(b, samp, 0), + nir_channel(b, samp, 1), nir_u2u32(b, map), + nir_imm_int(b, 0))); + return true; + } + if (tex->op == nir_texop_custom_border_color_agx) { unsigned offs = offsetof(struct hk_sampled_image_descriptor, border); diff --git a/src/asahi/vulkan/hk_physical_device.c b/src/asahi/vulkan/hk_physical_device.c index bbfda00428d..2c7c4f8d4f3 100644 --- a/src/asahi/vulkan/hk_physical_device.c +++ b/src/asahi/vulkan/hk_physical_device.c @@ -42,7 +42,8 @@ hk_get_vk_version() if (version_override) return version_override; - return VK_MAKE_VERSION(1, 4, VK_HEADER_VERSION); + /* G11 lacks the subgroup operations 1.4 requires */ + return VK_MAKE_VERSION(1, AGX_G11 ? 3 : 4, VK_HEADER_VERSION); } static void @@ -107,7 +108,8 @@ hk_get_device_extensions(const struct hk_instance *instance, .KHR_push_descriptor = true, .KHR_relaxed_block_layout = true, .KHR_robustness2 = true, - .KHR_sampler_mirror_clamp_to_edge = true, + /* G11's wrap modes stop at clamp to border (AGXMetalA12) */ + .KHR_sampler_mirror_clamp_to_edge = !AGX_G11, .KHR_sampler_ycbcr_conversion = true, .KHR_separate_depth_stencil_layouts = true, .KHR_shader_atomic_int64 = false, @@ -121,10 +123,10 @@ hk_get_device_extensions(const struct hk_instance *instance, .KHR_shader_integer_dot_product = true, .KHR_shader_maximal_reconvergence = true, .KHR_shader_non_semantic_info = true, - .KHR_shader_quad_control = true, + .KHR_shader_quad_control = !AGX_G11, .KHR_shader_relaxed_extended_instruction = true, .KHR_shader_subgroup_extended_types = true, - .KHR_shader_subgroup_rotate = true, + .KHR_shader_subgroup_rotate = !AGX_G11, .KHR_shader_subgroup_uniform_control_flow = true, .KHR_shader_terminate_invocation = true, .KHR_shader_untyped_pointers = true, @@ -151,7 +153,8 @@ hk_get_device_extensions(const struct hk_instance *instance, .EXT_calibrated_timestamps = true, .EXT_conditional_rendering = false, .EXT_color_write_enable = true, - .EXT_custom_border_color = true, + /* G11: no state slot for the clamp-to-0 sampler of the emulation */ + .EXT_custom_border_color = !AGX_G11, .EXT_debug_marker = true, .EXT_depth_bias_control = true, .EXT_depth_clip_control = false, @@ -200,7 +203,7 @@ hk_get_device_extensions(const struct hk_instance *instance, .EXT_provoking_vertex = true, .EXT_queue_family_foreign = true, .EXT_robustness2 = true, - .EXT_sample_locations = true, + .EXT_sample_locations = !AGX_G11, .EXT_sampler_filter_minmax = false, .EXT_scalar_block_layout = true, .EXT_separate_stencil_usage = true, @@ -210,17 +213,17 @@ hk_get_device_extensions(const struct hk_instance *instance, .EXT_shader_object = true, .EXT_shader_replicated_composites = true, .EXT_shader_stencil_export = true, - .EXT_shader_subgroup_ballot = true, - .EXT_shader_subgroup_vote = true, + .EXT_shader_subgroup_ballot = !AGX_G11, + .EXT_shader_subgroup_vote = !AGX_G11, .EXT_shader_uniform_buffer_unsized_array = true, - .EXT_shader_viewport_index_layer = true, + .EXT_shader_viewport_index_layer = !AGX_G11, .EXT_subgroup_size_control = true, #ifdef HK_USE_WSI_PLATFORM .EXT_swapchain_maintenance1 = true, #endif .EXT_texel_buffer_alignment = true, .EXT_tooling_info = true, - .EXT_transform_feedback = true, + .EXT_transform_feedback = !AGX_G11, .EXT_vertex_attribute_divisor = true, .EXT_vertex_input_dynamic_state = true, .EXT_ycbcr_2plane_444_formats = false, @@ -247,10 +250,13 @@ hk_get_device_features( .fullDrawIndexUint32 = true, .imageCubeArray = true, .independentBlend = true, - .geometryShader = true, - .tessellationShader = true, + /* G11: geometry and tessellation hang in the TA, dual source blending + * does not compile yet + */ + .geometryShader = !AGX_G11, + .tessellationShader = !AGX_G11, .sampleRateShading = true, - .dualSrcBlend = true, + .dualSrcBlend = !AGX_G11, .logicOp = true, .multiDrawIndirect = true, .drawIndirectFirstInstance = true, @@ -261,16 +267,17 @@ hk_get_device_features( .wideLines = true, .largePoints = true, .alphaToOne = true, - .multiViewport = true, + /* G11: one viewport, layer writes dropped (agx_nir_lower_uvs) */ + .multiViewport = !AGX_G11, .samplerAnisotropy = true, .textureCompressionETC2 = true, - .textureCompressionBC = true, + .textureCompressionBC = !AGX_G11, .textureCompressionASTC_LDR = true, .occlusionQueryPrecise = true, .pipelineStatisticsQuery = true, .vertexPipelineStoresAndAtomics = instance->drirc.misc.enable_vertex_pipeline_stores_atomics, .fragmentStoresAndAtomics = true, - .shaderTessellationAndGeometryPointSize = true, + .shaderTessellationAndGeometryPointSize = !AGX_G11, .shaderImageGatherExtended = true, .shaderStorageImageExtendedFormats = true, /* TODO: hitting the vertex shader timeout in CTS, but should work */ @@ -286,7 +293,8 @@ hk_get_device_features( .shaderFloat64 = false, .shaderInt64 = true, .shaderInt16 = true, - .shaderResourceResidency = true, + /* G11: no sparse texture reads, and unbound pages would fault */ + .shaderResourceResidency = !AGX_G11, .shaderResourceMinLod = true, .sparseBinding = true, @@ -296,9 +304,9 @@ hk_get_device_features( .sparseResidency2Samples = false, .sparseResidency4Samples = false, .sparseResidency8Samples = false, - .sparseResidencyAliased = true, - .sparseResidencyImage2D = true, - .sparseResidencyBuffer = true, + .sparseResidencyAliased = !AGX_G11, + .sparseResidencyImage2D = !AGX_G11, + .sparseResidencyBuffer = !AGX_G11, /* This needs investigation. */ .sparseResidencyImage3D = false, @@ -319,7 +327,7 @@ hk_get_device_features( .samplerYcbcrConversion = true, /* Vulkan 1.2 */ - .samplerMirrorClampToEdge = true, + .samplerMirrorClampToEdge = !AGX_G11, .drawIndirectCount = true, .storageBuffer8BitAccess = true, .uniformAndStorageBuffer8BitAccess = true, @@ -363,8 +371,8 @@ hk_get_device_features( .vulkanMemoryModel = true, .vulkanMemoryModelDeviceScope = true, .vulkanMemoryModelAvailabilityVisibilityChains = false, - .shaderOutputViewportIndex = true, - .shaderOutputLayer = true, + .shaderOutputViewportIndex = !AGX_G11, + .shaderOutputLayer = !AGX_G11, .subgroupBroadcastDynamicId = true, /* Vulkan 1.3 */ @@ -461,11 +469,11 @@ hk_get_device_features( .shaderMaximalReconvergence = true, /* VK_KHR_shader_quad_control */ - .shaderQuadControl = true, + .shaderQuadControl = !AGX_G11, /* VK_KHR_shader_subgroup_rotate */ - .shaderSubgroupRotate = true, - .shaderSubgroupRotateClustered = true, + .shaderSubgroupRotate = !AGX_G11, + .shaderSubgroupRotateClustered = !AGX_G11, /* VK_KHR_vertex_attribute_divisor */ .vertexAttributeInstanceRateDivisor = true, @@ -499,8 +507,8 @@ hk_get_device_features( .inheritedConditionalRendering = false, /* VK_EXT_custom_border_color */ - .customBorderColors = true, - .customBorderColorWithoutFormat = true, + .customBorderColors = !AGX_G11, + .customBorderColorWithoutFormat = !AGX_G11, /* VK_EXT_depth_bias_control */ .depthBiasControl = true, @@ -646,8 +654,8 @@ hk_get_device_features( .texelBufferAlignment = true, /* VK_EXT_transform_feedback */ - .transformFeedback = true, - .geometryStreams = true, + .transformFeedback = !AGX_G11, + .geometryStreams = !AGX_G11, /* VK_EXT_vertex_input_dynamic_state */ .vertexInputDynamicState = true, @@ -679,8 +687,11 @@ hk_get_device_properties(const struct agx_device *dev, struct vk_properties *properties) { /* Note: update nir_shader_compiler_options.max_samples when changing this. */ + /* G11: multisampled rendering is GL only so far (agx_g11_ms_layers) */ const VkSampleCountFlagBits sample_counts = - VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | VK_SAMPLE_COUNT_4_BIT; + AGX_G11 ? VK_SAMPLE_COUNT_1_BIT + : VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | + VK_SAMPLE_COUNT_4_BIT; uint64_t os_page_size = 16384; os_get_page_size(&os_page_size); @@ -762,7 +773,7 @@ hk_get_device_properties(const struct agx_device *dev, /* Set to match maxVertexOutputComponents, hardware limit is higher. */ .maxFragmentInputComponents = max_vgt_output_components, .maxFragmentOutputAttachments = HK_MAX_RTS, - .maxFragmentDualSrcAttachments = 1, + .maxFragmentDualSrcAttachments = AGX_G11 ? 0 : 1, .maxFragmentCombinedOutputResources = HK_MAX_RTS + HK_MAX_DESCRIPTORS, .maxComputeSharedMemorySize = HK_MAX_SHARED_SIZE, .maxComputeWorkGroupCount = {0x7fffffff, 65535, 65535}, @@ -775,7 +786,7 @@ hk_get_device_properties(const struct agx_device *dev, .maxDrawIndirectCount = UINT16_MAX, .maxSamplerLodBias = 15, .maxSamplerAnisotropy = 16, - .maxViewports = HK_MAX_VIEWPORTS, + .maxViewports = AGX_G11 ? 1 : HK_MAX_VIEWPORTS, .maxViewportDimensions = {32768, 32768}, .viewportBoundsRange = {-65536, 65536}, .viewportSubPixelBits = 8, @@ -792,7 +803,7 @@ hk_get_device_properties(const struct agx_device *dev, .subPixelInterpolationOffsetBits = 4, .maxFramebufferHeight = 16384, .maxFramebufferWidth = 16384, - .maxFramebufferLayers = 2048, + .maxFramebufferLayers = AGX_G11 ? HK_G11_MAX_LAYERS : 2048, .framebufferColorSampleCounts = sample_counts, .framebufferDepthSampleCounts = sample_counts, .framebufferNoAttachmentsSampleCounts = sample_counts, @@ -839,8 +850,12 @@ hk_get_device_properties(const struct agx_device *dev, /* Vulkan 1.1 properties */ .subgroupSize = 32, .subgroupSupportedStages = - VK_SHADER_STAGE_COMPUTE_BIT | VK_SHADER_STAGE_ALL_GRAPHICS, + AGX_G11 ? VK_SHADER_STAGE_COMPUTE_BIT | VK_SHADER_STAGE_VERTEX_BIT | + VK_SHADER_STAGE_FRAGMENT_BIT + : VK_SHADER_STAGE_COMPUTE_BIT | VK_SHADER_STAGE_ALL_GRAPHICS, + /* G11 has no SIMD-group operations beyond barriers and elect */ .subgroupSupportedOperations = + AGX_G11 ? VK_SUBGROUP_FEATURE_BASIC_BIT : VK_SUBGROUP_FEATURE_BASIC_BIT | VK_SUBGROUP_FEATURE_BALLOT_BIT | VK_SUBGROUP_FEATURE_VOTE_BIT | VK_SUBGROUP_FEATURE_QUAD_BIT | VK_SUBGROUP_FEATURE_SHUFFLE_BIT | @@ -849,7 +864,7 @@ hk_get_device_properties(const struct agx_device *dev, VK_SUBGROUP_FEATURE_ARITHMETIC_BIT | VK_SUBGROUP_FEATURE_CLUSTERED_BIT | VK_SUBGROUP_FEATURE_ROTATE_CLUSTERED_BIT_KHR, - .subgroupQuadOperationsInAllStages = true, + .subgroupQuadOperationsInAllStages = !AGX_G11, .pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY, .maxMultiviewViewCount = HK_MAX_MULTIVIEW_VIEW_COUNT, .maxMultiviewInstanceIndex = UINT32_MAX, diff --git a/src/asahi/vulkan/hk_private.h b/src/asahi/vulkan/hk_private.h index df6915f9069..cec447e3b8c 100644 --- a/src/asahi/vulkan/hk_private.h +++ b/src/asahi/vulkan/hk_private.h @@ -16,6 +16,7 @@ #define HK_MAX_PUSH_SIZE 256 #define HK_MAX_DYNAMIC_BUFFERS 64 #define HK_MAX_RTS 8 +#define HK_G11_MAX_LAYERS 256 #define HK_MIN_SSBO_ALIGNMENT 16 #define HK_MIN_TEXEL_BUFFER_ALIGNMENT 16 #define HK_MIN_UBO_ALIGNMENT 64 diff --git a/src/asahi/vulkan/hk_queue.c b/src/asahi/vulkan/hk_queue.c index e7e6aec2e51..7a56372b7f5 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -164,6 +164,13 @@ asahi_fill_vdm_command(struct hk_device *dev, struct hk_cs *cs, c->layers = 32; } + /* ponytail: G11 layered rendering is not known, everything is drawn to + * layer 0 (agx_nir_lower_uvs drops layer writes). Splitting renders per + * layer would cover it. + */ + if (AGX_G11) + c->layers = 1; + c->ppp_multisamplectl = cs->ppp_multisamplectl; c->sample_size_B = cs->tib.sample_size_B; @@ -274,10 +281,9 @@ queue_submit_single(struct hk_device *dev, struct drm_asahi_submit *submit, if (dev->dev.is_virtio) u_rwlock_rdunlock(&dev->external_bos.lock); - /* XXX: don't trap */ if (ret) { fprintf(stderr, "DRM_IOCTL_ASAHI_SUBMIT failed: %m\n"); - assert(0); + return VK_ERROR_DEVICE_LOST; } return VK_SUCCESS; diff --git a/src/asahi/vulkan/hk_sampler.c b/src/asahi/vulkan/hk_sampler.c index bbd98c09706..e16b8d23ff0 100644 --- a/src/asahi/vulkan/hk_sampler.c +++ b/src/asahi/vulkan/hk_sampler.c @@ -212,6 +212,10 @@ hk_CreateSampler(VkDevice device, struct agx_sampler_packed samp; pack_sampler(pdev, info, true, workaround_rgba4, &samp); + /* G11 adds the border in the shader, see g11_lower_border */ + if (AGX_G11 && uses_border(info)) + sampler->custom_border = sampler->vk.border_color_value; + /* LOD bias passed in the descriptor set */ sampler->lod_bias_fp16 = _mesa_float_to_half(info->mipLodBias); diff --git a/src/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index e12393a8f85..074ea793e1a 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -105,6 +105,28 @@ hk_physical_device_compiler_flags(const struct hk_physical_device *pdev) return pdev->dev.debug; } +/* + * A GPU fault wedges G11 until reboot (the firmware has no soft faults), so + * bound every buffer access whatever the pipeline asks for. + */ +static const struct vk_pipeline_robustness_state * +hk_robustness(const struct vk_pipeline_robustness_state *rs) +{ + static const struct vk_pipeline_robustness_state g11 = { + .storage_buffers = + VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_2_EXT, + .uniform_buffers = + VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_2_EXT, + .vertex_inputs = + VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_ROBUST_BUFFER_ACCESS_2_EXT, + .images = VK_PIPELINE_ROBUSTNESS_IMAGE_BEHAVIOR_ROBUST_IMAGE_ACCESS_2_EXT, + .null_uniform_buffer_descriptor = true, + .null_storage_buffer_descriptor = true, + }; + + return AGX_G11 ? &g11 : rs; +} + const nir_shader_compiler_options * hk_get_nir_options(struct vk_physical_device *vk_pdev, mesa_shader_stage stage, UNUSED const struct vk_pipeline_robustness_state *rs) @@ -117,6 +139,7 @@ hk_get_spirv_options(struct vk_physical_device *vk_pdev, UNUSED mesa_shader_stage stage, const struct vk_pipeline_robustness_state *rs) { + rs = hk_robustness(rs); return (struct spirv_to_nir_options){ .ssbo_addr_format = hk_buffer_addr_format(rs->storage_buffers), .phys_ssbo_addr_format = nir_address_format_64bit_global, @@ -265,6 +288,10 @@ enum hk_feature_key { static enum hk_feature_key hk_make_feature_key(const struct vk_features *feats) { + /* G11 has no custom border colours, see g11_lower_border */ + if (AGX_G11) + return 0; + return (!feats || feats->customBorderColors) ? HK_FEAT_CUSTOM_BORDER : 0; } @@ -573,6 +600,68 @@ hk_lower_multiview(nir_shader *nir) b.shader->info.outputs_written |= VARYING_BIT_LAYER; } +/* + * G11 records a layered render once per layer (hk_g11_layer_cs, the layer is + * load_layer_id). The vertex shader collapses primitives of other layers to + * one point outside the clip volume, and the fragment + * shader's gl_Layer is the render's layer. + */ +static void +hk_g11_lower_layer_vs(nir_shader *nir) +{ + nir_builder b = + nir_builder_at(nir_after_impl(nir_shader_get_entrypoint(nir))); + + nir_variable *layer = + nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_LAYER); + nir_variable *pos = + nir_find_variable_with_location(nir, nir_var_shader_out, VARYING_SLOT_POS); + + if (!pos) + return; + + /* The hardware drops layer writes (agx_nir_lower_uvs), keep it as a + * temporary to read back + */ + if (layer) { + layer->data.mode = nir_var_shader_temp; + layer->data.location = 0; + nir_fixup_deref_modes(nir); + nir->info.outputs_written &= ~VARYING_BIT_LAYER; + } + + /* Write the position once at the end, outside control flow */ + nir_variable *out = nir_variable_clone(pos, nir); + nir_shader_add_variable(nir, out); + pos->data.mode = nir_var_shader_temp; + pos->data.location = 0; + nir_fixup_deref_modes(nir); + + nir_def *l = layer ? nir_load_var(&b, layer) : nir_imm_int(&b, 0); + nir_def *cull = nir_imm_vec4(&b, -2.0, -2.0, -2.0, 1.0); + nir_store_var(&b, out, + nir_bcsel(&b, nir_ine(&b, l, nir_load_layer_id(&b)), cull, + nir_load_var(&b, pos)), + 0xf); +} + +static bool +hk_g11_lower_layer_fs_instr(nir_builder *b, nir_intrinsic_instr *intr, + UNUSED void *data) +{ + if (intr->intrinsic != nir_intrinsic_load_deref) + return false; + + nir_variable *var = nir_intrinsic_get_var(intr, 0); + if (!var || var->data.mode != nir_var_shader_in || + var->data.location != VARYING_SLOT_LAYER) + return false; + + b->cursor = nir_before_instr(&intr->instr); + nir_def_replace(&intr->def, nir_load_layer_id(b)); + return true; +} + static nir_def * query_custom_border(nir_builder *b, nir_tex_instr *tex) { @@ -659,6 +748,242 @@ agx_nir_lower_custom_border(nir_shader *nir) return nir_shader_tex_pass(nir, lower, nir_metadata_none, NULL); } +#if AGX_G11 +/* + * G11 samplers have no border colour: clamp to border reads transparent black, + * applied after the swizzle for colour formats (docs/gpu.md). Add the border + * colour times the filter weight of texels outside the image. The weight comes + * from the coordinates, the LOD and the sampler word, which the descriptor + * holds (hk_sampled_image_descriptor, read through sampler_descriptor_amd). + * Sampling through a state swizzled to 1 does not give it: G11 keeps the + * constants at the border. + * + * ponytail: no anisotropy, depth compares or view min LOD; add them when a + * test needs them + */ + +/* No ubfe on AGX */ +static nir_def * +g11_bits(nir_builder *b, nir_def *x, unsigned offset, unsigned bits) +{ + return nir_iand_imm(b, nir_ushr_imm(b, x, offset), BITFIELD_MASK(bits)); +} + +static bool +g11_border_sample(const nir_tex_instr *tex) +{ + return nir_tex_instr_need_sampler(tex) && !nir_tex_instr_is_query(tex) && + !tex->is_shadow && tex->sampler_dim != GLSL_SAMPLER_DIM_CUBE; +} + +/* + * Each sample adds about 180 instructions, and G11 runs very big shaders + * wrong (robustness2's 126 samples made 150 KiB). + * + * ponytail: shaders with more than 32 samples keep the transparent black + * border; find G11's code size limit if that matters + */ +static unsigned +g11_border_samples(nir_shader *nir) +{ + unsigned n = 0; + nir_foreach_function_impl(impl, nir) { + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_tex && + g11_border_sample(nir_instr_as_tex(instr))) + ++n; + } + } + } + + return n; +} + +static bool +g11_lower_border(nir_builder *b, nir_tex_instr *tex, UNUSED void *data) +{ + if (!g11_border_sample(tex)) + return false; + + /* Derivatives before the branch, which may diverge */ + b->cursor = nir_before_instr(&tex->instr); + unsigned n = tex->coord_components - tex->is_array; + nir_def *coord = nir_f2f32( + b, nir_trim_vector(b, nir_get_tex_src(tex, nir_tex_src_coord), n)); + nir_def *offset = nir_get_tex_src(tex, nir_tex_src_offset); + + nir_def *dx = NULL, *dy = NULL; + if (tex->op == nir_texop_txd) { + dx = nir_f2f32(b, nir_get_tex_src(tex, nir_tex_src_ddx)); + dy = nir_f2f32(b, nir_get_tex_src(tex, nir_tex_src_ddy)); + } else if ((tex->op == nir_texop_tex || tex->op == nir_texop_txb) && + b->shader->info.stage == MESA_SHADER_FRAGMENT) { + dx = nir_ddx(b, coord); + dy = nir_ddy(b, coord); + } + + b->cursor = nir_after_instr(&tex->instr); + nir_instr *orig = nir_instr_clone(b->shader, &tex->instr); + nir_builder_instr_insert(b, orig); + nir_def *res = &nir_instr_as_tex(orig)->def; + + /* A transparent black border is left to the hardware, robustness2 wants + * it as it is + */ + nir_def *border = query_custom_border(b, tex); + nir_push_if(b, nir_ine_imm(b, nir_channel(b, border, 3), 0)); + nir_def *replaced; + { + nir_def *samp = nir_build_texture_query( + b, tex, nir_texop_sampler_descriptor_amd, 4, nir_type_uint32, false, + false); + nir_def *size0 = nir_trim_vector( + b, + nir_build_texture_query(b, tex, nir_texop_txs, n + tex->is_array, + nir_type_int32, false, true), + n); + nir_def *levels = nir_build_texture_query( + b, tex, nir_texop_query_levels, 1, nir_type_int32, false, false); + nir_def *lo = nir_channel(b, samp, 0), *hi = nir_channel(b, samp, 1); + nir_def *map = nir_channel(b, samp, 2); + nir_def *zero = nir_imm_float(b, 0.0), *one_f = nir_imm_float(b, 1.0); + nir_def *unnorm = nir_i2b(b, g11_bits(b, hi, 6, 1)); + nir_def *s0 = nir_i2f32(b, size0); + + /* Vulkan LOD, unnormalized coordinates and gathers use level 0 */ + nir_def *lod = zero; + if (tex->op == nir_texop_txl) { + lod = nir_f2f32(b, nir_get_tex_src(tex, nir_tex_src_lod)); + } else if (dx) { + nir_def *sx = nir_fmul(b, dx, s0), *sy = nir_fmul(b, dy, s0); + nir_def *rho2 = nir_fmax(b, nir_fdot(b, sx, sx), nir_fdot(b, sy, sy)); + lod = nir_fmul_imm(b, nir_flog2(b, rho2), 0.5); + } + + nir_def *bias = nir_get_tex_src(tex, nir_tex_src_bias); + if (bias) + lod = nir_fadd(b, lod, nir_f2f32(b, bias)); + + nir_def *min_lod = nir_fmul_imm(b, nir_u2f32(b, g11_bits(b, lo, 0, 10)), + 1.0 / 64.0); + nir_def *max_lod = nir_fmul_imm( + b, nir_u2f32(b, g11_bits(b, lo, 10, 10)), 1.0 / 64.0); + lod = nir_bcsel(b, unnorm, zero, nir_fclamp(b, lod, min_lod, max_lod)); + + /* Filter: magnify at LOD 0 and below. Gathers read like linear. */ + nir_def *linear = nir_ieq_imm( + b, + nir_bcsel(b, nir_fle_imm(b, lod, 0.0), g11_bits(b, lo, 23, 2), + g11_bits(b, lo, 25, 2)), + 1); + if (tex->op == nir_texop_tg4) + linear = nir_imm_true(b); + + /* Mip levels A and B blended by delta (Vulkan 16.6.6) */ + nir_def *mip = g11_bits(b, lo, 27, 2); + nir_def *q = nir_fadd_imm(b, nir_i2f32(b, levels), -1.0); + nir_def *lc = nir_bcsel(b, nir_ieq_imm(b, mip, 0), zero, + nir_fclamp(b, lod, zero, q)); + nir_def *mip_linear = nir_ieq_imm(b, mip, 2); + nir_def *da = nir_bcsel(b, mip_linear, nir_ffloor(b, lc), + nir_ffloor(b, nir_fadd_imm(b, lc, 0.5))); + nir_def *db = nir_bcsel(b, mip_linear, + nir_fmin(b, nir_fadd_imm(b, da, 1.0), q), da); + nir_def *delta = nir_bcsel(b, mip_linear, nir_fsub(b, lc, da), zero); + + /* In-image filter weight per axis, x in texels: nearest reads the + * texel under x, linear the two around it + */ + nir_def *gather_in[2][2]; + nir_def *inside[2]; + for (unsigned l = 0; l < 2; ++l) { + nir_def *size = nir_i2f32( + b, nir_imax_imm(b, nir_ushr(b, size0, nir_f2u32(b, l ? db : da)), + 1)); + + inside[l] = one_f; + for (unsigned a = 0; a < n; ++a) { + nir_def *s = nir_channel(b, size, a); + nir_def *x = nir_channel(b, coord, a); + x = nir_bcsel(b, unnorm, x, nir_fmul(b, x, s)); + if (offset) + x = nir_fadd(b, x, nir_i2f32(b, nir_channel(b, offset, a))); + + nir_def *w_lin = nir_fsat( + b, nir_fmin(b, nir_fadd_imm(b, x, 0.5), + nir_fsub(b, nir_fadd_imm(b, s, 0.5), x))); + nir_def *w_near = nir_b2f32( + b, nir_iand(b, nir_fge(b, x, zero), nir_flt(b, x, s))); + nir_def *w = nir_bcsel(b, linear, w_lin, w_near); + + nir_def *border_axis = + nir_ieq_imm(b, g11_bits(b, a == 0 ? lo : hi, a == 0 ? 29 : 3 * (a - 1), 3), 3); + inside[l] = nir_fmul(b, inside[l], + nir_bcsel(b, border_axis, w, one_f)); + + /* Gathers: texels floor(x - 0.5) and the next */ + if (tex->op == nir_texop_tg4 && !l && a < 2) { + nir_def *i0 = nir_ffloor(b, nir_fadd_imm(b, x, -0.5)); + nir_def *i1 = nir_fadd_imm(b, i0, 1.0); + gather_in[a][0] = nir_ior( + b, nir_inot(b, border_axis), + nir_iand(b, nir_fge(b, i0, zero), nir_flt(b, i0, s))); + gather_in[a][1] = nir_ior( + b, nir_inot(b, border_axis), + nir_iand(b, nir_fge(b, i1, zero), nir_flt(b, i1, s))); + } + } + } + + /* The hardware border is transparent black after the swizzle, so each + * channel adds its border component or its swizzle constant + */ + bool is_float = + nir_alu_type_get_base_type(tex->dest_type) == nir_type_float; + unsigned bits = res->bit_size; + nir_def *one = + is_float ? nir_imm_floatN_t(b, 1.0, bits) : nir_imm_intN_t(b, 1, bits); + nir_def *zbits = nir_imm_zero(b, 1, bits); + nir_def *outside = + nir_fsub_imm(b, 1.0, nir_flrp(b, inside[0], inside[1], delta)); + b->shader->info.flrp_lowered = false; + + /* Gather order: (i0, j1), (i1, j1), (i1, j0), (i0, j0) */ + static const unsigned ij[4][2] = {{0, 1}, {1, 1}, {1, 0}, {0, 0}}; + + nir_def *out[4]; + for (unsigned c = 0; c < res->num_components; ++c) { + unsigned ch = tex->op == nir_texop_tg4 ? tex->component : c; + nir_def *code = g11_bits(b, map, 3 * ch, 3); + nir_def *add = + nir_bcsel(b, nir_ult(b, code, nir_imm_int(b, 4)), + nir_vector_extract(b, border, code), + nir_bcsel(b, nir_ieq_imm(b, code, 5), one, zbits)); + + nir_def *r = nir_channel(b, res, c); + if (tex->op == nir_texop_tg4) { + nir_def *in = nir_iand(b, gather_in[0][ij[c][0]], + gather_in[1][ij[c][1]]); + add = nir_bcsel(b, in, zbits, add); + out[c] = is_float ? nir_fadd(b, r, add) : nir_iadd(b, r, add); + } else if (is_float) { + out[c] = nir_ffma(b, nir_f2fN(b, outside, bits), add, r); + } else { + out[c] = nir_iadd(b, r, nir_bcsel(b, nir_fgt_imm(b, outside, 0.5), + add, zbits)); + } + } + + replaced = nir_vec(b, out, res->num_components); + } + nir_pop_if(b, NULL); + + nir_def_replace(&tex->def, nir_if_phi(b, replaced, res)); + return true; +} +#endif + static nir_def * query_min_lod(nir_builder *b, nir_tex_instr *tex, bool int_coords) { @@ -783,6 +1108,125 @@ should_lower_robust(const nir_intrinsic_instr *intr, const void *_) intr->intrinsic == nir_intrinsic_image_deref_atomic_swap; } +#if AGX_G11 +static nir_deref_instr * +hk_g11_dynamic_deref(nir_src src) +{ + nir_deref_instr *d = nir_src_as_deref(src); + return d && d->deref_type == nir_deref_type_array && + !nir_src_is_const(d->arr.index) + ? d + : NULL; +} + +static nir_src * +hk_g11_src(nir_instr *instr, unsigned i) +{ + return instr->type == nir_instr_type_tex + ? &nir_instr_as_tex(instr)->src[i].src + : &nir_instr_as_intrinsic(instr)->src[i]; +} + +/* + * G11 samples from state slots fixed at compile time (hk_g11_lower_handles), + * so a descriptor array indexed at run time becomes a chain of accesses with + * constant indices. Only 16 textures fit, so indices past 16 read zero, as do + * indices past the array. + */ +static bool +hk_g11_unroll_index(nir_builder *b, nir_instr *instr, UNUSED void *data) +{ + nir_deref_instr *d = NULL; + nir_def *def = NULL, *index; + unsigned src, len; + + if (instr->type == nir_instr_type_tex) { + nir_tex_instr *tex = nir_instr_as_tex(instr); + int o = nir_tex_instr_src_index(tex, nir_tex_src_texture_offset); + int t = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref); + int s = nir_tex_instr_src_index(tex, nir_tex_src_sampler_deref); + def = &tex->def; + + if (o >= 0 && !nir_src_is_const(tex->src[o].src)) { + /* Arrayed input attachments */ + src = o; + index = tex->src[o].src.ssa; + len = tex->texture_array_size; + } else if (t >= 0 && (d = hk_g11_dynamic_deref(tex->src[t].src))) { + src = t; + } else if (s >= 0 && (d = hk_g11_dynamic_deref(tex->src[s].src))) { + src = s; + } else { + return false; + } + } else if (instr->type == nir_instr_type_intrinsic) { + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + switch (intr->intrinsic) { + case nir_intrinsic_image_deref_load: + case nir_intrinsic_image_deref_sparse_load: + def = &intr->def; + break; + case nir_intrinsic_image_deref_store: + break; + default: + return false; + } + + src = 0; + if (!(d = hk_g11_dynamic_deref(intr->src[0]))) + return false; + } else { + return false; + } + + if (d) { + index = d->arr.index.ssa; + len = glsl_get_length(nir_deref_instr_parent(d)->type); + } + + len = len ? MIN2(len, 16) : 16; + b->cursor = nir_before_instr(instr); + nir_def *res = def ? nir_undef(b, def->num_components, def->bit_size) : NULL; + + for (unsigned i = 0; i < len; ++i) { + nir_push_if(b, nir_ieq_imm(b, index, i)); + + nir_def *repl = + d ? &nir_build_deref_array_imm(b, nir_deref_instr_parent(d), i)->def + : nir_imm_intN_t(b, i, index->bit_size); + + nir_instr *c = nir_instr_clone(b->shader, instr); + nir_builder_instr_insert(b, c); + nir_src_rewrite(hk_g11_src(c, src), repl); + + /* Combined image samplers share the deref */ + if (d && c->type == nir_instr_type_tex) { + nir_tex_instr *tex = nir_instr_as_tex(c); + for (unsigned j = 0; j < tex->num_srcs; ++j) { + if (tex->src[j].src.ssa == &d->def) + nir_src_rewrite(&tex->src[j].src, repl); + } + } + + nir_pop_if(b, NULL); + + if (def) { + nir_def *cdef = c->type == nir_instr_type_tex + ? &nir_instr_as_tex(c)->def + : &nir_instr_as_intrinsic(c)->def; + res = nir_if_phi(b, cdef, res); + } + } + + if (def) + nir_def_replace(def, res); + else + nir_instr_remove(instr); + + return true; +} +#endif + static void hk_lower_nir(struct hk_device *dev, nir_shader *nir, const struct vk_pipeline_robustness_state *rs, bool is_multiview, @@ -794,6 +1238,8 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, rs = &vk_robustness_disabled; } + rs = hk_robustness(rs); + const nir_opt_access_options access_options = { .is_vulkan = true, }; @@ -814,8 +1260,15 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, if (nir->info.stage == MESA_SHADER_VERTEX) { if (is_multiview) hk_lower_multiview(nir); + + if (AGX_G11) + hk_g11_lower_layer_vs(nir); } + if (AGX_G11 && nir->info.stage == MESA_SHADER_FRAGMENT) + NIR_PASS(_, nir, nir_shader_intrinsics_pass, hk_g11_lower_layer_fs_instr, + nir_metadata_control_flow, NULL); + if (nir->info.stage == MESA_SHADER_TESS_EVAL) { NIR_PASS(_, nir, nir_lower_patch_vertices, nir->info.tess.tcs_vertices_out, NULL); @@ -855,6 +1308,20 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, NIR_PASS(_, nir, agx_nir_lower_custom_border); } +#if AGX_G11 + if (g11_border_samples(nir) <= 32) + NIR_PASS(_, nir, nir_shader_tex_pass, g11_lower_border, + nir_metadata_none, NULL); + + /* A separate sampler array may need a second round */ + bool unrolled; + do { + unrolled = false; + NIR_PASS(unrolled, nir, nir_shader_instructions_pass, + hk_g11_unroll_index, nir_metadata_none, NULL); + } while (unrolled); +#endif + NIR_PASS(_, nir, hk_nir_lower_descriptors, rs, set_layout_count, set_layouts); NIR_PASS(_, nir, nir_lower_explicit_io, nir_var_mem_global, @@ -928,6 +1395,8 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, NIR_PASS(_, nir, agx_nir_lower_texture); NIR_PASS(_, nir, agx_nir_lower_multisampled_image_store); + if (AGX_G11) + NIR_PASS(_, nir, agx_nir_lower_g11_layered_image_store); agx_preprocess_nir(nir); @@ -996,6 +1465,123 @@ hk_init_link_ht(struct hk_shader *shader, mesa_shader_stage sw_stage) : VK_SUCCESS; } +#if AGX_G11 +static int +hk_g11_slot(struct hk_g11_state *tab, uint8_t *n, unsigned max, + nir_intrinsic_instr *handle) +{ + /* hk_g11_unroll_index leaves only constant offsets */ + uint32_t offs = 0; + if (nir_src_is_const(handle->src[0])) + offs = nir_src_as_uint(handle->src[0]); + else + mesa_logw_once("G11: dynamically indexed descriptor array"); + uint8_t set = nir_intrinsic_desc_set(handle); + + for (unsigned i = 0; i < *n; ++i) { + if (tab[i].set == set && tab[i].offset_B == offs) + return i < max ? i : -1; + } + + if (*n >= max) + return -1; + + tab[*n] = (struct hk_g11_state){.offset_B = offs, .set = set}; + return (*n)++; +} + +/* + * Give each descriptor the shader reads a texture or sampler state slot + * (hk_g11_bind_state fills them). The bindless handles stay for descriptor + * reads such as size queries; G11 samples from the slot. + */ +static bool +hk_g11_lower_handles(nir_builder *b, nir_instr *instr, void *data) +{ + struct hk_shader_info *info = data; + + if (instr->type == nir_instr_type_tex) { + nir_tex_instr *tex = nir_instr_as_tex(instr); + int s = nir_tex_instr_src_index(tex, nir_tex_src_sampler_handle); + bool progress = false; + if (s >= 0) { + nir_intrinsic_instr *h = nir_src_as_intrinsic(tex->src[s].src); + if (h && h->intrinsic == nir_intrinsic_bindless_sampler_agx) { + int slot = hk_g11_slot(info->g11_samp, &info->g11_nr_samp, + HK_G11_MAX_SAMPLERS, h); + tex->sampler_index = 1 + MAX2(slot, 0); + + /* Only sampling reads it, and the preamble would turn it into + * a heap index the compiler can't tell apart + */ + nir_tex_instr_remove_src(tex, s); + progress = true; + } + } + + int t = nir_tex_instr_src_index(tex, nir_tex_src_texture_handle); + if (t >= 0) { + int slot = hk_g11_slot(info->g11_tex, &info->g11_nr_tex, + HK_G11_MAX_READ_TEXTURES, + nir_src_as_intrinsic(tex->src[t].src)); + if (slot < 0) { + b->cursor = nir_before_instr(instr); + nir_def_replace(&tex->def, nir_imm_zero(b, tex->def.num_components, + tex->def.bit_size)); + return true; + } + + tex->texture_index = slot; + + /* Arrayed input attachments also carry the array index, which the + * handle already holds + */ + int o = nir_tex_instr_src_index(tex, nir_tex_src_texture_offset); + if (o >= 0) { + nir_tex_instr_remove_src(tex, o); + progress = true; + } + } + + return progress; + } + + if (instr->type != nir_instr_type_intrinsic) + return false; + + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + switch (intr->intrinsic) { + case nir_intrinsic_bindless_image_load: + case nir_intrinsic_bindless_image_sparse_load: + case nir_intrinsic_bindless_image_store: + break; + default: + return false; + } + + nir_intrinsic_instr *h = nir_src_as_intrinsic(intr->src[0]); + if (!h || h->intrinsic != nir_intrinsic_bindless_image_agx) + return false; + + bool store = intr->intrinsic == nir_intrinsic_bindless_image_store; + int slot = hk_g11_slot(info->g11_tex, &info->g11_nr_tex, + store ? HK_G11_MAX_TEXTURES : HK_G11_MAX_READ_TEXTURES, + h); + + b->cursor = nir_before_instr(instr); + if (slot < 0 && store) { + nir_instr_remove(instr); + } else if (slot < 0) { + nir_def_replace(&intr->def, nir_imm_zero(b, intr->def.num_components, + intr->def.bit_size)); + } else { + nir_rewrite_image_intrinsic(intr, nir_imm_intN_t(b, slot, 16), false); + } + + return true; +} +#endif + static bool lower_uniforms(nir_builder *b, nir_intrinsic_instr *intr, void *data) { @@ -1073,6 +1659,7 @@ hk_compile_nir(struct hk_device *dev, const VkAllocationCallbacks *pAllocator, struct hk_shader *shader, mesa_shader_stage sw_stage, bool hw, nir_xfb_info *xfb_info, unsigned set_count) { + rs = hk_robustness(rs); unsigned nr_vbos = 0; bool kill_psiz = false; @@ -1158,6 +1745,18 @@ hk_compile_nir(struct hk_device *dev, const VkAllocationCallbacks *pAllocator, shader->info.set_count = set_count; +#if AGX_G11 + /* Descriptor offsets must be constant to find the slot */ + NIR_PASS(_, nir, nir_opt_copy_prop); + NIR_PASS(_, nir, nir_opt_constant_folding); + NIR_PASS(_, nir, nir_shader_instructions_pass, hk_g11_lower_handles, + nir_metadata_control_flow, &shader->info); + + /* Spilled render targets follow the shader's textures */ + if (sw_stage == MESA_SHADER_FRAGMENT) + shader->info.fs.epilog_key.rt_spill_base = shader->info.g11_nr_tex; +#endif + /* XXX: rename */ NIR_PASS(_, nir, hk_lower_uvs_index, sw_stage, nr_vbos); NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_uniforms, @@ -1178,7 +1777,8 @@ hk_compile_nir(struct hk_device *dev, const VkAllocationCallbacks *pAllocator, .no_stop = nir->info.stage == MESA_SHADER_FRAGMENT, .has_scratch = !nir->info.internal, .promote_constants = true, - .promote_textures = true, + /* G11 has no bindless state for the preamble to load */ + .promote_textures = !AGX_G11, }; /* For now, sample shading is always dynamic. Indicate that. */ @@ -1283,10 +1883,10 @@ hk_api_shader_destroy(struct vk_device *vk_dev, struct vk_shader *vk_shader, hk_shader_destroy(dev, shader); } - vk_shader_free(&dev->vk, pAllocator, &obj->vk); - free(obj->debug_nir_dump); free(obj->debug_agx_dump); + + vk_shader_free(&dev->vk, pAllocator, &obj->vk); } static void dump_shader_ir( @@ -1952,6 +2552,18 @@ hk_fast_link(struct hk_device *dev, bool fragment, struct hk_shader *main, agx_usc_immediates(&b, &main->b.info.rodata, main->bo->va->addr); } +#if AGX_G11 + /* G11 takes one word for shader, registers and threadgroup memory (see + * agx_build_pipeline). Texture and sampler state come per draw, see + * hk_g11_bind_state. + */ + if (!fragment) { + agx_usc_shader_g11(&b, agx_usc_addr(&dev->dev, s->b.bo->va->addr), + s->b.nr_gprs, + main->b.info.spill_stride ? AGX_G11_SPILL_TOP + : main->b.info.local_size); + } +#else if (s->b.uses_txf) agx_usc_push_packed(&b, SAMPLER, dev->dev.txf_sampler); @@ -1961,6 +2573,7 @@ hk_fast_link(struct hk_device *dev, bool fragment, struct hk_shader *main, if (fragment) agx_usc_push_packed(&b, FRAGMENT_PROPERTIES, s->b.fragment_props); +#endif if (main && main->b.info.has_preamble) { agx_usc_pack(&b, PRESHADER, cfg) { diff --git a/src/asahi/vulkan/hk_shader.h b/src/asahi/vulkan/hk_shader.h index 382f7f18a08..c4af6a2bc7f 100644 --- a/src/asahi/vulkan/hk_shader.h +++ b/src/asahi/vulkan/hk_shader.h @@ -62,6 +62,14 @@ hk_tess_info_merge(struct hk_tess_info a, struct hk_tess_info b) return out; } +/* Texture state counts in 8s up to 64; leave room for 8 spilled render + * targets. Samplers 1-14: 0 is the txf sampler, 15 the txf-lod sampler. + */ +#define HK_G11_MAX_TEXTURES 48 +/* Samples and loads only reach states 0-15; past that they read zero */ +#define HK_G11_MAX_READ_TEXTURES 16 +#define HK_G11_MAX_SAMPLERS 14 + struct hk_shader_info { union { struct { @@ -109,6 +117,16 @@ struct hk_shader_info { uint8_t cull_distance_array_size; uint8_t set_count; + /* G11 has no bindless textures. These are the descriptors the shader uses + * (descriptor set, byte offset in it), copied into its texture and sampler + * state at draw time; see hk_g11_bind_state. + */ + struct hk_g11_state { + uint32_t offset_B; + uint8_t set; + } g11_tex[HK_G11_MAX_TEXTURES], g11_samp[HK_G11_MAX_SAMPLERS]; + uint8_t g11_nr_tex, g11_nr_samp; + /* XXX: is there a less goofy way to do this? I really don't want dynamic * allocation here. */ diff --git a/src/compiler/nir/nir_opt_shrink_vectors.c b/src/compiler/nir/nir_opt_shrink_vectors.c index b3b18e0709a..9bbb2a93967 100644 --- a/src/compiler/nir/nir_opt_shrink_vectors.c +++ b/src/compiler/nir/nir_opt_shrink_vectors.c @@ -111,8 +111,19 @@ shrink_dest_to_read_mask(nir_def *def, bool shrink_start) int last_bit = util_last_bit(mask); int first_bit = shrink_start ? (ffs(mask) - 1) : 0; - const unsigned comps = last_bit - first_bit; - const unsigned rounded = round_up_components_no_vec5(comps); + unsigned comps = last_bit - first_bit; + unsigned rounded = round_up_components_no_vec5(comps); + + /* Moving the start and then rounding up can read past the end of the + * original access, e.g. components 1-7 of a vec8 becoming an 8-wide load + * one component later. Without soft faults that can fault, so keep the + * start instead. + */ + if (first_bit + rounded > def->num_components) { + first_bit = 0; + comps = last_bit; + rounded = round_up_components_no_vec5(comps); + } if ((def->num_components > rounded) || first_bit > 0) { def->num_components = rounded; diff --git a/src/drm-shim/device.c b/src/drm-shim/device.c index 7df59002768..86a3c33d37f 100644 --- a/src/drm-shim/device.c +++ b/src/drm-shim/device.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -277,6 +278,16 @@ drm_shim_ioctl_syncobj_create(int fd, unsigned long request, void *arg) return 0; } +/* Every fence is signalled: export a readable eventfd as the sync file. */ +static int +drm_shim_ioctl_syncobj_handle_to_fd(int fd, unsigned long request, void *arg) +{ + struct drm_syncobj_handle *h = arg; + + h->fd = eventfd(1, EFD_CLOEXEC); + return h->fd < 0 ? -1 : 0; +} + static int drm_shim_ioctl_stub(int fd, unsigned long request, void *arg) { @@ -290,7 +301,7 @@ ioctl_fn_t core_ioctls[] = { [_IOC_NR(DRM_IOCTL_GEM_CLOSE)] = drm_shim_ioctl_gem_close, [_IOC_NR(DRM_IOCTL_SYNCOBJ_CREATE)] = drm_shim_ioctl_syncobj_create, [_IOC_NR(DRM_IOCTL_SYNCOBJ_DESTROY)] = drm_shim_ioctl_stub, - [_IOC_NR(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD)] = drm_shim_ioctl_stub, + [_IOC_NR(DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD)] = drm_shim_ioctl_syncobj_handle_to_fd, [_IOC_NR(DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE)] = drm_shim_ioctl_stub, [_IOC_NR(DRM_IOCTL_SYNCOBJ_WAIT)] = drm_shim_ioctl_stub, [_IOC_NR(DRM_IOCTL_SYNCOBJ_TRANSFER)] = drm_shim_ioctl_stub, diff --git a/src/gallium/drivers/asahi/agx_batch.c b/src/gallium/drivers/asahi/agx_batch.c index 75c41cd9e40..51e5ddbfc2c 100644 --- a/src/gallium/drivers/asahi/agx_batch.c +++ b/src/gallium/drivers/asahi/agx_batch.c @@ -126,6 +126,8 @@ agx_batch_init(struct agx_context *ctx, batch->clear = 0; batch->draw = 0; + batch->g11_heap_B = 0; + batch->g11_tib = false; batch->load = 0; batch->resolve = 0; batch->feedback = 0; diff --git a/src/gallium/drivers/asahi/agx_blit.c b/src/gallium/drivers/asahi/agx_blit.c index fbf1ce0b0f1..fa06ca45f75 100644 --- a/src/gallium/drivers/asahi/agx_blit.c +++ b/src/gallium/drivers/asahi/agx_blit.c @@ -229,7 +229,9 @@ asahi_blit_compute_shader(struct pipe_context *ctx, struct asahi_blit_key *key) static bool asahi_compute_blit_supported(const struct pipe_blit_info *info) { - return (info->src.box.depth == info->dst.box.depth) && !info->alpha_blend && + /* G11's block image store is unknown; blit with draws instead */ + return !AGX_G11 && (info->src.box.depth == info->dst.box.depth) && + !info->alpha_blend && !info->num_window_rectangles && !info->sample0_only && !info->scissor_enable && !info->window_rectangle_include && !info->swizzle_enable && info->src.resource->nr_samples <= 1 && diff --git a/src/gallium/drivers/asahi/agx_disk_cache.c b/src/gallium/drivers/asahi/agx_disk_cache.c index 6fe688d894a..79055ad96c3 100644 --- a/src/gallium/drivers/asahi/agx_disk_cache.c +++ b/src/gallium/drivers/asahi/agx_disk_cache.c @@ -64,6 +64,7 @@ write_shader(struct blob *blob, const struct agx_compiled_shader *binary, blob_write_bytes(blob, &binary->attrib_components_read, sizeof(binary->attrib_components_read)); blob_write_bytes(blob, &binary->epilog_key, sizeof(binary->epilog_key)); + blob_write_uint32(blob, binary->global_scratch_B); blob_write_uint32(blob, binary->push_range_count); blob_write_bytes(blob, binary->push, sizeof(binary->push[0]) * binary->push_range_count); @@ -115,6 +116,7 @@ read_shader(struct agx_screen *screen, struct blob_reader *blob, blob_copy_bytes(blob, &binary->attrib_components_read, sizeof(binary->attrib_components_read)); blob_copy_bytes(blob, &binary->epilog_key, sizeof(binary->epilog_key)); + binary->global_scratch_B = blob_read_uint32(blob); binary->push_range_count = blob_read_uint32(blob); blob_copy_bytes(blob, binary->push, sizeof(binary->push[0]) * binary->push_range_count); diff --git a/src/gallium/drivers/asahi/agx_nir_lower_bindings.c b/src/gallium/drivers/asahi/agx_nir_lower_bindings.c index 77e4ca90e0d..e20e6af2ab5 100644 --- a/src/gallium/drivers/asahi/agx_nir_lower_bindings.c +++ b/src/gallium/drivers/asahi/agx_nir_lower_bindings.c @@ -42,6 +42,17 @@ lower(nir_builder *b, nir_instr *instr, void *data) bool *uses_bindless_samplers = data; bool progress = false; bool force_bindless = agx_nir_needs_texture_crawl(instr); + + /* G11 has no bindless texture state, so only queries, which never reach + * the hardware, go bindless. The rest find their descriptor from the + * index, see agx_nir_lower_texture. + */ + if (AGX_G11 && force_bindless) { + force_bindless = + instr->type == nir_instr_type_intrinsic + ? nir_instr_as_intrinsic(instr)->intrinsic != nir_intrinsic_image_load + : nir_tex_instr_is_query(nir_instr_as_tex(instr)); + } b->cursor = nir_before_instr(instr); if (instr->type == nir_instr_type_intrinsic) { diff --git a/src/gallium/drivers/asahi/agx_nir_lower_sysvals.c b/src/gallium/drivers/asahi/agx_nir_lower_sysvals.c index 6a75c87ff79..627ce59560c 100644 --- a/src/gallium/drivers/asahi/agx_nir_lower_sysvals.c +++ b/src/gallium/drivers/asahi/agx_nir_lower_sysvals.c @@ -231,6 +231,8 @@ lower_intrinsic(nir_builder *b, nir_intrinsic_instr *intr, load_sysval(b, 1, 32, AGX_SYSVAL_TABLE_PARAMS, 0), nir_imm_int(b, 0)); case nir_intrinsic_load_draw_id: return load_sysval_root(b, 1, 32, &u->draw_id); + case nir_intrinsic_load_scratch_base_ptr: + return load_sysval_root(b, 1, 64, &u->scratch_base); default: return NULL; } diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index de7e004c54a..fec4387cf4a 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -144,8 +144,10 @@ agx_resource_setup(struct agx_device *dev, struct agx_resource *nresource) .format = templ->format, .width_px = templ->width0, .height_px = templ->height0, - .depth_px = templ->depth0 * templ->array_size, - .sample_count_sa = MAX2(templ->nr_samples, 1), + .depth_px = templ->depth0 * templ->array_size * + (agx_g11_ms_layers(templ) ? templ->nr_samples : 1), + .sample_count_sa = + agx_g11_ms_layers(templ) ? 1 : MAX2(templ->nr_samples, 1), .levels = templ->last_level + 1, .writeable_image = templ->bind & PIPE_BIND_SHADER_IMAGE, @@ -345,7 +347,7 @@ agx_linear_allowed(const struct agx_resource *pres) /* Multisampling not allowed with linear */ if (pres->base.nr_samples > 1) - return false; + return agx_g11_ms_layers(&pres->base); /* Block compression not allowed with linear */ if (util_format_is_compressed(pres->base.format)) @@ -369,7 +371,8 @@ agx_linear_allowed(const struct agx_resource *pres) case PIPE_TEXTURE_2D: case PIPE_TEXTURE_2D_ARRAY: case PIPE_TEXTURE_RECT: - if (pres->base.bind & PIPE_BIND_SHADER_IMAGE) + /* G11 has no image atomics, so its images can be linear */ + if ((pres->base.bind & PIPE_BIND_SHADER_IMAGE) && !AGX_G11) return false; break; @@ -400,6 +403,10 @@ agx_twiddled_allowed(const struct agx_resource *pres) static bool agx_compression_allowed(const struct agx_resource *pres) { + /* G11 compression is not known */ + if (AGX_G11) + return false; + /* Allow disabling compression for debugging */ if (agx_device(pres->base.screen)->debug & AGX_DBG_NOCOMPRESS) { rsrc_debug(pres, "No compression: disabled\n"); @@ -455,7 +462,9 @@ agx_select_best_modifier(const struct agx_resource *pres) /* Prefer linear for staging resources, which should be as fast as possible * to write from the CPU. */ - if (agx_linear_allowed(pres) && pres->base.usage == PIPE_USAGE_STAGING) + /* G11 too, where linear is the best tested */ + if (agx_linear_allowed(pres) && + (pres->base.usage == PIPE_USAGE_STAGING || AGX_G11)) return DRM_FORMAT_MOD_LINEAR; /* For SCANOUT or SHARED resources with no explicit modifier selection, force @@ -518,6 +527,13 @@ agx_resource_create_with_modifiers(struct pipe_screen *screen, nresource->base.bind |= PIPE_BIND_SHADER_IMAGE; } + /* Scanout buffers come from the display (see kmsro), linear */ + bool ro_dumb = dev->ro && (templ->bind & PIPE_BIND_SCANOUT) && + dev->ro->create_for_resource == + renderonly_create_kms_dumb_buffer_for_resource; + if (ro_dumb) + nresource->modifier = DRM_FORMAT_MOD_LINEAR; + nresource->mipmapped = (templ->last_level > 0); assert(templ->format != PIPE_FORMAT_Z24X8_UNORM && @@ -528,8 +544,34 @@ agx_resource_create_with_modifiers(struct pipe_screen *screen, pipe_reference_init(&nresource->base.reference, 1); + struct winsys_handle dumb = {0}; + if (ro_dumb) { + nresource->scanout = + renderonly_scanout_for_resource(&nresource->base, dev->ro, &dumb); + if (!nresource->scanout) { + FREE(nresource); + return NULL; + } + + nresource->layout.linear_stride_B = dumb.stride; + } + ail_make_miptree(&nresource->layout); + if (ro_dumb) { + nresource->bo = agx_bo_import(dev, dumb.handle); + close(dumb.handle); + + if (!nresource->bo) { + renderonly_scanout_destroy(nresource->scanout, dev->ro); + FREE(nresource); + return NULL; + } + + agx_resource_debug(nresource, "New (display): "); + return &nresource->base; + } + /* Fail Piglit's obnoxious allocations */ if (nresource->layout.size_B >= (1ull << 32)) { free(nresource); @@ -1994,7 +2036,8 @@ agx_init_compute_caps(struct pipe_screen *pscreen) caps->subgroup_sizes = 32; caps->max_variable_threads_per_block = 1024; - caps->max_subgroups = caps->max_variable_threads_per_block / 32; + /* G11 has no SIMD-group operations (simd_iadd kills the thread). */ + caps->max_subgroups = AGX_G11 ? 0 : caps->max_variable_threads_per_block / 32; } static void @@ -2054,9 +2097,11 @@ agx_init_screen_caps(struct pipe_screen *pscreen) caps->indep_blend_enable = true; caps->indep_blend_func = true; caps->uma = true; - caps->texture_float_linear = true; + /* G11 does not filter 32-bit float */ + caps->texture_float_linear = !AGX_G11; caps->texture_half_float_linear = true; - caps->texture_mirror_clamp_to_edge = true; + /* G11's wrap modes stop at clamp to border (AGXMetalA12) */ + caps->texture_mirror_clamp_to_edge = !AGX_G11; caps->shader_array_components = true; caps->packed_uniforms = true; caps->quads_follow_provoking_vertex_convention = true; @@ -2184,11 +2229,14 @@ agx_init_screen_caps(struct pipe_screen *pscreen) BITFIELD_BIT(MESA_PRIM_TRIANGLES_ADJACENCY) | BITFIELD_BIT(MESA_PRIM_TRIANGLE_STRIP_ADJACENCY) | BITFIELD_BIT(MESA_PRIM_PATCHES); - caps->map_unsynchronized_thread_safe = true; - caps->vs_layer_viewport = true; - caps->tes_layer_viewport = true; + /* G11 binds render targets at their first layer, see + * agx_upload_spilled_rt_descriptors + */ + caps->vs_layer_viewport = !AGX_G11; + caps->tes_layer_viewport = !AGX_G11; + caps->no_layered_rendering = AGX_G11; caps->context_priority_mask = PIPE_CONTEXT_PRIORITY_LOW | PIPE_CONTEXT_PRIORITY_MEDIUM | @@ -2255,6 +2303,18 @@ agx_is_format_supported(struct pipe_screen *pscreen, enum pipe_format format, if (!ail_is_valid_pixel_format(tex_format)) return false; + /* A12 has no BC decoding (S3TC sampling hangs it) and no ASTC HDR */ + if (AGX_G11) { + const struct util_format_description *d = + util_format_description(tex_format); + + if (d->layout == UTIL_FORMAT_LAYOUT_S3TC || + d->layout == UTIL_FORMAT_LAYOUT_RGTC || + d->layout == UTIL_FORMAT_LAYOUT_BPTC || + util_format_is_astc_hdr(tex_format)) + return false; + } + /* RGB32, luminance/alpha/intensity emulated for texture buffers only */ if ((ent.channels == AGX_CHANNELS_R32G32B32_EMULATED || util_format_is_luminance(tex_format) || diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index 616dd6d1c01..a45af40c2c7 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -29,6 +29,7 @@ #include "gallium/auxiliary/util/u_framebuffer.h" #include "gallium/auxiliary/util/u_helpers.h" #include "gallium/auxiliary/util/u_prim_restart.h" +#include "gallium/auxiliary/util/u_split_draw.h" #include "gallium/auxiliary/util/u_viewport.h" #include "pipe/p_context.h" #include "pipe/p_defines.h" @@ -231,6 +232,10 @@ agx_create_blend_state(struct pipe_context *ctx, if (rt.colormask) so->store |= (PIPE_CLEAR_COLOR0 << i); + + if (rt.colormask && (state->logicop_enable || rt.blend_enable || + rt.colormask != 0xf)) + so->reads |= BITFIELD_BIT(i); } return so; @@ -639,6 +644,42 @@ agx_pack_texture(void *out, struct agx_resource *rsrc, enum pipe_format format /* override */, const struct pipe_sampler_view *state) { + /* A sample per layer or channel, see agx_g11_ms_layers */ + struct pipe_sampler_view layers_state; + bool ms_layers = AGX_G11 && rsrc->base.nr_samples > 1 && + state->target != PIPE_BUFFER; + if (ms_layers) { + bool zs = util_format_is_depth_or_stencil(format); + unsigned n = rsrc->base.nr_samples; + + assert(rsrc->base.array_size == 1 && "no multisampled arrays"); + layers_state = *state; + layers_state.target = PIPE_TEXTURE_2D_ARRAY; + layers_state.u.tex.first_layer = 0; + layers_state.u.tex.last_layer = zs ? 0 : n - 1; + state = &layers_state; + + if (zs) { + static const enum pipe_format wide[][2] = { + {PIPE_FORMAT_R32G32_FLOAT, PIPE_FORMAT_R32G32B32A32_FLOAT}, + {PIPE_FORMAT_R16G16_UNORM, PIPE_FORMAT_R16G16B16A16_UNORM}, + {PIPE_FORMAT_R8G8_UINT, PIPE_FORMAT_R8G8B8A8_UINT}, + }; + /* Z24 is Z32F underneath */ + unsigned i = format == PIPE_FORMAT_Z16_UNORM ? 1 + : format == PIPE_FORMAT_S8_UINT ? 2 + : 0; + assert(i || util_format_get_blocksize(format) == 4); + format = wide[i][n == 4]; + + /* The shader picks the sample's channel */ + layers_state.swizzle_r = PIPE_SWIZZLE_X; + layers_state.swizzle_g = PIPE_SWIZZLE_Y; + layers_state.swizzle_b = PIPE_SWIZZLE_Z; + layers_state.swizzle_a = PIPE_SWIZZLE_W; + } + } + const struct util_format_description *desc = util_format_description(format); assert(ail_is_valid_pixel_format(format)); @@ -674,8 +715,8 @@ agx_pack_texture(void *out, struct agx_resource *rsrc, /* Pack the descriptor into GPU memory */ agx_pack(out, TEXTURE, cfg) { - cfg.dimension = agx_translate_tex_dim(state->target, - util_res_sample_count(&rsrc->base)); + cfg.dimension = agx_translate_tex_dim( + state->target, ms_layers ? 1 : util_res_sample_count(&rsrc->base)); cfg.layout = agx_translate_layout(rsrc->layout.tiling); cfg.channels = ail_pixel_format[format].channels; cfg.type = ail_pixel_format[format].type; @@ -743,7 +784,7 @@ agx_pack_texture(void *out, struct agx_resource *rsrc, } } - if (rsrc->base.nr_samples > 1) + if (rsrc->base.nr_samples > 1 && !ms_layers) cfg.samples = agx_translate_sample_count(rsrc->base.nr_samples); if (state->target == PIPE_BUFFER) { @@ -933,7 +974,17 @@ agx_get_scissor_extents(const struct pipe_viewport_state *vp, } } +/* See agx_g11_ppp_emit */ static void +agx_g11_ppp(struct agx_batch *batch, uint8_t **out, const uint32_t *words, + unsigned count) +{ + agx_g11_ppp_emit( + out, agx_pool_alloc_aligned(&batch->pool, count * 4, 4), words, count); +} + +/* Returns the depth bias/scissor word, which G11 emits on its own */ +static uint32_t agx_upload_viewport_scissor(struct agx_pool *pool, struct agx_batch *batch, uint8_t **out, const struct pipe_viewport_state *vp, const struct pipe_scissor_state *ss, @@ -946,9 +997,11 @@ agx_upload_viewport_scissor(struct agx_pool *pool, struct agx_batch *batch, unsigned count = multi_viewport ? AGX_MAX_VIEWPORTS : 1; /* Allocate scissor descriptors */ - unsigned index = batch->scissor.size / AGX_SCISSOR_LENGTH; - struct agx_scissor_packed *scissors = - util_dynarray_grow_bytes(&batch->scissor, count, AGX_SCISSOR_LENGTH); + /* G11 scissors are G13's without the Z range (AGXMetalA12 ScissorBits) */ + unsigned scissor_size = AGX_G11 ? 8 : AGX_SCISSOR_LENGTH; + unsigned index = batch->scissor.size / scissor_size; + uint8_t *scissors = + util_dynarray_grow_bytes(&batch->scissor, count, scissor_size); unsigned minx[AGX_MAX_VIEWPORTS], miny[AGX_MAX_VIEWPORTS]; unsigned maxx[AGX_MAX_VIEWPORTS], maxy[AGX_MAX_VIEWPORTS]; @@ -961,7 +1014,8 @@ agx_upload_viewport_scissor(struct agx_pool *pool, struct agx_batch *batch, float minz, maxz; util_viewport_zmin_zmax(vp, clip_halfz, &minz, &maxz); - agx_pack(scissors + i, SCISSOR, cfg) { + struct agx_scissor_packed scissor; + agx_pack(&scissor, SCISSOR, cfg) { cfg.min_x = minx[i]; cfg.min_y = miny[i]; cfg.min_z = minz; @@ -969,11 +1023,21 @@ agx_upload_viewport_scissor(struct agx_pool *pool, struct agx_batch *batch, cfg.max_y = maxy[i]; cfg.max_z = maxz; } + + memcpy(scissors + (i * scissor_size), &scissor, scissor_size); + } + + /* Use the current depth bias, we allocate linearly */ + unsigned nr_depth_bias = batch->depth_bias.size / AGX_DEPTH_BIAS_LENGTH; + uint32_t depth_bias_scissor; + agx_pack(&depth_bias_scissor, DEPTH_BIAS_SCISSOR, cfg) { + cfg.scissor = index; + cfg.depth_bias = nr_depth_bias ? nr_depth_bias - 1 : 0; } /* Upload state */ struct AGX_PPP_HEADER present = { - .depth_bias_scissor = true, + .depth_bias_scissor = !AGX_G11, .region_clip = true, .viewport = true, .viewport_count = count, @@ -984,17 +1048,13 @@ agx_upload_viewport_scissor(struct agx_pool *pool, struct agx_batch *batch, agx_pool_alloc_aligned(&batch->pool, size, AGX_PPP_HEADER_ALIGN); struct agx_ppp_update ppp = agx_new_ppp_update(T, size, &present); - agx_ppp_push(&ppp, DEPTH_BIAS_SCISSOR, cfg) { - cfg.scissor = index; - - /* Use the current depth bias, we allocate linearly */ - unsigned count = batch->depth_bias.size / AGX_DEPTH_BIAS_LENGTH; - cfg.depth_bias = count ? count - 1 : 0; - }; + if (!AGX_G11) + agx_ppp_push_packed(&ppp, &depth_bias_scissor, DEPTH_BIAS_SCISSOR); for (unsigned i = 0; i < count; ++i) { agx_ppp_push(&ppp, REGION_CLIP, cfg) { - cfg.enable = true; + /* AGXMetalA12 leaves the region clip disabled */ + cfg.enable = !AGX_G11; cfg.min_x = minx[i] / 32; cfg.min_y = miny[i] / 32; cfg.max_x = DIV_ROUND_UP(MAX2(maxx[i], 1), 32); @@ -1002,8 +1062,10 @@ agx_upload_viewport_scissor(struct agx_pool *pool, struct agx_batch *batch, } } - agx_ppp_push(&ppp, VIEWPORT_CONTROL, cfg) - ; + if (!AGX_G11) { + agx_ppp_push(&ppp, VIEWPORT_CONTROL, cfg) + ; + } /* Upload viewports */ for (unsigned i = 0; i < count; ++i) { @@ -1023,6 +1085,7 @@ agx_upload_viewport_scissor(struct agx_pool *pool, struct agx_batch *batch, } agx_ppp_fini(out, &ppp); + return depth_bias_scissor; } static void @@ -1177,7 +1240,8 @@ agx_batch_upload_pbe(struct agx_batch *batch, struct agx_pbe_packed *out, } cfg.buffer = agx_map_texture_gpu(tex, layer); - cfg.unk_mipmapped = tex->mipmapped; + /* Apple sets it on G11 too, but Mesa's image writes then drop level 0 */ + cfg.unk_mipmapped = tex->mipmapped && !AGX_G11; if (is_buffer) { unsigned size_el = @@ -1190,9 +1254,23 @@ agx_batch_upload_pbe(struct agx_batch *batch, struct agx_pbe_packed *out, cfg.width = AGX_TEXTURE_BUFFER_WIDTH; cfg.height = DIV_ROUND_UP(size_el, cfg.width); cfg.level = 0; - cfg.stride = (cfg.width * util_format_get_blocksize(view->format)) - 4; + cfg.stride = (cfg.width * util_format_get_blocksize(view->format)) - AGX_PBE_STRIDE_BIAS; + cfg.layers = 1; + cfg.levels = 1; + } else if (agx_g11_ms_layers(&tex->base)) { + /* The sample layers as one tall 2D image. + * + * ponytail: height is 14 bits, so 4x tops out at 4096 rows + */ + assert(level == 0 && layer == 0); + cfg.dimension = AGX_TEXTURE_DIMENSION_2D; + cfg.width = view->resource->width0; + cfg.height = view->resource->height0 * tex->base.nr_samples; + cfg.stride = + ail_get_linear_stride_B(&tex->layout, 0) - AGX_PBE_STRIDE_BIAS; cfg.layers = 1; cfg.levels = 1; + cfg.level = 0; } else if (util_res_sample_count(&tex->base) > 1 && !block_access) { /* Multisampled images are bound like buffer textures, with * addressing arithmetic to determine the texel to write. @@ -1209,7 +1287,7 @@ agx_batch_upload_pbe(struct agx_batch *batch, struct agx_pbe_packed *out, cfg.layout = AGX_LAYOUT_LINEAR; cfg.width = AGX_TEXTURE_BUFFER_WIDTH; cfg.height = DIV_ROUND_UP(size_px, cfg.width); - cfg.stride = (cfg.width * blocksize_B) - 4; + cfg.stride = (cfg.width * blocksize_B) - AGX_PBE_STRIDE_BIAS; cfg.layers = 1; cfg.levels = 1; @@ -1235,7 +1313,7 @@ agx_batch_upload_pbe(struct agx_batch *batch, struct agx_pbe_packed *out, } if (tex->layout.tiling == AIL_TILING_LINEAR) { - cfg.stride = ail_get_linear_stride_B(&tex->layout, level) - 4; + cfg.stride = ail_get_linear_stride_B(&tex->layout, level) - AGX_PBE_STRIDE_BIAS; cfg.levels = 1; } else { cfg.page_aligned_layers = tex->layout.page_aligned_layers; @@ -1244,6 +1322,15 @@ agx_batch_upload_pbe(struct agx_batch *batch, struct agx_pbe_packed *out, if (tex->base.nr_samples > 1) cfg.samples = agx_translate_sample_count(tex->base.nr_samples); + + /* G11 image writes take level 0, so point at the level itself */ + if (AGX_G11 && level) { + cfg.buffer += ail_get_level_offset_B(&tex->layout, level); + cfg.width = u_minify(cfg.width, level); + cfg.height = u_minify(cfg.height, level); + cfg.level = 0; + cfg.levels = 1; + } } if (tex->layout.compressed && !emrt) { @@ -1272,7 +1359,8 @@ agx_batch_upload_pbe(struct agx_batch *batch, struct agx_pbe_packed *out, ail_get_level_offset_B(&tex->layout, cfg.level); } - if (tex->layout.tiling != AIL_TILING_LINEAR || emrt) { + /* G11's linear render targets have no tiles */ + if (tex->layout.tiling != AIL_TILING_LINEAR || (emrt && !AGX_G11)) { struct ail_tile tile_size = tex->layout.tilesize_el[level]; cfg.tile_width_sw = tile_size.width_el; cfg.tile_height_sw = tile_size.height_el; @@ -1435,6 +1523,55 @@ agx_nir_lower_point_sprite_zw(nir_builder *b, nir_intrinsic_instr *intr, * shader key should have already been applied. agx_compile_variant may call * this multiple times if there are auxiliary shaders. */ +/* + * G11's stack instructions differ from G13's and are not known yet, so keep + * private memory in global memory instead: each thread gets stride bytes at + * scratch_base + its index in the dispatch * stride. + * + * ponytail: sized for the whole grid. Per-core slots like Apple's spill + * buffer once the G11 stack instructions are known. + */ +static bool +lower_scratch_to_global(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + unsigned stride = *(unsigned *)data; + bool store = intr->intrinsic == nir_intrinsic_store_scratch; + + if (!store && intr->intrinsic != nir_intrinsic_load_scratch) + return false; + + /* The thread's index in the grid, from the hardware grid position, which + * leaves out CL's global offset. + */ + b->cursor = nir_before_instr(&intr->instr); + nir_def *id = nir_load_global_invocation_id(b, 32); + nir_def *size = nir_imul(b, nir_load_num_workgroups(b), nir_load_workgroup_size(b)); + nir_def *index = nir_iadd( + b, nir_channel(b, id, 0), + nir_imul(b, nir_channel(b, size, 0), + nir_iadd(b, nir_channel(b, id, 1), + nir_imul(b, nir_channel(b, size, 1), nir_channel(b, id, 2))))); + nir_def *offset = nir_iadd(b, nir_imul_imm(b, index, stride), + intr->src[store ? 1 : 0].ssa); + nir_def *addr = nir_iadd(b, nir_load_scratch_base_ptr(b, 1, 64, 0), + nir_u2u64(b, offset)); + + if (store) { + nir_store_global(b, intr->src[0].ssa, addr, + .write_mask = nir_intrinsic_write_mask(intr), + .align_mul = nir_intrinsic_align_mul(intr), + .align_offset = nir_intrinsic_align_offset(intr)); + nir_instr_remove(&intr->instr); + } else { + nir_def_replace(&intr->def, + nir_load_global(b, intr->def.num_components, + intr->def.bit_size, addr, + .align_mul = nir_intrinsic_align_mul(intr), + .align_offset = nir_intrinsic_align_offset(intr))); + } + return true; +} + static struct agx_compiled_shader * agx_compile_nir(struct agx_device *dev, nir_shader *nir, struct util_debug_callback *debug, mesa_shader_stage stage, @@ -1463,6 +1600,13 @@ agx_compile_nir(struct agx_device *dev, nir_shader *nir, nir->info.fs.uses_sample_shading) key.fs.inside_sample_loop = true; + if (AGX_G11 && nir->info.stage == MESA_SHADER_COMPUTE && nir->scratch_size) { + compiled->global_scratch_B = align(nir->scratch_size, 16); + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_scratch_to_global, + nir_metadata_control_flow, &compiled->global_scratch_B); + nir->scratch_size = 0; + } + if (internal_kernel) { key.reserved_preamble = 8; } else if (!secondary) { @@ -1497,6 +1641,46 @@ agx_build_meta_shader_internal(struct agx_context *ctx, size_t data_size, bool prolog, bool epilog, unsigned cf_base, bool internal_kernel); +/* + * G11's st_vary only takes an immediate slot, so the slots must be known when + * compiling. They depend on the fragment shader's flat and linear inputs, + * which the GS copy program does not know, so G11 compiles it for each. + */ +static struct agx_compiled_shader * +agx_compile_gs_copy(struct agx_device *dev, struct util_debug_callback *debug, + nir_shader *gs_copy, const struct agx_uncompiled_shader *so, + uint64_t flat, uint64_t linear) +{ + /* Replace the point size write if present, but do not insert a write: + * the GS rast program writes point size iff we have points. + */ + NIR_PASS(_, gs_copy, agx_nir_lower_point_size, false); + + NIR_PASS(_, gs_copy, nir_lower_clip_halfz_dynamic); + + NIR_PASS(_, gs_copy, nir_lower_io_to_scalar, nir_var_shader_out, NULL, + NULL); + NIR_PASS(_, gs_copy, agx_nir_lower_cull_distance_vs); + + if (AGX_G11) { + gs_copy->info.known_interpolation_qualifiers = true; + gs_copy->info.linear_varyings = linear; + gs_copy->info.perspective_varyings = ~(flat | linear); + } + + struct agx_unlinked_uvs_layout uvs = {0}; + NIR_PASS(_, gs_copy, agx_nir_lower_uvs, &uvs); + + struct agx_compiled_shader *cs = agx_compile_nir( + dev, gs_copy, debug, MESA_SHADER_GEOMETRY, false, true, false, 0, NULL); + cs->so = so; + cs->stage = so->type; + cs->uvs = uvs; + cs->flat = flat; + cs->linear = linear; + return cs; +} + /* Does not take ownership of key. Clones if necessary. */ static struct agx_compiled_shader * agx_compile_variant(struct agx_device *dev, struct pipe_context *pctx, @@ -1535,7 +1719,11 @@ agx_compile_variant(struct agx_device *dev, struct pipe_context *pctx, } if (key->hw) { - NIR_PASS(_, nir, agx_nir_lower_point_size, true); + /* ponytail: no point size output on G11 (Metal writes it only + * when the shader does); points need it back + */ + if (!AGX_G11) + NIR_PASS(_, nir, agx_nir_lower_point_size, true); NIR_PASS(_, nir, nir_lower_clip_halfz_dynamic); NIR_PASS(_, nir, nir_lower_io_to_scalar, nir_var_shader_out, NULL, @@ -1575,6 +1763,9 @@ agx_compile_variant(struct agx_device *dev, struct pipe_context *pctx, if (dev->debug & AGX_DBG_SMALLTILE) tib.tile_size = 16 * 16; + if (!key->g11_tib) + agx_tilebuffer_drop_g11_tib(&tib); + /* XXX: don't replicate this all over the driver */ unsigned rt_spill_base = BITSET_LAST_BIT(nir->info.textures_used) + (2 * BITSET_LAST_BIT(nir->info.images_used)); @@ -1631,26 +1822,16 @@ agx_compile_variant(struct agx_device *dev, struct pipe_context *pctx, } if (gs_copy) { - /* Replace the point size write if present, but do not insert a write: - * the GS rast program writes point size iff we have points. - */ - NIR_PASS(_, gs_copy, agx_nir_lower_point_size, false); - - NIR_PASS(_, gs_copy, nir_lower_clip_halfz_dynamic); - - NIR_PASS(_, gs_copy, nir_lower_io_to_scalar, nir_var_shader_out, NULL, - NULL); - NIR_PASS(_, gs_copy, agx_nir_lower_cull_distance_vs); - - struct agx_unlinked_uvs_layout uvs = {0}; - NIR_PASS(_, gs_copy, agx_nir_lower_uvs, &uvs); + if (AGX_G11) { + compiled->gs_copy_nir = nir_shader_clone(NULL, gs_copy); + util_dynarray_init(&compiled->gs_copies, NULL); + } compiled->gs_copy = - agx_compile_nir(dev, gs_copy, &pctx->debug, MESA_SHADER_GEOMETRY, - false, true, false, 0, NULL); - compiled->gs_copy->so = so; - compiled->gs_copy->stage = so->type; - compiled->gs_copy->uvs = uvs; + agx_compile_gs_copy(dev, &pctx->debug, gs_copy, so, 0, 0); + + if (AGX_G11) + util_dynarray_append(&compiled->gs_copies, compiled->gs_copy); } compiled->gs = gs_info; @@ -1667,12 +1848,15 @@ agx_get_shader_variant(struct agx_screen *screen, struct pipe_context *pctx, struct agx_uncompiled_shader *so, union asahi_shader_key *key) { + /* The cache does not keep G11's GS copy NIR */ + bool cache = !(AGX_G11 && so->type == MESA_SHADER_GEOMETRY); struct agx_compiled_shader *compiled = - agx_disk_cache_retrieve(screen, so, key); + cache ? agx_disk_cache_retrieve(screen, so, key) : NULL; if (!compiled) { compiled = agx_compile_variant(&screen->dev, pctx, so, key); - agx_disk_cache_store(screen->disk_cache, so, key, compiled); + if (cache) + agx_disk_cache_store(screen->disk_cache, so, key, compiled); } /* key may be destroyed after we return, so clone it before using it as a @@ -1998,6 +2182,13 @@ agx_get_compute_state_info(struct pipe_context *pctx, void *cso, info->max_threads = agx_occupancy_for_register_count(so->b.info.nr_gprs).max_threads; + + /* G11 spills share the threadgroup memory, see agx_lower_spill.c */ + if (so->b.info.spill_stride) { + unsigned fit = (AGX_G11_SPILL_TOP - so->b.info.local_size) / + so->b.info.spill_stride; + info->max_threads = MIN2(info->max_threads, fit); + } info->private_memory = 0; info->preferred_simd_size = 32; info->simd_sizes = 32; @@ -2221,6 +2412,37 @@ agx_update_gs(struct agx_context *ctx, const struct pipe_draw_info *info, ctx->gs = _mesa_hash_table_next_entry( ctx->stage[MESA_SHADER_GEOMETRY].shader->variants, NULL) ->data; + + /* G11: pick or compile the copy program for this fragment shader. + * + * ponytail: switches the shared GS's current copy program without a lock + */ + if (ctx->gs->gs_copy_nir) { + struct agx_uncompiled_shader *fs = ctx->stage[MESA_SHADER_FRAGMENT].shader; + uint64_t flat = fs->info.inputs_flat_shaded; + uint64_t linear = fs->info.inputs_linear_shaded; + struct agx_compiled_shader *copy = NULL; + + util_dynarray_foreach(&ctx->gs->gs_copies, struct agx_compiled_shader *, + cs) { + if ((*cs)->flat == flat && (*cs)->linear == linear) + copy = *cs; + } + + if (!copy) { + nir_shader *nir = nir_shader_clone(NULL, ctx->gs->gs_copy_nir); + copy = agx_compile_gs_copy(agx_device(ctx->base.screen), + &ctx->base.debug, nir, gs, flat, linear); + util_dynarray_append(&ctx->gs->gs_copies, copy); + ralloc_free(nir); + } + + if (copy != ctx->gs->gs_copy) { + ctx->gs->gs_copy = copy; + ctx->dirty |= AGX_DIRTY_VS | AGX_DIRTY_VS_PROG; + } + } + return true; } @@ -2272,6 +2494,7 @@ agx_update_fs(struct agx_batch *batch) if (ctx->stage[MESA_SHADER_FRAGMENT].shader->info.uses_fbfetch) { key.nr_samples = nr_samples; + key.g11_tib = batch->g11_tib; for (unsigned i = 0; i < batch->key.nr_cbufs; ++i) { key.rt_formats[i] = batch->key.cbufs[i].format; @@ -2305,6 +2528,7 @@ agx_update_fs(struct agx_batch *batch) .epilog.fs.nr_samples = nr_samples, .epilog.fs.link = ctx->fs->epilog_key, .epilog.fs.force_small_tile = dev->debug & AGX_DBG_SMALLTILE, + .epilog.fs.g11_tib = batch->g11_tib, .main = ctx->fs, .nr_samples_shaded = ctx->fs->epilog_key.sample_shading ? nr_samples : 0, @@ -2435,8 +2659,15 @@ agx_delete_compiled_shader(struct agx_device *dev, if (so->pre_gs) agx_delete_compiled_shader(dev, so->pre_gs); - if (so->gs_copy) + if (so->gs_copy_nir) { + util_dynarray_foreach(&so->gs_copies, struct agx_compiled_shader *, cs) + agx_delete_compiled_shader(dev, *cs); + + util_dynarray_fini(&so->gs_copies); + ralloc_free(so->gs_copy_nir); + } else if (so->gs_copy) { agx_delete_compiled_shader(dev, so->gs_copy); + } free(so->b.binary); agx_bo_unreference(dev, so->bo); @@ -2613,8 +2844,10 @@ agx_build_meta_shader(struct agx_context *ctx, meta_shader_builder_t builder, static unsigned sampler_count(struct agx_context *ctx, mesa_shader_stage stage) { - /* We reserve sampler #0 for txf so add 1 to the API count */ - return ctx->stage[stage].sampler_count + 1; + /* We reserve sampler #0 for txf so add 1 to the API count. G11 also keeps + * AGX_G11_TXF_LOD_SAMPLER, the last of its 16. + */ + return AGX_G11 ? 16 : ctx->stage[stage].sampler_count + 1; } static inline enum agx_sampler_states @@ -2671,8 +2904,18 @@ agx_upload_spilled_rt_descriptors(struct agx_texture_packed *out, struct pipe_sampler_view sampler_view = sampler_view_for_surface(surf); sampler_view.target = PIPE_TEXTURE_2D_ARRAY; + /* G11 writes arrays with a longer form that ends the thread, so the + * next render target is never written. Bind the first layer as 2D. + * + * ponytail: one layer, as in agx_nir_lower_tilebuffer.c + */ + if (AGX_G11) { + sampler_view.target = PIPE_TEXTURE_2D; + view.u.tex.single_layer_view = true; + } + agx_pack_texture(texture, rsrc, surf->format, &sampler_view); - agx_batch_upload_pbe(batch, pbe, &view, false, false, true, true); + agx_batch_upload_pbe(batch, pbe, &view, false, false, !AGX_G11, true); } } @@ -2807,8 +3050,15 @@ agx_upload_samplers(struct agx_batch *batch, struct agx_compiled_shader *cs, agx_pool_alloc_aligned(&batch->pool, sampler_length * nr_samplers, 64); /* Sampler #0 is reserved for txf */ + memset(T.cpu, 0, sampler_length * nr_samplers); agx_pack_txf_sampler(T.cpu); + /* ponytail: a shader with 15 API samplers gives its last one this slot */ + if (AGX_G11 && ctx->stage[stage].sampler_count < AGX_G11_TXF_LOD_SAMPLER) { + agx_pack_txf_lod_sampler( + (void *)((uint8_t *)T.cpu + sampler_length * AGX_G11_TXF_LOD_SAMPLER)); + } + /* Remaining samplers are API samplers */ uint8_t *out_sampler = (uint8_t *)T.cpu + sampler_length; for (unsigned i = 0; i < ctx->stage[stage].sampler_count; ++i) { @@ -2889,7 +3139,11 @@ agx_build_pipeline(struct agx_batch *batch, struct agx_compiled_shader *cs, mesa_shader_stage stage = cs->stage; - if (batch->texture_count[stage]) { + /* G11 reads the words even for kernels without textures, so leave them out */ + bool textures = batch->texture_count[stage] && + (!AGX_G11 || agx_nr_tex_descriptors(batch, cs)); + + if (textures) { agx_usc_pack(&b, TEXTURE, cfg) { cfg.start = 0; cfg.count = @@ -2898,7 +3152,17 @@ agx_build_pipeline(struct agx_batch *batch, struct agx_compiled_shader *cs, } } - if (batch->sampler_count[stage]) { + /* + * On G11 the epilog reads spilled render targets through the txf sampler, + * so a blend with no API sampler still needs the samplers loaded. + */ + bool g11_samplers = + cs->b.info.sampler_state_count || + (stage == MESA_SHADER_FRAGMENT && + agx_tilebuffer_spills(&batch->tilebuffer_layout)); + + if (batch->sampler_count[stage] && + (!AGX_G11 || (textures && g11_samplers))) { agx_usc_pack(&b, SAMPLER, cfg) { cfg.start = 0; cfg.count = batch->sampler_count[stage]; @@ -2954,30 +3218,52 @@ agx_build_pipeline(struct agx_batch *batch, struct agx_compiled_shader *cs, } } - if (stage == MESA_SHADER_FRAGMENT) { - agx_usc_push_packed(&b, SHARED, &batch->tilebuffer_layout.usc); - } else { - agx_usc_shared_non_fragment(&b, &cs->b.info, variable_shared_mem); - } - - if (linked) { - agx_usc_push_packed(&b, SHADER, linked->shader); - agx_usc_push_packed(&b, REGISTERS, linked->regs); + /* G11 takes one combined shader word for every stage (Metal's + * emitUscStateLoad). The linked program has the prolog (vertex attributes) + * and epilog (colour output). + */ + if (AGX_G11) { + uint32_t code = + linked ? agx_usc_addr(dev, linked->bo->va->addr) + : agx_usc_addr(dev, cs->bo->va->addr + cs->b.info.main_offset); + unsigned gprs = linked ? linked->nr_gprs : cs->b.info.nr_gprs; + /* Spills take the whole window, see agx_lower_spill.c */ if (stage == MESA_SHADER_FRAGMENT) - agx_usc_push_packed(&b, FRAGMENT_PROPERTIES, linked->fragment_props); + agx_usc_shader_g11_frag( + &b, code, gprs, agx_g11_tib_size_B(&batch->tilebuffer_layout), + batch->tilebuffer_layout.tile_size); + else + agx_usc_shader_g11(&b, code, gprs, + cs->b.info.spill_stride + ? AGX_G11_SPILL_TOP + : cs->b.info.local_size + variable_shared_mem); } else { - agx_usc_pack(&b, SHADER, cfg) { - cfg.code = - agx_usc_addr(dev, cs->bo->va->addr + cs->b.info.main_offset); - cfg.unk_2 = 3; + if (stage == MESA_SHADER_FRAGMENT) { + agx_usc_push_packed(&b, SHARED, &batch->tilebuffer_layout.usc); + } else { + agx_usc_shared_non_fragment(&b, &cs->b.info, variable_shared_mem); } - agx_usc_pack(&b, REGISTERS, cfg) { - cfg.register_count = cs->b.info.nr_gprs; - cfg.spill_size = cs->b.info.scratch_size - ? agx_scratch_get_bucket(cs->b.info.scratch_size) - : 0; + if (linked) { + agx_usc_push_packed(&b, SHADER, linked->shader); + agx_usc_push_packed(&b, REGISTERS, linked->regs); + + if (stage == MESA_SHADER_FRAGMENT) + agx_usc_push_packed(&b, FRAGMENT_PROPERTIES, linked->fragment_props); + } else { + agx_usc_pack(&b, SHADER, cfg) { + cfg.code = + agx_usc_addr(dev, cs->bo->va->addr + cs->b.info.main_offset); + cfg.unk_2 = 3; + } + + agx_usc_pack(&b, REGISTERS, cfg) { + cfg.register_count = cs->b.info.nr_gprs; + cfg.spill_size = cs->b.info.scratch_size + ? agx_scratch_get_bucket(cs->b.info.scratch_size) + : 0; + } } } @@ -3070,7 +3356,8 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) if (store) { /* TODO: Suppress stores to discarded render targets */ key.op[rt] = AGX_EOT_STORE; - } else if (batch->tilebuffer_layout.spilled[rt] && partial_render) { + } else if (batch->tilebuffer_layout.spilled[rt] && + !batch->tilebuffer_layout.g11_tib[rt] && partial_render) { /* Partial render programs exist only to store/load the tilebuffer to * main memory. When render targets are already spilled to main memory, * there's nothing to do. @@ -3081,8 +3368,11 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) bool clear = (batch->clear & (PIPE_CLEAR_COLOR0 << rt)); bool load = valid && !clear; - /* Don't read back spilled render targets, they're already in memory */ - load &= !batch->tilebuffer_layout.spilled[rt]; + /* Don't read back spilled render targets, they're already in memory. + * G11 copies them into the tilebuffer for blending. + */ + load &= !batch->tilebuffer_layout.spilled[rt] || + batch->tilebuffer_layout.g11_tib[rt]; /* The background program used for partial renders must always load * whatever was stored in the mid-frame end-of-tile program. @@ -3206,8 +3496,6 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) } } - agx_usc_push_packed(&b, SHARED, &batch->tilebuffer_layout.usc); - /* Get the shader */ key.reserved_preamble = uniforms; struct agx_device *dev = agx_device(ctx->base.screen); @@ -3215,13 +3503,22 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) agx_batch_add_bo(batch, shader->bo); assert(shader->info.rodata.size_16 == 0); - agx_usc_pack(&b, SHADER, cfg) { - cfg.code = agx_usc_addr(dev, shader->ptr + shader->info.main_offset); - cfg.unk_2 = 0; - } + if (AGX_G11) { + agx_usc_shader_g11_frag( + &b, agx_usc_addr(dev, shader->ptr + shader->info.main_offset), + shader->info.nr_gprs, agx_g11_tib_size_B(&batch->tilebuffer_layout), + batch->tilebuffer_layout.tile_size); + } else { + agx_usc_push_packed(&b, SHARED, &batch->tilebuffer_layout.usc); - agx_usc_pack(&b, REGISTERS, cfg) - cfg.register_count = shader->info.nr_gprs; + agx_usc_pack(&b, SHADER, cfg) { + cfg.code = agx_usc_addr(dev, shader->ptr + shader->info.main_offset); + cfg.unk_2 = 0; + } + + agx_usc_pack(&b, REGISTERS, cfg) + cfg.register_count = shader->info.nr_gprs; + } if (shader->info.has_preamble) { agx_usc_pack(&b, PRESHADER, cfg) { @@ -3242,10 +3539,19 @@ agx_build_bg_eot(struct agx_batch *batch, bool store, bool partial_render) cfg.sampler_state_register_count = agx_translate_sampler_state_count(needs_sampler ? 1 : 0, false); - if (!store) + /* G11 (Apple's background programs use 2) has no such field. */ + if (!store && !AGX_G11) cfg.unknown = 0xFFFF; } + if (AGX_G11) { + agx_g11_clear_counts((uint32_t *)&ret.counts, nr_tex, needs_sampler); + + /* The tilebuffer words written, see agx_g11_encode_ppp */ + if (!store && agx_g11_tib_size_B(&batch->tilebuffer_layout)) + *(uint32_t *)&ret.counts |= BITFIELD_MASK(17) << 15; + } + return ret; } @@ -3313,27 +3619,30 @@ agx_batch_init_state(struct agx_batch *batch) cfg.usc_cache_inval = true; } - struct AGX_PPP_HEADER present = { - .w_clamp = true, - .occlusion_query_2 = true, - .output_unknown = true, - .varying_word_2 = true, - .viewport_count = 1, /* irrelevant */ - }; + /* G11 sends the W clamp with every draw, see agx_g11_encode_ppp */ + if (!AGX_G11) { + struct AGX_PPP_HEADER present = { + .w_clamp = true, + .occlusion_query_2 = true, + .output_unknown = true, + .varying_word_2 = true, + .viewport_count = 1, /* irrelevant */ + }; - size_t size = agx_ppp_update_size(&present); - struct agx_ptr T = - agx_pool_alloc_aligned(&batch->pool, size, AGX_PPP_HEADER_ALIGN); - struct agx_ppp_update ppp = agx_new_ppp_update(T, size, &present); + size_t size = agx_ppp_update_size(&present); + struct agx_ptr T = + agx_pool_alloc_aligned(&batch->pool, size, AGX_PPP_HEADER_ALIGN); + struct agx_ppp_update ppp = agx_new_ppp_update(T, size, &present); - /* clang-format off */ - agx_ppp_push(&ppp, W_CLAMP, cfg) cfg.w_clamp = 1e-10; - agx_ppp_push(&ppp, FRAGMENT_OCCLUSION_QUERY_2, cfg); - agx_ppp_push(&ppp, OUTPUT_UNKNOWN, cfg); - agx_ppp_push(&ppp, VARYING_2, cfg); - /* clang-format on */ + /* clang-format off */ + agx_ppp_push(&ppp, W_CLAMP, cfg) cfg.w_clamp = 1e-10; + agx_ppp_push(&ppp, FRAGMENT_OCCLUSION_QUERY_2, cfg); + agx_ppp_push(&ppp, OUTPUT_UNKNOWN, cfg); + agx_ppp_push(&ppp, VARYING_2, cfg); + /* clang-format on */ - agx_ppp_fini(&out, &ppp); + agx_ppp_fini(&out, &ppp); + } batch->vdm.current = out; /* Mark it as initialized now, since agx_batch_writes() will check this. */ @@ -3353,6 +3662,9 @@ agx_batch_init_state(struct agx_batch *batch) if (agx_device(batch->ctx->base.screen)->debug & AGX_DBG_SMALLTILE) batch->tilebuffer_layout.tile_size = 16 * 16; + if (!batch->g11_tib) + agx_tilebuffer_drop_g11_tib(&batch->tilebuffer_layout); + /* If the layout spilled render targets, we need to decompress those render * targets to ensure we can write to them. */ @@ -3416,9 +3728,171 @@ agx_point_object_type(struct agx_rasterizer *rast) : AGX_OBJECT_TYPE_POINT_SPRITE_UV10; } -#define MAX_PPP_UPDATES 2 +#define MAX_PPP_UPDATES (AGX_G11 ? 7 : 2) #define IS_DIRTY(ST) !!(ctx->dirty & AGX_DIRTY_##ST) +/* + * G11 draw state in AGXMetalA12's blocks and order (see agx_g11_ppp): the + * fragment shader, output select, fragment state, viewport, W clamp with + * cull, and depth bias/scissor. + */ +static void +agx_g11_encode_ppp(struct agx_batch *batch, uint8_t **out, + enum agx_object_type object_type, bool viewport_dirty, + bool fragment_dirty, + bool varyings_dirty, bool fs_dirty) +{ + struct agx_context *ctx = batch->ctx; + struct agx_rasterizer *rast = ctx->rast; + struct agx_compiled_shader *vs = ctx->gs ? ctx->gs->gs_copy : ctx->vs; + uint32_t fs_control = ctx->linked.fs->fragment_control.opaque[0]; + + if (fs_dirty) { + /* + * Resource counts as G13 without the preshader, and from bit 15 the + * tilebuffer words the shader writes, padded with ones up to 17 words + * when it writes the last one (agxsGetShaderPassInfo). + * + * ponytail: assumes the shader writes every render target in the + * tilebuffer; compute the mask from the outputs when partial writes + * matter + */ + bool tib_writes = batch->tilebuffer_layout.sample_size_B != 0; + uint32_t counts; + agx_pack(&counts, FRAGMENT_SHADER_WORD_0, cfg) { + cfg.uniform_register_count = ctx->fs->b.info.push_count; + cfg.texture_state_register_count = + agx_nr_tex_descriptors(batch, ctx->fs); + cfg.sampler_state_register_count = + translate_sampler_state_count(ctx, MESA_SHADER_FRAGMENT); + } + agx_g11_clear_counts(&counts, agx_nr_tex_descriptors(batch, ctx->fs), + ctx->stage[MESA_SHADER_FRAGMENT].sampler_count || + ctx->fs->b.info.uses_txf || + agx_tilebuffer_spills(&batch->tilebuffer_layout)); + + /* ponytail: varying count as the binding count, unverified with + * varyings (ProgramVaryingTable iteration counts) + */ + uint32_t words[] = { + BITFIELD_BIT(7) | BITFIELD_BIT(8), + counts | (tib_writes ? BITFIELD_MASK(17) << 15 : 0), + agx_build_pipeline(batch, ctx->fs, ctx->linked.fs, + MESA_SHADER_FRAGMENT, 0, 0) | + agx_g11_regs(ctx->linked.fs->nr_gprs), + batch->varyings, + ctx->linked.fs->cf.nr_bindings & 0x7f, + }; + + agx_g11_ppp(batch, out, words, ARRAY_SIZE(words)); + } + + /* Fragments always take varyings (RenderPipeline +0x7f0) */ + if (varyings_dirty) { + uint32_t osel = agx_g11_osel(&vs->uvs, ctx->linked.fs->osel.opaque[0]); + agx_g11_ppp(batch, out, (uint32_t[]){BITFIELD_BIT(17), osel}, 2); + } + + if (fragment_dirty) { + /* + * Control as G13 without bit 9, the visibility index in 13:0 + * (setVisibilityResultMode), the shader's tag write disable 21 and + * tri merging 26 (RenderPipeline::setFragmentVariant). + */ + uint32_t control; + agx_pack(&control, FRAGMENT_CONTROL, cfg) { + if (ctx->active_queries && ctx->occlusion_query) { + if (ctx->occlusion_query->type == PIPE_QUERY_OCCLUSION_COUNTER) + cfg.visibility_mode = AGX_VISIBILITY_MODE_COUNTING; + else + cfg.visibility_mode = AGX_VISIBILITY_MODE_BOOLEAN; + } + + /* Both stencil bits for any stencil (newDepthStencilState) */ + cfg.stencil_test_enable = ctx->zs->base.stencil[0].enabled; + cfg.two_sided_stencil = ctx->zs->base.stencil[0].enabled; + cfg.depth_bias_enable = + rast->depth_bias && object_type == AGX_OBJECT_TYPE_TRIANGLE; + cfg.scissor_enable = true; + cfg.disable_tri_merging = object_type != AGX_OBJECT_TYPE_TRIANGLE; + } + + control &= ~BITFIELD_BIT(9); + control |= fs_control & (BITFIELD_BIT(21) | BITFIELD_BIT(26)); + + if (ctx->active_queries && ctx->occlusion_query) + control |= (agx_get_oq_index(batch, ctx->occlusion_query) * 2) & + BITFIELD_MASK(14); + + /* + * Face: stencil reference 7:0, line width 15:8, depth compare 19:17, + * shader writes depth 20 (else the test uses the interpolated depth), + * depth write disable 21, pass type 26:24, object type 31:28 (5 line + * and 6 point filled triangles; encodeISPPrimitiveType). + */ + uint32_t depth = ctx->zs->depth.opaque[0]; + unsigned type = object_type; + if (object_type == AGX_OBJECT_TYPE_TRIANGLE && + rast->polygon_mode != AGX_POLYGON_MODE_FILL) + type = rast->polygon_mode == AGX_POLYGON_MODE_LINE ? 5 : 6; + + uint32_t face = (rast->line_width << 8) | (((depth >> 24) & 7) << 17) | + (depth & BITFIELD_BIT(21)) | + (((fs_control >> 29) & 7) << 24) | (type << 28); + + if (ctx->fs->b.info.depth_layout != FRAG_DEPTH_LAYOUT_UNCHANGED) + face |= BITFIELD_BIT(20); + + bool twosided = ctx->zs->base.stencil[1].enabled; + + agx_g11_ppp(batch, out, + (uint32_t[]){ + BITFIELD_MASK(5), + control, + face | ctx->stencil_ref.ref_value[0], + ctx->zs->front_stencil.opaque[0], + face | ctx->stencil_ref.ref_value[twosided ? 1 : 0], + ctx->zs->back_stencil.opaque[0], + }, + 6); + } + + uint32_t depth_bias_scissor = 0; + if (viewport_dirty) { + depth_bias_scissor = agx_upload_viewport_scissor( + &batch->pool, batch, out, ctx->viewport, + rast->base.scissor ? ctx->scissor : NULL, rast->base.clip_halfz, + vs->b.info.nonzero_viewport); + } + + /* G11 has no cull 2: clamp W is cull bit 5 */ + if (IS_DIRTY(RS)) { + uint32_t cull; + agx_pack(&cull, CULL, cfg) { + cfg.front_face_ccw = rast->base.front_ccw; + + if (ctx->in_tess && !ctx->gs) { + /* Yes, OpenGL is backwards. Deal with it. */ + cfg.front_face_ccw ^= + !ctx->stage[MESA_SHADER_TESS_EVAL].shader->tess.ccw; + } + } + + uint32_t rast_cull; + memcpy(&rast_cull, rast->cull, sizeof(rast_cull)); + + agx_g11_ppp(batch, out, + (uint32_t[]){BITFIELD_BIT(16) | BITFIELD_BIT(21), fui(1e-20), + cull | rast_cull | BITFIELD_BIT(5)}, + 3); + } + + if (viewport_dirty) { + agx_g11_ppp(batch, out, + (uint32_t[]){BITFIELD_BIT(5), depth_bias_scissor}, 2); + } +} + static uint8_t * agx_encode_state(struct agx_batch *batch, uint8_t *out) { @@ -3442,7 +3916,8 @@ agx_encode_state(struct agx_batch *batch, uint8_t *out) IS_DIRTY(PRIM)) { unsigned bindings = ctx->linked.fs->cf.nr_bindings; - if (bindings) { + /* G11 reads the binding header even without bindings (USC 0 faults) */ + if (bindings || AGX_G11) { size_t linkage_size = AGX_CF_BINDING_HEADER_LENGTH + (bindings * AGX_CF_BINDING_LENGTH); @@ -3457,6 +3932,10 @@ agx_encode_state(struct agx_batch *batch, uint8_t *out) : 0, &batch->generate_primitive_id); + /* AGXMetalA12 leaves the varying table empty without inputs */ + if (AGX_G11 && !bindings) + memset(t.cpu, 0, linkage_size); + batch->varyings = agx_usc_addr(dev, t.gpu); } else { batch->varyings = 0; @@ -3466,7 +3945,48 @@ agx_encode_state(struct agx_batch *batch, uint8_t *out) ppp_updates++; } - if (IS_DIRTY(VS) || varyings_dirty) { + if (AGX_G11 && (IS_DIRTY(VS) || varyings_dirty)) { + /* + * G11's vertex state is a fixed four word token (Metal's + * VDMEncoderGen2_1::VsStateToken): a header with a class for the + * vertex output count, the resource counts (G13's word 0 without the + * preshader, 0 for none), the pipeline with the register count in + * bits 5:2 and its length in 64 byte units in 1:0 (0: 256 bytes), and + * the output count. + */ + unsigned outputs = vs->uvs.size; + struct agx_linked_shader *linked = ctx->gs ? NULL : ctx->linked.vs; + uint32_t *w; + + /* Varying counts go with the vertex shader (VS variant PPP block) */ + if (varyings_dirty) { + uint32_t counts[3] = {BITFIELD_RANGE(18, 2)}; + + memcpy(&counts[1], &batch->linked_varyings.counts_32, 4); + memcpy(&counts[2], &batch->linked_varyings.counts_16, 4); + agx_g11_ppp(batch, &out, counts, 3); + } + + w = (uint32_t *)out; + + w[0] = agx_g11_vs_header(ctx->rast->base.rasterizer_discard, + ctx->rast->base.flatshade_first ? 0 : 2, + outputs); + batch->g11_vs_header = w[0]; + agx_pack(&w[1], VDM_STATE_VERTEX_SHADER_WORD_0, cfg) { + cfg.uniform_register_count = vs->b.info.push_count; + cfg.texture_state_register_count = agx_nr_tex_descriptors(batch, vs); + cfg.sampler_state_register_count = + translate_sampler_state_count(ctx, vs->stage); + } + agx_g11_clear_counts(&w[1], agx_nr_tex_descriptors(batch, vs), + ctx->stage[vs->stage].sampler_count || + vs->b.info.uses_txf); + w[2] = agx_build_pipeline(batch, vs, linked, MESA_SHADER_VERTEX, 0, 0) | + agx_g11_regs(linked ? linked->nr_gprs : vs->b.info.nr_gprs); + w[3] = outputs & 0xff; + out += 16; + } else if (IS_DIRTY(VS) || varyings_dirty) { agx_push(out, VDM_STATE, cfg) { cfg.vertex_shader_word_0_present = true; cfg.vertex_shader_word_1_present = true; @@ -3511,8 +4031,8 @@ agx_encode_state(struct agx_batch *batch, uint8_t *out) ctx->dirty |= AGX_DIRTY_SCISSOR_ZBIAS; } - if (ctx->dirty & (AGX_DIRTY_VIEWPORT | AGX_DIRTY_SCISSOR_ZBIAS | - AGX_DIRTY_RS | AGX_DIRTY_VS)) { + if (!AGX_G11 && (ctx->dirty & (AGX_DIRTY_VIEWPORT | AGX_DIRTY_SCISSOR_ZBIAS | + AGX_DIRTY_RS | AGX_DIRTY_VS))) { agx_upload_viewport_scissor(pool, batch, &out, ctx->viewport, ctx->rast->base.scissor ? ctx->scissor : NULL, @@ -3533,6 +4053,21 @@ agx_encode_state(struct agx_batch *batch, uint8_t *out) : is_lines ? AGX_OBJECT_TYPE_LINE : AGX_OBJECT_TYPE_TRIANGLE; + if (AGX_G11) { + agx_g11_encode_ppp(batch, &out, object_type, + ctx->dirty & (AGX_DIRTY_VIEWPORT | + AGX_DIRTY_SCISSOR_ZBIAS | AGX_DIRTY_RS | + AGX_DIRTY_VS), + IS_DIRTY(ZS) || IS_DIRTY(RS) || IS_DIRTY(PRIM) || + IS_DIRTY(QUERY) || IS_DIRTY(FS_PROG) || + fragment_face_dirty || object_type_dirty, + varyings_dirty, + (IS_DIRTY(FS) || varyings_dirty || + IS_DIRTY(SAMPLE_MASK)) && + !ctx->linked.fs->no_op); + return out; + } + struct AGX_PPP_HEADER dirty = { .fragment_control = IS_DIRTY(ZS) || IS_DIRTY(RS) || IS_DIRTY(PRIM) || IS_DIRTY(QUERY), @@ -4911,6 +5446,32 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, return; } + /* A draw too big for G11's tiler heap is split, see agx_g11_heap_cost */ + /* ctx->vs is only current once the VS is clean (it may be freed) */ + unsigned g11_outputs = + (ctx->vs && !(ctx->dirty & AGX_DIRTY_VS)) ? ctx->vs->uvs.size : 64; + if (AGX_G11 && !indirect && + (info->mode == MESA_PRIM_POINTS || info->mode == MESA_PRIM_LINES || + info->mode == MESA_PRIM_LINE_STRIP || + info->mode == MESA_PRIM_TRIANGLES || + info->mode == MESA_PRIM_TRIANGLE_STRIP)) { + uint32_t max = agx_g11_split_max(info->instance_count, g11_outputs); + + if (draws[0].count > max) { + struct pipe_draw_start_count_bias d = draws[0]; + + for (uint32_t left = draws[0].count; left > 2;) { + uint32_t step; + d.count = left; + u_split_draw(info, max, &d.count, &step); + agx_draw_vbo(pctx, info, drawid_offset, NULL, &d, 1); + d.start += step; + left -= step; + } + return; + } + } + /* We must legalize feedback loops and transform feedback writes before * getting the batch, since once we have the batch we're not allowed to flush * the bound render targets. @@ -4919,6 +5480,75 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, agx_legalize_xfb(ctx); struct agx_batch *batch = agx_get_batch(ctx); + + /* + * G11's blend epilog reads render targets from their tilebuffer copy. The + * copy costs every fragment a tile store, so a batch starts without it and + * the first draw that reads turns it on, in a new batch whose background + * program reloads the copy from memory. + * + * Targets without a copy are read through the texture unit, and nothing + * makes that see what the PBE or the clear wrote earlier in the pass + * (Apple's G11 shaders blend in the tilebuffer). A new pass starts with a + * clean texture cache. + * + * ponytail: one pass per such draw, slow, and overlap within one draw is + * still wrong + */ + if (AGX_G11) { + unsigned nr = batch->key.nr_cbufs; + unsigned reads = + (ctx->stage[MESA_SHADER_FRAGMENT].shader->info.uses_fbfetch + ? ~0u + : ctx->blend->reads) & + BITFIELD_MASK(nr); + + enum pipe_format formats[PIPE_MAX_COLOR_BUFS] = {0}; + for (unsigned i = 0; i < nr; ++i) + formats[i] = batch->key.cbufs[i].format; + + struct agx_tilebuffer_layout tib = agx_build_tilebuffer_layout( + formats, nr, util_framebuffer_get_num_samples(&batch->key), + util_framebuffer_get_num_layers(&batch->key) > 1); + + unsigned copied = 0; + u_foreach_bit(rt, reads) { + if (tib.g11_tib[rt]) + copied |= BITFIELD_BIT(rt); + } + + if (copied && !batch->g11_tib) { + if (batch->initialized) { + agx_flush_batch_for_reason(ctx, batch, "G11 tilebuffer copy"); + batch = agx_get_batch(ctx); + } + + batch->g11_tib = true; + } + + if ((reads & ~copied) && + ((batch->draw | batch->clear) & PIPE_CLEAR_COLOR)) { + agx_flush_batch_for_reason(ctx, batch, "G11 render target read"); + batch = agx_get_batch(ctx); + batch->g11_tib = copied != 0; + } + } + + if (AGX_G11) { + uint64_t cost = + indirect ? AGX_G11_HEAP_INDIRECT + : agx_g11_heap_cost((uint64_t)draws[0].count * + MAX2(info->instance_count, 1), + g11_outputs); + + if (batch->g11_heap_B && batch->g11_heap_B + cost > AGX_G11_HEAP_BUDGET) { + agx_flush_batch_for_reason(ctx, batch, "G11 tiler heap"); + batch = agx_get_batch(ctx); + } + + batch->g11_heap_B += cost; + } + uint64_t ib = 0; size_t ib_extent = 0; @@ -5202,7 +5832,10 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, uint8_t *out = agx_encode_state(batch, batch->vdm.current); - if (info->index_size && info->primitive_restart) { + if (AGX_G11 && info->index_size) { + out = agx_g11_restart(out, batch->g11_vs_header, info->primitive_restart, + info->restart_index); + } else if (info->index_size && info->primitive_restart) { agx_push(out, VDM_STATE, cfg) cfg.restart_index_present = true; @@ -5314,6 +5947,19 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, batch->incoherent_writes = true; } + if (cs && cs->global_scratch_B) { + assert(grid.mode == AGX_CDM_MODE_DIRECT && "no G11 scratch for indirect"); + uint64_t size = (uint64_t)grid.count[0] * grid.count[1] * grid.count[2] * + cs->global_scratch_B; + batch->uniforms.scratch_base = + agx_pool_alloc_aligned(&batch->pool, size, 64).gpu; + } + + assert(!cs || !cs->b.info.spill_stride || + cs->b.info.local_size + variable_shared_mem + + wg.x * wg.y * wg.z * cs->b.info.spill_stride <= + AGX_G11_SPILL_TOP); + agx_update_descriptors(batch, cs); agx_upload_uniforms(batch); @@ -5344,6 +5990,11 @@ agx_launch(struct agx_batch *batch, struct agx_grid grid, cs ? agx_nr_tex_descriptors(batch, cs) : 0; cfg.sampler_state_register_count = translate_sampler_state_count(ctx, stage); + + /* See agx_build_pipeline */ + if (AGX_G11 && (!cfg.texture_state_register_count || + !cs->b.info.sampler_state_count)) + cfg.sampler_state_register_count = 0; } agx_launch_internal(batch, grid, wg, launch, stage, usc); diff --git a/src/gallium/drivers/asahi/agx_state.h b/src/gallium/drivers/asahi/agx_state.h index 967cd2bd7a9..b9f8844d6a7 100644 --- a/src/gallium/drivers/asahi/agx_state.h +++ b/src/gallium/drivers/asahi/agx_state.h @@ -30,6 +30,7 @@ #include "util/disk_cache.h" #include "util/hash_table.h" #include "util/rwlock.h" +#include "util/u_dynarray.h" #include "util/u_range.h" #include "agx_bg_eot.h" #include "agx_helpers.h" @@ -168,6 +169,9 @@ struct PACKED agx_draw_uniforms { * indices. This mapping must be compatible with the fragment shader. */ uint16_t uvs_index[VARYING_SLOT_MAX]; + + /* G11: this dispatch's private memory, global_scratch_B per thread */ + uint64_t scratch_base; }; struct PACKED agx_stage_uniforms { @@ -223,6 +227,9 @@ struct agx_compiled_shader { /* Mapped executable memory */ struct agx_bo *bo; + /* G11: bytes of private memory per thread, kept in global memory */ + unsigned global_scratch_B; + /* Uniforms the driver must push */ unsigned push_range_count; struct agx_push_range push[AGX_MAX_PUSH_RANGES]; @@ -241,6 +248,13 @@ struct agx_compiled_shader { struct agx_compiled_shader *gs_count, *pre_gs; struct agx_compiled_shader *gs_copy; + /* G11: the copy program before its varying slots are known, and every + * variant compiled from it, one per fragment shader flat/linear masks + */ + nir_shader *gs_copy_nir; + struct util_dynarray gs_copies; + uint64_t flat, linear; + struct poly_gs_info gs; /* Logical shader stage used for descriptor access. This may differ from the @@ -389,6 +403,12 @@ struct agx_batch { uint32_t clear, draw, load, resolve, feedback; bool initialized; + /* Estimated G11 tiler heap use, see agx_g11_heap_cost */ + uint64_t g11_heap_B; + + /* G11: render targets have a tilebuffer copy, see agx_draw_vbo */ + bool g11_tib; + uint64_t uploaded_clear_color[PIPE_MAX_COLOR_BUFS]; double clear_depth; unsigned clear_stencil; @@ -405,6 +425,9 @@ struct agx_batch { uint32_t varyings; struct agx_varyings_vs linked_varyings; + /* G11: header of the last vertex state token, repeated by restart tokens */ + uint32_t g11_vs_header; + struct agx_draw_uniforms uniforms; struct agx_stage_uniforms stage_uniforms[MESA_SHADER_STAGES]; @@ -478,6 +501,9 @@ struct agx_blend { /* PIPE_CLEAR_* bitmask corresponding to this blend state */ uint32_t store; + + /* Render targets the blend or mask reads back */ + uint8_t reads; }; struct asahi_vs_shader_key { @@ -499,7 +525,8 @@ struct agx_vertex_elements { struct asahi_fs_shader_key { enum pipe_format rt_formats[PIPE_MAX_COLOR_BUFS]; uint8_t nr_samples; - bool padding[7]; + bool g11_tib; + bool padding[6]; }; static_assert(sizeof(struct asahi_fs_shader_key) == 40, "no holes"); @@ -942,6 +969,21 @@ agx_resource(struct pipe_resource *pctx) return (struct agx_resource *)pctx; } +/* + * G11 multisampled reads and writes are not known, so its multisampled colour + * buffers are linear 2D arrays with a layer per sample: sampled as arrays, + * written as one tall 2D image (agx_nir_lower_tilebuffer.c). Depth and stencil + * keep the hardware layout for ZLS, with a pixel's samples side by side, and + * are sampled as a one-layer array of a format that holds them all as + * channels (agx_pack_texture, agx_nir_lower_texture.c). + */ +static inline bool +agx_g11_ms_layers(const struct pipe_resource *p) +{ + return AGX_G11 && p->nr_samples > 1 && + !util_format_is_depth_or_stencil(p->format); +} + static inline bool agx_resource_valid(struct agx_resource *rsrc, int level) { diff --git a/src/gallium/include/pipe/p_defines.h b/src/gallium/include/pipe/p_defines.h index a1871e3de7d..d475c3351cb 100644 --- a/src/gallium/include/pipe/p_defines.h +++ b/src/gallium/include/pipe/p_defines.h @@ -940,6 +940,8 @@ struct pipe_caps { bool query_pipeline_statistics; bool mixed_framebuffer_sizes; bool vs_layer_viewport; + /* Only the first layer of a render target can be drawn to */ + bool no_layered_rendering; bool texture_gather_sm5; bool buffer_map_persistent_coherent; bool fake_sw_msaa; diff --git a/src/gallium/winsys/kmsro/drm/kmsro_drm_winsys.c b/src/gallium/winsys/kmsro/drm/kmsro_drm_winsys.c index 2ead5dbf693..3c5ab173fe1 100644 --- a/src/gallium/winsys/kmsro/drm/kmsro_drm_winsys.c +++ b/src/gallium/winsys/kmsro/drm/kmsro_drm_winsys.c @@ -88,7 +88,10 @@ struct pipe_screen *kmsro_drm_screen_create(int kms_fd, if (strcmp(render_dev_name, "asahi") == 0) { #if defined(GALLIUM_ASAHI) - ro->create_for_resource = renderonly_create_gpu_import_for_resource; + /* G11's display pipe scans contiguous memory only, so the display + * allocates scanout buffers and the GPU imports them. + */ + ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource; screen = asahi_drm_screen_create_renderonly(ro->gpu_fd, ro, config); #endif } diff --git a/src/mesa/state_tracker/st_pbo.c b/src/mesa/state_tracker/st_pbo.c index 7d7bfc287dd..dae7ab125b4 100644 --- a/src/mesa/state_tracker/st_pbo.c +++ b/src/mesa/state_tracker/st_pbo.c @@ -660,7 +660,7 @@ st_init_pbo_helpers(struct st_context *st) st->pbo.rgba_only = screen->caps.buffer_sampler_view_rgba_only; - if (screen->caps.vs_instanceid) { + if (screen->caps.vs_instanceid && !screen->caps.no_layered_rendering) { if (screen->caps.vs_layer_viewport) { st->pbo.layers = true; } else if (screen->caps.max_geometry_output_vertices >= 3) { diff --git a/src/util/u_thread.c b/src/util/u_thread.c index d6b3a074e1d..921e88e7385 100644 --- a/src/util/u_thread.c +++ b/src/util/u_thread.c @@ -86,6 +86,16 @@ int u_thread_create(thrd_t *thrd, int (*routine)(void *), void *param) */ sigdelset(&new_set, SIGSEGV); pthread_sigmask(SIG_BLOCK, &new_set, &saved_set); +#if DETECT_OS_LINUX && !defined(__GLIBC__) + /* musl gives threads 128 KiB of stack, which the compilers overflow on + * large shaders. Use glibc's 8 MiB. + */ + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setstacksize(&attr, 8 << 20); + pthread_setattr_default_np(&attr); + pthread_attr_destroy(&attr); +#endif ret = thrd_create(thrd, thread_routine, paramp); pthread_sigmask(SIG_SETMASK, &saved_set, NULL); #else diff --git a/src/vulkan/runtime/vk_meta.h b/src/vulkan/runtime/vk_meta.h index 0e4e35f1542..3c14dce949c 100644 --- a/src/vulkan/runtime/vk_meta.h +++ b/src/vulkan/runtime/vk_meta.h @@ -110,6 +110,8 @@ struct vk_meta_device { uint32_t max_bind_map_buffer_size_B; bool use_layered_rendering; + /* Blit one layer per render, through 2D views (no layered rendering) */ + bool blit_per_layer; bool use_gs_for_layer; bool use_stencil_export; diff --git a/src/vulkan/runtime/vk_meta_blit_resolve.c b/src/vulkan/runtime/vk_meta_blit_resolve.c index 17fbb4856af..7d688278a0a 100644 --- a/src/vulkan/runtime/vk_meta_blit_resolve.c +++ b/src/vulkan/runtime/vk_meta_blit_resolve.c @@ -525,6 +525,27 @@ do_blit(struct vk_command_buffer *cmd, const struct vk_device_dispatch_table *disp = &device->dispatch_table; VkResult result; + if (meta->blit_per_layer && (dst_layer_count > 1 || dst_rect->layer)) { + for (uint32_t l = 0; l < dst_layer_count; l++) { + uint32_t z = dst_rect->layer + l; + VkImageSubresourceLayers sub = dst_subres; + sub.baseArrayLayer = + dst_image->image_type == VK_IMAGE_TYPE_3D ? z : sub.baseArrayLayer + z; + sub.layerCount = 1; + + struct vk_meta_rect rect = *dst_rect; + rect.layer = 0; + + struct vk_meta_blit_push_data p = *push; + p.z_off += z * p.z_scale; + + do_blit(cmd, meta, src_image, src_format, src_image_layout, src_subres, + dst_image, dst_format, dst_image_layout, sub, sampler, key, &p, + &rect, 1); + } + return; + } + VkPipelineLayout pipeline_layout; result = get_blit_pipeline_layout(device, meta, &pipeline_layout); if (unlikely(result != VK_SUCCESS)) { @@ -630,7 +651,9 @@ do_blit(struct vk_command_buffer *cmd, .pNext = &dst_view_usage, .flags = VK_IMAGE_VIEW_CREATE_DRIVER_INTERNAL_BIT_MESA, .image = vk_image_to_handle(dst_image), - .viewType = vk_image_sampled_view_type(dst_image), + .viewType = meta->blit_per_layer + ? vk_image_render_view_type(dst_image, 1) + : vk_image_sampled_view_type(dst_image), .format = dst_format, .subresourceRange = { .aspectMask = dst_subres.aspectMask,