Repository navigation
pvr/g5p: shader compiler coverage for Dolphin, PPSSPP, Zink GL and WebGPU - #17
Merged
Merged
Conversation
The backend reads a uniform buffer at a register offset only as (index << s) + constant, with a vec4 at s = 4 or a scalar. Any other offset was refused: a struct array whose stride is not a power of two (k*48 + 16), a sum of two dynamic terms (k*64 + j*16), or a shift above 4. Apple's A9 compiler reads a constant buffer at such an offset by computing the element index with integer ALU and then using the same pointer add and load: p[k*3 + j + 2] on a float pointer is imul, iadd, iadd and the pointer add at shift 2 with a 4-byte load, and a float4 read at p[k*3 + 1] is the same at shift 4 with one 16-byte load. Rewrite these loads in NIR to that form. The offset is divided by the element size term by term where it divides exactly (k*12 + j*4 + 8 at s = 2 becomes k*3 + j + 2), else with a shift right. The index is clamped to the last element of the block, as the direct form already is in fragment shaders, so a negative or out-of-range index stays inside the block. A vec4 reads 16 bytes at shift 4 when its offset is a multiple of 16, by its alignment or by construction (naga's vec4 loads carry a 4-byte alignment); otherwise it becomes four scalar loads. Zink reads a GL uniform block as an array of uints, so four scalar loads that make up one 16-byte aligned vec4 are merged back into one vector load first. When the variable does not give the block size (Zink's ubos[n] with n > 1), the load's range bounds the index. Loads the direct form already accepts keep their exact code: which loads are general is decided on the components the backend reads, after it trims unread trailing ones. Assisted-by: LLM
A vertex min/max or subtract of a common-store operand and a register
was refused when the register or the destination was in bank 0 ("no
witnessed mixed-bank form"). Programs with control flow keep their temps
in bank 0, so WebGPU's robustness clamp of a storage-buffer index,
min(index, arrayLength - 1) inside a branch, hit it: a WebGPU game's
vertex shader was refused, its pipeline was never created, and the game
stopped at its first 3D frame.
Apple's offline compiler emits this form with every destination and B
bank pair when its vertex temps are in bank 0 (eight attribute words or
computed values, each clamped by its own uniform, under attribute
pressure; min, max, signed and unsigned, u - x and x - u). The fields are
the ones the register forms already use: the instruction's bank is the
destination's and header bit 0x10 marks B in bank 1. The existing
encoders produce Apple's instructions byte for byte from those fields,
so the change only drops the refusal.
Programs that compiled before keep their bytes: only compiles that were
refused take the new path.
Assisted-by: LLM
Dolphin's Vulkan backend creates R8, R16, RGBA8 and RG32 UINT views of a
uniform texel buffer at startup and does not start a game without them
("G5P refuses: VK_FORMAT_R8_UINT: texel buffers require hardware
qualification"). Zink's GL texture buffers failed after the same
refusal.
The texel-buffer path (a linear 4096-wide 2D state over the buffer, the
NIR fold of the index, the element count in software descriptor words)
gave exact pixels on the A9 for 30 formats, element counts 1 to 65536,
offsets 64 and 128, the size query and out-of-range reads, in fragment
and vertex programs. Turn it on. The fetch instruction now comes from
the general texture encoder. Storage texel buffers and compute
texel-buffer fetches stay refused by name, and so does a compute
textureSize() of a texel buffer, whose uploads do not carry the element
count.
pvr_get_buffer_format_features2() returned before computing any texel
buffer feature, for every architecture, since the G9P change that took
the statement of its scaled-format return. That return is back, so the
G9P buffer features are what they were. The A9's are its vertex formats
and the qualified uniform texel buffers: exactly 30 formats gain
VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT, nothing else changes.
Assisted-by: LLM
Zink draws GL_DEPTH_CLAMP with depthClampEnable and depthClipEnable off
(VK_EXT_depth_clip_enable), and the A9 path refused every such pipeline
("depth clamp or polygon mode 0", the message it shares with the
polygon-mode refusal), so PPSSPP's OpenGL backend drew nothing. A
pipeline with depth clipping off and clamping off was drawn with
clipping, ignoring the advertised extension.
AGXMetalA9's setDepthClipMode: writes PPP_CTRL clip mode 1 for Metal's
clip mode and 2 for its clamp mode, which keeps the geometry beyond the
near and far planes and clamps its depth to [0, 1]. On the A9, Vulkan's
explicit complementary pairs are exact in that mode, and GL depth clamp
is exact except for custom depth ranges, which it clamps to [0, 1].
A draw with depth clipping off now takes mode 2. With clamping off that
is Vulkan's result (depthClampZeroOne's [0, 1], undefined depth outside
the range without it); with clamping on it is Vulkan's clamp when the
viewport's depth range is [0, 1] in either order. A clamp to another
depth range, clamping with clipping on, and depth bias or fragment depth
export with clipping off are refused by name. depthClamp stays
unadvertised because of the custom ranges.
Assisted-by: LLM
Dolphin samples every texture as sampler2DArray, and its EFB copy,
restore, format-conversion and palette programs take the layer from a
varying. A single-level 2D colour image is linear on G5P, and the A9 has
no array state for a linear image: AGXMetalA9's ImageStateEncoderGen2
encodes no texture type for a linear 2D array (Metal types its textures
at creation and makes every array twiddled). Such draws were refused
("an array sample at a non-constant layer of a view without array
state").
A pipeline with array samples now also builds a one-layer variant, a
full linked pipeline with its own punch child, whose array samples are
2D samples of the view's base layer. With one layer that is exact:
Vulkan's layer is clamp(RNE(z), 0, layers - 1), and a texel fetch at
another layer is out of bounds (robustImageAccess is not exposed). The
draw selects the variant when an array-sampled view has no array state.
The variant's uploads (PCO_G5P_UPLOAD_IMAGE_ONE_LAYER) take one-layer
views only and refuse a multi-layer array view by name, so a program
that samples a one-layer linear view and a multi-layer array view at
non-constant layers is still refused. The base programs are unchanged.
Assisted-by: LLM
The PowerVR A9 path reads a sampler's mipLodBias from its descriptor metadata with load_smp_meta_pco, a preloaded uniform, and divergence analysis asserted on the unhandled intrinsic in debug builds. Like load_push_constant, it is divergent only when its array-element source is. Assisted-by: LLM
PPSSPP sets mipLodBias from the PSP's texture LOD bias, and every such
sampler was refused at creation ("LOD bias (no field in the sampler
word)"): it got no A9 words, a draw sampling it was refused, and the
command buffer failed with VK_ERROR_OUT_OF_HOST_MEMORY when a game
entered 3D.
The A9 sampler word has no bias. AGXMetalA9 builds it from
MTLSamplerDescriptor's filters, address modes, compare, anisotropy and
LOD clamps only (-[AGXA9FamilyDevice newSamplerStateWithDescriptor:]
reads no lodBias; SamplerStateEncoderGen2::SamplerStateFields packs min
and max LOD x 64), and Metal's LOD bias is the shader's sample argument,
the biased sample form. So the bias goes in the shader:
- The sampler keeps its words and carries the bias, clamped to
maxSamplerLodBias, as an f32 in its descriptor metadata (meta[RSVD2],
PVR_G5P_SAMPLER_META_LOD_BIAS).
- The LOD-bias variant's fragment program adds the bound sampler's bias
to every sample that uses its LOD, read per binding as a preloaded
uniform (load_smp_meta_pco, PCO_G5P_UPLOAD_SAMPLER_META). Following
Vulkan's LOD operation: tex becomes txb by the bias; txb takes
clamp(shader bias + sampler bias); txl takes lod + bias; txd scales its
gradients by exp2(bias). txf, size queries and gathers are unchanged.
- The backend records the samplers whose LOD each program uses
(pco_g5p_program::lod_samplers). A draw that binds a non-zero bias on
one of the fragment program's samplers selects the variant, a full
linked pipeline with its own punch child, built at that first draw from
the stages and state the pipeline kept, and reused after. The selection
comes after the transform feedback and one-layer variants and before
punch.
- A non-zero bias under a vertex-stage sample or a compute sample is
refused by name (there is no such variant). A variant whose compile is
refused leaves the draw refused by name.
Base programs are unchanged, and pipelines without biased samplers never
build the variant.
Assisted-by: LLM
User clip distances are written to the A9 TA user planes and shaderClipDistance is advertised, but the SPIR-V front end's capability list lacked ClipDistance, so every pipeline that writes gl_ClipDistance logged "Unsupported SPIR-V capability: SpvCapabilityClipDistance". The warning was cosmetic: spirv_to_nir parsed the shaders the same way. Assisted-by: LLM
Dolphin's output resampling program (its default post-process shader:
bicubic B-spline, Mitchell, Catmull-Rom and Hermite paths of 16
gamma-corrected vec4 taps each, sharp bilinear and area sampling behind
an if/else chain on the filter mode) was refused ("texture result needs
4 dword(s), 30 of 32 in use"), and Dolphin cannot present without it.
G9P's homes give each parked value its own register above r31 for the
whole program, and all 96 were used up by the second bicubic path. The
if/else chain's result also reached the outer phi through the
scalarized vec4 and its movs, so out-of-SSA gave each of its five levels
its own vec4 register, held across the whole chain.
A fragment program that every attempt refuses for register pressure is
now compiled once more, as the last attempt: its own control flow with
the phi webs coalesced (copy propagation before out-of-SSA), and the
homes as an allocator. A parked value takes the first register above
r31 that is free at its definition and frees it after its last read (a
loop's end for a value read in a loop it is defined outside of), and in
this attempt loop-carried NIR registers can live above r31 too
(load_reg and store_reg move through the same G9P OR form). Each attempt
parks as many more values as the failing allocation asked for. A read
after a home was freed is refused by name, never served from a reused
register. The temps count the highest register used.
Apple's A9 compiler does the same under pressure: it keeps values in
registers above r31 and spills to private memory only past 128 temps.
Its offline compile of this Dolphin program declares 80 temps and does
not spill; ours declares 92. Programs that compile today never reach
the new attempt, so their code is unchanged.
Assisted-by: LLM
A fragment program reading gl_FragCoord.z was refused ("Z coefficient
layout is not qualified"), and with it every PPSSPP pipeline that
rounds its depth. Implement it as AGXMetalA9 does on the A9:
- AGX::FragmentProgramVariant gives a type-6 varying item (position Z)
the f32/f16 offset 2 (the TSP dword after the RHW), Gouraud, not
perspective, and moves every other item's offsets by 2. The variant's
output-select bits are rhw_pres | tsp_unclamped_z_pres when any item
is position Z, else rhw_pres, and setRenderPipelineStateCommon merges
them into TA_OUTPUT_SEL.
- Apple's A9 compiler puts W at coefficient set 0 when present, then Z,
then the varyings (Z at set 0 without W), and reads Z with the
screen-linear position iterator.
The compiler's reflection lists the Z item first (then W, then the
varyings), and the PDS encoder issues the DOUTIs in that order.
plan_varyings() reserves the Z set and shifts the varyings,
hoist_iterators() reads Z first like W (no counter-1 form), the
coefficient program gains the Z item (first, as Apple's) and the +2 TSP
offsets, the USC varying size counts the Z set, and TA_OUTPUT_SEL sets
tsp_unclamped_z_pres from the drawn program (the LOD-bias and punch
variants carry their own flag; the LOD-bias variant must match its
node's). The position iterator's encoder argument is now the
coefficient set (0 or 1), as its last byte is set << 1. Programs that do
not read Z are unchanged.
Assisted-by: LLM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Ten commits that turn shader-compile refusals on the A9 into working programs. Each was a named refusal that stopped a
real application:
strides, sums of dynamic terms) is lowered in NIR to Apple's element-index form, with the index clamped inside the
block. Zink's uint-array uniform blocks and naga's vec4 loads were the main hits.
encoders already produce Apple's bytes for these bank pairs.
format features that an early return in
pvr_get_buffer_format_features2()dropped for every architecture.setDepthClipMode:(PPSSPP's GL backenddrew nothing).
sampler2DArray).nir/divergence+ samplermipLodBiasthrough a LOD-bias program variant (the A9 sampler word has no biasfield; Metal puts the bias in the shader).
ClipDistancecapability (a cosmetic warning per pipeline).resampling shader).
gl_FragCoord.zthrough the TSP's unclamped Z, as AGXMetalA9 lays it out.Programs that compiled before are unchanged; every change only affects compiles that were refused.
How to test
Expected on the A9
vertex programs (before it was moved onto main's texture encoder). Dolphin and PPSSPP ran on driver builds that carry
these commits; per-feature pixel checks of LOD bias and
gl_FragCoord.zare still to fly.gl_FragCoord.z16/16 recorded; the 38 PPSSPP Gran Turismo pipelines that read
gl_FragCoord.zget past that refusal (those thatalso write
gl_FragDepthrefuse by name: depth export is not in this series); the compiler corpus (888 compiles)is byte-identical for every program that compiled before.
A10
A10: enabled, untested on our side, for the texel-buffer format-feature fix in
pvr_formats.c(the G9P bufferfeatures return to what they were before the early return). Everything else is the A9 path (
apple_g5p) or a NIRdivergence rule.
The whole series (this PR and the ones stacked on it) also ran on an iPad 5 (J71t) with the current Orchard kernel: 4x MSAA and partial-clear pixel tests exact, Firefox and GNOME Web on the GPU desktop, no GPU faults.
🤖 Generated with Claude Code