Skip to content

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

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

IronsideXXVI wants to merge 4 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.
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.
@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: 4/5

[Medium impact] The changes affect selected G16G fragment-shader blend and scheduling paths.

Fix the float-control mismatch before merging.

Findings

  1. P1 Tiny values change rendered colours ▶
  2. P2 Unary operations read uninitialized memory ▶

Summary

Moves eligible G16G fragment arithmetic from per-pixel GPU work to per-draw CPU work. Review scope is the final commit; the three prerequisite commits belong to #49.

  • G16G blend shaders calculate uniform-only color math once per draw.
  • G16G additive blends put the destination term first.
  • G16G fragment shaders move uniform-only math before the pixel wait.

The evaluator needs to preserve the shader’s tiny-number rules and avoid reading an unused, uninitialized input.

Reviews (1) · Last reviewed commit: "asahi: evaluate uniform-only G16G fragme..." · 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 Tiny values change rendered colours. hk_g16_host_lower takes arithmetic from the whole fragment shader, not just the final blend. With normal host floating-point settings, a shader computing (x * y) * z from push constants x = 2^-100, y = 2^-30, and z = 2^127 gets 0.125 here. A shader requesting DenormFlushToZero must instead get zero: the first multiply produces 2^-130, which must be flushed before the next multiply.

UNORM output does not hide this difference because the second multiply makes the tiny value large again. Honor the shader’s float controls for each CPU operation, or refuse host evaluation when those rules cannot be matched.

Comment on lines +7230 to +7234
for (unsigned j = 0; j < 2; ++j) {
const struct hk_g16_host_src *src = &p->ops[i].src[j];
uint32_t u = 0;
if (src->kind == HK_G16_HOST_TEMP) {
s[j] = t[src->value];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Unary operations read uninitialized memory. This loop reads two inputs even for FSAT, FNEG, FABS, and MOV. Their unused second input is zero-filled when the operation is built, so its kind is HK_G16_HOST_TEMP and its index is zero. When the first operation is a clamp, the loop reads t[0] before anything writes it. The read is volatile, so discarding the unused result does not remove it.

Read only the inputs the operation uses, or initialize unused inputs as immediates. This avoids undefined behavior in the common clamp-first path.

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