Repository navigation
asahi: compile border-free twins on G16G - #51
IronsideXXVI wants to merge 5 commits into
Conversation
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.
[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
SummaryThe changes reduce repeated work in G16G fragment shaders and add border-free shader twins.
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 |
| case HK_G16_HOST_FMUL: r = s[0] * s[1]; break; | ||
| case HK_G16_HOST_FADD: r = s[0] + s[1]; break; |
There was a problem hiding this comment.
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.
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_negativeand 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
descriptorBindingSampledImageUpdateAfterBindkeep the emulation (the feature is part of the shader key).ASAHI_G16_BORDER_TWIN=0keeps the emulation in every shader; the setting is part of the physical device's compiler flags.Testing
ASAHI_G16_BORDER_TWIN=0.ASAHI_G16_BORDER_TWIN=0and 684.0, 684.6 µs by default (-13.0 %), alternating runs with rests; blended fill 250.4 µs; no GPU faults.