Repository navigation
asahi: compile border-free twins on G16G - #51
IronsideXXVI wants to merge 11 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.
[Medium impact] The reviewed changes appear safe to merge. SummaryThe PR extends border-free shader twins to G16G and includes the stacked fragment-shader optimizations.
Reviews (4) · Last reviewed commit: "hk: drop the G16G border-twin switch" · Reviewed by Greptile |
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.
e5a8fc2 to
2886796
Compare
|
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 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.
2886796 to
a20fd5d
Compare
DjDeveloperr
left a comment
There was a problem hiding this comment.
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:
ASAHI_G16_BORDER_TWINis a runtime switch, and merged code here can't carry it. Please drop it and its compiler-flag bit.- Hardware conformance: a G16G run of the sampler border-colour and texture groups of vk-default with 0 new Fail/Crash against the base.
- Merge order: after #49 and #50.
Mesa CI on a20fd5d is approved and running.
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
|
Pushed three commits on top of a20fd5d (no force-push):
The repo's CI 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. |
|
Retargeted to aurora-silicon#607 |
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.