Skip to content

asahi: evaluate uniform-only G16G fragment arithmetic per draw - #50

Open
IronsideXXVI wants to merge 9 commits into
omacom:integration/aurorafrom
IronsideXXVI:fix/g16-host-blend-src
Open

IronsideXXVI wants to merge 9 commits into
omacom:integration/aurorafrom
IronsideXXVI:fix/g16-host-blend-src

Conversation

@IronsideXXVI

Copy link
Copy Markdown

Stacked on #49. This branch contains the three commits of #49 followed by one new commit; only the last commit, "asahi: evaluate uniform-only G16G fragment arithmetic per draw", is for review here. It uses the switch helpers and compiler-flag bits #49 adds, and the measured program shape assumes #49's destination-term-first blend. Please merge #49 first; this PR will then show the single commit.

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 the alpha-blended fill this is 9 of the 13 ALU instructions in the per-pixel program.

This change takes, in an inline-blend fragment program on G16G, the exact fp32 operations (fsat, fmul, fadd, fneg, fabs) whose operands are push constants or immediates, evaluates them on the host when the draw's root table is built, and has the program load the results from uniform halves 0..11. Only the native spill table and the blend constants otherwise use those halves, and a program of this kind uses neither. The program's uniform layout and launch words are unchanged; the results reach the program as one more root-relative uniform range.

  • Fused multiply-add stays on the GPU: G16G FFMA does not round like the host.
  • The host evaluation matches the GPU operation for operation (each result rounded to fp32 on its own, no contraction). Only UNORM render targets qualify, where host and GPU differences in denormal or NaN handling cannot change a stored byte.
  • Programs built this way are not written to the disk cache, because their host half lives only in the linked shader.
  • ASAHI_G16_HOST_BLEND_SRC=0 restores per-pixel evaluation. The setting is part of the physical device's compiler flags, so cached pipelines from one setting are not reused under the other.

For the uniform-colour alpha blend the fragment program becomes: pixel wait, tile load, two unpacks, one multiply-add per channel with both factors uniform, two packs, pixel wait, tile store (22 -> 13 instructions, 13 -> 4 ALU, 19 -> 11 registers, 170 -> 120 bytes).

Testing

  • asahi test suite: 72/0 with the default and 72/0 with ASAHI_G16_HOST_BLEND_SRC=0.
  • Compiled programs for the blended-fill, opaque, textured, geometry and plain-triangle benchmark pipelines: only the blended-fill fragment program changes; 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 with a different push colour per layer, and a 64-frame blend/scissor/signed-zero sequence: byte-identical to the build without this change.
  • Performance, M4 MacBook Pro, 64 submissions in flight, alternating runs, no GPU faults (µs per submit, without -> with):
    • blended fill 1920x1080: 332.19, 332.26 -> 251.85, 249.88 (-24.45 %)
    • blended fill 2560x1440: 583.88, 584.00 -> 440.46, 439.69 (-24.64 %)
    • textured blended draws 1920x1080: 784.72, 784.52 -> 784.93, 784.52 (-0.01 %; this program does not change)
      The 2560x1440 and textured runs pause 45 s before each run: under minutes of continuous load this machine's GPU throughput drops by about a quarter for every build, which would otherwise mix states between runs.
  • Installed release build with this change: blended fill 1920x1080 251.0 µs per submit (332.1 before), zero 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.
@IronsideXXVI
IronsideXXVI force-pushed the fix/g16-host-blend-src branch from 6705206 to cc1294e Compare November 17, 2026 20:27
@greptile-apps

greptile-apps Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The changes appear safe to merge; no new blocking issue was found.

Summary

This PR moves supported push-constant arithmetic from G16G fragment programs to the CPU once per draw.

  • G16G inline blends compute uniform-only math once per draw.
  • G16G fragment shaders move uniform-only math ahead of pixel waits.
  • The compiler debug header gains a blank separator line.

