Skip to content

asahi: compile border-free twins on G16G - #51

Open
IronsideXXVI wants to merge 5 commits into
omacom:integration/aurorafrom
IronsideXXVI:fix/g16-border-twin
Open

IronsideXXVI wants to merge 5 commits into
omacom:integration/aurorafrom
IronsideXXVI:fix/g16-border-twin

Conversation

@IronsideXXVI

Copy link
Copy Markdown

Stacked on #50 (and through it #49). Only the last commit, "asahi: compile border-free twins on G16G", is for review here; it builds on the switch helpers and compiler-flag bits the earlier commits add. Please merge #49 and #50 first.

Native graphics compiles the custom-border-colour emulation out of a shader and keeps the emulating NIR as a twin until the physical device creates a sampler that needs it; shaders bound or drawn after that switch to the twin. The emulation branch is recognised by matching the border test with the index the clamp-to-0 sample uses.

On G16G the test reads clamp_0_sampler_index_or_negative and the clamp-to-0 sample reads the unbiased clamp-to-0 index of the same sampled-image descriptor. Only the G17P path accepted that pair, so on G16G every texture sample kept the emulation: a flag load and compare, a branch with a second sample for border samplers, and the merge.

This change accepts the same pair on G16G, with the offset between the two fields taken from struct hk_sampled_image_descriptor_sw, so G16G follows the existing G17P rule. Once a sampler needs emulation, shaders use the emulating twin as before.

A twin is chosen when the shader is compiled and replaced when a command buffer binds it or draws after a border sampler exists. With update-after-bind sampled images a border sampler could reach an already recorded border-free draw, so on G16G devices that enable descriptorBindingSampledImageUpdateAfterBind keep the emulation (the feature is part of the shader key).

  • ASAHI_G16_BORDER_TWIN=0 keeps the emulation in every shader; the setting is part of the physical device's compiler flags.

Testing

  • asahi test suite: 72/0 with the default and 72/0 with ASAHI_G16_BORDER_TWIN=0.
  • Compiled programs for the blended-fill, opaque, textured, geometry and plain-triangle benchmark pipelines: only the textured fragment program changes (79 -> 42 instructions, 624 -> 324 bytes); with the opt-out every program is byte-identical to the build without this change.
  • Pixels: a 1920x1080, 16-layer readback of the blended fill and textured paths is byte-identical to the build without this change; a clamp-to-border readback (transparent black, opaque white, opaque black, with the border sampler created before and after the pipeline) is byte-identical to the build without this change in both orders.
  • Performance (M4 MacBook Pro, 1920x1080, 64 submissions in flight, alternating runs with rests, no GPU faults): textured blended draws 784.3, 783.7 -> 680.1, 684.2 µs per submit (-13.0 %); blended fill unchanged (253.5 -> 251.8 µs). A repeat on the following kernel release gave 785.3 -> 684.5, 684.8 µs (-12.8 %) with the fill at 250.5 -> 250.7 µs.
  • Installed release build: textured blended draws 785.8, 787.0 µs per submit with ASAHI_G16_BORDER_TWIN=0 and 684.0, 684.6 µs by default (-13.0 %), alternating runs with rests; blended fill 250.4 µs; no GPU faults.

