Skip to content

borealis: lazily emulate VK_EXT_image_view_min_lod on AGX3 - #1261

Merged
DjDeveloperr merged 3 commits into
aurora-wipfrom
djdev/borealis-lazy-min-lod
Nov 28, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-wipfrom
djdev/borealis-lazy-min-lod

Conversation

@DjDeveloperr

Copy link
Copy Markdown

What: Borealis emulates VK_EXT_image_view_min_lod in every shader that samples, for every vkd3d-proton game (the driconf turns the extension on for that engine), even though most D3D12 games never create a view with a minimum LOD. This leaves the emulation out until the process creates its first view with minLod above its base level, then swaps in emulating twins at bind time, the way custom-border emulation already works. GTA V Enhanced at 3024x1964 on the M5 Pro gets 3.7 % more FPS.

Tested: J714s (M5 Pro, G17S) at 679956d08d61: GCC release and Clang assert --suite asahi pass, min_lod and custom-border VK CTS 0 fail, new twin test passes, GTA V A/B +3.7 % with 0 GPU faults.

Risk: two-way · M5 and Neo (the chips where the lazy path applies); other GPUs are unchanged.

Not yet: a J700 (G17P) row. It conflicts with #1245 at the end of hk_lower_nir(); whichever merges second needs a small rebase.

Details

The emulation turns plain samples into biased samples that load the view's min LOD from the descriptor, clamps explicit LODs, and wraps texel fetches and gathers with out-of-bounds selects. That costs every texture operation of every shader in a vkd3d-proton game.

How it works:

  • hk_physical_device::min_lod_emulation is set the first time hk_image_view_init() sees a view with a relative minimum LOD above zero. It is remembered per process in the disk cache, like border_emulation, so the next run compiles with the emulation from the start. The pipeline cache key carries it.
  • Until it is set, hk_lower_nir_ex() stops before texture lowering. The shader keeps that NIR as min_lod_source, then finishes texture lowering without the emulation (hk_lower_nir_post_tex()).
  • Binding a shader that has a min_lod_source once the flag is set builds its twin from that NIR with hk_nir_lower_image_view_min_lod_post_desc(). That variant reads the view's minimum LOD through the lowered texture handle (nir_load_from_texture_handle_agx). Graphics stages are re-checked at draw flush and compute at dispatch, as for border twins.
  • Border twins built before the flag flips carry a copy of the min_lod_source, so the two emulations compose either way round.
  • Shader binaries record whether they hold a min_lod source and whether they emulate. An emulating binary is refused on a device that does not need it, as for border emulation. The binary versions move up one for the new flag byte.

Known limit, the same as for border twins: a command buffer recorded before the first such view keeps the shaders it bound. A view created afterwards and reached through an update-after-bind descriptor in that command buffer would be sampled without its minimum LOD.

Evidence

Hardware: J714s (Apple M5 Pro, G17S B1), kernel 7.3.0-m5-cdirect16. Base aurora-wip 41857fdecfd4, candidate 679956d08d61 (this branch), both fetched exactly and built warm on the M5. The runtime kits were isolated through VK_DRIVER_FILES; system Mesa was untouched.

check base this PR
GCC release meson test --suite asahi 336/336 336/336
Clang assert (-Db_ndebug=false) --suite asahi 333/333
VK CTS *min_lod* (114 cases) with hk_image_view_min_lod=true 114 pass 114 pass (1 and 200 tests per process)
same, without the option 26 pass, 88 skip (extension not exposed)
VK CTS custom border cases (131) 66 pass, 65 skip, 0 fail (with and without the option)
vulkan-min-lod-emulation-cache (new) fails as expected: no twin is built pass: 2 compiles with the twin, then 0
vulkan-border-emulation-cache pass
hk-vulkan-shader-binary-root pass
Clang -Werror on the objects of every changed C file 8 of 9 clean; hk_cmd_draw.c stops only on three -Winitializer-overrides already in aurora-wip (lines 5675-5685, not touched here)