No new actionable issues were found. The earlier reports about tiny values, unused inputs, and CPU rounding are addressed in the current code. This review used source checks; no builds or runtime tests were run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Push constants] --> B[CPU evaluates supported operations]
  B --> C[Draw root table]
  C --> D[Fragment uniforms]
  D --> E[Remaining GPU arithmetic and blend]
Loading

Reviews (4) · Last reviewed commit: "hk: drop the G16G host blend-source swit..." · Reviewed by Greptile

Comment thread src/asahi/vulkan/hk_cmd_draw.c Resolved
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
IronsideXXVI force-pushed the fix/g16-host-blend-src branch from cc1294e to 674ac37 Compare November 20, 2026 18:21
@IronsideXXVI

Copy link
Copy Markdown
Author

Both notes were right. Fixed in 674ac37:

  • Denormals: the GPU flushes fp32 denormal operands and results (hk only exposes shaderDenormFlushToZeroFloat32), and the host now does the same for fadd, fmul and fsat. With the branch's hk_g16_host_eval compiled on its own (ASan/UBSan clean), your example (2^-100 * 2^-30) * 2^127 now gives 0, and (2^-60 * 2^-60) * 2^100 still gives 2^-20. fneg, fabs and mov only move bits; whatever consumes them on the GPU flushes. Results are flushed after rounding to fp32. I haven't measured on hardware yet whether G16G detects tininess before or after rounding, and that is the one edge case left.
  • Unused sources: the loop reads only the sources each op has (2 for fadd/fmul, 1 otherwise), so a leading clamp no longer reads t[0].

The repo's CI build.sh passes locally on the #51 head, which contains this commit (meson test: 156 ok, 0 fail). This branch is rebased on the updated #49.

Comment thread src/asahi/vulkan/hk_cmd_draw.c Resolved
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.

@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 only 67c5c8f, 674ac37 and d8e18f5 here; #49 covers the rest. Evaluating the uniform-only fp32 chain on the host for UNORM targets, with FMA left on the GPU, is a clean way to get the colour section down to the tile work. The 13 -> 4 ALU shape and the -24 % at both sizes are a big step. 674ac37 and d8e18f5 address all three Greptile notes: hk_g16_host_eval() flushes denormals like the GPU, reads only the sources each op has, and evaluates in round-to-nearest-even before restoring the caller's mode.

What blocks a merge:

  1. ASAHI_G16_HOST_BLEND_SRC, like #49's switches, is a runtime switch, and merged code here can't carry it. Please drop it and the compiler-flag bit it adds.
  2. Hardware conformance. This moves shader arithmetic to the CPU, so beyond the readbacks we need a G16G vk-default subset run (blend, push constants, fragment ops) with 0 new Fail/Crash against the base. The edge you flagged yourself belongs in that run as well: whether G16G detects tininess before or after rounding. A later multiply can bring a flushed intermediate back into range, so the host has to match the GPU there.
  3. Merge order: after #49.

The per-draw fegetround()/fesetround() pair is fine as it is. If CPU time per draw shows up in a profile later, a per-thread check is the obvious next step.

Mesa CI on d8e18f5 is approved and running.

@IronsideXXVI

Copy link
Copy Markdown
Author

Thanks. I'll drop ASAHI_G16_HOST_BLEND_SRC and its compiler-flag bit, and run the G16G vk-default subset (blend, push constants, fragment ops) against the base, with the tininess-before-or-after-rounding edge in that run. This one follows #49.

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
@IronsideXXVI

Copy link
Copy Markdown
Author

Pushed two commits on top of d8e18f5 (no force-push):

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

Still open: the G16G vk-default subset (blend, push constants, fragment ops) against the base, and inside that run, whether G16G detects tininess before or after rounding. I'll post both when they're done.

@IronsideXXVI

Copy link
Copy Markdown
Author

Retargeted to aurora-silicon#606

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