Additive blending currently emits src * src_factor + dst * dst_factor. With dst_term_first the destination term comes first, so contraction fuses the destination product into the final multiply-add and the source product becomes a separate multiply that a backend can schedule independently of the destination load. Off by default; no driver changes behaviour.
On G16G the epilog's additive blend now fuses dst * dst_factor into the final multiply-add, so each channel needs one dependent operation after the tilebuffer load instead of a multiply followed by a multiply-add. The source product no longer depends on the destination and is scheduled in the load's shadow. The association of the last add changes, so results may differ by one UNORM step from the previous order. The choice is carried in the epilog key, so it applies only to G16G and separates cached shaders. ASAHI_G16_BLEND_DST_FIRST=0 restores the previous order.
After scheduling, move fadd, fmul, ffma and mov_imm computed only from uniforms and immediates ahead of the first wait_pix of a G16G fragment block. With the blend destination term first, the source terms of a uniform-colour blend (clamps, 1 - alpha and the source products) are then computed before the ordered colour section, which holds only the tile load, unpack, one multiply-add per channel, pack and store. Texture, varying and tile-dependent work keeps its scheduled place. Operations, modifiers and operands are unchanged, so results are bit-identical. The choice is part of the Vulkan compiler flags and the Gallium disk-cache identity. ASAHI_G16_PIX_HOIST=0 restores the previous placement.
G16G forms no native constant program, so a fragment program whose
colour comes from push constants recomputes the same clamps, 1 - alpha
and source products for every pixel. For an inline-blend program, take
the exact fp32 operations (fsat, fmul, fadd, fneg, fabs) whose operands
are push constants or immediates, evaluate them on the host when the
draw's root is built, and load the results from uniform halves 0..11.
Those halves hold only the spill table and the blend constants, which
such a program does not use; the program's uniform layout and launch
words are unchanged. Fused multiply-add stays on the GPU because G16G
FFMA does not round like the host. Only UNORM targets qualify, where
host and GPU denormal or NaN handling cannot change a stored byte.

For a uniform-colour alpha blend the fragment program becomes the tile
load, unpack, one multiply-add per channel with uniform operands, pack
and store. Programs built this way are not written to the disk cache.

ASAHI_G16_HOST_BLEND_SRC=0 restores per-pixel evaluation; the setting
is part of the physical device's compiler flags.
Native graphics drops the custom-border emulation from a shader and keeps
the emulating NIR as a twin until the physical device creates a sampler
that needs it. The emulation branch is recognised by matching the border
test with the index the clamp-to-0 sample uses. On G16G the test reads
clamp_0_sampler_index_or_negative and the sample reads the unbiased
clamp-to-0 index of the same sampled-image descriptor, a pair only the
G17P path accepted, so every G16G texture sample kept the emulation: a
flag load and compare, a branch, a second sample for border samplers and
the merge.

Accept that pair on G16G, with the offset between the two fields taken
from struct hk_sampled_image_descriptor_sw. The textured-blend fragment
program of a common benchmark goes from 79 to 42 instructions; shaders
switch to the emulating twin exactly as on G17P once a border sampler
exists.

A twin is chosen when the shader is compiled and replaced when a command
buffer binds it or draws after a border sampler exists. With update-after-
bind sampled images a border sampler could reach an already recorded
border-free draw, so devices that enable
descriptorBindingSampledImageUpdateAfterBind keep the emulation on G16G.

ASAHI_G16_BORDER_TWIN=0 keeps the emulation in every shader; the setting
is part of the physical device's compiler flags.
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect native graphics shader work on G16G devices.

Fix the host arithmetic before merging; the UNORM-only check does not prevent wrong results.

Findings

  1. P1 Host arithmetic changes shader results ▶

Summary

The changes reduce repeated work in G16G fragment shaders and add border-free shader twins.

  • G16G fragment shaders move uniform math before pixel ordering waits.
  • Eligible G16G blend math runs once per draw on the host.
  • G16G shaders skip border checks until a custom-border sampler needs them.
  • G16G additive blending puts the destination term first.

The host arithmetic needs to preserve the GPU's handling of very small numbers before merging.

Reviews (1) · Last reviewed commit: "asahi: compile border-free twins on G16G" · Reviewed by Greptile

Comment on lines +7248 to +7249
case HK_G16_HOST_FMUL: r = s[0] * s[1]; break;
case HK_G16_HOST_FADD: r = s[0] + s[1]; break;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Host arithmetic changes shader results. hk_g16_host_eval preserves very small float values that GPU arithmetic flushes to zero. Checking only the attachment format does not hide this difference: later arithmetic can make those values large again. For example, two precise multiplies of push constants, (2^-126 * 0.5) * 2^126, produce 0.5 on a host that preserves small values but zero with the shader's requested flush-to-zero behavior. Both multiplies qualify for this path, and the difference remains visible in a UNORM attachment. Match the GPU's rules for each taken operation, or reject expressions whose results can differ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant