From 7c813dc59ec7c53c280a7685f4fba4f518fe3ac8 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Wed, 2 Dec 2026 22:58:25 -0600 Subject: [PATCH 01/44] pvr/g9p: kernel record word 6 is zero from 32 invocations up Word 6 of a kernel record carried (threads % 32) << 27. On the A10 that broke every workgroup of more than 32 invocations: with 33, invocation 1 never ran, and with 64 or 128, invocation 32 reported local index 0 of the next workgroup. Writing the field as the thread count below 32 and as 0 from 32 up fixes them. Tested on an iPhone 7 (D10) through Zink: our GLES 3.1 compute ID test (gl_GlobalInvocationID, gl_LocalInvocationIndex, gl_WorkGroupID for local sizes 1 to 128, 8x8x1 and 4x4x4, on 1x1x1 to 2x3x4 grids) passes every case. Before, the 33-, 64- and 128-wide ones failed. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_cdm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/imagination/g9p/g9p_cdm.h b/src/imagination/g9p/g9p_cdm.h index 97d2d6b7c88..3f300c1ab1d 100644 --- a/src/imagination/g9p/g9p_cdm.h +++ b/src/imagination/g9p/g9p_cdm.h @@ -152,7 +152,7 @@ static inline struct g9p_cdm_record g9p_cdm_kernel(uint32_t k0, uint32_t data, u uint32_t threads, uint32_t tg_alloc) { struct g9p_cdm_record r = { { k0, data, code, gx - 1, gy - 1, gz - 1, - (threads % 32) << 27 | tg_alloc, (threads - 1) << 20 } }; + (threads < 32 ? threads : 0) << 27 | tg_alloc, (threads - 1) << 20 } }; return r; } From 0774ed1e6d2b8e410b8fcc5f66af61e43a5da7c1 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Wed, 2 Dec 2026 22:58:25 -0600 Subject: [PATCH 02/44] pvr/g9p: 256-invocation workgroups on the A10 GLES 3.1 and Vulkan ask for 128 invocations per workgroup, the generic Rogue limit used so far. Measured on an iPhone 7 (D10): 256 invocations of up to 24 allocated registers run (16x16x1, 8x8x4), while 512 hang the GPU even at 10 registers. The A10 now offers 256, as the A9 does, and a compute pipeline whose registers go past what was seen to run (40 per invocation, the 16 unified ones included, at 256 invocations) is refused at creation instead of hanging the GPU. Tested through Zink: our compute ID test passes every local size up to 256 (16x16x1, 8x8x4, 8x4x8, 8x8x2), with no GPU reset. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_g9p.c | 17 +++++++++++++++++ src/imagination/vulkan/pvr_physical_device.c | 7 ++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index dd9f04ea3da..88e7bbbdba5 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -144,6 +144,23 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( goto out; } + /* + * A workgroup's tasks are resident together. Measured on the A10: 256 + * invocations of 19 registers (allocated in eights, so 24) run, 512 + * invocations hang the GPU even at 10 registers. The device limit is 256 + * invocations; refuse a pipeline whose registers go past what was seen to + * run (40 per invocation, counting the 16 unified ones, at 256) instead of + * hanging. + */ + const uint32_t threads = + shader.local_size[0] * shader.local_size[1] * shader.local_size[2]; + if ((ALIGN_POT(MAX2(shader.num_regs, 1), 8) + 16) * threads > 40 * 256) { + result = vk_errorf(device, VK_ERROR_UNKNOWN, + "G9P: %u invocations of %u registers each do not fit the USC", + threads, shader.num_regs); + goto out; + } + result = pvr_gpu_upload_usc(device, shader.code, shader.code_size, 64, &state->shader_bo); if (result != VK_SUCCESS) diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index d63073999f5..6e8e55bd5a2 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -761,10 +761,11 @@ static bool pvr_physical_device_get_properties( * tasks per USC). Barriers above 32 lanes are Apple's software barrier (g5p_barrier.h); a pipeline whose * temporaries cannot keep every task of a barriered workgroup resident is refused at creation * (pvr_arch_g5p_compute.c). */ - .maxComputeWorkGroupInvocations = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, + .maxComputeWorkGroupInvocations = + PVR_HAS_FEATURE(dev_info, apple_g5p) || PVR_HAS_FEATURE(dev_info, apple_g9p) ? 256U : 128U, .maxComputeWorkGroupSize = { - [0] = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, - [1] = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, + [0] = PVR_HAS_FEATURE(dev_info, apple_g5p) || PVR_HAS_FEATURE(dev_info, apple_g9p) ? 256U : 128U, + [1] = PVR_HAS_FEATURE(dev_info, apple_g5p) || PVR_HAS_FEATURE(dev_info, apple_g9p) ? 256U : 128U, [2] = 64U, }, From 6d34aa676058550694b9fefdcaa74d759f410d66 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Wed, 2 Dec 2026 23:27:27 -0600 Subject: [PATCH 03/44] pvr/g9p: mask a variable shift amount to 0-31 NIR's shifts use their amount modulo 32, and the A10's shift instructions give 0 for an amount of 32 or more: 1 << 0xfffffffe read 0 instead of 1 << 30. A shift by an amount that is not a constant now ANDs it with 31 first (ishl, ushr, ishr). NIR already folds constant amounts. Tested on an iPhone 7 (D10) through Zink: our GLES 3 integer test passes (shl, shr and sar by variable amounts failed 3801 to 3975 of 4096 cases). Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 38 +++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 37a05f35858..08ef9314c27 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -1445,6 +1445,8 @@ emit_vec(struct g9p_ctx *ctx, nir_alu_instr *alu) return true; } #include "g9p_a9_alu.h" +static int shift_amount(struct g9p_ctx *ctx, nir_alu_instr *alu, int amount); + static bool emit_alu(struct g9p_ctx *ctx, nir_alu_instr *alu) { @@ -1585,6 +1587,11 @@ emit_alu(struct g9p_ctx *ctx, nir_alu_instr *alu) if (r[0] < 0) return false; } + if (alu->op == nir_op_ishl || alu->op == nir_op_ushr || alu->op == nir_op_ishr) { + r[1] = shift_amount(ctx, alu, r[1]); + if (r[1] < 0) + return false; + } release(ctx, &alu->instr); dst = alloc_reg(ctx); if (dst < 0) @@ -1623,6 +1630,27 @@ fs_dword(struct g9p_ctx *ctx, unsigned slot, unsigned dword) : fs_buffer_base(ctx, slot) + dword; } +/* + * A shift's amount, masked to 0-31 as NIR's shifts are: the hardware's shifts + * by 32 or more give 0 (measured: 1 << 0xfffffffe read 0, not 1 << 30). + * Called before the sources are released: its temporaries must not land on + * the value being shifted (textureSize's size >> lod read 1 for 40 >> 0). + */ +static int +shift_amount(struct g9p_ctx *ctx, nir_alu_instr *alu, int amount) +{ + int mask, t; + + if (nir_src_is_const(alu->src[1].src)) + return amount; + mask = const_reg(ctx, 31); + t = alloc_temp(ctx); + if (mask < 0 || t < 0) + return -1; + g9p_alu2(ctx->c, G9P_AND, t, amount, mask, false); + return t; +} + /* The op itself, sources in @r, destination @dst */ static bool emit_alu_op(struct g9p_ctx *ctx, nir_alu_instr *alu, const int *r, int dst) @@ -1655,10 +1683,11 @@ emit_alu_op(struct g9p_ctx *ctx, nir_alu_instr *alu, const int *r, int dst) if (a < 0) return false; /* the A8: a constant amount in Apple's literal form (g9p_shift_imm()) */ - if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) + if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) { g9p_shift_imm(ctx->c, dst, a, nir_alu_src_as_uint(alu->src[1]) & 31, false); - else + } else { g9p_shift(ctx->c, dst, a, r[1], false); + } return true; } case nir_op_ushr: { @@ -1667,10 +1696,11 @@ emit_alu_op(struct g9p_ctx *ctx, nir_alu_instr *alu, const int *r, int dst) if (a < 0) return false; /* the A8: a constant amount in Apple's literal form (g9p_shift_imm()) */ - if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) + if (g5p_usc_g4p && !ctx->c->fragment && nir_src_is_const(alu->src[1].src)) { g9p_shift_imm(ctx->c, dst, a, nir_alu_src_as_uint(alu->src[1]) & 31, true); - else + } else { g9p_shift(ctx->c, dst, a, r[1], true); + } return true; } case nir_op_ishr: From c89687e6296940f07e44d7285ddcb1981616d9e7 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Wed, 2 Dec 2026 23:27:27 -0600 Subject: [PATCH 04/44] pvr/g9p: clamp gl_PointSize to the advertised range The A10 takes a point's size in a 9-bit field: 512 drew nothing and 1000 drew a point 488 pixels wide, while the device advertises sizes up to 511 and GL clamps to that range. Vertex shaders now clamp the size with nir_lower_point_size() to [PVR_POINT_SIZE_RANGE_MIN, _MAX]. Tested on an iPhone 7 (D10) through Zink: our point size test passes (sizes 512 and 1000 draw 511 pixels wide). Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 08ef9314c27..b562dcb16d6 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -28,6 +28,7 @@ #include "compiler/spirv/nir_spirv.h" #include "util/bitset.h" #include "util/ralloc.h" +#include "pvr_limits.h" #include "util/u_math.h" #include @@ -5328,6 +5329,12 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, NIR_PASS(_, nir, nir_shader_instructions_pass, lower_half_packing, nir_metadata_control_flow, (void *)(uintptr_t)(out->a9 && stage == MESA_SHADER_COMPUTE)); } + /* + * gl_PointSize in the advertised range: the hardware's size field is 9 + * bits (measured: 512 drew nothing and 1000 drew 488 pixels wide) + */ + if (stage == MESA_SHADER_VERTEX) + NIR_PASS(_, nir, nir_lower_point_size, PVR_POINT_SIZE_RANGE_MIN, PVR_POINT_SIZE_RANGE_MAX); /* integer division through floats: no such instruction (GLSL ES 1.00 int and ivec /) */ { const nir_lower_idiv_options idiv = { .allow_fp16 = false }; From b2c2c35cb206e860eaa9cc1107c699657b0688ac Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Wed, 2 Dec 2026 23:30:13 -0600 Subject: [PATCH 05/44] pvr/g9p: blend into 32-bit float colour targets R32_SFLOAT, R32G32_SFLOAT and R32G32B32A32_SFLOAT were colour targets without the blend feature, so GL (EXT_float_blend) refused a draw that blends into one and left the target as it was. These targets are raw: the tile holds their own 32-bit words, and the blend's framebuffer fetch (lower_fb_fetch_instr()) already takes them as they are, so blending needs no conversion. Advertise COLOR_ATTACHMENT_BLEND for them. Tested on an iPhone 7 (D10) through Zink: our GLES 3 render target test passes, with RGBA32F, RG32F and R32F drawn with SRC_ALPHA / ONE_MINUS_SRC_ALPHA blending (all 1024 pixels of each were wrong). Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_formats.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/imagination/vulkan/pvr_formats.c b/src/imagination/vulkan/pvr_formats.c index 1cb1eadd1bc..115306c2708 100644 --- a/src/imagination/vulkan/pvr_formats.c +++ b/src/imagination/vulkan/pvr_formats.c @@ -391,8 +391,13 @@ pvr_get_image_format_features2(struct pvr_physical_device *pdevice, else flags &= ~(VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BIT | VK_FORMAT_FEATURE_2_BLIT_DST_BIT); - /* blends read the tile as f16: not integers or 32-bit channels */ - if (g9p_format_renderable(vk_format) && g9p_format_filterable(vk_format)) + /* + * blends read the tile as f16, or a 32-bit float target's own words + * (lower_fb_fetch_instr(): raw targets keep their bits): not integers + */ + if (g9p_format_renderable(vk_format) && + (g9p_format_filterable(vk_format) || vk_format == VK_FORMAT_R32_SFLOAT || + vk_format == VK_FORMAT_R32G32_SFLOAT || vk_format == VK_FORMAT_R32G32B32A32_SFLOAT)) flags |= VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT; else flags &= ~VK_FORMAT_FEATURE_2_COLOR_ATTACHMENT_BLEND_BIT; From c112b9b3ae21b9c01ff8f4cdfad0e3c015ecb95b Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Wed, 2 Dec 2026 23:51:34 -0600 Subject: [PATCH 06/44] pvr/g9p: fold several discards into one Two hardware discards in one fragment shader kill pixels where neither condition holds. An opaque, depth-tested draw whose shader discarded on a uniform and on a texture value, neither of them taken, lost about 90% of its pixels in random tiles. Either discard alone drew every pixel. discards_to_flag() already turns the discards inside control flow into one flag and a single discard at the end. Do the same for every discard of a shader that has more than one, wherever they are. Tested on an iPhone 7 (D10) through Zink: a 750x1334 RGBA8 target with D24S8, drawn with both discards, has every pixel right (924680 of 1000500 were wrong). Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index b562dcb16d6..2c5720c5785 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -6282,17 +6282,33 @@ discards_to_if(nir_shader *nir) * Discards left inside control flow (nested, or with more around them): * each sets a flag instead, and one discard_if on it ends the shader. The * shader runs on for such pixels, which only matters for side effects - * (none in the shaders seen so far). + * (none in the shaders seen so far). A shader with more than one discard + * folds every one of them into the flag: two hardware discards in a shader + * kill pixels where neither condition holds (measured: an opaque, + * depth-tested draw whose shader had a uniform's discard and a texture's, + * neither taken, lost about 90% of its pixels in random tiles; either one + * alone drew every pixel). */ static bool discards_to_flag(nir_shader *nir) { nir_function_impl *impl = nir_shader_get_entrypoint(nir); nir_variable *flag = NULL; + unsigned num_discards = 0; nir_builder b; + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_intrinsic && + (nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_demote || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_terminate || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_demote_if || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_terminate_if)) + num_discards++; + } + } nir_foreach_block_safe(block, impl) { - if (block->cf_node.parent->type == nir_cf_node_function) + if (block->cf_node.parent->type == nir_cf_node_function && num_discards < 2) continue; nir_foreach_instr_safe(instr, block) { nir_intrinsic_instr *intr; From 8770f52c49b82e051e8d47a2f7aa95246fb499c7 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Wed, 2 Dec 2026 23:51:34 -0600 Subject: [PATCH 07/44] pvr/g9p: keep the discards of a blending shader with depth or stencil A shader that reads the tile turns its discards into selects that keep the tile's colour (discards_to_select()), since the hardware discard in the translucent pass lost random tiles. The pixel is still written, though, and with a depth/stencil attachment it writes its depth and stencil. A blended draw's discarded pixels then hid every later draw behind them. With a depth or stencil attachment, keep the hardware discards and run the shader as punch-through, as a shader that writes its coverage already does. Without one nothing changes. The compile gets the attachment from the render pass state, and replay captures carry it. Tested on an iPhone 7 (D10) through Zink: 26 of the 42 cases of our GLES 3 multi-target test pass (10 before), all up to three targets and 8 words a pixel, with D24S8, from 96x96 to 2048x2048. The 16 that fail have four targets, more words or 4x multisampling. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 14 ++++++++++--- src/imagination/g9p/g9p_compile.h | 10 ++++++++-- src/imagination/g9p/g9p_replay.c | 4 ++-- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 24 +++++++++++++++-------- src/imagination/vulkan/pvr_pipeline.h | 1 + 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 2c5720c5785..d0e0cef10ff 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -6542,8 +6542,8 @@ layer_id_zero(nir_builder *b, nir_intrinsic_instr *intr, void *data) bool g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, const nir_lower_blend_options *blend, const struct g9p_coverage *coverage, - const struct g9p_target targets[G9P_MAX_TARGETS], struct g9p_shader *out, - void *mem_ctx) + const struct g9p_target targets[G9P_MAX_TARGETS], bool zs_attachment, + struct g9p_shader *out, void *mem_ctx) { /* Targets past what a pixel holds are no part of its output */ struct g9p_target live[G9P_MAX_TARGETS]; @@ -6602,8 +6602,16 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, nir_metadata_control_flow, NULL); } out->uses_fbfetch = nir->info.fs.uses_fbfetch_output || out->native_blend; - if (out->uses_fbfetch) + /* + * With a depth/stencil attachment the discards stay: a select keeps the + * tile's colour, but the pixel would still write its depth and stencil + * (our GLES 3 multi-target test: a blended draw's discarded pixels hid + * the later draws behind them). Such a shader runs as punch-through + * (punch_fbfetch), where the hardware discard works with tile reads. + */ + if (out->uses_fbfetch && !zs_attachment) NIR_PASS(_, nir, discards_to_select, out->native_blend); + out->punch_fbfetch = out->uses_fbfetch && zs_attachment; NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_fb_fetch_instr, nir_metadata_control_flow, out); nir_shader *copy = nir_shader_clone(mem_ctx, nir); diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index a027a3c4a35..991a1f6b4d9 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -230,6 +230,11 @@ struct g9p_shader { /* writes its coverage (sample mask, alpha to coverage) */ bool writes_coverage; bool uses_fbfetch; + /* + * Fragment: reads the tile and may discard, with a depth/stencil attachment: + * the punch-through pass type, so a discarded pixel writes no depth or stencil + */ + bool punch_fbfetch; /* * Fragment: premultiplied source-over blended at the end with Apple's * half-precision sequence (g9p_blend_src_over()) instead of @@ -297,8 +302,8 @@ struct g9p_coverage { bool g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, const nir_lower_blend_options *blend, const struct g9p_coverage *coverage, - const struct g9p_target targets[G9P_MAX_TARGETS], struct g9p_shader *out, - void *mem_ctx); + const struct g9p_target targets[G9P_MAX_TARGETS], bool zs_attachment, + struct g9p_shader *out, void *mem_ctx); /* * A shader the backend refused, with what its compile was given, for replaying it offline @@ -312,6 +317,7 @@ struct g9p_capture_args { nir_lower_blend_options blend; struct g9p_coverage coverage; struct g9p_target targets[G9P_MAX_TARGETS]; + uint8_t zs_attachment; }; #define G9P_CAPTURE_MAGIC 0x43503947u /* "G9PC" */ diff --git a/src/imagination/g9p/g9p_replay.c b/src/imagination/g9p/g9p_replay.c index 6e94caabc76..acdbeb34f53 100644 --- a/src/imagination/g9p/g9p_replay.c +++ b/src/imagination/g9p/g9p_replay.c @@ -49,8 +49,8 @@ replay(const char *path) break; case MESA_SHADER_FRAGMENT: ok = g9p_compile_fragment(nir, args.frag_coord_slot, args.has_blend ? &args.blend : NULL, - args.has_coverage ? &args.coverage : NULL, args.targets, &out, - mem_ctx); + args.has_coverage ? &args.coverage : NULL, args.targets, args.zs_attachment, + &out, mem_ctx); break; default: ok = g9p_compile_compute(nir, &out, mem_ctx); diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index e9856ff4cc6..9dab898fa9c 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -764,7 +764,8 @@ compile_stage(struct pvr_device *device, nir_shader *nir, const VkPipelineShaderStageCreateInfo *stage, const struct g9p_vertex_attrib *attribs, int frag_coord_slot, bool point_coord, const nir_lower_blend_options *blend, const struct g9p_coverage *coverage, - const struct g9p_target *targets, struct g9p_shader *out, bool point_size, void *mem_ctx) + const struct g9p_target *targets, bool zs_attachment, struct g9p_shader *out, + bool point_size, void *mem_ctx) { bool ok, capture_ok = false; /* G9P_CAPTURE: the shader as the backend got it, kept for a refusal (g9p_replay) */ @@ -774,7 +775,8 @@ compile_stage(struct pvr_device *device, nir_shader *nir, ok = g9p_compile_vertex(nir, attribs, frag_coord_slot, point_coord, out, point_size, mem_ctx); else - ok = g9p_compile_fragment(nir, frag_coord_slot, blend, coverage, targets, out, mem_ctx); + ok = g9p_compile_fragment(nir, frag_coord_slot, blend, coverage, targets, zs_attachment, out, + mem_ctx); /* G9P_CAPTURE_REGS=N: also the shaders that compiled to N registers or more */ if (ok && capture && getenv("G9P_CAPTURE_REGS") && out->num_regs >= (unsigned)atoi(getenv("G9P_CAPTURE_REGS"))) @@ -787,6 +789,7 @@ compile_stage(struct pvr_device *device, nir_shader *nir, .point_size = point_size, .has_blend = blend != NULL, .has_coverage = coverage != NULL, + .zs_attachment = zs_attachment, }; if (attribs) @@ -1067,17 +1070,21 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo /* no attachment: as for BGRA8, the first target's programs stay valid */ if (!targets[0].present && !targets[1].present && !targets[2].present && !targets[3].present) targets[0] = (struct g9p_target){ true, G9P_OUTPUT_HALF2, false, false }; + const bool zs_attachment = + vk_state->rp && (vk_state->rp->depth_attachment_format != VK_FORMAT_UNDEFINED || + vk_state->rp->stencil_attachment_format != VK_FORMAT_UNDEFINED); + result = compile_stage(device, fs_nir, fs_info, NULL, slot, false, - blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, targets, &fs, false, - mem_ctx); + blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, targets, + zs_attachment, &fs, false, mem_ctx); if (result == VK_SUCCESS) result = compile_stage(device, vs_nir, vs_info, attribs, - fs.uses_frag_coord ? (int)slot : -1, fs.uses_point_coord, NULL, NULL, NULL, &vs, + fs.uses_frag_coord ? (int)slot : -1, fs.uses_point_coord, NULL, NULL, NULL, false, &vs, point_size, mem_ctx); for (unsigned v = 1; result == VK_SUCCESS && vs_views[1] && v < PVR_MAX_MULTIVIEW; v++) { result = compile_stage(device, vs_views[v], vs_info, attribs, fs.uses_frag_coord ? (int)slot : -1, fs.uses_point_coord, NULL, NULL, NULL, - &vs_view[v], point_size, mem_ctx); + false, &vs_view[v], point_size, mem_ctx); if (result == VK_SUCCESS) vs.num_regs = MAX2(vs.num_regs, vs_view[v].num_regs); } @@ -1193,6 +1200,7 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo state->straight_samples = fs.straight_samples; state->writes_coverage = fs.writes_coverage; state->uses_fbfetch = fs.uses_fbfetch; + state->punch_fbfetch = fs.punch_fbfetch; /* * Whole blocks of 4: 17 and 18 registers drew wrong values with the field * in VDM_STATE5 covering them, 20 did not (dEQP vertex shaders). @@ -2272,7 +2280,7 @@ g9p_ds_draw_isp(const struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_gr { const struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info; const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); - const unsigned pass = state->uses_fbfetch && !state->writes_coverage ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; + const unsigned pass = state->uses_fbfetch && !state->writes_coverage && !(state->punch_fbfetch && state->uses_discard) ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; if (!hw || hw->ds_attach_idx == VK_ATTACHMENT_UNUSED || !vk_format_has_stencil(info->attachments[hw->ds_attach_idx]->vk.format)) @@ -2682,7 +2690,7 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state */ /* a shader's depth (iOS `fragdepth`): pass 5, and bit 20 of the ISPA words off */ /* a written coverage with tile reads: punch-through, or the samples it drops are lost */ - pass = state->uses_fbfetch && !state->writes_coverage ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; + pass = state->uses_fbfetch && !state->writes_coverage && !(state->punch_fbfetch && state->uses_discard) ? 1u : state->writes_depth ? 5u : state->uses_discard ? 4u : 0u; const uint32_t obj = g9p_isp_objtype(&cmd_buffer->vk.dynamic_graphics_state) << 28 | g9p_isp_line_width(&cmd_buffer->vk.dynamic_graphics_state) << 8; diff --git a/src/imagination/vulkan/pvr_pipeline.h b/src/imagination/vulkan/pvr_pipeline.h index 8118ddf8f3c..c93e5860d1a 100644 --- a/src/imagination/vulkan/pvr_pipeline.h +++ b/src/imagination/vulkan/pvr_pipeline.h @@ -163,6 +163,7 @@ struct pvr_g9p_graphics_state { /* the fragment shader may discard: punch-through pass type */ bool uses_discard; bool uses_fbfetch; + bool punch_fbfetch; /* the vertex shader's common-store slots: vertex buffers, push constants, buffers */ unsigned num_buffers; struct { From 8ba5cf58f868be99de62ea6b83d7dff9aff0980c Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 00:10:16 -0600 Subject: [PATCH 08/44] pvr/g9p: interpolate noperspective varyings without perspective The fragment shader's inputs reach the backend as load_input, with no barycentric whose mode perspective() could read, so every varying was iterated perspective-correct. A noperspective one read the perspective value instead of the screen-linear one (measured: -0.2450 where the pixel wants 0.2344, exactly the perspective-correct value). Mark noperspective inputs from their variables, as flat ones already are, and iterate them without perspective. Tested on an iPhone 7 (D10) through Zink: a noperspective varying holding each vertex's NDC x now gives every pixel centre's own NDC x on a triangle with w 1, 2 and 3, and the unclipped list, strip and fan cases of our flat varying clipping test pass. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 24 +++++++++++++++++++----- src/imagination/g9p/g9p_compile.h | 2 ++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index d0e0cef10ff..f7a7fb87120 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -2216,6 +2216,7 @@ emit_varying_load(struct g9p_ctx *ctx, nir_intrinsic_instr *intr, bool persp) gl_varying_slot_name_for_stage(sem.location, MESA_SHADER_FRAGMENT)); return false; } + persp = persp && !ctx->out->varying_flat[n]; ctx->out->num_varyings = MAX2(ctx->out->num_varyings, n + 1); ctx->out->varying_perspective[n] = persp; @@ -3652,7 +3653,11 @@ emit_intrinsic(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) case nir_intrinsic_load_input: if (ctx->stage == MESA_SHADER_VERTEX) return emit_attrib_load(ctx, intr); - return emit_varying_load(ctx, intr, true); + { + const unsigned n = nir_intrinsic_io_semantics(intr).location - VARYING_SLOT_VAR0; + + return emit_varying_load(ctx, intr, n >= G9P_MAX_VARYINGS || !ctx->out->varying_linear[n]); + } case nir_intrinsic_load_frag_coord: return emit_frag_coord(ctx, intr); case nir_intrinsic_load_point_coord: @@ -6514,18 +6519,27 @@ lower_centroid(nir_builder *b, nir_intrinsic_instr *intr, void *data) return true; } -/* Flat inputs, by varying: NIR's loads do not say */ +/* + * Flat and noperspective inputs, by varying: NIR's loads do not say (they are + * load_input, without a barycentric whose mode perspective() could read) + */ static void find_flat_varyings(nir_shader *nir, struct g9p_shader *out) { nir_foreach_variable_with_modes(var, nir, nir_var_shader_in) { - if (var->data.interpolation != INTERP_MODE_FLAT || var->data.location < VARYING_SLOT_VAR0) + if ((var->data.interpolation != INTERP_MODE_FLAT && + var->data.interpolation != INTERP_MODE_NOPERSPECTIVE) || + var->data.location < VARYING_SLOT_VAR0) continue; const unsigned first = var->data.location - VARYING_SLOT_VAR0; const unsigned slots = glsl_count_attribute_slots(var->type, false); - for (unsigned i = first; i < MIN2(first + slots, G9P_MAX_VARYINGS); i++) - out->varying_flat[i] = true; + for (unsigned i = first; i < MIN2(first + slots, G9P_MAX_VARYINGS); i++) { + if (var->data.interpolation == INTERP_MODE_FLAT) + out->varying_flat[i] = true; + else + out->varying_linear[i] = true; + } } } diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index 991a1f6b4d9..5fc5cd397a9 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -208,6 +208,8 @@ struct g9p_shader { unsigned num_varyings; bool varying_perspective[G9P_MAX_VARYINGS]; bool varying_flat[G9P_MAX_VARYINGS]; + /* Fragment: noperspective inputs, by varying (find_flat_varyings()) */ + bool varying_linear[G9P_MAX_VARYINGS]; /* Fragment: what the first render target takes; integers its low 16 bits */ enum g9p_output output; bool output_int; From 7df1bf17c059bf3b6e6cf290e8535bc3db0a2472 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 00:10:16 -0600 Subject: [PATCH 09/44] pvr/g9p: keep flat and noperspective values where the TA clips Triangles the TA clipped (a vertex past the near plane, behind the eye or outside the guard band) lost their flat values, which came from other vertices than the provoking one, and their noperspective ones. Two TA settings were wrong for them: - VARYING0 counted every varying dword as perspective. It has separate counts for perspective, flat and screen-linear dwords, in that order in the vertex. The varyings are now ordered by class in both stages (g9p_gap_varyings(), which already moved their locations) and counted by class. Transform feedback keeps its layout and the old count. - The PPP control word's bits 7-8 hold the provoking vertex + 1, whose flat values the clipped triangles keep. 0x4a0 named the first vertex, while GL's is the last. It follows the draw's provoking vertex now, as VDM_STATE0's flatshade control does. Either change alone leaves clipped triangles wrong. Tested on an iPhone 7 (D10) through Zink: our flat varying clipping test passes, all 36 cases (lists, strips, fans, indexed, with a restart, near plane, behind the eye and guard band). The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 110 +++++++++++++++++----- src/imagination/vulkan/pvr_pipeline.h | 3 + 2 files changed, 90 insertions(+), 23 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 9dab898fa9c..c656cd46cd3 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -811,19 +811,31 @@ compile_stage(struct pvr_device *device, nir_shader *nir, } /* + * The varyings by the TA's classes (TA_STATE_VARYING0: perspective, flat and + * screen-linear dwords, in that order in the vertex): perspective ones first + * (and those only the vertex shader writes), then flat ones (flat or + * integer), then noperspective ones, gl_FragCoord's after them all. With one + * count for all of them the TA took every varying as perspective, which it + * is not where it clips: a clipped triangle's flat values came from a new + * vertex, and noperspective ones were wrong (our flat varying clipping test). + * Sets the slots of the first two classes. Not with transform feedback, + * whose layout names the locations: everything is perspective then. + * * A varying without W (flat or noperspective) right after one with W loses that one's last * component: its first iterate lands there (dEQP fragment_out). An unused slot between - * them, in both stages' locations. Returns whether the last varying has W, which wants - * the same gap before gl_FragCoord's. Not with transform feedback, whose layout names - * the locations. + * them, in both stages' locations, counted with the perspective ones. Returns whether the + * last varying has W, which wants the same gap before gl_FragCoord's. */ static bool -g9p_gap_varyings(nir_shader *vs, nir_shader *fs) +g9p_gap_varyings(nir_shader *vs, nir_shader *fs, unsigned *persp_slots, unsigned *flat_slots) { bool with_w[G9P_MAX_VARYINGS], used[G9P_MAX_VARYINGS] = { 0 }; - unsigned shift[G9P_MAX_VARYINGS]; + uint8_t cls[G9P_MAX_VARYINGS] = { 0 }; /* 0 perspective, 1 flat, 2 screen-linear */ + unsigned shift[G9P_MAX_VARYINGS], order[G9P_MAX_VARYINGS], num = 0; bool prev = false; + *persp_slots = G9P_MAX_VARYINGS; + *flat_slots = 0; for (unsigned s = 0; s < G9P_MAX_VARYINGS; s++) with_w[s] = true; nir_foreach_variable_with_modes(var, fs, nir_var_shader_in) { @@ -831,13 +843,14 @@ g9p_gap_varyings(nir_shader *vs, nir_shader *fs) continue; const unsigned first = var->data.location - VARYING_SLOT_VAR0; const unsigned n = glsl_count_attribute_slots(var->type, false); - const bool w = var->data.interpolation != INTERP_MODE_FLAT && - var->data.interpolation != INTERP_MODE_NOPERSPECTIVE && - !glsl_type_is_integer(glsl_without_array(var->type)); + const bool flat = var->data.interpolation == INTERP_MODE_FLAT || + glsl_type_is_integer(glsl_without_array(var->type)); + const bool w = !flat && var->data.interpolation != INTERP_MODE_NOPERSPECTIVE; for (unsigned s = first; s < MIN2(first + n, G9P_MAX_VARYINGS); s++) { with_w[s] = w; used[s] = true; + cls[s] = w ? 0 : flat ? 1 : 2; } } nir_foreach_variable_with_modes(var, vs, nir_var_shader_out) { @@ -849,24 +862,47 @@ g9p_gap_varyings(nir_shader *vs, nir_shader *fs) for (unsigned s = first; s < MIN2(first + n, G9P_MAX_VARYINGS); s++) used[s] = true; } - unsigned gap = 0; - - for (unsigned s = 0; s < G9P_MAX_VARYINGS; s++) { - if (used[s]) { - gap += prev && !with_w[s]; - prev = with_w[s]; + if (vs->xfb_info) + return false; + /* the used slots by class, each class in its slots' order */ + for (unsigned c = 0; c < 3; c++) { + for (unsigned s = 0; s < G9P_MAX_VARYINGS; s++) { + if (used[s] && cls[s] == c) + order[num++] = s; } - shift[s] = gap; } - if (vs->xfb_info) + /* new locations: in that order, with the gap where W ends */ + unsigned next = 0; + + for (unsigned i = 0; i < num; i++) { + const unsigned s = order[i]; + + if (prev && !with_w[s]) + next++; + if (cls[s] == 1 && *persp_slots == G9P_MAX_VARYINGS) + *persp_slots = next; + if (cls[s] == 2 && *persp_slots == G9P_MAX_VARYINGS) + *persp_slots = next; + if (cls[s] == 1) + (*flat_slots)++; + prev = with_w[s]; + shift[s] = next++; + } + if (*persp_slots == G9P_MAX_VARYINGS) + *persp_slots = next; + /* past the last slot: no room for the gap, every varying stays where it is, as perspective */ + if (next > G9P_MAX_VARYINGS) { + *persp_slots = G9P_MAX_VARYINGS; + *flat_slots = 0; return false; + } nir_foreach_variable_with_modes(var, vs, nir_var_shader_out) { if (var->data.location >= VARYING_SLOT_VAR0 && var->data.location - VARYING_SLOT_VAR0 < G9P_MAX_VARYINGS) - var->data.location += shift[var->data.location - VARYING_SLOT_VAR0]; + var->data.location = VARYING_SLOT_VAR0 + shift[var->data.location - VARYING_SLOT_VAR0]; } nir_foreach_variable_with_modes(var, fs, nir_var_shader_in) { if (var->data.location >= VARYING_SLOT_VAR0 && var->data.location - VARYING_SLOT_VAR0 < G9P_MAX_VARYINGS) - var->data.location += shift[var->data.location - VARYING_SLOT_VAR0]; + var->data.location = VARYING_SLOT_VAR0 + shift[var->data.location - VARYING_SLOT_VAR0]; } return prev; } @@ -1024,7 +1060,13 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_CUBE; } { - const bool last_with_w = g9p_gap_varyings(vs_nir, fs_nir) && !vs_nir->xfb_info; + unsigned persp_slots, flat_slots; + const bool last_with_w = g9p_gap_varyings(vs_nir, fs_nir, &persp_slots, &flat_slots) && + !vs_nir->xfb_info; + + state->varying_classes = persp_slots < G9P_MAX_VARYINGS; + state->varying_persp_slots = persp_slots; + state->varying_flat_slots = flat_slots; slot = MIN2(g9p_frag_coord_slot(vs_nir) + last_with_w, G9P_MAX_VARYINGS - 1); } @@ -2638,8 +2680,19 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state at += ALIGN_POT(ARRAY_SIZE(_w), 4); \ } while (0) - /* 0: varying words (f32_linear = the varyings' components) */ - PPP_BLOCK(0x001c0000, 4 * state->num_varyings, 0, 0); + /* + * 0: varying words by class (g9p_gap_varyings()): f32_linear (perspective), + * f32_flat, f32_npc (screen-linear) + */ + { + const unsigned persp = state->varying_classes ? MIN2(state->varying_persp_slots, state->num_varyings) + : state->num_varyings; + const unsigned flat = state->varying_classes ? MIN2(state->varying_flat_slots, state->num_varyings - persp) + : 0; + + PPP_BLOCK(0x001c0000, 4 * persp | 4 * flat << 8 | 4 * (state->num_varyings - persp - flat) << 16, + 0, 0); + } /* * 1: PDS state pointers 0-1: shader base, texture/uniform program, * SIZEINFO1 (uniform data in 16-byte units, varying sizes), SIZEINFO2 @@ -2751,8 +2804,19 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state fui(dp->vp[0]), fui(dp->vp[1]), fui(dp->vp[2]), fui(dp->vp[3]), fui(dp->vp[4]), fui(dp->vp[5])); } - /* 5: w clamp (1e-20) and PPP control */ - PPP_BLOCK(0x00210000, 0x1e3ce508, 0x4a0 | dp->cull); + /* + * 5: w clamp (1e-20) and PPP control: bits 7-8 the provoking vertex + 1, whose flat + * values the triangles of a clipped one keep (0x4a0 kept the first: GL's last lost + * them where the TA clipped, in our flat varying clipping test) + */ + { + const struct vk_dynamic_graphics_state *dyn = &cmd_buffer->vk.dynamic_graphics_state; + const uint32_t pv = dyn->rs.provoking_vertex == VK_PROVOKING_VERTEX_MODE_LAST_VERTEX_EXT ? 2 + : dyn->ia.primitive_topology == VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN ? 1 + : 0; + + PPP_BLOCK(0x00210000, 0x1e3ce508, 0x420 | (pv + 1) << 7 | dp->cull); + } /* 6: depth bias / scissor index (ISPCTL bit 16 enables the scissor) */ PPP_BLOCK(0x20, (dp->dbias >= 0 ? (uint32_t)dp->dbias << 16 : 0) | (dp->scissor >= 0 ? (uint32_t)dp->scissor : 0)); diff --git a/src/imagination/vulkan/pvr_pipeline.h b/src/imagination/vulkan/pvr_pipeline.h index c93e5860d1a..47dc39609ad 100644 --- a/src/imagination/vulkan/pvr_pipeline.h +++ b/src/imagination/vulkan/pvr_pipeline.h @@ -156,6 +156,9 @@ struct pvr_g9p_graphics_state { /* the DOUTI data in 16-byte units */ unsigned varying_units, varying_coeffs; unsigned num_varyings; + /* the varying slots by the TA's classes (g9p_gap_varyings()), or all perspective */ + bool varying_classes; + unsigned varying_persp_slots, varying_flat_slots; /* vertex output dwords */ unsigned vs_outputs; /* vertex shader registers (VDM_STATE5 sets their allocation) */ From 939effe65c02fcaf36e7246b80a323307e098b82 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 00:17:07 -0600 Subject: [PATCH 10/44] pvr/g9p: round colours written to R5G6B5 targets The PBE stores floor(v * 2^bits) of each channel of an R5G6B5 target: a 0-255 ramp blitted from RGBA8, or drawn, took the top bits and came out a level low for 52 of 256 values in the 5-bit channels and 42 in the 6-bit one. GL rounds to the nearest level. The fragment shader writes the middle of the nearest level instead, (round(v * m) + 0.5) / 2^bits, which the PBE's floor keeps. A tile read for blending decodes that back to the level's own value, and such a target doesn't take the native blend. Tested on an iPhone 7 (D10) through Zink: the ramp is exact in all three channels, blitted, drawn and after blended passes that keep the destination, and our GLES 3 blit test passes (its RGBA8 to RGB565 case had 2304 of 4096 pixels wrong). Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 50 ++++++++++++++++++++++- src/imagination/g9p/g9p_compile.h | 2 + src/imagination/vulkan/pvr_arch_g9p_gfx.c | 3 +- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index f7a7fb87120..23b63ef1f11 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -4639,11 +4639,57 @@ lower_fb_fetch_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data) comps[i] = nir_bcsel(b, low, nir_imm_int(b, 0), nir_bcsel(b, nir_uge_imm(b, h, 0x3c00), nir_imm_int(b, 0x3f800000), bits)); + /* R5G6B5: the level lower_rgb565_output() wrote, as the stored value */ + if (out->targets[read_target].rgb565 && k < 3) { + const unsigned scale = k == 1 ? 64 : 32; + + comps[i] = nir_fmul_imm(b, nir_fmin(b, nir_ffloor(b, nir_fmul_imm(b, comps[i], scale)), + nir_imm_float(b, scale - 1)), + 1.0 / (scale - 1)); + } } nir_def_replace(&intr->def, nir_vec(b, comps, intr->def.num_components)); return true; } +/* + * R5G6B5 targets: the PBE stores floor(v * 2^bits) of each channel (measured: a + * 0-255 ramp blitted from RGBA8 took the top bits, a level low for 52 of 256 + * values in 5 bits and 42 in 6). The shader writes the middle of the level + * nearest to v instead, (round(v * m) + 0.5) / 2^bits, which that keeps. + */ +static bool +lower_rgb565_output(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + const struct g9p_target *targets = data; + + if (intr->intrinsic != nir_intrinsic_store_output) + return false; + const nir_io_semantics sem = nir_intrinsic_io_semantics(intr); + const unsigned t = sem.location == FRAG_RESULT_COLOR ? 0 : sem.location - FRAG_RESULT_DATA0; + + if ((sem.location != FRAG_RESULT_COLOR && sem.location < FRAG_RESULT_DATA0) || + t >= G9P_MAX_TARGETS || !targets[t].rgb565 || intr->src[0].ssa->bit_size != 32) + return false; + b->cursor = nir_before_instr(&intr->instr); + nir_def *v = intr->src[0].ssa; + nir_def *comps[4]; + + for (unsigned i = 0; i < v->num_components; i++) { + const unsigned k = nir_intrinsic_component(intr) + i; + const unsigned scale = k == 1 ? 64 : 32; + + comps[i] = nir_channel(b, v, i); + if (k < 3) { + nir_def *level = nir_ffloor(b, nir_fadd_imm(b, nir_fmul_imm(b, nir_fsat(b, comps[i]), scale - 1), 0.5)); + + comps[i] = nir_fmul_imm(b, nir_fadd_imm(b, level, 0.5), 1.0 / scale); + } + } + nir_src_rewrite(&intr->src[0], nir_vec(b, comps, v->num_components)); + return true; +} + /* One function, globals as locals; buffers and push constants lowered */ static void lower_front(nir_shader *nir) @@ -6600,7 +6646,7 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, out->native_blend = out->native_blend_kind != G9P_BLEND_NONE && out->output == G9P_OUTPUT_HALF2 && !out->output_float && !targets[1].present && !targets[2].present && - !targets[3].present && !getenv("G9P_NO_NATIVE_BLEND"); + !targets[3].present && !targets[0].rgb565 && !getenv("G9P_NO_NATIVE_BLEND"); if (blend && !out->native_blend && getenv("G9P_PASSLOG")) { const nir_lower_blend_rt *rt = &blend->rt[0]; @@ -6615,6 +6661,8 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_blend_constant, nir_metadata_control_flow, NULL); } + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_rgb565_output, nir_metadata_control_flow, + (void *)targets); out->uses_fbfetch = nir->info.fs.uses_fbfetch_output || out->native_blend; /* * With a depth/stencil attachment the discards stay: a select keeps the diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index 5fc5cd397a9..eced8479025 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -161,6 +161,8 @@ struct g9p_target { uint8_t write_mask; /* past what a pixel holds: the program writes it to memory */ bool overflow; + /* R5G6B5: the PBE keeps floor(v * 2^bits), the shader rounds for it (lower_rgb565_output()) */ + bool rgb565; }; struct g9p_shader { diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index c656cd46cd3..0d5d41ca8c2 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -1101,7 +1101,8 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo if (f != VK_FORMAT_UNDEFINED) targets[t] = (struct g9p_target){ true, g9p_format_output(f), vk_format_is_int(f), - vk_format_is_float(f), .write_mask = 0xf }; + vk_format_is_float(f), .write_mask = 0xf, + .rgb565 = f == VK_FORMAT_R5G6B5_UNORM_PACK16 }; } for (unsigned t = 0; vk_state->cb && t < MIN2(vk_state->cb->attachment_count, G9P_MAX_TARGETS); t++) { From b81c80c6cbb677a1afc6a70fecf4921d755773b1 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 10:29:42 -0600 Subject: [PATCH 11/44] pvr/g9p: keep vertex fetches and index counts inside their buffers The device advertises robustBufferAccess, but a vertex index far past the vertex buffer, or an index count far past the index buffer, faulted the GPU and lost the device. GL_KHR_robustness depends on both being safe. A vertex shader now clamps the element it fetches to the binding's last whole element. The stride slot is 64 bits wide and the stride needs only the low word, so the draw puts that element in the high word, from the bound range and the end of the binding's furthest attribute (kept in the pipeline). An indexed draw draws only the indices inside the index buffer. Tested on an iPhone 7 (D10) through Zink: our robustness test's vertex index of 1,000,000 draws the last vertex, an index count of 1,000,000 from a buffer of 6 draws the 6, per-instance data for 2 instances repeats the second for instances 2 and 3, and the GPU keeps working. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 19 ++++++++--- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 13 +++++++- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 33 ++++++++++++++++++-- src/imagination/vulkan/pvr_pipeline.h | 2 ++ 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 23b63ef1f11..e2ac0bd9fd6 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -2136,7 +2136,12 @@ emit_attrib_load(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) offset = (a->offset & ~3u) + 4 * nir_intrinsic_component(intr) + 16 * nir_src_as_uint(*offset_src); - /* the stride from its slot: the address form with 0 added (rs:rs+1) */ + /* + * The stride from its slot, the address form with 0 added: the stride in + * rs and the binding's last whole element in rs + 1 (buffer_addrs()). The + * element is clamped to that, since robust buffer access is advertised + * and a fetch far past the buffer faults the GPU. + */ zero = const_reg(ctx, 0); zero = zero < 0 ? zero : low_reg(ctx, zero); rs = alloc_pair(ctx); @@ -2145,12 +2150,16 @@ emit_attrib_load(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) return false; g9p_addr(ctx->c, rs, 2 * stride_slot, zero, 0, false); { - const int element = divided ? src_reg(ctx, nir_get_scalar(intr->src[0].ssa, 0)) - : a->per_instance ? ctx->instance_reg : ctx->vertex_reg; + int element = divided ? src_reg(ctx, nir_get_scalar(intr->src[0].ssa, 0)) + : a->per_instance ? ctx->instance_reg : ctx->vertex_reg; + int clamped; - if (element < 0) + element = element < 0 ? element : low_reg(ctx, element); + clamped = element < 0 ? element : alloc_low_temp(ctx); + if (clamped < 0) return false; - g9p_alu2(ctx->c, G9P_IMUL, t, element, rs, false); + emit_select(ctx, G9P_ULT, false, element, rs + 1, element, rs + 1, clamped); + g9p_alu2(ctx->c, G9P_IMUL, t, clamped, rs, false); } ctx->busy &= ~BITFIELD64_RANGE(rs, 2); if (offset) { diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index bc6efa3cdff..c361871123c 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -9915,16 +9915,27 @@ void PVR_PER_ARCH(CmdDrawIndexed)(VkCommandBuffer commandBuffer, return; const unsigned size = vk_index_type_to_bytes(state->index_buffer_binding.type); + /* + * Robust buffer access: only the indices inside the buffer are drawn. + * The VDM reads every index it is given, and far past the buffer that + * faults the GPU. + */ + const uint64_t in_buffer = (state->index_buffer_binding.buffer->vk.size - + state->index_buffer_binding.offset) / size; + const uint32_t count = firstIndex < in_buffer ? MIN2(indexCount, in_buffer - firstIndex) : 0; const struct pvr_g9p_draw draw = { .indexed = true, .index_addr = state->index_buffer_binding.buffer->dev_addr.addr + state->index_buffer_binding.offset + (uint64_t)firstIndex * size, .index_size = size, - .count = indexCount, + .count = count, .instances = instanceCount, .offset = vertexOffset, }; + if (!count) + goto end_cmd_draw_indexed; + /* the vertex shader's G9P_DRAW_PARAMS slot */ state->draw_state.base_instance = firstInstance; result = pvr_flush_push_descriptors(cmd_buffer, &state->gfx_desc_state); diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 0d5d41ca8c2..110d9b051f4 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -1017,6 +1017,9 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo } const struct vk_vertex_binding_state *b = &vk_state->vi->bindings[a->binding]; + state->vertex_binding_end[a->binding] = + MAX2(state->vertex_binding_end[a->binding], + a->offset + vk_format_get_blocksize(a->format)); attribs[loc] = (struct g9p_vertex_attrib){ .present = true, .per_instance = b->input_rate == VK_VERTEX_INPUT_RATE_INSTANCE, @@ -1826,6 +1829,27 @@ realign_vertex_binding(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_verte return VK_SUCCESS; } +/* + * The last element of vertex binding @binding whose attributes all lie in + * the bound range, which the vertex shader clamps its element to (0 when + * not even the first does: the fetch stays at the binding's start) + */ +static uint32_t +vertex_binding_last(const struct pvr_cmd_buffer *cmd_buffer, unsigned binding, uint32_t stride) +{ + const struct pvr_vertex_binding *vb = &cmd_buffer->state.vertex_bindings[binding]; + const struct pvr_graphics_pipeline *pipeline = cmd_buffer->state.gfx_pipeline; + const uint64_t end = pipeline ? pipeline->g9p.vertex_binding_end[binding] : 0; + uint64_t size; + + if (!vb->buffer || !stride) + return 0; + size = vb->size == VK_WHOLE_SIZE ? vb->buffer->vk.size - vb->offset : vb->size; + if (size < end) + return 0; + return MIN2((size - end) / stride, UINT32_MAX); +} + /* * The addresses of a stage's buffers by common-store slot (vertex buffers * and strides, push constants, the viewport, descriptor set buffers) @@ -1861,9 +1885,12 @@ buffer_addrs(struct pvr_cmd_buffer *cmd_buffer, unsigned n, const unsigned (*set dump32("vertex buffer", addrs[k], v, 16); } } else if (set == G9P_VERTEX_STRIDES) { - addrs[k] = cmd_buffer->vk.dynamic_graphics_state.vi_binding_strides[binding]; - if (binding_needs_copy(&cs->vertex_bindings[binding], addrs[k])) - addrs[k] = realigned_stride(addrs[k]); + const uint32_t stride = cmd_buffer->vk.dynamic_graphics_state.vi_binding_strides[binding]; + + addrs[k] = binding_needs_copy(&cs->vertex_bindings[binding], stride) + ? realigned_stride(stride) + : stride; + addrs[k] |= (uint64_t)vertex_binding_last(cmd_buffer, binding, stride) << 32; } else if (set == G9P_PUSH_CONSTANTS) { struct pvr_push_constants *pc = &cs->push_consts[stage]; diff --git a/src/imagination/vulkan/pvr_pipeline.h b/src/imagination/vulkan/pvr_pipeline.h index 47dc39609ad..e53d434dcfa 100644 --- a/src/imagination/vulkan/pvr_pipeline.h +++ b/src/imagination/vulkan/pvr_pipeline.h @@ -159,6 +159,8 @@ struct pvr_g9p_graphics_state { /* the varying slots by the TA's classes (g9p_gap_varyings()), or all perspective */ bool varying_classes; unsigned varying_persp_slots, varying_flat_slots; + /* per vertex binding: the end of its furthest attribute (the fetch's clamp) */ + uint32_t vertex_binding_end[PVR_MAX_VERTEX_INPUT_BINDINGS]; /* vertex output dwords */ unsigned vs_outputs; /* vertex shader registers (VDM_STATE5 sets their allocation) */ From 657ab0eda5b6e75ed3d20c44256078da591bdc07 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 10:29:56 -0600 Subject: [PATCH 12/44] pvr/g9p: keep buffer accesses inside their buffers Storage buffer accesses millions of elements past a buffer, and uniform buffer loads at such offsets, faulted the GPU in compute and vertex shaders and lost the device, while robustBufferAccess is advertised. A10 compute and vertex shaders now go through nir_lower_robust_access() for every storage buffer access and for the uniform buffer loads whose offset is only known when the shader runs. A load past the end reads the buffer's start, and a store or atomic there is dropped. The sizes come from the descriptors: compute's binding DMA already copies them, and a draw now fills the vertex shader's size slots too (get_ubo_size is handled as get_ssbo_size). Constant uniform loads keep their common-store copies. Fragment shaders read their uniform buffers from the common store, so they never fault. A dynamic offset past the copied dwords read 0 there. It now reads the same component of the buffer's first vec4, as the other stages' vector loads do. The A9's compute path is left as it was. Tested on an iPhone 7 (D10) through Zink: our robustness test's storage buffer, uniform block (vertex, fragment and compute) and shared array cases pass, the GPU keeps working after them, and the other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_a9_compute.h | 2 +- src/imagination/g9p/g9p_compile.c | 70 ++++++++++++++++++++--- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 22 ++++++- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/src/imagination/g9p/g9p_a9_compute.h b/src/imagination/g9p/g9p_a9_compute.h index 56f282e758a..6d9feb1431a 100644 --- a/src/imagination/g9p/g9p_a9_compute.h +++ b/src/imagination/g9p/g9p_a9_compute.h @@ -303,7 +303,7 @@ bool g9p_a9_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_c memset(out, 0, sizeof(*out)); out->a9 = true; a9_last_refusal[0] = 0; - lower_front(nir); + lower_front(nir, false); NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_lower_subgroups_size1, nir_metadata_control_flow, NULL); NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_pack_image_store, diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index e2ac0bd9fd6..b6346c8630f 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -3434,9 +3434,14 @@ emit_intrinsic(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) return true; } - case nir_intrinsic_get_ssbo_size: { - /* compute: the size the binding DMA copies from the descriptor (pvr_arch_g9p.c) */ - const int slot = ctx->stage == MESA_SHADER_COMPUTE + case nir_intrinsic_get_ssbo_size: + case nir_intrinsic_get_ubo_size: { + /* + * The size from the descriptor: compute's binding DMA copies it + * (pvr_arch_g9p.c), and a draw fills the vertex shader's slot + * (buffer_addrs()) + */ + const int slot = ctx->stage == MESA_SHADER_COMPUTE || ctx->stage == MESA_SHADER_VERTEX ? buffer_slot_flags(ctx, intr->src[0], G9P_BINDING_SIZE) : -1; int zero = const_reg(ctx, 0), r; @@ -4699,9 +4704,32 @@ lower_rgb565_output(nir_builder *b, nir_intrinsic_instr *intr, void *data) return true; } -/* One function, globals as locals; buffers and push constants lowered */ +/* + * The buffer accesses robust access bounds: every storage buffer access, + * and the uniform buffer loads at an offset only known when the shader runs + */ +static bool +robust_access_filter(const nir_intrinsic_instr *intr, const void *data) +{ + switch (intr->intrinsic) { + case nir_intrinsic_load_ssbo: + case nir_intrinsic_store_ssbo: + case nir_intrinsic_ssbo_atomic: + case nir_intrinsic_ssbo_atomic_swap: + return true; + case nir_intrinsic_load_ubo: + return !nir_src_is_const(intr->src[1]); + default: + return false; + } +} + +/* + * One function, globals as locals; buffers and push constants lowered, + * bounded with @robust + */ static void -lower_front(nir_shader *nir) +lower_front(nir_shader *nir, bool robust) { NIR_PASS(_, nir, nir_lower_variable_initializers, nir_var_function_temp); NIR_PASS(_, nir, nir_lower_returns); @@ -4732,6 +4760,17 @@ lower_front(nir_shader *nir) nir_metadata_control_flow, NULL); NIR_PASS(_, nir, nir_shader_intrinsics_pass, zero_descriptor_offset, nir_metadata_control_flow, NULL); + /* + * Robust buffer access: the memory accesses of A10 compute and vertex + * shaders stay inside their buffers (loads past the end read its start, + * stores and atomics there are dropped), since far past a buffer they + * fault the GPU. Fragment shaders read their uniform buffers from the + * common store. + */ + if (robust) { + NIR_PASS(_, nir, nir_opt_constant_folding); + NIR_PASS(_, nir, nir_lower_robust_access, robust_access_filter, NULL); + } } /* @@ -4963,7 +5002,20 @@ lower_dynamic_ubo_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data) return false; n = MIN2(nir_intrinsic_range(intr) / 4, G9P_FS_BUF_DWORDS); b->cursor = nir_before_instr(&intr->instr); - value = nir_imm_int(b, 0); + /* + * An offset past them reads the same component of the buffer's first + * vec4, as robust access gives the vector loads of other stages + */ + value = nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, 0), .align_mul = 4, + .align_offset = 0, .range_base = 0, .range = 16); + for (unsigned c = 1; c < 4; c++) { + nir_def *first = nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, 4 * c), + .align_mul = 4, .align_offset = 0, .range_base = 0, + .range = 16); + + value = nir_bcsel(b, nir_ieq_imm(b, nir_iand_imm(b, intr->src[1].ssa, 12), 4 * c), first, + value); + } for (unsigned k = 0; k < n; k++) { const unsigned at = nir_intrinsic_range_base(intr) + 4 * k; nir_def *load = nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, at), @@ -5896,7 +5948,7 @@ g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) }; memset(out, 0, sizeof(*out)); - lower_front(nir); + lower_front(nir, true); NIR_PASS(_, nir, nir_lower_compute_system_values, &csv); return compile_back(nir, out, MESA_SHADER_COMPUTE, NULL, 0, false, false, mem_ctx, false, false); } @@ -6230,7 +6282,7 @@ g9p_compile_vertex(nir_shader *nir, const struct g9p_vertex_attrib *attribs, NIR_PASS(_, nir, nir_fixup_deref_modes); NIR_PASS(_, nir, nir_lower_vars_to_ssa); NIR_PASS(_, nir, nir_opt_dce); - lower_front(nir); + lower_front(nir, true); if (frag_coord_slot >= 0) add_frag_coord_output(nir, frag_coord_slot, point_coord); lower_io(nir); @@ -6640,7 +6692,7 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, out->output = targets[0].output; out->output_int = targets[0].integer; out->output_float = targets[0].is_float; - lower_front(nir); + lower_front(nir, false); NIR_PASS(_, nir, discards_to_if); NIR_PASS(_, nir, discards_to_flag); NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_centroid, nir_metadata_control_flow, NULL); diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 110d9b051f4..324aca52a28 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -1721,6 +1721,24 @@ descriptor_buffer_addr(const struct pvr_descriptor_set *ds, unsigned slot_bindin ->addr; } +/* The size of the buffer behind a descriptor (get_ssbo_size, get_ubo_size) */ +static uint32_t +descriptor_buffer_size(const struct pvr_descriptor_set *ds, unsigned slot_binding) +{ + const unsigned element = slot_binding >> G9P_BINDING_ELEMENT_SHIFT; + const unsigned index = slot_binding & BITFIELD_MASK(G9P_BINDING_ELEMENT_SHIFT); + const struct pvr_descriptor_set_layout_binding *b = &ds->layout->bindings[index]; + + if (index >= ds->layout->binding_count) + return 0; + if (b->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC || + b->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) + return ds->dynamic_buffers[b->dynamic_buffer_idx + element].size; + return ((const struct pvr_buffer_descriptor *)((const uint8_t *)ds->mapping + b->offset + + element * b->stride)) + ->size; +} + /* * Vertex fetch reads words from the buffer's address + index x stride + the * attribute's offset, and only the offset's bytes are handled in the shader. A @@ -1971,7 +1989,9 @@ buffer_addrs(struct pvr_cmd_buffer *cmd_buffer, unsigned n, const unsigned (*set } else { const struct pvr_descriptor_set *ds = cs->gfx_desc_state.sets[set]; - if (ds && ds->layout) + if (ds && ds->layout && (binding & G9P_BINDING_SIZE)) + addrs[k] = descriptor_buffer_size(ds, binding & ~G9P_BINDING_SIZE); + else if (ds && ds->layout) addrs[k] = descriptor_buffer_addr(ds, binding); if (addrs[k] && dumping()) { const uint32_t *v = (const uint32_t *)PVR_PER_ARCH(g9p_cpu_addr)(cmd_buffer->device, From 5a90202d0c1b22156b13d3a5d7c86fab93c75de6 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 10:30:05 -0600 Subject: [PATCH 13/44] pvr/g9p: robust image access, for GL_KHR_robustness Zink offers GL_KHR_robustness when the device has robustBufferAccess and robustImageAccess. The A10 has no storage images yet, so its image reads are texel fetches, which are samples at the texel's centre (lower_txf()). Outside the level such a sample wraps or clamps to a texel inside it. A fetch outside the level now reads 0, and Zink bounds the level itself. The A10 advertises VK_EXT_image_robustness with robustImageAccess. Tested on an iPhone 7 (D10) through Zink: GL_KHR_robustness is offered, our robustness test passes, and the other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 16 ++++++++++++++-- src/imagination/vulkan/pvr_physical_device.c | 9 +++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index b6346c8630f..86600341059 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5299,21 +5299,33 @@ lower_txf(nir_builder *b, nir_tex_instr *tex, void *data) b->cursor = nir_before_instr(&tex->instr); lod = li >= 0 ? tex->src[li].src.ssa : nir_imm_int(b, 0); size = nir_get_texture_size(b, tex); - coord = nir_i2f32(b, tex->src[ci].src.ssa); + /* + * Robust image access: a texel outside the level reads 0, where the + * sample would wrap or clamp to one inside (Zink bounds the level itself) + */ + nir_def *icoord = tex->src[ci].src.ssa, *inside = nir_imm_true(b); + coord = nir_i2f32(b, icoord); for (unsigned c = 0; c < n; c++) { nir_def *level_size = nir_imax_imm(b, nir_ishr(b, nir_channel(b, size, c), lod), 1); + inside = nir_iand(b, inside, nir_ult(b, nir_channel(b, icoord, c), level_size)); comps[c] = nir_fdiv(b, nir_fadd_imm(b, nir_channel(b, coord, c), 0.5), nir_i2f32(b, level_size)); } - if (tex->is_array) + if (tex->is_array) { + inside = nir_iand(b, inside, nir_ult(b, nir_channel(b, icoord, n), nir_channel(b, size, n))); comps[n] = nir_channel(b, coord, n); + } nir_src_rewrite(&tex->src[ci].src, nir_vec(b, comps, tex->coord_components)); if (li >= 0) nir_src_rewrite(&tex->src[li].src, nir_i2f32(b, lod)); else nir_tex_instr_add_src(tex, nir_tex_src_lod, nir_imm_float(b, 0.0f)); tex->op = nir_texop_txl; + b->cursor = nir_after_instr(&tex->instr); + nir_def *robust = nir_bcsel(b, inside, &tex->def, nir_imm_zero(b, tex->def.num_components, + tex->def.bit_size)); + nir_def_rewrite_uses_after(&tex->def, robust); return true; } diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index 6e8e55bd5a2..34ff6e96bd7 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1319,6 +1319,15 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, supported_extensions.EXT_color_write_enable = false; pvr_physical_device_get_supported_features(&pdevice->dev_info, &supported_features); + /* + * G9P: a texel fetch outside its level reads 0 (lower_txf()), and there + * are no storage images yet, so image accesses are robust. With robust + * buffer access, Zink then offers GL_KHR_robustness. + */ + if (PVR_HAS_FEATURE(&pdevice->dev_info, apple_g9p)) { + supported_extensions.EXT_image_robustness = true; + supported_features.robustImageAccess = true; + } if (!pvr_physical_device_get_properties(pdevice, &supported_properties)) { result = vk_errorf(instance, VK_ERROR_INITIALIZATION_FAILED, From ee97b146ca176708a3bed97a35d2876223d51f40 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 10:40:57 -0600 Subject: [PATCH 14/44] pvr/g9p: render depth into any level and layer A depth/stencil attachment at a mip level or array layer other than the first was refused, so GL renders into depth texture levels, array layers and cube faces did nothing. The ZLS now loads and stores the attachment's own level and layer, at the offsets the sampler reads them from. In a chain of levels, a depth plane alone takes its level's exact size for ISP_ZLS_PIXELS: the ZLS stores a level smaller than a tile then (measured down to 1x1), while whole tiles would reach into the next level. A stencil plane takes ZLS_PIXELS aligned to 32, so stencil levels under 32 pixels in a chain are still refused by name. Upstream's copy of a small attachment through a tile-rounded buffer is skipped on G9P, as on the A9. Its CPU transfers cannot reach that buffer in the general heap ("G9P transfer: no CPU mapping"), and the render went to the buffer while the image kept its old depth. Tested on an iPhone 7 (D10) through Zink: our depth mip test passes for D32F and D16 2D chains of 64x64, 64x16 and 48x40, a D32F 2D array of 32x32x3 and a D32F cube map of 16x16, every level down to 1x1, with each render leaving the other levels as they were. The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 8 +++ src/imagination/vulkan/pvr_arch_g9p_gfx.c | 55 ++++++++++++++++---- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index c361871123c..0c569830a14 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -1462,6 +1462,14 @@ static bool pvr_sub_cmd_gfx_requires_ds_subtile_alignment( rogue_get_zls_tile_size_xy(dev_info, &zls_tile_size_x, &zls_tile_size_y); PVR_G5P_HOOK(ds_subtile_alignment, dev_info) + /* + * G9P: the ZLS's surface is the attachment's level and layer, sized for + * the ZLS (g9p_ds_addr(), g9p_zls_pixels(), pvr_image.c). The + * tile-rounded buffer would be copied by CPU transfers, which cannot + * reach the general heap. + */ + if (PVR_HAS_FEATURE(dev_info, apple_g9p)) + return false; if (ds_plane->physical_extent.width >= zls_tile_size_x && ds_plane->physical_extent.height >= zls_tile_size_y) { return false; diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 324aca52a28..c6e6348a82f 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -3575,13 +3575,20 @@ g9p_ds_check(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwse if (!g9p_ds_format(iview->vk.format)) return g9p_feature_error(cmd_buffer, "G9P: depth/stencil format %s", vk_Format_to_str(iview->vk.format)); - /* Sides under 32 pixels: the image holds the ZLS's whole tiles (pvr_image.c) */ - if (iview->vk.base_mip_level || iview->vk.base_array_layer) + /* + * A stencil plane takes ZLS_PIXELS aligned to 32 (g9p_zls_pixels()): in a + * chain of levels one under 32 pixels would be stored past its end + */ + const struct pvr_image *image = pvr_image_view_get_image(iview); + const unsigned level = iview->vk.base_mip_level; + + if (vk_format_has_stencil(iview->vk.format) && image->vk.mip_levels > 1 && + (u_minify(plane->physical_extent.width, level) < 32 || + u_minify(plane->physical_extent.height, level) < 32)) return g9p_feature_error(cmd_buffer, - "G9P: a depth/stencil attachment of %ux%u (level %u, layer %u): " - "level 0, layer 0 only", - plane->physical_extent.width, plane->physical_extent.height, - iview->vk.base_mip_level, iview->vk.base_array_layer); + "G9P: level %u of a %ux%u depth/stencil chain: " + "stencil levels of 32 pixels and more only", + level, plane->physical_extent.width, plane->physical_extent.height); return VK_SUCCESS; } @@ -4130,10 +4137,30 @@ PVR_PER_ARCH(g9p_geom_stream)(const struct pvr_device *device, * Zink renders GL's DEPTH24_STENCIL8 with D32F_S8. * Sampled as the matching colour format. * Sides under 32 pixels: the ZLS writes whole 32-pixel tiles, so such an - * image is allocated with them (pvr_image.c). Refused by name: mip levels - * and layers other than the first. + * image is allocated with them (pvr_image.c). In a chain of levels, a depth + * plane alone takes its level's exact size instead, and stencil levels under + * 32 pixels are refused by name. */ +/* + * The ZLS's surface: the attachment's level and layer, at the offsets the + * sampler reads them from (g9p_pack_image_view()) + */ +static uint64_t +g9p_ds_addr(const struct pvr_render_job *job) +{ + const struct pvr_image_view *iview = job->ds.iview; + const struct pvr_image *img; + const struct pvr_image_plane *pl; + + if (!iview) + return job->ds.addr.addr; + img = pvr_image_view_get_image(iview); + pl = pvr_single_plane_const(img); + return img->dev_addr.addr + pl->offset + pl->mip_levels[iview->vk.base_mip_level].offset + + iview->vk.base_array_layer * pl->layer_size; +} + /* The stencil plane's offset in a D32F_S8 image: the second half, after the depth's whole chain */ static uint64_t g9p_ds_stencil_offset(const struct pvr_image *img) @@ -4177,7 +4204,7 @@ g9p_ds_zls(const struct pvr_render_job *job, uint64_t *zlsctl, uint64_t *stencil memcpy(zlsctl, ctl, sizeof(ctl)); if (job->has_stencil_attachment) { pvr_csb_pack (&base, CR_ISP_STENCIL_LOAD_BASE, s) { - s.addr = PVR_DEV_ADDR_OFFSET(job->ds.addr, g9p_ds_stencil_offset(img)); + s.addr = PVR_DEV_ADDR(g9p_ds_addr(job) + g9p_ds_stencil_offset(img)); s.enable = true; } } @@ -4207,6 +4234,14 @@ g9p_zls_pixels(const struct pvr_render_job *job) const bool stencil = job->ds.iview && vk_format_has_stencil(job->ds.iview->vk.format); uint32_t w = job->ds.physical_extent.width, h = job->ds.physical_extent.height; + /* + * A depth plane alone in a chain of levels: its level's exact size, the + * ZLS stores a level smaller than a tile then (measured down to 1x1), + * where the whole tiles would reach into the next level + */ + if (!stencil && job->ds.iview && + pvr_image_view_get_image(job->ds.iview)->vk.mip_levels > 1) + return (w - 1) | (uint64_t)(h - 1) << 15; if (stencil || w < 32 || h < 32) { w = ALIGN_POT(w, 32); h = ALIGN_POT(h, 32); @@ -4246,7 +4281,7 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, g9p_ds_zls(job, &zlsctl, &stencil_base, &bgobjdepth, &bgobjvals); put64(&s, zlsctl); - put64(&s, job->has_depth_attachment ? job->ds.addr.addr : 0); + put64(&s, job->has_depth_attachment ? g9p_ds_addr(job) : 0); /* * The stencil base. The kernel's Apple record takes the depth base for * 0xf50/0xf58 (Z load and store) and this one for 0xf60/0xf68; a stencil From 2089884f6d90e7f38a138f4b954f200942f7a8dc Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 11:22:05 -0600 Subject: [PATCH 15/44] pvr/g9p: one compute job at a time Two or three compute dispatches with a barrier between them, in one submission, hung the A10's firmware ("hang: compute job 1", the device lost), though each job waits for the one before it through the barrier's syncobjs. The same dispatches in separate submissions, or with G9P_SYNC waiting for each job, ran. A compute job is now submitted only once the previous one has completed. A compute-to-compute barrier inside a job emitted Rogue's data fence and fence kernel, which are not A10 programs. It ends the job instead, and the next dispatch starts another. A compute pipeline the backend refuses now says so on stderr, as g9p_feature_error() does: vk_errorf() is quiet outside debug builds and Zink reports only VK_ERROR_UNKNOWN. Tested on an iPhone 7 (D10) through Zink: three dispatches of a storage buffer kernel with glMemoryBarrier() between them, and four of an image kernel, give their results with no GPU reset (they lost the device), and our GLES 3.1 test's image round trips no longer lose it. The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 12 ++++++++++++ src/imagination/vulkan/pvr_arch_g9p.c | 3 +++ src/imagination/vulkan/pvr_arch_queue.c | 16 ++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index 0c569830a14..ebf45af36d6 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -11179,6 +11179,18 @@ void PVR_PER_ARCH(CmdPipelineBarrier2)(VkCommandBuffer commandBuffer, * anything new. */ PVR_G5P_HOOK(compute_barrier) + /* + * G9P: Rogue's data fence and fence kernel crash the A10's firmware + * (two dispatches with a barrier between them in one job): the job + * ends here, and the next dispatch starts one after it + */ + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { + VkResult result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + + if (result != VK_SUCCESS) + pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result); + break; + } /* Issue Data Fence, Wait for Data Fence (IDFWDF) makes the PDS wait * for data. */ diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 88e7bbbdba5..cda7d725da0 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -137,6 +137,7 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( nir_shader *capture = getenv("G9P_CAPTURE") ? nir_shader_clone(mem_ctx, nir) : NULL; if (!g9p_compile_compute(nir, &shader, mem_ctx)) { + mesa_loge("G9P: compute shader not supported by the backend yet"); if (capture) g9p_capture_failure(capture, &(struct g9p_capture_args){ .stage = MESA_SHADER_COMPUTE }); result = vk_errorf(device, VK_ERROR_UNKNOWN, @@ -155,6 +156,8 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( const uint32_t threads = shader.local_size[0] * shader.local_size[1] * shader.local_size[2]; if ((ALIGN_POT(MAX2(shader.num_regs, 1), 8) + 16) * threads > 40 * 256) { + mesa_loge("G9P: %u invocations of %u registers each do not fit the USC", threads, + shader.num_regs); result = vk_errorf(device, VK_ERROR_UNKNOWN, "G9P: %u invocations of %u registers each do not fit the USC", threads, shader.num_regs); diff --git a/src/imagination/vulkan/pvr_arch_queue.c b/src/imagination/vulkan/pvr_arch_queue.c index fc264dcd2d1..0baa9e090de 100644 --- a/src/imagination/vulkan/pvr_arch_queue.c +++ b/src/imagination/vulkan/pvr_arch_queue.c @@ -723,6 +723,22 @@ static VkResult pvr_process_compute_cmd(struct pvr_device *device, if (result != VK_SUCCESS) return result; + /* + * G9P: a compute job queued while another runs hangs the A10's firmware + * (two or three dispatches with barriers between them in one submission: + * "hang: compute job 1"), though the job waits for the one before it. + * The CPU waits for the previous compute job first. + */ + if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p) && + queue->last_job_signal_sync[PVR_JOB_TYPE_COMPUTE]) { + result = vk_sync_wait(&device->vk, queue->last_job_signal_sync[PVR_JOB_TYPE_COMPUTE], 0, + VK_SYNC_WAIT_COMPLETE, UINT64_MAX); + if (result != VK_SUCCESS) { + vk_sync_destroy(&device->vk, sync); + return result; + } + } + result = pvr_arch_compute_job_submit( queue->compute_ctx, sub_cmd, From 11a0945173df489d0d4db25b16bbad8631445adf Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 11:22:17 -0600 Subject: [PATCH 16/44] pvr/g9p: compute storage images on the A10 The A10 had no storage images: imageStore, imageLoad and imageSize failed to compile ("intrinsic image_deref_store not supported yet"), and GLES 3.1 asks for them in compute shaders. A10 compute shaders now read and write a storage image as memory. Its descriptor holds a record in place of texture state (struct g9p_storage_image): the address of the view's level and first layer, the layer stride, the size and the layout. Twiddled, the low bits of x and y are interleaved, y first, up to the shorter side, and the rest of the longer side follows, as the sampler and the CPU transfers lay the texels out. A linear image is the same with no interleaved bits and its row pitch. The shader reads the record through a slot holding the descriptor's own address (G9P_BINDING_DESCRIPTOR) and addresses the texel through the descriptor's first word. The texel's words are packed and unpacked in 32-bit code for formats of R, G, B, A channels of 8, 16 or 32 bits, since the backend has no 16-bit values. Outside the view a load reads 0, without a branch, and a store or atomic is dropped. Image atomics are storage buffer atomics on the texel. Tested on an iPhone 7 (D10) through Zink: our GLES 3.1 test's imageStore into a single-level texture (linear), level 1 of four (twiddled) and layer 2 of an array, a render loaded by imageLoad and stored for a draw to sample, and the r32f, r32ui and r32i round trips (store, load and store, load, imageSize, a load outside the image) pass. The round trips of four-channel formats still fail to compile, out of registers in their checking shaders, and compare-and-swap needs ssbo_atomic_swap, which the A10 backend lacks. The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 293 ++++++++++++++++++ src/imagination/g9p/g9p_compile.h | 25 ++ .../vulkan/pvr_arch_descriptor_set.c | 13 + src/imagination/vulkan/pvr_arch_g9p.c | 58 +++- src/imagination/vulkan/pvr_g9p.h | 3 + src/imagination/vulkan/pvr_physical_device.c | 5 +- 6 files changed, 392 insertions(+), 5 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 86600341059..d2b1948fcee 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5951,6 +5951,288 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, return true; } +static nir_def *f32_to_half(nir_builder *b, nir_def *f); + +/* + * A storage image format whose channels are R, G, B, A in order, each + * 8, 16 or 32 bits wide: what the texel words of lower_storage_image() hold + */ +static const struct util_format_description * +storage_format(enum pipe_format fmt) +{ + const struct util_format_description *desc = util_format_description(fmt); + const unsigned bits = desc ? util_format_get_blocksizebits(fmt) : 0; + + if (!desc || desc->layout != UTIL_FORMAT_LAYOUT_PLAIN || (bits != 32 && bits != 64 && bits != 128)) + return NULL; + for (unsigned i = 0; i < desc->nr_channels; i++) { + const struct util_format_channel_description *ch = &desc->channel[i]; + + if (desc->swizzle[i] != i || (ch->size != 8 && ch->size != 16 && ch->size != 32) || + (ch->type == UTIL_FORMAT_TYPE_FLOAT && ch->size == 8)) + return NULL; + } + return desc; +} + +/* + * A texel's words from the shader's value, in 32-bit integer code (the + * backend has no 16-bit values, which nir_format_pack_rgba() would give): + * normalized channels rounded, integer channels clamped to their range + */ +static nir_def * +storage_pack(nir_builder *b, const struct util_format_description *desc, nir_def *value) +{ + nir_def *word[4] = { NULL }; + + for (unsigned i = 0; i < desc->nr_channels; i++) { + const struct util_format_channel_description *ch = &desc->channel[i]; + nir_def *v = nir_channel(b, value, i), *bits; + const unsigned n = ch->size; + + if (ch->type == UTIL_FORMAT_TYPE_FLOAT) { + bits = n == 32 ? v : f32_to_half(b, v); + } else if (ch->normalized && ch->type == UTIL_FORMAT_TYPE_UNSIGNED) { + const float m = (float)BITFIELD_MASK(n); + + bits = nir_f2u32(b, nir_ffloor(b, nir_fadd_imm(b, nir_fmul_imm(b, nir_fsat(b, v), m), 0.5))); + } else if (ch->normalized) { + const float m = (float)BITFIELD_MASK(n - 1); + nir_def *c = nir_fmin(b, nir_fmax(b, v, nir_imm_float(b, -1.0f)), nir_imm_float(b, 1.0f)); + + bits = nir_f2i32(b, nir_fround_even(b, nir_fmul_imm(b, c, m))); + } else if (ch->type == UTIL_FORMAT_TYPE_UNSIGNED) { + bits = n == 32 ? v : nir_umin(b, v, nir_imm_int(b, BITFIELD_MASK(n))); + } else { + bits = n == 32 ? v + : nir_imin(b, nir_imax(b, v, nir_imm_int(b, -(1 << (n - 1)))), + nir_imm_int(b, BITFIELD_MASK(n - 1))); + } + if (n < 32) + bits = nir_ishl_imm(b, nir_iand_imm(b, bits, BITFIELD_MASK(n)), ch->shift % 32); + word[ch->shift / 32] = word[ch->shift / 32] ? nir_ior(b, word[ch->shift / 32], bits) : bits; + } + for (unsigned i = 0; i < util_format_get_blocksizebits(desc->format) / 32; i++) { + if (!word[i]) + word[i] = nir_imm_int(b, 0); + } + return nir_vec(b, word, util_format_get_blocksizebits(desc->format) / 32); +} + +/* The shader's value of a texel's words: storage_pack() backwards, 0, 0, 0, 1 where missing */ +static nir_def * +storage_unpack(nir_builder *b, const struct util_format_description *desc, nir_def *words) +{ + const bool is_int = desc->channel[0].type != UTIL_FORMAT_TYPE_FLOAT && !desc->channel[0].normalized; + nir_def *rgba[4]; + + for (unsigned i = 0; i < 4; i++) { + const struct util_format_channel_description *ch = &desc->channel[i]; + + if (i >= desc->nr_channels) { + rgba[i] = i == 3 ? (is_int ? nir_imm_int(b, 1) : nir_imm_float(b, 1.0f)) : nir_imm_int(b, 0); + continue; + } + + const unsigned n = ch->size; + nir_def *w = nir_channel(b, words, ch->shift / 32); + nir_def *u = n == 32 ? w : nir_ubitfield_extract_imm(b, w, ch->shift % 32, n); + nir_def *sx = n == 32 ? w : nir_ibitfield_extract_imm(b, w, ch->shift % 32, n); + + if (ch->type == UTIL_FORMAT_TYPE_FLOAT) + rgba[i] = n == 32 ? w : half_to_f32(b, u); + else if (ch->normalized && ch->type == UTIL_FORMAT_TYPE_UNSIGNED) + rgba[i] = nir_fmul_imm(b, nir_u2f32(b, u), 1.0f / BITFIELD_MASK(n)); + else if (ch->normalized) + rgba[i] = nir_fmax(b, nir_fmul_imm(b, nir_i2f32(b, sx), 1.0f / BITFIELD_MASK(n - 1)), + nir_imm_float(b, -1.0f)); + else + rgba[i] = ch->type == UTIL_FORMAT_TYPE_UNSIGNED ? u : sx; + } + return nir_vec(b, rgba, 4); +} + +/* The low 16 bits of @v spread to the even bits */ +static nir_def * +spread_bits(nir_builder *b, nir_def *v) +{ + v = nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 8)), 0x00ff00ff); + v = nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 4)), 0x0f0f0f0f); + v = nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 2)), 0x33333333); + return nir_iand_imm(b, nir_ior(b, v, nir_ishl_imm(b, v, 1)), 0x55555555); +} + +/* + * A10 compute storage images, as memory: each access reads the view's + * record from its descriptor (struct g9p_storage_image, at the slot of + * G9P_BINDING_DESCRIPTOR), then loads, stores or an atomic on the texel's + * raw words in the format the shader declares (storage_pack()). + * Outside the view a store or atomic is dropped and a load reads 0, as + * robust image access asks. @data: set when an access cannot be lowered. + */ +static bool +lower_storage_image(nir_builder *b, nir_intrinsic_instr *in, void *data) +{ + bool *failed = data; + + switch (in->intrinsic) { + case nir_intrinsic_image_deref_load: + case nir_intrinsic_image_deref_store: + case nir_intrinsic_image_deref_size: + case nir_intrinsic_image_deref_samples: + case nir_intrinsic_image_deref_atomic: + case nir_intrinsic_image_deref_atomic_swap: + break; + default: + return false; + } + nir_deref_instr *deref = nir_src_as_deref(in->src[0]); + unsigned element = 0; + + if (deref && deref->deref_type == nir_deref_type_array) { + if (!nir_src_is_const(deref->arr.index)) { + *failed = true; + return false; + } + element = nir_src_as_uint(deref->arr.index); + deref = nir_deref_instr_parent(deref); + } + nir_variable *var = deref && deref->deref_type == nir_deref_type_var ? deref->var : NULL; + + if (!var || var->data.binding >= BITFIELD_BIT(G9P_BINDING_ELEMENT_SHIFT)) { + *failed = true; + return false; + } + b->cursor = nir_before_instr(&in->instr); + if (in->intrinsic == nir_intrinsic_image_deref_samples) { + nir_def_replace(&in->def, nir_imm_int(b, 1)); + return true; + } + + const enum glsl_sampler_dim dim = nir_intrinsic_image_dim(in); + const bool array = nir_intrinsic_image_array(in); + nir_def *rec = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, element), + .desc_set = var->data.descriptor_set, + .binding = var->data.binding | G9P_BINDING_DESCRIPTOR, + .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); + nir_def *word[8]; + + /* as uniform loads, which rematerialize_loads() repeats where they are used */ + for (unsigned i = 2; i < 8; i++) + word[i] = nir_load_ubo(b, 1, 32, rec, nir_imm_int(b, 4 * i), .align_mul = 4, + .align_offset = 0, .range_base = 0, + .range = sizeof(struct g9p_storage_image)); + + nir_def *tbits = word[2], *row_bytes = word[3], *stride = word[4]; + nir_def *w = word[5], *h = word[6], *n = word[7]; + + if (in->intrinsic == nir_intrinsic_image_deref_size) { + nir_def *size[3]; + unsigned k = 0; + + size[k++] = w; + if (dim != GLSL_SAMPLER_DIM_1D) + size[k++] = h; + if (dim == GLSL_SAMPLER_DIM_3D || array) + size[k++] = dim == GLSL_SAMPLER_DIM_CUBE ? nir_udiv_imm(b, n, 6) : n; + while (k < in->def.num_components) + size[k++] = nir_imm_int(b, 1); + nir_def_replace(&in->def, nir_u2uN(b, nir_vec(b, size, in->def.num_components), + in->def.bit_size)); + return true; + } + + enum pipe_format fmt = nir_intrinsic_format(in); + + if (fmt == PIPE_FORMAT_NONE) + fmt = var->data.image.format; + const struct util_format_description *desc = + fmt == PIPE_FORMAT_NONE ? NULL : storage_format(fmt); + + if (!desc) { + *failed = true; + return false; + } + const unsigned bits = util_format_get_blocksizebits(fmt), words = bits / 32; + + nir_def *c = in->src[1].ssa; + nir_def *x = nir_channel(b, c, 0); + nir_def *y = dim == GLSL_SAMPLER_DIM_1D ? nir_imm_int(b, 0) : nir_channel(b, c, 1); + /* layers (3D slices, cube faces): only when there are any, for fewer live values */ + const bool layered = array || dim == GLSL_SAMPLER_DIM_3D || dim == GLSL_SAMPLER_DIM_CUBE; + nir_def *z = dim == GLSL_SAMPLER_DIM_1D && array ? nir_channel(b, c, 1) : nir_channel(b, c, 2); + nir_def *inside = nir_iand(b, nir_ult(b, x, w), nir_ult(b, y, h)); + + if (layered) + inside = nir_iand(b, inside, nir_ult(b, z, n)); + nir_def *mask = nir_iadd_imm(b, nir_ishl(b, nir_imm_int(b, 1), tbits), -1); + nir_def *col = nir_ior(b, nir_ishl_imm(b, spread_bits(b, nir_iand(b, x, mask)), 1), + nir_ishl(b, nir_iand(b, x, nir_inot(b, mask)), tbits)); + nir_def *row = nir_ior(b, spread_bits(b, nir_iand(b, y, mask)), + nir_ishl(b, nir_iand(b, y, nir_inot(b, mask)), tbits)); + nir_def *off = nir_iadd(b, nir_imul_imm(b, col, bits / 8), nir_imul(b, row, row_bytes)); + nir_def *texels = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, element), + .desc_set = var->data.descriptor_set, + .binding = var->data.binding, + .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); + + if (layered) + off = nir_iadd(b, off, nir_imul(b, z, stride)); + /* before the ifs: a phi must come first in the block after one */ + nir_def *zero = nir_imm_int(b, 0); + + if (in->intrinsic == nir_intrinsic_image_deref_atomic || + in->intrinsic == nir_intrinsic_image_deref_atomic_swap) { + nir_def *r; + + if (bits != 32 || in->def.bit_size != 32) { + *failed = true; + return false; + } + nir_push_if(b, inside); + if (in->intrinsic == nir_intrinsic_image_deref_atomic_swap) + r = nir_ssbo_atomic_swap(b, 32, texels, off, in->src[3].ssa, in->src[4].ssa, + .atomic_op = nir_intrinsic_atomic_op(in)); + else + r = nir_ssbo_atomic(b, 32, texels, off, in->src[3].ssa, + .atomic_op = nir_intrinsic_atomic_op(in)); + nir_pop_if(b, NULL); + nir_def_replace(&in->def, nir_if_phi(b, r, zero)); + return true; + } + if (in->intrinsic == nir_intrinsic_image_deref_store) { + nir_def *raw = storage_pack(b, desc, in->src[3].ssa); + + nir_push_if(b, inside); + for (unsigned i = 0; i < words; i++) + nir_store_ssbo(b, nir_channel(b, raw, i), texels, nir_iadd_imm(b, off, 4 * i), + .write_mask = 1, .align_mul = 4, .align_offset = 0); + nir_pop_if(b, NULL); + nir_instr_remove(&in->instr); + return true; + } + + /* + * A load outside reads the first texel and gives 0: no branch, whose phis + * would hold registers for the whole program + */ + nir_def *raw[4]; + + off = nir_bcsel(b, inside, off, zero); + for (unsigned i = 0; i < words; i++) + raw[i] = nir_bcsel(b, inside, + nir_load_ssbo(b, 1, 32, texels, nir_iadd_imm(b, off, 4 * i), + .align_mul = 4, .align_offset = 0), + zero); + + nir_def *rgba = storage_unpack(b, desc, nir_vec(b, raw, words)); + + if (rgba->bit_size != in->def.bit_size) + rgba = nir_u2uN(b, rgba, in->def.bit_size); + nir_def_replace(&in->def, nir_trim_vector(b, rgba, in->def.num_components)); + return true; +} + bool g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) { @@ -5961,6 +6243,17 @@ g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) memset(out, 0, sizeof(*out)); lower_front(nir, true); + { + bool failed = false; + + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_storage_image, nir_metadata_none, + &failed); + if (failed) + return g9p_fail(out, "a storage image access without a format of RGBA channels of 8, 16 " + "or 32 bits, or of an image array by a computed index"); + NIR_PASS(_, nir, nir_opt_dce); + NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_image, NULL); + } NIR_PASS(_, nir, nir_lower_compute_system_values, &csv); return compile_back(nir, out, MESA_SHADER_COMPUTE, NULL, 0, false, false, mem_ctx, false, false); } diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index eced8479025..87936c928fe 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -26,6 +26,31 @@ #define G9P_BINDING_ELEMENT_SHIFT 8 /* On a slot's binding: the buffer's size (the descriptor's dword 2), not its address */ #define G9P_BINDING_SIZE (1u << 30) +/* + * On a slot's binding: the address of the descriptor itself, for a storage + * image's record (struct g9p_storage_image) + */ +#define G9P_BINDING_DESCRIPTOR (1u << 29) +#define G9P_BINDING_FLAGS (G9P_BINDING_SIZE | G9P_BINDING_DESCRIPTOR) + +/* + * A10 compute storage images are read and written as memory + * (lower_storage_image()): their descriptor holds this record in place of + * texture state. The texels of the view's level and first layer, as the + * sampler lays them out. Twiddled, the low @bits bits of x and y are + * interleaved (y in bit 0) and the rest of the longer side follows, so a + * texel's index is col(x) + row(y) with disjoint bits. A linear image is the + * same with no interleaved bits and @row_bytes per row: texel + * col(x) * cpp + row(y) * row_bytes, with @row_bytes the texel size when + * twiddled. + */ +struct g9p_storage_image { + uint64_t base; + uint32_t bits; + uint32_t row_bytes; + uint32_t layer_stride; + uint32_t width, height, layers; +}; #define G9P_MAX_BUFFERS 40 /* 16 vertex bindings x address and stride, 4 capture buffers, uniforms */ #define G9P_MAX_ATTRIBS 16 /* diff --git a/src/imagination/vulkan/pvr_arch_descriptor_set.c b/src/imagination/vulkan/pvr_arch_descriptor_set.c index 4a49af05b4a..04cd815ddcd 100644 --- a/src/imagination/vulkan/pvr_arch_descriptor_set.c +++ b/src/imagination/vulkan/pvr_arch_descriptor_set.c @@ -19,6 +19,8 @@ #include "pvr_physical_device.h" #include "pvr_sampler.h" #include "pvr_g5p_compute_state.h" +#include "pvr_g9p.h" +#include "g9p_compile.h" static void write_buffer(const struct pvr_descriptor_set *set, const VkDescriptorBufferInfo *buffer_info, @@ -208,6 +210,17 @@ write_storage_image(const struct pvr_descriptor_set *set, VK_FROM_HANDLE(pvr_image_view, image_view, image_info->imageView); + /* G9P: the record its compute shaders address the texels with */ + if (PVR_HAS_FEATURE(dev_info, apple_g9p)) { + struct g9p_storage_image rec; + + static_assert(sizeof(rec) <= sizeof(struct pvr_image_descriptor), + "a storage image's record fits its descriptor"); + PVR_PER_ARCH(g9p_storage_image_record)(image_view, &rec); + memcpy(desc_mapping, &rec, sizeof(rec)); + return; + } + bool is_cube = image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE || image_view->vk.view_type == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY; diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index cda7d725da0..4e936d851f2 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -25,6 +25,7 @@ #include "pvr_entrypoints.h" #include "pvr_free_list.h" #include "pvr_g9p.h" +#include "pvr_image.h" #include "pvr_physical_device.h" #include "pvr_pipeline.h" @@ -199,6 +200,41 @@ emit_record(struct pvr_csb *csb, const struct g9p_cdm_record *rec) * kernel, whose PDS program writes the workgroup ID into threadgroup words * 0-1 and kicks the USC. */ +/* + * A storage image view's record (struct g9p_storage_image): its level's + * texels from its first layer, the layer stride (a 3D image's slices are + * layers, pvr_image.c), and the twiddle box's shorter side, which the + * level's power-of-two physical extent gives (as the sampler and the + * transfers lay the texels out) + */ +void +PVR_PER_ARCH(g9p_storage_image_record)(const struct pvr_image_view *iview, + struct g9p_storage_image *rec) +{ + const struct pvr_image *image = pvr_image_view_get_image(iview); + const struct pvr_image_plane *plane = pvr_single_plane_const(image); + const unsigned level = iview->vk.base_mip_level; + const bool is3d = iview->vk.view_type == VK_IMAGE_VIEW_TYPE_3D; + + memset(rec, 0, sizeof(*rec)); + rec->base = image->dev_addr.addr + plane->offset + plane->mip_levels[level].offset + + (is3d ? 0 : iview->vk.base_array_layer * plane->layer_size); + rec->layer_stride = plane->layer_size; + rec->width = iview->vk.extent.width; + rec->height = iview->vk.extent.height; + rec->layers = is3d ? iview->vk.extent.depth : iview->vk.layer_count; + if (image->memlayout == PVR_MEMLAYOUT_TWIDDLED || + image->memlayout == PVR_MEMLAYOUT_3DTWIDDLED) { + const uint32_t pw = u_minify(plane->physical_extent.width, level); + const uint32_t ph = u_minify(plane->physical_extent.height, level); + + rec->bits = util_logbase2(util_next_power_of_two(MAX2(MIN2(pw, ph), 1))); + rec->row_bytes = vk_format_get_blocksize(iview->vk.format); + } else { + rec->row_bytes = plane->mip_levels[level].pitch; + } +} + VkResult PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_compute *sub_cmd, @@ -239,10 +275,26 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, uniforms[2 * k] = pc->dev_addr.addr; uniforms[2 * k + 1] = pc->dev_addr.addr >> 32; } - if (state->buffers[k].set < PVR_MAX_DESCRIPTOR_SETS) { + if (state->buffers[k].set < PVR_MAX_DESCRIPTOR_SETS && + (state->buffers[k].binding & G9P_BINDING_DESCRIPTOR)) { + /* a storage image's record: the descriptor's own address */ + const struct pvr_descriptor_set *set = desc->sets[state->buffers[k].set]; + const unsigned element = + (state->buffers[k].binding & ~G9P_BINDING_FLAGS) >> G9P_BINDING_ELEMENT_SHIFT; + const unsigned b = state->buffers[k].binding & BITFIELD_MASK(G9P_BINDING_ELEMENT_SHIFT); + uint64_t addr; + + if (!set || !set->layout || b >= set->layout->binding_count) + return vk_error(cmd_buffer, VK_ERROR_UNKNOWN); + addr = set->dev_addr.addr + set->layout->bindings[b].offset + + element * set->layout->bindings[b].stride; + uniforms[2 * k] = addr; + uniforms[2 * k + 1] = addr >> 32; + from_uniforms[k] = true; + } else if (state->buffers[k].set < PVR_MAX_DESCRIPTOR_SETS) { /* a dynamic buffer has no descriptor in set memory (binding offset ~0) */ const unsigned element = - (state->buffers[k].binding & ~G9P_BINDING_SIZE) >> G9P_BINDING_ELEMENT_SHIFT; + (state->buffers[k].binding & ~G9P_BINDING_FLAGS) >> G9P_BINDING_ELEMENT_SHIFT; const unsigned b = state->buffers[k].binding & BITFIELD_MASK(G9P_BINDING_ELEMENT_SHIFT); if (!pvr_descriptor_set_dynamic_slot(desc->sets[state->buffers[k].set], b, element, @@ -273,7 +325,7 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_descriptor_set *set = desc->sets[state->buffers[k].set]; /* an array's element (Zink keeps all storage buffers in one array) */ const unsigned element = - (state->buffers[k].binding & ~G9P_BINDING_SIZE) >> G9P_BINDING_ELEMENT_SHIFT; + (state->buffers[k].binding & ~G9P_BINDING_FLAGS) >> G9P_BINDING_ELEMENT_SHIFT; const struct pvr_descriptor_set_layout_binding *binding; if (!set) diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index 7bfebf3e624..b8da4722306 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -163,6 +163,9 @@ void PVR_PER_ARCH(g9p_xfb_free)(struct pvr_cmd_buffer *cmd_buffer); struct pvr_image_view; void PVR_PER_ARCH(g9p_pack_image_view)(const struct pvr_image_view *iview, uint64_t words[2]); +struct g9p_storage_image; +void PVR_PER_ARCH(g9p_storage_image_record)(const struct pvr_image_view *iview, + struct g9p_storage_image *rec); uint64_t PVR_PER_ARCH(g9p_pack_sampler)(const VkSamplerCreateInfo *info); struct pvr_sub_cmd_transfer; diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index 34ff6e96bd7..c85d2593118 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1320,8 +1320,9 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, pvr_physical_device_get_supported_features(&pdevice->dev_info, &supported_features); /* - * G9P: a texel fetch outside its level reads 0 (lower_txf()), and there - * are no storage images yet, so image accesses are robust. With robust + * G9P: a texel fetch outside its level reads 0 (lower_txf()), and a + * storage image access outside the view reads 0 or is dropped + * (lower_storage_image()), so image accesses are robust. With robust * buffer access, Zink then offers GL_KHR_robustness. */ if (PVR_HAS_FEATURE(&pdevice->dev_info, apple_g9p)) { From 88d02a6ba499eecb91999d8001ec4db10b3e8eed Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 11:27:46 -0600 Subject: [PATCH 17/44] pvr/g9p: indirect draws vkCmdDrawIndirect and vkCmdDrawIndexedIndirect fell through to Rogue's VDM index list, which the A10 cannot run: glDrawArraysIndirect lost the device. An A10 indirect draw is now recorded as a direct one, and its INDEX_LIST words are filled from its arguments when the job is submitted, as the unaligned vertex bindings are copied then: the count, instances, first vertex or vertex offset, the index list's address from firstIndex, and the first instance in the vertex shader's G9P_DRAW_PARAMS slot. Indices past the index buffer are not drawn, as in a direct draw, and a draw of no instances gets a count of 0, which draws nothing (measured, points too). The CPU reads the arguments, and waits first for the job before when the GPU may have written them. Tested on an iPhone 7 (D10) through Zink: our GLES 3.1 test's glDrawArraysIndirect and glDrawElementsIndirect, from index 3, draws of nothing, and arguments a compute shader wrote just before pass (they lost the device). The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 60 +++++++++++ src/imagination/vulkan/pvr_arch_g9p_gfx.c | 101 ++++++++++++++++++- src/imagination/vulkan/pvr_arch_queue.c | 12 +++ src/imagination/vulkan/pvr_cmd_buffer.h | 2 + src/imagination/vulkan/pvr_g9p.h | 24 +++++ 5 files changed, 198 insertions(+), 1 deletion(-) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index ebf45af36d6..de401376da4 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -207,6 +207,7 @@ static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, util_dynarray_fini(&sub_cmd->gfx.sec_query_indices); util_dynarray_fini(&sub_cmd->gfx.g9p_realigns); + util_dynarray_fini(&sub_cmd->gfx.g9p_indirects); util_dynarray_fini(&sub_cmd->gfx.g9p_fills); util_dynarray_fini(&sub_cmd->gfx.g9p_view_kicks); util_dynarray_foreach (&sub_cmd->gfx.g9p_view_bos, struct pvr_bo *, bo) @@ -3093,6 +3094,7 @@ VkResult pvr_arch_cmd_buffer_start_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, sub_cmd->gfx.sec_query_indices = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_realigns = UTIL_DYNARRAY_INIT; + sub_cmd->gfx.g9p_indirects = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_fills = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_view_kicks = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_view_bos = UTIL_DYNARRAY_INIT; @@ -9983,6 +9985,56 @@ void PVR_PER_ARCH(CmdDrawIndexed)(VkCommandBuffer commandBuffer, PVR_TRACE_END_DRAW(cmd_buffer); } +/* + * G9P: indirect draws, each recorded with its words filled from its + * arguments when the job is submitted (g9p_patch_indirect_draws()): the + * arguments are read by the CPU, which waits first for a GPU job that may + * have written them + */ +static void +g9p_draw_indirect(struct pvr_cmd_buffer *cmd_buffer, struct pvr_buffer *buffer, + VkDeviceSize offset, uint32_t count, uint32_t stride, bool indexed) +{ + struct pvr_cmd_buffer_state *state = &cmd_buffer->state; + const unsigned size = indexed ? sizeof(VkDrawIndexedIndirectCommand) + : sizeof(VkDrawIndirectCommand); + VkResult result; + + if (!state->gfx_pipeline || !count) + return; + result = pvr_flush_push_descriptors(cmd_buffer, &state->gfx_desc_state); + for (uint32_t i = 0; i < count && result == VK_SUCCESS; i++) { + const uint64_t at = offset + (uint64_t)i * stride; + const uint8_t *args = + at + size <= buffer->vk.size + ? PVR_PER_ARCH(g9p_cpu_addr)(cmd_buffer->device, buffer->dev_addr.addr + at) + : NULL; + struct pvr_g9p_draw draw = { + .indexed = indexed, + .count = 1, + .instances = 1, + .indirect = args, + .indirect_buffer = buffer, + }; + + if (!args) { + result = g9p_feature_error(cmd_buffer, "G9P: indirect draw arguments the CPU cannot read"); + break; + } + if (indexed) { + const struct pvr_buffer *ib = state->index_buffer_binding.buffer; + + draw.index_size = vk_index_type_to_bytes(state->index_buffer_binding.type); + draw.index_addr = ib->dev_addr.addr + state->index_buffer_binding.offset; + draw.index_end = ib->dev_addr.addr + ib->vk.size; + } + state->draw_state.base_instance = 0; + result = pvr_arch_g9p_cmd_draw(cmd_buffer, &draw); + } + if (result != VK_SUCCESS) + vk_command_buffer_set_error(&cmd_buffer->vk, result); +} + void PVR_PER_ARCH(CmdDrawIndexedIndirect)(VkCommandBuffer commandBuffer, VkBuffer _buffer, VkDeviceSize offset, @@ -10005,6 +10057,10 @@ void PVR_PER_ARCH(CmdDrawIndexedIndirect)(VkCommandBuffer commandBuffer, PVR_TRACE_BEGIN_DRAW(cmd_buffer, PVR_DRAW_INDEXED_INDIRECT); PVR_G5P_HOOK(g5p_indexed_indirect) + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { + g9p_draw_indirect(cmd_buffer, buffer, offset, drawCount, stride, true); + goto end_cmd_draw_indexed_indirect; + } pvr_update_draw_state(state, &draw_state); result = pvr_validate_draw_state(cmd_buffer); @@ -10049,6 +10105,10 @@ void PVR_PER_ARCH(CmdDrawIndirect)(VkCommandBuffer commandBuffer, PVR_TRACE_BEGIN_DRAW(cmd_buffer, PVR_DRAW_INDIRECT); PVR_G5P_HOOK(g5p_indirect) + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { + g9p_draw_indirect(cmd_buffer, buffer, offset, drawCount, stride, false); + goto end_cmd_draw_indirect; + } pvr_update_draw_state(state, &draw_state); result = pvr_validate_draw_state(cmd_buffer); diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index c6e6348a82f..5f4bf4d589f 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -1765,6 +1765,72 @@ binding_needs_copy(const struct pvr_vertex_binding *vb, uint32_t stride) */ /* struct g9p_realign: pvr_g9p.h (G5P's draws queue them too) */ +/* + * An indirect draw's words from its arguments: the index list's address + * from firstIndex, the counts (indices past the index buffer not drawn, as + * a direct draw), the first vertex or vertex offset, and the first + * instance. A draw of no instances gets a count of 0, which draws nothing + * (measured, points too). + */ +static void +patch_indirect(const struct g9p_indirect *p) +{ + uint32_t count, instances, offset, first_instance; + uint32_t *w = p->words; + + if (!w) + return; + if (p->indexed) { + VkDrawIndexedIndirectCommand c; + + memcpy(&c, p->args, sizeof(c)); + const uint64_t in_buffer = (p->index_end - p->index_addr) / p->index_size; + const uint64_t addr = p->index_addr + (uint64_t)c.firstIndex * p->index_size; + + count = c.firstIndex < in_buffer ? MIN2(c.indexCount, in_buffer - c.firstIndex) : 0; + instances = c.instanceCount; + offset = c.vertexOffset; + first_instance = c.firstInstance; + w[0] = (w[0] & ~0xffu) | (uint32_t)(addr >> 32); + w[1] = (uint32_t)addr; + w += 2; + } else { + VkDrawIndirectCommand c; + + memcpy(&c, p->args, sizeof(c)); + count = c.vertexCount; + instances = c.instanceCount; + offset = c.firstVertex; + first_instance = c.firstInstance; + w += 1; + } + if (!instances) + count = 0; + w[0] = count; + w[1] = MAX2(instances, 1) - 1; + w[2] = offset; + if (p->first_instance) + *p->first_instance = first_instance; +} + +void +PVR_PER_ARCH(g9p_patch_indirect_draws)(struct pvr_sub_cmd_gfx *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_indirect, p) + patch_indirect(p); +} + +/* As g9p_realign_needs_wait(): arguments a GPU job before may have written */ +bool +PVR_PER_ARCH(g9p_indirect_needs_wait)(const struct pvr_sub_cmd_gfx *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_indirect, p) { + if (p->buffer && p->buffer->g9p_gpu_written) + return true; + } + return false; +} + static void realign_copy(const struct g9p_realign *r) { @@ -2194,6 +2260,8 @@ struct g9p_draw_programs { /* the depth test (a depth attachment and the test enabled), its write and compare op */ bool depth_test, depth_write; VkCompareOp depth_compare; + /* an indirect draw: the vertex shader's G9P_DRAW_PARAMS slot, filled at submission */ + uint32_t *first_instance; }; /* @@ -2565,6 +2633,11 @@ g9p_cmd_draw_now(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_draw *d result = upload_vs_uniforms(cmd_buffer, state, &dma, &vs_block); if (result != VK_SUCCESS) return result; + /* the addresses come first in the uploaded block, two dwords each */ + for (unsigned k = 0; draw->indirect && vs_block && k < state->num_buffers; k++) { + if (state->buffers[k].set == G9P_DRAW_PARAMS) + dp.first_instance = (uint32_t *)vs_block + 2 * k; + } if (state->has_xfb) { result = xfb_draw(cmd_buffer, state, draw, vs_block, false); if (result != VK_SUCCESS) @@ -2978,11 +3051,37 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state (draw->indexed ? util_logbase2(draw->index_size) : 2u) << 17; if (draw->indexed) w[n++] = (uint32_t)draw->index_addr; + const unsigned list = n; + w[n++] = draw->count; w[n++] = MAX2(draw->instances, 1) - 1; w[n++] = draw->offset; w[n++] = 0xaa; - emit_words(csb, w, n); + if (draw->indirect) { + uint32_t *dst = pvr_arch_csb_alloc_dwords(csb, n); + const struct g9p_indirect ind = { + .words = dst ? dst + list - (draw->indexed ? 2 : 1) : NULL, + .first_instance = dp->first_instance, + .args = draw->indirect, + .buffer = draw->indirect_buffer, + .index_addr = draw->index_addr, + .index_end = draw->index_end, + .index_size = draw->index_size, + .indexed = draw->indexed, + }; + + if (!dst) + return csb->status; + memcpy(dst, w, 4 * n); + /* a secondary's render joins its primary's job: filled now */ + if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) + patch_indirect(&ind); + else + util_dynarray_append_typed(&cs->current_sub_cmd->gfx.g9p_indirects, + struct g9p_indirect, ind); + } else { + emit_words(csb, w, n); + } } /* * Multiview: where this draw's kick word ended up (the whole draw sits in the current diff --git a/src/imagination/vulkan/pvr_arch_queue.c b/src/imagination/vulkan/pvr_arch_queue.c index 0baa9e090de..545c958a3e5 100644 --- a/src/imagination/vulkan/pvr_arch_queue.c +++ b/src/imagination/vulkan/pvr_arch_queue.c @@ -648,6 +648,18 @@ static VkResult pvr_process_graphics_cmd(struct pvr_device *device, const pvr_dev_addr_t ds_addr = sub_cmd->job.ds.addr; PVR_G5P_HOOK(xfb_before_job, device, cmd_buffer, sub_cmd) + if (util_dynarray_num_elements(&sub_cmd->g9p_indirects, char)) { + struct vk_sync *wait = queue->next_job_wait_sync[PVR_JOB_TYPE_GEOM]; + + if (wait && PVR_PER_ARCH(g9p_indirect_needs_wait)(sub_cmd)) { + VkResult result = vk_sync_wait(&device->vk, wait, 0, VK_SYNC_WAIT_COMPLETE, UINT64_MAX); + + if (result != VK_SUCCESS) + return result; + } + PVR_PER_ARCH(g9p_patch_indirect_draws)(sub_cmd); + } + if (util_dynarray_num_elements(&sub_cmd->g9p_realigns, char)) { struct vk_sync *wait = queue->next_job_wait_sync[PVR_JOB_TYPE_GEOM]; diff --git a/src/imagination/vulkan/pvr_cmd_buffer.h b/src/imagination/vulkan/pvr_cmd_buffer.h index 622d85ea9b9..8c9b6eaae46 100644 --- a/src/imagination/vulkan/pvr_cmd_buffer.h +++ b/src/imagination/vulkan/pvr_cmd_buffer.h @@ -187,6 +187,8 @@ struct pvr_sub_cmd_gfx { /* G9P: vertex bindings to copy when the job is submitted */ struct util_dynarray g9p_realigns; + /* G9P: indirect draws to fill when the job is submitted (struct g9p_indirect) */ + struct util_dynarray g9p_indirects; /* G9P: clears of targets the fragment program writes to memory (struct g9p_fill) */ struct util_dynarray g9p_fills; /* diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index b8da4722306..38ba27c48e5 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -17,6 +17,7 @@ #include "util/log.h" #include "vk_log.h" +struct pvr_buffer; struct pvr_cmd_buffer; struct pvr_compute_pipeline; struct pvr_device; @@ -51,6 +52,25 @@ struct pvr_g9p_draw { uint64_t index_addr; unsigned index_size; uint32_t count, instances, offset; + /* an indirect draw: its arguments (CPU), from @indirect_buffer; the index buffer's end */ + const uint8_t *indirect; + struct pvr_buffer *indirect_buffer; + uint64_t index_end; +}; + +/* + * An indirect draw's INDEX_LIST, filled from its arguments when its job is + * submitted (pvr_arch_g9p_gfx.c): the A10's VDM is given the counts in its + * words + */ +struct g9p_indirect { + uint32_t *words; /* INDEX_LIST's first word */ + uint32_t *first_instance; /* the vertex shader's G9P_DRAW_PARAMS slot, or NULL */ + const uint8_t *args; + struct pvr_buffer *buffer; /* where the arguments are, if the GPU may write them */ + uint64_t index_addr, index_end; + unsigned index_size; + bool indexed; }; #ifdef PVR_PER_ARCH @@ -68,6 +88,10 @@ struct pvr_sub_cmd_gfx; void PVR_PER_ARCH(g9p_run_fills)(struct pvr_device *device, struct pvr_sub_cmd_gfx *sub_cmd); void PVR_PER_ARCH(g9p_device_finish)(struct pvr_device *device); +/* Fill a render's indirect draws from their arguments; before its job is submitted */ +void PVR_PER_ARCH(g9p_patch_indirect_draws)(struct pvr_sub_cmd_gfx *sub_cmd); +bool PVR_PER_ARCH(g9p_indirect_needs_wait)(const struct pvr_sub_cmd_gfx *sub_cmd); + /* Copy a render's unaligned vertex bindings; before its job is submitted */ void PVR_PER_ARCH(g9p_realign_vertex_bindings)(struct pvr_sub_cmd_gfx *sub_cmd); bool PVR_PER_ARCH(g9p_realign_needs_wait)(const struct pvr_sub_cmd_gfx *sub_cmd); From 790e3cf9521c9e1688b6cc9d014d0c849f2e2ef4 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 11:31:24 -0600 Subject: [PATCH 18/44] pvr/g9p: textureGather of 2D textures and arrays The A10 backend refused tg4 ("only plain 2D samples of combined image samplers so far"), and GLES 3.1 has textureGather, textureGatherOffset and textureGatherOffsets, with a depth comparison form. A gather of a 2D texture or array is now four samples at level 0, at the centres of the texels of the bilinear footprint, in the order (i0, j1), (i1, j1), (i1, j0), (i0, j0), each giving the gathered component. The sampler's wrap applies to texels outside the level, a linear filter at a texel's centre reads that texel alone, and with a shadow sampler each sample is that texel's comparison. An offset moves the footprint, and textureGatherOffsets takes each texel from its own. Cube maps are still refused. Tested on an iPhone 7 (D10) through Zink: our GLES 3.1 test's textureGather of each component, textureGatherOffset with (-1, 1) and the depth comparison form pass. The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 75 +++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index d2b1948fcee..a16f2fca1a2 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5258,6 +5258,79 @@ lower_image_packed_load(nir_builder *b, nir_intrinsic_instr *in, void *data) return true; } +/* + * textureGather (tg4) of 2D textures and arrays: the four texels of the + * bilinear footprint, each sampled at its centre at level 0, in the order + * (i0, j1), (i1, j1), (i1, j0), (i0, j0). The sampler's wrap applies to + * texels outside the level, a linear filter at a texel's centre reads that + * texel alone, and a shadow sampler compares each one. + */ +static bool +lower_tg4(nir_builder *b, nir_tex_instr *tex, void *data) +{ + static const int corner[4][2] = { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 0, 0 } }; + const int ci = nir_tex_instr_src_index(tex, nir_tex_src_coord); + const int oi = nir_tex_instr_src_index(tex, nir_tex_src_offset); + + if (tex->op != nir_texop_tg4 || tex->sampler_dim != GLSL_SAMPLER_DIM_2D || ci < 0) + return false; + b->cursor = nir_before_instr(&tex->instr); + + nir_def *coord = tex->src[ci].src.ssa; + nir_def *size = nir_i2f32(b, nir_get_texture_size(b, tex)); + nir_def *x = nir_fadd_imm(b, nir_fmul(b, nir_channel(b, coord, 0), nir_channel(b, size, 0)), -0.5); + nir_def *y = nir_fadd_imm(b, nir_fmul(b, nir_channel(b, coord, 1), nir_channel(b, size, 1)), -0.5); + nir_def *i0 = nir_ffloor(b, x), *j0 = nir_ffloor(b, y); + nir_def *texel[4]; + + if (oi >= 0) { + nir_def *off = nir_i2f32(b, tex->src[oi].src.ssa); + + i0 = nir_fadd(b, i0, nir_channel(b, off, 0)); + j0 = nir_fadd(b, j0, nir_channel(b, off, 1)); + } + for (unsigned k = 0; k < 4; k++) { + int di = corner[k][0], dj = corner[k][1]; + unsigned n = 0; + + if (nir_tex_instr_has_explicit_tg4_offsets(tex)) { + /* textureGatherOffsets: each texel's own footprint, its (i0, j0) */ + di = tex->tg4_offsets[k][0]; + dj = tex->tg4_offsets[k][1]; + } + + nir_def *cx = nir_fdiv(b, nir_fadd_imm(b, i0, di + 0.5), nir_channel(b, size, 0)); + nir_def *cy = nir_fdiv(b, nir_fadd_imm(b, j0, dj + 0.5), nir_channel(b, size, 1)); + nir_def *comps[3] = { cx, cy, tex->is_array ? nir_channel(b, coord, 2) : NULL }; + nir_tex_instr *s = nir_tex_instr_create(b->shader, tex->num_srcs + 1); + + for (unsigned i = 0; i < tex->num_srcs; i++) { + if (tex->src[i].src_type == nir_tex_src_coord || tex->src[i].src_type == nir_tex_src_offset) + continue; + s->src[n].src_type = tex->src[i].src_type; + s->src[n++].src = nir_src_for_ssa(tex->src[i].src.ssa); + } + s->src[n].src_type = nir_tex_src_coord; + s->src[n++].src = nir_src_for_ssa(nir_vec(b, comps, tex->is_array ? 3 : 2)); + s->src[n].src_type = nir_tex_src_lod; + s->src[n++].src = nir_src_for_ssa(nir_imm_float(b, 0.0f)); + s->num_srcs = n; + s->op = nir_texop_txl; + s->sampler_dim = tex->sampler_dim; + s->is_array = tex->is_array; + s->is_shadow = tex->is_shadow; + s->coord_components = tex->is_array ? 3 : 2; + s->dest_type = tex->dest_type; + s->texture_index = tex->texture_index; + s->sampler_index = tex->sampler_index; + nir_def_init(&s->instr, &s->def, 4, tex->def.bit_size); + nir_builder_instr_insert(b, &s->instr); + texel[k] = nir_channel(b, &s->def, tex->is_shadow ? 0 : tex->component); + } + nir_def_replace(&tex->def, nir_vec(b, texel, 4)); + return true; +} + static bool lower_txf(nir_builder *b, nir_tex_instr *tex, void *data) { @@ -5420,6 +5493,8 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, /* texture2DProj */ { + if (!out->a9) + NIR_PASS(_, nir, nir_shader_tex_pass, lower_tg4, nir_metadata_control_flow, NULL); const nir_lower_tex_options tex = { .lower_txp = ~0u, .lower_txs_lod = true, From 846d49187f8c88ffd7c0d366d908f62a6d6dca00 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 11:37:48 -0600 Subject: [PATCH 19/44] pvr/g9p: compare-and-swap, and image atomics without a branch The A10 backend had no ssbo_atomic_swap, so atomicCompSwap and imageAtomicCompSwap failed to compile. The A10 now uses the A9's compare-and-swap loop (g5p_device_cas()): each lane in turn loads the word, compares it and stores the new value. An image atomic outside the view was in a branch whose phi held a register for the whole program, and shaders with several image atomics ran out of registers. It now goes to the view's first texel with an operand that changes nothing there (0 for add, or and xor, all ones for and and umin, the extremes for min and max, the compared value as the new one for compare-and-swap). Only exchange keeps the branch. The result is undefined outside the view either way. Tested on an iPhone 7 (D10) through Zink: a contended atomicCompSwap loop on a storage buffer counts every invocation (16 and 128), and a compare that fails leaves the word. On an r32ui image, single-level (linear) and with two levels (twiddled), 128 invocations' imageAtomicAdd over four texels, a compare-and-swap loop, imageAtomicMax and imageAtomicMin give exact results, and atomics outside the image change nothing. The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 79 +++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index a16f2fca1a2..36c1e402b7a 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -3434,6 +3434,36 @@ emit_intrinsic(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) return true; } + case nir_intrinsic_ssbo_atomic_swap: { + /* + * Compare-and-swap, which has no device atomic of its own: the A9's + * loop (g5p_device_cas()), each lane in turn loading the word, + * comparing it and storing the new value + */ + if (ctx->stage != MESA_SHADER_COMPUTE || intr->def.bit_size != 32 || + nir_intrinsic_atomic_op(intr) != nir_atomic_op_cmpxchg) { + fprintf(stderr, "g9p: ssbo compare-and-swap in stage %d not supported yet\n", ctx->stage); + return false; + } + const int slot = buffer_slot(ctx, intr->src[0]); + const int off = src_reg(ctx, nir_get_scalar(intr->src[1].ssa, 0)); + const int cmp = src_reg(ctx, nir_get_scalar(intr->src[2].ssa, 0)); + const int val = src_reg(ctx, nir_get_scalar(intr->src[3].ssa, 0)); + const int pair = alloc_pair(ctx), i = alloc_temp(ctx), dst = alloc_reg(ctx); + + if (slot < 0 || off < 0 || cmp < 0 || val < 0 || pair < 0 || i < 0 || dst < 0 || + pair > 30 || dst > 31 || i > 31) + return false; + ctx->temps |= BITFIELD64_RANGE(pair, 2); + g9p_addr(ctx->c, pair, 2 * slot, off, 0, false); + g5p_device_cas(ctx->c, pair, dst, cmp, val, i, ctx->st); + release(ctx, &intr->instr); + ctx->reg[key(&intr->def, 0)] = dst; + ctx->busy &= ~ctx->temps; + ctx->temps = 0; + return true; + } + case nir_intrinsic_get_ssbo_size: case nir_intrinsic_get_ubo_size: { /* @@ -6264,15 +6294,46 @@ lower_storage_image(nir_builder *b, nir_intrinsic_instr *in, void *data) *failed = true; return false; } - nir_push_if(b, inside); - if (in->intrinsic == nir_intrinsic_image_deref_atomic_swap) - r = nir_ssbo_atomic_swap(b, 32, texels, off, in->src[3].ssa, in->src[4].ssa, - .atomic_op = nir_intrinsic_atomic_op(in)); - else - r = nir_ssbo_atomic(b, 32, texels, off, in->src[3].ssa, - .atomic_op = nir_intrinsic_atomic_op(in)); - nir_pop_if(b, NULL); - nir_def_replace(&in->def, nir_if_phi(b, r, zero)); + /* + * Outside the view, the first texel with an operand that changes + * nothing (the result is undefined there): no branch, whose phi would + * hold a register for the whole program. Exchange has none. + */ + const nir_atomic_op op = nir_intrinsic_atomic_op(in); + nir_def *data = in->src[3].ssa, *keep; + + switch (op) { + case nir_atomic_op_iadd: case nir_atomic_op_ior: case nir_atomic_op_ixor: + case nir_atomic_op_umax: + keep = nir_imm_int(b, 0); + break; + case nir_atomic_op_iand: case nir_atomic_op_umin: + keep = nir_imm_int(b, ~0); + break; + case nir_atomic_op_imin: + keep = nir_imm_int(b, INT32_MAX); + break; + case nir_atomic_op_imax: + keep = nir_imm_int(b, INT32_MIN); + break; + default: + keep = NULL; + break; + } + if (in->intrinsic == nir_intrinsic_image_deref_atomic_swap) { + /* storing the compared value back changes nothing */ + r = nir_ssbo_atomic_swap(b, 32, texels, nir_bcsel(b, inside, off, zero), data, + nir_bcsel(b, inside, in->src[4].ssa, data), .atomic_op = op); + } else if (keep) { + r = nir_ssbo_atomic(b, 32, texels, nir_bcsel(b, inside, off, zero), + nir_bcsel(b, inside, data, keep), .atomic_op = op); + } else { + nir_push_if(b, inside); + r = nir_ssbo_atomic(b, 32, texels, off, data, .atomic_op = op); + nir_pop_if(b, NULL); + r = nir_if_phi(b, r, zero); + } + nir_def_replace(&in->def, r); return true; } if (in->intrinsic == nir_intrinsic_image_deref_store) { From 5a57f04db48ecc06ba7da2df6a2572aa2dbb2bd5 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 11:56:16 -0600 Subject: [PATCH 20/44] pvr/g9p: framebuffer fetch through local reads, for advanced blending GL_KHR_blend_equation_advanced was missing: Zink offers framebuffer fetch, and with it the advanced blend modes, when the device has VK_KHR_dynamic_rendering_local_read, and G9P had no input attachments. A fragment shader's input attachment load now reads colour attachment i from the tile for input attachment i, as blending already does (lower_fb_fetch_instr() decodes the target's words), and the shader runs in the pass type of a shader that reads the tile. The A10 offers VK_KHR_dynamic_rendering_local_read with dynamicRenderingLocalRead. A fragment-to-fragment barrier inside a render no longer adds Rogue's transparent object, which is not an A10 draw: the tile reads see the draws before them in order. Tested on an iPhone 7 (D10) through Zink: our GLES 3.2 test passes, all 15 KHR_blend_equation_advanced modes within 1/255 of the formulas (the extension was missing). The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 46 +++++++++++++++++++- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 8 ++++ src/imagination/vulkan/pvr_physical_device.c | 7 +++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 36c1e402b7a..2ba7981d4fb 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5288,6 +5288,38 @@ lower_image_packed_load(nir_builder *b, nir_intrinsic_instr *in, void *data) return true; } +/* + * Framebuffer fetch through input attachments (VK_KHR_dynamic_rendering_local_read, + * Zink's fbfetch): a subpass load of input attachment i reads colour attachment + * i from the tile, as blending does (lower_fb_fetch_instr()); @data: the outputs' + * bases by colour attachment + */ +static bool +lower_subpass_load(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + const int *base = data; + + if (intr->intrinsic != nir_intrinsic_image_deref_input_attachment_load || + nir_intrinsic_image_dim(intr) != GLSL_SAMPLER_DIM_SUBPASS) + return false; + /* the input attachment index; the identity map to colour attachments */ + const unsigned i = nir_intrinsic_base(intr); + + b->cursor = nir_before_instr(&intr->instr); + if (i >= G9P_MAX_TARGETS || base[i] < 0) { + nir_def_replace(&intr->def, nir_imm_zero(b, intr->def.num_components, intr->def.bit_size)); + return true; + } + nir_def *v = nir_load_output(b, 4, 32, nir_imm_int(b, 0), .base = base[i], .component = 0, + .dest_type = nir_intrinsic_dest_type(intr), + .io_semantics.location = FRAG_RESULT_DATA0 + i, + .io_semantics.num_slots = 1); + + nir_def_replace(&intr->def, nir_trim_vector(b, v, intr->def.num_components)); + b->shader->info.fs.uses_fbfetch_output = true; + return true; +} + /* * textureGather (tg4) of 2D textures and arrays: the four texels of the * bilinear footprint, each sampled at its centre at level 0, in the order @@ -7138,7 +7170,19 @@ g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, NIR_PASS(_, nir, discards_to_flag); NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_centroid, nir_metadata_control_flow, NULL); find_flat_varyings(nir, out); - lower_io(nir); + { + /* the colour outputs present: lower_io() makes a location the base */ + int base[G9P_MAX_TARGETS] = { -1, -1, -1, -1 }; + + nir_foreach_shader_out_variable(var, nir) { + if (var->data.location >= FRAG_RESULT_DATA0 && + var->data.location < FRAG_RESULT_DATA0 + G9P_MAX_TARGETS) + base[var->data.location - FRAG_RESULT_DATA0] = var->data.location; + } + lower_io(nir); + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_subpass_load, nir_metadata_control_flow, + base); + } NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_overflow_target, nir_metadata_control_flow, (void *)targets); if (coverage) lower_coverage(nir, coverage); diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index de401376da4..3c3b45f4bbb 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -11215,6 +11215,14 @@ void PVR_PER_ARCH(CmdPipelineBarrier2)(VkCommandBuffer commandBuffer, if (!render_pass) break; + /* + * G9P: a fragment shader's tile reads see the draws before it in + * order (lower_subpass_load()); Rogue's transparent object is not an + * A10 draw + */ + if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) + break; + assert(current_sub_cmd->type == PVR_SUB_CMD_TYPE_GRAPHICS); /* Flush all fragment work up to this point. */ diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index c85d2593118..d7d44f9707e 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -1328,6 +1328,13 @@ VkResult pvr_physical_device_init(struct pvr_physical_device *pdevice, if (PVR_HAS_FEATURE(&pdevice->dev_info, apple_g9p)) { supported_extensions.EXT_image_robustness = true; supported_features.robustImageAccess = true; + /* + * G9P: a subpass load reads the colour attachment from the tile, as + * blending does (lower_subpass_load()), in the order the draws were + * given, so Zink offers framebuffer fetch and advanced blending + */ + supported_extensions.KHR_dynamic_rendering_local_read = true; + supported_features.dynamicRenderingLocalRead = true; } if (!pvr_physical_device_get_properties(pdevice, &supported_properties)) { result = vk_errorf(instance, From 8b5ba19ebc16125b879a9d7ad42241645b27817c Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 12:36:43 -0600 Subject: [PATCH 21/44] pvr/g9p: fragment uniform arrays indexed at run time, as a tree A fragment shader reads its uniform buffers from the common store, so a load at a computed offset was a chain of selects over every dword of the buffer, up to 512 a component. A vec4 array of 64 or 100 entries ran out of registers ("out of registers above r31"), and GL's large uniform arrays indexed per pixel failed to compile. The loads' alignments are first updated from what the offsets prove (nir_opt_load_store_update_alignments()), so a component of a vec4 array reaches one dword in four. The dwords a load can reach are then chosen by the bits of the entry number, a binary tree built depth first, which keeps a few partial results live where the chain kept the whole buffer (each component's chain shared its loads with the others). An offset past them reads the same component of the buffer's first vec4, as before. Tested on an iPhone 7 (D10) through Zink: our uniform test passes, a uniform block of 64 vec4s and a default-uniform array of 100 indexed per pixel exact on all 1024 pixels (they failed to compile), and our robustness test's fragment read far past a block still reads its first entry. The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 80 +++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 2ba7981d4fb..fb86569f1f2 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5021,40 +5021,68 @@ rematerialize_loads(nir_shader *nir, bool every_use) * operands with a fixed index: a load at a computed offset becomes a chain of * selects over the offsets it can have (GL's uniform arrays indexed in a loop) */ +/* A dword of a fragment shader's buffer at a constant offset (the common store) */ +static nir_def * +ubo_dword(nir_builder *b, nir_intrinsic_instr *intr, unsigned at) +{ + return nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, at), .align_mul = 4, + .align_offset = 0, .range_base = nir_intrinsic_range_base(intr), + .range = nir_intrinsic_range(intr)); +} + +/* + * Entries @lo .. @lo + @size - 1 of @count, at @first + @step x entry, + * chosen by the bits of @j below @size: a binary tree built depth first, so + * a few partial results are live at once. Entries past @count read entry 0. + */ +static nir_def * +ubo_select_tree(nir_builder *b, nir_intrinsic_instr *intr, nir_def *j, unsigned lo, + unsigned size, unsigned count, unsigned first, unsigned step) +{ + if (size == 1) + return ubo_dword(b, intr, first + step * (lo < count ? lo : 0)); + + nir_def *low = ubo_select_tree(b, intr, j, lo, size / 2, count, first, step); + nir_def *high = ubo_select_tree(b, intr, j, lo + size / 2, size / 2, count, first, step); + + return nir_bcsel(b, nir_ine_imm(b, nir_iand_imm(b, j, size / 2), 0), high, low); +} + +/* + * A fragment shader reads its buffers from the common store, as uniform + * operands with a fixed index: a load at a computed offset becomes a + * selection among the dwords it can reach, those its alignment allows + * (one component of a vec4 array: every fourth), by the bits of its entry + * number. An offset past them reads the same component of the buffer's + * first vec4, as robust access gives the vector loads of other stages. + */ static bool lower_dynamic_ubo_instr(nir_builder *b, nir_intrinsic_instr *intr, void *data) { - nir_def *value; - unsigned n; - if (intr->intrinsic != nir_intrinsic_load_ubo || nir_src_is_const(intr->src[1]) || intr->def.num_components != 1 || intr->def.bit_size != 32) return false; - n = MIN2(nir_intrinsic_range(intr) / 4, G9P_FS_BUF_DWORDS); + + const unsigned end = nir_intrinsic_range_base(intr) + + 4 * MIN2(nir_intrinsic_range(intr) / 4, G9P_FS_BUF_DWORDS); + const unsigned step = MAX2(nir_intrinsic_align_mul(intr), 4); + const unsigned phase = nir_intrinsic_align_offset(intr) % step; + /* the first reachable dword at or after the range's start */ + const unsigned first = phase + ALIGN_POT(MAX2(nir_intrinsic_range_base(intr), phase) - phase, step); + const unsigned count = end > first ? DIV_ROUND_UP(end - first, step) : 1; + b->cursor = nir_before_instr(&intr->instr); - /* - * An offset past them reads the same component of the buffer's first - * vec4, as robust access gives the vector loads of other stages - */ - value = nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, 0), .align_mul = 4, - .align_offset = 0, .range_base = 0, .range = 16); - for (unsigned c = 1; c < 4; c++) { - nir_def *first = nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, 4 * c), - .align_mul = 4, .align_offset = 0, .range_base = 0, - .range = 16); + /* the entry: an offset below the first wraps past count */ + nir_def *j = nir_udiv_imm(b, nir_iadd_imm(b, intr->src[1].ssa, -(int)first), step); + nir_def *value = ubo_select_tree(b, intr, j, 0, util_next_power_of_two(count), count, first, step); - value = nir_bcsel(b, nir_ieq_imm(b, nir_iand_imm(b, intr->src[1].ssa, 12), 4 * c), first, - value); - } - for (unsigned k = 0; k < n; k++) { - const unsigned at = nir_intrinsic_range_base(intr) + 4 * k; - nir_def *load = nir_load_ubo(b, 1, 32, intr->src[0].ssa, nir_imm_int(b, at), - .align_mul = 4, .align_offset = 0, - .range_base = nir_intrinsic_range_base(intr), - .range = nir_intrinsic_range(intr)); + /* past them: the same component of the first vec4, whatever the alignment says */ + nir_def *oob = ubo_dword(b, intr, nir_intrinsic_range_base(intr)); - value = nir_bcsel(b, nir_ieq_imm(b, intr->src[1].ssa, at), load, value); - } + for (unsigned c = 1; c < 4; c++) + oob = nir_bcsel(b, nir_ieq_imm(b, nir_iand_imm(b, intr->src[1].ssa, 12), 4 * c), + ubo_dword(b, intr, nir_intrinsic_range_base(intr) + 4 * c), oob); + value = nir_bcsel(b, nir_ult_imm(b, j, count), value, oob); nir_def_replace(&intr->def, value); return true; } @@ -5619,6 +5647,8 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, optimize(nir); } if (stage == MESA_SHADER_FRAGMENT) { + /* the alignments the offsets prove: the dwords a computed offset can reach */ + NIR_PASS(_, nir, nir_opt_load_store_update_alignments); NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_dynamic_ubo_instr, nir_metadata_control_flow, NULL); optimize(nir); From 2c85fa6fbc5382a999cc3da16af3933184e232af Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 12:52:13 -0600 Subject: [PATCH 22/44] pvr/g9p: a 16-byte aligned stencil plane for D32F_S8 D32F_S8's stencil plane starts at half the image's size, after the depth plane's chain of levels. A chain ends at a 4-byte 1x1 level, so the plane could start 4 bytes past a 16-byte boundary, and the ZLS's stencil base is 16-byte aligned (ISP_STENCIL_LOAD_BASE): a render stored its stencil from 4 bytes earlier, over the 1x1 depth level. A 64x64 chain of 7 levels lost its last level whenever level 0 or 1 was rendered. The depth plane's half is now rounded up to 64 bytes. The ZLS, the sampler and the transfers all take the stencil plane at half the image's size, so they move together. Tested on an iPhone 7 (D10) through Zink: our depth mip test's D24S8 and D32FS8 chains (D32F_S8 in Zink) keep every other level when levels 0 and 1 are rendered (the 1x1 level read 0 before). The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_image.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/imagination/vulkan/pvr_image.c b/src/imagination/vulkan/pvr_image.c index 3c65d2dc4ff..5b7ed717c9d 100644 --- a/src/imagination/vulkan/pvr_image.c +++ b/src/imagination/vulkan/pvr_image.c @@ -311,12 +311,17 @@ static void pvr_image_setup_mip_levels(struct pvr_image *image) } image->total_size = offset; - /* G9P: the stencil plane of D32F_S8 (pvr_image_plane_setup_mip_levels()) */ + /* + * G9P: the stencil plane of D32F_S8 (pvr_image_plane_setup_mip_levels()), + * at an aligned offset: the ZLS's stencil base is 16-byte aligned, and a + * chain of levels ends at a 4-byte one (its stencil stored over the depth + * plane's 1x1 level) + */ if (PVR_HAS_FEATURE(&container_of(image->vk.base.device, struct pvr_device, vk) ->pdevice->dev_info, apple_g9p) && image->vk.format == VK_FORMAT_D32_SFLOAT_S8_UINT) - image->total_size *= 2; + image->total_size = align64(image->total_size, 64) * 2; } static unsigned get_pbe_stride_align(const struct pvr_device_info *dev_info); From 54268805712e755a623851cadc16fcc063fa1a30 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 12:58:49 -0600 Subject: [PATCH 23/44] pvr/g9p: D32F_S8 levels under 32 pixels through a scratch plane A stencil plane takes ISP_ZLS_PIXELS aligned to 32, so the ZLS stores whole tiles, and a level of a chain with a side under 32 pixels would be stored past its end, over the next levels. Such levels were refused, and GL's depth/stencil textures (D24S8 and D32F_S8, both D32F_S8 in Zink) could not be rendered below 32 pixels. A D32F_S8 level of a chain with a side under 32 pixels now renders in a scratch plane of whole tiles, as iOS does: a CPU transfer copies the level's depth and stencil into it before the render when the render loads them, and back after the render when it stores them, both serialized with the fragment jobs. The device allocates the plane, 16384 x 32 pixels of depth and stencil, at its first use and registers it for the CPU transfers. Other stencil formats are still refused below 32 pixels. Tested on an iPhone 7 (D10) through Zink: our depth mip test's D24S8 and D32FS8 chains of 64x64 (7 levels) and 48x40 (6), the D24S8 2D array of 32x32x3 and the D24S8 cube map of 16x16 pass, every level down to 1x1 with its stencil, each render leaving the other levels as they were. The 64x16 chains are right but for one pixel of the stencil check at their 2x1 level. The other GPU tests are unchanged. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 79 ++++++++++++++++++++ src/imagination/vulkan/pvr_arch_g9p.c | 36 +++++++++ src/imagination/vulkan/pvr_arch_g9p_gfx.c | 11 ++- src/imagination/vulkan/pvr_blit.h | 9 +++ src/imagination/vulkan/pvr_device.h | 9 +++ src/imagination/vulkan/pvr_g9p.h | 4 + src/imagination/vulkan/pvr_job_render.h | 7 ++ src/imagination/vulkan/rogue/pvr_blit.c | 37 +++++++++ 8 files changed, 189 insertions(+), 3 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index 3c3b45f4bbb..a15d766d76a 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -1495,6 +1495,80 @@ static bool pvr_sub_cmd_gfx_requires_ds_subtile_alignment( return true; } +/* + * G9P: a D32F_S8 level of a chain with a side under 32 pixels. Its stencil + * plane takes ZLS_PIXELS aligned to 32 (g9p_zls_pixels()), which would + * store whole tiles past the level, so the render loads and stores a + * scratch plane of whole tiles instead (as iOS), copied in before it when + * it loads and back after it when it stores, by CPU transfers serialized + * with the fragment jobs, so renders take the scratch plane one at a time. + */ +static VkResult +g9p_ds_scratch(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx *const gfx_sub_cmd) +{ + struct pvr_sub_cmd *const prev_sub_cmd = container_of(gfx_sub_cmd, struct pvr_sub_cmd, gfx); + struct pvr_ds_attachment *const ds = &gfx_sub_cmd->job.ds; + const struct pvr_image *const image = pvr_image_view_get_image(ds->iview); + const struct pvr_image_plane *const plane = pvr_single_plane_const(image); + const uint32_t level = ds->iview->vk.base_mip_level; + const uint32_t layer = ds->iview->vk.base_array_layer; + const uint32_t pw = u_minify(plane->physical_extent.width, level); + const uint32_t ph = u_minify(plane->physical_extent.height, level); + const uint32_t sbw = MAX2(pw, 32), sbh = MAX2(ph, 32); + const uint64_t stencil = (uint64_t)sbw * sbh * 4; + VkResult result; + + ds->g9p_scratch = 0; + if (image->vk.format != VK_FORMAT_D32_SFLOAT_S8_UINT || image->vk.mip_levels == 1 || + (pw >= 32 && ph >= 32) || !pvr_ds_attachment_requires_zls(ds)) + return VK_SUCCESS; + + const uint64_t scratch = PVR_PER_ARCH(g9p_zls_scratch)(cmd_buffer->device); + + if (!scratch || stencil + (uint64_t)sbw * sbh > G9P_ZLS_SCRATCH_SIZE) + return g9p_feature_error(cmd_buffer, "G9P: no scratch plane for a %ux%u stencil level", pw, ph); + + if (ds->load.d || ds->load.s) { + struct pvr_sub_cmd *new_sub_cmd; + + cmd_buffer->state.current_sub_cmd = NULL; + result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_TRANSFER); + if (result != VK_SUCCESS) + return result; + new_sub_cmd = cmd_buffer->state.current_sub_cmd; + result = pvr_g9p_ds_scratch_transfer(cmd_buffer, image, level, layer, scratch, sbw, sbh, + stencil, true); + if (result != VK_SUCCESS) + return result; + new_sub_cmd->transfer.serialize_with_frag = true; + result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + if (result != VK_SUCCESS) + return result; + /* before the render (list_move_to() places it after the entry given) */ + list_move_to(&new_sub_cmd->link, prev_sub_cmd->link.prev); + cmd_buffer->state.current_sub_cmd = prev_sub_cmd; + } + if (ds->store.d || ds->store.s) { + cmd_buffer->state.current_sub_cmd = NULL; + result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_TRANSFER); + if (result != VK_SUCCESS) + return result; + result = pvr_g9p_ds_scratch_transfer(cmd_buffer, image, level, layer, scratch, sbw, sbh, + stencil, false); + if (result != VK_SUCCESS) + return result; + /* after the render's fragment job */ + cmd_buffer->state.current_sub_cmd->transfer.serialize_with_frag = true; + result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + if (result != VK_SUCCESS) + return result; + cmd_buffer->state.current_sub_cmd = prev_sub_cmd; + } + ds->g9p_scratch = scratch; + ds->g9p_scratch_stencil = stencil; + return VK_SUCCESS; +} + static VkResult pvr_sub_cmd_gfx_align_ds_subtiles(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx *const gfx_sub_cmd) @@ -2131,6 +2205,11 @@ static VkResult pvr_sub_cmd_gfx_job_init(const struct pvr_device_info *dev_info, if (result != VK_SUCCESS) return result; } + if (PVR_HAS_FEATURE(dev_info, apple_g9p)) { + result = g9p_ds_scratch(cmd_buffer, sub_cmd); + if (result != VK_SUCCESS) + return result; + } } } else { job->has_depth_attachment = false; diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 4e936d851f2..48adf438725 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -51,6 +51,7 @@ PVR_PER_ARCH(g9p_device_init)(struct pvr_device *device) VkResult result; simple_mtx_init(&device->g9p_vmas.lock, mtx_plain); + simple_mtx_init(&device->g9p.zls_scratch.lock, mtx_plain); util_dynarray_init(&device->g9p_vmas.vmas, NULL); for (unsigned i = 0; i < ARRAY_SIZE(empty); i++) @@ -111,10 +112,45 @@ PVR_PER_ARCH(g9p_device_finish)(struct pvr_device *device) pvr_bo_suballoc_free(device->g9p.fence.pvr_bo); pvr_bo_suballoc_free(device->g9p.kick.pvr_bo); pvr_bo_suballoc_free(device->g9p.empty_kernel); + if (device->g9p.zls_scratch.bo) { + util_dynarray_delete_unordered(&device->g9p_vmas.vmas, struct pvr_winsys_vma *, + device->g9p.zls_scratch.bo->vma); + pvr_bo_free(device, device->g9p.zls_scratch.bo); + } + simple_mtx_destroy(&device->g9p.zls_scratch.lock); util_dynarray_fini(&device->g9p_vmas.vmas); simple_mtx_destroy(&device->g9p_vmas.lock); } +/* + * The ZLS scratch plane (device->g9p.zls_scratch): its device address, 0 if + * it cannot be had. Whole tiles of a level with a side under 32 pixels: 32 + * on that side, at most 16384 on the other, 4 bytes of depth and 1 of + * stencil a pixel. + */ +uint64_t +PVR_PER_ARCH(g9p_zls_scratch)(struct pvr_device *device) +{ + uint64_t addr = 0; + + simple_mtx_lock(&device->g9p.zls_scratch.lock); + if (!device->g9p.zls_scratch.bo) { + struct pvr_bo *bo; + + if (pvr_bo_alloc(device, device->heaps.general_heap, G9P_ZLS_SCRATCH_SIZE, 16384, + PVR_BO_ALLOC_FLAG_CPU_MAPPED, &bo) == VK_SUCCESS) { + device->g9p.zls_scratch.bo = bo; + simple_mtx_lock(&device->g9p_vmas.lock); + util_dynarray_append_typed(&device->g9p_vmas.vmas, struct pvr_winsys_vma *, bo->vma); + simple_mtx_unlock(&device->g9p_vmas.lock); + } + } + if (device->g9p.zls_scratch.bo) + addr = device->g9p.zls_scratch.bo->vma->dev_addr.addr; + simple_mtx_unlock(&device->g9p.zls_scratch.lock); + return addr; +} + VkResult PVR_PER_ARCH(g9p_compute_pipeline_compile)( struct pvr_device *device, diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 5f4bf4d589f..be0f48b7071 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -3676,12 +3676,14 @@ g9p_ds_check(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwse "G9P: depth/stencil format %s", vk_Format_to_str(iview->vk.format)); /* * A stencil plane takes ZLS_PIXELS aligned to 32 (g9p_zls_pixels()): in a - * chain of levels one under 32 pixels would be stored past its end + * chain of levels one under 32 pixels would be stored past its end. D32F_S8 + * renders such a level in a scratch plane (g9p_ds_scratch()). */ const struct pvr_image *image = pvr_image_view_get_image(iview); const unsigned level = iview->vk.base_mip_level; - if (vk_format_has_stencil(iview->vk.format) && image->vk.mip_levels > 1 && + if (vk_format_has_stencil(iview->vk.format) && iview->vk.format != VK_FORMAT_D32_SFLOAT_S8_UINT && + image->vk.mip_levels > 1 && (u_minify(plane->physical_extent.width, level) < 32 || u_minify(plane->physical_extent.height, level) < 32)) return g9p_feature_error(cmd_buffer, @@ -4252,6 +4254,8 @@ g9p_ds_addr(const struct pvr_render_job *job) const struct pvr_image *img; const struct pvr_image_plane *pl; + if (job->ds.g9p_scratch) + return job->ds.g9p_scratch; if (!iview) return job->ds.addr.addr; img = pvr_image_view_get_image(iview); @@ -4303,7 +4307,8 @@ g9p_ds_zls(const struct pvr_render_job *job, uint64_t *zlsctl, uint64_t *stencil memcpy(zlsctl, ctl, sizeof(ctl)); if (job->has_stencil_attachment) { pvr_csb_pack (&base, CR_ISP_STENCIL_LOAD_BASE, s) { - s.addr = PVR_DEV_ADDR(g9p_ds_addr(job) + g9p_ds_stencil_offset(img)); + s.addr = PVR_DEV_ADDR(g9p_ds_addr(job) + (job->ds.g9p_scratch ? job->ds.g9p_scratch_stencil + : g9p_ds_stencil_offset(img))); s.enable = true; } } diff --git a/src/imagination/vulkan/pvr_blit.h b/src/imagination/vulkan/pvr_blit.h index 323724a74fe..934ca3eb69e 100644 --- a/src/imagination/vulkan/pvr_blit.h +++ b/src/imagination/vulkan/pvr_blit.h @@ -53,6 +53,15 @@ VkResult pvr_copy_or_resolve_depth_stencil_region( const VkClearDepthStencilValue *ds_clear_values, const VkImageCopy2 *region); +/* + * G9P: both planes of a depth/stencil level to or from the ZLS scratch plane + * (@sbw x @sbh, twiddled, its stencil plane @stencil bytes in), a CPU transfer + */ +VkResult pvr_g9p_ds_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_image *image, uint32_t level, + uint32_t layer, uint64_t scratch, uint32_t sbw, + uint32_t sbh, uint64_t stencil, bool to_scratch); + VkResult pvr_copy_buffer_to_image_region_format(struct pvr_cmd_buffer *cmd_buffer, pvr_dev_addr_t buffer_dev_addr, diff --git a/src/imagination/vulkan/pvr_device.h b/src/imagination/vulkan/pvr_device.h index d28e7d32198..fd486939308 100644 --- a/src/imagination/vulkan/pvr_device.h +++ b/src/imagination/vulkan/pvr_device.h @@ -155,6 +155,15 @@ struct pvr_device { * free list (pvr_arch_render_target_dataset_create()) */ struct pvr_free_list *free_list; + /* + * A stencil level under a tile's 32 pixels renders here, in whole tiles + * (g9p_ds_scratch()): allocated at its first use, CPU-mapped, its vma in + * g9p_vmas for the CPU transfers in and out + */ + struct { + simple_mtx_t lock; + struct pvr_bo *bo; + } zls_scratch; } g9p; /* Compute shaders for queries. */ diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index 38ba27c48e5..2992c56c69a 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -82,6 +82,10 @@ VkResult PVR_PER_ARCH(g9p_clear_attachments)(struct pvr_cmd_buffer *cmd_buffer, VkResult PVR_PER_ARCH(g9p_device_init)(struct pvr_device *device); +/* The ZLS scratch plane's device address (pvr_arch_g9p.c), 0 if none */ +#define G9P_ZLS_SCRATCH_SIZE (16384u * 32u * 5u) +uint64_t PVR_PER_ARCH(g9p_zls_scratch)(struct pvr_device *device); + /* The CPU address of a device address in a bound buffer or image, or NULL */ uint8_t *PVR_PER_ARCH(g9p_cpu_addr)(struct pvr_device *device, uint64_t addr); struct pvr_sub_cmd_gfx; diff --git a/src/imagination/vulkan/pvr_job_render.h b/src/imagination/vulkan/pvr_job_render.h index 85636aad7b8..91bf961cf63 100644 --- a/src/imagination/vulkan/pvr_job_render.h +++ b/src/imagination/vulkan/pvr_job_render.h @@ -119,6 +119,13 @@ struct pvr_render_job { const struct pvr_image_view *iview; bool has_alignment_transfers; + + /* + * G9P: the ZLS's surface instead of the level, a scratch plane of whole + * 32-pixel tiles (g9p_ds_scratch()), and its stencil plane's offset; 0 + * for none + */ + uint64_t g9p_scratch, g9p_scratch_stencil; } ds; VkClearDepthStencilValue ds_clear_value; diff --git a/src/imagination/vulkan/rogue/pvr_blit.c b/src/imagination/vulkan/rogue/pvr_blit.c index 23564c50ff2..6f321991c4f 100644 --- a/src/imagination/vulkan/rogue/pvr_blit.c +++ b/src/imagination/vulkan/rogue/pvr_blit.c @@ -1168,6 +1168,43 @@ pvr_copy_buffer_to_image_region(struct pvr_cmd_buffer *const cmd_buffer, flags); } +VkResult +pvr_g9p_ds_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_image *image, + uint32_t level, uint32_t layer, uint64_t scratch, uint32_t sbw, + uint32_t sbh, uint64_t stencil, bool to_scratch) +{ + struct pvr_transfer_cmd *transfer_cmd = pvr_transfer_cmd_alloc(cmd_buffer); + const VkExtent3D extent = { u_minify(image->vk.extent.width, level), + u_minify(image->vk.extent.height, level), 1 }; + const VkOffset3D origin = { 0 }; + struct pvr_transfer_cmd_surface level_surface, scratch_surface; + VkRect2D rect; + VkResult result; + + if (!transfer_cmd) + return VK_ERROR_OUT_OF_HOST_MEMORY; + pvr_setup_surface_for_image(cmd_buffer->device, &level_surface, &rect, image, layer, 0, level, + &origin, &extent, 0, image->vk.format, + VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT); + scratch_surface = level_surface; + scratch_surface.dev_addr = PVR_DEV_ADDR(scratch); + scratch_surface.width = scratch_surface.stride = sbw; + scratch_surface.height = sbh; + scratch_surface.mem_layout = PVR_MEMLAYOUT_TWIDDLED; + scratch_surface.g9p_stencil_offset = stencil; + transfer_cmd->sources[0].surface = to_scratch ? level_surface : scratch_surface; + transfer_cmd->dst = to_scratch ? scratch_surface : level_surface; + transfer_cmd->scissor = rect; + transfer_cmd->sources[0].mappings[0].src_rect = rect; + transfer_cmd->sources[0].mappings[0].dst_rect = rect; + transfer_cmd->sources[0].mapping_count = 1; + transfer_cmd->source_count = 1; + result = pvr_arch_cmd_buffer_add_transfer_cmd(cmd_buffer, transfer_cmd); + if (result != VK_SUCCESS) + vk_free(&cmd_buffer->vk.pool->alloc, transfer_cmd); + return result; +} + void pvr_rogue_CmdCopyBufferToImage2( VkCommandBuffer commandBuffer, const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) From 052df4fb388be9120e1d4c12363e321ada9b7d6b Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 15:31:41 -0600 Subject: [PATCH 24/44] pvr/g9p: compute values above r31, workgroups launched as whole tasks Compute shaders that ran out of the 16 low registers failed to compile, among them egles31's image round trips of four channels and its image atomics. Graphics code already parks such values above r31, and compute code now does too. Kernels of up to 93 registers ran correctly. Larger groups with those registers hung the GPU, and so did some sizes without them. Measured with 64 workgroups of a kernel that keeps a set of loaded values live: - Past one task of 32, a group whose size was not a multiple of 32 could hang. Without parked values every size 8 past a multiple of 16 from 136 to 248 hung, and with them 40, 56 and 72 did. The kernel record now launches whole tasks, as the A9's does, and the program's prologue already turns off the lanes past the group. Every size tested then ran. - How many registers the tasks of a group may use together: 24 per invocation ran at 256 invocations, 40 at 160 (192 hung in one run of two), 48 and 64 at 128 (48 hung at 160) and 72 to 96 at 64 (72 hung at 96). A pipeline past these is refused at creation, where the earlier rule let some of the hanging ones through. egles31 now passes in full. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_cdm.h | 28 +++++++++++++++++++++++++++ src/imagination/g9p/g9p_compile.c | 10 +++++++--- src/imagination/vulkan/pvr_arch_g9p.c | 13 +++++-------- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/imagination/g9p/g9p_cdm.h b/src/imagination/g9p/g9p_cdm.h index 3f300c1ab1d..a140ab5230b 100644 --- a/src/imagination/g9p/g9p_cdm.h +++ b/src/imagination/g9p/g9p_cdm.h @@ -157,6 +157,34 @@ static inline struct g9p_cdm_record g9p_cdm_kernel(uint32_t k0, uint32_t data, u return r; } +/* + * The threads a kernel record launches for a workgroup of @threads: past one + * task, whole tasks of 32, as the A9 launches them (g5p_cdm_workgroup_threads()). + * The program's prologue turns off the lanes past the workgroup (g9p_prologue() + * compares the index with the count in the common store). Measured on the A10 + * with 64 workgroups: launched as they were, groups past 128 invocations and 8 + * past a multiple of 16 (136, 152 ... 248) hung the GPU, and with registers + * above r31 so did 40, 56 and 72. As whole tasks every size tested ran. + */ +static inline uint32_t g9p_compute_hw_threads(uint32_t threads) +{ + return threads > 32 ? (threads + 31) & ~31u : threads; +} + +/* + * The most tasks of 32 a workgroup of programs of @regs registers may have. + * Measured on the A10 with 64 workgroups of whole tasks, registers in eights: + * 24 ran at 256 invocations, 40 at 160 (192 hung one run in two), 48 and 64 at + * 128 (48 hung at 160), 72 to 96 at 64 (72 hung at 96). 32 is not measured + * and keeps the 192 invocations the earlier rule allowed. + */ +static inline unsigned g9p_compute_max_tasks(unsigned regs) +{ + const unsigned r = (regs + 7) & ~7u; + + return r <= 24 ? 8 : r <= 32 ? 6 : r <= 40 ? 5 : r <= 64 ? 4 : r <= 96 ? 2 : 1; +} + /* --- Internal programs and the end of a compute pass --------------------- */ /* diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index fb86569f1f2..ab2743f51c1 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5850,9 +5850,13 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, ctx.reserve_st_low = true; continue; } - /* A9 compute parks values above its bank-1 limit in bank 0. */ - if (!ctx.oom || (stage == MESA_SHADER_COMPUTE && !(out->a9)) || - g9p_no_homes) + /* + * A9 compute parks values above its bank-1 limit in bank 0. A10 compute parks them + * above r31 as graphics code does: measured up to r95 (93 registers) at 64 + * invocations. The pipeline refuses workgroups too large for the registers + * (g9p_compute_max_tasks()). + */ + if (!ctx.oom || g9p_no_homes) return backend_failure(out, &ctx); stuck = ctx.oom_ip == last_oom_ip ? stuck + 1 : 0; last_oom_ip = ctx.oom_ip; diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 48adf438725..9588c081da1 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -183,16 +183,13 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( } /* - * A workgroup's tasks are resident together. Measured on the A10: 256 - * invocations of 19 registers (allocated in eights, so 24) run, 512 - * invocations hang the GPU even at 10 registers. The device limit is 256 - * invocations; refuse a pipeline whose registers go past what was seen to - * run (40 per invocation, counting the 16 unified ones, at 256) instead of - * hanging. + * A workgroup's tasks are resident together, and too many registers for + * them hang the GPU (g9p_compute_max_tasks()). 512 invocations hang even at + * 10 registers: the device limit is 256. */ const uint32_t threads = shader.local_size[0] * shader.local_size[1] * shader.local_size[2]; - if ((ALIGN_POT(MAX2(shader.num_regs, 1), 8) + 16) * threads > 40 * 256) { + if (DIV_ROUND_UP(threads, 32) > g9p_compute_max_tasks(shader.num_regs)) { mesa_loge("G9P: %u invocations of %u registers each do not fit the USC", threads, shader.num_regs); result = vk_errorf(device, VK_ERROR_UNKNOWN, @@ -440,7 +437,7 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, G9P_CDM_KERNEL_REGS(state->num_regs) | G9P_CDM_ANY_CLUSTER | G9P_CDM_FENCE | G9P_CDM_DATA_BLOCKS(G9P_KERNEL_DATA_DWORDS / 4), kernel.data_offset, kernel.code_offset, group_count[0], group_count[1], - group_count[2], threads, tg_alloc); + group_count[2], g9p_compute_hw_threads(threads), tg_alloc); pvr_csb_set_relocation_mark(csb); emit_record(csb, &bind_rec); From f2db9702119b7aba74937f9cd9bf461f8a417f86 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 15:34:17 -0600 Subject: [PATCH 25/44] pvr/g9p: discards at four samples through the coverage mask A discarding fragment shader at four samples left only sample 0 of each pixel it let through. A conditional discard that was never taken drew a quarter of every pixel. That held with a task per pixel and with one per sample, and also with the full coverage mask written after the discard. Alpha-tested draws into a multisampled target came out faint, and the later cases of eglmsaa inherited the damage. The discards of a multisampled shader now become its coverage mask, 0 where one is taken, as alpha to coverage already does. The draw stays punch-through, and depth and stencil follow the mask. A shader that writes memory keeps its hardware discard, so a discarded pixel makes no stores. Measured: a discarding draw after a clear went from 1029 wrong pixels of eglmsaa's model to none. In eglmsaa's sequence discard went from 1206 to 178 and blending from 1216 to 238, and what is left comes from the load of the earlier pass. The discard cases of eglcoverage went from 3712 wrong pixels each to none, and the one-target 4x cases of eglmrtbig pass. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 79 ++++++++++++++++++++++- src/imagination/g9p/g9p_compile.h | 2 + src/imagination/vulkan/pvr_arch_g9p_gfx.c | 4 +- 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index ab2743f51c1..7b8859cddac 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -4837,6 +4837,53 @@ lower_io(nir_shader *nir) NIR_PASS(_, nir, nir_shader_intrinsics_pass, clamp_frag_depth, nir_metadata_control_flow, NULL); } +/* + * A discard at four samples keeps only sample 0 of the pixels it lets through + * (measured: a conditional discard never taken left a quarter of every pixel, + * at either sample rate, also with the full mask written). So a multisampled + * shader's discards become its coverage: a mask of 0 where one is taken, and + * the shader runs on. Not a shader that writes memory, whose stores and + * atomics a discarded pixel must not make. + */ +bool +g9p_folds_discards(const nir_shader *nir) +{ + bool discards = false; + + nir_foreach_function_impl (impl, nir) { + nir_foreach_block (block, impl) { + nir_foreach_instr (instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + switch (nir_instr_as_intrinsic(instr)->intrinsic) { + case nir_intrinsic_terminate: + case nir_intrinsic_terminate_if: + case nir_intrinsic_demote: + case nir_intrinsic_demote_if: + discards = true; + break; + case nir_intrinsic_store_ssbo: + case nir_intrinsic_ssbo_atomic: + case nir_intrinsic_ssbo_atomic_swap: + case nir_intrinsic_store_global: + case nir_intrinsic_global_atomic: + case nir_intrinsic_global_atomic_swap: + case nir_intrinsic_image_deref_store: + case nir_intrinsic_image_deref_atomic: + case nir_intrinsic_image_deref_atomic_swap: + case nir_intrinsic_image_store: + case nir_intrinsic_image_atomic: + case nir_intrinsic_image_atomic_swap: + return false; + default: + break; + } + } + } + } + return discards; +} + /* * Sample coverage and alpha to coverage: what the fragment writes as its mask, the word * `6104 half` reads (metal-tt: Apple's alpha to coverage is this mask computed in the @@ -4846,9 +4893,37 @@ static void lower_coverage(nir_shader *nir, const struct g9p_coverage *coverage) { nir_function_impl *impl = nir_shader_get_entrypoint(nir); - nir_builder b = nir_builder_at(nir_after_impl(impl)); + nir_builder b = nir_builder_at(nir_before_impl(impl)); nir_def *alpha = NULL; + nir_variable *kept = NULL; + + if (g9p_folds_discards(nir)) { + kept = nir_local_variable_create(impl, glsl_bool_type(), "kept"); + nir_store_var(&b, kept, nir_imm_true(&b), 1); + nir_foreach_block_safe (block, impl) { + nir_foreach_instr_safe (instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + b.cursor = nir_before_instr(instr); + switch (intr->intrinsic) { + case nir_intrinsic_demote: + case nir_intrinsic_terminate: + nir_store_var(&b, kept, nir_imm_false(&b), 1); + break; + case nir_intrinsic_demote_if: + case nir_intrinsic_terminate_if: + nir_store_var(&b, kept, nir_iand(&b, nir_load_var(&b, kept), nir_inot(&b, intr->src[0].ssa)), 1); + break; + default: + continue; + } + nir_instr_remove(instr); + } + } + } + b.cursor = nir_after_impl(impl); nir_foreach_block (block, impl) { nir_foreach_instr (instr, block) { if (instr->type != nir_instr_type_intrinsic) @@ -4879,6 +4954,8 @@ lower_coverage(nir_shader *nir, const struct g9p_coverage *coverage) mask = nir_iand(&b, mask, nir_bcsel(&b, nir_ine_imm(&b, enabled, 0), by_alpha, nir_imm_int(&b, ~0))); } + if (kept) + mask = nir_bcsel(&b, nir_load_var(&b, kept), mask, nir_imm_int(&b, 0)); nir_store_output(&b, mask, nir_imm_int(&b, 0), .base = FRAG_RESULT_SAMPLE_MASK, .src_type = nir_type_uint32, .io_semantics.location = FRAG_RESULT_SAMPLE_MASK, .io_semantics.num_slots = 1); diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index 87936c928fe..0f66c19f034 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -329,6 +329,8 @@ struct g9p_coverage { bool dynamic; }; +bool g9p_folds_discards(const nir_shader *nir); + bool g9p_compile_fragment(nir_shader *nir, unsigned frag_coord_slot, const nir_lower_blend_options *blend, const struct g9p_coverage *coverage, const struct g9p_target targets[G9P_MAX_TARGETS], bool zs_attachment, diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index be0f48b7071..a5fdc09e85f 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -1091,10 +1091,12 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo } /* Four samples with a coverage: the fragment program writes it (Zink: static state) */ /* only a pipeline that drops samples writes its coverage: that takes punch-through */ + /* so does one that discards (g9p_folds_discards()) */ const struct g9p_coverage coverage = { true }; const struct g9p_coverage *coverage_ptr = vk_state->ms && vk_state->ms->rasterization_samples == VK_SAMPLE_COUNT_4_BIT && - ((vk_state->ms->sample_mask & 0xf) != 0xf || vk_state->ms->alpha_to_coverage_enable) + ((vk_state->ms->sample_mask & 0xf) != 0xf || vk_state->ms->alpha_to_coverage_enable || + g9p_folds_discards(fs_nir)) ? &coverage : NULL; struct g9p_target targets[G9P_MAX_TARGETS] = { 0 }; From 3a43ed7669e475937a4102cef4149fb2824654cd Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 15:46:52 -0600 Subject: [PATCH 26/44] pvr/g9p: colour renders into the tail of a mip chain A render into a mip level whose address is not a multiple of 64 bytes kept none of its pixels, not even the clear. Such levels are the tail of a chain, past its first level under 64 bytes: the 2x1 and 1x1 levels of a 64x16 RGBA8 texture, or the 4x1 level of a 16x2 one at byte 160. Every level at a multiple of 64 bytes came out exact, and the sampler reads all of them where they are. egldepthmip lost its one stencil pixel this way, since its stencil check draws into the 2x1 colour level. The end of tile now stores such a target to a 64-byte aligned slot at the end of the scratch plane. The level is copied in before the render, so that tiles left unrendered keep it, and back after it, by CPU transfers serialized with the fragment jobs as the depth and stencil scratch plane does. Measured with renders into every level of 64x16, 64x64, 32x4, 16x2, 8x8 and 4x1 chains, each level read back after its render and again after all of them: all exact. egldepthmip passes. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 49 ++++++++++++++++++++ src/imagination/vulkan/pvr_arch_g9p_gfx.c | 40 ++++++++++++++-- src/imagination/vulkan/pvr_blit.h | 8 ++++ src/imagination/vulkan/pvr_g9p.h | 12 +++++ src/imagination/vulkan/pvr_job_render.h | 6 +++ src/imagination/vulkan/rogue/pvr_blit.c | 31 +++++++++++++ 6 files changed, 142 insertions(+), 4 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index a15d766d76a..1482ac3b468 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -1569,6 +1569,50 @@ g9p_ds_scratch(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx * return VK_SUCCESS; } +/* + * G9P: the colour targets that store to a scratch slot (g9p_eot_addr()): the + * level copied in before the render, so that its tiles left unrendered keep + * it, and back after it, by CPU transfers serialized with the fragment jobs + * as g9p_ds_scratch()'s are. + */ +static VkResult +g9p_colour_scratch(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx *const gfx_sub_cmd, + const struct pvr_render_pass_info *info, + const struct pvr_renderpass_hwsetup_render *hw_render) +{ + struct pvr_sub_cmd *const prev_sub_cmd = container_of(gfx_sub_cmd, struct pvr_sub_cmd, gfx); + const uint64_t scratch = PVR_PER_ARCH(g9p_zls_scratch)(cmd_buffer->device); + VkResult result; + + for (unsigned before = 0; before < 2; before++) { + cmd_buffer->state.current_sub_cmd = NULL; + result = pvr_arch_cmd_buffer_start_sub_cmd(cmd_buffer, PVR_SUB_CMD_TYPE_TRANSFER); + if (result != VK_SUCCESS) + return result; + struct pvr_sub_cmd *const new_sub_cmd = cmd_buffer->state.current_sub_cmd; + + u_foreach_bit (t, gfx_sub_cmd->job.g9p.colour_scratch) { + const struct pvr_image_view *iview = info->attachments[hw_render->eot_surfaces[t].attachment_idx]; + + result = pvr_g9p_colour_scratch_transfer(cmd_buffer, pvr_image_view_get_image(iview), + iview->vk.base_mip_level, + iview->vk.base_array_layer, + g9p_colour_scratch_slot(scratch, t), before); + if (result != VK_SUCCESS) + return result; + } + new_sub_cmd->transfer.serialize_with_frag = true; + result = pvr_arch_cmd_buffer_end_sub_cmd(cmd_buffer); + if (result != VK_SUCCESS) + return result; + /* the copy in before the render (list_move_to() places it after the entry given) */ + if (before) + list_move_to(&new_sub_cmd->link, prev_sub_cmd->link.prev); + } + cmd_buffer->state.current_sub_cmd = prev_sub_cmd; + return VK_SUCCESS; +} + static VkResult pvr_sub_cmd_gfx_align_ds_subtiles(struct pvr_cmd_buffer *const cmd_buffer, struct pvr_sub_cmd_gfx *const gfx_sub_cmd) @@ -2216,6 +2260,11 @@ static VkResult pvr_sub_cmd_gfx_job_init(const struct pvr_device_info *dev_info, job->has_stencil_attachment = false; job->ds_clear_value = default_ds_clear_value; } + if (PVR_HAS_FEATURE(dev_info, apple_g9p) && job->g9p.colour_scratch) { + result = g9p_colour_scratch(cmd_buffer, sub_cmd, render_pass_info, hw_render); + if (result != VK_SUCCESS) + return result; + } if (hw_render->ds_attach_idx != VK_ATTACHMENT_UNUSED) { struct pvr_image_view *iview = diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index a5fdc09e85f..426a5737338 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -3816,6 +3816,35 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa return VK_SUCCESS; } +/* + * Where the end of tile stores colour target @t, at @addr in its level. Its + * emit takes only a 64-byte aligned address: a level at 0x1550 in a 64x16 + * RGBA8 chain (2x1) or at 0xa0 in a 16x2 one (4x1) kept none of a render's + * pixels, where every level at a multiple of 64 bytes was exact, and the + * sampler reads them all where they are. Such levels (a chain's tail, past + * its first level under 64 bytes) store to a scratch slot instead, which + * g9p_colour_scratch() fills from the level before the render and copies + * back after it. Not with multiview: the views share the slot. + */ +static uint64_t +g9p_eot_addr(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwsetup_render *hw, + struct pvr_render_job *job, unsigned t, uint64_t addr) +{ + uint64_t scratch; + + if (!(addr & 63)) + return addr; + if (util_bitcount(hw->view_mask) > 1) { + mesa_logw_once("G9P: multiview render into a level not 64-byte aligned"); + return addr; + } + scratch = PVR_PER_ARCH(g9p_zls_scratch)(cmd_buffer->device); + if (!scratch) + return addr; + job->g9p.colour_scratch |= BITFIELD_BIT(t); + return g9p_colour_scratch_slot(scratch, t); +} + VkResult PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwsetup_render *hw_render, @@ -3899,8 +3928,9 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, job->g9p.target_addr = addr; job->g9p.target_pitch = pitch; job->g9p.target_linear = linear; - pack_image_state(image, addr, iview->vk.extent.width, iview->vk.extent.height, linear, - pitch, iview->vk.format); + pack_image_state(image, g9p_eot_addr(cmd_buffer, hw_render, job, 0, addr), + iview->vk.extent.width, iview->vk.extent.height, linear, pitch, + iview->vk.format); if (image_obj->vk.samples == 4) image[0] = (image[0] & ~0xfu) | 0x0c; /* 2D multisample, twiddled (iOS msaa4-store) */ /* the other targets' blocks: their state, their output-half offset (PR #6) */ @@ -3910,8 +3940,10 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_image_plane *pl = pvr_single_plane_const(im); pack_image_state(&image[8 * t], - im->dev_addr.addr + pl->offset + pl->mip_levels[v->vk.base_mip_level].offset + - (v->vk.base_array_layer + view) * pl->layer_size, + g9p_eot_addr(cmd_buffer, hw_render, job, t, + im->dev_addr.addr + pl->offset + + pl->mip_levels[v->vk.base_mip_level].offset + + (v->vk.base_array_layer + view) * pl->layer_size), v->vk.extent.width, v->vk.extent.height, im->memlayout == PVR_MEMLAYOUT_LINEAR, pl->mip_levels[v->vk.base_mip_level].pitch, v->vk.format); diff --git a/src/imagination/vulkan/pvr_blit.h b/src/imagination/vulkan/pvr_blit.h index 934ca3eb69e..ac1a7cd03a1 100644 --- a/src/imagination/vulkan/pvr_blit.h +++ b/src/imagination/vulkan/pvr_blit.h @@ -62,6 +62,14 @@ VkResult pvr_g9p_ds_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, uint32_t layer, uint64_t scratch, uint32_t sbw, uint32_t sbh, uint64_t stencil, bool to_scratch); +/* + * G9P: a colour level to or from its scratch slot (g9p_colour_scratch()), the + * same layout at another address, a CPU transfer + */ +VkResult pvr_g9p_colour_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_image *image, uint32_t level, + uint32_t layer, uint64_t scratch, bool to_scratch); + VkResult pvr_copy_buffer_to_image_region_format(struct pvr_cmd_buffer *cmd_buffer, pvr_dev_addr_t buffer_dev_addr, diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index 2992c56c69a..fc596c36f05 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -86,6 +86,18 @@ VkResult PVR_PER_ARCH(g9p_device_init)(struct pvr_device *device); #define G9P_ZLS_SCRATCH_SIZE (16384u * 32u * 5u) uint64_t PVR_PER_ARCH(g9p_zls_scratch)(struct pvr_device *device); +/* + * The end of the scratch plane: a slot per colour target, for a level the end + * of tile can't store to (g9p_job_init()). The levels past a chain's first + * level under 64 bytes, each under 64 bytes (four samples: 256). + */ +#define G9P_COLOUR_SCRATCH_SLOT 256u +static inline uint64_t +g9p_colour_scratch_slot(uint64_t scratch, unsigned t) +{ + return scratch + G9P_ZLS_SCRATCH_SIZE - G9P_COLOUR_SCRATCH_SLOT * (t + 1); +} + /* The CPU address of a device address in a bound buffer or image, or NULL */ uint8_t *PVR_PER_ARCH(g9p_cpu_addr)(struct pvr_device *device, uint64_t addr); struct pvr_sub_cmd_gfx; diff --git a/src/imagination/vulkan/pvr_job_render.h b/src/imagination/vulkan/pvr_job_render.h index 91bf961cf63..79e52385c2f 100644 --- a/src/imagination/vulkan/pvr_job_render.h +++ b/src/imagination/vulkan/pvr_job_render.h @@ -171,6 +171,12 @@ struct pvr_render_job { bool target_linear; /* the background loads the target (load op load) */ bool load; + /* + * the colour targets whose level the end of tile can't store to (an + * address not 64-byte aligned): they store to a scratch slot + * (g9p_colour_scratch_slot()) + */ + uint32_t colour_scratch; /* * the first colour attachment's load op, G9P_LOAD_OP_NONE without one * (don't care: the render pass setup initializes nothing) diff --git a/src/imagination/vulkan/rogue/pvr_blit.c b/src/imagination/vulkan/rogue/pvr_blit.c index 6f321991c4f..e293ce07845 100644 --- a/src/imagination/vulkan/rogue/pvr_blit.c +++ b/src/imagination/vulkan/rogue/pvr_blit.c @@ -1205,6 +1205,37 @@ pvr_g9p_ds_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_ return result; } +VkResult +pvr_g9p_colour_scratch_transfer(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_image *image, + uint32_t level, uint32_t layer, uint64_t scratch, bool to_scratch) +{ + struct pvr_transfer_cmd *transfer_cmd = pvr_transfer_cmd_alloc(cmd_buffer); + const VkExtent3D extent = { u_minify(image->vk.extent.width, level), + u_minify(image->vk.extent.height, level), 1 }; + const VkOffset3D origin = { 0 }; + struct pvr_transfer_cmd_surface level_surface, scratch_surface; + VkRect2D rect; + VkResult result; + + if (!transfer_cmd) + return VK_ERROR_OUT_OF_HOST_MEMORY; + pvr_setup_surface_for_image(cmd_buffer->device, &level_surface, &rect, image, layer, 0, level, + &origin, &extent, 0, image->vk.format, VK_IMAGE_ASPECT_COLOR_BIT); + scratch_surface = level_surface; + scratch_surface.dev_addr = PVR_DEV_ADDR(scratch); + transfer_cmd->sources[0].surface = to_scratch ? level_surface : scratch_surface; + transfer_cmd->dst = to_scratch ? scratch_surface : level_surface; + transfer_cmd->scissor = rect; + transfer_cmd->sources[0].mappings[0].src_rect = rect; + transfer_cmd->sources[0].mappings[0].dst_rect = rect; + transfer_cmd->sources[0].mapping_count = 1; + transfer_cmd->source_count = 1; + result = pvr_arch_cmd_buffer_add_transfer_cmd(cmd_buffer, transfer_cmd); + if (result != VK_SUCCESS) + vk_free(&cmd_buffer->vk.pool->alloc, transfer_cmd); + return result; +} + void pvr_rogue_CmdCopyBufferToImage2( VkCommandBuffer commandBuffer, const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) From e5822e7cf00fe9010e66bbcb3e16f91cb9123d99 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 15:57:25 -0600 Subject: [PATCH 27/44] pvr/g9p: alpha to coverage keeps a nested set of samples Alpha to coverage kept the first n of four samples for an alpha of n quarters, so two kept samples were 0 and 1, both near the top of the pixel. A sample coverage of 0.5 inverted keeps 2 and 3, and the two together kept nothing. eglcoverage expects samples 0, 3, 1 and 2 in turn, as the GLES driver kept them, so two kept samples are a diagonal pair. The shader now builds that mask. Measured with eglcoverage at 4x: the edge, both, depth with two renders, stencil, query and blending cases went from wrong to exact, and its GL error went away. Its depth-only case with alpha to coverage now fails like the sample coverage one already did. Both show a colour where the model has none, which appears in the next render, the one that loads the 4x target, and not in the depth-only render itself. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 7b8859cddac..a1a0bf6928d 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -4950,7 +4950,15 @@ lower_coverage(nir_shader *nir, const struct g9p_coverage *coverage) if (alpha) { nir_def *enabled = nir_load_ubo(&b, 1, 32, index, nir_imm_int(&b, 4), .align_mul = 4, .range = 8); nir_def *n = nir_f2u32(&b, nir_fadd_imm(&b, nir_fmul_imm(&b, nir_fsat(&b, alpha), 4.0f), 0.5f)); - nir_def *by_alpha = nir_iadd_imm(&b, nir_ishl(&b, nir_imm_int(&b, 1), n), -1); + /* + * a nested set, samples 0, 3, 1 and 2 in turn: two kept are a diagonal pair + * (G9P_POSITIONS_4X), as the GLES driver kept them and eglcoverage expects + */ + nir_def *by_alpha = nir_bcsel(&b, nir_ieq_imm(&b, n, 0), nir_imm_int(&b, 0x0), + nir_bcsel(&b, nir_ieq_imm(&b, n, 1), nir_imm_int(&b, 0x1), + nir_bcsel(&b, nir_ieq_imm(&b, n, 2), nir_imm_int(&b, 0x9), + nir_bcsel(&b, nir_ieq_imm(&b, n, 3), nir_imm_int(&b, 0xb), + nir_imm_int(&b, 0xf))))); mask = nir_iand(&b, mask, nir_bcsel(&b, nir_ine_imm(&b, enabled, 0), by_alpha, nir_imm_int(&b, ~0))); } From 7dd27cba3d13cce6b59ba6235ef739529ab23200 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 16:03:36 -0600 Subject: [PATCH 28/44] pvr/g9p: resolve surfaces take no words of the tile A multisampled render whose first target Zink resolves in the render has a third end-of-tile surface between the targets: the resolve of target 0. Its words were counted in the tile like a target's, so the end of tile read target 1 from past its words, and the background wrote target 1's clear or load there. With two 4x targets and a resolve, target 1 kept its clear colour under every draw, or held words of another target. eglmrtbig's 4x cases with two targets and eglmsaa's mrt case failed this way. A resolve surface now stores the words of the target it resolves and has none of its own: the end of tile takes its source's offset, the background skips it, and a clear or a target written to memory finds colour attachment j as the j-th surface that is not a resolve. Measured: each sample of two 4x targets read back with texelFetch after a draw and both resolves is exact (target 1 was zeros and stray words before). eglmsaa's mrt case at 4x and eglmrtbig's 4x cases with two and three targets pass. Four targets at 4x still fail. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 60 +++++++++++++++++++---- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 426a5737338..75f528670e4 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -547,7 +547,7 @@ g9p_hw_overflow(const struct pvr_render_pass_info *info, const struct pvr_render for (unsigned t = 0; hw && t < MIN2(hw->eot_surface_count, 8); t++) { outputs[t] = g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format); - present |= BITFIELD_BIT(t); + present |= hw->eot_surfaces[t].need_resolve ? 0 : BITFIELD_BIT(t); } return g9p_overflow_targets(outputs, present); } @@ -600,12 +600,41 @@ g9p_render_words(const struct pvr_render_pass_info *info, unsigned words = 0; for (unsigned t = 0; hw && t < MIN2(hw->eot_surface_count, first); t++) { - if (!(overflow >> t & 1)) + if (!(overflow >> t & 1) && !hw->eot_surfaces[t].need_resolve) words += g9p_output_words(g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format)); } return words; } +/* + * A resolve's end-of-tile surface (a multisampled target stored once more, + * resolved): no words of its own in the tile, its source's. The surface whose + * words surface @t stores. + */ +static unsigned +g9p_words_surface(const struct pvr_renderpass_hwsetup_render *hw, unsigned t) +{ + if (!hw->eot_surfaces[t].need_resolve) + return t; + for (unsigned s = 0; s < hw->eot_surface_count; s++) { + if (!hw->eot_surfaces[s].need_resolve && + hw->eot_surfaces[s].attachment_idx == hw->eot_surfaces[t].src_attachment_idx) + return s; + } + return t; +} + +/* Colour attachment @j's end-of-tile surface: the j-th that is not a resolve, or -1 */ +static int +g9p_colour_surface(const struct pvr_renderpass_hwsetup_render *hw, unsigned j) +{ + for (unsigned t = 0; hw && t < hw->eot_surface_count; t++) { + if (!hw->eot_surfaces[t].need_resolve && j-- == 0) + return t; + } + return -1; +} + /* Target @t's words from the common store, the others as they are in the tile */ static VkResult g9p_clear_one_kick(struct pvr_device *device, unsigned words, unsigned base, unsigned n, @@ -2003,7 +2032,8 @@ buffer_addrs(struct pvr_cmd_buffer *cmd_buffer, unsigned n, const unsigned (*set } else if (set == G9P_OVERFLOW_TARGETS) { const struct pvr_render_pass_info *info = &cs->render_pass_info; const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); - const struct pvr_image_view *v = info->attachments[hw->eot_surfaces[binding].attachment_idx]; + const int surface = g9p_colour_surface(hw, binding); + const struct pvr_image_view *v = info->attachments[hw->eot_surfaces[surface >= 0 ? surface : binding].attachment_idx]; const struct pvr_image *im = pvr_image_view_get_image(v); const struct pvr_image_plane *pl = pvr_single_plane_const(im); const uint64_t addr = im->dev_addr.addr + pl->offset + pl->mip_levels[v->vk.base_mip_level].offset + @@ -3404,14 +3434,15 @@ PVR_PER_ARCH(g9p_clear_attachments)(struct pvr_cmd_buffer *cmd_buffer, uint32_t { const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); - if (hw && colour->colorAttachment < hw->eot_surface_count && - (g9p_hw_overflow(info, hw) >> colour->colorAttachment & 1)) { - g9p_fill_target(cmd_buffer, info->attachments[hw->eot_surfaces[colour->colorAttachment].attachment_idx], + const int surface = g9p_colour_surface(hw, colour->colorAttachment); + + if (surface >= 0 && (g9p_hw_overflow(info, hw) >> surface & 1)) { + g9p_fill_target(cmd_buffer, info->attachments[hw->eot_surfaces[surface].attachment_idx], colour->clearValue.color.uint32); return VK_SUCCESS; } - if (hw && colour->colorAttachment < hw->eot_surface_count) { - const unsigned t = colour->colorAttachment; + if (surface >= 0) { + const unsigned t = surface; const VkFormat format = info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format; if (hw->eot_surface_count > 1) { @@ -3724,6 +3755,9 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa for (unsigned t = 0; t < n; t++) { const uint32_t idx = hw->eot_surfaces[t].attachment_idx; + /* a resolve has no words of its own (g9p_words_surface()) */ + if (hw->eot_surfaces[t].need_resolve) + continue; for (unsigned j = 0; j < hw->color_init_count; j++) loads |= (hw->color_init[j].index == idx && hw->color_init[j].op == VK_ATTACHMENT_LOAD_OP_LOAD) << t; } @@ -3746,6 +3780,11 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa const enum g9p_output o = g9p_format_output(v->vk.format); const unsigned w = g9p_output_words(o); + if (hw->eot_surfaces[t].need_resolve) { + key |= (uint64_t)1 << (20 + t); + continue; + } + key |= (uint64_t)(o | (loads >> t & 1) << 3) << (4 * t); at[t] = tmp; if (loads >> t & 1) { @@ -3781,6 +3820,9 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa for (unsigned t = 0; t < n; t++) { const unsigned w = g9p_output_words(g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format)); + if (hw->eot_surfaces[t].need_resolve) + continue; + for (unsigned k = 0; k < w; k++) g9p_alu2(c, G9P_OR, base + k, at[t] + k, at[t] + k, false); base += w; @@ -3949,7 +3991,7 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, pl->mip_levels[v->vk.base_mip_level].pitch, v->vk.format); if (im->vk.samples == 4) image[8 * t] = (image[8 * t] & ~0xfu) | 0x0c; - image[8 * t + 6] = 2 * g9p_render_words(info, hw_render, t); + image[8 * t + 6] = 2 * g9p_render_words(info, hw_render, g9p_words_surface(hw_render, t)); image[8 * t - 1] = 0xffffffff; } job->g9p.width = iview->vk.extent.width; From e9b7ad52fe6a3450f0e1edce6cc10c8757212177 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 16:12:03 -0600 Subject: [PATCH 29/44] pvr/g9p: a four-sample load gives each sample its own value A render that loads a 4x target read it as a 2D texture of twice the size at the pixel centre, so every sample of a pixel took one sample's value. Edges drawn by an earlier render came out all or nothing, and everything later drawn over them inherited that. In eglmsaa that was the load case, and the discard, blending and depth cases that load after it. The background program runs a task per sample already. It now reads the sample's own y (special value 6), takes the index from it as the positions go down in y, and samples the target with the texture unit's multisample form: dimension 4, integer coordinates and the sample index, with the state at the pixels' size and the sample count at bit 55, as the GLES driver read 4x surfaces. Reading the twice-size texture at each sample's texel instead lost one sample in every 8 x 4 pixel block of some tasks. Both the one-target load and the load of several targets do so. Measured with eglmsaa at 4x, 96x64 and 300x200: coverage, load, discard, blending, depth, the depth resolve, two targets and every format are exact (load, discard, blending and depth were wrong in 63 to 238 pixels). A load followed by nothing, with every tile processed, keeps each pixel's samples. At 4x eglcoverage fails only its depth-only case. eglmsaa still fails at 2x and 8x, which the driver does not offer. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 111 +++++++++++++++++++--- src/imagination/vulkan/pvr_device.h | 3 + 2 files changed, 103 insertions(+), 11 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 75f528670e4..4e92a42a3ce 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -204,6 +204,67 @@ g9p_kick_4x(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_pds_upload *kick return pvr_arch_cmd_buffer_upload_pds(cmd_buffer, data, ARRAY_SIZE(data), 16, code, ARRAY_SIZE(code), 32, 64, out); } +/* dst = a << n or a >> n (logical) in fragment code: the amount in register @tmp */ +static void +g9p_shift_by(struct g9p_code *c, unsigned dst, unsigned a, unsigned n, bool right, unsigned tmp) +{ + g9p_mov_imm(c, tmp, n); + g9p_shift(c, dst, a, tmp, right); +} + +/* + * A load at four samples, in its task per sample (g9p_kick_4x()): the pixel in + * r0-r1 and the sample's index in r2, for g9p_ms_sample(). 111c 0006 is the + * sample's y (measured: the samples of a pixel differ, where 0002 is the + * centre's), and 211c 6800 the pixel centre, both in 1/16 pixel. The + * positions go down in y (G9P_POSITIONS_4X), so the index is (y >> 2) & 3. + * Uses r6 and r8. + */ +static void +g9p_ms_load_coords(struct g9p_code *c) +{ + G9P_EMIT(c, 0x111c, 0x6900 | 2 * 6, 0x0006); + g9p_wait(c); + G9P_EMIT(c, 0x211c, 0x6800); + g9p_wait(c); + g9p_shift_by(c, 0, 0, 4, true, 8); + g9p_shift_by(c, 1, 1, 4, true, 8); + g9p_shift_by(c, 2, 6, 2, true, 8); + g9p_mov_imm(c, 8, 3); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); +} + +/* + * A sample of a four-sample texture into @dst: dimension 4 (W0 bits 9-11), + * integer coordinates (W1 bit 6) in r0-r1 and the sample index in r2, as the + * GLES driver read them, with the state at common-store dword @state and the + * sampler at @sampler. Its state is g9p_ms_texture()'s. Read as a 2D texture + * of twice the size instead, one sample of each task of 32 lanes in a block of + * 8 x 4 pixels came from another (measured over whole renders). + */ +static void +g9p_ms_sample(struct g9p_code *c, bool half, unsigned dst, unsigned state, unsigned sampler) +{ + G9P_EMIT(c, half ? 0xf918 : 0xf91a, g9p_sample_w1(dst, state) | 0x40, 0x8100 | sampler, 0x0700, + 0x0200); +} + +/* + * The texture state of a four-sample image as g9p_ms_sample() reads it: + * g9p_pack_image_view()'s with dimension 4, the pixels' size and log2(samples) + * - 1 at bit 55 + */ +static void +g9p_ms_texture(const struct pvr_image_view *v, uint64_t tex[2]) +{ + const uint32_t w = v->vk.extent.width, h = v->vk.extent.height; + uint32_t d0 = (uint32_t)tex[0], d1 = (uint32_t)(tex[0] >> 32); + + d0 = (d0 & ~(0x1fu << 27) & ~7u) | (w - 1) << 27 | 4; + d1 = (d1 & ~0x7fffffu) | (w - 1) >> 5 | (h - 1) << 9 | 1u << 23; + tex[0] = d0 | (uint64_t)d1 << 32; +} + /* --- Device programs ------------------------------------------------------ */ VkResult @@ -360,6 +421,16 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) result = upload_usc(device, c, &p->load_usc[o]); if (result != VK_SUCCESS) goto out; + + /* at four samples: each sample its own, from the state in g9p_ms_texture()'s form */ + c->n = 0; + g9p_ms_load_coords(c); + g9p_ms_sample(c, half, 0, 0, 8); + g9p_wait(c); + g9p_end_fragment(c, g9p_output_words(o)); + result = upload_usc(device, c, &p->load_ms_usc[o]); + if (result != VK_SUCCESS) + goto out; } /* @@ -399,6 +470,8 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) result = upload_kick(device, p->keep_usc[o], 8, 0, &p->keep_kick[o]); if (result == VK_SUCCESS) result = upload_kick(device, p->load_usc[o], 8, 0, &p->load_kick[o]); + if (result == VK_SUCCESS) + result = upload_kick(device, p->load_ms_usc[o], 9, 0, &p->load_ms_kick[o]); if (result == VK_SUCCESS) result = upload_kick(device, p->clear_raw_usc[o], 8, 0, &p->clear_raw_kick[o]); } @@ -468,9 +541,11 @@ PVR_PER_ARCH(g9p_gfx_device_finish)(struct pvr_device *device) simple_mtx_destroy(&p->cached_lock); for (unsigned o = 0; o < G9P_OUTPUT_COUNT; o++) { pvr_bo_suballoc_free(p->load_usc[o]); + pvr_bo_suballoc_free(p->load_ms_usc[o]); pvr_bo_suballoc_free(p->keep_usc[o]); pvr_bo_suballoc_free(p->clear_raw_usc[o]); pvr_bo_suballoc_free(p->load_kick[o].pvr_bo); + pvr_bo_suballoc_free(p->load_ms_kick[o].pvr_bo); pvr_bo_suballoc_free(p->keep_kick[o].pvr_bo); pvr_bo_suballoc_free(p->clear_raw_kick[o].pvr_bo); } @@ -3765,14 +3840,20 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa const struct pvr_image_view *v = info->attachments[hw->eot_surfaces[0].attachment_idx]; key |= (uint64_t)v->vk.extent.width << 40 | (uint64_t)v->vk.extent.height << 26; - G9P_EMIT(c, 0x211c, 0x6800); - g9p_wait(c); - g9p_convert(c, 2, 0, G9P_U2F); - g9p_convert(c, 3, 1, G9P_U2F); - g9p_mov_imm(c, 4, fui(1.0f / (16 * v->vk.extent.width))); - g9p_mov_imm(c, 5, fui(1.0f / (16 * v->vk.extent.height))); - g9p_alu2(c, G9P_FMUL, 0, 2, 4, false); - g9p_alu2(c, G9P_FMUL, 1, 3, 5, false); + if (job->g9p.samples == 4) { + /* each sample its own (g9p_ms_load_coords()), the multisampled targets so read */ + g9p_ms_load_coords(c); + key |= 1ull << 56; + } else { + G9P_EMIT(c, 0x211c, 0x6800); + g9p_wait(c); + g9p_convert(c, 2, 0, G9P_U2F); + g9p_convert(c, 3, 1, G9P_U2F); + g9p_mov_imm(c, 4, fui(1.0f / (16 * v->vk.extent.width))); + g9p_mov_imm(c, 5, fui(1.0f / (16 * v->vk.extent.height))); + g9p_alu2(c, G9P_FMUL, 0, 2, 4, false); + g9p_alu2(c, G9P_FMUL, 1, 3, 5, false); + } } for (unsigned t = 0; t < n; t++) { const uint32_t idx = hw->eot_surfaces[t].attachment_idx; @@ -3791,9 +3872,14 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa uint64_t tex[2]; PVR_PER_ARCH(g9p_pack_image_view)(v, tex); + if (job->g9p.samples == 4) { + g9p_ms_texture(v, tex); + g9p_ms_sample(c, o < G9P_OUTPUT_RAW1, tmp, 8 * t, 8 * n); + } else { + G9P_EMIT(c, o >= G9P_OUTPUT_RAW1 ? 0xf51a : 0xf518, g9p_sample_w1(tmp, 8 * t), + 0x8100 | 8 * n, 0x0700, 0x0200); + } memcpy(&data[8 * t], tex, sizeof(tex)); - G9P_EMIT(c, o >= G9P_OUTPUT_RAW1 ? 0xf51a : 0xf518, g9p_sample_w1(tmp, 8 * t), - 0x8100 | 8 * n, 0x0700, 0x0200); tmp += 4; } else { float clear[4] = { 0 }; @@ -4087,6 +4173,8 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, uint64_t tex[2]; PVR_PER_ARCH(g9p_pack_image_view)(iview, tex); + if (pvr_image_view_get_image(iview)->vk.samples == VK_SAMPLE_COUNT_4_BIT) + g9p_ms_texture(iview, tex); memcpy(bg, tex, sizeof(tex)); { /* @@ -4190,7 +4278,8 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, if (result != VK_SUCCESS) return result; } else { - struct pvr_pds_upload bg_kick = load ? p->load_kick[bg_output] + struct pvr_pds_upload bg_kick = load && job->g9p.samples == 4 ? p->load_ms_kick[bg_output] + : load ? p->load_kick[bg_output] : bg_raw ? p->clear_raw_kick[bg_output] : p->clear_kick; diff --git a/src/imagination/vulkan/pvr_device.h b/src/imagination/vulkan/pvr_device.h index fd486939308..60922da0675 100644 --- a/src/imagination/vulkan/pvr_device.h +++ b/src/imagination/vulkan/pvr_device.h @@ -119,6 +119,9 @@ struct pvr_device { /* load op: the old target sampled at the pixel position, and its kick */ struct pvr_suballoc_bo *load_usc[G9P_OUTPUT_COUNT]; struct pvr_pds_upload load_kick[G9P_OUTPUT_COUNT]; + /* the same at four samples, each sample its own (g9p_ms_load_coords()) */ + struct pvr_suballoc_bo *load_ms_usc[G9P_OUTPUT_COUNT]; + struct pvr_pds_upload load_ms_kick[G9P_OUTPUT_COUNT]; /* depth/stencil clear draws: the tile's colour unchanged, and its kick */ struct pvr_suballoc_bo *keep_usc[G9P_OUTPUT_COUNT]; struct pvr_pds_upload keep_kick[G9P_OUTPUT_COUNT]; From f273e8a1a2014f9482ac1ae15360062dae1efeea Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 16:17:29 -0600 Subject: [PATCH 30/44] pvr/g9p: no render merge that drops a colour attachment A render that only loads attachments of the one kept open continues it in tile memory (g9p_merge_compatible()). A render with fewer colour attachments passed that check, so its draws ran in the open render and wrote the attachment they did not have. eglcoverage clears a 4x target and its depth, then draws into a framebuffer with the depth buffer alone: the draw joined the clear's render and coloured the target. A glFinish() between the two kept them apart, and with it the case passed. The new render now has to have every colour attachment of the open one. Zink's case the merge is for, a render begun again on the same images, still merges. Measured with eglcoverage: all 37 of its 4x cases pass (its four depth-only cases, at 96x64 and 300x200, failed on every pixel before). It still fails at 2x and 8x, which the driver does not offer. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index 1482ac3b468..eb6a52c4c08 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -5664,6 +5664,21 @@ static bool g9p_merge_compatible(const struct pvr_cmd_buffer *cmd_buffer, const info->layerCount != state->g9p_merge.layers || info->viewMask != state->g9p_merge.view_mask || info->colorAttachmentCount > state->g9p_merge.color_count) return false; + /* + * Every colour attachment of the open render in the new one too: draws of a + * render without it ran in the open render and wrote it (eglcoverage's draw + * into a framebuffer with the depth buffer alone, after a clear of colour + * and depth: the clear's render took the draw, which coloured the target) + */ + for (uint32_t i = 0; i < state->g9p_merge.color_count; i++) { + if (!state->g9p_merge.color[i].image) + continue; + if (i >= info->colorAttachmentCount) + return false; + g9p_merge_attachment(&info->pColorAttachments[i], &image, &level, &layer); + if (!image) + return false; + } for (uint32_t i = 0; i < info->colorAttachmentCount; i++) { const VkRenderingAttachmentInfo *a = &info->pColorAttachments[i]; From 93ab995784f0e2883b98748a90331f123d8cffac Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 16:46:41 -0600 Subject: [PATCH 31/44] pvr/g9p: workgroup barriers across tasks A workgroup of more than 32 invocations runs as several tasks of 32, and the tasks run apart. The A10's barrier was only a wait for memory, which holds within one task, so eglcompute's sums through shared memory with barrier() came out wrong at 128 and 256 invocations (64 passed by chance). A workgroup barrier of a group of more than one task now synchronizes the tasks as the GLES driver did, with coherent atomics on a word of a device buffer (G9P_BARRIER_COUNTERS, one word per workgroup modulo 4096). Lane 0 of each task adds 1, whose low byte counts the tasks in. The last one adds 256 - tasks, which sets the count back to 0 and advances the generation above it, and the others wait until the generation changes. A task's lanes run in step, so they wait with their lane 0. The count is back at 0 after every barrier, so one zeroed buffer serves every dispatch, since compute jobs run one at a time. The workgroup ID and count are read again where used instead of kept live from the top of the shader. Otherwise the barrier's use of them took 34 registers, past what 256 invocations may have. Measured: eglcompute's reduce case passes at 64, 128 and 256 invocations, in three runs out of three, at 17 registers. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 77 +++++++++++++++++++++++++++ src/imagination/g9p/g9p_compile.h | 6 +++ src/imagination/vulkan/pvr_arch_g9p.c | 35 ++++++++++++ src/imagination/vulkan/pvr_device.h | 9 ++++ 4 files changed, 127 insertions(+) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index a1a0bf6928d..0299f3cdef2 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5070,6 +5070,9 @@ rematerialize_loads(nir_shader *nir, bool every_use) intr->intrinsic != nir_intrinsic_load_interpolated_input && intr->intrinsic != nir_intrinsic_load_frag_coord && intr->intrinsic != nir_intrinsic_load_point_coord && + /* compute: the dispatch's words, read again where used (lower_task_barrier()) */ + intr->intrinsic != nir_intrinsic_load_workgroup_id && + intr->intrinsic != nir_intrinsic_load_num_workgroups && !(intr->intrinsic == nir_intrinsic_load_input && (nir->info.stage == MESA_SHADER_FRAGMENT || every_use))) continue; @@ -6520,6 +6523,72 @@ lower_storage_image(nir_builder *b, nir_intrinsic_instr *in, void *data) return true; } +/* + * A workgroup barrier of a group of more than one task. The tasks of a group + * run apart, so the barrier the hardware does (a wait for memory) left + * eglcompute's sums through shared memory wrong from 128 invocations up. As + * the GLES driver did it, with coherent atomics on a word of the device's + * counters (G9P_BARRIER_COUNTERS): lane 0 of each task adds 1, whose low byte + * counts the tasks in. The last one adds 256 - tasks, which sets the count + * back to 0 and advances the generation above it, and the others wait for the + * generation to change. A task's lanes run in step, so they wait with their + * lane 0. One word, so that the barrier keeps few registers. Words by + * workgroup modulo G9P_BARRIER_SLOTS: so many groups are never resident at + * once. + */ +static bool +lower_task_barrier(nir_builder *b, nir_intrinsic_instr *intr, void *data) +{ + const unsigned *tasks = data; + + if (intr->intrinsic != nir_intrinsic_barrier || + nir_intrinsic_execution_scope(intr) != SCOPE_WORKGROUP) + return false; + b->cursor = nir_after_instr(&intr->instr); + nir_def *index = nir_vulkan_resource_index(b, 2, 32, nir_imm_int(b, 0), + .desc_set = G9P_BARRIER_COUNTERS, .binding = 0, + .desc_type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); + nir_def *lane0 = nir_ieq_imm(b, nir_iand_imm(b, nir_load_local_invocation_index(b), 31), 0); + + nir_push_if(b, lane0); + { + nir_def *id = nir_load_workgroup_id(b); + nir_def *groups = nir_load_num_workgroups(b); + nir_def *flat = nir_iadd(b, nir_channel(b, id, 0), + nir_imul(b, nir_channel(b, groups, 0), + nir_iadd(b, nir_channel(b, id, 1), + nir_imul(b, nir_channel(b, groups, 1), + nir_channel(b, id, 2))))); + nir_def *word = nir_ishl_imm(b, nir_iand_imm(b, flat, G9P_BARRIER_SLOTS - 1), 2); + nir_def *old = nir_ssbo_atomic(b, 32, index, word, nir_imm_int(b, 1), + .atomic_op = nir_atomic_op_iadd); + + nir_push_if(b, nir_ieq_imm(b, nir_iand_imm(b, old, 0xff), *tasks - 1)); + { + nir_ssbo_atomic(b, 32, index, word, nir_imm_int(b, 256 - *tasks), + .atomic_op = nir_atomic_op_iadd); + } + nir_push_else(b, NULL); + { + nir_def *generation = nir_ushr_imm(b, old, 8); + nir_loop *loop = nir_push_loop(b); + { + nir_def *now = nir_ssbo_atomic(b, 32, index, word, nir_imm_int(b, 0), + .atomic_op = nir_atomic_op_iadd); + + nir_break_if(b, nir_ine(b, nir_ushr_imm(b, now, 8), generation)); + } + nir_pop_loop(b, loop); + } + nir_pop_if(b, NULL); + } + nir_pop_if(b, NULL); + /* the memory the other tasks wrote before the barrier, after it */ + nir_barrier(b, .execution_scope = SCOPE_NONE, .memory_scope = SCOPE_WORKGROUP, + .memory_semantics = NIR_MEMORY_ACQ_REL, .memory_modes = nir_intrinsic_memory_modes(intr)); + return true; +} + bool g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) { @@ -6541,6 +6610,14 @@ g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) NIR_PASS(_, nir, nir_opt_dce); NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_image, NULL); } + { + const unsigned threads = nir->info.workgroup_size[0] * nir->info.workgroup_size[1] * + nir->info.workgroup_size[2]; + unsigned tasks = DIV_ROUND_UP(threads, 32); + + if (tasks > 1 && !getenv("G9P_NO_TASK_BARRIER")) + NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_task_barrier, nir_metadata_none, &tasks); + } NIR_PASS(_, nir, nir_lower_compute_system_values, &csv); return compile_back(nir, out, MESA_SHADER_COMPUTE, NULL, 0, false, false, mem_ctx, false, false); } diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index 0f66c19f034..fbe43ada52e 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -128,6 +128,12 @@ g9p_fs_buf_base(unsigned n) #define G9P_BLEND_CONSTANTS (~5u) /* The set of target t's memory (binding t) when it overflows: address, pitch (3 dwords) */ #define G9P_OVERFLOW_TARGETS (~20u) +/* + * The set of the device's barrier counters (binding 0), for workgroups of more + * than one task: G9P_BARRIER_SLOTS slots of a count and a generation + */ +#define G9P_BARRIER_COUNTERS (~27u) +#define G9P_BARRIER_SLOTS 4096u /* The set of a draw's coverage state (binding 0): the sample mask, alpha to coverage */ #define G9P_COVERAGE (~21u) /* The set of the front face state: one word, ~0 for clockwise */ diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 9588c081da1..15cc82d4269 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -52,6 +52,7 @@ PVR_PER_ARCH(g9p_device_init)(struct pvr_device *device) simple_mtx_init(&device->g9p_vmas.lock, mtx_plain); simple_mtx_init(&device->g9p.zls_scratch.lock, mtx_plain); + simple_mtx_init(&device->g9p.barriers.lock, mtx_plain); util_dynarray_init(&device->g9p_vmas.vmas, NULL); for (unsigned i = 0; i < ARRAY_SIZE(empty); i++) @@ -118,6 +119,9 @@ PVR_PER_ARCH(g9p_device_finish)(struct pvr_device *device) pvr_bo_free(device, device->g9p.zls_scratch.bo); } simple_mtx_destroy(&device->g9p.zls_scratch.lock); + if (device->g9p.barriers.bo) + pvr_bo_free(device, device->g9p.barriers.bo); + simple_mtx_destroy(&device->g9p.barriers.lock); util_dynarray_fini(&device->g9p_vmas.vmas); simple_mtx_destroy(&device->g9p_vmas.lock); } @@ -151,6 +155,28 @@ PVR_PER_ARCH(g9p_zls_scratch)(struct pvr_device *device) return addr; } +/* The barrier counters (device->g9p.barriers, G9P_BARRIER_COUNTERS): their device address, 0 if none */ +static uint64_t +g9p_barrier_counters(struct pvr_device *device) +{ + uint64_t addr = 0; + + simple_mtx_lock(&device->g9p.barriers.lock); + if (!device->g9p.barriers.bo) { + struct pvr_bo *bo; + + if (pvr_bo_alloc(device, device->heaps.general_heap, 8 * G9P_BARRIER_SLOTS, 64, + PVR_BO_ALLOC_FLAG_CPU_MAPPED, &bo) == VK_SUCCESS) { + memset(bo->bo->map, 0, 8 * G9P_BARRIER_SLOTS); + device->g9p.barriers.bo = bo; + } + } + if (device->g9p.barriers.bo) + addr = device->g9p.barriers.bo->vma->dev_addr.addr; + simple_mtx_unlock(&device->g9p.barriers.lock); + return addr; +} + VkResult PVR_PER_ARCH(g9p_compute_pipeline_compile)( struct pvr_device *device, @@ -301,6 +327,15 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, uniforms[2 * k] = group_count[state->buffers[k].binding ? 2 : 0]; uniforms[2 * k + 1] = state->buffers[k].binding ? 0 : group_count[1]; } + if (state->buffers[k].set == G9P_BARRIER_COUNTERS) { + const uint64_t addr = g9p_barrier_counters(cmd_buffer->device); + + if (!addr) + return vk_error(cmd_buffer, VK_ERROR_OUT_OF_DEVICE_MEMORY); + uniforms[2 * k] = addr; + uniforms[2 * k + 1] = addr >> 32; + from_uniforms[k] = true; + } if (state->buffers[k].set == G9P_PUSH_CONSTANTS) { const struct pvr_push_constants *pc = &cmd_buffer->state.push_consts[PVR_STAGE_ALLOCATION_COMPUTE]; diff --git a/src/imagination/vulkan/pvr_device.h b/src/imagination/vulkan/pvr_device.h index 60922da0675..9998e8a2c00 100644 --- a/src/imagination/vulkan/pvr_device.h +++ b/src/imagination/vulkan/pvr_device.h @@ -167,6 +167,15 @@ struct pvr_device { simple_mtx_t lock; struct pvr_bo *bo; } zls_scratch; + /* + * The counters of compute barriers across tasks (G9P_BARRIER_COUNTERS): + * allocated zeroed at the first such pipeline, each count back at 0 + * after its barrier + */ + struct { + simple_mtx_t lock; + struct pvr_bo *bo; + } barriers; } g9p; /* Compute shaders for queries. */ From a7f531596f8b3fd45123ad43136487fd3ebbfc84 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 16:47:55 -0600 Subject: [PATCH 32/44] pvr/g9p: atomics on shared variables, through memory The A10 has no atomics for its shared memory, and the backend refused them ("intrinsic shared_atomic not supported yet"), so eglcompute's shared case could not create its pipeline. A compute shader with atomics on shared variables now keeps its shared variables in memory instead, a region of the variables' size per workgroup, where the storage-buffer atomics are coherent. It is the A9's pass for shared memory past its size (a9_spill_shared()), and its barriers also order that memory. The dispatch allocates the regions. Measured: eglcompute's shared case passes (an atomicAdd and atomicMax on shared counters from every invocation), and its reduce, atomics and counter cases still do. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 24 ++++++++++++++++ src/imagination/vulkan/pvr_arch_g9p.c | 40 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 0299f3cdef2..259f9fd74ac 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -6523,6 +6523,8 @@ lower_storage_image(nir_builder *b, nir_intrinsic_instr *in, void *data) return true; } +static bool a9_spill_shared(nir_builder *b, nir_intrinsic_instr *in, void *data); + /* * A workgroup barrier of a group of more than one task. The tasks of a group * run apart, so the barrier the hardware does (a wait for memory) left @@ -6610,6 +6612,28 @@ g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) NIR_PASS(_, nir, nir_opt_dce); NIR_PASS(_, nir, nir_remove_dead_variables, nir_var_image, NULL); } + /* + * Atomics on shared variables: the A10 has none for its shared memory, so such a + * shader keeps its shared variables in memory, a region per workgroup, where the + * storage-buffer atomics work (a9_spill_shared(), as the A9's go past its size) + */ + { + bool shared_atomics = false; + + nir_foreach_function_impl (impl, nir) + nir_foreach_block (block, impl) + nir_foreach_instr (instr, block) + if (instr->type == nir_instr_type_intrinsic && + (nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_shared_atomic || + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_shared_atomic_swap)) + shared_atomics = true; + if (shared_atomics && nir->info.shared_size) { + out->shared_spill_stride = ALIGN_POT(nir->info.shared_size, 64); + NIR_PASS(_, nir, nir_shader_intrinsics_pass, a9_spill_shared, nir_metadata_control_flow, + &out->shared_spill_stride); + nir->info.shared_size = 0; + } + } { const unsigned threads = nir->info.workgroup_size[0] * nir->info.workgroup_size[1] * nir->info.workgroup_size[2]; diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 15cc82d4269..cd42339cb4b 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -6,6 +6,7 @@ * of the pco/Rogue paths. */ +#include #include #include "vk_pipeline.h" @@ -238,6 +239,7 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( state->uses_workgroup_id = shader.uses_workgroup_id; memcpy(state->local_size, shader.local_size, sizeof(state->local_size)); state->shared_size = shader.shared_size; + state->shared_spill_stride = shader.shared_spill_stride; out: ralloc_free(mem_ctx); @@ -317,11 +319,49 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, unsigned bind_dwords, ctrl0, nc = 0; VkResult result; + /* + * Shared variables in memory (a9_spill_shared(): a shader with atomics on them): + * a region of the pipeline's stride per workgroup, for this dispatch + */ + struct pvr_bo *shared_spill = NULL; + + if (state->shared_spill_stride) { + const uint64_t groups = (uint64_t)group_count[0] * group_count[1] * group_count[2]; + struct pvr_suballoc_bo *owner; + + if (groups > (64u << 20) / state->shared_spill_stride) + return vk_errorf(cmd_buffer, VK_ERROR_OUT_OF_DEVICE_MEMORY, + "G9P: %" PRIu64 " workgroups of %u bytes of shared variables in memory", + groups, state->shared_spill_stride); + owner = vk_zalloc(&cmd_buffer->device->vk.alloc, sizeof(*owner), 8, + VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); + if (!owner) + return vk_error(cmd_buffer, VK_ERROR_OUT_OF_HOST_MEMORY); + result = pvr_bo_alloc(cmd_buffer->device, cmd_buffer->device->heaps.general_heap, + groups * state->shared_spill_stride, 64, PVR_BO_ALLOC_FLAG_CPU_MAPPED, + &shared_spill); + if (result != VK_SUCCESS) { + vk_free(&cmd_buffer->device->vk.alloc, owner); + return result; + } + /* freed with the command buffer's uploads (pvr_arch_g5p_compute.c does the same) */ + owner->allocator = pvr_cmd_suballocator(cmd_buffer->device, &cmd_buffer->device->suballoc_general); + owner->bo = shared_spill; + owner->dev_addr = shared_spill->vma->dev_addr; + owner->size = groups * state->shared_spill_stride; + list_add(&owner->link, &cmd_buffer->bo_list); + } + /* * Values the DMA copies that are not in descriptor memory: the * push-constant buffer's address and the thread count. */ for (unsigned k = 0; k < n; k++) { + if (state->buffers[k].set == G9P_SHARED_SPILL) { + uniforms[2 * k] = shared_spill ? shared_spill->vma->dev_addr.addr : 0; + uniforms[2 * k + 1] = shared_spill ? shared_spill->vma->dev_addr.addr >> 32 : 0; + from_uniforms[k] = true; + } if (state->buffers[k].set == G9P_NUM_WORKGROUPS) { /* gl_NumWorkGroups: x and y in binding 0's slot, z in binding 1's */ uniforms[2 * k] = group_count[state->buffers[k].binding ? 2 : 0]; From 6b56c3d0d12f98de836f768f8804586f70d3b977 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 16:49:11 -0600 Subject: [PATCH 33/44] pvr/g9p: indirect compute dispatches An indirect dispatch ran one workgroup, and gl_NumWorkGroups read 1, 1, 1 (eglcompute's misc case got 101 for 302). The G9P dispatch took the group count it was recorded with. The bind DMA now copies gl_NumWorkGroups from the arguments themselves, and the queue fills the kernel record's group counts from them when the job is submitted, after the jobs before it are done (they may write the arguments), as indirect draws do. A dispatch of no groups runs the empty kernel once instead, since the record has no count of 0. An indirect dispatch of a shader whose shared variables are in memory is refused, as the A9 refuses it: the regions are sized by the group count. Measured: eglcompute's misc case passes (the group count, the workgroup size, uniforms and an indirect dispatch), and an indirect dispatch of 5 x 2 x 3 groups with arguments written by a compute shader runs 240 invocations and reads its count, where one of 0 x 2 x 3 runs none. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 6 +- src/imagination/vulkan/pvr_arch_g9p.c | 62 +++++++++++++++++++- src/imagination/vulkan/pvr_arch_queue.c | 14 +++++ src/imagination/vulkan/pvr_cmd_buffer.h | 18 ++++++ src/imagination/vulkan/pvr_g9p.h | 8 ++- 5 files changed, 103 insertions(+), 5 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index eb6a52c4c08..694294e32d1 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -229,6 +229,7 @@ static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, case PVR_SUB_CMD_TYPE_COMPUTE: case PVR_SUB_CMD_TYPE_QUERY: + util_dynarray_fini(&sub_cmd->compute.g9p_indirects); vk_free(&cmd_buffer->vk.pool->alloc, sub_cmd->compute.g9p_query); for (struct pvr_g5p_dispatch_log *l = sub_cmd->compute.g5p_log_first, *next; l; l = next) { next = l->next; @@ -3235,6 +3236,7 @@ VkResult pvr_arch_cmd_buffer_start_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, pvr_csb_init(device, PVR_CMD_STREAM_TYPE_COMPUTE, &sub_cmd->compute.control_stream); + sub_cmd->compute.g9p_indirects = UTIL_DYNARRAY_INIT; break; case PVR_SUB_CMD_TYPE_TRANSFER: @@ -7419,8 +7421,8 @@ static void pvr_cmd_dispatch( return; PVR_G5P_HOOK(compute_dispatch) if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g9p)) { - /* Indirect dispatches and base groups are not supported yet */ - result = pvr_arch_g9p_cmd_dispatch(cmd_buffer, sub_cmd, workgroup_size); + /* base groups are not supported yet */ + result = pvr_arch_g9p_cmd_dispatch(cmd_buffer, sub_cmd, workgroup_size, indirect_addr.addr); if (result != VK_SUCCESS) pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result); goto end_cmd_dispatch; diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index cd42339cb4b..e59b1881a1b 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -299,7 +299,8 @@ PVR_PER_ARCH(g9p_storage_image_record)(const struct pvr_image_view *iview, VkResult PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_compute *sub_cmd, - const uint32_t group_count[static 3]) + const uint32_t group_count[static 3], + uint64_t indirect) { struct pvr_device *device = cmd_buffer->device; const struct pvr_compute_pipeline *pipeline = cmd_buffer->state.compute_pipeline; @@ -325,6 +326,9 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, */ struct pvr_bo *shared_spill = NULL; + if (state->shared_spill_stride && indirect) + return vk_errorf(cmd_buffer, VK_ERROR_FEATURE_NOT_PRESENT, + "G9P: an indirect dispatch of shared variables in memory"); if (state->shared_spill_stride) { const uint64_t groups = (uint64_t)group_count[0] * group_count[1] * group_count[2]; struct pvr_suballoc_bo *owner; @@ -443,6 +447,9 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, /* pvr_buffer_descriptor: the address comes first, the size at byte 8 */ src = set->dev_addr.addr + binding->offset + element * binding->stride + (state->buffers[k].binding & G9P_BINDING_SIZE ? 8 : 0); + } else if (k < n && indirect && state->buffers[k].set == G9P_NUM_WORKGROUPS) { + /* an indirect dispatch's x and y, or z, straight from its arguments */ + src = indirect + (state->buffers[k].binding ? 8 : 0); } else { src = uniforms_bo->dev_addr.addr + 8 * k; if (k == n) @@ -516,8 +523,26 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, pvr_csb_set_relocation_mark(csb); emit_record(csb, &bind_rec); - emit_record(csb, &kernel_rec); + uint32_t *const kernel_words = pvr_arch_csb_alloc_dwords(csb, 8); + + if (kernel_words) + memcpy(kernel_words, kernel_rec.w, sizeof(kernel_rec.w)); pvr_csb_clear_relocation_mark(csb); + if (indirect) { + /* the arguments may come from a job before: always waited for */ + const struct g9p_dispatch_indirect patch = { + .groups = kernel_words ? kernel_words + 3 : NULL, + .kick = pvr_bo_suballoc_get_map_addr(kernel.pvr_bo), + .empty_kernel = usc_offset(device, device->g9p.empty_kernel), + .args = (const uint32_t *)PVR_PER_ARCH(g9p_cpu_addr)(device, indirect), + .gpu_written = true, + }; + + if (!patch.groups || !patch.args) + return vk_errorf(cmd_buffer, VK_ERROR_FEATURE_NOT_PRESENT, + "G9P: an indirect dispatch's arguments without a CPU mapping"); + util_dynarray_append_typed(&sub_cmd->g9p_indirects, struct g9p_dispatch_indirect, patch); + } } return VK_SUCCESS; @@ -549,3 +574,36 @@ PVR_PER_ARCH(g9p_end_pass)(struct pvr_cmd_buffer *cmd_buffer, memcpy(w, words, 32 * sizeof(uint32_t)); pvr_csb_clear_relocation_mark(csb); } + +/* + * An indirect dispatch's group counts from its arguments, when the job is + * submitted: the kernel record's words 3-5. A dispatch of no groups runs the + * empty kernel once instead (its record has no count of 0). + */ +void +PVR_PER_ARCH(g9p_patch_indirect_dispatches)(struct pvr_sub_cmd_compute *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_dispatch_indirect, p) { + const uint32_t x = p->args[0], y = p->args[1], z = p->args[2]; + + if (!x || !y || !z) { + p->groups[0] = p->groups[1] = p->groups[2] = 0; + p->kick[0] = p->empty_kernel; + } else { + p->groups[0] = x - 1; + p->groups[1] = y - 1; + p->groups[2] = z - 1; + } + } +} + +/* Arguments a GPU job before may have written */ +bool +PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait)(const struct pvr_sub_cmd_compute *sub_cmd) +{ + util_dynarray_foreach (&sub_cmd->g9p_indirects, struct g9p_dispatch_indirect, p) { + if (p->gpu_written) + return true; + } + return false; +} diff --git a/src/imagination/vulkan/pvr_arch_queue.c b/src/imagination/vulkan/pvr_arch_queue.c index 545c958a3e5..4f0a5f7a2e1 100644 --- a/src/imagination/vulkan/pvr_arch_queue.c +++ b/src/imagination/vulkan/pvr_arch_queue.c @@ -750,6 +750,20 @@ static VkResult pvr_process_compute_cmd(struct pvr_device *device, return result; } } + /* G9P: indirect dispatches' group counts, once what writes their arguments is done */ + if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p) && + util_dynarray_num_elements(&sub_cmd->g9p_indirects, char)) { + struct vk_sync *wait = queue->next_job_wait_sync[PVR_JOB_TYPE_COMPUTE]; + + if (wait && PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait)(sub_cmd)) { + result = vk_sync_wait(&device->vk, wait, 0, VK_SYNC_WAIT_COMPLETE, UINT64_MAX); + if (result != VK_SUCCESS) { + vk_sync_destroy(&device->vk, sync); + return result; + } + } + PVR_PER_ARCH(g9p_patch_indirect_dispatches)(sub_cmd); + } result = pvr_arch_compute_job_submit( queue->compute_ctx, diff --git a/src/imagination/vulkan/pvr_cmd_buffer.h b/src/imagination/vulkan/pvr_cmd_buffer.h index 8c9b6eaae46..87168ee1fcb 100644 --- a/src/imagination/vulkan/pvr_cmd_buffer.h +++ b/src/imagination/vulkan/pvr_cmd_buffer.h @@ -280,6 +280,24 @@ struct pvr_sub_cmd_compute { bool g5p_kernel_since_end; struct pvr_g5p_dispatch_log *g5p_log_first, *g5p_log_last; unsigned g5p_log_count; + /* + * G9P: indirect dispatches, their kernel records' group counts filled from + * the arguments when the job is submitted (struct g9p_dispatch_indirect) + */ + struct util_dynarray g9p_indirects; +}; + +/* An indirect dispatch's words to fill at submission (g9p_patch_indirect_dispatches()) */ +struct g9p_dispatch_indirect { + /* the kernel record's words 3-5: groups - 1 */ + uint32_t *groups; + /* the kernel's PDS data: its USC program, the empty kernel for no groups */ + uint32_t *kick; + uint32_t empty_kernel; + /* VkDispatchIndirectCommand, CPU-mapped */ + const uint32_t *args; + /* the arguments' buffer can be written by the GPU: wait for the jobs before */ + bool gpu_written; }; struct pvr_sub_cmd_transfer { diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index fc596c36f05..0e93f2f0f30 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -119,7 +119,11 @@ VkResult PVR_PER_ARCH(g9p_compute_pipeline_compile)( VkResult PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_compute *sub_cmd, - const uint32_t group_count[static 3]); + const uint32_t group_count[static 3], + uint64_t indirect); +/* The group counts of a compute job's indirect dispatches, from their arguments */ +void PVR_PER_ARCH(g9p_patch_indirect_dispatches)(struct pvr_sub_cmd_compute *sub_cmd); +bool PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait)(const struct pvr_sub_cmd_compute *sub_cmd); void PVR_PER_ARCH(g9p_end_pass)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_compute *sub_cmd); @@ -250,6 +254,8 @@ bool PVR_PER_ARCH(g9p_meta_upload)(struct pvr_cmd_buffer *cmd_buffer, const VkCo # define pvr_arch_g9p_compute_pipeline_compile \ PVR_PER_ARCH(g9p_compute_pipeline_compile) # define pvr_arch_g9p_cmd_dispatch PVR_PER_ARCH(g9p_cmd_dispatch) +# define pvr_arch_g9p_patch_indirect_dispatches PVR_PER_ARCH(g9p_patch_indirect_dispatches) +# define pvr_arch_g9p_dispatch_indirect_needs_wait PVR_PER_ARCH(g9p_dispatch_indirect_needs_wait) # define pvr_arch_g9p_end_pass PVR_PER_ARCH(g9p_end_pass) #endif /* PVR_PER_ARCH */ From fe3a837b9b92242fb1be13da1bcfd4b93a677937 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 18:17:35 -0600 Subject: [PATCH 34/44] pvr/g9p: two and eight samples per pixel The A10 renders 2, 4 and 8 samples a pixel. Its kernel already sets the tiling for each count (TE_AA 1, 5 and 7, the ISP macrotile size) and gives the TA Vulkan's standard positions. The driver offered only 4. - The render takes Vulkan's standard positions in 0xfa8 and 0xc78, ISP_AA log2 of the count, and region headers for tiles of 1x2, 2x2 or 2x4. - A load runs per sample and finds the sample's index from its y, which differs between the samples of a pixel at every count: 2x by y >> 3, 4x by y >> 2, and 8x through a table of nibbles by y >> 1. - A multisampled image view takes the texture unit's multisample form (dimension 4, log2 of the count less one at bit 55, the pixels' size), which the background's loads already used. A shader's texelFetch reads it the same way, with the pixel in r0-r1 and the sample index in r2. The 2D texture of twice the size that four samples were read as has no counterpart at 2x or 8x, whose samples follow one another in a pixel. Such fetches work in fragment shaders only. - Alpha to coverage keeps a nested set for each count. - The CPU transfers address a pixel's samples one after another at any count, for clears, copies and resolves. The host image copies set the sample fields of their surfaces too. eglmsaa and eglcoverage now pass every case at 2x, 4x and 8x. egles31 measures each sample's position at 2x, 4x and 8x, and all match what glGetMultisamplefv reports. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 108 ++++++---- src/imagination/g9p/g9p_compile.h | 3 +- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 201 ++++++++++-------- .../vulkan/pvr_arch_g9p_transfer.c | 109 +++++++--- src/imagination/vulkan/pvr_device.h | 6 +- src/imagination/vulkan/pvr_physical_device.c | 23 +- 6 files changed, 281 insertions(+), 169 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 259f9fd74ac..e2b949e0bde 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -1372,7 +1372,7 @@ only_tex_uses(nir_def *def) const nir_tex_instr *tex = nir_instr_as_tex(nir_src_use_instr(src)); if (tex->is_array || tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE || - tex->sampler_dim == GLSL_SAMPLER_DIM_3D || + tex->sampler_dim == GLSL_SAMPLER_DIM_3D || tex->op == nir_texop_txf_ms || tex->op == nir_texop_txl || tex->op == nir_texop_txb) return false; } @@ -2911,8 +2911,9 @@ sample_lod(struct g9p_ctx *ctx, nir_tex_instr *tex, unsigned at, int lod) } /* - * The fragment shader samples a 2D array or a cube, whose coordinates take - * r0-r3 (emit_tex_layered()) + * The fragment shader samples a 2D array or a cube, or fetches from a + * multisampled texture, whose coordinates take r0-r3 (emit_tex_layered(), + * emit_txf_ms()) */ static bool uses_layered_tex(nir_function_impl *impl) @@ -2920,7 +2921,7 @@ uses_layered_tex(nir_function_impl *impl) nir_foreach_block(block, impl) { nir_foreach_instr(instr, block) { if (instr->type == nir_instr_type_tex && - (nir_instr_as_tex(instr)->is_array || + (nir_instr_as_tex(instr)->is_array || nir_instr_as_tex(instr)->op == nir_texop_txf_ms || nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_CUBE || nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_3D)) return true; @@ -3154,6 +3155,51 @@ emit_tex_layered(struct g9p_ctx *ctx, nir_tex_instr *tex) return true; } +/* + * texelFetch of a multisampled texture: the texture unit's multisample form + * (g9p_pack_image_view()), dimension 4 in W0 and integer coordinates (W1 bit + * 6), the pixel in r0-r1 and the sample index in r2, as the background reads + * a multisampled target. r0-r3 are reserved in such shaders (generate()). + */ +static bool +emit_txf_ms(struct g9p_ctx *ctx, nir_tex_instr *tex) +{ + const int ci = nir_tex_instr_src_index(tex, nir_tex_src_coord); + const int si = nir_tex_instr_src_index(tex, nir_tex_src_ms_index); + const int slot = texture_slot(ctx, tex); + unsigned at; + int dst; + + if (ctx->stage != MESA_SHADER_FRAGMENT || ci < 0 || slot < 0 || tex->is_array) { + fprintf(stderr, "g9p: multisample fetches of 2D textures in fragment shaders only\n"); + return false; + } + for (unsigned c = 0; c < 3; c++) { + const int r = c < 2 ? src_reg(ctx, nir_get_scalar(tex->src[ci].src.ssa, c)) + : si >= 0 ? src_reg(ctx, nir_get_scalar(tex->src[si].src.ssa, 0)) + : const_reg(ctx, 0); + + if (r < 0) + return false; + g9p_alu2(ctx->c, G9P_OR, c, r, r, false); + } + release(ctx, &tex->instr); + dst = alloc_range(ctx, 4); + if (dst < 0) + return false; + at = ctx->c->n; + G9P_EMIT(ctx->c, 0xf91a, g9p_sample_w1(dst, tex_state_dword(ctx, slot)) | 0x40, + 0x8100 | tex_sampler_dword(ctx, slot), 0x0700, 0x0200); + note_sample(ctx, tex, at); + g9p_wait(ctx->c); + for (unsigned c = 0; c < 4; c++) { + ctx->reg[key(&tex->def, c)] = dst + c; + if (c >= tex->def.num_components || !ctx->last_use[key(&tex->def, c)]) + ctx->busy &= ~BITFIELD64_BIT(dst + c); + } + return true; +} + /* * A vertex shader's sample: explicit level (level 0 without one), the * coordinates in registers of their own, as r0-r1 hold the vertex and instance @@ -3269,6 +3315,8 @@ emit_tex(struct g9p_ctx *ctx, nir_tex_instr *tex) if (tex->op == nir_texop_txs) return emit_txs(ctx, tex); + if (tex->op == nir_texop_txf_ms) + return emit_txf_ms(ctx, tex); if (ctx->stage == MESA_SHADER_VERTEX) return emit_tex_vertex(ctx, tex); if (tex->is_array || tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE || @@ -4949,16 +4997,23 @@ lower_coverage(nir_shader *nir, const struct g9p_coverage *coverage) if (alpha) { nir_def *enabled = nir_load_ubo(&b, 1, 32, index, nir_imm_int(&b, 4), .align_mul = 4, .range = 8); - nir_def *n = nir_f2u32(&b, nir_fadd_imm(&b, nir_fmul_imm(&b, nir_fsat(&b, alpha), 4.0f), 0.5f)); + const float count = coverage->samples == 2 || coverage->samples == 8 ? coverage->samples : 4; + nir_def *n = nir_f2u32(&b, nir_fadd_imm(&b, nir_fmul_imm(&b, nir_fsat(&b, alpha), count), 0.5f)); /* - * a nested set, samples 0, 3, 1 and 2 in turn: two kept are a diagonal pair - * (G9P_POSITIONS_4X), as the GLES driver kept them and eglcoverage expects + * a nested set by the count kept. 4x: samples 0, 3, 1 and 2 in turn, so two + * kept are a diagonal pair, as the GLES driver kept them. 8x: the centre + * ones first, samples 0, 1, 5, 2, 3, 6, 7 and 4 (Vulkan's positions). 2x: + * sample 0, then 1. */ - nir_def *by_alpha = nir_bcsel(&b, nir_ieq_imm(&b, n, 0), nir_imm_int(&b, 0x0), - nir_bcsel(&b, nir_ieq_imm(&b, n, 1), nir_imm_int(&b, 0x1), - nir_bcsel(&b, nir_ieq_imm(&b, n, 2), nir_imm_int(&b, 0x9), - nir_bcsel(&b, nir_ieq_imm(&b, n, 3), nir_imm_int(&b, 0xb), - nir_imm_int(&b, 0xf))))); + static const uint32_t nested2[] = { 0x0, 0x1, 0x3 }; + static const uint32_t nested4[] = { 0x0, 0x1, 0x9, 0xb, 0xf }; + static const uint32_t nested8[] = { 0x00, 0x01, 0x03, 0x23, 0x27, 0x2f, 0x6f, 0xef, 0xff }; + const unsigned samples = coverage->samples == 2 || coverage->samples == 8 ? coverage->samples : 4; + const uint32_t *nested = samples == 2 ? nested2 : samples == 8 ? nested8 : nested4; + nir_def *by_alpha = nir_imm_int(&b, nested[samples]); + + for (int k = samples - 1; k >= 0; k--) + by_alpha = nir_bcsel(&b, nir_ieq_imm(&b, n, k), nir_imm_int(&b, nested[k]), by_alpha); mask = nir_iand(&b, mask, nir_bcsel(&b, nir_ine_imm(&b, enabled, 0), by_alpha, nir_imm_int(&b, ~0))); } @@ -5512,34 +5567,7 @@ lower_tg4(nir_builder *b, nir_tex_instr *tex, void *data) static bool lower_txf(nir_builder *b, nir_tex_instr *tex, void *data) { - /* - * A four-sample texture reads as a 2D one of twice the size, each pixel's samples - * its 2x2 block with the index in Morton order, y in bit 0 (g9p_pack_image_view()) - */ - if (tex->op == nir_texop_txs && tex->sampler_dim == GLSL_SAMPLER_DIM_MS) { - b->cursor = nir_after_instr(&tex->instr); - nir_def *half = nir_ushr_imm(b, &tex->def, 1); - - nir_def_rewrite_uses_after(&tex->def, half); - tex->sampler_dim = GLSL_SAMPLER_DIM_2D; - return true; - } - if (tex->op == nir_texop_txf_ms) { - const int coord_i = nir_tex_instr_src_index(tex, nir_tex_src_coord); - const int ms_i = nir_tex_instr_src_index(tex, nir_tex_src_ms_index); - - if (coord_i < 0 || ms_i < 0) - return false; - b->cursor = nir_before_instr(&tex->instr); - nir_def *c = tex->src[coord_i].src.ssa, *s = tex->src[ms_i].src.ssa; - nir_def *x = nir_iadd(b, nir_ishl_imm(b, nir_channel(b, c, 0), 1), nir_ushr_imm(b, s, 1)); - nir_def *y = nir_iadd(b, nir_ishl_imm(b, nir_channel(b, c, 1), 1), nir_iand_imm(b, s, 1)); - - nir_tex_instr_remove_src(tex, ms_i); - nir_src_rewrite(&tex->src[nir_tex_instr_src_index(tex, nir_tex_src_coord)].src, nir_vec2(b, x, y)); - tex->op = nir_texop_txf; - tex->sampler_dim = GLSL_SAMPLER_DIM_2D; - } + /* a multisampled texture's fetch and size: emit_txf_ms() and its state's own size */ const int ci = nir_tex_instr_src_index(tex, nir_tex_src_coord); const int li = nir_tex_instr_src_index(tex, nir_tex_src_lod); const unsigned n = tex->coord_components - tex->is_array; diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index fbe43ada52e..c58e8bbd76b 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -328,11 +328,12 @@ bool g9p_compile_vertex(nir_shader *nir, const struct g9p_vertex_attrib *attribs * pipeline to mark its draws translucent. */ /* - * A four-sample pipeline: the fragment program writes its coverage from the draw's + * A multisampled pipeline: the fragment program writes its coverage from the draw's * sample mask and alpha to coverage, the block of G9P_COVERAGE (binding 0: mask, enable) */ struct g9p_coverage { bool dynamic; + uint8_t samples; /* 2, 4 or 8 */ }; bool g9p_folds_discards(const nir_shader *nir); diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 4e92a42a3ce..738be1715ab 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -121,8 +121,24 @@ usc_temps(unsigned regs) return DIV_ROUND_UP(MAX2(regs, 8), 4) << 3; } -/* Vulkan's standard 4x positions, x | y << 4 per sample in 1/16 pixel (iOS msaa4, PR #6) */ -#define G9P_POSITIONS_4X 0xeaa26e26u +/* + * Vulkan's standard sample positions, x | y << 4 per sample in 1/16 pixel (4x: iOS + * msaa4, PR #6), the same words the kernel gives the TA (its multisample control) + */ +static uint64_t +g9p_positions(unsigned samples) +{ + switch (samples) { + case 2: + return 0x44ccull; + case 4: + return 0xeaa26e26ull; + case 8: + return 0x1ffb71d3359db759ull; + default: + return 0x88ull; + } +} /* DOUTD control word: last, common store, destination dword, dwords */ static uint32_t @@ -188,11 +204,11 @@ g9p_pack_clear(VkFormat format, const float colour[4], uint32_t *words) } /* - * A fragment-side kick at four samples: DOUTU's sample rate 2, data word 1 bits 1-2 + * A fragment-side kick at several samples: DOUTU's sample rate 2, data word 1 bits 1-2 * (src0[34:33]; iOS 0x10 -> 0x14, PR #6), for the same program. Only the kick differs. */ static VkResult -g9p_kick_4x(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_pds_upload *kick, struct pvr_pds_upload *out) +g9p_kick_ms(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_pds_upload *kick, struct pvr_pds_upload *out) { const uint32_t *old = pvr_bo_suballoc_get_map_addr(kick->pvr_bo); const uint32_t data[2] = { old[0], old[1] | 2 << 1 }; @@ -213,15 +229,16 @@ g9p_shift_by(struct g9p_code *c, unsigned dst, unsigned a, unsigned n, bool righ } /* - * A load at four samples, in its task per sample (g9p_kick_4x()): the pixel in + * A load at several samples, in its task per sample (g9p_kick_ms()): the pixel in * r0-r1 and the sample's index in r2, for g9p_ms_sample(). 111c 0006 is the * sample's y (measured: the samples of a pixel differ, where 0002 is the - * centre's), and 211c 6800 the pixel centre, both in 1/16 pixel. The - * positions go down in y (G9P_POSITIONS_4X), so the index is (y >> 2) & 3. - * Uses r6 and r8. + * centre's), and 211c 6800 the pixel centre, both in 1/16 pixel. Each sample + * of a pixel has a y of its own (g9p_positions()), which gives the index: 4x + * (y >> 2) & 3, 2x (y 12 and 4) ((y >> 3) & 1) ^ 1, and 8x (y 5, 11, 9, 3, 13, + * 7, 15 and 1) a table of nibbles by y >> 1. Uses r6 and r8. */ static void -g9p_ms_load_coords(struct g9p_code *c) +g9p_ms_load_coords(struct g9p_code *c, unsigned samples) { G9P_EMIT(c, 0x111c, 0x6900 | 2 * 6, 0x0006); g9p_wait(c); @@ -229,16 +246,36 @@ g9p_ms_load_coords(struct g9p_code *c) g9p_wait(c); g9p_shift_by(c, 0, 0, 4, true, 8); g9p_shift_by(c, 1, 1, 4, true, 8); - g9p_shift_by(c, 2, 6, 2, true, 8); - g9p_mov_imm(c, 8, 3); - g9p_alu2(c, G9P_AND, 2, 2, 8, false); + switch (samples) { + case 2: + g9p_shift_by(c, 2, 6, 3, true, 8); + g9p_mov_imm(c, 8, 1); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + g9p_alu2(c, G9P_XOR, 2, 2, 8, false); + break; + case 8: + g9p_shift_by(c, 2, 6, 1, true, 8); + g9p_mov_imm(c, 8, 7); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + g9p_shift_by(c, 2, 2, 2, false, 8); + g9p_mov_imm(c, 8, 0x64125037); + g9p_shift(c, 2, 8, 2, true); + g9p_mov_imm(c, 8, 7); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + break; + default: + g9p_shift_by(c, 2, 6, 2, true, 8); + g9p_mov_imm(c, 8, 3); + g9p_alu2(c, G9P_AND, 2, 2, 8, false); + break; + } } /* - * A sample of a four-sample texture into @dst: dimension 4 (W0 bits 9-11), + * A sample of a multisampled texture into @dst: dimension 4 (W0 bits 9-11), * integer coordinates (W1 bit 6) in r0-r1 and the sample index in r2, as the * GLES driver read them, with the state at common-store dword @state and the - * sampler at @sampler. Its state is g9p_ms_texture()'s. Read as a 2D texture + * sampler at @sampler. Its state is g9p_pack_image_view()'s. Read as a 2D texture * of twice the size instead, one sample of each task of 32 lanes in a block of * 8 x 4 pixels came from another (measured over whole renders). */ @@ -249,22 +286,6 @@ g9p_ms_sample(struct g9p_code *c, bool half, unsigned dst, unsigned state, unsig 0x0200); } -/* - * The texture state of a four-sample image as g9p_ms_sample() reads it: - * g9p_pack_image_view()'s with dimension 4, the pixels' size and log2(samples) - * - 1 at bit 55 - */ -static void -g9p_ms_texture(const struct pvr_image_view *v, uint64_t tex[2]) -{ - const uint32_t w = v->vk.extent.width, h = v->vk.extent.height; - uint32_t d0 = (uint32_t)tex[0], d1 = (uint32_t)(tex[0] >> 32); - - d0 = (d0 & ~(0x1fu << 27) & ~7u) | (w - 1) << 27 | 4; - d1 = (d1 & ~0x7fffffu) | (w - 1) >> 5 | (h - 1) << 9 | 1u << 23; - tex[0] = d0 | (uint64_t)d1 << 32; -} - /* --- Device programs ------------------------------------------------------ */ VkResult @@ -306,7 +327,7 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) * RGBA32F, 051c for RGBA16F and RGBA8, iOS capture). */ for (unsigned raw = 0; raw < 2; raw++) { - /* A store of four samples: the emit's sample field 2 (iOS msaa4-store, PR #6) */ + /* A store of every sample: the emit's sample field 2 (iOS msaa4-store, PR #6) */ for (unsigned ms = 0; ms < 2; ms++) { c->n = 0; g9p_mov_imm(c, 0, 0); @@ -422,15 +443,17 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) if (result != VK_SUCCESS) goto out; - /* at four samples: each sample its own, from the state in g9p_ms_texture()'s form */ - c->n = 0; - g9p_ms_load_coords(c); - g9p_ms_sample(c, half, 0, 0, 8); - g9p_wait(c); - g9p_end_fragment(c, g9p_output_words(o)); - result = upload_usc(device, c, &p->load_ms_usc[o]); - if (result != VK_SUCCESS) - goto out; + /* at 2, 4 and 8 samples: each sample its own, the state in the multisample form */ + for (unsigned m = 0; m < 3; m++) { + c->n = 0; + g9p_ms_load_coords(c, 2u << m); + g9p_ms_sample(c, half, 0, 0, 8); + g9p_wait(c); + g9p_end_fragment(c, g9p_output_words(o)); + result = upload_usc(device, c, &p->load_ms_usc[m][o]); + if (result != VK_SUCCESS) + goto out; + } } /* @@ -470,8 +493,8 @@ PVR_PER_ARCH(g9p_gfx_device_init)(struct pvr_device *device) result = upload_kick(device, p->keep_usc[o], 8, 0, &p->keep_kick[o]); if (result == VK_SUCCESS) result = upload_kick(device, p->load_usc[o], 8, 0, &p->load_kick[o]); - if (result == VK_SUCCESS) - result = upload_kick(device, p->load_ms_usc[o], 9, 0, &p->load_ms_kick[o]); + for (unsigned m = 0; m < 3 && result == VK_SUCCESS; m++) + result = upload_kick(device, p->load_ms_usc[m][o], 9, 0, &p->load_ms_kick[m][o]); if (result == VK_SUCCESS) result = upload_kick(device, p->clear_raw_usc[o], 8, 0, &p->clear_raw_kick[o]); } @@ -541,11 +564,13 @@ PVR_PER_ARCH(g9p_gfx_device_finish)(struct pvr_device *device) simple_mtx_destroy(&p->cached_lock); for (unsigned o = 0; o < G9P_OUTPUT_COUNT; o++) { pvr_bo_suballoc_free(p->load_usc[o]); - pvr_bo_suballoc_free(p->load_ms_usc[o]); + for (unsigned m = 0; m < 3; m++) + pvr_bo_suballoc_free(p->load_ms_usc[m][o]); pvr_bo_suballoc_free(p->keep_usc[o]); pvr_bo_suballoc_free(p->clear_raw_usc[o]); pvr_bo_suballoc_free(p->load_kick[o].pvr_bo); - pvr_bo_suballoc_free(p->load_ms_kick[o].pvr_bo); + for (unsigned m = 0; m < 3; m++) + pvr_bo_suballoc_free(p->load_ms_kick[m][o].pvr_bo); pvr_bo_suballoc_free(p->keep_kick[o].pvr_bo); pvr_bo_suballoc_free(p->clear_raw_kick[o].pvr_bo); } @@ -1193,13 +1218,15 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo nir_metadata_control_flow, &zero); } } - /* Four samples with a coverage: the fragment program writes it (Zink: static state) */ + /* Several samples with a coverage: the fragment program writes it (Zink: static state) */ /* only a pipeline that drops samples writes its coverage: that takes punch-through */ /* so does one that discards (g9p_folds_discards()) */ - const struct g9p_coverage coverage = { true }; + const unsigned samples = vk_state->ms ? vk_state->ms->rasterization_samples : 1; + const uint32_t all = BITFIELD_MASK(samples); + const struct g9p_coverage coverage = { true, samples }; const struct g9p_coverage *coverage_ptr = - vk_state->ms && vk_state->ms->rasterization_samples == VK_SAMPLE_COUNT_4_BIT && - ((vk_state->ms->sample_mask & 0xf) != 0xf || vk_state->ms->alpha_to_coverage_enable || + samples > 1 && + ((vk_state->ms->sample_mask & all) != all || vk_state->ms->alpha_to_coverage_enable || g9p_folds_discards(fs_nir)) ? &coverage : NULL; @@ -2928,8 +2955,8 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state */ struct pvr_pds_upload fs_kick = state->fs_kick; - if (g9p_current_samples(info) == 4) { - result = g9p_kick_4x(cmd_buffer, &state->fs_kick, &fs_kick); + if (g9p_current_samples(info) > 1) { + result = g9p_kick_ms(cmd_buffer, &state->fs_kick, &fs_kick); if (result != VK_SUCCESS) return result; } @@ -3005,8 +3032,8 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state memcpy(&ppp[blocks[nb - 1][0] + 1], dp->isp, sizeof(dp->isp)); else g9p_ds_draw_isp(cmd_buffer, state, dp->scissor >= 0, &ppp[blocks[nb - 1][0]]); - /* Four samples: user pass 7 for every draw that shades */ - if (g9p_current_samples(info) == 4) { + /* Several samples: user pass 7 for every draw that shades */ + if (g9p_current_samples(info) > 1) { uint32_t *isp0 = &ppp[blocks[nb - 1][0] + 1]; if (((*isp0 >> 22) & 0xf) == 0xf) @@ -3619,15 +3646,20 @@ PVR_PER_ARCH(g9p_pack_image_view)(const struct pvr_image_view *iview, uint64_t w */ const uint64_t base = image->dev_addr.addr + plane->offset + plane_offset + iview->vk.base_array_layer * plane->layer_size; - /* - * Four samples, sampled: a twiddled texture of twice the size, each pixel's samples - * its 2x2 block (each pixel 4 samples in Morton order, y in bit 0: iOS msaa4-store) - */ - const uint32_t ms = image->vk.samples == VK_SAMPLE_COUNT_4_BIT ? 2 : 1; - const uint32_t w0 = image->vk.extent.width * ms, h0 = image->vk.extent.height * ms; + const uint32_t w0 = image->vk.extent.width, h0 = image->vk.extent.height; d[0] = (d[0] & ~(0x1fu << 27)) | (w0 - 1) << 27; d[1] = (w0 - 1) >> 5 | (h0 - 1) << 9 | iview->vk.base_mip_level << 25; + /* + * Several samples: the texture unit's multisample form (g9p_ms_sample()), + * dimension 4 and log2(samples) - 1 at bit 55, the pixels' size. The + * surface is twiddled by pixel, each pixel's samples one after another + * (iOS msaa4-store at four samples). + */ + if (image->vk.samples > 1) { + d[0] = (d[0] & ~7u) | 4; + d[1] |= (util_logbase2(image->vk.samples) - 1) << 23; + } d[2] = (uint32_t)(base >> 1) | (iview->vk.base_mip_level + iview->vk.level_count - 1) | (image->vk.mip_levels > 1 ? 0x10 : 0); d[3] = (uint32_t)(base >> 33); @@ -3840,10 +3872,10 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa const struct pvr_image_view *v = info->attachments[hw->eot_surfaces[0].attachment_idx]; key |= (uint64_t)v->vk.extent.width << 40 | (uint64_t)v->vk.extent.height << 26; - if (job->g9p.samples == 4) { + if (job->g9p.samples > 1) { /* each sample its own (g9p_ms_load_coords()), the multisampled targets so read */ - g9p_ms_load_coords(c); - key |= 1ull << 56; + g9p_ms_load_coords(c, job->g9p.samples); + key |= (uint64_t)util_logbase2(job->g9p.samples) << 56; } else { G9P_EMIT(c, 0x211c, 0x6800); g9p_wait(c); @@ -3872,8 +3904,7 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa uint64_t tex[2]; PVR_PER_ARCH(g9p_pack_image_view)(v, tex); - if (job->g9p.samples == 4) { - g9p_ms_texture(v, tex); + if (job->g9p.samples > 1) { g9p_ms_sample(c, o < G9P_OUTPUT_RAW1, tmp, 8 * t, 8 * n); } else { G9P_EMIT(c, o >= G9P_OUTPUT_RAW1 ? 0xf51a : 0xf518, g9p_sample_w1(tmp, 8 * t), @@ -3932,8 +3963,8 @@ g9p_background_mrt(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_render_pa } const uint64_t units = DIV_ROUND_UP(8 * n + 2, 16); - if (job->g9p.samples == 4) { - result = g9p_kick_4x(cmd_buffer, &kick, &kick); + if (job->g9p.samples > 1) { + result = g9p_kick_ms(cmd_buffer, &kick, &kick); if (result != VK_SUCCESS) return result; } @@ -4031,7 +4062,7 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, memset(&job->g9p, 0, sizeof(job->g9p)); job->g9p.enabled = true; job->g9p.samples = hw_render->sample_count; - if (hw_render->sample_count != 1 && hw_render->sample_count != 4) + if (!util_is_power_of_two_nonzero(hw_render->sample_count) || hw_render->sample_count > 8) return g9p_feature_error(cmd_buffer, "G9P: %u samples", hw_render->sample_count); /* multiview: each view's job stores to its own layer */ @@ -4059,7 +4090,7 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, pack_image_state(image, g9p_eot_addr(cmd_buffer, hw_render, job, 0, addr), iview->vk.extent.width, iview->vk.extent.height, linear, pitch, iview->vk.format); - if (image_obj->vk.samples == 4) + if (image_obj->vk.samples > 1) image[0] = (image[0] & ~0xfu) | 0x0c; /* 2D multisample, twiddled (iOS msaa4-store) */ /* the other targets' blocks: their state, their output-half offset (PR #6) */ for (unsigned t = 1; t < eot_targets; t++) { @@ -4075,7 +4106,7 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, v->vk.extent.width, v->vk.extent.height, im->memlayout == PVR_MEMLAYOUT_LINEAR, pl->mip_levels[v->vk.base_mip_level].pitch, v->vk.format); - if (im->vk.samples == 4) + if (im->vk.samples > 1) image[8 * t] = (image[8 * t] & ~0xfu) | 0x0c; image[8 * t + 6] = 2 * g9p_render_words(info, hw_render, g9p_words_surface(hw_render, t)); image[8 * t - 1] = 0xffffffff; @@ -4113,9 +4144,9 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const uint32_t data[8] = { bo->dev_addr.addr, bo->dev_addr.addr >> 32, doutd_ctrl(0, eot_dwords, true), 0, usc_offset(device, eot_targets > 1 - ? (job->g9p.samples == 4 ? p->eot_mrt_ms_usc[eot_targets][raw] + ? (job->g9p.samples > 1 ? p->eot_mrt_ms_usc[eot_targets][raw] : p->eot_mrt_usc[eot_targets][raw]) - : (job->g9p.samples == 4 ? p->eot_ms_usc[raw & 1] + : (job->g9p.samples > 1 ? p->eot_ms_usc[raw & 1] : p->eot_usc[raw & 1])), usc_temps(8), 0, 0 }; @@ -4173,8 +4204,6 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, uint64_t tex[2]; PVR_PER_ARCH(g9p_pack_image_view)(iview, tex); - if (pvr_image_view_get_image(iview)->vk.samples == VK_SAMPLE_COUNT_4_BIT) - g9p_ms_texture(iview, tex); memcpy(bg, tex, sizeof(tex)); { /* @@ -4278,13 +4307,13 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, if (result != VK_SUCCESS) return result; } else { - struct pvr_pds_upload bg_kick = load && job->g9p.samples == 4 ? p->load_ms_kick[bg_output] + struct pvr_pds_upload bg_kick = load && job->g9p.samples > 1 ? p->load_ms_kick[util_logbase2(job->g9p.samples) - 1][bg_output] : load ? p->load_kick[bg_output] : bg_raw ? p->clear_raw_kick[bg_output] : p->clear_kick; - if (job->g9p.samples == 4) { - result = g9p_kick_4x(cmd_buffer, &bg_kick, &bg_kick); + if (job->g9p.samples > 1) { + result = g9p_kick_ms(cmd_buffer, &bg_kick, &bg_kick); if (result != VK_SUCCESS) return result; } @@ -4373,7 +4402,7 @@ PVR_PER_ARCH(g9p_geom_stream)(const struct pvr_device *device, put64(&s, 0); for (unsigned i = 0; i < 6; i++) put64(&s, 0); /* cluster */ - put32(&s, job->g9p.samples == 4 ? G9P_POSITIONS_4X : 0x88); /* 0xc78 */ + put32(&s, (uint32_t)g9p_positions(job->g9p.samples)); /* 0xc78 */ dump32("geom stream", 0, (const uint32_t *)buf, s.len / 4); return s.len; } @@ -4568,8 +4597,8 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, put32(&s, 0); put32(&s, bgobjdepth); /* ISP_BGOBJDEPTH: the depth clear */ put32(&s, bgobjvals); /* ISP_BGOBJVALS */ - /* ISP_AA: no multisampling */ - put32(&s, job->g9p.samples == 4 ? 2 : 0); + /* ISP_AA: log2 of the samples */ + put32(&s, util_logbase2(job->g9p.samples)); /* * ISP_CTL: bit 17 (Rogue's process_empty_tiles) runs the background object * on tiles without geometry too; without it a render's clear reaches @@ -4582,8 +4611,8 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, * small damaged part. G9P_ALL_TILES=1 keeps it on. */ { - /* Four samples: the low bits 7, and the partition mode of the output below */ - const bool ms = job->g9p.samples == 4; + /* Several samples: the low bits 7, and the partition mode of the output below */ + const bool ms = job->g9p.samples > 1; const uint32_t ctl = (job->g9p.load || job->g9p.load_op == G9P_LOAD_OP_NONE) && !getenv("G9P_ALL_TILES") ? 0x21000f : 0x23000f; @@ -4598,7 +4627,7 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, *(uint32_t *)buf = s.len; put32(&s, G9P_EXTHDR_APPLE); - put64(&s, job->g9p.samples == 4 ? G9P_POSITIONS_4X : 0x88); /* 0xfa8 sample positions */ + put64(&s, g9p_positions(job->g9p.samples)); /* 0xfa8 sample positions */ for (unsigned i = 0; i < 4; i++) { const uint32_t view_data = job->g9p.bgnd_data_view[job->view_state.view_index]; @@ -4611,7 +4640,8 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, * a depth buffer), 0x4070 and each core's word (PR #74's g5p_ru_fill_3d_width(), * Apple's table by width; iOS record +0x6ec): 0x1d3 is width 4 */ - const unsigned output_count = 0x4000 / (job->g9p.pixel_width << (job->g9p.samples == 4 ? 11 : 10)); + /* tiles of 2048 samples when multisampled: 32 x 32 pixels at 2x, 32 x 16 at 4x, 16 x 16 at 8x */ + const unsigned output_count = 0x4000 / (job->g9p.pixel_width << (job->g9p.samples > 1 ? 11 : 10)); const unsigned output_code = job->g9p.pixel_width == 8 ? 0x20 : job->g9p.pixel_width == 4 ? 0x10 : 0; for (unsigned i = 0; i < 6; i++) @@ -4628,8 +4658,11 @@ PVR_PER_ARCH(g9p_frag_stream)(const struct pvr_device *device, const uint32_t mx = ALIGN_POT(DIV_ROUND_UP(DIV_ROUND_UP(w, 32), 4), 4); const uint32_t my = ALIGN_POT(DIV_ROUND_UP(DIV_ROUND_UP(h, 32), 4), 4); - /* Four samples are 2x2 tiles per tile (PR #6's g5p_ru_rt_derive_samples()) */ - put32(&s, DIV_ROUND_UP(mx * my * (job->g9p.samples == 4 ? 4 : 1) * + /* + * Several samples are tiles of tiles, 1x2 at 2x, 2x2 at 4x (PR #6's + * g5p_ru_rt_derive_samples()) and 2x4 at 8x, as the kernel's TE_AA has them + */ + put32(&s, DIV_ROUND_UP(mx * my * job->g9p.samples * rogue_get_region_header_size(&device->pdevice->dev_info), 4)); } diff --git a/src/imagination/vulkan/pvr_arch_g9p_transfer.c b/src/imagination/vulkan/pvr_arch_g9p_transfer.c index b02f3c65e84..b9a6d4de18b 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_transfer.c +++ b/src/imagination/vulkan/pvr_arch_g9p_transfer.c @@ -72,13 +72,32 @@ struct surface { uint8_t *base; enum pipe_format format; unsigned bpp, stride, width, height; - /* 1 or 4; four samples are a twiddled surface of twice the size (2x2 per pixel) */ - unsigned samples; + /* + * 1, 2, 4 or 8. A multisampled surface is twiddled by pixel, each pixel's + * samples one after another; its texels here are the samples, sx x sy of + * them a pixel (ms_shape()), and width and height count them. + */ + unsigned samples, sx, sy; + /* the pixels' width and height */ + unsigned pw, ph; /* D32F_S8: from the depth to the stencil, 0 when unknown */ uint64_t stencil_offset; bool twiddled; }; +/* A surface's samples, with its width and height in pixels: then counted in samples */ +static void +surface_samples(struct surface *out, unsigned samples) +{ + out->samples = samples == 2 || samples == 4 || samples == 8 ? samples : 1; + out->sx = out->samples >= 4 ? 2 : 1; + out->sy = out->samples / out->sx; + out->pw = out->width; + out->ph = out->height; + out->width *= out->sx; + out->height *= out->sy; +} + static bool surface_init(struct pvr_device *device, const struct pvr_transfer_cmd_surface *s, struct surface *out) @@ -90,12 +109,8 @@ surface_init(struct pvr_device *device, const struct pvr_transfer_cmd_surface *s out->width = s->width; out->height = s->height; out->twiddled = s->mem_layout == PVR_MEMLAYOUT_TWIDDLED; - out->samples = s->sample_count == 4 ? 4 : 1; out->stencil_offset = s->g9p_stencil_offset; - if (out->samples == 4) { - out->width *= 2; - out->height *= 2; - } + surface_samples(out, s->sample_count); if (!out->base) mesa_loge("G9P transfer: no CPU mapping for %#" PRIx64, s->dev_addr.addr); return out->base != NULL; @@ -117,13 +132,35 @@ twiddle(unsigned x, unsigned y, unsigned w, unsigned h) return m; } +/* + * Sample k's place in its pixel's block of sx x sy texels, and back: 2x + * one above the other, 4x 2x2 with y in bit 0 (the twiddled order of a + * surface of twice the size), 8x 2x4 with y in bits 0 and 2 + */ +static void +ms_sample_xy(const struct surface *s, unsigned k, unsigned *dx, unsigned *dy) +{ + *dx = s->samples >= 4 ? (k >> 1) & 1 : 0; + *dy = s->samples == 2 ? k : s->samples == 8 ? (k & 1) | ((k >> 2) & 1) << 1 : k & 1; +} + +static unsigned +ms_sample_bits(const struct surface *s, unsigned dx, unsigned dy) +{ + return (s->samples >= 4 ? dx << 1 : 0) | (s->samples == 8 ? (dy & 1) | (dy >> 1) << 2 : dy); +} + /* * A texel's byte offset is a column part plus a row part: in twiddled - * surfaces the x and y bits of the index are interleaved, disjoint + * surfaces the x and y bits of the index are interleaved, disjoint, and + * a multisampled one's pixel index (times the samples) and sample index too */ static size_t column_offset(const struct surface *s, unsigned x) { + if (s->twiddled && s->samples > 1) + return ((size_t)twiddle(x / s->sx, 0, s->pw, s->ph) * s->samples + + ms_sample_bits(s, x % s->sx, 0)) * s->bpp; if (s->twiddled) return (size_t)twiddle(x, 0, s->width, s->height) * s->bpp; return (size_t)x * s->bpp; @@ -132,6 +169,9 @@ column_offset(const struct surface *s, unsigned x) static size_t row_offset(const struct surface *s, unsigned y) { + if (s->twiddled && s->samples > 1) + return ((size_t)twiddle(0, y / s->sy, s->pw, s->ph) * s->samples + + ms_sample_bits(s, 0, y % s->sy)) * s->bpp; if (s->twiddled) return (size_t)twiddle(0, y, s->width, s->height) * s->bpp; return (size_t)y * s->stride * s->bpp; @@ -140,9 +180,8 @@ row_offset(const struct surface *s, unsigned y) static void fill(const struct pvr_transfer_cmd *cmd, const struct surface *dst) { - const unsigned ms = dst->samples == 4 ? 2 : 1; - const VkRect2D scaled = { { cmd->scissor.offset.x * ms, cmd->scissor.offset.y * ms }, - { cmd->scissor.extent.width * ms, cmd->scissor.extent.height * ms } }; + const VkRect2D scaled = { { cmd->scissor.offset.x * dst->sx, cmd->scissor.offset.y * dst->sy }, + { cmd->scissor.extent.width * dst->sx, cmd->scissor.extent.height * dst->sy } }; const VkRect2D *r = &scaled; uint8_t packed[16] = { 0 }; @@ -513,15 +552,16 @@ copy_linear(const struct pvr_rect_mapping *m, const struct surface *src, const s free(src_cols); } -/* A rectangle in a four-sample surface's texels (2x2 per pixel) */ +/* A rectangle in a multisampled surface's texels (sx x sy per pixel) */ static VkRect2D -ms_rect(const VkRect2D *r) +ms_rect(const VkRect2D *r, const struct surface *s) { - return (VkRect2D){ { r->offset.x * 2, r->offset.y * 2 }, { r->extent.width * 2, r->extent.height * 2 } }; + return (VkRect2D){ { r->offset.x * s->sx, r->offset.y * s->sy }, + { r->extent.width * s->sx, r->extent.height * s->sy } }; } /* - * Four samples into one: the average, or sample 0 for integers and depth/stencil. Nearest + * Several samples into one: the average, or sample 0 for integers and depth/stencil. Nearest * for a scaled mapping, as a resolve has none in Vulkan. */ static void @@ -538,21 +578,24 @@ resolve(const struct pvr_rect_mapping *m, const struct surface *src, const struc uint8_t *d = row + column_offset(dst, m->dst_rect.offset.x + x); if (first) { - memcpy(d, src->base + row_offset(src, 2 * sy) + column_offset(src, 2 * sx), dst->bpp); + memcpy(d, src->base + row_offset(src, src->sy * sy) + column_offset(src, src->sx * sx), + dst->bpp); continue; } float sum[4] = { 0 }; - for (unsigned s = 0; s < 4; s++) { + for (unsigned s = 0; s < src->samples; s++) { float texel[4]; + unsigned dx, dy; + ms_sample_xy(src, s, &dx, &dy); util_format_unpack_rgba(src->format, texel, - src->base + row_offset(src, 2 * sy + (s & 1)) + - column_offset(src, 2 * sx + (s >> 1)), + src->base + row_offset(src, src->sy * sy + dy) + + column_offset(src, src->sx * sx + dx), 1); for (unsigned c = 0; c < 4; c++) - sum[c] += texel[c] / 4; + sum[c] += texel[c] / src->samples; } util_format_pack_rgba(dst->format, d, sum, 1); } @@ -563,19 +606,19 @@ resolve(const struct pvr_rect_mapping *m, const struct surface *src, const struc static void copy_samples(const struct pvr_rect_mapping *m, const struct surface *src, const struct surface *dst) { - if (src->samples == 4 && dst->samples == 1) { + if (src->samples > 1 && dst->samples == 1) { resolve(m, src, dst); - } else if (src->samples == dst->samples && src->samples == 4) { + } else if (src->samples == dst->samples && src->samples > 1) { struct pvr_rect_mapping scaled = *m; - scaled.src_rect = ms_rect(&m->src_rect); - scaled.dst_rect = ms_rect(&m->dst_rect); + scaled.src_rect = ms_rect(&m->src_rect, src); + scaled.dst_rect = ms_rect(&m->dst_rect, dst); copy(&scaled, src, dst); - } else if (dst->samples == 4) { - /* one sample into all four */ + } else if (dst->samples > 1) { + /* one sample into all of them */ struct pvr_rect_mapping scaled = *m; - scaled.dst_rect = ms_rect(&m->dst_rect); + scaled.dst_rect = ms_rect(&m->dst_rect, dst); copy(&scaled, src, dst); } else { copy(m, src, dst); @@ -706,6 +749,8 @@ host_image_surface(struct pvr_device *device, const struct pvr_image *image, out->width = MAX2(image->vk.extent.width >> sub->mipLevel, 1); out->height = MAX2(image->vk.extent.height >> sub->mipLevel, 1); out->twiddled = image->memlayout == PVR_MEMLAYOUT_TWIDDLED; + out->stencil_offset = 0; + surface_samples(out, image->vk.samples); return out->base != NULL; } @@ -721,6 +766,8 @@ host_memory_surface(const struct pvr_image *image, const void *ptr, uint32_t row out->width = out->stride; out->height = image_height ? image_height : extent->height; out->twiddled = false; + out->stencil_offset = 0; + surface_samples(out, 1); } static VkResult @@ -738,8 +785,8 @@ host_copy(struct pvr_device *device, const struct pvr_image *image, bool to_imag return vk_error(device, VK_ERROR_MEMORY_MAP_FAILED); if (flags & VK_HOST_IMAGE_COPY_MEMCPY_BIT) { /* the subresource as it is laid out, back to back per layer */ - const size_t size = (size_t)img.stride * (img.twiddled ? util_next_power_of_two(img.height) - : img.height) * img.bpp; + const size_t size = (size_t)img.stride * (img.twiddled ? util_next_power_of_two(img.ph) + : img.ph) * img.bpp * img.samples; uint8_t *host = (uint8_t *)ptr + l * size; if (to_image) @@ -781,7 +828,7 @@ host_copy(struct pvr_device *device, const struct pvr_image *image, bool to_imag const struct pvr_rect_mapping m = { .src_rect = to_image ? at_memory : at_image, .dst_rect = to_image ? at_image : at_memory }; - copy(&m, to_image ? &mem : &img, to_image ? &img : &mem); + copy_samples(&m, to_image ? &mem : &img, to_image ? &img : &mem); } return VK_SUCCESS; } @@ -845,7 +892,7 @@ PVR_PER_ARCH(CopyImageToImage)(VkDevice _device, const VkCopyImageToImageInfo *i .dst_rect = { { r->dstOffset.x, r->dstOffset.y }, { r->extent.width, r->extent.height } }, }; - copy(&m, &src, &dst); + copy_samples(&m, &src, &dst); } } return VK_SUCCESS; diff --git a/src/imagination/vulkan/pvr_device.h b/src/imagination/vulkan/pvr_device.h index 9998e8a2c00..3c06b87c47f 100644 --- a/src/imagination/vulkan/pvr_device.h +++ b/src/imagination/vulkan/pvr_device.h @@ -119,9 +119,9 @@ struct pvr_device { /* load op: the old target sampled at the pixel position, and its kick */ struct pvr_suballoc_bo *load_usc[G9P_OUTPUT_COUNT]; struct pvr_pds_upload load_kick[G9P_OUTPUT_COUNT]; - /* the same at four samples, each sample its own (g9p_ms_load_coords()) */ - struct pvr_suballoc_bo *load_ms_usc[G9P_OUTPUT_COUNT]; - struct pvr_pds_upload load_ms_kick[G9P_OUTPUT_COUNT]; + /* the same at 2, 4 and 8 samples, each sample its own (g9p_ms_load_coords()) */ + struct pvr_suballoc_bo *load_ms_usc[3][G9P_OUTPUT_COUNT]; + struct pvr_pds_upload load_ms_kick[3][G9P_OUTPUT_COUNT]; /* depth/stencil clear draws: the tile's colour unchanged, and its kick */ struct pvr_suballoc_bo *keep_usc[G9P_OUTPUT_COUNT]; struct pvr_pds_upload keep_kick[G9P_OUTPUT_COUNT]; diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index d7d44f9707e..842dc09b37f 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -636,6 +636,10 @@ static bool pvr_physical_device_get_properties( UNUSED const uint32_t max_sample_bits = ((max_multisample << 1) - 1); + /* the A10 renders 2, 4 and 8 samples a pixel (its kernel's TE_AA 1, 5, 7), the others 4 */ + const VkSampleCountFlags sample_counts = + PVR_HAS_FEATURE(dev_info, apple_g9p) ? max_sample_bits : VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT; + UNUSED const uint32_t max_user_vertex_components = pvr_get_max_user_vertex_output_components(dev_info); @@ -809,15 +813,15 @@ static bool pvr_physical_device_get_properties( .maxFramebufferLayers = 256U, /* Note: update nir_shader_compiler_options.max_samples when changing this. */ - .framebufferColorSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .framebufferDepthSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .framebufferStencilSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .framebufferNoAttachmentsSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, + .framebufferColorSampleCounts = sample_counts, + .framebufferDepthSampleCounts = sample_counts, + .framebufferStencilSampleCounts = sample_counts, + .framebufferNoAttachmentsSampleCounts = sample_counts, .maxColorAttachments = PVR_HAS_FEATURE(dev_info, apple_g5p) ? 8U : 4U, /* G5P: 5..8 compact UNORM8 targets (pvr_g5p_mrt.h) */ - .sampledImageColorSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .sampledImageDepthSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, - .sampledImageStencilSampleCounts = VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, + .sampledImageColorSampleCounts = sample_counts, + .sampledImageIntegerSampleCounts = sample_counts, + .sampledImageDepthSampleCounts = sample_counts, + .sampledImageStencilSampleCounts = sample_counts, .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT, /* Requires shaderStorageImageMultisample */ .maxSampleMaskWords = 1U, .timestampComputeAndGraphics = false, @@ -884,8 +888,7 @@ static bool pvr_physical_device_get_properties( .maxDescriptorSetUpdateAfterBindInputAttachments = 0, .filterMinmaxSingleComponentFormats = false, .filterMinmaxImageComponentMapping = false, - .framebufferIntegerColorSampleCounts = - VK_SAMPLE_COUNT_1_BIT | VK_SAMPLE_COUNT_4_BIT, + .framebufferIntegerColorSampleCounts = sample_counts, /* Vulkan 1.0 / VK_KHR_maintenance2 */ .pointClippingBehavior = From 58ba607f8797ccdd094a564ef426f7325cc3db32 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 18:39:49 -0600 Subject: [PATCH 35/44] pvr/g9p: a discarded pixel writes no target past what a pixel holds A blended shader without a depth or stencil attachment keeps the tile's colour where a pixel is discarded, through selects on the discard's condition, and the discard goes. A target past the 8 words a pixel holds is written to memory by the shader, and those stores ran at the discarded pixels too. They now run under the condition, at the end. The pass also gave up on shaders with more than 8 colour stores, as Zink writes them one component at a time. It takes 32 now. eglmrtbig's RGBA8, RGBA32UI, RGBA8UI and RG32F case without depth (9 words here, as RGBA8 takes two) is now right at every size. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index e2b949e0bde..39772cd7a4a 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -7192,8 +7192,8 @@ static bool discards_to_select(nir_shader *nir, bool zero) { nir_function_impl *impl = nir_shader_get_entrypoint(nir); - nir_intrinsic_instr *discards[16], *stores[8]; - unsigned num_discards = 0, num_stores = 0; + nir_intrinsic_instr *discards[16], *stores[32], *writes[32]; + unsigned num_discards = 0, num_stores = 0, num_writes = 0; nir_def *cond = NULL; nir_builder b; @@ -7222,6 +7222,12 @@ discards_to_select(nir_shader *nir, bool zero) return nir_no_progress(impl); stores[num_stores++] = intr; break; + case nir_intrinsic_store_ssbo: + /* a target past what a pixel holds (lower_overflow_target()) */ + if (!top || num_writes == ARRAY_SIZE(writes)) + return nir_no_progress(impl); + writes[num_writes++] = intr; + break; default: break; } @@ -7262,6 +7268,15 @@ discards_to_select(nir_shader *nir, bool zero) .io_semantics = nir_intrinsic_io_semantics(st)); nir_instr_remove(&st->instr); } + /* a discarded pixel writes no memory either: those stores at the end, where it is kept */ + if (num_writes) { + nir_push_if(&b, nir_inot(&b, cond)); + for (unsigned i = 0; i < num_writes; i++) { + nir_instr_remove(&writes[i]->instr); + nir_builder_instr_insert(&b, &writes[i]->instr); + } + nir_pop_if(&b, NULL); + } return nir_progress(true, impl, nir_metadata_none); } From 150fcc3fead9192c16ec45a6043f5a1968bd4569 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 18:39:49 -0600 Subject: [PATCH 36/44] pvr/g9p: resolves by transfer past four end-of-tile surfaces An end of tile stores up to four surfaces, colour targets and resolves together. Four multisampled targets with a resolve kept only the first four, so the fourth target was never stored. A program of five emits stored wrong values for every target, also with each surface's offset as an immediate in place of its load. A render whose targets and resolves come to more than four now resolves by transfer after the render, as upstream does past its own limit, and the end of tile leaves those resolves out. eglmrtbig's four RGBA16F targets at 4x pass. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_cmd_buffer.c | 22 ++++++++++++++++++++ src/imagination/vulkan/pvr_arch_g9p_gfx.c | 11 ++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index 694294e32d1..42665b6ce56 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -88,6 +88,7 @@ #include "vk_pipeline_layout.h" #include "vk_synchronization.h" #include "pvr_g9p.h" +#include "g9p_compile.h" #include "vk_util.h" #define G5P_UPLOAD_BLOCK_SIZE (64u * 1024u) @@ -4697,6 +4698,25 @@ static VkResult pvr_dynamic_rendering_output_attachments_setup( dr_info->color_attachment_count = pRenderingInfo->colorAttachmentCount; + /* + * G9P: an end of tile stores at most four surfaces (G9P_MAX_TARGETS, the image state + * blocks its emits reach). Targets and resolves past that: the resolves by transfer, + * after the render. + */ + bool g9p_transfer_resolves = false; + + if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p)) { + unsigned surfaces = 0; + + for (uint32_t i = 0; i < pRenderingInfo->colorAttachmentCount; i++) { + const VkRenderingAttachmentInfo *a = &pRenderingInfo->pColorAttachments[i]; + + if (a->imageView != VK_NULL_HANDLE) + surfaces += 1 + (a->resolveMode && a->resolveImageView != VK_NULL_HANDLE); + } + g9p_transfer_resolves = surfaces > G9P_MAX_TARGETS; + } + if (dr_info->color_attachment_count) { const uint32_t size = sizeof(*dr_info->color_attachments) * dr_info->color_attachment_count; @@ -4769,6 +4789,8 @@ static VkResult pvr_dynamic_rendering_output_attachments_setup( image->vk.samples, attachment_info->resolveMode); PVR_G5P_HOOK(msaa_pbe_resolve, &cmd_buffer->device->pdevice->dev_info, attachment, iview->vk.view_format, image->vk.samples, attachment_info->resolveMode) + if (g9p_transfer_resolves) + attachment->is_pbe_downscalable = false; dr_info->color_attachments[i].index_color = cur_attach_idx; dr_info->hw_render.sample_count = MAX2(dr_info->hw_render.sample_count, attachment->sample_count); diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 738be1715ab..ebcff9016cf 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -4030,14 +4030,21 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_renderpass_hwsetup_eot_surface live_eot[G9P_MAX_TARGETS]; struct pvr_renderpass_colorinit live_init[G9P_MAX_TARGETS]; const unsigned overflow = g9p_hw_overflow(info, hw_render); + /* resolves by transfer (more surfaces than an end of tile stores): no part of it either */ + unsigned by_transfer = 0; - if (overflow) { + for (unsigned t = 0; t < hw_render->eot_surface_count; t++) { + if (hw_render->eot_surfaces[t].need_resolve && + hw_render->eot_surfaces[t].resolve_type == PVR_RESOLVE_TYPE_TRANSFER) + by_transfer |= BITFIELD_BIT(t); + } + if (overflow || by_transfer) { live_render.eot_surfaces = live_eot; live_render.color_init = live_init; live_render.eot_surface_count = 0; live_render.color_init_count = 0; for (unsigned t = 0; t < hw_render->eot_surface_count; t++) { - if (!(overflow >> t & 1)) + if (!((overflow | by_transfer) >> t & 1) && live_render.eot_surface_count < G9P_MAX_TARGETS) live_eot[live_render.eot_surface_count++] = hw_render->eot_surfaces[t]; } for (unsigned i = 0; i < hw_render->color_init_count; i++) { From e8b609cfb962774949bd0896e39843d92b834fb9 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 18:45:24 -0600 Subject: [PATCH 37/44] pvr/g9p: a resolve sums in double and divides once The CPU resolve added each sample's value divided by the count in single precision. Eight samples of an R32F value near 7.8 million came out one higher, as the running sum loses its last bit past 4 million. The sum is now a double, divided by the count at the end. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_g9p_transfer.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g9p_transfer.c b/src/imagination/vulkan/pvr_arch_g9p_transfer.c index b9a6d4de18b..6c5da8b7bad 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_transfer.c +++ b/src/imagination/vulkan/pvr_arch_g9p_transfer.c @@ -583,7 +583,9 @@ resolve(const struct pvr_rect_mapping *m, const struct surface *src, const struc continue; } - float sum[4] = { 0 }; + /* in double, divided once: eight float sums of a value / 8 lost its last bit */ + double sum[4] = { 0 }; + float mean[4]; for (unsigned s = 0; s < src->samples; s++) { float texel[4]; @@ -595,9 +597,11 @@ resolve(const struct pvr_rect_mapping *m, const struct surface *src, const struc column_offset(src, src->sx * sx + dx), 1); for (unsigned c = 0; c < 4; c++) - sum[c] += texel[c] / src->samples; + sum[c] += texel[c]; } - util_format_pack_rgba(dst->format, d, sum, 1); + for (unsigned c = 0; c < 4; c++) + mean[c] = sum[c] / src->samples; + util_format_pack_rgba(dst->format, d, mean, 1); } } } From b3a66ce94b7d9608ca262fb895df31b7ccdb532e Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 18:45:24 -0600 Subject: [PATCH 38/44] pvr/g9p: several samples of one and two raw words R32, RG32, RGBA8 and RGBA16 integer targets and the 32-bit float ones were refused at more than one sample. The end of tile's store of every sample takes the tile in 16-bit channels, so R32F stored as itself came out wrong. A target of one or two raw words is now stored as RG16 or RGBA16 uint, bit for bit, with the emit of 16-bit channels. Loads read it in its own format, and its resolves go through a transfer as before. Four raw words (RGBA32) stay single-sampled, as they hung the fragment job. eglmrtbig's R32F target at 4x and new cases of R32F, RG32F, R32UI and RG32UI targets at 2x and 8x pass. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 28 ++++++++++++++++++++--- src/imagination/vulkan/pvr_formats.c | 6 ++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index ebcff9016cf..8c5d7f5f86a 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -3771,6 +3771,26 @@ PVR_PER_ARCH(g9p_pack_sampler)(const VkSamplerCreateInfo *info) * dword 5 0xc0000000, as iPadOS writes them. */ +/* + * The format an end of tile stores a target as. The emit of every sample takes the + * tile in 16-bit channels: R32F stored as itself came out wrong. A target of one or two + * raw words goes out as 16-bit integers then, bit for bit (RG16 and RGBA16 uint). + */ +static VkFormat +g9p_eot_format(VkFormat format, unsigned samples) +{ + if (samples <= 1) + return format; + switch (g9p_format_output(format)) { + case G9P_OUTPUT_RAW1: + return VK_FORMAT_R16G16_UINT; + case G9P_OUTPUT_RAW2: + return VK_FORMAT_R16G16B16A16_UINT; + default: + return format; + } +} + static void pack_image_state(uint32_t *w, uint64_t addr, uint32_t width, uint32_t height, bool linear, uint32_t pitch, VkFormat format) @@ -4096,7 +4116,7 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, job->g9p.target_linear = linear; pack_image_state(image, g9p_eot_addr(cmd_buffer, hw_render, job, 0, addr), iview->vk.extent.width, iview->vk.extent.height, linear, pitch, - iview->vk.format); + g9p_eot_format(iview->vk.format, image_obj->vk.samples)); if (image_obj->vk.samples > 1) image[0] = (image[0] & ~0xfu) | 0x0c; /* 2D multisample, twiddled (iOS msaa4-store) */ /* the other targets' blocks: their state, their output-half offset (PR #6) */ @@ -4112,7 +4132,8 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, (v->vk.base_array_layer + view) * pl->layer_size), v->vk.extent.width, v->vk.extent.height, im->memlayout == PVR_MEMLAYOUT_LINEAR, - pl->mip_levels[v->vk.base_mip_level].pitch, v->vk.format); + pl->mip_levels[v->vk.base_mip_level].pitch, + g9p_eot_format(v->vk.format, im->vk.samples)); if (im->vk.samples > 1) image[8 * t] = (image[8 * t] & ~0xfu) | 0x0c; image[8 * t + 6] = 2 * g9p_render_words(info, hw_render, g9p_words_surface(hw_render, t)); @@ -4144,7 +4165,8 @@ PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const VkFormat f = info->attachments[hw_render->eot_surfaces[t].attachment_idx]->vk.format; - if (g9p_format_output(f) >= G9P_OUTPUT_RAW1) + /* several samples: the emit of 16-bit channels (g9p_eot_format()) */ + if (g9p_format_output(f) >= G9P_OUTPUT_RAW1 && job->g9p.samples == 1) raw |= BITFIELD_BIT(t); } diff --git a/src/imagination/vulkan/pvr_formats.c b/src/imagination/vulkan/pvr_formats.c index 115306c2708..553b1e7b344 100644 --- a/src/imagination/vulkan/pvr_formats.c +++ b/src/imagination/vulkan/pvr_formats.c @@ -861,11 +861,11 @@ pvr_get_image_format_properties(struct pvr_physical_device *pdevice, pImageFormatProperties->maxArrayLayers = max_array_layers; pImageFormatProperties->sampleCounts = max_sample_bits; /* - * G9P: four samples of 32-bit channels: RGBA hangs the fragment job and RG and R - * come out wrong (dEQP fbo.msaa); GL ES needs no samples of these formats + * G9P: several samples of four raw words (RGBA32) hang the fragment job (dEQP + * fbo.msaa). One and two words are stored in 16-bit channels (g9p_eot_format()). */ if (PVR_HAS_FEATURE(&pdevice->dev_info, apple_g9p) && vk_format_is_color(info->format) && - g9p_format_output(info->format) >= G9P_OUTPUT_RAW1) + g9p_format_output(info->format) == G9P_OUTPUT_RAW4) pImageFormatProperties->sampleCounts = VK_SAMPLE_COUNT_1_BIT; } From 0b5f048be1e6c817f68528f673d31561074a5d71 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 19:03:46 -0600 Subject: [PATCH 39/44] pvr/g9p: more colour words than a pixel holds, a render per group of targets A pixel holds 8 output words. Past them a target was written to memory by the fragment program, and fragments hidden by the depth test behind a punch-through draw still wrote it. Such a target was never blended either. eglmrtbig's cases of 9, 12 and 16 words with a depth buffer came out wrong. The targets now go in groups of at most 8 words, in order, and the render runs once per group: - A pipeline compiles a fragment program per group, each writing only its group's targets. The others' stores are dropped at the end, so every program takes the same uniforms, textures and varyings. If two differ, or the shader writes memory, uses transform feedback or reads the view index, the pipeline keeps the old path. - Each draw gets its state words once per group, with that group's fragment kick. Clears in such a render take per group a program that clears or keeps its words. Occlusion queries count in group 0 alone. - Per group past the first, a copy of the control stream names those words. Indirect draws are filled in each copy too. - Each group has its own background and end of tile. Every group loads the depth and stencil the render started with, and only the last stores them. - The queue submits the groups in turn. eglmrtbig passes every case, up to four RGBA32UI targets at 2048 x 2048 with depth, and egles31's new indirect draw into three RGBA32UI targets passes. It failed with the indirect draws left out of the copies. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_formats.h | 31 ++ src/imagination/vulkan/pvr_arch_cmd_buffer.c | 5 + src/imagination/vulkan/pvr_arch_g9p_gfx.c | 513 +++++++++++++++++-- src/imagination/vulkan/pvr_arch_queue.c | 29 ++ src/imagination/vulkan/pvr_cmd_buffer.h | 8 + src/imagination/vulkan/pvr_g9p.h | 16 + src/imagination/vulkan/pvr_job_render.h | 13 +- src/imagination/vulkan/pvr_pipeline.h | 10 + 8 files changed, 586 insertions(+), 39 deletions(-) diff --git a/src/imagination/g9p/g9p_formats.h b/src/imagination/g9p/g9p_formats.h index 43216da1dce..0bcba1360fc 100644 --- a/src/imagination/g9p/g9p_formats.h +++ b/src/imagination/g9p/g9p_formats.h @@ -202,6 +202,37 @@ g9p_output_words(enum g9p_output output) /* A pixel holds eight output words (A10: 32 bytes) */ #define G9P_PIXEL_WORDS 8 +/* Groups of targets, at most one a target (G9P_MAX_TARGETS) */ +#define G9P_MAX_GROUPS 4 + +/* + * More words than a pixel holds: the targets in groups of at most G9P_PIXEL_WORDS + * words, in order, each group rendered by itself (the draws of the render again for + * each). Returns how many groups, 1 when the targets fit; @masks gets each one's + * targets. Every caller with the same outputs gets the same groups. + */ +static inline unsigned +g9p_target_groups(const enum g9p_output *outputs, unsigned present, unsigned masks[G9P_MAX_GROUPS]) +{ + unsigned n = 0, words = 0; + + for (unsigned g = 0; g < G9P_MAX_GROUPS; g++) + masks[g] = 0; + for (unsigned t = 0; t < G9P_MAX_GROUPS; t++) { + const unsigned w = g9p_output_words(outputs[t]); + + if (!(present >> t & 1)) + continue; + if (words + w > G9P_PIXEL_WORDS && masks[n]) { + n++; + words = 0; + } + masks[n] |= 1u << t; + words += w; + } + return n + 1; +} + /* * The targets past what a pixel holds: the fragment program writes these to memory itself. * Only 32-bit channels, which need no packing and don't blend; four targets of the others diff --git a/src/imagination/vulkan/pvr_arch_cmd_buffer.c b/src/imagination/vulkan/pvr_arch_cmd_buffer.c index 42665b6ce56..ad96f9694f9 100644 --- a/src/imagination/vulkan/pvr_arch_cmd_buffer.c +++ b/src/imagination/vulkan/pvr_arch_cmd_buffer.c @@ -211,6 +211,7 @@ static void pvr_cmd_buffer_free_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, util_dynarray_fini(&sub_cmd->gfx.g9p_indirects); util_dynarray_fini(&sub_cmd->gfx.g9p_fills); util_dynarray_fini(&sub_cmd->gfx.g9p_view_kicks); + util_dynarray_fini(&sub_cmd->gfx.g9p_group_ppps); util_dynarray_foreach (&sub_cmd->gfx.g9p_view_bos, struct pvr_bo *, bo) pvr_bo_free(cmd_buffer->device, *bo); util_dynarray_fini(&sub_cmd->gfx.g9p_view_bos); @@ -2875,6 +2876,8 @@ VkResult pvr_arch_cmd_buffer_end_sub_cmd(struct pvr_cmd_buffer *cmd_buffer) /* G9P: per-view copies of the stream instead (Rogue's PDS program is not G9P's) */ if (PVR_HAS_FEATURE(&device->pdevice->dev_info, apple_g9p)) { result = pvr_arch_g9p_build_view_streams(cmd_buffer, gfx_sub_cmd); + if (result == VK_SUCCESS) + result = pvr_arch_g9p_build_group_streams(cmd_buffer, gfx_sub_cmd); if (result != VK_SUCCESS) return pvr_cmd_buffer_set_error_unwarned(cmd_buffer, result); } else if (gfx_sub_cmd->view_index_wanted) { @@ -3227,6 +3230,8 @@ VkResult pvr_arch_cmd_buffer_start_sub_cmd(struct pvr_cmd_buffer *cmd_buffer, sub_cmd->gfx.g9p_indirects = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_fills = UTIL_DYNARRAY_INIT; sub_cmd->gfx.g9p_view_kicks = UTIL_DYNARRAY_INIT; + sub_cmd->gfx.g9p_group_ppps = UTIL_DYNARRAY_INIT; + memset(sub_cmd->gfx.g9p_group_stream, 0, sizeof(sub_cmd->gfx.g9p_group_stream)); sub_cmd->gfx.g9p_view_bos = UTIL_DYNARRAY_INIT; memset(sub_cmd->gfx.g9p_view_stream, 0, sizeof(sub_cmd->gfx.g9p_view_stream)); sub_cmd->gfx.unbound_deferred_clears = UTIL_DYNARRAY_INIT; diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 8c5d7f5f86a..203fa91cad3 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -638,12 +638,22 @@ g9p_keep_kick(struct pvr_device *device, unsigned words, struct pvr_pds_upload * static const struct pvr_renderpass_hwsetup_render * g9p_current_hw_render(const struct pvr_render_pass_info *info); -/* The targets of a render the fragment program writes to memory (g9p_overflow_targets()) */ +static unsigned g9p_render_groups(const struct pvr_render_pass_info *info, + const struct pvr_renderpass_hwsetup_render *hw, + unsigned masks[G9P_MAX_GROUPS]); + +/* + * The targets of a render the fragment program writes to memory (g9p_overflow_targets()), + * none when the render runs once per group of them + */ static unsigned g9p_hw_overflow(const struct pvr_render_pass_info *info, const struct pvr_renderpass_hwsetup_render *hw) { enum g9p_output outputs[8] = { 0 }; - unsigned present = 0; + unsigned present = 0, masks[G9P_MAX_GROUPS]; + + if (g9p_render_groups(info, hw, masks) > 1) + return 0; for (unsigned t = 0; hw && t < MIN2(hw->eot_surface_count, 8); t++) { outputs[t] = g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format); @@ -735,6 +745,49 @@ g9p_colour_surface(const struct pvr_renderpass_hwsetup_render *hw, unsigned j) return -1; } +/* + * A render's target groups (g9p_target_groups()) over its colour targets, numbered as a + * pipeline's: with more words than a pixel holds, the draws run once per group. Not + * with multiview; G9P_NO_GROUPS: never (the targets past a pixel go to memory then). + */ +static unsigned +g9p_render_groups(const struct pvr_render_pass_info *info, const struct pvr_renderpass_hwsetup_render *hw, + unsigned masks[G9P_MAX_GROUPS]) +{ + enum g9p_output outputs[8] = { 0 }; + unsigned present = 0; + + for (unsigned j = 0; hw && j < G9P_MAX_TARGETS; j++) { + const int t = g9p_colour_surface(hw, j); + + if (t < 0) + break; + outputs[j] = g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format); + present |= BITFIELD_BIT(j); + } + const unsigned n = g9p_target_groups(outputs, present, masks); + + return n > 1 && util_bitcount(hw->view_mask) <= 1 && !getenv("G9P_NO_GROUPS") ? n : 1; +} + +/* The words of colour targets @mask before colour target @upto */ +static unsigned +g9p_group_words(const struct pvr_render_pass_info *info, const struct pvr_renderpass_hwsetup_render *hw, + unsigned mask, unsigned upto) +{ + unsigned words = 0; + + for (unsigned j = 0; hw && j < MIN2(upto, G9P_MAX_TARGETS); j++) { + const int t = g9p_colour_surface(hw, j); + + if (t < 0) + break; + if (mask >> j & 1) + words += g9p_output_words(g9p_format_output(info->attachments[hw->eot_surfaces[t].attachment_idx]->vk.format)); + } + return words; +} + /* Target @t's words from the common store, the others as they are in the tile */ static VkResult g9p_clear_one_kick(struct pvr_device *device, unsigned words, unsigned base, unsigned n, @@ -1097,6 +1150,75 @@ upload_varyings(struct pvr_device *device, struct pvr_g9p_graphics_state *state, return VK_SUCCESS; } +/* The fragment shader stores to memory: run once per group of targets, it would store again */ +static bool +g9p_writes_memory(const nir_shader *nir) +{ + nir_foreach_function_impl (impl, nir) { + nir_foreach_block (block, impl) { + nir_foreach_instr (instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + switch (nir_instr_as_intrinsic(instr)->intrinsic) { + case nir_intrinsic_store_ssbo: + case nir_intrinsic_ssbo_atomic: + case nir_intrinsic_ssbo_atomic_swap: + case nir_intrinsic_store_global: + case nir_intrinsic_global_atomic: + case nir_intrinsic_global_atomic_swap: + case nir_intrinsic_image_deref_store: + case nir_intrinsic_image_deref_atomic: + case nir_intrinsic_image_deref_atomic_swap: + case nir_intrinsic_image_store: + case nir_intrinsic_image_atomic: + case nir_intrinsic_image_atomic_swap: + return true; + default: + break; + } + } + } + } + return false; +} + +/* Group @g's targets of @all: the others absent, their outputs dropped (into @buf) */ +static const struct g9p_target * +g9p_group_of(const struct g9p_target *all, const struct pvr_g9p_graphics_state *state, unsigned g, + struct g9p_target *buf) +{ + if (state->num_groups <= 1) + return all; + for (unsigned t = 0; t < G9P_MAX_TARGETS; t++) { + buf[t] = all[t]; + buf[t].present = all[t].present && (state->group_targets[g] >> t & 1); + } + return buf; +} + +/* + * Two groups' fragment programs take the same uniforms, textures and varyings and run + * as the same pass type: a draw's state words serve both but for the kick + */ +static bool +g9p_same_interface(const struct g9p_shader *a, const struct g9p_shader *b) +{ + if (a->num_buffers != b->num_buffers || a->num_fs_runs != b->num_fs_runs || + a->num_varyings != b->num_varyings || a->num_textures != b->num_textures || + a->uses_frag_coord != b->uses_frag_coord || a->uses_point_coord != b->uses_point_coord || + a->point_coord_slot != b->point_coord_slot || a->uses_discard != b->uses_discard || + a->writes_depth != b->writes_depth || a->uses_derivatives != b->uses_derivatives || + a->straight_samples != b->straight_samples || a->writes_coverage != b->writes_coverage || + a->uses_fbfetch != b->uses_fbfetch || a->punch_fbfetch != b->punch_fbfetch) + return false; + return !memcmp(a->buffers, b->buffers, a->num_buffers * sizeof(a->buffers[0])) && + !memcmp(a->fs_runs, b->fs_runs, a->num_fs_runs * sizeof(a->fs_runs[0])) && + !memcmp(a->varying_perspective, b->varying_perspective, sizeof(a->varying_perspective)) && + !memcmp(a->varying_flat, b->varying_flat, sizeof(a->varying_flat)) && + !memcmp(a->varying_linear, b->varying_linear, sizeof(a->varying_linear)) && + !memcmp(a->textures, b->textures, a->num_textures * sizeof(a->textures[0])); +} + static VkResult g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo *create_info, struct pvr_graphics_pipeline *pipeline, @@ -1230,7 +1352,7 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo g9p_folds_discards(fs_nir)) ? &coverage : NULL; - struct g9p_target targets[G9P_MAX_TARGETS] = { 0 }; + struct g9p_target targets[G9P_MAX_TARGETS] = { 0 }, group_targets[G9P_MAX_TARGETS]; for (unsigned t = 0; vk_state->rp && t < MIN2(vk_state->rp->color_attachment_count, G9P_MAX_TARGETS); t++) { const VkFormat f = vk_state->rp->color_attachment_formats[t]; @@ -1253,9 +1375,68 @@ g9p_compile_state(struct pvr_device *device, const VkGraphicsPipelineCreateInfo vk_state->rp && (vk_state->rp->depth_attachment_format != VK_FORMAT_UNDEFINED || vk_state->rp->stencil_attachment_format != VK_FORMAT_UNDEFINED); + /* + * More words than a pixel holds: a fragment program per group of targets + * (g9p_target_groups()), each writing only its own, when the draws can run once + * per group: no memory writes, transform feedback or multiview + */ + { + enum g9p_output outputs[8] = { 0 }; + unsigned present = 0; + + for (unsigned t = 0; t < G9P_MAX_TARGETS; t++) { + outputs[t] = targets[t].output; + present |= targets[t].present ? BITFIELD_BIT(t) : 0; + } + state->num_groups = g9p_target_groups(outputs, present, state->group_targets); + if (state->num_groups > 1 && + (g9p_writes_memory(fs_nir) || vs_xfb || vs_views[1] || getenv("G9P_NO_GROUPS"))) + state->num_groups = 1; + } + nir_shader *fs_all = state->num_groups > 1 ? nir_shader_clone(mem_ctx, fs_nir) : NULL; + result = compile_stage(device, fs_nir, fs_info, NULL, slot, false, - blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, targets, + blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, + g9p_group_of(targets, state, 0, group_targets), zs_attachment, &fs, false, mem_ctx); + for (unsigned g = 1; result == VK_SUCCESS && g < state->num_groups; g++) { + struct g9p_shader *gs = rzalloc(mem_ctx, struct g9p_shader); + + result = compile_stage(device, nir_shader_clone(mem_ctx, fs_all), fs_info, NULL, slot, false, + blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, + g9p_group_of(targets, state, g, group_targets), + zs_attachment, gs, false, mem_ctx); + if (result != VK_SUCCESS) { + state->num_groups = 1; + result = VK_SUCCESS; + break; + } + if (!g9p_same_interface(&fs, gs)) { + if (getenv("G9P_PASSLOG")) + fprintf(stderr, "g9p: target group %u's program differs: no groups\n", g); + state->num_groups = 1; + break; + } + if (result == VK_SUCCESS) + result = pvr_gpu_upload_usc(device, gs->code, gs->code_size, 64, &state->fs_group_bo[g]); + if (result == VK_SUCCESS) + result = upload_kick(device, state->fs_group_bo[g], gs->num_regs, 0, &state->fs_group_kick[g]); + } + /* a group's program differs or failed: all targets in one, those past a pixel to memory */ + if (fs_all && state->num_groups == 1) { + for (unsigned g = 1; g < ARRAY_SIZE(state->fs_group_bo); g++) { + if (state->fs_group_kick[g].pvr_bo) + pvr_bo_suballoc_free(state->fs_group_kick[g].pvr_bo); + if (state->fs_group_bo[g]) + pvr_bo_suballoc_free(state->fs_group_bo[g]); + } + memset(state->fs_group_bo, 0, sizeof(state->fs_group_bo)); + memset(state->fs_group_kick, 0, sizeof(state->fs_group_kick)); + memset(&fs, 0, sizeof(fs)); + result = compile_stage(device, fs_all, fs_info, NULL, slot, false, + blend_options(vk_state, &blend) ? &blend : NULL, coverage_ptr, targets, + zs_attachment, &fs, false, mem_ctx); + } if (result == VK_SUCCESS) result = compile_stage(device, vs_nir, vs_info, attribs, fs.uses_frag_coord ? (int)slot : -1, fs.uses_point_coord, NULL, NULL, NULL, false, &vs, @@ -1498,6 +1679,12 @@ g9p_state_destroy(struct pvr_g9p_graphics_state *state) pvr_bo_suballoc_free(state->vs_view_bo[v]); } pvr_bo_suballoc_free(state->fs_bo); + for (unsigned g = 1; g < ARRAY_SIZE(state->fs_group_bo); g++) { + if (state->fs_group_kick[g].pvr_bo) + pvr_bo_suballoc_free(state->fs_group_kick[g].pvr_bo); + if (state->fs_group_bo[g]) + pvr_bo_suballoc_free(state->fs_group_bo[g]); + } pvr_bo_suballoc_free(state->vs_bo); } @@ -2960,6 +3147,9 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state if (result != VK_SUCCESS) return result; } + /* the fragment kick's word, for the copies of a render of target groups */ + const unsigned fs_kick_at = at + 1; + PPP_BLOCK(0xc0, fs_kick.data_offset, tex ? tex->code_offset : p->halt.code_offset, (tex ? MAX2(tex->data_size / 4, 1) : 1) << 23 | (state->num_varyings @@ -3039,7 +3229,10 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state if (((*isp0 >> 22) & 0xf) == 0xf) *isp0 = (*isp0 & ~(0xfu << 22)) | 7u << 22; } - /* An occlusion query counts draws; never the clears (isp_set) */ + /* An occlusion query counts draws; never the clears (isp_set). Target groups: in group 0. */ + const unsigned isp_at = blocks[nb - 1][0] + 1; + const uint32_t isp_uncounted = ppp[isp_at]; + if (cmd_buffer->state.vis_test_enabled && !dp->isp_set && !dp->discard) { ppp[blocks[nb - 1][0] + 1] |= BITFIELD_BIT(15) | cmd_buffer->state.vis_reg; } @@ -3082,6 +3275,42 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state result = pvr_arch_cmd_buffer_upload_general(cmd_buffer, ppp, 4 * at, &ppp_bo); if (result != VK_SUCCESS) return result; + /* + * A render of target groups: per group past the first the same words with that group's + * fragment kick, the occlusion query left out (g9p_build_group_streams() names them) + */ + unsigned group_masks[G9P_MAX_GROUPS]; + const unsigned groups = g9p_render_groups(info, g9p_current_hw_render(info), group_masks); + uint64_t group_addr[G9P_MAX_GROUPS] = { ppp_bo->dev_addr.addr }; + + if (groups <= 1 && state->num_groups > 1) + mesa_logw_once("G9P: a program of target groups in a render without them (multiview)"); + if (groups > 1) { + const bool own = state->num_groups == groups && + !memcmp(state->group_targets, group_masks, groups * sizeof(group_masks[0])); + + if (!own) + mesa_logw_once("G9P: a draw whose program writes all targets in a render of target groups"); + for (unsigned g = 1; g < groups; g++) { + const struct pvr_pds_upload *kick = own ? &state->fs_group_kick[g] : &state->fs_kick; + struct pvr_pds_upload ms_kick = *kick; + struct pvr_suballoc_bo *bo; + uint32_t words[ARRAY_SIZE(ppp)]; + + if (g9p_current_samples(info) > 1) { + result = g9p_kick_ms(cmd_buffer, kick, &ms_kick); + if (result != VK_SUCCESS) + return result; + } + memcpy(words, ppp, 4 * at); + words[fs_kick_at] = ms_kick.data_offset; + words[isp_at] = isp_uncounted; + result = pvr_arch_cmd_buffer_upload_general(cmd_buffer, words, 4 * at, &bo); + if (result != VK_SUCCESS) + return result; + group_addr[g] = bo->dev_addr.addr; + } + } for (unsigned i = 0; i < nb; i++) dump32("ppp", ppp_bo->dev_addr.addr + 4 * blocks[i][0], &ppp[blocks[i][0]], blocks[i][1]); dump_pds("draw dma", dma); @@ -3241,10 +3470,146 @@ emit_draw(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g9p_graphics_state util_dynarray_append_typed(&cs->current_sub_cmd->gfx.g9p_view_kicks, struct g9p_view_kick, k); } } + /* Target groups: where each of the draw's state-word pointers ended up */ + if (groups > 1 && csb->status == VK_SUCCESS) { + struct g9p_group_ppp r = { .bo = csb->pvr_bo, .n = nb }; + const uint32_t *start = csb->start; + + memcpy(r.addr, group_addr, sizeof(r.addr)); + for (unsigned i = 0; i < nb; i++) { + const uint64_t a = ppp_bo->dev_addr.addr + 4 * blocks[i][0]; + const uint32_t hi = blocks[i][1] << 8 | (uint32_t)(a >> 32), lo = (uint32_t)a; + const uint32_t *q = csb->next; + + while (--q > start && !(q[0] == lo && q[-1] == hi)) + ; + if (q <= start) { + mesa_logw_once("G9P: a draw's state words not found for its target groups"); + r.n = 0; + break; + } + r.dword[i] = q - start; + r.offset[i] = 4 * blocks[i][0]; + r.words[i] = blocks[i][1]; + } + if (r.n) + util_dynarray_append_typed(&cs->current_sub_cmd->gfx.g9p_group_ppps, struct g9p_group_ppp, r); + } pvr_csb_clear_relocation_mark(csb); return csb->status; } +/* + * A copy of a sub-command's control stream buffers @orig into @copy (kept with the + * sub-command), each one's link to the next naming the next copy + */ +static VkResult +g9p_copy_stream(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_gfx *sub_cmd, + struct pvr_bo *const *orig, unsigned nbo, struct pvr_bo **copy) +{ + struct pvr_device *device = cmd_buffer->device; + unsigned i; + + for (i = 0; i < nbo; i++) { + VkResult result = pvr_bo_alloc(device, device->heaps.general_heap, orig[i]->bo->size, + pvr_get_slc_cache_line_size(&device->pdevice->dev_info), + PVR_BO_ALLOC_FLAG_CPU_MAPPED, ©[i]); + + if (result != VK_SUCCESS) + return result; + util_dynarray_append_typed(&sub_cmd->g9p_view_bos, struct pvr_bo *, copy[i]); + memcpy(copy[i]->bo->map, orig[i]->bo->map, orig[i]->bo->size); + } + /* each buffer's link to the next, packed as pvr_csb_emit_link_unmarked() does */ + for (i = 0; i + 1 < nbo; i++) { + uint32_t from[2], to[2], *w = copy[i]->bo->map; + const unsigned n = orig[i]->bo->size / 4; + unsigned j; + + pvr_csb_pack (&from[0], VDMCTRL_STREAM_LINK0, link) + link.link_addrmsb = orig[i + 1]->vma->dev_addr; + pvr_csb_pack (&from[1], VDMCTRL_STREAM_LINK1, link) + link.link_addrlsb = orig[i + 1]->vma->dev_addr; + pvr_csb_pack (&to[0], VDMCTRL_STREAM_LINK0, link) + link.link_addrmsb = copy[i + 1]->vma->dev_addr; + pvr_csb_pack (&to[1], VDMCTRL_STREAM_LINK1, link) + link.link_addrlsb = copy[i + 1]->vma->dev_addr; + for (j = 0; j + 1 < n && !(w[j] == from[0] && w[j + 1] == from[1]); j++) + ; + if (j + 1 == n) + return vk_errorf(cmd_buffer, VK_ERROR_UNKNOWN, "G9P: control stream link not found"); + w[j] = to[0]; + w[j + 1] = to[1]; + } + /* indirect draws: their words in the copy are filled at submission too (patch_indirect()) */ + const unsigned num_indirects = util_dynarray_num_elements(&sub_cmd->g9p_indirects, struct g9p_indirect); + + for (unsigned k = 0; k < num_indirects; k++) { + struct g9p_indirect ind = + *util_dynarray_element(&sub_cmd->g9p_indirects, struct g9p_indirect, k); + + for (i = 0; i < nbo; i++) { + const uint32_t *map = orig[i]->bo->map; + + if (ind.words >= map && ind.words < map + orig[i]->bo->size / 4) { + ind.words = (uint32_t *)copy[i]->bo->map + (ind.words - map); + util_dynarray_append_typed(&sub_cmd->g9p_indirects, struct g9p_indirect, ind); + break; + } + } + } + return VK_SUCCESS; +} + +/* + * A render of target groups: per group past the first a copy of the sub-command's control + * stream whose draws name that group's state words (struct g9p_group_ppp) + */ +VkResult +PVR_PER_ARCH(g9p_build_group_streams)(struct pvr_cmd_buffer *cmd_buffer, + struct pvr_sub_cmd_gfx *sub_cmd) +{ + struct pvr_csb *csb = &sub_cmd->control_stream; + const unsigned nbo = list_length(&csb->pvr_bo_list); + struct pvr_bo *orig[64], *copy[64]; + unsigned i = 0, groups = 1; + + util_dynarray_foreach (&sub_cmd->g9p_group_ppps, struct g9p_group_ppp, r) { + for (unsigned g = 1; g < G9P_MAX_GROUPS; g++) + groups = r->addr[g] ? MAX2(groups, g + 1) : groups; + } + if (groups <= 1) + return VK_SUCCESS; + if (csb->stream_type != PVR_CMD_STREAM_TYPE_GRAPHICS || nbo > ARRAY_SIZE(orig)) { + mesa_logw_once("G9P: target groups not rendered for this command buffer"); + return VK_SUCCESS; + } + list_for_each_entry (struct pvr_bo, bo, &csb->pvr_bo_list, link) + orig[i++] = bo; + for (unsigned g = 1; g < groups; g++) { + VkResult result = g9p_copy_stream(cmd_buffer, sub_cmd, orig, nbo, copy); + + if (result != VK_SUCCESS) + return result; + util_dynarray_foreach (&sub_cmd->g9p_group_ppps, struct g9p_group_ppp, r) { + for (i = 0; i < nbo && orig[i] != r->bo; i++) + ; + if (i == nbo || !r->addr[g]) + continue; + uint32_t *w = copy[i]->bo->map; + + for (unsigned b = 0; b < r->n; b++) { + const uint64_t a = r->addr[g] + r->offset[b]; + + w[r->dword[b] - 1] = r->words[b] << 8 | (uint32_t)(a >> 32); + w[r->dword[b]] = (uint32_t)a; + } + } + sub_cmd->g9p_group_stream[g] = copy[0]->vma->dev_addr.addr; + } + return VK_SUCCESS; +} + /* * Multiview with the view index in the vertex shader: per view a copy of the sub-command's * control stream, its links between buffers and its draws' kick words naming that view's @@ -3254,7 +3619,6 @@ VkResult PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_gfx *sub_cmd) { - struct pvr_device *device = cmd_buffer->device; struct pvr_csb *csb = &sub_cmd->control_stream; const unsigned nbo = list_length(&csb->pvr_bo_list); struct pvr_bo *orig[64], *copy[64]; @@ -3270,37 +3634,10 @@ PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, orig[i++] = bo; u_foreach_bit (view, sub_cmd->view_mask) { - for (i = 0; i < nbo; i++) { - VkResult result = pvr_bo_alloc(device, device->heaps.general_heap, orig[i]->bo->size, - pvr_get_slc_cache_line_size(&device->pdevice->dev_info), - PVR_BO_ALLOC_FLAG_CPU_MAPPED, ©[i]); + VkResult result = g9p_copy_stream(cmd_buffer, sub_cmd, orig, nbo, copy); - if (result != VK_SUCCESS) - return result; - util_dynarray_append_typed(&sub_cmd->g9p_view_bos, struct pvr_bo *, copy[i]); - memcpy(copy[i]->bo->map, orig[i]->bo->map, orig[i]->bo->size); - } - /* each buffer's link to the next, packed as pvr_csb_emit_link_unmarked() does */ - for (i = 0; i + 1 < nbo; i++) { - uint32_t from[2], to[2], *w = copy[i]->bo->map; - const unsigned n = orig[i]->bo->size / 4; - unsigned j; - - pvr_csb_pack (&from[0], VDMCTRL_STREAM_LINK0, link) - link.link_addrmsb = orig[i + 1]->vma->dev_addr; - pvr_csb_pack (&from[1], VDMCTRL_STREAM_LINK1, link) - link.link_addrlsb = orig[i + 1]->vma->dev_addr; - pvr_csb_pack (&to[0], VDMCTRL_STREAM_LINK0, link) - link.link_addrmsb = copy[i + 1]->vma->dev_addr; - pvr_csb_pack (&to[1], VDMCTRL_STREAM_LINK1, link) - link.link_addrlsb = copy[i + 1]->vma->dev_addr; - for (j = 0; j + 1 < n && !(w[j] == from[0] && w[j + 1] == from[1]); j++) - ; - if (j + 1 == n) - return vk_errorf(cmd_buffer, VK_ERROR_UNKNOWN, "G9P: control stream link not found"); - w[j] = to[0]; - w[j + 1] = to[1]; - } + if (result != VK_SUCCESS) + return result; util_dynarray_foreach (&sub_cmd->g9p_view_kicks, struct g9p_view_kick, k) { for (i = 0; i < nbo && orig[i] != k->bo; i++) ; @@ -3318,6 +3655,39 @@ PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, return VK_SUCCESS; } +/* + * A clear draw in a render of target groups: per group a program that keeps the tile's + * words, or clears colour target @colour's from the common store in its group (-1: none) + */ +static VkResult +g9p_clear_group_kicks(struct pvr_cmd_buffer *cmd_buffer, int colour, struct pvr_g9p_graphics_state *state) +{ + const struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info; + const struct pvr_renderpass_hwsetup_render *hw = g9p_current_hw_render(info); + unsigned masks[G9P_MAX_GROUPS]; + const unsigned n = g9p_render_groups(info, hw, masks); + + if (n <= 1) + return VK_SUCCESS; + for (unsigned g = 0; g < n; g++) { + const unsigned words = g9p_group_words(info, hw, masks[g], G9P_MAX_TARGETS); + VkResult result; + + if (colour >= 0 && (masks[g] >> colour & 1)) + result = g9p_clear_one_kick(cmd_buffer->device, words, g9p_group_words(info, hw, masks[g], colour), + g9p_group_words(info, hw, BITFIELD_BIT(colour), G9P_MAX_TARGETS), + &state->fs_group_kick[g]); + else + result = g9p_keep_kick(cmd_buffer->device, words, &state->fs_group_kick[g]); + if (result != VK_SUCCESS) + return result; + state->group_targets[g] = masks[g]; + } + state->num_groups = n; + state->fs_kick = state->fs_group_kick[0]; + return VK_SUCCESS; +} + /* * vkCmdClearAttachments of depth and/or stencil: a draw of the rectangles * with depth ALWAYS (written when cleared) and stencil REPLACE by the @@ -3359,6 +3729,8 @@ g9p_ds_clear(struct pvr_cmd_buffer *cmd_buffer, const VkClearAttachment *att, if (targets > 1) { VkResult keep = g9p_keep_kick(cmd_buffer->device, g9p_render_words(info, hw, targets), &state.fs_kick); + if (keep == VK_SUCCESS) + keep = g9p_clear_group_kicks(cmd_buffer, -1, &state); if (keep != VK_SUCCESS) return keep; } @@ -3554,6 +3926,8 @@ PVR_PER_ARCH(g9p_clear_attachments)(struct pvr_cmd_buffer *cmd_buffer, uint32_t g9p_render_words(info, hw, hw->eot_surface_count), g9p_render_words(info, hw, t), g9p_output_words(g9p_format_output(format)), &state.fs_kick); + if (result == VK_SUCCESS) + result = g9p_clear_group_kicks(cmd_buffer, colour->colorAttachment, &state); if (result != VK_SUCCESS) return result; state.uses_fbfetch = true; @@ -4024,10 +4398,73 @@ g9p_eot_addr(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwse return g9p_colour_scratch_slot(scratch, t); } +static VkResult +g9p_job_init_targets(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_renderpass_hwsetup_render *hw_render, + struct pvr_render_job *job); + +/* + * A render's job. With target groups (g9p_render_groups()) the same for each group's + * surfaces alone, a render each (pvr_process_graphics_cmd()): only the last stores depth + * and stencil, the others load what the render started with. + */ VkResult PVR_PER_ARCH(g9p_job_init)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_renderpass_hwsetup_render *hw_render, struct pvr_render_job *job) +{ + const struct pvr_render_pass_info *info = &cmd_buffer->state.render_pass_info; + unsigned masks[G9P_MAX_GROUPS]; + const unsigned n = g9p_render_groups(info, hw_render, masks); + + job->g9p_groups = 1; + if (n <= 1) + return g9p_job_init_targets(cmd_buffer, hw_render, job); + for (unsigned g = n; g-- > 0;) { + struct pvr_renderpass_hwsetup_render part = *hw_render; + struct pvr_renderpass_hwsetup_eot_surface eot[2 * G9P_MAX_TARGETS]; + struct pvr_renderpass_colorinit init[2 * G9P_MAX_TARGETS]; + VkResult result; + + part.eot_surfaces = eot; + part.color_init = init; + part.eot_surface_count = 0; + part.color_init_count = 0; + for (unsigned t = 0; t < hw_render->eot_surface_count && part.eot_surface_count < ARRAY_SIZE(eot); t++) { + /* a surface's colour target, a resolve's its source's */ + const unsigned s = g9p_words_surface(hw_render, t); + unsigned j = 0; + + for (unsigned k = 0; k < s; k++) + j += !hw_render->eot_surfaces[k].need_resolve; + if (masks[g] >> j & 1) + eot[part.eot_surface_count++] = hw_render->eot_surfaces[t]; + } + for (unsigned i = 0; i < hw_render->color_init_count && part.color_init_count < ARRAY_SIZE(init); i++) { + for (unsigned t = 0; t < part.eot_surface_count; t++) { + if (!eot[t].need_resolve && eot[t].attachment_idx == hw_render->color_init[i].index) { + init[part.color_init_count++] = hw_render->color_init[i]; + break; + } + } + } + result = g9p_job_init_targets(cmd_buffer, &part, job); + if (result != VK_SUCCESS) + return result; + if (job->g9p.colour_scratch) + mesa_logw_once("G9P: target groups with a level the end of tile can't store to"); + job->g9p.zs_keep = g + 1 < n; + job->g9p_group[g] = job->g9p; + job->g9p_group_eot_data[g] = job->view_state.view[0].pds_pixel_event_data_offset; + } + job->g9p_groups = n; + return VK_SUCCESS; +} + +static VkResult +g9p_job_init_targets(struct pvr_cmd_buffer *cmd_buffer, + const struct pvr_renderpass_hwsetup_render *hw_render, + struct pvr_render_job *job) { struct pvr_device *device = cmd_buffer->device; const struct pvr_g9p_gfx_programs *p = &device->g9p.gfx; @@ -4520,9 +4957,9 @@ g9p_ds_zls(const struct pvr_render_job *job, uint64_t *zlsctl, uint64_t *stencil ? ROGUE_CR_ZLS_FORMAT_TYPE_16BITINT : ROGUE_CR_ZLS_FORMAT_TYPE_F32Z; z.zloaden = job->has_depth_attachment && job->ds.load.d; - z.zstoreen = job->has_depth_attachment && job->ds.store.d; + z.zstoreen = job->has_depth_attachment && job->ds.store.d && !job->g9p.zs_keep; z.sloaden = job->has_stencil_attachment && job->ds.load.s; - z.sstoreen = job->has_stencil_attachment && job->ds.store.s; + z.sstoreen = job->has_stencil_attachment && job->ds.store.s && !job->g9p.zs_keep; z.forcezload = z.zloaden || z.sloaden; z.forcezstore = z.zstoreen || z.sstoreen; z.loadtwiddled = z.storetwiddled = true; diff --git a/src/imagination/vulkan/pvr_arch_queue.c b/src/imagination/vulkan/pvr_arch_queue.c index 4f0a5f7a2e1..df42964b662 100644 --- a/src/imagination/vulkan/pvr_arch_queue.c +++ b/src/imagination/vulkan/pvr_arch_queue.c @@ -705,6 +705,35 @@ static VkResult pvr_process_graphics_cmd(struct pvr_device *device, u_foreach_bit (view_idx, sub_cmd->view_mask) { VkResult result; + /* + * G9P target groups (more colour words than a pixel holds): the render once per + * group, each with its own end of tile and background and its copy of the stream + */ + if (sub_cmd->job.g9p.enabled && sub_cmd->job.g9p_groups > 1) { + const uint64_t oclqry = sub_cmd->job.g9p.oclqry_base; + const pvr_dev_addr_t stream = sub_cmd->job.ctrl_stream_addr; + const uint32_t eot_data = sub_cmd->job.view_state.view[view_idx].pds_pixel_event_data_offset; + + for (unsigned g = 0; g < sub_cmd->job.g9p_groups; g++) { + if (g && !sub_cmd->g9p_group_stream[g]) + break; + sub_cmd->job.g9p = sub_cmd->job.g9p_group[g]; + sub_cmd->job.g9p.oclqry_base = oclqry; + sub_cmd->job.view_state.view[view_idx].pds_pixel_event_data_offset = + sub_cmd->job.g9p_group_eot_data[g]; + if (g) + sub_cmd->job.ctrl_stream_addr.addr = sub_cmd->g9p_group_stream[g]; + result = pvr_process_graphics_cmd_for_view(device, queue, cmd_buffer, sub_cmd, view_idx); + sub_cmd->job.ds.addr = ds_addr; + sub_cmd->job.ctrl_stream_addr = stream; + if (result != VK_SUCCESS) + return result; + } + sub_cmd->job.g9p = sub_cmd->job.g9p_group[0]; + sub_cmd->job.g9p.oclqry_base = oclqry; + sub_cmd->job.view_state.view[view_idx].pds_pixel_event_data_offset = eot_data; + continue; + } result = pvr_process_graphics_cmd_for_view(device, queue, cmd_buffer, diff --git a/src/imagination/vulkan/pvr_cmd_buffer.h b/src/imagination/vulkan/pvr_cmd_buffer.h index 87168ee1fcb..bfc6c7bf966 100644 --- a/src/imagination/vulkan/pvr_cmd_buffer.h +++ b/src/imagination/vulkan/pvr_cmd_buffer.h @@ -199,6 +199,14 @@ struct pvr_sub_cmd_gfx { struct util_dynarray g9p_view_kicks; uint64_t g9p_view_stream[PVR_MAX_MULTIVIEW]; struct util_dynarray g9p_view_bos; + /* + * G9P: a render of target groups (more colour words than a pixel holds): each + * draw's state words per group (struct g9p_group_ppp), and per group past the first + * a copy of the control stream naming them (pvr_arch_g9p_build_group_streams()), its + * buffers in g9p_view_bos + */ + struct util_dynarray g9p_group_ppps; + uint64_t g9p_group_stream[4]; /* Control stream builder object */ struct pvr_csb control_stream; diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index 0e93f2f0f30..2f84780a3e9 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -156,6 +156,21 @@ struct pvr_sub_cmd_gfx; VkResult PVR_PER_ARCH(g9p_build_view_streams)(struct pvr_cmd_buffer *cmd_buffer, struct pvr_sub_cmd_gfx *sub_cmd); +/* + * A draw in a render of target groups: where its state-word pointers are in the + * control stream (each PPP_STATE's address word), and per group its state words + */ +#define G9P_DRAW_PPP_BLOCKS 12 +struct g9p_group_ppp { + struct pvr_bo *bo; + unsigned n; + uint32_t dword[G9P_DRAW_PPP_BLOCKS], offset[G9P_DRAW_PPP_BLOCKS], words[G9P_DRAW_PPP_BLOCKS]; + uint64_t addr[4]; +}; + +VkResult PVR_PER_ARCH(g9p_build_group_streams)(struct pvr_cmd_buffer *cmd_buffer, + struct pvr_sub_cmd_gfx *sub_cmd); + /* The job streams with the Apple register block; return their length */ uint32_t PVR_PER_ARCH(g9p_geom_stream)(const struct pvr_device *device, const struct pvr_render_job *job, uint8_t *buf, @@ -247,6 +262,7 @@ bool PVR_PER_ARCH(g9p_meta_upload)(struct pvr_cmd_buffer *cmd_buffer, const VkCo # define pvr_arch_g9p_cmd_draw PVR_PER_ARCH(g9p_cmd_draw) # define pvr_arch_g9p_job_init PVR_PER_ARCH(g9p_job_init) # define pvr_arch_g9p_build_view_streams PVR_PER_ARCH(g9p_build_view_streams) +# define pvr_arch_g9p_build_group_streams PVR_PER_ARCH(g9p_build_group_streams) # define pvr_arch_g9p_geom_stream PVR_PER_ARCH(g9p_geom_stream) # define pvr_arch_g9p_frag_stream PVR_PER_ARCH(g9p_frag_stream) # define pvr_arch_g9p_device_init PVR_PER_ARCH(g9p_device_init) diff --git a/src/imagination/vulkan/pvr_job_render.h b/src/imagination/vulkan/pvr_job_render.h index 79e52385c2f..52294e6ec2f 100644 --- a/src/imagination/vulkan/pvr_job_render.h +++ b/src/imagination/vulkan/pvr_job_render.h @@ -149,7 +149,7 @@ struct pvr_render_job { #define G9P_LOAD_OP_NONE (-1) /* Apple G9P (pvr_arch_g9p_gfx.c): what the kernel's Apple register block takes */ - struct { + struct pvr_g9p_job { bool enabled; /* end-of-tile PDS code; its data is the view's pds_pixel_event_data_offset */ uint32_t eot_code; @@ -182,8 +182,19 @@ struct pvr_render_job { * (don't care: the render pass setup initializes nothing) */ int load_op; + /* a target group's render before the last: depth and stencil loaded, not stored */ + bool zs_keep; } g9p; + /* + * More colour words than a pixel holds: the render once per group of targets + * (g9p_target_groups()), each with its own background and end of tile and its + * own copy of the control stream (but group 0); group 0's are in g9p + */ + unsigned g9p_groups; + struct pvr_g9p_job g9p_group[4]; + uint32_t g9p_group_eot_data[4]; + /* * G9P_SYNC: the pipelines of the job's first draws, reported on a timeout; * G9P_TRACE: also the render area and the 32-pixel tiles the draws' region diff --git a/src/imagination/vulkan/pvr_pipeline.h b/src/imagination/vulkan/pvr_pipeline.h index e53d434dcfa..7d559192f9e 100644 --- a/src/imagination/vulkan/pvr_pipeline.h +++ b/src/imagination/vulkan/pvr_pipeline.h @@ -147,6 +147,16 @@ struct pvr_g9p_graphics_state { struct pvr_suballoc_bo *vs_bo, *fs_bo; /* vertex kick (code and data), fragment kick bundle, DOUTI program */ struct pvr_pds_upload vs_kick, fs_kick; + /* + * More colour words than a pixel holds: the targets in groups, a render each + * (g9p_target_groups()), and per group the fragment program that writes only its + * targets, all with the same uniforms, textures and varyings; group 0's are fs_bo + * and fs_kick + */ + unsigned num_groups; + unsigned group_targets[4]; + struct pvr_suballoc_bo *fs_group_bo[4]; + struct pvr_pds_upload fs_group_kick[4]; /* multiview: a vertex kick per view and its program (the view index a constant in it) */ struct pvr_pds_upload vs_kick_views[PVR_MAX_MULTIVIEW]; struct pvr_suballoc_bo *vs_view_bo[PVR_MAX_MULTIVIEW]; From 24b98f8a713543efefde95e8cb74875b3db067e7 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 19:10:56 -0600 Subject: [PATCH 40/44] pvr/g9p: compute values past a workgroup's bank-1 share in bank 0 A10 compute kept every value in bank 1, which the kernel record's register field allocates, and a workgroup's tasks must fit there together: 256 invocations take at most 24 registers each. A shader of 256 invocations that needed 34 (eglcompute's divergent case, shared atomics and compare-and-swap loops) was refused at pipeline creation, though the device offers 256. Such a shader is now compiled again as A9 compute is: values in bank 1 up to what the workgroup may hold there (24 at 256 invocations, 32 at 192, 40 at 160, 64 at 128, 96 at 64), the mask stack after them, and the rest parked in bank 0. The record's field takes the bank-1 count and the kick's DOUTU temps allocate bank 0, as in A9 kernels. Shaders that fit keep their single bank. eglcompute passes every case up to the reported 256 invocations. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 26 +++++++++++++++++++++++++- src/imagination/g9p/g9p_compile.h | 1 + src/imagination/vulkan/pvr_arch_g9p.c | 26 ++++++++++++++++++++++++-- src/imagination/vulkan/pvr_pipeline.h | 2 ++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 39772cd7a4a..8f5796e5eb3 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -5033,6 +5033,12 @@ static thread_local bool g9p_no_schedule; * 100 registers with homes on the first attempt, 4 instances, geometry 10x slower) */ static thread_local bool g9p_no_homes; +/* + * A10 compute whose registers are too many for its workgroup in bank 1: values in bank 1 + * below this many (the mask stack after them), the rest parked in bank 0 as A9 compute + * does (g9p_compile_compute_bank0()); 0: everything in bank 1 + */ +static thread_local unsigned g9p_bank1_cap; /* vertex shaders: attribute loads moved next to their uses (g9p_compile_vertex() tries both) */ static thread_local bool g9p_move_inputs; @@ -5828,13 +5834,15 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, ctx.c->w = ctx.c->initial_w; ctx.c->capacity = G9P_INITIAL_CODE_WORDS; ctx.c->fragment = stage == MESA_SHADER_FRAGMENT; - ctx.c->high_bank0 = stage == MESA_SHADER_COMPUTE && out->a9; + ctx.c->high_bank0 = stage == MESA_SHADER_COMPUTE && (out->a9 || g9p_bank1_cap); ctx.c->a9_stack = stage == MESA_SHADER_COMPUTE && out->a9; /* A9 compute: values in bank 1 up to the workgroup's limit, the mask stack after them; the rest in * bank-0 homes (g9p_a9_bank1_limit()) */ if (stage == MESA_SHADER_COMPUTE && out->a9) ctx.value_cap = g9p_a9_bank1_limit(MAX2(out->local_size[0], 1) * MAX2(out->local_size[1], 1) * MAX2(out->local_size[2], 1)) - 1; + else if (stage == MESA_SHADER_COMPUTE && g9p_bank1_cap) + ctx.value_cap = g9p_bank1_cap - 1; ctx.reg = ralloc_array(mem_ctx, int8_t, impl->ssa_alloc * 4); ctx.uni = ralloc_array(mem_ctx, int16_t, impl->ssa_alloc * 4); memset(ctx.uni, -1, impl->ssa_alloc * 4 * sizeof(*ctx.uni)); @@ -6619,6 +6627,22 @@ lower_task_barrier(nir_builder *b, nir_intrinsic_instr *intr, void *data) return true; } +/* + * A10: the same with at most @bank1 registers in bank 1, the kernel record's register field; + * the values past them in bank 0, which the kick's DOUTU temps (out->num_regs) allocate. + * out->a9_bank1_regs: the bank-1 count. + */ +bool +g9p_compile_compute_bank0(nir_shader *nir, struct g9p_shader *out, void *mem_ctx, unsigned bank1) +{ + bool ok; + + g9p_bank1_cap = bank1; + ok = g9p_compile_compute(nir, out, mem_ctx); + g9p_bank1_cap = 0; + return ok; +} + bool g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx) { diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index c58e8bbd76b..891d83c344c 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -304,6 +304,7 @@ int g9p_buffer_slot(struct g9p_shader *out, unsigned set, unsigned binding); * stderr for anything the backend does not handle yet. */ bool g9p_compile_compute(nir_shader *nir, struct g9p_shader *out, void *mem_ctx); +bool g9p_compile_compute_bank0(nir_shader *nir, struct g9p_shader *out, void *mem_ctx, unsigned bank1); /* The varying slot after the vertex shader's own: gl_FragCoord's */ unsigned g9p_frag_coord_slot(const nir_shader *vs); diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index e59b1881a1b..90efaa642eb 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -199,6 +199,7 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( goto out; nir_shader *capture = getenv("G9P_CAPTURE") ? nir_shader_clone(mem_ctx, nir) : NULL; + nir_shader *again = nir_shader_clone(mem_ctx, nir); if (!g9p_compile_compute(nir, &shader, mem_ctx)) { mesa_loge("G9P: compute shader not supported by the backend yet"); @@ -216,7 +217,24 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( */ const uint32_t threads = shader.local_size[0] * shader.local_size[1] * shader.local_size[2]; - if (DIV_ROUND_UP(threads, 32) > g9p_compute_max_tasks(shader.num_regs)) { + /* + * Too many for bank 1: again with the values past what the workgroup's tasks may hold + * there in bank 0, as A9 compute keeps them (g9p_compile_compute_bank0()); the kick's + * DOUTU temps allocate bank 0 + */ + state->bank1_regs = 0; + if (DIV_ROUND_UP(threads, 32) > g9p_compute_max_tasks(shader.num_regs) && !getenv("G9P_NO_BANK0")) { + const unsigned tasks = DIV_ROUND_UP(threads, 32); + const unsigned bank1 = tasks <= 2 ? 96 : tasks <= 4 ? 64 : tasks <= 5 ? 40 : tasks <= 6 ? 32 : 24; + struct g9p_shader capped; + + if (g9p_compile_compute_bank0(again, &capped, mem_ctx, bank1) && + DIV_ROUND_UP(threads, 32) <= g9p_compute_max_tasks(capped.a9_bank1_regs)) { + shader = capped; + state->bank1_regs = capped.a9_bank1_regs; + } + } + if (DIV_ROUND_UP(threads, 32) > g9p_compute_max_tasks(state->bank1_regs ? state->bank1_regs : shader.num_regs)) { mesa_loge("G9P: %u invocations of %u registers each do not fit the USC", threads, shader.num_regs); result = vk_errorf(device, VK_ERROR_UNKNOWN, @@ -481,6 +499,9 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, * -> word 2 (z; COMPUTE.md "y and z", Apple's EncodeComputePDSProgram) */ g9p_pds_kick_data(kernel_data, usc_offset(device, state->shader_bo)); + /* values in bank 0 too: the DOUTU temps hold them all (A9 compute's form) */ + if (state->bank1_regs) + kernel_data[1] = DIV_ROUND_UP(MAX2(state->num_regs, 8), 4) << 3; kernel_data[2] = 0x80000000; kernel_data[4] = 0x10000002; kernel_data[5] = 0x90004001; @@ -516,7 +537,8 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, g9p_cdm_dma_task(G9P_CDM_DATA_BLOCKS(bind_dwords / 4), bind.data_offset, bind.code_offset); const struct g9p_cdm_record kernel_rec = g9p_cdm_kernel( - G9P_CDM_KERNEL_REGS(state->num_regs) | G9P_CDM_ANY_CLUSTER | G9P_CDM_FENCE | + G9P_CDM_KERNEL_REGS(state->bank1_regs ? state->bank1_regs : state->num_regs) | + G9P_CDM_ANY_CLUSTER | G9P_CDM_FENCE | G9P_CDM_DATA_BLOCKS(G9P_KERNEL_DATA_DWORDS / 4), kernel.data_offset, kernel.code_offset, group_count[0], group_count[1], group_count[2], g9p_compute_hw_threads(threads), tg_alloc); diff --git a/src/imagination/vulkan/pvr_pipeline.h b/src/imagination/vulkan/pvr_pipeline.h index 7d559192f9e..f97264bcfc4 100644 --- a/src/imagination/vulkan/pvr_pipeline.h +++ b/src/imagination/vulkan/pvr_pipeline.h @@ -127,6 +127,8 @@ struct pvr_g9p_compute_state { unsigned set, binding; } buffers[40]; /* G9P_MAX_BUFFERS */ unsigned num_regs; + /* registers in bank 1 (the record's field) when the rest are in bank 0; 0: all in bank 1 */ + unsigned bank1_regs; bool uses_workgroup_id; unsigned local_size[3]; PVR_G5P_COMPUTE_STATE_FIELDS unsigned shared_size; From 6fb51f08e1516e3c7fe52bb0bc4fdffeecd08313 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 20:34:01 -0600 Subject: [PATCH 41/44] pvr/g9p: a compute dispatch's common store holds every buffer The DMA task before a kernel loads each buffer's address (two dwords) and the workgroup's thread count into the common store. Its record's word 6 kept the 1 iPadOS writes there, and that field is the store's size in blocks of 16 dwords: varied on the A10, kernels read other values from dword 16 on with one block. A shader with eight storage buffers and four uniform buffers lost the GPU, and the buffers past the first 16 dwords were never written. The field now covers the buffers' addresses and the count. eglcstex's case of eight storage and four uniform buffers passes, and fails with the field forced back to one block. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_cdm.h | 11 ++++++++--- src/imagination/vulkan/pvr_arch_g9p.c | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/imagination/g9p/g9p_cdm.h b/src/imagination/g9p/g9p_cdm.h index a140ab5230b..86b9f34422a 100644 --- a/src/imagination/g9p/g9p_cdm.h +++ b/src/imagination/g9p/g9p_cdm.h @@ -130,13 +130,18 @@ static inline struct g9p_cdm_record g9p_cdm_task(uint32_t k0, uint32_t data, uin /* * A DMA-only task loading constants into the USC common store (the buffer * binding; iPadOS's constants record). Word 6 = 0x40000001 as observed on - * every captured DMA task (low bit: Rogue's common-store allocation, open). + * every captured DMA task. Its low bits are the common store the kernels + * after it have, in blocks of 16 dwords (decoded + tested on the A10: with + * one block, kernels read other values from dword 16 on, and buffer + * addresses there lost the GPU; 16 blocks, 256 dwords, read right). + * @dwords is the store the kernels read. */ -static inline struct g9p_cdm_record g9p_cdm_dma_task(uint32_t k0, uint32_t data, uint32_t code) +static inline struct g9p_cdm_record g9p_cdm_dma_task(uint32_t k0, uint32_t data, uint32_t code, + uint32_t dwords) { struct g9p_cdm_record r = g9p_cdm_task(k0, data, G9P_CDM_K1_DMA, code); - r.w[6] |= 1; + r.w[6] |= dwords ? (dwords + 15) / 16 : 1; return r; } diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 90efaa642eb..3a884903178 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -533,9 +533,10 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, const uint32_t tg_alloc = MAX2(DIV_ROUND_UP(G9P_SHARED_BASE + state->shared_size, 16), 1); { struct pvr_csb *csb = &sub_cmd->control_stream; + /* the buffers' addresses and the thread count after them */ const struct g9p_cdm_record bind_rec = g9p_cdm_dma_task(G9P_CDM_DATA_BLOCKS(bind_dwords / 4), bind.data_offset, - bind.code_offset); + bind.code_offset, 2 * n + 1); const struct g9p_cdm_record kernel_rec = g9p_cdm_kernel( G9P_CDM_KERNEL_REGS(state->bank1_regs ? state->bank1_regs : state->num_regs) | G9P_CDM_ANY_CLUSTER | G9P_CDM_FENCE | From fe3d74abf61cb82fecc38dafe498a917f11d5cc1 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 20:37:44 -0600 Subject: [PATCH 42/44] pvr/g9p: textures in compute shaders A10 compute shaders could not sample. Any texture read, texelFetch and textureSize included, failed the pipeline, so a GLES 3.1 compute shader with a sampler never ran. Compute shaders now sample as vertex shaders do, with the vertex form of the sample at an explicit level (level 0 for texture()), and gradients and compare offsets lowered as for vertex shaders. The textures' states and sampler words sit in the common store from dword 96, past the buffers' addresses and the thread count. 16 textures end at dword 255, the last a sample's fields reach. The dispatch copies them there from the bound descriptor sets, and the DMA task's store covers them. A cube sample takes r0-r2, as in vertex shaders, so the local index moves out of r0 in such a shader. eglcstex passes: texelFetch, textureLod and textureSize of 2D arrays, 3D textures and cubes, and all 16 texture units in one shader. eglmsfetch's compute shader reads a 2D texture with texelFetch and texture(). Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 50 ++++++++++++++---- src/imagination/g9p/g9p_compile.h | 6 +++ src/imagination/vulkan/pvr_arch_g9p.c | 63 ++++++++++++++++++----- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 28 ++++++++++ src/imagination/vulkan/pvr_g9p.h | 5 ++ src/imagination/vulkan/pvr_pipeline.h | 7 +++ 6 files changed, 138 insertions(+), 21 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index 8f5796e5eb3..d9aa35a684c 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -350,7 +350,8 @@ struct g9p_ctx { bool repeat_loads; /* an attempt after running out of registers */ bool point_size; /* a point pipeline's vertex shader: the size is the last output */ int psize_reg; - int vertex_reg, instance_reg; /* vertex shaders: r0-r1, or copies of them */ + /* vertex shaders: r0-r1, or copies of them; compute: the local index's copy, or r0 */ + int vertex_reg, instance_reg; bool reserve_st_low; /* the mask stack is r15, reserved (compile_back()) */ unsigned reserve_low; /* r11-r14 for address work, after running out (compile_back()) */ unsigned ip; @@ -2727,17 +2728,25 @@ count_textures(nir_function_impl *impl) return n; } +/* Where the textures' words start in the common store (g9p_tex_state()) */ +static unsigned +tex_base(const struct g9p_ctx *ctx) +{ + return ctx->stage == MESA_SHADER_VERTEX ? G9P_VS_TEX_BASE + : ctx->stage == MESA_SHADER_COMPUTE ? G9P_CS_TEX_BASE + : 0; +} + static unsigned tex_state_dword(const struct g9p_ctx *ctx, unsigned slot) { - return g9p_tex_state(ctx->stage == MESA_SHADER_VERTEX ? G9P_VS_TEX_BASE : 0, slot); + return g9p_tex_state(tex_base(ctx), slot); } static unsigned tex_sampler_dword(const struct g9p_ctx *ctx, unsigned slot) { - return g9p_tex_sampler(ctx->stage == MESA_SHADER_VERTEX ? G9P_VS_TEX_BASE : 0, - ctx->tex_total, slot); + return g9p_tex_sampler(tex_base(ctx), ctx->tex_total, slot); } /* The varying component an SSA scalar is a plain load of, or -1 */ @@ -3317,7 +3326,8 @@ emit_tex(struct g9p_ctx *ctx, nir_tex_instr *tex) return emit_txs(ctx, tex); if (tex->op == nir_texop_txf_ms) return emit_txf_ms(ctx, tex); - if (ctx->stage == MESA_SHADER_VERTEX) + /* compute samples as vertex shaders do: explicit levels, no derivatives */ + if (ctx->stage == MESA_SHADER_VERTEX || ctx->stage == MESA_SHADER_COMPUTE) return emit_tex_vertex(ctx, tex); if (tex->is_array || tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE || tex->sampler_dim == GLSL_SAMPLER_DIM_3D) @@ -3398,8 +3408,8 @@ emit_intrinsic(struct g9p_ctx *ctx, nir_intrinsic_instr *intr) G9P_A9_INTRINSICS switch (intr->intrinsic) { case nir_intrinsic_load_local_invocation_index: - /* r0 at entry; reserved from the start when used */ - ctx->reg[key(&intr->def, 0)] = 0; + /* r0 at entry; reserved from the start when used, or its copy (generate()) */ + ctx->reg[key(&intr->def, 0)] = ctx->stage == MESA_SHADER_COMPUTE ? ctx->vertex_reg : 0; return true; case nir_intrinsic_load_workgroup_id: @@ -4285,6 +4295,28 @@ generate(struct g9p_ctx *ctx, nir_function_impl *impl, unsigned st) g9p_alu2(ctx->c, G9P_OR, ctx->vertex_reg, 0, 0, false); g9p_alu2(ctx->c, G9P_OR, ctx->instance_reg, 1, 1, false); } + /* + * A10 compute takes the vertex form's samples (emit_tex_vertex()), whose cubes use r0-r3 + * too: the local index, r0 at entry, then in a copy + */ + if (ctx->stage == MESA_SHADER_COMPUTE && !ctx->out->a9 && uses_layered_tex(impl)) { + bool local_index = false; + + nir_foreach_block(block, impl) { + nir_foreach_instr(instr, block) + local_index |= instr->type == nir_instr_type_intrinsic && + nir_instr_as_intrinsic(instr)->intrinsic == + nir_intrinsic_load_local_invocation_index; + } + ctx->busy |= BITFIELD64_RANGE(0, 4), ctx->pinned |= BITFIELD64_RANGE(0, 4); + if (local_index) { + ctx->vertex_reg = alloc_reg(ctx); + if (ctx->vertex_reg < 0) + return false; + ctx->pinned |= BITFIELD64_BIT(ctx->vertex_reg); + g9p_alu2(ctx->c, G9P_OR, ctx->vertex_reg, 0, 0, false); + } + } /* compute: r0 holds the local index from the start when it is used. Pinned: every * load_local_invocation_index reads r0 without code, so r0 must not be reallocated after @@ -5710,7 +5742,7 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, const nir_lower_tex_options tex = { .lower_txp = ~0u, .lower_txs_lod = true, - .lower_txd = stage == MESA_SHADER_VERTEX, + .lower_txd = stage == MESA_SHADER_VERTEX || (stage == MESA_SHADER_COMPUTE && !out->a9), .lower_txd_cb = lower_txd_to_level, /* A9: 1D textures are 2D ones of height 1 (lower_image_1d()) */ .lower_1d = out->a9, @@ -5719,7 +5751,7 @@ compile_back(nir_shader *nir, struct g9p_shader *out, mesa_shader_stage stage, NIR_PASS(_, nir, nir_lower_tex, &tex); if (out->a9) NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_image_1d, nir_metadata_control_flow, NULL); - if (stage == MESA_SHADER_VERTEX) { + if (stage == MESA_SHADER_VERTEX || (stage == MESA_SHADER_COMPUTE && !out->a9)) { /* after the gradients became levels above */ const nir_lower_tex_options offsets = { .lower_offset_filter = vertex_compare_offset, diff --git a/src/imagination/g9p/g9p_compile.h b/src/imagination/g9p/g9p_compile.h index 891d83c344c..70e7f9328d2 100644 --- a/src/imagination/g9p/g9p_compile.h +++ b/src/imagination/g9p/g9p_compile.h @@ -69,6 +69,12 @@ struct g9p_storage_image { * fragment code does). */ #define G9P_VS_TEX_BASE (2 * G9P_MAX_BUFFERS) +/* + * Compute shaders: the textures' words from dword 96, past the buffers' address slots and the + * thread count after them (2 * G9P_MAX_BUFFERS + 1 dwords), so that 16 textures end at 255, + * the last dword a sample's state and sampler fields reach + */ +#define G9P_CS_TEX_BASE 96 /* * Vertex shaders: the first dwords of each buffer they read at constant offsets, copied into * the common store per draw from g9p_vs_buf_base() (the buffers in slot order, diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 3a884903178..328bafb3711 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -258,6 +258,15 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( memcpy(state->local_size, shader.local_size, sizeof(state->local_size)); state->shared_size = shader.shared_size; state->shared_spill_stride = shader.shared_spill_stride; + state->num_textures = shader.num_textures; + for (unsigned i = 0; i < shader.num_textures; i++) { + state->textures[i].set = shader.textures[i].set; + state->textures[i].binding = shader.textures[i].binding; + state->textures[i].index = shader.textures[i].index; + state->textures[i].sampler_set = shader.textures[i].sampler_set; + state->textures[i].sampler_binding = shader.textures[i].sampler_binding; + state->textures[i].sampler_index = shader.textures[i].sampler_index; + } out: ralloc_free(mem_ctx); @@ -330,8 +339,8 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, const uint32_t count = threads | threads << 16; struct pvr_suballoc_bo *uniforms_bo; uint32_t uniforms[2 * G9P_MAX_BUFFERS + 1] = { 0 }; - uint32_t bind_data[4 * (G9P_MAX_BUFFERS + 1) + 4] = { 0 }; - uint32_t bind_code[G9P_MAX_BUFFERS + 2]; + uint32_t bind_data[4 * (G9P_MAX_BUFFERS + 2) + 4] = { 0 }; + uint32_t bind_code[G9P_MAX_BUFFERS + 3]; uint32_t kernel_data[G9P_KERNEL_DATA_DWORDS] = { 0 }, kernel_code[8]; bool from_uniforms[G9P_MAX_BUFFERS] = { false }; struct pvr_pds_upload bind, kernel; @@ -440,13 +449,39 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, if (result != VK_SUCCESS) return result; + /* + * The textures' states and sampler words, as the shader finds them from + * G9P_CS_TEX_BASE (g9p_tex_state(), g9p_tex_sampler()) + */ + const unsigned nt = state->num_textures; + uint64_t tex_src = 0; + + if (nt) { + uint32_t block[10 * G9P_MAX_TEXTURES] = { 0 }; + struct pvr_suballoc_bo *tex_bo; + + for (unsigned t = 0; t < nt; t++) + PVR_PER_ARCH(g9p_texture_words)(desc, state->textures[t].set, state->textures[t].binding, + state->textures[t].index, state->textures[t].sampler_set, + state->textures[t].sampler_binding, + state->textures[t].sampler_index, + &block[g9p_tex_state(0, t)], &block[g9p_tex_sampler(0, nt, t)]); + result = pvr_arch_cmd_buffer_upload_general(cmd_buffer, block, 40 * nt, &tex_bo); + if (result != VK_SUCCESS) + return result; + tex_src = tex_bo->dev_addr.addr; + } + /* * Binding task data: the source addresses (64-bit each), then one DOUTD * control word per transfer: Rogue DOUTD src1, last (bit 31), common - * store (bit 28), destination dword << 13, dwords. + * store (bit 28), destination dword << 13, dwords. The textures' block, + * if any, is the last transfer. */ - ctrl0 = 2 * (n + 1); - for (unsigned k = 0; k <= n; k++) { + const unsigned transfers = n + 1 + !!nt; + + ctrl0 = 2 * transfers; + for (unsigned k = 0; k < transfers; k++) { uint64_t src; uint32_t dest = 2 * k, dwords = 2; @@ -465,6 +500,10 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, /* pvr_buffer_descriptor: the address comes first, the size at byte 8 */ src = set->dev_addr.addr + binding->offset + element * binding->stride + (state->buffers[k].binding & G9P_BINDING_SIZE ? 8 : 0); + } else if (k > n) { + src = tex_src; + dest = G9P_CS_TEX_BASE; + dwords = 10 * nt; } else if (k < n && indirect && state->buffers[k].set == G9P_NUM_WORKGROUPS) { /* an indirect dispatch's x and y, or z, straight from its arguments */ src = indirect + (state->buffers[k].binding ? 8 : 0); @@ -476,12 +515,12 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, bind_data[2 * k] = src; bind_data[2 * k + 1] = src >> 32; bind_data[ctrl0 + k] = - (k == n ? BITFIELD_BIT(31) : 0) | BITFIELD_BIT(28) | dest << 13 | dwords; + (k == transfers - 1 ? BITFIELD_BIT(31) : 0) | BITFIELD_BIT(28) | dest << 13 | dwords; bind_code[nc++] = g9p_pds_dout(G9P_DOUTD, k, G9P_PDS_DATA, ctrl0 + k, - k == n ? G9P_PDS_END : 0); + k == transfers - 1 ? G9P_PDS_END : 0); } bind_code[nc++] = g9p_pds_halt(); - bind_dwords = ALIGN_POT(ctrl0 + n + 1, 4); + bind_dwords = ALIGN_POT(ctrl0 + transfers, 4); result = pvr_arch_cmd_buffer_upload_pds(cmd_buffer, bind_data, bind_dwords, ROGUE_CDMCTRL_KERNEL1_DATA_ADDR_ALIGNMENT, @@ -533,10 +572,10 @@ PVR_PER_ARCH(g9p_cmd_dispatch)(struct pvr_cmd_buffer *cmd_buffer, const uint32_t tg_alloc = MAX2(DIV_ROUND_UP(G9P_SHARED_BASE + state->shared_size, 16), 1); { struct pvr_csb *csb = &sub_cmd->control_stream; - /* the buffers' addresses and the thread count after them */ - const struct g9p_cdm_record bind_rec = - g9p_cdm_dma_task(G9P_CDM_DATA_BLOCKS(bind_dwords / 4), bind.data_offset, - bind.code_offset, 2 * n + 1); + /* the buffers' addresses, the thread count after them and the textures' words */ + const struct g9p_cdm_record bind_rec = g9p_cdm_dma_task( + G9P_CDM_DATA_BLOCKS(bind_dwords / 4), bind.data_offset, bind.code_offset, + nt ? g9p_tex_sampler(G9P_CS_TEX_BASE, nt, nt) : 2 * n + 1); const struct g9p_cdm_record kernel_rec = g9p_cdm_kernel( G9P_CDM_KERNEL_REGS(state->bank1_regs ? state->bank1_regs : state->num_regs) | G9P_CDM_ANY_CLUSTER | G9P_CDM_FENCE | diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index 203fa91cad3..6325b8eed0a 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -1801,6 +1801,34 @@ tex_sampler_at(const struct pvr_descriptor_state *desc, unsigned set_idx, unsign return set; } +/* + * A texture slot's words for a common store: the image's state and its sampler word, from the + * bound descriptor sets (as the vertex shader's block takes them); zero when unbound. Compute + * dispatches copy them to G9P_CS_TEX_BASE. + */ +void +PVR_PER_ARCH(g9p_texture_words)(const struct pvr_descriptor_state *desc, unsigned set_idx, + unsigned binding, unsigned index, int sampler_set, + int sampler_binding, unsigned sampler_index, uint32_t state[4], + uint32_t sampler[2]) +{ + const struct pvr_descriptor_set *set = desc->sets[set_idx]; + const struct pvr_descriptor_set *ss; + unsigned so = 0; + + memset(state, 0, 16); + memset(sampler, 0, 8); + if (set && set->mapping) { + const struct pvr_descriptor_set_layout_binding *b = &set->layout->bindings[binding]; + + memcpy(state, (const uint8_t *)set->mapping + b->offset + index * b->stride, 16); + } + ss = tex_sampler_at(desc, set_idx, binding, index, sampler_set, sampler_binding, sampler_index, + &so); + if (ss && ss->mapping) + memcpy(sampler, (const uint8_t *)ss->mapping + so, 8); +} + #define TEX_SAMPLER_AT(desc, tex, offset) \ tex_sampler_at(desc, (tex).set, (tex).binding, (tex).index, (tex).sampler_set, \ (tex).sampler_binding, (tex).sampler_index, offset) diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index 2f84780a3e9..7bdebb917d7 100644 --- a/src/imagination/vulkan/pvr_g9p.h +++ b/src/imagination/vulkan/pvr_g9p.h @@ -226,6 +226,11 @@ struct g9p_storage_image; void PVR_PER_ARCH(g9p_storage_image_record)(const struct pvr_image_view *iview, struct g9p_storage_image *rec); uint64_t PVR_PER_ARCH(g9p_pack_sampler)(const VkSamplerCreateInfo *info); +struct pvr_descriptor_state; +void PVR_PER_ARCH(g9p_texture_words)(const struct pvr_descriptor_state *desc, unsigned set_idx, + unsigned binding, unsigned index, int sampler_set, + int sampler_binding, unsigned sampler_index, + uint32_t state[4], uint32_t sampler[2]); struct pvr_sub_cmd_transfer; struct vk_sync; diff --git a/src/imagination/vulkan/pvr_pipeline.h b/src/imagination/vulkan/pvr_pipeline.h index f97264bcfc4..cc825ac0ec9 100644 --- a/src/imagination/vulkan/pvr_pipeline.h +++ b/src/imagination/vulkan/pvr_pipeline.h @@ -132,6 +132,13 @@ struct pvr_g9p_compute_state { bool uses_workgroup_id; unsigned local_size[3]; PVR_G5P_COMPUTE_STATE_FIELDS unsigned shared_size; + /* combined image samplers, their words in the common store from G9P_CS_TEX_BASE */ + unsigned num_textures; + struct { + unsigned set, binding, index; + int sampler_set, sampler_binding; /* a separate sampler, binding -1 if none */ + unsigned sampler_index; + } textures[16]; /* G9P_MAX_TEXTURES */ }; /* Apple G9P (has_apple_g9p): a graphics pipeline's programs (pvr_arch_g9p_gfx.c) */ From 8c9dee860b1f0b98f1a8dc918c158cb24be698fb Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 20:40:33 -0600 Subject: [PATCH 43/44] pvr/g9p: multisample texelFetch in vertex and compute shaders texelFetch of a 2D multisample texture compiled in fragment shaders only. Their form, the pixel and the sample index in r0-r2, read wrong values in a vertex shader. Vertex and compute shaders now take the vertex form of the sample, as their other samples do, with the multisample dimension and integer coordinates: the pixel in two registers and the sample index in the next. The level operand is not read there, and a level of the sample index read the same values. These fetches no longer reserve r0-r3 outside fragment shaders. eglmsfetch passes at 2, 4 and 8 samples, RGBA8 and R32UI, with every sample of every pixel read back by fragment, vertex and compute shaders. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_compile.c | 54 +++++++++++++++++++++++++++++-- src/imagination/g9p/g9p_isa.h | 4 ++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/imagination/g9p/g9p_compile.c b/src/imagination/g9p/g9p_compile.c index d9aa35a684c..6b2d6226301 100644 --- a/src/imagination/g9p/g9p_compile.c +++ b/src/imagination/g9p/g9p_compile.c @@ -2927,10 +2927,13 @@ sample_lod(struct g9p_ctx *ctx, nir_tex_instr *tex, unsigned at, int lod) static bool uses_layered_tex(nir_function_impl *impl) { + const bool fragment = impl->function->shader->info.stage == MESA_SHADER_FRAGMENT; + nir_foreach_block(block, impl) { nir_foreach_instr(instr, block) { if (instr->type == nir_instr_type_tex && - (nir_instr_as_tex(instr)->is_array || nir_instr_as_tex(instr)->op == nir_texop_txf_ms || + (nir_instr_as_tex(instr)->is_array || + (fragment && nir_instr_as_tex(instr)->op == nir_texop_txf_ms) || nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_CUBE || nir_instr_as_tex(instr)->sampler_dim == GLSL_SAMPLER_DIM_3D)) return true; @@ -3179,10 +3182,55 @@ emit_txf_ms(struct g9p_ctx *ctx, nir_tex_instr *tex) unsigned at; int dst; - if (ctx->stage != MESA_SHADER_FRAGMENT || ci < 0 || slot < 0 || tex->is_array) { - fprintf(stderr, "g9p: multisample fetches of 2D textures in fragment shaders only\n"); + if (ci < 0 || slot < 0 || tex->is_array) { + fprintf(stderr, "g9p: multisample fetches of 2D textures only\n"); return false; } + /* + * Vertex and compute shaders: the vertex form (g9p_sample_vertex()) at level 0, with + * dimension 4 and integer coordinates as above, the pixel in r[coord]-r[coord + 1] and + * the sample index in r[coord + 2]. Measured: the level operand is not read for these, + * a level of the sample index read the same values. + */ + if (ctx->stage != MESA_SHADER_FRAGMENT) { + int coord, lod; + + coord = alloc_low_range(ctx, 4); + if (coord < 0) + return false; + ctx->temps |= BITFIELD64_RANGE(coord, 4); + for (unsigned c = 0; c < 3; c++) { + const int r = c < 2 ? src_reg(ctx, nir_get_scalar(tex->src[ci].src.ssa, c)) + : si >= 0 ? src_reg(ctx, nir_get_scalar(tex->src[si].src.ssa, 0)) + : const_reg(ctx, 0); + + if (r < 0) + return false; + g9p_alu2(ctx->c, G9P_OR, coord + c, r, r, false); + } + lod = const_reg(ctx, 0); + lod = lod < 0 ? lod : low_reg(ctx, lod); + if (lod < 0) + return false; + dst = alloc_range(ctx, 4); + if (dst < 0) + return false; + at = ctx->c->n; + g9p_sample_vertex(ctx->c, dst, tex_state_dword(ctx, slot), tex_sampler_dword(ctx, slot), + coord, lod); + (*g9p_code_word(ctx->c, at)) = ((*g9p_code_word(ctx->c, at)) & ~0x0f00) | G9P_TEX_2D_MS << 8; + (*g9p_code_word(ctx->c, at + 1)) |= 0x40; + g9p_wait(ctx->c); + release(ctx, &tex->instr); + ctx->temps &= ~BITFIELD64_RANGE(coord, 4); + ctx->busy &= ~BITFIELD64_RANGE(coord, 4); + for (unsigned c = 0; c < 4; c++) { + ctx->reg[key(&tex->def, c)] = dst + c; + if (c >= tex->def.num_components || !ctx->last_use[key(&tex->def, c)]) + ctx->busy &= ~BITFIELD64_BIT(dst + c); + } + return true; + } for (unsigned c = 0; c < 3; c++) { const int r = c < 2 ? src_reg(ctx, nir_get_scalar(tex->src[ci].src.ssa, c)) : si >= 0 ? src_reg(ctx, nir_get_scalar(tex->src[si].src.ssa, 0)) diff --git a/src/imagination/g9p/g9p_isa.h b/src/imagination/g9p/g9p_isa.h index 9c39840acc1..d6f01da6bd1 100644 --- a/src/imagination/g9p/g9p_isa.h +++ b/src/imagination/g9p/g9p_isa.h @@ -976,8 +976,10 @@ g9p_end_fragment(struct g9p_code *c, unsigned words) /* * Texture dimensions of the sample instruction: W0 bits 8-11 (2D 5, as - * g9p_sample_regs()'s f51a; 2D array 7, cube d). From the A9 (PR #5). + * g9p_sample_regs()'s f51a; 2D array 7, cube d). From the A9 (PR #5). A 2D + * multisample fetch takes 9 (the f91a of emit_txf_ms()). */ +#define G9P_TEX_2D_MS 0x9 #define G9P_TEX_2D_ARRAY 0x7 #define G9P_TEX_3D 0xb #define G9P_TEX_CUBE 0xd From 796cd04f073ba6d014ae8866ef99aea5ed9a9818 Mon Sep 17 00:00:00 2001 From: Paul Scalise Date: Thu, 3 Dec 2026 20:54:21 -0600 Subject: [PATCH 44/44] pvr/g9p: workgroups of 512 invocations The device offered 256 invocations per workgroup, as an earlier measurement had 512 hang the GPU even at 10 registers. With the workgroup launch and barriers of today's driver they run. At 512, 16 tasks of 32, each task now keeps at most 16 registers in bank 1 and the rest in bank 0, as larger workgroups already did past their bank-1 share. Measured with 64 workgroups: 16 registers ran at 512 invocations, also with values in bank 0 up to r95. 8 registers did not leave room for the barrier's atomics. The device reports 512 invocations and 512 for the x and y sizes. eglcompute passes at 512 and 384 invocations, ten runs in a row without a GPU reset: IDs over 1D, 2D and 3D workgroups, a sum through shared memory with barriers, shared atomics, compare-and-swap from odd invocations only, and 16 to 48 values live through a loop. 72 values (96 registers) ran too. The run-tests modes with every value in the extended range pass as well. Generated-by: LLM Signed-off-by: Paul Scalise --- src/imagination/g9p/g9p_cdm.h | 8 ++++---- src/imagination/vulkan/pvr_arch_g9p.c | 7 ++++--- src/imagination/vulkan/pvr_physical_device.c | 12 ++++++++---- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/imagination/g9p/g9p_cdm.h b/src/imagination/g9p/g9p_cdm.h index 86b9f34422a..ed57ba0b7bd 100644 --- a/src/imagination/g9p/g9p_cdm.h +++ b/src/imagination/g9p/g9p_cdm.h @@ -179,15 +179,15 @@ static inline uint32_t g9p_compute_hw_threads(uint32_t threads) /* * The most tasks of 32 a workgroup of programs of @regs registers may have. * Measured on the A10 with 64 workgroups of whole tasks, registers in eights: - * 24 ran at 256 invocations, 40 at 160 (192 hung one run in two), 48 and 64 at - * 128 (48 hung at 160), 72 to 96 at 64 (72 hung at 96). 32 is not measured - * and keeps the 192 invocations the earlier rule allowed. + * 16 ran at 512 invocations, 24 at 256, 40 at 160 (192 hung one run in two), + * 48 and 64 at 128 (48 hung at 160), 72 to 96 at 64 (72 hung at 96). 32 is + * not measured and keeps the 192 invocations the earlier rule allowed. */ static inline unsigned g9p_compute_max_tasks(unsigned regs) { const unsigned r = (regs + 7) & ~7u; - return r <= 24 ? 8 : r <= 32 ? 6 : r <= 40 ? 5 : r <= 64 ? 4 : r <= 96 ? 2 : 1; + return r <= 16 ? 16 : r <= 24 ? 8 : r <= 32 ? 6 : r <= 40 ? 5 : r <= 64 ? 4 : r <= 96 ? 2 : 1; } /* --- Internal programs and the end of a compute pass --------------------- */ diff --git a/src/imagination/vulkan/pvr_arch_g9p.c b/src/imagination/vulkan/pvr_arch_g9p.c index 328bafb3711..ed10b49500d 100644 --- a/src/imagination/vulkan/pvr_arch_g9p.c +++ b/src/imagination/vulkan/pvr_arch_g9p.c @@ -212,8 +212,8 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( /* * A workgroup's tasks are resident together, and too many registers for - * them hang the GPU (g9p_compute_max_tasks()). 512 invocations hang even at - * 10 registers: the device limit is 256. + * them hang the GPU (g9p_compute_max_tasks()). 512 invocations, the device + * limit, take at most 16 in bank 1. */ const uint32_t threads = shader.local_size[0] * shader.local_size[1] * shader.local_size[2]; @@ -225,7 +225,8 @@ PVR_PER_ARCH(g9p_compute_pipeline_compile)( state->bank1_regs = 0; if (DIV_ROUND_UP(threads, 32) > g9p_compute_max_tasks(shader.num_regs) && !getenv("G9P_NO_BANK0")) { const unsigned tasks = DIV_ROUND_UP(threads, 32); - const unsigned bank1 = tasks <= 2 ? 96 : tasks <= 4 ? 64 : tasks <= 5 ? 40 : tasks <= 6 ? 32 : 24; + const unsigned bank1 = tasks <= 2 ? 96 : tasks <= 4 ? 64 : tasks <= 5 ? 40 : tasks <= 6 ? 32 : + tasks <= 8 ? 24 : 16; struct g9p_shader capped; if (g9p_compile_compute_bank0(again, &capped, mem_ctx, bank1) && diff --git a/src/imagination/vulkan/pvr_physical_device.c b/src/imagination/vulkan/pvr_physical_device.c index 842dc09b37f..e4310018354 100644 --- a/src/imagination/vulkan/pvr_physical_device.c +++ b/src/imagination/vulkan/pvr_physical_device.c @@ -764,12 +764,16 @@ static bool pvr_physical_device_get_properties( /* G5P: 256 (AGXMetalA9: 512 per threadgroup, maxThreadsPerThreadgroup; Rogue's CDM: 32 lanes x 16 * tasks per USC). Barriers above 32 lanes are Apple's software barrier (g5p_barrier.h); a pipeline whose * temporaries cannot keep every task of a barriered workgroup resident is refused at creation - * (pvr_arch_g5p_compute.c). */ + * (pvr_arch_g5p_compute.c). G9P: 512, 16 tasks with 16 registers each in bank 1 and the rest + * in bank 0 (pvr_arch_g9p.c). */ .maxComputeWorkGroupInvocations = - PVR_HAS_FEATURE(dev_info, apple_g5p) || PVR_HAS_FEATURE(dev_info, apple_g9p) ? 256U : 128U, + PVR_HAS_FEATURE(dev_info, apple_g9p) ? 512U : + PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, .maxComputeWorkGroupSize = { - [0] = PVR_HAS_FEATURE(dev_info, apple_g5p) || PVR_HAS_FEATURE(dev_info, apple_g9p) ? 256U : 128U, - [1] = PVR_HAS_FEATURE(dev_info, apple_g5p) || PVR_HAS_FEATURE(dev_info, apple_g9p) ? 256U : 128U, + [0] = PVR_HAS_FEATURE(dev_info, apple_g9p) ? 512U : + PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, + [1] = PVR_HAS_FEATURE(dev_info, apple_g9p) ? 512U : + PVR_HAS_FEATURE(dev_info, apple_g5p) ? 256U : 128U, [2] = 64U, },