Skip to content

fix(agc): emulate comparison sampling of color textures in the shader - #668

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
ThisIsAkill:fix/agc-emulated-color-compare-main
Oct 6, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
ThisIsAkill:fix/agc-emulated-color-compare-main

Conversation

@ThisIsAkill

@ThisIsAkill ThisIsAkill commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

What

AMD hardware compares a sample_c reference against the red channel of a colour texture. Vulkan defines comparison sampling only for depth formats, so the driver refused any compare-sampled texture that isn't R32 float or R16 unorm. PPSA21567 (Astro Bot) uses the same LESSEQUAL, clamp-to-border (opaque white) sampler on a shadow map and on a 1x1 RGBA8 texture.

For a compare-sampled image whose format isn't natively comparable:

  • the specialization records the paired sampler's compare function, filter, addressing and border, plus the view's reference clamp kind;
  • the image then binds as an ordinary colour texture;
  • the backend fetches the red channel, applies wrap, clamp-to-edge or clamp-to-border addressing with the sampler's texel-centre convention (border taps take the border's red), compares, and blends four comparisons for bilinear filtering.

Real depth formats keep the native path.

Scope: only sample_c / sample_c_lz on 2D and 2D-array views, float/unorm/snorm formats, wrap / clamp-to-edge / clamp-to-border, and point or bilinear filtering at one level. Half-border, the border colour table (#665) and everything else on this path throw. AMD doesn't document the reference clamp for unorm/snorm colour formats; that's recorded in the technical debt.

Tested

  • Linux (CachyOS, x86-64, GCC 16.2.1): full build with BUILD_TESTING=ON, ctest passes 285/285.
  • New agc_driver_emulated_color_compare execution test. It runs a LESSEQUAL compare on the GPU against a CPU reference: point and bilinear with clamp-to-edge, bilinear with wrap, and point and bilinear with white and black clamp-to-border. It also checks that R32 float keeps the native depth compare and that an RGBA8 texture doesn't, and that the out-of-scope cases throw (uint format, mirror, half-border, border colour table, anisotropic).
  • PPSA21567: on main the title stopped at this refusal. The same change on oneandonlydean's astrobot branch ran 2 of 2 runs (35 min each, Linux, virtual display) to the Crash Site tutorial, with no new throws. This exact main version has not been run against the title, since main doesn't reach that point on it.
  • Not tested on Windows.

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

🤖 Generated with Claude Code

AMD hardware compares a sample_c reference against a color texture's red
channel; Vulkan defines comparison sampling only for depth formats, so the
driver refused any compare-sampled texture that is not R32 float or R16
unorm. PPSA21567 compares the same LESSEQUAL, clamp-to-border (opaque
white) sampler against a shadow map and against a 1x1 RGBA8 texture.

For a compare-sampled image whose format is not natively comparable, the
specialization records the paired sampler's compare function, filter,
addressing and border, and the view's reference clamp kind; the image then
binds as an ordinary color texture. The backend fetches the red channel,
applies wrap, clamp-to-edge or clamp-to-border addressing with the
sampler's texel-centre convention (border taps take the border's red),
compares, and blends four comparisons for bilinear filtering. Real depth
formats keep the native path.

Only sample_c / sample_c_lz on 2D and 2D array views, float/unorm/snorm
formats, wrap, clamp-to-edge or clamp-to-border, point or bilinear at one
level are implemented; half-border, the border color table and everything
else on this path throw. The reference clamp for unorm/snorm color formats
is not documented by AMD and is recorded in the technical debt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ThisIsAkill

Copy link
Copy Markdown
Contributor Author

