fix(shader): count folded draw offsets once in the vertex and instance indices #2285
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2285"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-09T14:33:18Z.
What
An embedded vertex fetch adds the base vertex or start instance user SGPR to the index VGPR (
v_add_co_u32 v8, vcc, s19, v8), and the driver folds that SGPR into the Vulkan draw's first vertex and first instance so the replaced attribute fetch starts there. v5 and v8 weregl_VertexIndexandgl_InstanceIndex, which already hold that offset, so the translated add counted it twice: index + offset before the add, index + 2 × offset after it. This hits direct draws and indirect records drawn on the CPU path alike; PPSA26344 indexes a per-instance buffer with v8 + s19 at start instances up to 59,455.Driver::FoldDrawOffsetswith the same rules.APS5_INDX_OFFSET_SKIP_FOLDandAPS5_NO_INDEXED_OFFSET_FOLDgo, as skipping the fold would now leave the indices short.si_emit_draw_packets, RADVradv_cs_emit_indirect_draw_packet).EmbeddedVertexFetchAnalyzernow counts the register as shared unless the fetch add is the only reader of the index VGPR and the SGPR (no earlier read, carry, clamp or second add); such records take the CPU path, where both values are exact.foldlambda, which keeps its signature.Tested
maind70b8998: configure-DBUILD_TESTING=ON, full build,--target libs,ctest:ctest --output-on-failure)maind70b8998: 517/517 passagc_driver_draw_offset_indices: a vertex program stores v5/v8 before and after its fetch adds for direct draws, records the now publicClassifyIndirectDrawsends to the CPU path, one drawn in place and one whose packet names no register; it also checks which programs may stay in place. With main's translator it fails (at start instance 40, v8 reads 40, not 0, and 80 after the add; the 59,455 record gives 118,910), and without the analyzer change a record whose program copies v5/v8 first stays in place.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T14:38:03Z.
One remaining semantics question from the full diff:
DrawOffsetIndicespassescheckRaw=falsefor the in-place GPU indirect case, so it verifies only the values after the embedded add.drawIndexsubtracts the SGPR in the shader prologue for every draw using that embedded-fetch program, but this path keeps the indirect record in place and does not fold that SGPR into the Vulkan draw parameters. That makes the initialv5/v8observably different from the previous translation; the new TechnicalDebt entry records this. Could you provide a source or hardware basis that guest code cannot rely on those pre-add values in this path, or preserve them and test both values? The current test establishes the post-add result only.Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-09T16:47:41Z.
You're right that the in-place case only proved the value after the add, so I went back to what the hardware puts in these registers. Neither the old pre-add values nor my first version's were the hardware's: on GFX10 the VertexID/InstanceID VGPRs are zero-based, and the base vertex and start instance reach the shader only through the user SGPRs the draw names, which the CP fills from the record for DRAW_INDIRECT and DRAW_INDEX_INDIRECT (radeonsi and RADV both rely on this, sources below). For the test's in-place record (vertex offset 700, start instance 59,455, stale user data 0x51/0x62) the hardware has v5/v8 = index/instance before the adds; main gives index + 700 / instance + 59,455 there, since gl_VertexIndex/gl_InstanceIndex already contain the record, and my first version gave index + 619 / instance + 59,357.
So I made everything a program can observe match the hardware instead. In an in-place draw the shader's s16 still holds the CPU's word rather than the record's, so the analyzer now keeps a record in place only when the fetch add is the only thing that sees v5/v8 and the SGPR. A program that touches v5/v8 or writes EXEC before the add, adds the SGPR into another VGPR, reads the add's carry, clamps it, adds the SGPR twice, writes the SGPR through a carry or lane mask, or addresses SGPRs relatively counts the register as shared, and its records take the CPU path, where both values are exact. Programs that pass have no pre-add value anything can read, and their post-add value is gl_VertexIndex itself, so the TechnicalDebt entry is gone. There's no request or cache layout change, and nothing moves in Yotei: all 28 of its programs that fold an SGPR also read or rewrite it elsewhere, so their records already took the CPU path.
The test now checks both values on every path that has a pre-add value, including the in-place-shaped record whose program copies v5/v8 (
ClassifyIndirectDrawnow sends it to the CPU path, and the test draws it the way that path does) and a GPU draw whose packet names no register. Does that answer it, or would you rather see the gl_BaseVertex route below?Details
Sources (Mesa main ed458ffa):
si_emit_draw_packets, indirect: DRAW_INDEX_INDIRECT / DRAW_INDIRECT (and the _MULTI forms) name theSI_SGPR_BASE_VERTEXandSI_SGPR_START_INSTANCEregisters. Direct draws writeindex_biasorstartinto the same SGPR (L1655-1697) and DRAW_INDEX_AUTO carries only the count (L1876-1882).radv_cs_emit_indirect_draw_packetpasses the vertex offset / start instance SGPR locations;radv_emit_direct_draw_packetsputsfirstVertexin that SGPR andradv_cs_emit_draw_packetemits DRAW_INDEX_AUTO with the count only.ac_nir_set_optionssetsvertex_id_zero_based = true, so nir_lower_system_values builds VertexIndex asvertex_id_zero_base + first_vertex, and InstanceIndex asinstance_id + base_instance(L202-207). ac_nir_lower_intrinsics_to_args takesfirst_vertex/base_instancefrom the base_vertex / start_instance SGPRs andvertex_id/instance_idfrom the VGPRs (L436-445).declare_shader_argsin radv_shader_args.c (NGG, LS) withdeclare_vs_input_vgprs: an NGG vertex wave has five GS VGPRs, then vertex_id (v5), two user VGPRs, instance_id (v8); an LS wave has two TCS VGPRs, then vertex_id (v2) and instance_id (v5).Values per path (U = the CPU's word in s16, R = the record's offset, i = index; instance analogous; before / after the add):
(i includes GE_INDX_OFFSET wherever the driver already applied it.)
Why not gl_BaseVertex/gl_BaseInstance for the SGPR: that makes the in-place value exact too, but on draws with a GE_INDX_OFFSET firstVertex also carries that offset, so BaseVertex isn't the SGPR's value there. It needs a per-draw value or a shader variant per path, plus
shaderDrawParametersand a request change. Sending the rare program that looks at the raw index to the CPU path costs nothing measurable here and keeps the layout as it is.Test cases (NVIDIA RTX 3080 Laptop and Windows llvmpipe): eight programs check the analyzer (copies v5/v8 first, adds only, reads the instance add's carry, clamped v_sad_u32, s16 added twice, EXEC written first, s17 overwritten by another add's carry, s16 added into v0); direct auto and indexed draws; an indexed and an auto record of the copying program, classified to the CPU path and drawn with both values checked; an indexed record of the adds-only program, classified in place and checked after the add; and an indexed record whose packet names no register, drawn on the GPU with both values checked. With main's translator the test fails as before (start instance 40 gives v8 = 40 and 80); with the analyzer change reverted, the copying program's records are classified to the GPU. The full suite on
fcc5105apasses: 508/508 on Windows, 517/517 in the Linux job merged with maind70b8998.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:15:43Z.
Checked on Linux (build + ctest):
agc_driver_draw_offset_indices,agc_driver_graphics,agc_driver_vertex_fetch,guest_indexed_drawpass. The new test fails with theInstructionTranslator.cppchange reverted and also with theEmbeddedVertexFetch.cppanalyzer change reverted. The Mesavertex_id_zero_basedclaim checks out against the linked sources. Merging.Heads-up: this has adjacency conflicts with #2313 (
Driver.hpp) and #1896 (CMakeLists.txt, and it calls thefoldlambda); whichever lands later needs a rebase.