From 5339acdb7c7af4c741b453b4e0f0e1516aa4dffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Thu, 3 Dec 2026 01:29:13 +0100 Subject: [PATCH 1/3] pvr/g5p: mark buffers written by transform feedback, query copies and A7 transfers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit g9p_gpu_written tells the realign copy of an unaligned vertex binding (g5p_realign_binding()) whether it must wait for the jobs before it. On G5P only the storage and texel buffer descriptors set it, so the realign check also tested the buffer's usage flags to stay safe. Set it on every other path where the GPU writes an app buffer on G5P: - transform feedback buffers and counter buffers, as the G9P bind does; - the destination of vkCmdCopyQueryPoolResults, which G5P runs as a compute kernel (pvr_arch_g5p_query.c), not on the CPU; - the destination of the A7's buffer copies, fills, updates and image to buffer copies, which run as compute jobs (pvr_arch_g3_compute.c). By default (PVR_G3_GFX=tri) the A7 draws through upstream's Rogue path; it reaches g5p_cmd_draw() and the realign copy when PVR_G3_GFX names another path (e.g. ta). The A8/A9 transfers run on the CPU at submission, in queue order, after waiting for every earlier job, so they need no flag. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- src/imagination/vulkan/pvr_arch_g5p_query.c | 3 +++ src/imagination/vulkan/pvr_arch_g5p_xfb.c | 3 +++ src/imagination/vulkan/pvr_buffer.h | 3 ++- src/imagination/vulkan/rogue/pvr_blit.c | 4 ++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/imagination/vulkan/pvr_arch_g5p_query.c b/src/imagination/vulkan/pvr_arch_g5p_query.c index b08ebf26c2c..32c88b3658f 100644 --- a/src/imagination/vulkan/pvr_arch_g5p_query.c +++ b/src/imagination/vulkan/pvr_arch_g5p_query.c @@ -552,6 +552,9 @@ PVR_PER_ARCH(g5p_add_query_program)(struct pvr_cmd_buffer *cmd_buffer, params.count = query_info->copy_query_results.query_count; params.dest_stride = query_info->copy_query_results.stride; params.flags = query_info->copy_query_results.flags; + /* the copy is a GPU kernel: a later realigned vertex binding of + * this buffer must wait for it (g5p_realign_binding()) */ + dst->g9p_gpu_written = true; buffers[0] = dst->dev_addr.addr + query_info->copy_query_results.dst_offset; buffers[1] = diff --git a/src/imagination/vulkan/pvr_arch_g5p_xfb.c b/src/imagination/vulkan/pvr_arch_g5p_xfb.c index 0a4efa7579b..f5979e65191 100644 --- a/src/imagination/vulkan/pvr_arch_g5p_xfb.c +++ b/src/imagination/vulkan/pvr_arch_g5p_xfb.c @@ -116,6 +116,8 @@ PVR_PER_ARCH(g5p_xfb_bind)(struct pvr_cmd_buffer *cmd_buffer, uint32_t first, ui const VkDeviceSize off = offsets[i]; const VkDeviceSize size = !sizes || sizes[i] == VK_WHOLE_SIZE ? buffer->vk.size - off : sizes[i]; st->bind[first + i] = (struct g5p_xfb_range){ buffer, off, size }; + if (buffer) + buffer->g9p_gpu_written = true; } } @@ -128,6 +130,7 @@ g5p_xfb_counters(struct g5p_xfb_range *out, uint32_t first, uint32_t count, cons continue; VK_FROM_HANDLE(pvr_buffer, buffer, counters[i]); out[first + i] = (struct g5p_xfb_range){ buffer, offsets ? offsets[i] : 0, 4 }; + buffer->g9p_gpu_written = true; } } diff --git a/src/imagination/vulkan/pvr_buffer.h b/src/imagination/vulkan/pvr_buffer.h index da4d8958fb6..01916d4b52c 100644 --- a/src/imagination/vulkan/pvr_buffer.h +++ b/src/imagination/vulkan/pvr_buffer.h @@ -33,7 +33,8 @@ struct pvr_buffer { struct pvr_winsys_vma *vma; /* Device address the buffer is mapped to in device virtual address space */ pvr_dev_addr_t dev_addr; - /* G9P: bound where the GPU can write it (storage descriptor, transform feedback) */ + /* G9P, G5P: bound where the GPU can write it (storage descriptor, transform + * feedback; on G5P also a query copy and the A7's compute transfers) */ bool g9p_gpu_written; }; diff --git a/src/imagination/vulkan/rogue/pvr_blit.c b/src/imagination/vulkan/rogue/pvr_blit.c index 23564c50ff2..776f59daa17 100644 --- a/src/imagination/vulkan/rogue/pvr_blit.c +++ b/src/imagination/vulkan/rogue/pvr_blit.c @@ -1407,6 +1407,7 @@ void pvr_rogue_CmdCopyImageToBuffer2( /* the A7: the compute copy kernel (pvr_arch_g3_compute.c) */ if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g3)) { + dst->g9p_gpu_written = true; PVR_PER_ARCH(g3_copy_buffer_image)(cmd_buffer, dst, src, false, pCopyImageToBufferInfo->regionCount, pCopyImageToBufferInfo->pRegions); return; @@ -1713,6 +1714,7 @@ void pvr_rogue_CmdUpdateBuffer(VkCommandBuffer commandBuffer, if (result != VK_SUCCESS) return; if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g3)) { + dst->g9p_gpu_written = true; PVR_PER_ARCH(g3_copy_buffer)(cmd_buffer, pvr_bo->dev_addr.addr, dst->dev_addr.addr + dstOffset, dataSize); return; } @@ -1738,6 +1740,7 @@ void pvr_rogue_CmdCopyBuffer2(VkCommandBuffer commandBuffer, /* the A7: the compute copy kernel (pvr_arch_g3_compute.c) */ if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g3)) { + dst->g9p_gpu_written = true; for (uint32_t i = 0; i < pCopyBufferInfo->regionCount; i++) PVR_PER_ARCH(g3_copy_buffer)(cmd_buffer, src->dev_addr.addr + pCopyBufferInfo->pRegions[i].srcOffset, dst->dev_addr.addr + pCopyBufferInfo->pRegions[i].dstOffset, @@ -1790,6 +1793,7 @@ void pvr_rogue_CmdFillBuffer(VkCommandBuffer commandBuffer, */ fillSize &= ~3ULL; if (PVR_HAS_FEATURE(&cmd_buffer->device->pdevice->dev_info, apple_g3)) { + dst->g9p_gpu_written = true; PVR_PER_ARCH(g3_fill_buffer)(cmd_buffer, dst->dev_addr.addr + dstOffset, fillSize, data); return; } From f13c4a8c674202aeb848fbfb5519f0d1f28dfa15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Thu, 3 Dec 2026 01:29:41 +0100 Subject: [PATCH 2/3] pvr/g5p: wait before a vertex realign copy only for GPU-written sources MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit g5p_realign_binding() treated a source as GPU-written when its buffer had storage, storage texel or transform feedback usage. Zink creates every buffer with VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, so every realigned binding under Zink counted as GPU-written, even CPU-filled vertex and instance data. The submission of each such render then waited on the CPU for the GPU queue to drain (pvr_arch_queue.c, "wait realign"). Firefox's WebRender on YouTube's home page did this about 18 times per frame. Every path where the G5P GPU writes an app buffer now sets g9p_gpu_written, so test that flag alone, as G9P does. Under G9P_WAITLOG, also log each realigned binding: why it is copied (unaligned address or stride, or a fetch overread past the mapping), its address, stride, size and whether the copy waits. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- src/imagination/vulkan/pvr_arch_g5p_gfx.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g5p_gfx.c b/src/imagination/vulkan/pvr_arch_g5p_gfx.c index 6fe858b5cc7..39537f69910 100644 --- a/src/imagination/vulkan/pvr_arch_g5p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g5p_gfx.c @@ -2312,9 +2312,10 @@ g5p_record_ds_usage(struct pvr_sub_cmd_gfx *sub, const struct vk_dynamic_graphic * of 4 draws from a copy whose elements start on dwords: new stride = align(max(stride, 16), 4), room for the * widest attribute past a small stride (GL's byte vec4 of stride 2). The bytes inside an element keep their * offsets; an attribute at an unaligned offset is shifted out in the vertex program (pvr_g5p_vertex_nir.c). - * The copy runs when the job is submitted (pvr_arch_queue.c drains sub_cmd->gfx.g9p_realigns): a transfer - * recorded before the draw (they run on the CPU at submission) may be what fills the source; a source the GPU - * can write waits for the jobs before it. */ + * The copy runs when the job is submitted (pvr_arch_queue.c drains sub_cmd->gfx.g9p_realigns): a source the GPU + * wrote (storage, texel or transform-feedback binding, a query copy, an A7 compute transfer: g9p_gpu_written) + * waits for the jobs before it; CPU-written sources and A8/A9 transfer destinations (transfers run on the CPU at + * submission, in order) are copied without a wait. */ static uint32_t g5p_realigned_stride(uint32_t stride) { @@ -2344,7 +2345,7 @@ g5p_cpu_addr(struct pvr_device *device, uint64_t addr, uint64_t *available) static VkResult g5p_realign_binding(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_vertex_binding *vb, uint32_t stride, - uint32_t extra, uint64_t *addr) + uint32_t extra, bool unaligned, uint64_t *addr) { uint64_t available; const uint8_t *src = g5p_cpu_addr(cmd_buffer->device, vb->buffer->dev_addr.addr + vb->offset, &available); @@ -2379,10 +2380,7 @@ g5p_realign_binding(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_vertex_b const struct g9p_realign r = { .src = src, .dst = pvr_bo_suballoc_get_map_addr(bo), .size = size, .count = count, .stride = stride, .new_stride = new_stride, .dst_addr = bo->dev_addr.addr, .extra = extra, - .gpu_written = vb->buffer->g9p_gpu_written || - (vb->buffer->vk.usage & (VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | - VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT | - VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT)), + .gpu_written = vb->buffer->g9p_gpu_written, }; /* a secondary's render joins its primary's job: copied now */ if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) { @@ -2391,6 +2389,10 @@ g5p_realign_binding(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_vertex_b memcpy(r.dst + i * r.new_stride, r.src + i * r.stride, MIN2(MAX2(r.new_stride, 16), r.size - i * r.stride)); } else { + if (getenv("G9P_WAITLOG")) + fprintf(stderr, "g5p: realign %s addr 0x%" PRIx64 " stride %u size %" PRIu64 " gpu_written %d\n", + unaligned ? "unaligned" : "overread", vb->buffer->dev_addr.addr + vb->offset, stride, size, + r.gpu_written); util_dynarray_append_typed(&cmd_buffer->state.current_sub_cmd->gfx.g9p_realigns, struct g9p_realign, r); } return VK_SUCCESS; @@ -2667,7 +2669,8 @@ PVR_PER_ARCH(g5p_cmd_draw)(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_g uint64_t binding_base = vb->buffer->dev_addr.addr + vb->offset; if (copy[fe->binding]) { if (!realigned[fe->binding]) { - result = g5p_realign_binding(cmd_buffer, vb, stride, extent[fe->binding], &realigned[fe->binding]); + result = g5p_realign_binding(cmd_buffer, vb, stride, extent[fe->binding], + ((binding_base | stride) & 3) != 0, &realigned[fe->binding]); if (result != VK_SUCCESS) return result; } From 10b7528737eb9bca0c90eb5e1d130bd11e7240fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Thu, 3 Dec 2026 01:40:26 +0100 Subject: [PATCH 3/3] pvr: read a realigned vertex source's GPU-written flag again at submission MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The realign copy of an unaligned vertex binding runs on the CPU when its job is submitted, and waits for the jobs before it only when the source buffer may have been written by the GPU (g9p_gpu_written). The flag was sampled when the draw was recorded. A buffer first bound for GPU writes (storage descriptor, transform feedback, ...) after that, in a command buffer submitted before the draw's, set the flag too late: the copy skipped the wait and could read the buffer before the writer finished. G5P's former usage-flag test covered this case; G9P never did. Keep the source buffer in struct g9p_realign and test its flag again in g9p_realign_needs_wait(), on G9P and G5P. The pointer is valid there: destroying a buffer a command buffer uses makes that command buffer invalid, an invalid command buffer cannot be submitted, and a pending one keeps its buffers alive until it completes. The realign copy already reads the buffer's memory at the same point. Known limits, unchanged: - memory aliasing: another VkBuffer or a linear image bound to the same memory as a GPU-written buffer does not set this buffer's flag; - a secondary command buffer copies its realigned bindings when it is recorded, without waiting for any job. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- src/imagination/vulkan/pvr_arch_g5p_gfx.c | 7 ++++--- src/imagination/vulkan/pvr_arch_g9p_gfx.c | 8 ++++++-- src/imagination/vulkan/pvr_g9p.h | 7 ++++++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/imagination/vulkan/pvr_arch_g5p_gfx.c b/src/imagination/vulkan/pvr_arch_g5p_gfx.c index 39537f69910..6f04da86f7b 100644 --- a/src/imagination/vulkan/pvr_arch_g5p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g5p_gfx.c @@ -2314,8 +2314,9 @@ g5p_record_ds_usage(struct pvr_sub_cmd_gfx *sub, const struct vk_dynamic_graphic * offsets; an attribute at an unaligned offset is shifted out in the vertex program (pvr_g5p_vertex_nir.c). * The copy runs when the job is submitted (pvr_arch_queue.c drains sub_cmd->gfx.g9p_realigns): a source the GPU * wrote (storage, texel or transform-feedback binding, a query copy, an A7 compute transfer: g9p_gpu_written) - * waits for the jobs before it; CPU-written sources and A8/A9 transfer destinations (transfers run on the CPU at - * submission, in order) are copied without a wait. */ + * waits for the jobs before it (the flag is read again at submission, g9p_realign_needs_wait()); CPU-written + * sources and A8/A9 transfer destinations (transfers run on the CPU at submission, in order) are copied without + * a wait. */ static uint32_t g5p_realigned_stride(uint32_t stride) { @@ -2380,7 +2381,7 @@ g5p_realign_binding(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_vertex_b const struct g9p_realign r = { .src = src, .dst = pvr_bo_suballoc_get_map_addr(bo), .size = size, .count = count, .stride = stride, .new_stride = new_stride, .dst_addr = bo->dev_addr.addr, .extra = extra, - .gpu_written = vb->buffer->g9p_gpu_written, + .gpu_written = vb->buffer->g9p_gpu_written, .buffer = vb->buffer, }; /* a secondary's render joins its primary's job: copied now */ if (cmd_buffer->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) { diff --git a/src/imagination/vulkan/pvr_arch_g9p_gfx.c b/src/imagination/vulkan/pvr_arch_g9p_gfx.c index e9856ff4cc6..9ba4f7d41f2 100644 --- a/src/imagination/vulkan/pvr_arch_g9p_gfx.c +++ b/src/imagination/vulkan/pvr_arch_g9p_gfx.c @@ -1715,13 +1715,16 @@ PVR_PER_ARCH(g9p_realign_vertex_bindings)(struct pvr_sub_cmd_gfx *sub_cmd) /* * Whether the copies must wait for the jobs before: host writes are visible at * submission and buffer transfers run on the CPU in order before this job, so - * only a source the GPU itself can write needs it. + * only a source the GPU itself can write needs it. The buffer's flag is read + * here, at submission, not only when the draw was recorded: a storage or + * transform feedback binding recorded after the draw, in a command buffer + * submitted before this one, sets it in between. */ bool PVR_PER_ARCH(g9p_realign_needs_wait)(const struct pvr_sub_cmd_gfx *sub_cmd) { util_dynarray_foreach (&sub_cmd->g9p_realigns, struct g9p_realign, r) { - if (r->gpu_written) + if (r->gpu_written || (r->buffer && r->buffer->g9p_gpu_written)) return true; } return false; @@ -1764,6 +1767,7 @@ realign_vertex_binding(struct pvr_cmd_buffer *cmd_buffer, const struct pvr_verte .stride = stride, .new_stride = new_stride, .gpu_written = vb->buffer->g9p_gpu_written || getenv("G9P_REALIGN_WAIT"), + .buffer = vb->buffer, }; /* a secondary's render joins its primary's job: no list of its own there */ diff --git a/src/imagination/vulkan/pvr_g9p.h b/src/imagination/vulkan/pvr_g9p.h index 7bfebf3e624..8423d0a9375 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; @@ -31,7 +32,11 @@ struct g9p_realign { uint8_t *dst; uint64_t size, count; uint32_t stride, new_stride; - bool gpu_written; /* the source may be written by an earlier GPU job */ + bool gpu_written; /* the source may be written by an earlier GPU job, known when the draw was recorded */ + /* the source's buffer: its g9p_gpu_written is read again at submission (g9p_realign_needs_wait()), as a + * GPU writer recorded after the draw may be submitted before it. A command buffer is only submitted while + * every buffer it uses is alive (destroying one makes it invalid), so the pointer is valid there. */ + const struct pvr_buffer *buffer; uint64_t dst_addr; /* the copy's device address (G5P reuses a copy across the sub-command's draws) */ uint32_t extra; /* G5P: zero bytes after the copy's last element, for its fetches' overread */ };