CI note: the linux job failed with 284 of 285 tests passed. The failing test is agc_driver_flat_cache_bits, a 120 s timeout (https://github.com/boykopovar/AnyPS5/actions/runs/37314963696/job/111779270934); windows passed. This PR doesn't touch that test or the code it covers (the diff is the comparison-sampling emulation, its execution test and the shader disk cache). On this branch locally (Linux, GCC 16.2.1) the test passes in about 4 s: 15 of 15 runs, and 285/285 in the full ctest. The Linux job is green on the other recent PRs, so it looks like a hang on that runner. Could a maintainer re-run the job?

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for porting this to main. It replaces the stopgap I have on my integration branch, which leaves the colour image under the compare sampler and takes whatever the driver returns, so I'm glad to see a defined result.

I went through the decoding and it matches the GFX10 layouts as far as I can tell: CLAMP_X/Y, DEPTH_COMPARE_FUNC, FORCE_UNNORMALIZED, LOD_BIAS, the XY filters, BORDER_COLOR_TYPE, and BASE/LAST_LEVEL for the single level check. Comparing ref OP texel per tap and then blending is the right order for bilinear PCF. The CI failure on Linux is agc_driver_flat_cache_bits timing out, which fails other PRs in the same window too; your new test passed all three repeats.

A few questions, none of them blocking on its own:

  1. The test samples a 2D view only, but every compare sample in PPSA21564's shaders is image_sample_c_lz ... dim:SQ_RSRC_IMG_2D_ARRAY (1,723 shaders, no gathers, no offsets). Could you add a 2D array case, so the layer size query and the floor(z + 0.5) clamp are covered?
  2. NaN: the compares use OpFOrd*, so NOTEQUAL gives 0 for a NaN texel or reference. I believe the hardware's NOT_EQUAL is unordered (true for NaN), as in D3D. If you agree, OpFUnordNotEqual would match; otherwise a short line in the TechnicalDebt entry would do.
  3. A few sampler fields still change the result but aren't checked: FILTER_MODE (word 0 bits 29 to 30, min/max reduction) and TRUNC_COORD (bit 27). Would you throw for non-default values, like the other out-of-scope cases?
  4. Only LESSEQUAL runs in the test. The other functions are simple selects, so this is optional, but one more function, such as GREATER, would guard the operand order.

One thing to be aware of: the maintainer's #639 removes ResourceSpecialization and moves image handling to a fixed set of runtime modes, so this change and #639 conflict in ResourceMaterializer and ShaderDiskCache. Whichever lands second will need the emulated compare as a runtime mode, with the sampler state read at run time. Nothing to do here now; I'm only mentioning it so it's not a surprise.

PPSA21564 does reach this path (a 1x1 RGBA8 surface under a LESSEQUAL sampler), so I'll run it through my visual regression check before it lands.

Locally (Linux, GCC, NVIDIA): agc_driver_emulated_color_compare fails on main's code, passes with the change, and passes 20 times in a row. The full ctest passes with it merged together with the other GPU PRs I tested, apart from one unrelated test.

@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#757 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp L34-36, L56-60
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp L501-502, L634-644, L963
core/shader/recompiler/ShaderDiskCache.cpp L73-75, L83, L410-414, L499-503, L814-815
core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp L5-6, L894-895
#758 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp L34-36, L56-60
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp L501-502, L634-644, L963
core/shader/recompiler/ShaderDiskCache.cpp L73-75, L83, L410-414, L499-503, L814-815
core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp L5-6, L894-895
#759 core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/Resources.hpp L34-36, L56-60
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp L501-502, L634-644, L963
core/shader/recompiler/ShaderDiskCache.cpp L73-75, L83, L410-414, L499-503, L814-815
core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp L5-6, L894-895

@oneandonlydean
oneandonlydean merged commit 021e457 into boykopovar:main Oct 6, 2026
4 of 5 checks passed
@ThisIsAkill

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. Pushed aaac3189 (the code) and f8b209c6 (the test):

  1. 2D array case. The test now samples a two-layer 2D array (image_sample_c_lz ... dim:SQ_RSRC_IMG_2D_ARRAY, assembled with llvm-mc for gfx1030) with a different texel table in each layer. The layer coordinates are -0.4, 0, 0.49, 0.5, 0.99, 1, 1.4 and 2.7, so the nearest-slice rounding and the clamp to the layer count are both exercised. Cases: point and bilinear clamp-to-edge, bilinear wrap, bilinear white border, and GREATER with point sampling.
  2. NaN. NOT_EQUAL now uses OpFUnordNotEqual: it is the IEEE != (true for a NaN reference or texel), and it is what Vulkan defines for VK_COMPARE_OP_NOT_EQUAL on the native depth path. I couldn't find a source for what the hardware does, so the TechnicalDebt entry says the NaN behaviour is unverified. There is no NaN test: an unorm texel can't be NaN, and the reference is clamped first, which SPIR-V leaves undefined for NaN.
  3. FILTER_MODE and TRUNC_COORD. FILTER_MODE (word 0 bits 29–30) already throws when the sampler is created; the specialization now refuses it explicitly too, so the emulation doesn't depend on that. TRUNC_COORD (bit 27) throws when the sampler is point-filtered (it only affects point sampling; with bilinear it's ignored). Both have a rejection test. What I can't tell is how TRUNC_COORD differs from the default at exact texel boundaries: the emulation selects floor(u * width), as Vulkan's nearest does.
  4. More compare functions. GREATER now runs next to LESSEQUAL (point and bilinear), which guards the operand order. The other functions are the same select, as you say.

Tested on Linux (CachyOS, GCC 16.2.1, NVIDIA): agc_driver_emulated_color_compare passes with the new cases, and the full ctest passes 285/285. Noted on #639 and the overlap with #757–#759: nothing to do here until one of them lands.

@ThisIsAkill

Copy link
Copy Markdown
Contributor Author

A note on timing: this was merged at 00:27 UTC, before I pushed the follow-up commits that answer the review, so main has the first commit only (52021996). The review fixes are not in main yet. They are two commits on my branch fix/agc-emulated-color-compare-main (aaac3189: unordered NOT_EQUAL, refuse FILTER_MODE and TRUNC_COORD with point sampling, TechnicalDebt; f8b209c6: the 2D array, GREATER and rejection tests), tested as described above. I'm moving them onto current main as a follow-up change so that the 2D-array coverage and the NaN and sampler-state handling land too.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Sorry about that, it went in on the gate result before your answers arrived; that's on my side. Thanks for moving them onto main. Please link the follow-up here, and I'll review and gate it straight away.

@ThisIsAkill

Copy link
Copy Markdown
Contributor Author

The follow-up is open: #907. It carries the two commits that answer the four review points (unordered NOT_EQUAL and refusing FILTER_MODE reduction and TRUNC_COORD, plus the 2D array and GREATER test cases), rebased on current main. No other changes. Full ctest passes (359 of 359, one test skips itself), and agc_driver_emulated_color_compare passes 4 of 4 runs. #639 is already conflicting with main and touches ResourceMaterializer.cpp, which #907 edits by four lines; I noted that in the PR.

ThisIsAkill added a commit to ThisIsAkill/AnyPS5 that referenced this pull request Oct 7, 2026
The emulated comparison picked the array layer as floor(z + 0.5). Measured
on RDNA2 (adriadam10, in the boykopovar#907 review), the hardware takes
clamp(roundEven(z), 0, layers - 1): z = 0.5 selects layer 0 and 2.5 layer
2, and 0.49999997, which floor(z + 0.5) rounds up to 1 in f32, selects 0.
GLSL.std.450 RoundEven matches that and is the rule Vulkan uses for its own
array layer. The floor(z + 0.5) came in with boykopovar#668.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants