shader: apply texel offsets of image_sample*_o #1220
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1220
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1220/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@TheCruZ on GitHub · 2026-10-08 18:39 UTC · original
pr/sample-texel-offsets→main· +304 −12 · 4 files · 1 commits · closed without mergingSummary
Applies the texel offsets of
IMAGE_SAMPLE*_Oinstructions. In Grand Theft Auto V (PPSA04263) pixel shader0xe59f3254a91fedb0fetches its +1 texel neighbour this way and read the unshifted texel instead.Problem
The offset DWORD of the
SAMPLE*_Ovariants was decoded into the address layout (layout.offset) but never reached the SPIR-V sample instruction, so every offset sample read the texel at the unshifted coordinate.Change
ConstOffsetrange [-8, 7] become aConstOffsetimage operand (only emitted when an offset is non-zero; cube maps have no offsets).Offseton sample instructions, so other offsets shift the coordinates byoffset / sizeof the sampled mip: the explicit LOD, the LOD of the gradients of derivative samples, level zero, or the LOD queried withOpImageQueryLodplus the instruction's bias in pixel shaders, clamped to the view's last level.v_movof the offsets stays a constant and takes theConstOffsetpath.Testing
shader_recompiler_compute_tests: newImageSampleOffsetMovesTexelcase (also in the default run; it also checks runtime offsets onIMAGE_SAMPLE_L_Owith a LOD past the last level and onIMAGE_SAMPLE_D_O) and the--image-sample-offset-onlyselector, which also runsImageSampleA16OffsetKeepsTexelOffset32BitOnGpuandImageSampleAndGather. The default run andshader_cfg_testspass.AI usage
AI (Claude Opus 5.5) was used to investigate the issue and to implement the change. I reviewed the code, understand it, tested it as described above, and wrote this description myself, in general fully tested the game while doing few missions and playing in-game with my GTA V fork, i will be bringing PRs here after test them enough.
@coderabbitai[bot] on GitHub · 2026-10-08 18:46 UTC · original · edited 2026-10-08 21:09 UTC
📝 Walkthrough
Walkthrough
The SPIR-V emitter now supports image sampling offsets. It uses
ConstOffsetfor supported constant offsets and adjusts coordinates using mip dimensions for other offsets on non-cube images. Image offset addresses are masked by the execution predicate, and constant propagation simplifies matching nested selects. Compute tests cover constant and runtime offsets.Suggested reviewers:
nmzikPriority: ⬇️ Low
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
d9103Some offset samples can read the wrong texel, particularly with explicit LOD, gradients, bias, or unnormalized coordinates. Resolve the sampling mismatches before merging.
🚥 Pre-merge checks | ✅ 4 | ❓ 1
❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. (1 skipped: 1 too large.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-10-08 18:46 UTC · original · edited 2026-10-08 21:08 UTC
On
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppline 681 (outdated):🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Clamp the level before querying mip dimensions.
If an explicit LOD exceeds the image view’s last level,
level_u32can exceed its level count. Vulkan leaves theOpImageQuerySizeLodresult undefined in that case. The fallback then divides the offset by undefined dimensions. Clamp the query level to the view’s last available level beforeOpImageQuerySizeLod. (docs.vulkan.org)✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-10-08 18:46 UTC · original · edited 2026-10-08 20:53 UTC
On
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppline 704 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3323
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42294
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 35466
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42211
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 21020
🌐 Web query:
Khronos Vulkan specification sampler unnormalizedCoordinates texel coordinates restrictions💡 Result:
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 13814
Apply the offset in texel space for unnormalized samplers.
When
ForceUnormCoords()is set, Vulkan interprets sampling coordinates as texel coordinates. The fallback currently addsfloat(offset) / float(extent), which is valid only for normalized coordinates. For an unnormalized sampler, this can leave the sample on the original texel or produce a different sampled value instead of applying the requested texel offset.Select the coordinate-space conversion from the sampler mode. Add the signed offset directly for unnormalized samplers, and keep division by the mip extent for normalized samplers.
@ -821,0 +997,4 @@level = Binary(state, spv::OpFAdd, TypeF32(state), level,AddressF32(ctx, mem, *address, layout.bias));}}@coderabbitai[bot] on GitHub · 2026-10-08 18:46 UTC · original · edited 2026-10-08 21:08 UTC
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Derive fallback dimensions from the sample’s LOD inputs.
If a runtime offset accompanies
Grador instructionBias, this branch usesOpImageQueryLodto choose dimensions. That query models an implicit-LOD sample; it does not receive this sample’s explicit gradients or bias. The fallback can therefore divide by the wrong mip dimensions and shift the sampled texel incorrectly. Account for the actualGradorBiasinputs when selecting the dimensions. (registry.khronos.org)✅ Confirmed as addressed by @TheCruZ
@coderabbitai[bot] on GitHub · 2026-10-08 18:46 UTC · original · reviewed
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
3d74cb8d-83a7-4ea0-8f71-4fe06a15dfdf📥 Commits
Reviewing files that changed from the base of the PR and between
2f4b8a335dand d910353575d2a6e2c3d58768782553cff00eb25e.📒 Files selected for processing (4)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppsrc/graphics/shader/recompiler/frontend/translate/Memory.cppsrc/graphics/shader/recompiler/ir/passes/ConstantPropagation.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@TheCruZ on GitHub · 2026-10-08 20:53 UTC · original
On
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppline 704 (outdated):I'm leaving this one out. Whether a sampler uses unnormalized coordinates is a bit of the runtime S# (
ForceUnormCoords), which the shader does not see when it is translated; supporting it would mean passing that word into the shader (or specializing on it) for a combination I haven't seen in any title. Also, Vulkan does not allowConstOffset/Offsetat all on samples through a sampler withunnormalizedCoordinates, so the constant-offset path could not be made valid for that case either; a proper fix would have to emulate the whole unnormalized sample, which is out of scope for this PR.@coderabbitai[bot] on GitHub · 2026-10-08 20:53 UTC · original
On
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppline 704 (outdated):@TheCruZ, thanks for the clarification. My suggested coordinate-space switch was incomplete.ForceUnormCoordscomes from runtime S# state that the shader translator cannot inspect. Vulkan also forbidsConstOffsetandOffsetwithunnormalizedCoordinates.I accept leaving this unsupported combination outside this PR. Would you like a follow-up issue for unnormalized sampling with texel offsets?
✏️ Learnings added
You are interacting with an AI system.
@TheCruZ on GitHub · 2026-10-08 21:08 UTC · original
On
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppline 681 (outdated):Fixed in
3aa2037: the fallback clamps the level toOpImageQueryLevels - 1beforeOpImageQuerySizeLod.ImageSampleOffsetMovesTexelnow also samples withIMAGE_SAMPLE_L_Oat LOD 3 on a single-level image with a runtime offset.@ -821,0 +997,4 @@level = Binary(state, spv::OpFAdd, TypeF32(state), level,AddressF32(ctx, mem, *address, layout.bias));}}@TheCruZ on GitHub · 2026-10-08 21:08 UTC · original
Fixed in
3aa2037: derivative samples now pick the fallback level from their own gradients (log2 of the longer gradient in base-level texels), and in pixel shaders the instruction's bias is added to the queried LOD.ImageSampleOffsetMovesTexelalso coversIMAGE_SAMPLE_D_Owith a runtime offset.@coderabbitai[bot] on GitHub · 2026-10-08 21:08 UTC · original
On
src/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppline 681 (outdated):@TheCruZ, the supplied code clamps the query level toOpImageQueryLevels - 1beforeOpImageQuerySizeLod. This addresses the undefined-dimensions concern. The reported single-level image test covers the out-of-range explicit LOD case.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@ -821,0 +997,4 @@level = Binary(state, spv::OpFAdd, TypeF32(state), level,AddressF32(ctx, mem, *address, layout.bias));}}@coderabbitai[bot] on GitHub · 2026-10-08 21:08 UTC · original
@TheCruZ, thanks for the fix. The supplied code uses the sample’s explicit gradients to select fallback mip dimensions and adds the instruction bias to the queried pixel-shader LOD. This addresses both parts of the finding.The reported runtime-offset
IMAGE_SAMPLE_D_Otest covers the derivative path. I have not run the tests.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@overlordxrz-source on GitHub · 2026-10-12 02:26 UTC · original
@TheCruZ heads-up on this one. Since
984cfcebd("shader: emit native texel offsets using sample execution context", nmzik/netwarex, 10-11), main does the constant part itself:ConstOffsetwhen the offset is constant in the active lanes, which it resolves through the exec mask. For runtime offsets on samples it now callsctx.Fail("has a nonconstant sample texel offset"). So this PR conflicts now, and its runtime path is the piece main is still missing.NHL 27 (#1101) hits exactly that on compute shader
0x8ff7afec7c5ab786, so it's no longer just GTA V.I rebased only your coordinate-shift path onto
5b730d4. I kept main's ConstOffset/exec handling and left out your select folding, since main resolves exec its own way. I also left out the [-8, 7] range check: main currently emitsConstOffsetfor any constant in [-32, 31], so that part of yours still adds something and could go in separately. It's a single commit with you as co-author, plus a smaller version of yourImageSampleOffsetMovesTexeltest. How would you like to do it? Would you rather rebase #1220 yourself, should I push mine as a branch you can cherry-pick, or should I open it as a follow-up PR with credit to you? Whichever you prefer.@TheCruZ on GitHub · 2026-10-12 08:37 UTC · original
Please go ahead and open it as a follow-up PR. Your version is a better fit for main now: keeping main's ConstOffset/exec handling and dropping my select folding makes sense. Good to know NHL 27 needs the runtime path too.
I'll close #1220 once your PR is up and link it from here.
@overlordxrz-source on GitHub · 2026-10-12 08:55 UTC · original
@TheCruZ thanks, opened as #1446 with you as co-author. It keeps main's ConstOffset/exec handling and adds only your coordinate-shift path for runtime offsets, plus the smaller test. The [-8, 7] range check isn't in it; I can send that separately if you'd like.
@TheCruZ on GitHub · 2026-10-12 09:10 UTC · original
thanks, for the The [-8, 7] range check i think that we can just wait to see if any game really requires it
Pull request closed