Skip to content

fix(agc): sample the base level of mipmapped views with unnormalized coordinates - #1377

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
gjonn:fix/agc-unnormalized-base-level-view
Oct 11, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
gjonn:fix/agc-unnormalized-base-level-view

Conversation

@gjonn

@gjonn gjonn commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

What

A sampled image used with an unnormalized-coordinate sampler had to be a single-level view starting at mip 0, otherwise binding it threw. A title samples full mip chains this way from compute shaders during start-up: a 10-level 2D view at base level 0, and a 7-level view at base level 3 (a downsample chain).

Vulkan accepts unnormalized samplers only on single-level views, and the hardware samples the view's base level in that mode (the existing test already checks that image_sample_l and sampler LOD bias/clamps do not move it off level 0).

  • Texture keeps the create info for a one-level, one-layer view of the descriptor's base mip (1D, 2D, or the first layer of a 2D array) when the sampled view has more than one level, and creates that view on first use.
  • ShaderResources binds that view for unnormalized samplers instead of throwing. Views with more than one layer, other view types and a minimum LOD clamp still throw.
  • The base-level restriction is dropped: unnormalized coordinates address the view's base mip, which is what the one-level view samples.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0, RTX 5090: agc_driver_image_sample_unnormalized passes with two new cases for every sampler and instruction: a full 4-level view samples like a 1-level image, and a 3-level view starting at mip 1 samples mip 1 in mip-1 texel coordinates (mip 1 now holds its own pattern).
  • A title that samples these views no longer aborts on either view and continues to the next start-up issue.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

@adriadam10 adriadam10 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.

Measured on RDNA2 hardware (gfx1035 via our HW oracle; 2D T# with 4 levels, BASE_LEVEL 0..3, the same instructions and S# words as this test). Probe: sample_unnormalized_mips.py, happy to share it.

  • image_sample_lz with an S# that has no MIN_LOD/bias does read BASE_LEVEL, with coordinates in that level's texels (0 mismatches over every row, clamp and border modes included). So your one-level base view is exact for that case.
  • It is not exact for the rest. With unnormalized coordinates the hardware still selects a LOD:
    • image_sample_l with LOD 2.7 reads mip 3 (base 0, 1 and 2 alike).
    • image_sample picks the level from the derivatives. Lanes in the same dispatch read mips 0 to 3.
    • The MIN_LOD 1, LOD bias 1.5 sampler moves even image_sample_lz off the base level (base 0 → a blend of mips 1 and 2, base 2 → mip 3).
  • ImageSampleUnnormalized.cpp:248-251 requires the base level for all 3 instructions and all 5 samplers on chain and fromLevelOne. It pins results the hardware doesn't produce. main threw in these cases, and now they return a plausible but wrong value.
  • A possible direction: accept levels > 1 only when the result is exact. That means every sample through the binding is an _lz form (the recompiler would need to flag that next to imageUnnormalized), and the S# has MIN_LOD 0 and no positive bias. Keep throwing otherwise. In the test, check the exact case and use ExpectFailure for the others.
  • Minor: the llvmpipe skip leaves the mip-1 case untested on Linux CI. On RADV it passes here.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 8, 2026
@fisherjoey

Copy link
Copy Markdown
Contributor

Linux results, since this was tested on Windows only. Everything passes here.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main 29e533a.

main 29e533a main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 183 / 183 183 / 183
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 183 / 183 183 / 183
CPU tests (-E agc_driver) 279 / 280 279 / 280

The CPU failure is guest_dynamic_loader, which fails the same way on main.

gjonn and others added 2 commits October 11, 2026 11:07
…coordinates

Vulkan only accepts unnormalized samplers on single-level views, while the
guest binds full mip chains and samples their base level. Bind a lazily
created one-level view of the descriptor's base mip instead of refusing
mipmapped views and views that start above mip 0.
…d coordinates only where the hardware does

Multi-level views, and views starting above mip 0, are bound as their base
level only when every use of the image is image_sample_lz through an
unnormalized S# without minimum LOD or LOD bias, the case measured exact on
RDNA2. Selected-LOD, implicit-LOD and offset uses keep throwing.

The recompiler records selected-LOD uses (SamplerUseNonZeroLod) and proves
the case per image (imageUnnormalizedBaseLevel); the shader disk cache
format goes to 27.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the fix/agc-unnormalized-base-level-view branch from 22b49f5 to ac1c3b7 Compare October 11, 2026 09:12

@adriadam10 adriadam10 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 @fisherjoey for the Linux runs. Checked this on RDNA2 hardware (gfx1035): with unnormalized coordinates the base level is only read by image_sample_lz through an S# without MIN_LOD or LOD bias; image_sample_l, image_sample and biased samples pick other mips on a mipmapped view.

Added on your branch, since it had no push after the review:

  • Rebased onto current main.
  • Multi-level views, and views starting above mip 0, are bound as their base level only when every use of the image is image_sample_lz through an unnormalized S# with MIN_LOD 0 and bias 0 (imageUnnormalizedBaseLevel, proved in DescriptorBindingBuilder; new SamplerUseNonZeroLod use bit). Selected-LOD, implicit-LOD and texel-offset uses keep throwing.
  • Shader disk cache FormatVersion 26 → 27 and the layout checks for the new field and the wider uses.
  • ImageSampleUnnormalized: an image_sample_lz-only program on the 4-level view and on the view from mip 1, and ExpectFailure cases for _l/image_sample, a view from mip 1 with them, texel offsets, and an S# with MIN_LOD and bias. ShaderMemory/ShaderDiskCache cover the new flag.

agc_driver_image_sample_unnormalized, agc_shader_memory and agc_shader_disk_cache pass on RADV; a mutant that accepts _l is caught. The mip 1 case is still skipped on llvmpipe, so CI does not cover it.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 11, 2026
@adriadam10
adriadam10 merged commit 5ad2cb8 into boykopovar:main Oct 11, 2026
14 checks passed
@frankea

frankea commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

@adriadam10 a follow-up for your RDNA2 oracle, if you have time.

Gran Turismo 7 (PPSA01317) has ten hull programs that sample a 10-level 512x256 2D texture with image_sample_l through a constant S#, 00008036 00fff000 05000000 00000000. That S# is FORCE_UNNORMALIZED, clamps to border, and uses point min/mag filters and a point mip filter, with MIN_LOD 0, MAX_LOD 0xfff and no bias. Each lane computes an integer level m and samples at LOD m at ((x >> m) + 0.5, (y >> m) + 0.5). That only makes sense if unnormalized coordinates are in the texels of the level the LOD selects, not in the base level's.

Your review here found that image_sample_l at LOD 2.7 reads mip 3. Do you know which texel of mip 3 it read for a given coordinate: was the coordinate used as is, or divided by 2^3? If your probe can show that, I would cite it as the source for emulating these samples. The emulation would divide the coordinates by the selected level's size and sample through a normalized point-mip sampler at that level.

AI-assisted: yes (Claude Code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants