Skip to content

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

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

IronsideXXVI wants to merge 11 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.
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The reviewed changes appear safe to merge.

Summary

The PR extends border-free shader twins to G16G and includes the stacked fragment-shader optimizations.

  • Eligible G16G blend math runs during draw setup instead of for every pixel.
  • G16G fragment shaders move uniform-only math ahead of pixel waits.
  • Eligible G16G shaders can use a border-free twin after a custom-border sampler appears.
  • The compiler debug header gains one blank line.

Reviews (4) · Last reviewed commit: "hk: drop the G16G border-twin switch" · Reviewed by Greptile

Comment thread src/asahi/vulkan/hk_cmd_draw.c Resolved
agx3_g16_hoist_pixel_independent reorders instructions after scheduling,
so cover its rules directly instead of relying on hardware readbacks:

- a chain of uniform-only fmul, fadd, ffma and mov_imm moves ahead of
  WAIT_PIX and keeps its scheduled order;
- a tile load, the operation reading it and the operation reading that
  result stay after the wait, although they also read uniforms;
- nothing moves without a wait, on other chips, in other stages or in
  preambles;
- ASAHI_G16_PIX_HOIST=0 keeps the scheduled order.

Export the pass so the test can call it.
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.
The GPU flushes fp32 denormal operands and results of fadd, fmul and
fsat to zero: hk exposes shaderDenormFlushToZeroFloat32 and no fp32
denormal preservation. hk_g16_host_eval kept them, so a tiny intermediate
the GPU flushes could be scaled back into range by a later multiply,
for example (2^-100 * 2^-30) * 2^127 gave 0.125 on the host and 0 on the
GPU, and a UNORM target does not hide that. Flush operands and results
the same way on the host. fneg, fabs and mov only move bits and stay
as they are; their GPU consumers flush.

The source loop also read both sources of every operation. The unused
second source of fsat, fneg, fabs and mov is zero-filled TEMP 0, so a
clamp as first operation read t[0] before anything wrote it. Read only
the sources each operation has.
@IronsideXXVI

Copy link
Copy Markdown
Author

This is the same host denormal issue as on #50. It's fixed by 674ac37, which this branch now carries (rebased on #50). With the branch's hk_g16_host_eval compiled on its own, (2^-126 * 0.5) * 2^126 now gives 0, the same flush the GPU does. The repo's CI build.sh passes locally on this head (meson test: 156 ok, 0 fail).

hk_g16_host_eval ran fadd and fmul in the calling thread's rounding
mode, but GPU fp32 arithmetic rounds to nearest even. With FE_UPWARD
set by the application, ((1 + 2^-24) - 1) * 2^23 gave 1 on the host
instead of 0. Switch to FE_TONEAREST for the evaluation when the
caller uses another mode, and restore the caller's mode afterwards.
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.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks Dylan. Reviewing a20fd5d only. Accepting the G17P border-test pair on G16G, with the field offset taken from struct hk_sampled_image_descriptor_sw rather than a literal, is the right shape. Keeping the emulation when descriptorBindingSampledImageUpdateAfterBind is enabled covers the case where a border sampler reaches an already recorded draw. The clamp-to-border readback with the sampler created both before and after the pipeline is the check this needed.

What blocks a merge:

  1. ASAHI_G16_BORDER_TWIN is a runtime switch, and merged code here can't carry it. Please drop it and its compiler-flag bit.
  2. Hardware conformance: a G16G run of the sampler border-colour and texture groups of vk-default with 0 new Fail/Crash against the base.
  3. Merge order: after #49 and #50.

Mesa CI on a20fd5d is approved and running.

@IronsideXXVI

Copy link
Copy Markdown
Author

Thanks. I'll drop ASAHI_G16_BORDER_TWIN and its compiler-flag bit, and run the sampler border-colour and texture groups of vk-default on G16G against the base. This one follows #49 and #50.

Merged code cannot carry runtime switches. Remove ASAHI_G16_BLEND_DST_FIRST
and ASAHI_G16_PIX_HOIST with their helpers: on G16G the blend destination
term is always emitted first and uniform-only arithmetic is always issued
ahead of the first WAIT_PIX. The Vulkan compiler-flag bit and the Gallium
disk-cache bit only kept the settings apart, so they go too.

agx3_g16_hoist_pixel_independent() moved uniform reads ahead of the wait
without looking for a uniform write between the two. Stop the scan of a
block at the first instruction after the wait that writes the uniform file
(uniform_store or a uniform destination); nothing after it moves.

Tests: the opt-out test is replaced by StopsAtUniformWrite; the other-chip
test already checks the scheduled program is left as it was where the pass
does not run.

Both changes stay limited to G16G because that is the only AGX3b part they
were measured on. G16S and G16C were not measured; nothing known rules them
out.

Assisted-by: LLM
Merged code cannot carry runtime switches. Remove ASAHI_G16_HOST_BLEND_SRC
and agx_g16_host_blend_src_enabled(): on G16G, uniform-only fp32 arithmetic
of an inline-blend fragment program is always evaluated on the host per
draw (fused multiply-add stays on the GPU). The compiler-flag bit only kept
the two settings apart, so it goes too.

Limited to G16G because that is the only AGX3b part it was measured on.
G16S and G16C were not measured; nothing known rules them out.

Assisted-by: LLM
Merged code cannot carry runtime switches. Remove ASAHI_G16_BORDER_TWIN
and agx_g16_border_twin_enabled(): on G16G, border-free twins are always
compiled (the emulation stays when descriptorBindingSampledImageUpdateAfterBind
is enabled). The compiler-flag bit only kept the two settings apart, so it
goes too.

Limited to G16G because that is the only AGX3b part it was measured on.
G16S and G16C were not measured; nothing known rules them out.

Assisted-by: LLM
@IronsideXXVI

Copy link
Copy Markdown
Author

Pushed three commits on top of a20fd5d (no force-push):

The repo's CI build.sh passes locally on this head (meson test: 156 ok, 0 fail).

Still open: the G16G run of the sampler border-colour and texture groups of vk-default against the base. I'll post it when it's done.

@IronsideXXVI

Copy link
Copy Markdown
Author

Retargeted to aurora-silicon#607

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.

2 participants