The dEQP-VK image_view_min_lod cases create their views before their pipelines, so they test the shaders compiled with the emulation from the start. vulkan-min-lod-emulation-cache creates the pipeline first. That makes the draw build the twin, and the second instance on the same cache must compile nothing. The texture's three levels are cleared to different colours, so a shader that ignores the minimum LOD reads the wrong level.

GTA V Enhanced, -benchmark, 3024x1964 native (ResScalingType 0), VSync off, fullscreen, vkd3d-proton from Proton 11.0 (ARM64), isolated shader cache per run. Order A1 B1 B2 A2, same boot, FPS per benchmark pass:

run P0 P1 P2 P3 P4 mean
A1 base 58.85 52.43 52.53 59.27 57.05 56.03
B1 PR 60.97 54.37 54.40 61.90 59.56 58.24
B2 PR 61.00 54.55 55.20 60.82 59.34 58.18
A2 base 58.92 52.87 52.76 59.42 57.20 56.23

Base 56.13 FPS, PR 58.21 FPS: +3.71 %. Every pass gains 3.4-4.1 %, and the spread within each arm is under 0.4 %. The GPU had 0 faults, 0 crashed or quarantined contexts, and 0 kernel errors in every run. The only failed unit was tailscaled, which was already failed before the slot.

On AGX3, hk_nir_lower_image_view_min_lod() currently lowers every non-buffer
texture operation in every shader unconditionally to load the view's minLod
from the texture descriptor, converting plain tex ops into txb with
lod_bias_min_agx, clamping txl into register-LOD fmax(lod, min_lod), and
wrapping txf/txf_ms/tg4 with per-channel out-of-bounds zero selects.

Mirror Borealis's existing lazy custom-border emulation architecture
(pdev->border_emulation / hk_border_twin()):
- Track pdev->min_lod_emulation (persisted in the disk cache per process and
  hashed into hk_shader_cache_key()), flipping 0 -> 1 in hk_image_view_init()
  the first time an application creates a VkImageView with
  minLod > baseMipLevel.
- Until then, defer hk_nir_lower_image_view_min_lod() and texture lowering in
  hk_lower_nir_ex(), serialize the post-descriptor / pre-texture NIR into
  obj->min_lod_source, and compile the primary shader without image-view
  minLod emulation.
- Once pdev->min_lod_emulation flips, lazily compile hk_min_lod_twin() from
  min_lod_source using hk_nir_lower_image_view_min_lod_post_desc() (via
  nir_load_from_texture_handle_agx and aligned 32-bit descriptor loads) and
  swap bound graphics/compute stages transparently.
- Preserve full 2x2 composition with border_emulation (cloning min_lod_source
  onto border twins built before min_lod_emulation flips) and bump the shader
  binary version for the new variant_flags4 byte.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Add vulkan-min-lod-emulation-cache, the image-view minLod counterpart of
vulkan-border-emulation-cache. It creates its pipeline before its first
view with minLod above the base level, so the fragment shader is compiled
without the emulation and the first draw must build the emulating twin.
A second instance on the same disk cache must compile the shader with the
emulation from the start and build nothing while recording.

The texture's three levels are cleared to different colours and the draw
magnifies it, so a shader that ignores the view's minLod reads level 0 and
the test fails.

The dEQP-VK image_view_min_lod cases create their views before their
pipelines and only run with the hk_image_view_min_lod option, so they do
not reach the twin.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 27, 2026 22:16
#1245 changed when hk_lower_nir() may take the post-lower preprocessing
(can_post_lower). This branch moved texture lowering and that
preprocessing into hk_lower_nir_post_tex(), so the new condition now
feeds the hk_lower_nir_post_tex() call at the end of hk_lower_nir_ex();
lowered_tex is still checked inside it.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Author

Exact-head row at 4b9511d4bbd3, which merges aurora-wip a0164c9101c1 (#1245). J714s (M5 Pro, G17S), kernel 7.3.0-m5-cdirect16, built warm on the M5:

  • GCC release --suite asahi: 337/337. Clang assert (-Db_ndebug=false) --suite asahi: 334/334.
  • VK CTS *min_lod* (114 cases) with hk_image_view_min_lod=true: 114 pass, both 1 and 200 tests per process. Without the option: 26 pass, 88 skip.
  • VK CTS custom border cases (131): 66 pass, 65 skip, 0 fail.
  • vulkan-min-lod-emulation-cache, vulkan-border-emulation-cache and hk-vulkan-shader-binary-root: pass.
  • 0 GPU faults, 0 kernel errors.
  • Clang -Werror on the objects of every changed C file: 8 of 9 clean. hk_cmd_draw.c stops only on three -Winitializer-overrides that aurora-wip already has (d4afb55, lines 5818-5828), none of them in this change.

The merge resolution: #1245's new can_post_lower condition now feeds the hk_lower_nir_post_tex() call that this branch moved the end of hk_lower_nir() into. The branch's diff against its base is otherwise unchanged (15 files, +976/-124), so the GTA V A/B in the description (+3.71 %, measured at 679956d08d61) still applies.

@RyanTheTide

Copy link
Copy Markdown

Exact-head 4b9511d4bbd3 2-gen Borealis peer rows (J316s + J700): PASS

  • Build: native aarch64 Clang 23.1.4 (-Dbuildtype=release -Db_ndebug=false -Dtools=drm-shim), overlay libvulkan_asahi.so SHA-256 04f2e7b99841b05fce9f54b37b59441274a825a146cadd683250e60e924fdb3c via isolated VK_DRIVER_FILES (stock /usr/lib64/libvulkan_asahi.so untouched, verified pre/post on both Macs).
  • J316s (M1 Pro, G13S C0, 7.3.0-aurora-fleet89):
    • meson test --suite asahi: 261/261 PASS (asserts enabled).
    • Vulkan CTS: 24 PASS / 0 FAIL (api.smoke 8/8, texture.mipmap.2d.image_view_min_lod.* with hk_image_view_min_lod=true 16/32 [16 NS]); 0 GPU faults/recoveries, Enforcing, 0 failed units, 0 new AVC.
  • J700 (Neo, G17P B1, 7.3.0-aurora-fleet89):
    • vulkan-min-lod-emulation-cache (MESA_SHADER_CACHE_DISABLE=1): PASS (recording with the emulating twin built: 2 compiles, 9.35 ms; emulating from the start: 0 compiles, 0.52 ms; 0 GPU faults/recoveries).
    • Vulkan CTS (MemoryMax=3G): 72 PASS / 0 FAIL (api.smoke 8/8, texture.mipmap.2d.image_view_min_lod.* 32/32, texture.mipmap.3d.image_view_min_lod.* 32/32 with hk_image_view_min_lod=true); 0 GPU faults/recoveries, Enforcing, 0 failed units, 0 new AVC.

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed 4b9511d4bbd3 and verified on Apple M5 Max (J775c, G17C B1):

  • Mirrors the existing lazy border_emulation twin mechanism cleanly for VK_EXT_image_view_min_lod, bumps the shader binary cache version across chip generations, and handles both activation orders safely: activating border_emulation first clones obj->min_lod_source onto the border twin (hk_compile_twin()), and activating min_lod_emulation first compiles the minLod twin with twin_of != NULL, which keeps custom-border emulation enabled at hk_shader.c:9196.
  • Built with -Dbuildtype=release -Db_ndebug=false (including vulkan-min-lod-emulation-cache) and ran MESA_SHADER_CACHE_DISABLE=1 MALLOC_PERTURB_=42 meson test -C build --suite asahi: 337/337 PASS (0 fail).
  • Live Borealis cold-cache VK CTS (MESA_SHADER_CACHE_DISABLE=1): dEQP-VK.api.smoke.* 8/8 PASS, dEQP-VK.texture.explicit_lod.2d.sizes.128x128_* 32/32 PASS, zero GPU faults in dmesg.

@DjDeveloperr
DjDeveloperr merged commit 27f931f into aurora-wip Nov 28, 2026
@DjDeveloperr
DjDeveloperr deleted the djdev/borealis-lazy-min-lod branch November 28, 2026 03:23
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.

3 participants