Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/imagination/g5p/g5p_mrt_programs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 34 additions & 4 deletions src/imagination/g5p/g5p_usc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ 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;
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"};
Expand Down Expand Up @@ -1147,8 +1152,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. */
Expand All @@ -1166,7 +1174,18 @@ 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. 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),
"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");
Expand All @@ -1183,7 +1202,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));
}
Expand Down Expand Up @@ -1975,6 +1994,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);
}
Expand Down
17 changes: 17 additions & 0 deletions src/imagination/g5p/g5p_usc.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ 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;
/* 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;
/* 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;
/* 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 {
G5P_BIN_FADD, G5P_BIN_FMUL, G5P_BIN_IADD, G5P_BIN_IMUL,
Expand Down
77 changes: 67 additions & 10 deletions src/imagination/g5p/pco_g5p_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ 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 */
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;
Expand Down Expand Up @@ -2262,6 +2265,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;
}
Expand Down Expand Up @@ -2445,8 +2449,22 @@ 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). 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 &&
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->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];
Expand Down Expand Up @@ -3044,7 +3062,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,
Expand All @@ -3064,8 +3084,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;
Expand Down Expand Up @@ -5489,15 +5513,32 @@ 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. 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; 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];
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)");
/* In Apple's multi-image layout (other images, or other common data first) the cube sample is captured with a
* 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 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];
unsigned reg[3];
Expand All @@ -5517,12 +5558,28 @@ 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;
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);
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
Expand Down
Loading
Loading