From 3c323a553f45ab13f6d2ef6831dfff0987bec7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Tue, 24 Nov 2026 12:38:21 +0100 Subject: [PATCH 1/6] pvr/g5p: the A8's float ge select as lt with the values swapped A float ge select was the lt compare/write with the ge relation extension (`.. 59 61 00 10`) and an inverse move. No A8 capture from Apple's compiler has that extension, and the A8 ignores it: on the iPod touch 6 a fragment select of a >= b (a varying against a constant, and step(0.0, gl_FragCoord.x)) gave the lt result at every pixel, while the plain lt select next to it in the same program was right. On the A8, compare lt and swap the two selected values, as the integer ge select already does, in both stages. With this, every ge case of a 33-case set of small GLES programs (varying, uniform and gl_FragCoord operands, mix() of a boolean, step() in the vertex stage) reads back right on the iPod touch 6; the one remaining failure reads gl_FragCoord.z/w (next commit). An unordered compare now selects the true value, where ge selects the false one. PVR_A8_FGE_SWAP=0 gives the extension form back. Generated-by: LLM --- src/imagination/g5p/g5p_usc.c | 1 + src/imagination/g5p/g5p_usc.h | 4 ++++ src/imagination/g5p/pco_g5p_general.c | 12 +++++++++--- src/imagination/vulkan/pvr_physical_device.c | 2 ++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/imagination/g5p/g5p_usc.c b/src/imagination/g5p/g5p_usc.c index a0db8b64121..eee08622465 100644 --- a/src/imagination/g5p/g5p_usc.c +++ b/src/imagination/g5p/g5p_usc.c @@ -16,6 +16,7 @@ int g5p_usc_g4p_texfetch_level; bool g5p_usc_g4p_vtex; bool g5p_usc_g4p_vtex_apple; bool g5p_usc_g4p_tbo; +bool g5p_usc_g4p_fge_swap; const char *const g5p_binop_name[G5P_BIN_COUNT] = {"fadd", "fmul", "iadd", "imul", "and", "or", "xor", "ishr", "isub"}; diff --git a/src/imagination/g5p/g5p_usc.h b/src/imagination/g5p/g5p_usc.h index 9ee631fa754..2813eae566d 100644 --- a/src/imagination/g5p/g5p_usc.h +++ b/src/imagination/g5p/g5p_usc.h @@ -71,6 +71,10 @@ extern bool g5p_usc_g4p_vtex_apple; /* A fragment texel-buffer read on the A8 (st/mesa's pixel-buffer uploads) as a 2D texelFetch of the buffer's linear * state (PVR_A8_TBO, on by default; 0 refuses it) */ extern bool g5p_usc_g4p_tbo; +/* The A8's float ge select as the lt compare/write with the selected values swapped, without the ge relation + * extension (`00 10` after `59 61`), which no A8 capture has and which the A8 ignores: a float ge select through it + * runs as lt on the iPod touch 6. PVR_A8_FGE_SWAP, on by default; 0 gives the extension form. */ +extern bool g5p_usc_g4p_fge_swap; enum g5p_binop { G5P_BIN_FADD, G5P_BIN_FMUL, G5P_BIN_IADD, G5P_BIN_IMUL, diff --git a/src/imagination/g5p/pco_g5p_general.c b/src/imagination/g5p/pco_g5p_general.c index 2678837f621..4f99e798a39 100644 --- a/src/imagination/g5p/pco_g5p_general.c +++ b/src/imagination/g5p/pco_g5p_general.c @@ -3044,7 +3044,9 @@ struct gsrc4 { unsigned a, b, t, f; }; * false value first. */ static bool cmp_swaps(nir_op op) { - return op == nir_op_fneu || op == nir_op_ige || op == nir_op_ine || op == nir_op_uge; + /* the A8's float ge is the lt compare with the values swapped, as the integer ge (PVR_A8_FGE_SWAP) */ + return op == nir_op_fneu || op == nir_op_ige || op == nir_op_ine || op == nir_op_uge || + (op == nir_op_fge && g5p_usc_g4p_fge_swap); } static void cmp_select_regs(struct gctx *c, nir_op op, unsigned a, unsigned b, unsigned t, unsigned f, unsigned dst, @@ -3064,8 +3066,12 @@ static void cmp_select_regs(struct gctx *c, nir_op op, unsigned a, unsigned b, u case nir_op_ult: code = G5P_ULT; break; default: code = G5P_ULT; ge = true; break; /* uge */ } - unsigned rc = fl && ge ? 0x10 : 0; - bool swap = fl ? ne : (ge || ne); + /* The ge relation extension (`.. 59 61 00 10`) has no A8 capture, and the A8 ignores it: a float ge select + * through it gives lt's value. On the A8 the lt compare with the values swapped (PVR_A8_FGE_SWAP); an unordered + * compare then selects the true value, where ge selects the false one. */ + const bool ge_swap = fl && ge && g5p_usc_g4p_fge_swap; + unsigned rc = fl && ge && !ge_swap ? 0x10 : 0; + bool swap = fl ? (ne || ge_swap) : (ge || ne); if (swap) { unsigned tmp = t; t = f; diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index c31d049dc33..dd3b25ea094 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1423,6 +1423,8 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, g5p_usc_g4p_vtex = debug_get_bool_option("PVR_A8_VTEX", false) || g5p_usc_g4p_vtex_apple; /* fragment texel-buffer reads (g5p_usc.h) */ g5p_usc_g4p_tbo = debug_get_bool_option("PVR_A8_TBO", true); + /* float ge selects without the ge relation extension (g5p_usc.h) */ + g5p_usc_g4p_fge_swap = debug_get_bool_option("PVR_A8_FGE_SWAP", true); } /* The A8 skips refused work by default: a refused program or pass loses its draws (named on stderr) instead of From 80aea79a2bade906292f5b7f3e73605aaf404428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Tue, 24 Nov 2026 12:38:45 +0100 Subject: [PATCH 2/6] pvr/g5p: the A8's gl_FragCoord.z/w by its varying iterator, Apple's form gl_FragCoord.z and .w were read with the A9's position iterator (`08 09 ..`). No A8 capture has it, and on the iPod touch 6 it reads 0: gl_FragCoord.w, gl_FragCoord.z, and WebRender's clipped text (do_clip(), uv times gl_FragCoord.w) all came out 0. Apple's A8 compiler (iOS 12.4, Metal fragment functions returning position.w, position.z, both, and a perspective varying times position.w) reads them with the ordinary A8 varying iterator, screen linear (no perspective bit), from the same W/Z coefficient sets. Each captured program drew the expected value on the A8. The PDS DOUTI items and TA_OUTPUT_SEL in those captures are the words we already emit, so only the USC instruction changes: set 0 (W, or Z alone): 18 09 DD 00 Apple's W read, byte for byte set 1 (Z after W): 18 09 DD 01 04 00 inferred The set-1 f32 form is not captured: Apple reads Z and W there as one f16 pair (`18 11 ..`). Ours is the f32 screen-linear shape that Apple's varying reads at other sets take (byte 3 bit 0 announcing the slot byte set << 2). Both components are kept in f32. A read of both sets in one instruction is refused by name on the A8; nothing emits it. The captured set-0 form has not yet been run under this driver on the device; the device check is the same programs that read 0 before. PVR_A8_POS_ITER=0 gives the A9 form back. Generated-by: LLM --- src/imagination/g5p/g5p_usc.c | 12 ++++++++++++ src/imagination/g5p/g5p_usc.h | 4 ++++ src/imagination/vulkan/pvr_physical_device.c | 2 ++ 3 files changed, 18 insertions(+) diff --git a/src/imagination/g5p/g5p_usc.c b/src/imagination/g5p/g5p_usc.c index eee08622465..d6f87b0ba3a 100644 --- a/src/imagination/g5p/g5p_usc.c +++ b/src/imagination/g5p/g5p_usc.c @@ -17,6 +17,7 @@ bool g5p_usc_g4p_vtex; bool g5p_usc_g4p_vtex_apple; bool g5p_usc_g4p_tbo; bool g5p_usc_g4p_fge_swap; +bool g5p_usc_g4p_pos_iter; const char *const g5p_binop_name[G5P_BIN_COUNT] = {"fadd", "fmul", "iadd", "imul", "and", "or", "xor", "ishr", "isub"}; @@ -1976,6 +1977,17 @@ unsigned g5p_usc_position_zw(uint8_t *out, unsigned dst_half, bool set1, bool bo bool minimum = dst_half || set1 || both; bool wide = long_form < 0 ? minimum : long_form; CHECK(!(minimum && !wide), "short position iterator omits nonzero fields"); + if (g5p_usc_g4p && g5p_usc_g4p_pos_iter) { + /* The A8: Apple's A8 compiler reads the position's W and Z with its varying iterator `18 ..`, screen-linear + * (byte 3 without 0x80), one f32 component. Set 0 is `18 09 DD 00` with no slot byte, as Apple reads W next + * to a perspective varying. Set 1 (Z after W) is inferred: the slot byte set << 2 after byte 3 = 0x01, the + * f32 screen-linear shape of Apple's varying reads at other sets (Apple's own set-1 position read is the f16 + * pair `18 11 02 01 00 01 04 00`). The coefficient side (DOUTI items, TA_OUTPUT_SEL with unclamped Z) is the + * A9's, the same words as Apple's A8 output. */ + CHECK(!both, "A8 position iterator: both sets in one read not captured in f32"); + const uint8_t g4[6] = {0x18, 0x09, (uint8_t)dst_half, set1 ? 0x01 : 0x00, 0x04, 0x00}; + return finish_g4p(out, g4, set1 ? 6 : 4); + } uint8_t raw[4] = {8, (both ? 0x10 : 8) | wide, dst_half, set1 ? 2 : 0}; return finish(out, raw, wide ? 4 : 2); } diff --git a/src/imagination/g5p/g5p_usc.h b/src/imagination/g5p/g5p_usc.h index 2813eae566d..9611752d6af 100644 --- a/src/imagination/g5p/g5p_usc.h +++ b/src/imagination/g5p/g5p_usc.h @@ -75,6 +75,10 @@ extern bool g5p_usc_g4p_tbo; * extension (`00 10` after `59 61`), which no A8 capture has and which the A8 ignores: a float ge select through it * runs as lt on the iPod touch 6. PVR_A8_FGE_SWAP, on by default; 0 gives the extension form. */ extern bool g5p_usc_g4p_fge_swap; +/* The A8's gl_FragCoord.z/.w read in Apple's form: the varying iterator `18 09 ..` without perspective on the W/Z + * coefficient sets (g5p_usc_position_zw()), in place of the A9's position iterator `08 09 ..`, which no A8 capture + * has and which reads 0 on the iPod touch 6. PVR_A8_POS_ITER, on by default; 0 gives the A9 form. */ +extern bool g5p_usc_g4p_pos_iter; enum g5p_binop { G5P_BIN_FADD, G5P_BIN_FMUL, G5P_BIN_IADD, G5P_BIN_IMUL, diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index dd3b25ea094..ce51cc9425d 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1425,6 +1425,8 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, g5p_usc_g4p_tbo = debug_get_bool_option("PVR_A8_TBO", true); /* float ge selects without the ge relation extension (g5p_usc.h) */ g5p_usc_g4p_fge_swap = debug_get_bool_option("PVR_A8_FGE_SWAP", true); + /* gl_FragCoord.z/w by the A8's varying iterator (g5p_usc.h) */ + g5p_usc_g4p_pos_iter = debug_get_bool_option("PVR_A8_POS_ITER", true); } /* The A8 skips refused work by default: a refused program or pass loses its draws (named on stderr) instead of From 81a923a50ac3dc1ff015c59128203d171dea009e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 23 Nov 2026 12:48:23 +0100 Subject: [PATCH 3/6] pvr/g5p: the A8's LOAD of an R8 colour target in Apple's form Apple's load program for an R8Unorm target with loadAction Load (iOS 12.4, AGXMetalA8) is the BGRA8 load program up to the wait after its sample, then a zero into half 1 (88 01 01 01 01 01 20 01 28 01 39 00) and the one-word emitpix (1c 11 00 09 00 82) and END, as R8's clear and fragment programs end. Its background data is the BGRA8 load's: the target's texture state, the read sampler 00000fff 02022400 and the identity matrix, by the same three DOUTDs (10000004 10008004 90010006), common store size 14. The end-of-tile word (0x002f0002) and PBE swizzle (0x968) are R8's store ones. The program is uploaded on first use, so the device's other allocations keep their addresses. A linear R8 target and RG8 stay refused. PVR_A8_R8_LOAD=0 refuses the R8 LOAD as before. Generated-by: LLM --- src/imagination/g5p/g5p_mrt_programs.h | 22 ++++++++++ src/imagination/g5p/g5p_usc.c | 1 + src/imagination/g5p/g5p_usc.h | 3 ++ src/imagination/vulkan/pvr_arch_g5p.c | 44 ++++++++++++++++---- src/imagination/vulkan/pvr_g5p.h | 3 ++ src/imagination/vulkan/pvr_physical_device.c | 2 + 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/imagination/g5p/g5p_mrt_programs.h b/src/imagination/g5p/g5p_mrt_programs.h index 79ebeae50b0..c328dfd0c28 100644 --- a/src/imagination/g5p/g5p_mrt_programs.h +++ b/src/imagination/g5p/g5p_mrt_programs.h @@ -225,6 +225,28 @@ static inline unsigned g5p_mrt_load_usc_g4p(uint8_t *out) return sizeof(usc); } +/* The A8's LOAD background for one R8 target (AGXMetalA8, a8-passcap r8_load: an R8Unorm target with loadAction + * Load, USC heap 0x1fc0; the same bytes are in the r8 pass's heap at 0x2140): BGRA8's program up to the wait after the + * sample (its first 116 bytes), then, as R8's clear and fragment programs, a zero into half 1 + * (0188 0101 0101 0120 0128 0039) and the one-word emitpix 111c 0900 8200 and END. 136 bytes. The texture state, the + * sampler, the matrix and their three DOUTDs (10000004 10008004 90010006) are BGRA8's; only the state's format + * differs. */ +#define G5P_A8_LOAD_R8_USC_BYTES 136 +static inline unsigned g5p_mrt_load_usc_g4p_r8(uint8_t *out) +{ + static const uint8_t zero_g[12] = {0x88, 0x01, 0x01, 0x01, 0x01, 0x01, 0x20, 0x01, 0x28, 0x01, 0x39, 0x00}; + uint8_t bgra[G5P_A8_LOAD_USC_BYTES]; + g5p_mrt_load_usc_g4p(bgra); + unsigned o = G5P_A8_LOAD_USC_BYTES - 8; /* up to and with the wait (4d 00); BGRA8's two-word emitpix and END after */ + memcpy(out, bgra, o); + memcpy(out + o, zero_g, sizeof(zero_g)); + o += sizeof(zero_g); + const unsigned len = g5p_usc_emit(out + o, 1, 0); + if (!len) + return 0; + return o + len + g5p_usc_special(out + o + len, true); +} + static inline unsigned g5p_mrt_clear_usc(uint8_t *out, unsigned n) { if (n > 4) diff --git a/src/imagination/g5p/g5p_usc.c b/src/imagination/g5p/g5p_usc.c index d6f87b0ba3a..9826fe27d83 100644 --- a/src/imagination/g5p/g5p_usc.c +++ b/src/imagination/g5p/g5p_usc.c @@ -18,6 +18,7 @@ bool g5p_usc_g4p_vtex_apple; bool g5p_usc_g4p_tbo; bool g5p_usc_g4p_fge_swap; bool g5p_usc_g4p_pos_iter; +bool g5p_usc_g4p_r8_load; const char *const g5p_binop_name[G5P_BIN_COUNT] = {"fadd", "fmul", "iadd", "imul", "and", "or", "xor", "ishr", "isub"}; diff --git a/src/imagination/g5p/g5p_usc.h b/src/imagination/g5p/g5p_usc.h index 9611752d6af..b64cca88123 100644 --- a/src/imagination/g5p/g5p_usc.h +++ b/src/imagination/g5p/g5p_usc.h @@ -79,6 +79,9 @@ extern bool g5p_usc_g4p_fge_swap; * coefficient sets (g5p_usc_position_zw()), in place of the A9's position iterator `08 09 ..`, which no A8 capture * has and which reads 0 on the iPod touch 6. PVR_A8_POS_ITER, on by default; 0 gives the A9 form. */ extern bool g5p_usc_g4p_pos_iter; +/* An R8 colour target with loadOp LOAD on the A8, in Apple's form (g5p_mrt_load_usc_g4p_r8(); PVR_A8_R8_LOAD, on by + * default; 0 refuses it, as before) */ +extern bool g5p_usc_g4p_r8_load; enum g5p_binop { G5P_BIN_FADD, G5P_BIN_FMUL, G5P_BIN_IADD, G5P_BIN_IMUL, diff --git a/src/imagination/vulkan/pvr_arch_g5p.c b/src/imagination/vulkan/pvr_arch_g5p.c index 654f5f2b2ec..020a75ff1b2 100644 --- a/src/imagination/vulkan/pvr_arch_g5p.c +++ b/src/imagination/vulkan/pvr_arch_g5p.c @@ -238,6 +238,8 @@ g5p_free_programs(struct pvr_device *device, struct pvr_g5p_programs *p) pvr_bo_suballoc_free(p->a8_load_usc); pvr_bo_suballoc_free(p->a8_load_kick.pvr_bo); pvr_bo_suballoc_free(p->a8_load_dma.pvr_bo); + pvr_bo_suballoc_free(p->a8_load_r8_usc); + pvr_bo_suballoc_free(p->a8_load_r8_kick.pvr_bo); for (unsigned i = 0; i < ARRAY_SIZE(p->raw_load); i++) { if (p->raw_load[i]) { g5p_free_programs(device, p->raw_load[i]); @@ -722,11 +724,12 @@ g5p_load_background(struct pvr_cmd_buffer *cmd_buffer, const uint64_t state[][4] * three DOUTDs of 4 + 4 + 6 dwords into common dwords 0, 4 and 8, the target's texture state (its first two qwords), * Apple's load sampler (00000fff 02022400 0 0: the passcap's words) and the 2x3 matrix from the fragment position to the * sample coordinate (identity: 1, 0, 0, 1, 0, 0). Data controls 10000004, 10008004, 90010006; uniform size 3 units, - * common-store size 14 dwords, as Apple's record has them. + * common-store size 14 dwords, as Apple's record has them. An R8 target (a8-passcap r8_load) has the same uploads, + * controls and sampler, with Apple's R8 program behind the kick. */ static VkResult g5p_load_background_g4p(struct pvr_cmd_buffer *cmd_buffer, const uint64_t state[4], uint64_t bgnd[4], - const struct pvr_g5p_programs *p) + const struct pvr_g5p_programs *p, const struct pvr_pds_upload *kick) { static const uint32_t sampler[4] = { 0x00000fff, 0x02022400, 0, 0 }; static const float matrix[6] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }; @@ -749,7 +752,7 @@ g5p_load_background_g4p(struct pvr_cmd_buffer *cmd_buffer, const uint64_t state[ if (result != VK_SUCCESS) return result; pvr_csb_pack (&bgnd[0], CR_PDS_BGRND0_BASE, b0) { - b0.shader_addr = PVR_DEV_ADDR(p->a8_load_kick.data_offset); + b0.shader_addr = PVR_DEV_ADDR(kick->data_offset); b0.texunicode_addr = PVR_DEV_ADDR(p->a8_load_dma.code_offset); } pvr_csb_pack (&bgnd[2], CR_PDS_BGRND2_BASE, b2) { @@ -1246,12 +1249,37 @@ PVR_PER_ARCH(g5p_job_init)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd /* a linear target loads through its stride texture state (above): Apple's load program samples whatever the * state describes. Without it, a compositor's damage-only redraw into its (linear, shared) output buffer was * refused and the frame never drawn. */ - if (nt != 1 || raw || compact || g->samples != 1 || g->a8_narrow_halves || - (f != VK_FORMAT_B8G8R8A8_UNORM && f != VK_FORMAT_R8G8B8A8_UNORM)) + /* one twiddled R8 target: Apple's R8 load program (a8-passcap r8_load), the same three uploads. A linear R8 + * target and RG8 are not captured. */ + const bool r8 = f == VK_FORMAT_R8_UNORM && g->a8_narrow_halves == 1 && a8_load_twiddled && g5p_usc_g4p_r8_load; + if (nt != 1 || raw || compact || g->samples != 1 || (g->a8_narrow_halves && !r8) || + (f != VK_FORMAT_B8G8R8A8_UNORM && f != VK_FORMAT_R8G8B8A8_UNORM && !r8)) return G5P_REFUSE(cmd_buffer, "A8 LOAD of %s (%u targets, %u samples%s): one single-sample " - "BGRA8/RGBA8 target is captured", vk_Format_to_str(f), nt, g->samples, - a8_load_twiddled ? "" : ", linear"); - result = g5p_load_background_g4p(cmd_buffer, load_state[0], bgnd, p); + "BGRA8/RGBA8 target or one twiddled R8 target is captured%s", vk_Format_to_str(f), nt, + g->samples, a8_load_twiddled ? "" : ", linear", + f == VK_FORMAT_R8_UNORM && !g5p_usc_g4p_r8_load ? " (PVR_A8_R8_LOAD=0)" : ""); + if (r8) { + struct pvr_g5p_programs *programs = &device->g5p->programs; + VkResult lr = VK_SUCCESS; + simple_mtx_lock(&device->g5p->clear_lock); + if (!programs->a8_load_r8_usc) { + uint8_t code[G5P_A8_LOAD_R8_USC_BYTES + 8]; + const unsigned size = g5p_mrt_load_usc_g4p_r8(code); + lr = !size ? vk_errorf(device, VK_ERROR_UNKNOWN, "G5P: A8 R8 load: %s", g5p_usc_error) + : pvr_gpu_upload_usc(device, code, size, 64, &programs->a8_load_r8_usc); + if (lr == VK_SUCCESS) + lr = g5p_upload_kick(device, programs->a8_load_r8_usc, 1, &programs->a8_load_r8_kick); + if (lr != VK_SUCCESS) { + pvr_bo_suballoc_free(programs->a8_load_r8_usc); + programs->a8_load_r8_usc = NULL; + } + } + simple_mtx_unlock(&device->g5p->clear_lock); + if (lr != VK_SUCCESS) + return lr; + } + result = g5p_load_background_g4p(cmd_buffer, load_state[0], bgnd, p, + r8 ? &p->a8_load_r8_kick : &p->a8_load_kick); if (result != VK_SUCCESS) return result; } else if (load) { diff --git a/src/imagination/vulkan/pvr_g5p.h b/src/imagination/vulkan/pvr_g5p.h index 5a58ce35576..1759d23ffad 100644 --- a/src/imagination/vulkan/pvr_g5p.h +++ b/src/imagination/vulkan/pvr_g5p.h @@ -188,6 +188,9 @@ struct pvr_g5p_programs { /* the A8's colour LOAD background (g5p_mrt_load_usc_g4p()): its USC, kick and the three-DOUTD loader */ struct pvr_suballoc_bo *a8_load_usc; struct pvr_pds_upload a8_load_kick, a8_load_dma; + /* its one-target R8 form (g5p_mrt_load_usc_g4p_r8(), lazy: the device's other allocations keep their order) */ + struct pvr_suballoc_bo *a8_load_r8_usc; + struct pvr_pds_upload a8_load_r8_kick; /* raw clear programs by total output words: 2, 4, 8 (two 32-bit integer targets) */ struct pvr_suballoc_bo *raw_clear_usc[4]; /*2/4/8 words, then scalar1 */ /* raw end-of-tile programs by target count: 1, 2 */ diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index ce51cc9425d..cb99b10e2d7 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1427,6 +1427,8 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, g5p_usc_g4p_fge_swap = debug_get_bool_option("PVR_A8_FGE_SWAP", true); /* gl_FragCoord.z/w by the A8's varying iterator (g5p_usc.h) */ g5p_usc_g4p_pos_iter = debug_get_bool_option("PVR_A8_POS_ITER", true); + /* an R8 target's LOAD in Apple's form, from an iOS capture (g5p_usc.h) */ + g5p_usc_g4p_r8_load = debug_get_bool_option("PVR_A8_R8_LOAD", true); } /* The A8 skips refused work by default: a refused program or pass loses its draws (named on stderr) instead of From 5ffadde38061fbaef7845d24974ef5ed7f8fda6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 23 Nov 2026 12:48:23 +0100 Subject: [PATCH 4/6] pvr/g5p: the A8's cube sample read by ALU in Apple's form Apple compiles a cube sample whose result feeds arithmetic (iOS 12.4, AGXMetalA8, sample * constant) as the narrow cube sample with byte 1 0x79, the 32-bit result form of the 2D sample: 1a 79 00 41 00 01 c8 01 00 01 00 80, then the wait 4d 00 and the ALU reading the four 32-bit results. The direction setup before it is the narrow form's. Take it in Apple's single-image layout (image at common dword 0, sampler at dword 4). A cube sample in the multi-image layout is refused by name with its image and sampler positions: that form is not captured. PVR_A8_CUBE_ALU=0 refuses the 32-bit form as before. Generated-by: LLM --- src/imagination/g5p/g5p_usc.c | 8 +++-- src/imagination/g5p/g5p_usc.h | 3 ++ src/imagination/g5p/pco_g5p_general.c | 32 ++++++++++++++++---- src/imagination/vulkan/pvr_physical_device.c | 3 +- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/imagination/g5p/g5p_usc.c b/src/imagination/g5p/g5p_usc.c index 9826fe27d83..6a9c314386f 100644 --- a/src/imagination/g5p/g5p_usc.c +++ b/src/imagination/g5p/g5p_usc.c @@ -19,6 +19,7 @@ bool g5p_usc_g4p_tbo; bool g5p_usc_g4p_fge_swap; bool g5p_usc_g4p_pos_iter; bool g5p_usc_g4p_r8_load; +bool g5p_usc_g4p_cube_alu; const char *const g5p_binop_name[G5P_BIN_COUNT] = {"fadd", "fmul", "iadd", "imul", "and", "or", "xor", "ishr", "isub"}; @@ -1150,8 +1151,11 @@ static unsigned sample_g4p(uint8_t *out, const struct g5p_tex *t) "A8 sample captured only as a 2D or cube sample with an implicit LOD"); /* the cube sample: mode 4 (byte 6 0xc8 with the sampler at dword 4), the direction in three temps from the * coordinate dword, no cube-coordinate instruction (AGXMetalA8's f_tex_cube, passcap USC 0x6076: two FMAs and a - * move build r0..r2, then 1a 59 00 41 00 01 c8 01 00 01 00 80). Only the narrow form is captured. */ - CHECK(t->dim != G5P_TEX_CUBE || t->narrow, "A8 cube sample captured only in the narrow form (result straight to the output)"); + * move build r0..r2, then 1a 59 00 41 00 01 c8 01 00 01 00 80). The 32-bit form is byte 1 0x79, as for 2D + * (f_tex_cube_mul, an iOS capture: 1a 79 00 41 00 01 c8 01 00 01 00 80, then 4d 00 and the multiply); with + * PVR_A8_CUBE_ALU=0 only the narrow form is taken. */ + CHECK(t->dim != G5P_TEX_CUBE || t->narrow || g5p_usc_g4p_cube_alu, + "A8 cube sample: the 32-bit form is off (PVR_A8_CUBE_ALU=0)"); /* the explicit level in the register after the coordinate (Apple's sample at a varying level, iOS capture, USC * heap 0x1_02328000 + 0x8814: the iterated coordinate in r0..r1, r2 = r0 * 4.0, then * 1a 59 00 41 00 01 a8 09 00 01 00 80): byte 6 mode 2, byte 7 0x09, byte 10 0. 2D only. */ diff --git a/src/imagination/g5p/g5p_usc.h b/src/imagination/g5p/g5p_usc.h index b64cca88123..393044b28ae 100644 --- a/src/imagination/g5p/g5p_usc.h +++ b/src/imagination/g5p/g5p_usc.h @@ -82,6 +82,9 @@ extern bool g5p_usc_g4p_pos_iter; /* An R8 colour target with loadOp LOAD on the A8, in Apple's form (g5p_mrt_load_usc_g4p_r8(); PVR_A8_R8_LOAD, on by * default; 0 refuses it, as before) */ extern bool g5p_usc_g4p_r8_load; +/* The A8's cube sample with 32-bit results that ALU reads after the wait, Apple's + * 1a 79 00 41 00 01 c8 01 CC 01 00 80 (sample_g4p(); PVR_A8_CUBE_ALU, on by default; 0 refuses it, as before) */ +extern bool g5p_usc_g4p_cube_alu; enum g5p_binop { G5P_BIN_FADD, G5P_BIN_FMUL, G5P_BIN_IADD, G5P_BIN_IMUL, diff --git a/src/imagination/g5p/pco_g5p_general.c b/src/imagination/g5p/pco_g5p_general.c index 4f99e798a39..b96caa1efc3 100644 --- a/src/imagination/g5p/pco_g5p_general.c +++ b/src/imagination/g5p/pco_g5p_general.c @@ -5495,15 +5495,21 @@ static void emit_tex_ms_own_sample(struct gctx *c, nir_tex_instr *t, struct tex_ } /* The A8's cube sample, as AGXMetalA8's f_tex_cube: the direction in three consecutive temps (computed: FMAs and a - * move, no cube-coordinate instruction; the hardware selects the face), then the narrow sample in mode 4 */ + * move, no cube-coordinate instruction; the hardware selects the face), then the sample in mode 4: narrow when the + * result goes straight to the output, else (PVR_A8_CUBE_ALU) Apple's 32-bit form, as f_tex_cube_mul (a8-passcap + * tex_cube_mul, USC heap 0x1bf6): 1a 79 00 41 00 01 c8 01 00 01 00 80, the wait 4d 00, then the ALU that reads the + * four 32-bit results. Only Apple's single-image layout (image at common dword 0, sampler at dword 4) is captured. */ static void emit_tex_cube_g4p(struct gctx *c, nir_tex_instr *t) { uint8_t raw[G5P_USC_MAX]; struct tex_bind *b = tex_for(c, t); const bool narrow = c->narrow_tex == t; - if (!narrow) - refuse(c, "A8 cube sample whose result feeds arithmetic (only the narrow form, straight to the output, is " - "captured)"); + if (!narrow && !g5p_usc_g4p_cube_alu) + refuse(c, "A8 cube sample whose result feeds arithmetic (PVR_A8_CUBE_ALU=0)"); + if (c->a8_slots) + refuse(c, "A8 cube sample in Apple's multi-image layout (image at common dword %u, sampler at %u, %u images; " + "%s result): the cube sample is captured only with the image at dword 0 and the sampler at dword 4", + b->image, b->sampler, c->ntex, narrow ? "narrow" : "32-bit"); nir_def *cd = nir_get_tex_src(t, nir_tex_src_coord); struct ref r[3]; unsigned reg[3]; @@ -5523,12 +5529,26 @@ static void emit_tex_cube_g4p(struct gctx *c, nir_tex_instr *t) c->scratch |= BITFIELD64_RANGE(coord, 3); } struct g5p_tex s = {.image_qword = c->a8_slots ? b->image : b->image / 2, .a8_slots = c->a8_slots, .sampler_qword = b->sampler, .coord_dword = coord, - .coord_control = true, .dim = G5P_TEX_CUBE, .narrow = true}; + .coord_control = true, .dim = G5P_TEX_CUBE, .narrow = narrow}; + unsigned dst = 0; + if (!narrow) + dst = alloc_run(c, 4, "cube sample result"); + s.dst_half = 2 * dst; put(c, raw, g5p_usc_sample(raw, &s), "cube sample"); c->tex_coord_pending |= BITFIELD64_RANGE(coord, 3); c->tex_batch++; + if (!narrow) { + /* Apple waits right after the sample, before the ALU that reads it */ + c->tex_pending |= BITFIELD64_RANGE(dst, 4); + for (unsigned k = 0; k < 4; k++) { + struct val *v = V(c, &t->def, k); + v->state = VAL_REG; + v->reg = dst + k; + v->held = true; + } + } tex_flush(c); - c->p->evidence |= PCO_G5P_EV_TEX_CAPTURE; + c->p->evidence |= narrow ? PCO_G5P_EV_TEX_CAPTURE : PCO_G5P_EV_TEX_F32; } /* The A8's vertex-stage sample in Apple's form (PVR_A8_VTEX_APPLE), as AGXMetalA8 compiles v_sample, v_read_f32 and diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index cb99b10e2d7..1a2e8e7db2e 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1427,8 +1427,9 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, g5p_usc_g4p_fge_swap = debug_get_bool_option("PVR_A8_FGE_SWAP", true); /* gl_FragCoord.z/w by the A8's varying iterator (g5p_usc.h) */ g5p_usc_g4p_pos_iter = debug_get_bool_option("PVR_A8_POS_ITER", true); - /* an R8 target's LOAD in Apple's form, from an iOS capture (g5p_usc.h) */ + /* an R8 target's LOAD and a cube sample read by ALU, both in Apple's form from an iOS capture (g5p_usc.h) */ g5p_usc_g4p_r8_load = debug_get_bool_option("PVR_A8_R8_LOAD", true); + g5p_usc_g4p_cube_alu = debug_get_bool_option("PVR_A8_CUBE_ALU", true); } /* The A8 skips refused work by default: a refused program or pass loses its draws (named on stderr) instead of From c31d62dce690930c33ca9f3cbff6a25778308ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 23 Nov 2026 13:12:19 +0100 Subject: [PATCH 5/6] pvr/g5p: the A8's cube sample with 2D samples in Apple's form Apple compiles a fragment program that samples a 2D texture and a cube through one shared sampler (iOS 12.4, AGXMetalA8, 2D at texture 0, cube at texture 1, sampler 0) in its multi-image layout: the two image states DMA'd to common 0..7 by one eight-dword DOUTD (10000008), the sampler to 8..11 (10010004). The samples are 1a b9 00 41 00 01 88 01 00 01 00 80 2D, image dword 0, sampler slot 2 4d 04 1a b9 0a 41 08 01 c8 01 08 01 00 80 cube, image dword 4, sampler slot 2 4d 00 so the cube sample in this form is the 2D multi-image sample with mode 4 in byte 6 (0xc8 over 0x88), as the single-image form carries it over 0x80. Byte 8 is the coordinate register in both, byte 2 the result. Take it when a program that samples a cube names one sampler for every image (a separate sampler): the images in their four-dword slots, that sampler in the slot after them, the cube sample in the slot form. The cube path now orders samples of different images as the 2D path does (4d 04 before a sample of another image while one is outstanding); where our schedule already waited with 4d 00, it stays 4d 00. One cube after other common data (images from slot 1) takes the same rule. Programs without a cube keep their layout. A sampler per texture with a cube, and a narrow cube result in this layout, are not captured and stay refused by name. PVR_A8_CUBE_SLOTS=0 refuses it as before. Generated-by: LLM --- src/imagination/g5p/g5p_usc.c | 14 ++++++-- src/imagination/g5p/g5p_usc.h | 3 ++ src/imagination/g5p/pco_g5p_general.c | 34 ++++++++++++++++---- src/imagination/vulkan/pvr_physical_device.c | 2 ++ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/imagination/g5p/g5p_usc.c b/src/imagination/g5p/g5p_usc.c index 6a9c314386f..be886038a54 100644 --- a/src/imagination/g5p/g5p_usc.c +++ b/src/imagination/g5p/g5p_usc.c @@ -20,6 +20,7 @@ bool g5p_usc_g4p_fge_swap; bool g5p_usc_g4p_pos_iter; bool g5p_usc_g4p_r8_load; bool g5p_usc_g4p_cube_alu; +bool g5p_usc_g4p_cube_slots; const char *const g5p_binop_name[G5P_BIN_COUNT] = {"fadd", "fmul", "iadd", "imul", "and", "or", "xor", "ishr", "isub"}; @@ -1173,7 +1174,16 @@ static unsigned sample_g4p(uint8_t *out, const struct g5p_tex *t) * test passed with it. */ if (t->a8_slots) { const unsigned image_dword = t->image_qword, sampler_dword = t->sampler_qword; /* dwords (g5p_tex) */ - CHECK(t->dim == G5P_TEX_2D, "A8 multi-image sample: 2D only (Apple's form is captured for 2D)"); + /* The cube sample in this form (a8-passcap tex2d_cube_mul, iOS 12.4: a 2D image at common dword 0, the cube's + * at 4, one shared sampler at 8): 1a b9 00 41 00 01 88 01 00 01 00 80 for the 2D sample, 4d 04, then + * 1a b9 0a 41 08 01 c8 01 08 01 00 80 and 4d 00. The cube's image and sampler fields are the 2D sample's + * (byte 4 = image dword * 2, slot 2 in byte 1 bits 7:6) and byte 6 carries mode 4 over 0x88, as the + * single-image form does over 0x80. Only the 32-bit result is captured in this form. */ + CHECK(t->dim == G5P_TEX_2D || (t->dim == G5P_TEX_CUBE && g5p_usc_g4p_cube_slots), + "A8 multi-image sample: 2D or (PVR_A8_CUBE_SLOTS) cube only"); + CHECK(t->dim != G5P_TEX_CUBE || (!t->narrow && !t->lod_reg && !t->read_flag && !t->vtx), + "A8 multi-image cube sample: captured only with a 32-bit result and an implicit level"); + const uint8_t mode = t->dim == G5P_TEX_CUBE ? 4 << 4 : lod_mode; CHECK(!(image_dword % 4) && image_dword <= 124, "A8 multi-image sample: image dword a multiple of 4 below 128"); CHECK(!(sampler_dword % 4) && sampler_dword / 4 <= 11, "A8 multi-image sample: sampler slot 0..11"); CHECK(t->coord_dword <= 31 && !(t->dst_half & 1) && t->dst_half < 64, "A8 sample: coordinate/destination range"); @@ -1190,7 +1200,7 @@ static unsigned sample_g4p(uint8_t *out, const struct g5p_tex *t) } const uint8_t raw[12] = {0x1a, (uint8_t)((slot & 3) << 6 | (t->narrow ? 0x19 : 0x39)), (uint8_t)t->dst_half, 0x41, (uint8_t)(2 * image_dword), (uint8_t)(1 | (slot >> 2) << 1), - (uint8_t)(0x88 | lod_mode | (t->read_flag ? 4 : 0)), lod_byte7, + (uint8_t)(0x88 | mode | (t->read_flag ? 4 : 0)), lod_byte7, (uint8_t)(t->coord_dword << 1), 0x01, 0x00, 0x80}; return finish_g4p(out, raw, sizeof(raw)); } diff --git a/src/imagination/g5p/g5p_usc.h b/src/imagination/g5p/g5p_usc.h index 393044b28ae..1646dc828ec 100644 --- a/src/imagination/g5p/g5p_usc.h +++ b/src/imagination/g5p/g5p_usc.h @@ -85,6 +85,9 @@ extern bool g5p_usc_g4p_r8_load; /* The A8's cube sample with 32-bit results that ALU reads after the wait, Apple's * 1a 79 00 41 00 01 c8 01 CC 01 00 80 (sample_g4p(); PVR_A8_CUBE_ALU, on by default; 0 refuses it, as before) */ extern bool g5p_usc_g4p_cube_alu; +/* That cube sample in Apple's multi-image layout (sample_g4p() with g5p_tex.a8_slots): images in four-dword slots, one + * shared sampler after them (PVR_A8_CUBE_SLOTS, on by default; 0 refuses it, as before) */ +extern bool g5p_usc_g4p_cube_slots; enum g5p_binop { G5P_BIN_FADD, G5P_BIN_FMUL, G5P_BIN_IADD, G5P_BIN_IMUL, diff --git a/src/imagination/g5p/pco_g5p_general.c b/src/imagination/g5p/pco_g5p_general.c index b96caa1efc3..2929c74bf5a 100644 --- a/src/imagination/g5p/pco_g5p_general.c +++ b/src/imagination/g5p/pco_g5p_general.c @@ -192,6 +192,8 @@ struct gctx { unsigned tex_batch; bool a8_slots; /* the A8's multi-image layout (plan_common) */ unsigned tex_batch_image; /* a8_slots: the common dword of the batch's last sampled image */ + bool a8_cube; /* the A8 program samples a cube (a8_cube_tex()) */ + bool a8_one_sampler; /* a8_slots: one sampler slot after the images, every sample's */ nir_shader *nir; nir_function_impl *impl; bool vs, half_output; @@ -2262,6 +2264,7 @@ static void plan_common(struct gctx *c) } else { tex_for(c, t); record_lod_sampler(c, t); + c->a8_cube |= a8_cube_tex(t); } continue; } @@ -2445,8 +2448,20 @@ static void plan_common(struct gctx *c) * and the A9 form. */ const unsigned slot_base = next ? align(next, 4) : 0, base_slot = slot_base / 4; const bool a8_multi = g5p_usc_g4p && !c->vs && !qualified && c->ntex && (next || c->ntex >= 2); - const bool a8_each = base_slot + 2 * c->ntex - 1 <= 11, a8_shared = base_slot + c->ntex <= 11; + /* A program that samples a cube and names one sampler for every image (a separate sampler, not combined) takes + * Apple's shared form: the images, then that sampler in the slot after them, as a8-passcap tex2d_cube_mul + * (iOS 12.4: a 2D image to common 0..3 and the cube's to 4..7 in one eight-dword DOUTD, the sampler to 8..11, + * both samples naming slot 2). The cube sample is captured only this way (emit_tex_cube_g4p()); 2D-only + * programs keep their flown layout. */ + bool one_sampler = c->a8_cube && g5p_usc_g4p_cube_slots; + for (unsigned i = 0; one_sampler && i < c->ntex; i++) + one_sampler = !c->tex[i].fetch && c->tex[i].needs_sampler && c->tex[i].sset == c->tex[0].sset && + c->tex[i].sbinding == c->tex[0].sbinding && c->tex[i].selement == c->tex[0].selement; + const bool a8_shared = base_slot + c->ntex <= 11; + const bool a8_each = !(one_sampler && a8_shared) && base_slot + 2 * c->ntex - 1 <= 11; c->a8_slots = a8_multi && (a8_each || a8_shared); + /* one image, or the shared form chosen above; not the shared fallback of seven images with distinct samplers */ + c->a8_one_sampler = c->a8_slots && (c->ntex == 1 || (one_sampler && !a8_each)); c->fetch_sampler_common = sampler_base + sdw * sampler_count; for (unsigned i = 0; c->a8_slots && i < c->ntex; i++) { struct tex_bind *t = &c->tex[i]; @@ -5498,7 +5513,8 @@ static void emit_tex_ms_own_sample(struct gctx *c, nir_tex_instr *t, struct tex_ * move, no cube-coordinate instruction; the hardware selects the face), then the sample in mode 4: narrow when the * result goes straight to the output, else (PVR_A8_CUBE_ALU) Apple's 32-bit form, as f_tex_cube_mul (a8-passcap * tex_cube_mul, USC heap 0x1bf6): 1a 79 00 41 00 01 c8 01 00 01 00 80, the wait 4d 00, then the ALU that reads the - * four 32-bit results. Only Apple's single-image layout (image at common dword 0, sampler at dword 4) is captured. */ + * four 32-bit results. In Apple's multi-image layout (PVR_A8_CUBE_SLOTS), as f_tex2d_cube_mul: the 2D sample, 4d 04, + * 1a b9 0a 41 08 01 c8 01 08 01 00 80 (the cube's image at dword 4, the shared sampler in slot 2), 4d 00. */ static void emit_tex_cube_g4p(struct gctx *c, nir_tex_instr *t) { uint8_t raw[G5P_USC_MAX]; @@ -5506,10 +5522,14 @@ static void emit_tex_cube_g4p(struct gctx *c, nir_tex_instr *t) const bool narrow = c->narrow_tex == t; if (!narrow && !g5p_usc_g4p_cube_alu) refuse(c, "A8 cube sample whose result feeds arithmetic (PVR_A8_CUBE_ALU=0)"); - if (c->a8_slots) - refuse(c, "A8 cube sample in Apple's multi-image layout (image at common dword %u, sampler at %u, %u images; " - "%s result): the cube sample is captured only with the image at dword 0 and the sampler at dword 4", - b->image, b->sampler, c->ntex, narrow ? "narrow" : "32-bit"); + /* In Apple's multi-image layout (other images, or other common data first) the cube sample is captured with a + * 32-bit result and one sampler shared by every image (a8-passcap tex2d_cube_mul): the image in its four-dword + * slot, that sampler in the slot after the images. A sampler per texture, or a narrow result, is not. */ + if (c->a8_slots && (!g5p_usc_g4p_cube_slots || narrow || !c->a8_one_sampler)) + refuse(c, "A8 cube sample in Apple's multi-image layout (image at common dword %u, sampler at %u, %u images, " + "%s; %s result): captured only with a 32-bit result and one sampler shared by every image%s", + b->image, b->sampler, c->ntex, c->a8_one_sampler ? "one shared sampler" : "a sampler per texture", + narrow ? "narrow" : "32-bit", g5p_usc_g4p_cube_slots ? "" : " (PVR_A8_CUBE_SLOTS=0)"); nir_def *cd = nir_get_tex_src(t, nir_tex_src_coord); struct ref r[3]; unsigned reg[3]; @@ -5534,9 +5554,11 @@ static void emit_tex_cube_g4p(struct gctx *c, nir_tex_instr *t) if (!narrow) dst = alloc_run(c, 4, "cube sample result"); s.dst_half = 2 * dst; + a8_image_order(c, b->image); /* Apple's 4d 04 after the 2D sample, before the cube's */ put(c, raw, g5p_usc_sample(raw, &s), "cube sample"); c->tex_coord_pending |= BITFIELD64_RANGE(coord, 3); c->tex_batch++; + c->tex_batch_image = b->image; if (!narrow) { /* Apple waits right after the sample, before the ALU that reads it */ c->tex_pending |= BITFIELD64_RANGE(dst, 4); diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index 1a2e8e7db2e..0c05190cb56 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1430,6 +1430,8 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, /* an R8 target's LOAD and a cube sample read by ALU, both in Apple's form from an iOS capture (g5p_usc.h) */ g5p_usc_g4p_r8_load = debug_get_bool_option("PVR_A8_R8_LOAD", true); g5p_usc_g4p_cube_alu = debug_get_bool_option("PVR_A8_CUBE_ALU", true); + /* and that cube sample with 2D samples, one shared sampler (Apple's multi-image layout, an iOS capture) */ + g5p_usc_g4p_cube_slots = g5p_usc_g4p_cube_alu && debug_get_bool_option("PVR_A8_CUBE_SLOTS", true); } /* The A8 skips refused work by default: a refused program or pass loses its draws (named on stderr) instead of From 28510c284494d639f47012fd5dd55d5acc14e331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 23 Nov 2026 13:19:58 +0100 Subject: [PATCH 6/6] pvr/g5p: the A8's cube sample with a sampler per texture in Apple's form Apple compiles a fragment program that samples a 2D texture and a cube, each through its own sampler (iOS 12.4, AGXMetalA8, 2D at texture 0, cube at texture 1, samplers 0 and 1 with distinct states), in its multi-image layout with a sampler per texture: the two image states DMA'd to common 0..7 by one eight-dword DOUTD (10000008), the two samplers to 8..15 by another (10010008). The samples are 1a b9 00 41 00 01 88 01 00 01 00 80 2D, image dword 0, sampler slot 2 4d 04 1a f9 0a 41 08 01 c8 01 08 01 00 80 cube, image dword 4, sampler slot 3 4d 00 so the cube sample names its own sampler's slot exactly as a 2D sample in this layout does; slot and mode fields compose. This is the shape GL programs take through Zink (a combined sampler per texture). Take a cube in the per-texture form the planner already uses for 2D programs (texture j's sampler in slot N + j). A narrow cube result in the multi-image layout, and the planner's shared fallback (texture 0's sampler for images that name distinct samplers), stay refused by name. PVR_A8_CUBE_SLOTS=0 refuses it as before. Generated-by: LLM --- src/imagination/g5p/g5p_usc.c | 4 +++- src/imagination/g5p/g5p_usc.h | 4 ++-- src/imagination/g5p/pco_g5p_general.c | 25 +++++++++++++------- src/imagination/vulkan/pvr_physical_device.c | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/imagination/g5p/g5p_usc.c b/src/imagination/g5p/g5p_usc.c index be886038a54..c5f23f941bb 100644 --- a/src/imagination/g5p/g5p_usc.c +++ b/src/imagination/g5p/g5p_usc.c @@ -1178,7 +1178,9 @@ static unsigned sample_g4p(uint8_t *out, const struct g5p_tex *t) * at 4, one shared sampler at 8): 1a b9 00 41 00 01 88 01 00 01 00 80 for the 2D sample, 4d 04, then * 1a b9 0a 41 08 01 c8 01 08 01 00 80 and 4d 00. The cube's image and sampler fields are the 2D sample's * (byte 4 = image dword * 2, slot 2 in byte 1 bits 7:6) and byte 6 carries mode 4 over 0x88, as the - * single-image form does over 0x80. Only the 32-bit result is captured in this form. */ + * single-image form does over 0x80. With a sampler per texture (tex2d_cube_mul2s, samplers at 8 and 12) the + * cube's names its own, slot 3: 1a f9 0a 41 08 01 c8 01 08 01 00 80. Only the 32-bit result is captured in + * this form. */ CHECK(t->dim == G5P_TEX_2D || (t->dim == G5P_TEX_CUBE && g5p_usc_g4p_cube_slots), "A8 multi-image sample: 2D or (PVR_A8_CUBE_SLOTS) cube only"); CHECK(t->dim != G5P_TEX_CUBE || (!t->narrow && !t->lod_reg && !t->read_flag && !t->vtx), diff --git a/src/imagination/g5p/g5p_usc.h b/src/imagination/g5p/g5p_usc.h index 1646dc828ec..8ffedf5cb1d 100644 --- a/src/imagination/g5p/g5p_usc.h +++ b/src/imagination/g5p/g5p_usc.h @@ -85,8 +85,8 @@ extern bool g5p_usc_g4p_r8_load; /* The A8's cube sample with 32-bit results that ALU reads after the wait, Apple's * 1a 79 00 41 00 01 c8 01 CC 01 00 80 (sample_g4p(); PVR_A8_CUBE_ALU, on by default; 0 refuses it, as before) */ extern bool g5p_usc_g4p_cube_alu; -/* That cube sample in Apple's multi-image layout (sample_g4p() with g5p_tex.a8_slots): images in four-dword slots, one - * shared sampler after them (PVR_A8_CUBE_SLOTS, on by default; 0 refuses it, as before) */ +/* That cube sample in Apple's multi-image layout (sample_g4p() with g5p_tex.a8_slots): images in four-dword slots, then + * one shared sampler or a sampler per texture after them (PVR_A8_CUBE_SLOTS, on by default; 0 refuses it, as before) */ extern bool g5p_usc_g4p_cube_slots; enum g5p_binop { diff --git a/src/imagination/g5p/pco_g5p_general.c b/src/imagination/g5p/pco_g5p_general.c index 2929c74bf5a..0240f5564f7 100644 --- a/src/imagination/g5p/pco_g5p_general.c +++ b/src/imagination/g5p/pco_g5p_general.c @@ -194,6 +194,7 @@ struct gctx { unsigned tex_batch_image; /* a8_slots: the common dword of the batch's last sampled image */ bool a8_cube; /* the A8 program samples a cube (a8_cube_tex()) */ bool a8_one_sampler; /* a8_slots: one sampler slot after the images, every sample's */ + bool a8_sampler_each; /* a8_slots: texture j's own sampler in slot N + j after the N images */ nir_shader *nir; nir_function_impl *impl; bool vs, half_output; @@ -2451,8 +2452,9 @@ static void plan_common(struct gctx *c) /* A program that samples a cube and names one sampler for every image (a separate sampler, not combined) takes * Apple's shared form: the images, then that sampler in the slot after them, as a8-passcap tex2d_cube_mul * (iOS 12.4: a 2D image to common 0..3 and the cube's to 4..7 in one eight-dword DOUTD, the sampler to 8..11, - * both samples naming slot 2). The cube sample is captured only this way (emit_tex_cube_g4p()); 2D-only - * programs keep their flown layout. */ + * both samples naming slot 2). With a sampler per texture the cube takes the per-texture form below, as + * tex2d_cube_mul2s (the images to 0..7, the two samplers to 8..15 in one eight-dword DOUTD, the cube naming + * slot 3). 2D-only programs keep their flown layout. */ bool one_sampler = c->a8_cube && g5p_usc_g4p_cube_slots; for (unsigned i = 0; one_sampler && i < c->ntex; i++) one_sampler = !c->tex[i].fetch && c->tex[i].needs_sampler && c->tex[i].sset == c->tex[0].sset && @@ -2462,6 +2464,7 @@ static void plan_common(struct gctx *c) c->a8_slots = a8_multi && (a8_each || a8_shared); /* one image, or the shared form chosen above; not the shared fallback of seven images with distinct samplers */ c->a8_one_sampler = c->a8_slots && (c->ntex == 1 || (one_sampler && !a8_each)); + c->a8_sampler_each = c->a8_slots && a8_each; c->fetch_sampler_common = sampler_base + sdw * sampler_count; for (unsigned i = 0; c->a8_slots && i < c->ntex; i++) { struct tex_bind *t = &c->tex[i]; @@ -5514,7 +5517,9 @@ static void emit_tex_ms_own_sample(struct gctx *c, nir_tex_instr *t, struct tex_ * result goes straight to the output, else (PVR_A8_CUBE_ALU) Apple's 32-bit form, as f_tex_cube_mul (a8-passcap * tex_cube_mul, USC heap 0x1bf6): 1a 79 00 41 00 01 c8 01 00 01 00 80, the wait 4d 00, then the ALU that reads the * four 32-bit results. In Apple's multi-image layout (PVR_A8_CUBE_SLOTS), as f_tex2d_cube_mul: the 2D sample, 4d 04, - * 1a b9 0a 41 08 01 c8 01 08 01 00 80 (the cube's image at dword 4, the shared sampler in slot 2), 4d 00. */ + * 1a b9 0a 41 08 01 c8 01 08 01 00 80 (the cube's image at dword 4, the shared sampler in slot 2), 4d 00; with a + * sampler per texture, as f_tex2d_cube_mul2s: the same with the cube's own sampler in slot 3, + * 1a f9 0a 41 08 01 c8 01 08 01 00 80. */ static void emit_tex_cube_g4p(struct gctx *c, nir_tex_instr *t) { uint8_t raw[G5P_USC_MAX]; @@ -5523,12 +5528,16 @@ static void emit_tex_cube_g4p(struct gctx *c, nir_tex_instr *t) if (!narrow && !g5p_usc_g4p_cube_alu) refuse(c, "A8 cube sample whose result feeds arithmetic (PVR_A8_CUBE_ALU=0)"); /* In Apple's multi-image layout (other images, or other common data first) the cube sample is captured with a - * 32-bit result and one sampler shared by every image (a8-passcap tex2d_cube_mul): the image in its four-dword - * slot, that sampler in the slot after the images. A sampler per texture, or a narrow result, is not. */ - if (c->a8_slots && (!g5p_usc_g4p_cube_slots || narrow || !c->a8_one_sampler)) + * 32-bit result, the image in its four-dword slot and the samplers after the images: one shared by every image + * (a8-passcap tex2d_cube_mul, slot N) or one per texture (tex2d_cube_mul2s, texture j's in slot N + j; what GL + * through Zink's combined samplers gives). A narrow result is not captured in this layout, and the planner's + * shared fallback (texture 0's sampler for images that name distinct samplers) would sample with the wrong one. */ + if (c->a8_slots && (!g5p_usc_g4p_cube_slots || narrow || !(c->a8_one_sampler || c->a8_sampler_each))) refuse(c, "A8 cube sample in Apple's multi-image layout (image at common dword %u, sampler at %u, %u images, " - "%s; %s result): captured only with a 32-bit result and one sampler shared by every image%s", - b->image, b->sampler, c->ntex, c->a8_one_sampler ? "one shared sampler" : "a sampler per texture", + "%s; %s result): captured only with a 32-bit result and one shared sampler or a sampler per texture%s", + b->image, b->sampler, c->ntex, + c->a8_one_sampler ? "one shared sampler" : c->a8_sampler_each ? "a sampler per texture" + : "texture 0's sampler for distinct samplers", narrow ? "narrow" : "32-bit", g5p_usc_g4p_cube_slots ? "" : " (PVR_A8_CUBE_SLOTS=0)"); nir_def *cd = nir_get_tex_src(t, nir_tex_src_coord); struct ref r[3]; diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index 0c05190cb56..6122f45d092 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1430,7 +1430,7 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, /* an R8 target's LOAD and a cube sample read by ALU, both in Apple's form from an iOS capture (g5p_usc.h) */ g5p_usc_g4p_r8_load = debug_get_bool_option("PVR_A8_R8_LOAD", true); g5p_usc_g4p_cube_alu = debug_get_bool_option("PVR_A8_CUBE_ALU", true); - /* and that cube sample with 2D samples, one shared sampler (Apple's multi-image layout, an iOS capture) */ + /* and that cube sample with 2D samples, one shared sampler or one each (Apple's multi-image layout, iOS captures) */ g5p_usc_g4p_cube_slots = g5p_usc_g4p_cube_alu && debug_get_bool_option("PVR_A8_CUBE_SLOTS", true); }