From c013b58e4a1c5659b40c217dd7622013589c6fd7 Mon Sep 17 00:00:00 2001 From: kieroid Date: Thu, 3 Dec 2026 04:12:12 +0800 Subject: [PATCH] asahi: Bring the A12 (G11) support up to badapple 74ef26a The A12 port came from an older badapple snapshot (61bd066f, through Project Cascadia). badapple has since made G11 pass dEQP-GLES3 (44523 tests) and run Vulkan through Honeykrisp (vkcube, multisampling) on the iPhone XS Max. Take the difference between the two versions of its patch: - compiler: G11 sample results stay in r0-r31 under early kill, LOD queries honour sampler clamps and the mip filter, gathers and min-LOD clamps without hardware support, coherent memory through atomics, no fp16 denormal modes, coherent loads skip the zero page; - GL: drops draws past 16 texture states, compressed mip copies, the tilebuffer copy in place after clears; - Vulkan: multisampling, sparse residency refused, big binds split into at most 256 operations a call (the A12 kernel's limit); - device: G11 heap budget. [Orchard: from kiki/badapple@74ef26a patches/mesa-0001-asahi-g11.patch; applied as the difference to the 61bd066f patch (via rzxvx/Project-Cascadia@566808a devices/a12/mesa/mesa-0001-asahi-g11.patch, both on Mesa 24a0a3bd) onto our main; all 24 files applied without conflicts, our capture heap kept.] Assisted-by: LLM --- src/asahi/compiler/agx_compile.c | 5 +- src/asahi/compiler/agx_nir.h | 2 + src/asahi/compiler/agx_nir_lower_address.c | 48 +++ src/asahi/compiler/agx_nir_lower_texture.c | 329 +++++++++++++++++++-- src/asahi/compiler/agx_pack.c | 6 +- src/asahi/compiler/agx_register_allocate.c | 73 +++-- src/asahi/genxml/cmdbuf.xml | 4 + src/asahi/lib/agx_device.c | 25 +- src/asahi/lib/agx_helpers.h | 9 + src/asahi/lib/agx_nir_lower_msaa.c | 4 + src/asahi/libagx/copy.cl | 10 + src/asahi/libagx/texture.cl | 13 +- src/asahi/vulkan/hk_cmd_buffer.c | 5 +- src/asahi/vulkan/hk_cmd_clear.c | 6 + src/asahi/vulkan/hk_cmd_draw.c | 52 +++- src/asahi/vulkan/hk_cmd_meta.c | 63 +++- src/asahi/vulkan/hk_image.c | 29 +- src/asahi/vulkan/hk_image.h | 12 + src/asahi/vulkan/hk_image_view.c | 90 +++++- src/asahi/vulkan/hk_physical_device.c | 14 +- src/asahi/vulkan/hk_shader.c | 96 +++++- src/gallium/drivers/asahi/agx_pipe.c | 27 +- src/gallium/drivers/asahi/agx_state.c | 49 ++- src/gallium/drivers/asahi/agx_state.h | 3 + 24 files changed, 856 insertions(+), 118 deletions(-) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index a6cd71a7afc..c3e18b154ab 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -2295,8 +2295,11 @@ agx_emit_tex(agx_builder *b, nir_tex_instr *instr) case nir_tex_src_lod: case nir_tex_src_bias: lod = index; + /* G11 fragment shaders take automatic LOD from derivatives */ lod_is_zero = nir_src_is_const(instr->src[i].src) && - nir_src_as_uint(instr->src[i].src) == 0; + nir_src_as_uint(instr->src[i].src) == 0 && + !(AGX_G11 && b->shader->stage == MESA_SHADER_FRAGMENT && + instr->op == nir_texop_txl); break; case nir_tex_src_lod_bias_min_agx: diff --git a/src/asahi/compiler/agx_nir.h b/src/asahi/compiler/agx_nir.h index 32dbfc54a81..a7dc21d30a6 100644 --- a/src/asahi/compiler/agx_nir.h +++ b/src/asahi/compiler/agx_nir.h @@ -25,6 +25,8 @@ 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_g11_coherent_image_atomics(struct nir_shader *s); +bool agx_g11_samplers_from_0(const 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_address.c b/src/asahi/compiler/agx_nir_lower_address.c index d59860f7c9d..36a26b59471 100644 --- a/src/asahi/compiler/agx_nir_lower_address.c +++ b/src/asahi/compiler/agx_nir_lower_address.c @@ -5,6 +5,7 @@ #include #include "compiler/nir/nir_builder.h" +#include "asahi/genxml/agx_g11.h" #include "agx_nir.h" #include "nir.h" #include "nir_intrinsics.h" @@ -150,11 +151,58 @@ pass(struct nir_builder *b, nir_intrinsic_instr *intr, void *data) return true; } +/* + * G11 plain loads and stores stay in the core's cache: barriers do not make + * them visible to other cores, and the big barriers G13 uses end the thread. + * Atomics are coherent, so coherent 32 and 64-bit accesses become atomics. + */ +static bool +g11_coherent_atomics(struct nir_builder *b, nir_intrinsic_instr *intr, + void *data) +{ + bool store = intr->intrinsic == nir_intrinsic_store_global; + if ((!store && intr->intrinsic != nir_intrinsic_load_global) || + !(nir_intrinsic_access(intr) & ACCESS_COHERENT)) + return false; + + nir_def *value = store ? intr->src[0].ssa : &intr->def; + unsigned bits = value->bit_size; + if (bits < 32) + return false; + + b->cursor = nir_before_instr(&intr->instr); + nir_def *addr = intr->src[store ? 1 : 0].ssa; + nir_def *out[NIR_MAX_VEC_COMPONENTS]; + + for (unsigned c = 0; c < value->num_components; ++c) { + if (store && !(nir_intrinsic_write_mask(intr) & BITFIELD_BIT(c))) + continue; + + nir_def *a = nir_iadd_imm(b, addr, c * (bits / 8)); + out[c] = nir_global_atomic( + b, bits, a, + store ? nir_channel(b, value, c) : nir_imm_intN_t(b, 0, bits), + .atomic_op = store ? nir_atomic_op_xchg : nir_atomic_op_iadd); + } + + if (store) + nir_instr_remove(&intr->instr); + else + nir_def_replace(&intr->def, nir_vec(b, out, value->num_components)); + + return true; +} + bool agx_nir_lower_address(nir_shader *nir) { bool progress = false; + /* Precompiled kernels are built without a device key, which atomics need */ + if (AGX_G11 && !nir->info.internal) + NIR_PASS(progress, nir, nir_shader_intrinsics_pass, g11_coherent_atomics, + nir_metadata_control_flow, NULL); + /* First, clean up as much as possible. This will make fusing more effective. */ do { diff --git a/src/asahi/compiler/agx_nir_lower_texture.c b/src/asahi/compiler/agx_nir_lower_texture.c index 239fa1ed7e0..54d08110cab 100644 --- a/src/asahi/compiler/agx_nir_lower_texture.c +++ b/src/asahi/compiler/agx_nir_lower_texture.c @@ -22,6 +22,80 @@ /* Residency flags are inverted from NIR */ #define AGX_RESIDENT (0) +/* + * As for buffers (agx_nir_lower_address.c), G11 image loads and stores are not + * seen by other cores after a barrier, but atomics are. Coherent accesses to + * single 32-bit integer channels become image atomics, while images are still + * derefs so the drivers lower them like any other atomic. + * + * ponytail: other formats stay plain, they have no atomics + */ +static bool +g11_coherent_image_atomic(struct nir_builder *b, nir_intrinsic_instr *intr, + void *data) +{ + bool store = intr->intrinsic == nir_intrinsic_image_deref_store; + if (!store && intr->intrinsic != nir_intrinsic_image_deref_load) + return false; + + /* The GLSL coherent qualifier sits on the variable. SPIR-V texel + * availability (devicecoherent) leaves a barrier just before the store or + * after the load, which nir_lower_memory_model drops without marking the + * access, so look for those first. + */ + nir_variable *var = nir_intrinsic_get_var(intr, 0); + enum gl_access_qualifier access = + nir_intrinsic_access(intr) | (var ? var->data.access : 0); + + nir_instr *next = &intr->instr; + do { + next = store ? nir_instr_prev(next) : nir_instr_next(next); + } while (next && next->type != nir_instr_type_intrinsic && + next->type != nir_instr_type_tex && + next->type != nir_instr_type_call); + + if (next && next->type == nir_instr_type_intrinsic) { + nir_intrinsic_instr *bar = nir_instr_as_intrinsic(next); + if (bar->intrinsic == nir_intrinsic_barrier && + (nir_intrinsic_memory_modes(bar) & nir_var_image) && + (nir_intrinsic_memory_semantics(bar) & + (store ? NIR_MEMORY_MAKE_AVAILABLE : NIR_MEMORY_MAKE_VISIBLE))) + access |= ACCESS_COHERENT; + } + + enum pipe_format format = nir_intrinsic_format(intr); + if (!(access & ACCESS_COHERENT) || + (format != PIPE_FORMAT_R32_UINT && format != PIPE_FORMAT_R32_SINT)) + return false; + + b->cursor = nir_before_instr(&intr->instr); + nir_def *data_in = + store ? nir_channel(b, intr->src[3].ssa, 0) : nir_imm_int(b, 0); + nir_def *res = nir_image_deref_atomic( + b, 32, intr->src[0].ssa, intr->src[1].ssa, intr->src[2].ssa, data_in, + .image_dim = nir_intrinsic_image_dim(intr), + .image_array = nir_intrinsic_image_array(intr), .format = format, + .access = access, + .atomic_op = store ? nir_atomic_op_xchg : nir_atomic_op_iadd); + + if (store) { + nir_instr_remove(&intr->instr); + } else { + nir_def *zero = nir_imm_int(b, 0); + nir_def *v[4] = {res, zero, zero, nir_imm_int(b, 1)}; + nir_def_replace(&intr->def, nir_vec(b, v, intr->def.num_components)); + } + + return true; +} + +bool +agx_nir_g11_coherent_image_atomics(nir_shader *s) +{ + return nir_shader_intrinsics_pass(s, g11_coherent_image_atomic, + nir_metadata_control_flow, NULL); +} + static bool fence_image(struct nir_builder *b, nir_intrinsic_instr *intr, void *data) { @@ -116,7 +190,8 @@ lower_tex_crawl(nir_builder *b, nir_instr *instr, UNUSED void *data) libagx_txs(b, ptr, lod, nir_imm_int(b, nr_comps), nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_BUF), nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_1D), - nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_2D), + nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_2D || + tex->sampler_dim == GLSL_SAMPLER_DIM_MS), nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE), nir_imm_bool(b, tex->is_array)); } else if (tex->op == nir_texop_query_levels) { @@ -278,9 +353,40 @@ g11_txf_to_txl(nir_builder *b, nir_tex_instr *tex) nir_vec(b, c, tex->coord_components)); } +/* + * G11 has 16 sampler slots: the txf sampler takes 0 and API samplers follow. + * A shader using API sampler 15 (GLES needs 16) cannot fit that, so it gets + * slots 0 to 15 for API samplers 0 to 15 and no txf sampler. The GL driver + * uploads such shaders' samplers 1:1 (agx_upload_samplers). + * + * ponytail: texel fetches and image loads in such a shader sample through API + * sampler 0; give them the txf state if a 16-sampler shader ever needs them + */ +bool +agx_g11_samplers_from_0(const nir_shader *s) +{ + if (!AGX_G11) + return false; + + nir_foreach_function_impl(impl, s) { + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_tex && + nir_tex_instr_need_sampler(nir_instr_as_tex(instr)) && + nir_instr_as_tex(instr)->sampler_index >= 15) + return true; + } + } + } + + return false; +} + static bool -lower_regular_texture(nir_builder *b, nir_instr *instr, UNUSED void *data) +lower_regular_texture(nir_builder *b, nir_instr *instr, void *data) { + bool samplers_from_0 = *(bool *)data; + if (instr->type != nir_instr_type_tex) return false; @@ -297,38 +403,29 @@ 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 + /* Layer * samples + sample for colour, a channel per sample 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); + nir_def *s = nir_u2u32(b, nir_steal_tex_src(tex, nir_tex_src_ms_index)); + nir_def *l = tex->is_array ? nir_u2u32(b, nir_channel(b, xy, 2)) + : nir_imm_int(b, 0); + + nir_def *k = libagx_g11_samples_log2(b, texture_descriptor_ptr(b, tex)); + nir_def *per_layer = nir_ine_imm(b, k, 0); + nir_def *layer = + nir_bcsel(b, per_layer, nir_iadd(b, nir_ishl(b, l, k), s), l); 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)))); + nir_u2uN(b, layer, xy->bit_size))); b->cursor = nir_after_instr(&tex->instr); nir_def *res = nir_bcsel( @@ -479,8 +576,9 @@ lower_regular_texture(nir_builder *b, nir_instr *instr, UNUSED void *data) if (!is_txf && nir_tex_instr_src_index(tex, nir_tex_src_sampler_handle) < 0) { - nir_tex_instr_add_src(tex, nir_tex_src_sampler_handle, - nir_iadd_imm(b, nir_u2u16(b, sampler), 1)); + nir_tex_instr_add_src( + tex, nir_tex_src_sampler_handle, + nir_iadd_imm(b, nir_u2u16(b, sampler), samplers_from_0 ? 0 : 1)); } return true; @@ -834,6 +932,10 @@ agx_nir_lower_texture_early(nir_shader *s, bool support_lod_bias) { bool progress = false; + if (AGX_G11) + NIR_PASS(progress, s, nir_shader_intrinsics_pass, + g11_coherent_image_atomic, nir_metadata_control_flow, NULL); + NIR_PASS(progress, s, nir_shader_intrinsics_pass, lower_robustness, nir_metadata_control_flow, NULL); @@ -859,7 +961,8 @@ agx_nir_lower_texture_early(nir_shader *s, bool support_lod_bias) /* * G11 has no LOD query: compute the LOD from coordinate derivatives. The - * sampler's LOD bias and clamps are not applied. + * sampler's LOD bias is not applied; Vulkan applies its clamps and mip filter + * after this (g11_clamp_lod_query in hk_shader.c). */ static bool g11_lower_lod_query(nir_builder *b, nir_tex_instr *tex, UNUSED void *data) @@ -910,10 +1013,175 @@ g11_lower_lod_query(nir_builder *b, nir_tex_instr *tex, UNUSED void *data) return true; } +/* + * G11 has no gather: sample each of the four texels at its centre. The sampler + * still applies the wrap mode, the border and the depth compare, and linear + * filtering at a texel centre returns that texel. Cube gathers move the + * direction across the face, so edge texels come from the next face. + * + * ponytail: no rectangle gathers (GL only), cube corners are whatever the + * direction lands on + */ +static bool +g11_lower_gather(nir_builder *b, nir_tex_instr *tex, UNUSED void *data) +{ + if (tex->op != nir_texop_tg4) + return false; + + b->cursor = nir_before_instr(&tex->instr); + bool cube = tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE; + int c = nir_tex_instr_src_index(tex, nir_tex_src_coord); + nir_def *coord = nir_f2f32(b, tex->src[c].src.ssa); + nir_def *offset = nir_steal_tex_src(tex, nir_tex_src_offset); + nir_def *size = nir_i2f32( + b, nir_trim_vector(b, + nir_build_texture_query(b, tex, nir_texop_txs, + 2 + tex->is_array, + nir_type_int32, false, true), + 2)); + + /* Face coordinates in [0, 1] (Vulkan 16.5.4), m is the major axis */ + nir_def *x = nir_channel(b, coord, 0), *y = nir_channel(b, coord, 1); + nir_def *uv = nir_vec2(b, x, y); + nir_def *z = NULL, *m = NULL, *isx = NULL, *isy = NULL; + if (cube) { + z = nir_channel(b, coord, 2); + nir_def *ax = nir_fabs(b, x), *ay = nir_fabs(b, y), *az = nir_fabs(b, z); + isx = nir_iand(b, nir_fge(b, ax, ay), nir_fge(b, ax, az)); + isy = nir_iand(b, nir_inot(b, isx), nir_fge(b, ay, az)); + m = nir_bcsel(b, isx, ax, nir_bcsel(b, isy, ay, az)); + nir_def *sc = + nir_bcsel(b, isx, nir_fneg(b, nir_fmul(b, nir_fsign(b, x), z)), + nir_bcsel(b, isy, x, nir_fmul(b, nir_fsign(b, z), x))); + nir_def *tc = nir_bcsel(b, isy, nir_fmul(b, nir_fsign(b, y), z), + nir_fneg(b, y)); + uv = nir_fmul_imm( + b, nir_fadd_imm(b, nir_fdiv(b, nir_vec2(b, sc, tc), m), 1.0), 0.5); + } + + nir_def *t = nir_fadd_imm(b, nir_fmul(b, uv, size), -0.5); + if (offset) + t = nir_fadd(b, t, nir_i2f32(b, offset)); + nir_def *i0 = nir_fadd_imm(b, nir_ffloor(b, t), 0.5); + nir_def *centre[2] = {nir_fdiv(b, i0, size), + nir_fdiv(b, nir_fadd_imm(b, i0, 1.0), size)}; + + /* 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 k = 0; k < 4; ++k) { + nir_def *s = nir_channel(b, centre[ij[k][0]], 0); + nir_def *u = nir_channel(b, centre[ij[k][1]], 1); + nir_def *p; + if (cube) { + /* A texel off the face is the next face's edge texel: put it at + * k = size / (size - 1), which projects to that texel's centre + */ + s = nir_fadd_imm(b, nir_fmul_imm(b, s, 2.0), -1.0); + u = nir_fadd_imm(b, nir_fmul_imm(b, u, 2.0), -1.0); + nir_def *out_s = nir_fgt_imm(b, nir_fabs(b, s), 1.0); + nir_def *out_u = nir_fgt_imm(b, nir_fabs(b, u), 1.0); + nir_def *w = nir_channel(b, size, 0); + nir_def *k = nir_fdiv(b, w, nir_fmax_imm(b, nir_fadd_imm(b, w, -1.0), 1.0)); + nir_def *s2 = nir_bcsel(b, out_s, nir_fmul(b, nir_fsign(b, s), k), + nir_bcsel(b, out_u, nir_fmul(b, s, k), s)); + u = nir_bcsel(b, out_u, nir_fmul(b, nir_fsign(b, u), k), + nir_bcsel(b, out_s, nir_fmul(b, u, k), u)); + s = nir_fmul(b, s2, m); + u = nir_fmul(b, u, m); + p = nir_vec3( + b, + nir_bcsel(b, isx, x, + nir_bcsel(b, isy, s, nir_fmul(b, nir_fsign(b, z), s))), + nir_bcsel(b, isy, y, nir_fneg(b, u)), + nir_bcsel(b, isx, nir_fneg(b, nir_fmul(b, nir_fsign(b, x), s)), + nir_bcsel(b, isy, nir_fmul(b, nir_fsign(b, y), u), z))); + } else { + p = nir_vec2(b, s, u); + } + + if (tex->is_array) { + p = nir_vector_insert_imm( + b, nir_pad_vector(b, p, p->num_components + 1), + nir_channel(b, coord, tex->coord_components - 1), + p->num_components); + } + p = nir_f2fN(b, p, tex->src[c].src.ssa->bit_size); + + nir_def *zero = nir_imm_float(b, 0.0); + + /* Clones get their uses on insertion, so edit them after */ + nir_tex_instr *smp = + nir_instr_as_tex(nir_instr_clone(b->shader, &tex->instr)); + smp->op = nir_texop_txl; + smp->def.num_components = tex->is_shadow ? 1 : 4; + smp->is_new_style_shadow = true; + smp->component = 0; + nir_builder_instr_insert(b, &smp->instr); + nir_src_rewrite(&smp->src[c].src, p); + nir_tex_instr_add_src(smp, nir_tex_src_lod, zero); + out[k] = nir_channel(b, &smp->def, tex->is_shadow ? 0 : tex->component); + } + + nir_def_replace(&tex->def, nir_vec(b, out, 4)); + return true; +} + +/* + * G11 has no LOD modes with a minimum. Compute the LOD from derivatives + * (gradients go through nir_lower_tex) and sample at max(LOD, min_lod). + */ +static bool +g11_lower_min_lod(nir_builder *b, nir_tex_instr *tex, UNUSED void *data) +{ + if ((tex->op != nir_texop_tex && tex->op != nir_texop_txb) || + nir_tex_instr_src_index(tex, nir_tex_src_min_lod) < 0) + return false; + + b->cursor = nir_before_instr(&tex->instr); + + /* LOD queries take no layer */ + unsigned n = tex->coord_components - tex->is_array; + nir_def *coord = + nir_trim_vector(b, nir_get_tex_src(tex, nir_tex_src_coord), n); + + /* Clones get their uses on insertion, so edit them after */ + nir_tex_instr *q = nir_instr_as_tex(nir_instr_clone(b->shader, &tex->instr)); + q->op = nir_texop_lod; + q->is_shadow = false; + q->dest_type = nir_type_float32; + q->coord_components = n; + q->def.num_components = 2; + q->def.bit_size = 32; + nir_builder_instr_insert(b, &q->instr); + for (int i = q->num_srcs - 1; i >= 0; --i) { + nir_tex_src_type t = q->src[i].src_type; + if (t == nir_tex_src_coord) + nir_src_rewrite(&q->src[i].src, coord); + else if (t != nir_tex_src_texture_handle && + t != nir_tex_src_sampler_handle && + t != nir_tex_src_texture_offset && + t != nir_tex_src_sampler_offset) + nir_tex_instr_remove_src(q, i); + } + + nir_def *lod = nir_channel(b, &q->def, 1); + nir_def *bias = nir_steal_tex_src(tex, nir_tex_src_bias); + if (bias) + lod = nir_fadd(b, lod, nir_f2f32(b, bias)); + lod = nir_fmax(b, lod, + nir_f2f32(b, nir_steal_tex_src(tex, nir_tex_src_min_lod))); + + tex->op = nir_texop_txl; + nir_tex_instr_add_src(tex, nir_tex_src_lod, lod); + return true; +} + bool agx_nir_lower_texture(nir_shader *s) { bool progress = false; + bool samplers_from_0 = agx_g11_samplers_from_0(s); nir_tex_src_type_constraints tex_constraints = { [nir_tex_src_lod] = {true, 16}, @@ -932,6 +1200,15 @@ agx_nir_lower_texture(nir_shader *s) NIR_PASS(progress, s, nir_lower_image_atomics_to_global, NULL, NULL); + if (AGX_G11) { + nir_lower_tex_options txd_clamp = {.lower_txd_clamp = true}; + NIR_PASS(progress, s, nir_lower_tex, &txd_clamp); + NIR_PASS(progress, s, nir_shader_tex_pass, g11_lower_min_lod, + nir_metadata_control_flow, NULL); + NIR_PASS(progress, s, nir_shader_tex_pass, g11_lower_gather, + nir_metadata_control_flow, NULL); + } + NIR_PASS(progress, s, nir_shader_intrinsics_pass, legalize_image_lod, nir_metadata_control_flow, NULL); NIR_PASS(progress, s, nir_shader_intrinsics_pass, lower_images, @@ -952,7 +1229,7 @@ agx_nir_lower_texture(nir_shader *s) nir_metadata_control_flow, NULL); NIR_PASS(progress, s, nir_shader_instructions_pass, lower_regular_texture, - nir_metadata_none, NULL); + nir_metadata_none, &samplers_from_0); NIR_PASS(progress, s, nir_shader_instructions_pass, lower_tex_crawl, nir_metadata_control_flow, NULL); diff --git a/src/asahi/compiler/agx_pack.c b/src/asahi/compiler/agx_pack.c index d1e888ff410..117db5ce01c 100644 --- a/src/asahi/compiler/agx_pack.c +++ b/src/asahi/compiler/agx_pack.c @@ -783,12 +783,14 @@ agx_pack_instr(struct util_dynarray *emission, struct util_dynarray *fixups, * 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. + * A register index (separately compiled shaders) clears bit 22, as + * G13 clears its bit 23. */ - 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.type == AGX_INDEX_IMMEDIATE ? (1 << 22) : 0) | + ((value.value >> 6) << 24) | ((index_src.value >> 6) << 26) | (0x8u << 28); memcpy(util_dynarray_grow_bytes(emission, 1, 4), &raw, 4); diff --git a/src/asahi/compiler/agx_register_allocate.c b/src/asahi/compiler/agx_register_allocate.c index 033f0083f7f..882cdd53534 100644 --- a/src/asahi/compiler/agx_register_allocate.c +++ b/src/asahi/compiler/agx_register_allocate.c @@ -1014,34 +1014,6 @@ kill_source(struct ra_ctx *rctx, const agx_instr *I, unsigned s) BITSET_CLEAR_COUNT(rctx->used_regs[cls], reg, count); } -static void -try_kill_early_sources(struct ra_ctx *rctx, const agx_instr *I, - unsigned first_source, unsigned last_source, - unsigned region_end, unsigned region_base) -{ - unsigned dest_size = util_next_power_of_two(rctx->ncomps[I->dest[0].value]); - unsigned dest_end = region_base + dest_size; - - /* We can only early-kill a region if we can trivially allocate the - * destination to it. That way we never shuffle killed sources. - * - * To ensure that, the region must be aligned and cover the destination. - */ - if (region_base == region_end || - (rctx->ssa_to_reg[I->src[first_source].value] & (dest_size - 1)) || - ((region_end < dest_end) && - BITSET_TEST_RANGE(rctx->used_regs[RA_GPR], region_end, dest_end))) - return; - - for (unsigned s = first_source; s <= last_source; ++s) { - if (I->src[s].kill && !I->src[s].memory) { - kill_source(rctx, I, s); - rctx->early_killed = true; - I->src[s].kill = false; - } - } -} - /* * G11 samples, image writes and tile accesses encode their data and * coordinate registers in six bits (16-bit halves), so those must sit in @@ -1071,6 +1043,36 @@ g11_low_operand(const agx_instr *I, int s) } } +static void +try_kill_early_sources(struct ra_ctx *rctx, const agx_instr *I, + unsigned first_source, unsigned last_source, + unsigned region_end, unsigned region_base) +{ + unsigned dest_size = util_next_power_of_two(rctx->ncomps[I->dest[0].value]); + unsigned dest_end = region_base + dest_size; + + /* We can only early-kill a region if we can trivially allocate the + * destination to it. That way we never shuffle killed sources. + * + * To ensure that, the region must be aligned and cover the destination. + * A G11 low destination can't take a region past r31 (g11_low_dest). + */ + if (region_base == region_end || + (g11_low_operand(I, -1) && dest_end > G11_LOW_REGS) || + (rctx->ssa_to_reg[I->src[first_source].value] & (dest_size - 1)) || + ((region_end < dest_end) && + BITSET_TEST_RANGE(rctx->used_regs[RA_GPR], region_end, dest_end))) + return; + + for (unsigned s = first_source; s <= last_source; ++s) { + if (I->src[s].kill && !I->src[s].memory) { + kill_source(rctx, I, s); + rctx->early_killed = true; + I->src[s].kill = false; + } + } +} + /* * Move whatever lives in [base, base + n) to free registers elsewhere, * recording the copies. Fails, changing nothing, if there is no room. @@ -1083,21 +1085,25 @@ g11_evict_range(struct ra_ctx *rctx, unsigned base, unsigned n, 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) { + /* Walk from the bottom, since reg_to_ssa is only valid at a value's first + * register: a vector below base can reach into a range smaller than it. + */ + for (unsigned r = reserved_size(rctx->shader); 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) + if (r + k > base && (r < base || r + k > base + n)) return false; - vars[nr++] = var; + if (r >= base) + vars[nr++] = var; r += k - 1; } /* Find homes outside the range, reserving as we go */ + unsigned nr_vars = nr; BITSET_SET_COUNT(used, base, n); for (unsigned i = 0; i < nr && ok; ++i) { @@ -1109,7 +1115,10 @@ g11_evict_range(struct ra_ctx *rctx, unsigned base, unsigned n, nr = i; } + /* Free the range again, keeping the occupants if they stay */ BITSET_CLEAR_COUNT(used, base, n); + for (unsigned i = 0; i < nr_vars && !ok; ++i) + BITSET_SET_COUNT(used, rctx->ssa_to_reg[vars[i]], rctx->ncomps[vars[i]]); for (unsigned i = 0; i < nr; ++i) { unsigned k = rctx->ncomps[vars[i]]; diff --git a/src/asahi/genxml/cmdbuf.xml b/src/asahi/genxml/cmdbuf.xml index 6dd0b714f97..1395cca5f13 100644 --- a/src/asahi/genxml/cmdbuf.xml +++ b/src/asahi/genxml/cmdbuf.xml @@ -326,6 +326,10 @@ + + + diff --git a/src/asahi/lib/agx_device.c b/src/asahi/lib/agx_device.c index 7a31dbfb4a3..ef9ca906030 100644 --- a/src/asahi/lib/agx_device.c +++ b/src/asahi/lib/agx_device.c @@ -110,16 +110,21 @@ static int agx_drm_bo_bind(struct agx_device *dev, struct drm_asahi_gem_bind_op *ops, uint32_t count) { - struct drm_asahi_vm_bind vm_bind = { - .num_binds = count, - .vm_id = dev->vm_id, - .userptr = (uintptr_t)ops, - .stride = sizeof(*ops), - }; - - int ret = drmIoctl(dev->fd, DRM_IOCTL_ASAHI_VM_BIND, &vm_bind); - if (ret) { - fprintf(stderr, "DRM_IOCTL_ASAHI_VM_BIND failed\n"); + int ret = 0; + + /* The A12 kernel takes at most 256 ops a call, sparse binds can be more */ + for (uint32_t i = 0; i < count && !ret; i += 256) { + struct drm_asahi_vm_bind vm_bind = { + .num_binds = MIN2(count - i, 256), + .vm_id = dev->vm_id, + .userptr = (uintptr_t)(ops + i), + .stride = sizeof(*ops), + }; + + ret = drmIoctl(dev->fd, DRM_IOCTL_ASAHI_VM_BIND, &vm_bind); + if (ret) { + fprintf(stderr, "DRM_IOCTL_ASAHI_VM_BIND failed\n"); + } } return ret; diff --git a/src/asahi/lib/agx_helpers.h b/src/asahi/lib/agx_helpers.h index 818b19fcb91..056c6b4dac4 100644 --- a/src/asahi/lib/agx_helpers.h +++ b/src/asahi/lib/agx_helpers.h @@ -176,6 +176,15 @@ agx_pack_zls_control(struct agx_zls_control_packed *packed, #define AGX_G11_HEAP_BUDGET (6u << 20) #define AGX_G11_HEAP_INDIRECT (AGX_G11_HEAP_BUDGET / 64) +/* + * Each draw also takes heap per tile it touches, whatever its primitive + * count: 400 full-screen quad draws at 1024x1024 fit, 1500 overflowed, and a + * WebGL test's textured ones overflowed at about 400, so 20-27 B. Charged + * for every tile in the draw's viewport and scissor, since coverage is not + * known on the CPU. + */ +#define AGX_G11_HEAP_TILE_B 24 + static inline uint64_t agx_g11_heap_cost(uint64_t vertices, unsigned outputs) { diff --git a/src/asahi/lib/agx_nir_lower_msaa.c b/src/asahi/lib/agx_nir_lower_msaa.c index 351b4b861c7..27edc27de61 100644 --- a/src/asahi/lib/agx_nir_lower_msaa.c +++ b/src/asahi/lib/agx_nir_lower_msaa.c @@ -92,6 +92,10 @@ agx_nir_wrap_per_sample_loop(nir_shader *shader, uint8_t nr_samples) /* Create a loop in the wrapped function */ nir_loop *loop = nir_push_loop(&b); + /* Unrolled, G11's register allocator loses values moved into r0-r31 for + * image stores inside loops (agx_register_allocate g11_lower_sources) + */ + loop->control = nir_loop_control_unroll; { bit = nir_load_var(&b, i); nir_break_if(&b, nir_uge(&b, bit, end_bit)); diff --git a/src/asahi/libagx/copy.cl b/src/asahi/libagx/copy.cl index b5045e18051..1e949b4095e 100644 --- a/src/asahi/libagx/copy.cl +++ b/src/asahi/libagx/copy.cl @@ -27,6 +27,16 @@ libagx_copy_uchar(global uint8_t *dest, global uint8_t *src) dest[cl_global_id.x] = src[cl_global_id.x]; } +/* Byte rows of linear layers, z picks the layer */ +KERNEL(32) +libagx_copy_rows(global uint8_t *dest, global uint8_t *src, uint dest_stride_B, + uint src_stride_B, ulong dest_layer_B, ulong src_layer_B) +{ + uint3 id = cl_global_id; + dest[id.z * dest_layer_B + id.y * dest_stride_B + id.x] = + src[id.z * src_layer_B + id.y * src_stride_B + id.x]; +} + KERNEL(32) libagx_fill_uint4(global uint4 *address, uint layer_stride_uint4, uint a, uint b, uint c, uint d) diff --git a/src/asahi/libagx/texture.cl b/src/asahi/libagx/texture.cl index 090b2b0cac9..4f4ec6f0a93 100644 --- a/src/asahi/libagx/texture.cl +++ b/src/asahi/libagx/texture.cl @@ -34,7 +34,7 @@ libagx_txs(constant struct agx_texture_packed *ptr, uint16_t lod, /* 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; + size.z = d.depth_linear >> d.g11_samples_log2_sw; /* 1D Arrays have their second component as the layer count */ if (is_1d && is_array) @@ -58,6 +58,14 @@ libagx_txs(constant struct agx_texture_packed *ptr, uint16_t lod, return size; } +/* G11 multisampled colour reads layer * samples + sample, see agx_g11_ms_layers */ +uint +libagx_g11_samples_log2(constant struct agx_texture_packed *ptr) +{ + agx_unpack(NULL, ptr, TEXTURE, d); + return d.g11_samples_log2_sw; +} + uint libagx_texture_samples(constant struct agx_texture_packed *ptr) { @@ -67,6 +75,9 @@ libagx_texture_samples(constant struct agx_texture_packed *ptr) if (d.address == AGX_ZERO_PAGE_ADDRESS) return 0; + if (d.g11_samples_log2_sw) + return 1 << d.g11_samples_log2_sw; + /* We may assume the input is multisampled, so just check the samples */ return (d.samples == AGX_SAMPLE_COUNT_2) ? 2 : 4; } diff --git a/src/asahi/vulkan/hk_cmd_buffer.c b/src/asahi/vulkan/hk_cmd_buffer.c index f4ff25543a6..d42e3cd495a 100644 --- a/src/asahi/vulkan/hk_cmd_buffer.c +++ b/src/asahi/vulkan/hk_cmd_buffer.c @@ -657,7 +657,8 @@ hk_usc_upload_spilled_rt_descs(struct agx_usc_builder *b, 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) + * through the txf sampler (agx_nir_lower_tilebuffer.c). Its txs still + * reads the descriptor through u0_u1. */ if (AGX_G11) { agx_usc_pack(b, TEXTURE, cfg) { @@ -665,8 +666,6 @@ hk_usc_upload_spilled_rt_descs(struct agx_usc_builder *b, cfg.count = 2 * render->color_att_count; cfg.buffer = descs.gpu; } - - return; } /* Bind the base as u0_u1 for bindless access */ diff --git a/src/asahi/vulkan/hk_cmd_clear.c b/src/asahi/vulkan/hk_cmd_clear.c index a1fab549c53..715dccaa018 100644 --- a/src/asahi/vulkan/hk_cmd_clear.c +++ b/src/asahi/vulkan/hk_cmd_clear.c @@ -59,6 +59,12 @@ hk_clear_image(struct hk_cmd_buffer *cmd, struct hk_image *image, layer_count = vk_image_subresource_layer_count(&image->vk, range); } + /* A layer per sample, see hk_g11_ms_layers */ + if (hk_g11_ms_layers(image->vk.samples, image->vk.format)) { + base_array_layer *= image->vk.samples; + layer_count *= image->vk.samples; + } + enum pipe_format format = view_format ? view_format : layout->format; bool stencil = format == PIPE_FORMAT_S8_UINT; diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index f2bba244b1b..a74a2ff7969 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -645,8 +645,12 @@ hk_CmdBeginRendering(VkCommandBuffer commandBuffer, if (iview) { struct hk_image *image = container_of(iview->vk.image, struct hk_image, vk); + /* A layer per sample, see hk_g11_ms_layers */ render->g11_rt_stride_B[i] = - image->planes[iview->planes[0].image_plane].layout.layer_stride_B; + image->planes[iview->planes[0].image_plane].layout.layer_stride_B * + (hk_g11_ms_layers(image->vk.samples, image->vk.format) + ? image->vk.samples + : 1); } } } @@ -3915,15 +3919,53 @@ hk_g11_outputs(struct hk_cmd_buffer *cmd) : 64; } +/* 32x32 tiles a draw can touch: its viewports and scissors, in the render */ +static uint64_t +hk_g11_tiles(struct hk_cmd_buffer *cmd) +{ + const struct vk_dynamic_graphics_state *dyn = + &cmd->vk.dynamic_graphics_state; + const struct hk_render_registers *cr = &cmd->state.gfx.render.cr; + float minx = cr->width, miny = cr->height, maxx = 0, maxy = 0; + + for (unsigned i = 0; i < dyn->vp.viewport_count; i++) { + const VkViewport *vp = &dyn->vp.viewports[i]; + float x0 = MAX2(MIN2(vp->x, vp->x + vp->width), 0); + float y0 = MAX2(MIN2(vp->y, vp->y + vp->height), 0); + float x1 = MIN2(MAX2(vp->x, vp->x + vp->width), cr->width); + float y1 = MIN2(MAX2(vp->y, vp->y + vp->height), cr->height); + + if (i < dyn->vp.scissor_count) { + const VkRect2D *s = &dyn->vp.scissors[i]; + + x0 = MAX2(x0, s->offset.x); + y0 = MAX2(y0, s->offset.y); + x1 = MIN2(x1, (float)s->offset.x + s->extent.width); + y1 = MIN2(y1, (float)s->offset.y + s->extent.height); + } + if (x1 > x0 && y1 > y0) { + minx = MIN2(minx, x0); + miny = MIN2(miny, y0); + maxx = MAX2(maxx, x1); + maxy = MAX2(maxy, y1); + } + } + if (maxx <= minx || maxy <= miny) + return 0; + return (uint64_t)(DIV_ROUND_UP((unsigned)ceilf(maxx), 32) - (unsigned)minx / 32) * + (DIV_ROUND_UP((unsigned)ceilf(maxy), 32) - (unsigned)miny / 32); +} + /* 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)); + (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))) + + AGX_G11_HEAP_TILE_B * hk_g11_tiles(cmd); struct hk_cs *cs = cmd->current_cs.gfx; if (cs && cs->g11_heap_B && cs->g11_heap_B + cost > AGX_G11_HEAP_BUDGET) { diff --git a/src/asahi/vulkan/hk_cmd_meta.c b/src/asahi/vulkan/hk_cmd_meta.c index 039c19d3465..f3fcf6c729f 100644 --- a/src/asahi/vulkan/hk_cmd_meta.c +++ b/src/asahi/vulkan/hk_cmd_meta.c @@ -1125,6 +1125,18 @@ hk_meta_copy_image2(struct vk_command_buffer *cmd, struct vk_meta_device *meta, uint32_t blocksize_B = util_format_get_blocksize(hk_format_to_pipe_format(canonical)); + /* G11 multisampled depth and stencil: a texel per pixel holding + * all its samples, the layout ail gives them + */ + bool g11_wide = AGX_G11 && src_image->samples > 1; + if (g11_wide) { + blocksize_B *= src_image->samples; + canonical = blocksize_B == 2 ? VK_FORMAT_R8G8_UINT + : blocksize_B == 4 ? VK_FORMAT_R32_UINT + : blocksize_B == 8 ? VK_FORMAT_R32G32_UINT + : VK_FORMAT_R32G32B32A32_UINT; + } + bool use_dst_aspect_mask = vk_format_get_ycbcr_info(dst_image->format) || vk_format_get_ycbcr_info(src_image->format) || @@ -1139,17 +1151,20 @@ hk_meta_copy_image2(struct vk_command_buffer *cmd, struct vk_meta_device *meta, .key_type = VK_META_OBJECT_KEY_COPY_IMAGE_TO_BUFFER, .type = IMG2IMG, .block_size = blocksize_B, - .nr_samples = dst_image->samples, + .nr_samples = g11_wide ? 1 : dst_image->samples, .src_format = hk_format_to_pipe_format(canonical), - .dst_format = canonical_format_pipe(hk_format_to_pipe_format( - aspect_format(dst_image->format, dst_aspect_mask))), + .dst_format = + g11_wide ? hk_format_to_pipe_format(canonical) + : canonical_format_pipe(hk_format_to_pipe_format( + aspect_format(dst_image->format, + dst_aspect_mask))), /* TODO: MSAA path. G11's block store is not known. */ .block_based = (dst_image->image_type != VK_IMAGE_TYPE_1D) && dst_image->samples == 1 && !AGX_G11, }; - assert(key.nr_samples == src_image->samples); + assert(g11_wide || key.nr_samples == src_image->samples); VkPipelineLayout pipeline_layout; result = get_image_copy_pipeline_layout( @@ -1469,6 +1484,46 @@ hk_CmdCopyImage2(VkCommandBuffer commandBuffer, bool gfx = hk_copy_requires_gfx(dst_image); VkPipelineBindPoint bind_point = hk_bind_point(gfx); + /* G11 multisampled colour is linear with a layer per sample + * (hk_g11_ms_layers), so copy it a byte row at a time. + * + * ponytail: byte copies, vectorize if copies get hot + */ + if (hk_g11_ms_layers(src_image->vk.samples, src_image->vk.format) && + hk_g11_ms_layers(dst_image->vk.samples, dst_image->vk.format)) { + struct ail_layout *sl = &src_image->planes[0].layout; + struct ail_layout *dl = &dst_image->planes[0].layout; + unsigned n = src_image->vk.samples; + unsigned bpp_B = util_format_get_blocksize( + hk_format_to_pipe_format(src_image->vk.format)); + + for (unsigned r = 0; r < pCopyImageInfo->regionCount; ++r) { + const VkImageCopy2 *region = &pCopyImageInfo->pRegions[r]; + const VkImageSubresourceLayers *ss = ®ion->srcSubresource; + const VkImageSubresourceLayers *ds = ®ion->dstSubresource; + unsigned layers = vk_image_subresource_layer_count(&src_image->vk, ss); + + uint64_t src = hk_image_base_address(src_image, 0) + + ail_get_layer_offset_B(sl, ss->baseArrayLayer * n) + + region->srcOffset.y * ail_get_linear_stride_B(sl, 0) + + region->srcOffset.x * bpp_B; + uint64_t dst = hk_image_base_address(dst_image, 0) + + ail_get_layer_offset_B(dl, ds->baseArrayLayer * n) + + region->dstOffset.y * ail_get_linear_stride_B(dl, 0) + + region->dstOffset.x * bpp_B; + + libagx_copy_rows( + cmd, + agx_3d(region->extent.width * bpp_B, region->extent.height, + layers * n), + AGX_BARRIER_ALL, dst, src, ail_get_linear_stride_B(dl, 0), + ail_get_linear_stride_B(sl, 0), dl->layer_stride_B, + sl->layer_stride_B); + } + + return; + } + struct hk_meta_save save; hk_meta_begin(cmd, &save, bind_point); diff --git a/src/asahi/vulkan/hk_image.c b/src/asahi/vulkan/hk_image.c index 9bbc10ebcf4..22c22c2eaa0 100644 --- a/src/asahi/vulkan/hk_image.c +++ b/src/asahi/vulkan/hk_image.c @@ -451,6 +451,14 @@ hk_GetPhysicalDeviceImageFormatProperties2( return VK_ERROR_FORMAT_NOT_SUPPORTED; } + /* G11 has sparse binding only (hk_get_device_features). Reporting resident + * formats let apps make resident images, whose reads fault and wedge it. + */ + if (AGX_G11 && (pImageFormatInfo->flags & + (VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT | + VK_IMAGE_CREATE_SPARSE_ALIASED_BIT))) + return VK_ERROR_FORMAT_NOT_SUPPORTED; + const uint32_t max_dim = 16384; VkExtent3D maxExtent; uint32_t maxArraySize; @@ -479,7 +487,11 @@ hk_GetPhysicalDeviceImageFormatProperties2( maxMipLevels = 1; VkSampleCountFlags sampleCounts = VK_SAMPLE_COUNT_1_BIT; - if (!AGX_G11 && pImageFormatInfo->tiling == VK_IMAGE_TILING_OPTIMAL && + /* G11 keeps multisampled colour as a layer per sample (hk_g11_ms_layers), + * which shaders cannot store to + */ + if ((!AGX_G11 || !(pImageFormatInfo->usage & VK_IMAGE_USAGE_STORAGE_BIT)) && + 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)) && @@ -749,6 +761,9 @@ hk_map_tiling(struct hk_device *dev, const VkImageCreateInfo *info, /* 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 (hk_g11_ms_layers(info->samples, info->format)) + return AIL_TILING_LINEAR; + if (AGX_G11 && info->mipLevels == 1 && info->samples == 1 && info->imageType == VK_IMAGE_TYPE_2D && !(info->flags & (VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT | @@ -924,6 +939,7 @@ hk_image_init(struct hk_device *dev, struct hk_image *image, bool compressed = hk_map_compression(dev, pCreateInfo, plane, image->vk.drm_format_mod); + bool ms_layers = hk_g11_ms_layers(pCreateInfo->samples, format); image->planes[plane].layout = (struct ail_layout){ .tiling = hk_map_tiling(dev, pCreateInfo, image->vk.drm_format_mod), @@ -935,10 +951,11 @@ hk_image_init(struct hk_device *dev, struct hk_image *image, .width_px = pCreateInfo->extent.width / width_scale, .height_px = pCreateInfo->extent.height / height_scale, - .depth_px = MAX2(pCreateInfo->extent.depth, pCreateInfo->arrayLayers), + .depth_px = MAX2(pCreateInfo->extent.depth, pCreateInfo->arrayLayers) * + (ms_layers ? pCreateInfo->samples : 1), .levels = pCreateInfo->mipLevels, - .sample_count_sa = pCreateInfo->samples, + .sample_count_sa = ms_layers ? 1 : pCreateInfo->samples, .writeable_image = !compressed, /* TODO: Maybe optimize this, our GL driver doesn't bother though */ @@ -1031,12 +1048,6 @@ 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.h b/src/asahi/vulkan/hk_image.h index a28add8b990..eb06b656e47 100644 --- a/src/asahi/vulkan/hk_image.h +++ b/src/asahi/vulkan/hk_image.h @@ -7,6 +7,7 @@ #pragma once +#include "asahi/genxml/agx_g11.h" #include "asahi/layout/layout.h" #include "vulkan/vulkan_core.h" @@ -79,6 +80,17 @@ struct hk_image { VK_DEFINE_NONDISP_HANDLE_CASTS(hk_image, vk.base, VkImage, VK_OBJECT_TYPE_IMAGE) +/* + * G11 multisampled colour, as in GL (agx_g11_ms_layers): a linear 2D array + * of single-sampled layers, layer * samples + sample. Shaders read it as an + * array and write it as one tall image. Depth and stencil keep ZLS's layout. + */ +static inline bool +hk_g11_ms_layers(unsigned samples, VkFormat format) +{ + return AGX_G11 && samples > 1 && !vk_format_is_depth_or_stencil(format); +} + static inline uint64_t hk_image_plane_base_address(const struct hk_image_plane *plane) { diff --git a/src/asahi/vulkan/hk_image_view.c b/src/asahi/vulkan/hk_image_view.c index 3e2eb7762a2..299c31ae905 100644 --- a/src/asahi/vulkan/hk_image_view.c +++ b/src/asahi/vulkan/hk_image_view.c @@ -143,6 +143,12 @@ get_stencil_format(enum pipe_format format) return PIPE_FORMAT_S8X24_UINT; case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT: return PIPE_FORMAT_X32_S8X24_UINT; + /* G11 multisampled copies view all samples as one texel */ + case PIPE_FORMAT_R8G8_UINT: + case PIPE_FORMAT_R32_UINT: + if (AGX_G11) + return format; + FALLTHROUGH; default: UNREACHABLE("Unsupported depth/stencil format"); } @@ -288,6 +294,29 @@ pack_texture(struct hk_image_view *view, unsigned view_plane, if (g11_2d) layers = 1; + /* G11 multisampling, see hk_g11_ms_layers. Colour is an array of a layer + * per sample. Depth and stencil keep ZLS's layout and read as a format + * with a channel per sample; the shader picks the channel. + */ + unsigned samples = view->vk.image->samples; + bool g11_ms = AGX_G11 && samples > 1; + if (g11_ms && util_format_is_depth_or_stencil(p_format)) { + 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}, + }; + unsigned i = p_format == PIPE_FORMAT_Z16_UNORM ? 1 + : util_format_is_pure_uint(p_format) || + p_format == PIPE_FORMAT_S8_UINT + ? 2 + : 0; + p_format = wide[i][samples == 4]; + desc = util_format_description(p_format); + for (unsigned c = 0; c < 4; ++c) + out_swizzle[c] = PIPE_SWIZZLE_X + c; + } + agx_pack(out, TEXTURE, cfg) { cfg.dimension = translate_image_view_type( view->vk.view_type, view->vk.image->samples > 1, layers > 1, usage); @@ -355,7 +384,7 @@ pack_texture(struct hk_image_view *view, unsigned view_plane, } cfg.srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB); - cfg.unk_mipmapped = layout->levels > 1; + cfg.unk_mipmapped = layout->levels > 1 && !(AGX_G11 && denom.x > 1); cfg.srgb_2_channel = cfg.srgb && util_format_colormask(desc) == 0x3; if (layout->compressed) { @@ -379,7 +408,7 @@ pack_texture(struct hk_image_view *view, unsigned view_plane, cfg.depth = layers; } - if (view->vk.image->samples > 1) { + if (view->vk.image->samples > 1 && !g11_ms) { cfg.samples = agx_translate_sample_count(view->vk.image->samples); } @@ -388,6 +417,17 @@ pack_texture(struct hk_image_view *view, unsigned view_plane, } else { cfg.page_aligned_layers = layout->page_aligned_layers; } + + if (g11_ms) + cfg.dimension = AGX_TEXTURE_DIMENSION_2D_ARRAY; + + if (g11_ms && layout->tiling == AIL_TILING_LINEAR) { + cfg.address = base_addr + ail_get_layer_offset_B(layout, layer * samples); + cfg.depth_linear = (g11_2d ? 1 : view->vk.layer_count) * samples; + cfg.layer_stride_linear = layout->layer_stride_B - 0x80; + cfg.extended = true; + cfg.g11_samples_log2_sw = util_logbase2(samples); + } } } @@ -416,7 +456,10 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, if (eot) p_format = util_format_linear(p_format); - bool msaa = view->vk.image->samples > 1; + /* G11 multisampled depth and stencil are written as a single-sampled + * image of a texel per pixel, all samples in it (hk_meta_copy_image2) + */ + bool msaa = view->vk.image->samples > 1 && !AGX_G11; struct hk_3d denom = view_denominator(view); unsigned layers = view->vk.view_type == VK_IMAGE_VIEW_TYPE_3D @@ -428,6 +471,41 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, if (g11_2d) layers = 1; + /* G11 multisampled colour: the sample layers as one tall 2D image, see + * hk_g11_ms_layers and agx_nir_lower_tilebuffer's store_memory. + * + * ponytail: height is 14 bits, so 4x tops out at 4096 rows + */ + if (hk_g11_ms_layers(image->vk.samples, image->vk.format)) { + 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_offset_B(layout, layer * image->vk.samples); + cfg.width = layout->width_px; + cfg.height = layout->height_px * image->vk.samples; + cfg.stride = ail_get_linear_stride_B(layout, 0) - AGX_PBE_STRIDE_BIAS; + cfg.layers = 1; + cfg.levels = 1; + } + return; + } + agx_pack(out, PBE, cfg) { cfg.dimension = translate_image_view_type(view->vk.view_type, msaa, layers > 1, usage); @@ -505,7 +583,8 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, cfg.layers = layers; } - cfg.levels = image->vk.mip_levels; + /* A G11 uncompressed view of a compressed level is one image */ + cfg.levels = (AGX_G11 && denom.x > 1) ? 1 : image->vk.mip_levels; if (layout->tiling == AIL_TILING_LINEAR) { cfg.stride = @@ -515,7 +594,7 @@ pack_pbe(struct hk_device *dev, struct hk_image_view *view, unsigned view_plane, cfg.page_aligned_layers = layout->page_aligned_layers; } - if (image->vk.samples > 1) + if (msaa) cfg.samples = agx_translate_sample_count(image->vk.samples); /* G11 image writes take level 0, so point at the level itself */ @@ -564,6 +643,7 @@ 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. */ diff --git a/src/asahi/vulkan/hk_physical_device.c b/src/asahi/vulkan/hk_physical_device.c index 2c7c4f8d4f3..e3b9b0d77fd 100644 --- a/src/asahi/vulkan/hk_physical_device.c +++ b/src/asahi/vulkan/hk_physical_device.c @@ -255,7 +255,7 @@ hk_get_device_features( */ .geometryShader = !AGX_G11, .tessellationShader = !AGX_G11, - .sampleRateShading = true, + .sampleRateShading = !AGX_G11, .dualSrcBlend = !AGX_G11, .logicOp = true, .multiDrawIndirect = true, @@ -687,11 +687,8 @@ 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 = - AGX_G11 ? VK_SAMPLE_COUNT_1_BIT - : VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_2_BIT | - VK_SAMPLE_COUNT_4_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); @@ -813,7 +810,7 @@ hk_get_device_properties(const struct agx_device *dev, .sampledImageIntegerSampleCounts = sample_counts, .sampledImageDepthSampleCounts = sample_counts, .sampledImageStencilSampleCounts = sample_counts, - .storageImageSampleCounts = sample_counts, + .storageImageSampleCounts = AGX_G11 ? VK_SAMPLE_COUNT_1_BIT : sample_counts, .maxSampleMaskWords = 1, .timestampComputeAndGraphics = true, /* FIXME: Is timestamp period actually 1? */ @@ -887,7 +884,10 @@ hk_get_device_properties(const struct agx_device *dev, .shaderSignedZeroInfNanPreserveFloat16 = true, .shaderSignedZeroInfNanPreserveFloat32 = true, .shaderSignedZeroInfNanPreserveFloat64 = false, - .shaderDenormPreserveFloat16 = true, + /* G11 flushes half denormals in arithmetic but not in min, max and + * clamp, so it can promise neither + */ + .shaderDenormPreserveFloat16 = !AGX_G11, .shaderDenormPreserveFloat32 = false, .shaderDenormPreserveFloat64 = false, .shaderDenormFlushToZeroFloat16 = false, diff --git a/src/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index 074ea793e1a..3da975fd83a 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -433,6 +433,23 @@ lower_load_global_bounded(nir_builder *b, nir_intrinsic_instr *intr) access |= ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER; } + /* G11 turns coherent loads into atomics (agx_nir_lower_address.c), which + * must not write the read-only zero page: out of bounds reads 0 instead. + */ + if (AGX_G11 && (access & ACCESS_COHERENT)) { + nir_def *zero = nir_imm_zero(b, intr->def.num_components, + intr->def.bit_size); + nir_push_if(b, valid); + nir_def *in = nir_load_global( + b, intr->def.num_components, intr->def.bit_size, + nir_iadd(b, intr->src[0].ssa, nir_u2u64(b, intr->src[1].ssa)), + .align_mul = nir_intrinsic_align_mul(intr), + .align_offset = nir_intrinsic_align_offset(intr), .access = access); + nir_pop_if(b, NULL); + nir_def_replace(&intr->def, nir_if_phi(b, in, zero)); + return; + } + nir_def *val = nir_load_global( b, intr->def.num_components, intr->def.bit_size, nir_iadd(b, base, nir_u2u64(b, offset)), @@ -769,6 +786,55 @@ 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)); } +/* + * The compiler computes G11 LOD queries from derivatives (agx_nir_lower_texture) + * and cannot see the sampler. Clamp the level it returns to the sampler's + * minLod and maxLod, held as u4.6 in bits 9:0 and 19:10 of the sampler word. + */ +static bool +g11_clamp_lod_query(nir_builder *b, nir_tex_instr *tex, UNUSED void *data) +{ + if (tex->op != nir_texop_lod) + return false; + + b->cursor = nir_after_instr(&tex->instr); + nir_def *w = nir_channel( + b, + nir_build_texture_query(b, tex, nir_texop_sampler_descriptor_amd, 4, + nir_type_uint32, false, false), + 0); + nir_def *min_lod = nir_fmul_imm(b, nir_u2f32(b, nir_iand_imm(b, w, 0x3ff)), + 1.0 / 64); + nir_def *max_lod = nir_fmul_imm( + b, nir_u2f32(b, nir_iand_imm(b, nir_ushr_imm(b, w, 10), 0x3ff)), 1.0 / 64); + nir_def *last = nir_fadd_imm( + b, + nir_i2f32(b, nir_build_texture_query(b, tex, nir_texop_query_levels, 1, + nir_type_int32, false, false)), + -1.0); + + nir_def *res = nir_f2f32(b, &tex->def); + nir_def *level = nir_fmin( + b, nir_fmax(b, nir_fmin(b, nir_channel(b, res, 0), max_lod), min_lod), + last); + + /* The mip filter (bits 28:27) picks the level: none gives 0, nearest + * rounds as ceil(level + 0.5) - 1. Null descriptors query 0. + */ + nir_def *mip = nir_iand_imm(b, nir_ushr_imm(b, w, 27), 3); + nir_def *rounded = nir_fmax( + b, nir_fadd_imm(b, nir_fceil(b, nir_fadd_imm(b, level, 0.5)), -1.0), + nir_imm_float(b, 0.0)); + level = nir_bcsel(b, nir_ieq_imm(b, mip, 1), rounded, level); + level = nir_bcsel(b, nir_ior(b, nir_ieq_imm(b, mip, 0), + nir_flt_imm(b, last, 0.0)), + nir_imm_float(b, 0.0), level); + nir_def *out = nir_f2fN(b, nir_vector_insert_imm(b, res, level, 0), + tex->def.bit_size); + nir_def_rewrite_uses_after(&tex->def, out); + return true; +} + static bool g11_border_sample(const nir_tex_instr *tex) { @@ -1287,6 +1353,10 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, // NIR_PASS(_, nir, nir_opt_large_constants, NULL, 32); + /* Before nir_lower_memory_model drops the texel availability barriers */ + if (AGX_G11) + NIR_PASS(_, nir, agx_nir_g11_coherent_image_atomics); + /* Turn cache flushes into image coherency bits while we still have derefs */ NIR_PASS(_, nir, nir_lower_memory_model); @@ -1309,6 +1379,9 @@ hk_lower_nir(struct hk_device *dev, nir_shader *nir, } #if AGX_G11 + NIR_PASS(_, nir, nir_shader_tex_pass, g11_clamp_lod_query, + nir_metadata_control_flow, NULL); + if (g11_border_samples(nir) <= 32) NIR_PASS(_, nir, nir_shader_tex_pass, g11_lower_border, nir_metadata_none, NULL); @@ -1490,17 +1563,24 @@ hk_g11_slot(struct hk_g11_state *tab, uint8_t *n, unsigned max, return (*n)++; } +struct hk_g11_handles { + struct hk_shader_info *info; + bool stores; +}; + /* * 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. + * reads such as size queries; G11 samples from the slot. Reads go first, in + * states 0-15, and image stores take the slots after them. */ static bool hk_g11_lower_handles(nir_builder *b, nir_instr *instr, void *data) { - struct hk_shader_info *info = data; + struct hk_g11_handles *ctx = data; + struct hk_shader_info *info = ctx->info; - if (instr->type == nir_instr_type_tex) { + if (instr->type == nir_instr_type_tex && !ctx->stores) { 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; @@ -1564,6 +1644,9 @@ hk_g11_lower_handles(nir_builder *b, nir_instr *instr, void *data) return false; bool store = intr->intrinsic == nir_intrinsic_bindless_image_store; + if (store != ctx->stores) + return false; + int slot = hk_g11_slot(info->g11_tex, &info->g11_nr_tex, store ? HK_G11_MAX_TEXTURES : HK_G11_MAX_READ_TEXTURES, h); @@ -1749,8 +1832,11 @@ hk_compile_nir(struct hk_device *dev, const VkAllocationCallbacks *pAllocator, /* 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); + for (unsigned stores = 0; stores < 2; ++stores) { + struct hk_g11_handles ctx = {.info = &shader->info, .stores = stores}; + NIR_PASS(_, nir, nir_shader_instructions_pass, hk_g11_lower_handles, + nir_metadata_control_flow, &ctx); + } /* Spilled render targets follow the shader's textures */ if (sw_stage == MESA_SHADER_FRAGMENT) diff --git a/src/gallium/drivers/asahi/agx_pipe.c b/src/gallium/drivers/asahi/agx_pipe.c index fec4387cf4a..52dfe3cdacb 100644 --- a/src/gallium/drivers/asahi/agx_pipe.c +++ b/src/gallium/drivers/asahi/agx_pipe.c @@ -621,6 +621,25 @@ agx_resource_create_with_modifiers(struct pipe_screen *screen, if (bind & (PIPE_BIND_SCANOUT | PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SHARED)) create_flags |= AGX_BO_SHAREABLE; + /* GPU memory is pinned and shared with the CPU. On a 4 GB phone a 16384^2 + * WebGL canvas (1 GB colour, 1.3 GB depth/stencil) swapped everything out + * and got processes killed. Failing instead lets Firefox retry smaller. + */ + uint64_t ram; + if (os_get_total_physical_memory(&ram) && nresource->layout.size_B > ram / 4) { + FREE(nresource); + return NULL; + } + + /* G11 renders over 8192x4096 pixels wedge the firmware (the kernel rejects + * them), so do not make targets that big. Callers retry smaller. + */ + if (AGX_G11 && (bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) && + (uint64_t)templ->width0 * templ->height0 > 8192 * 4096) { + FREE(nresource); + return NULL; + } + nresource->bo = agx_bo_create(dev, nresource->layout.size_B, 0, create_flags, label); @@ -2163,8 +2182,12 @@ agx_init_screen_caps(struct pipe_screen *pscreen) caps->query_pipeline_statistics_single = true; - caps->max_texture_2d_size = 16384; - caps->max_texture_cube_levels = 15; /* Max 16384x16384 */ + /* G11 renders to 16384 wide wedge the firmware (8192x4096 works), and GL + * ties render and viewport size to this. A 16384^2 RGBA texture is 1 GB + * on a 4 GB phone anyway. + */ + caps->max_texture_2d_size = AGX_G11 ? 8192 : 16384; + caps->max_texture_cube_levels = AGX_G11 ? 14 : 15; caps->max_texture_3d_levels = 12; /* Max 2048x2048x2048 */ caps->fs_coord_origin_upper_left = true; diff --git a/src/gallium/drivers/asahi/agx_state.c b/src/gallium/drivers/asahi/agx_state.c index a45af40c2c7..40f759686d6 100644 --- a/src/gallium/drivers/asahi/agx_state.c +++ b/src/gallium/drivers/asahi/agx_state.c @@ -787,6 +787,9 @@ agx_pack_texture(void *out, struct agx_resource *rsrc, if (rsrc->base.nr_samples > 1 && !ms_layers) cfg.samples = agx_translate_sample_count(rsrc->base.nr_samples); + if (agx_g11_ms_layers(&rsrc->base)) + cfg.g11_samples_log2_sw = util_logbase2(rsrc->base.nr_samples); + if (state->target == PIPE_BUFFER) { cfg.stride = (cfg.width * util_format_get_blocksize(format)) - 16; } else if (rsrc->layout.tiling == AIL_TILING_LINEAR) { @@ -2000,6 +2003,7 @@ agx_shader_initialize(struct agx_device *dev, struct agx_uncompiled_shader *so, .callback = agx_mem_vectorize_cb, }); + so->g11_samplers_from_0 = agx_g11_samplers_from_0(nir); NIR_PASS(_, nir, agx_nir_lower_texture); NIR_PASS(_, nir, nir_lower_ssbo, NULL); @@ -3049,19 +3053,24 @@ agx_upload_samplers(struct agx_batch *batch, struct agx_compiled_shader *cs, struct agx_ptr T = agx_pool_alloc_aligned(&batch->pool, sampler_length * nr_samplers, 64); - /* Sampler #0 is reserved for txf */ + /* Sampler #0 is reserved for txf, unless a G11 shader needs all 16 */ + bool from_0 = cs && cs->so && cs->so->g11_samplers_from_0; + unsigned base = from_0 ? 0 : 1; memset(T.cpu, 0, sampler_length * nr_samplers); - agx_pack_txf_sampler(T.cpu); + if (!from_0) + 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) { + if (AGX_G11 && !from_0 && + 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) { + uint8_t *out_sampler = (uint8_t *)T.cpu + sampler_length * base; + unsigned count = MIN2(ctx->stage[stage].sampler_count, nr_samplers - base); + for (unsigned i = 0; i < count; ++i) { struct agx_sampler_state *sampler = ctx->stage[stage].samplers[i]; struct agx_sampler_packed *out = (struct agx_sampler_packed *)out_sampler; @@ -5518,9 +5527,14 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, } if (copied && !batch->g11_tib) { - if (batch->initialized) { + if (batch->draw) { agx_flush_batch_for_reason(ctx, batch, "G11 tilebuffer copy"); batch = agx_get_batch(ctx); + } else if (batch->initialized) { + /* Only fast clears so far, which the background program also + * writes to the copy. GTK clears then blends, every pass. + */ + batch->tilebuffer_layout = tib; } batch->g11_tib = true; @@ -5540,6 +5554,16 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, : agx_g11_heap_cost((uint64_t)draws[0].count * MAX2(info->instance_count, 1), g11_outputs); + unsigned minx, miny, maxx, maxy; + + agx_get_scissor_extents(&ctx->viewport[0], + ctx->rast->base.scissor ? &ctx->scissor[0] : NULL, + &batch->key, &minx, &miny, &maxx, &maxy); + if (maxx > minx && maxy > miny) { + cost += (uint64_t)AGX_G11_HEAP_TILE_B * + (DIV_ROUND_UP(maxx, 32) - minx / 32) * + (DIV_ROUND_UP(maxy, 32) - miny / 32); + } if (batch->g11_heap_B && batch->g11_heap_B + cost > AGX_G11_HEAP_BUDGET) { agx_flush_batch_for_reason(ctx, batch, "G11 tiler heap"); @@ -5671,6 +5695,19 @@ agx_draw_vbo(struct pipe_context *pctx, const struct pipe_draw_info *info, ctx->dirty |= AGX_DIRTY_FS; } + /* G11 samples and image writes reach texture states 0 to 15. Its render + * targets are always spilled, so their descriptors follow the textures, and + * a draw needing more states reads or writes through the wrong descriptor + * and wedges the GPU. Drop it: 16 textures and a render target do not fit. + */ + if (AGX_G11 && agx_nr_tex_descriptors(batch, ctx->fs) > 16) { + static bool warned; + if (!warned) + agx_msg("G11: dropping draws needing over 16 texture states\n"); + warned = true; + return; + } + /* This is subtle. But agx_update_fs will be true at least once per batch. */ assert(!ctx->fs->bo || agx_batch_uses_bo(batch, ctx->fs->bo)); assert(agx_batch_uses_bo(batch, ctx->linked.fs->bo)); diff --git a/src/gallium/drivers/asahi/agx_state.h b/src/gallium/drivers/asahi/agx_state.h index b9f8844d6a7..a7d0bad0888 100644 --- a/src/gallium/drivers/asahi/agx_state.h +++ b/src/gallium/drivers/asahi/agx_state.h @@ -313,6 +313,9 @@ struct agx_uncompiled_shader { /* Whether the shader accesses indexed samplers via the bindless heap */ bool uses_bindless_samplers; + /* G11: API samplers in slots 0 to 15, no txf sampler (agx_g11_samplers_from_0) */ + bool g11_samplers_from_0; + /* Set on VS, passed to FS for linkage */ unsigned base_varying;