graphics: keep image-to-image copies inside both images #1176
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!1176
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1176"
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?
@overlordxrz-source on GitHub · 2026-10-08 01:14 UTC · original
overlordxrz-source:graphics/clamp-image-copy-extent→main· +86 −6 · 3 files · 2 commits · openImage::CopyImageandImage::CopyImageWithBuffersize every mip level of the copy from the source image only. When the cache copies a larger image into a smaller one (for example a replacement whose descriptor asks for a smaller extent), the region runs past the destination. That is out of bounds forvkCmdCopyImage/vkCmdCopyBufferToImage: the validation layer reports it, and without the layers the copy writes past the destination image on the GPU.This clamps the copy to what fits in both images:
Image::CopyExtenthelper. Vulkan measures a copy region in source texels and scales it by the texel block ratio on the destination, so it converts the destination size into source texels before taking the minimum. For formats with the same block shape it is justmin(source, destination).CopyImagehad the same problem in the third dimension: it copied as many layers as the 3D image had slices (or the reverse) without checking the 2D image had that many layers. That count is clamped too.One thing I'm not sure about: when two block-compressed images differ in size, the clamped extent can end on a partial block of the smaller image. I haven't seen this case in a log.
Evidence
#1153 The Adventures of Elliot (build 2026-10-07-d2413fc, windowed mode), with the Vulkan SDK installed:
Both formats are 4 bytes per texel, so this is the plain
min(475, 455)case.The same game is reported in #1124 and #1042. #1124 stops earlier on a mesh shader
Not implemented, and #1042 reports broken graphics in game with no log, so I can't say this copy is the cause there. I'm not claiming this fixes the game's rendering.Testing
tests/ShaderRecompilerComputeTests.cpp:ImageTestAccessexposesCopyExtent, and a new check next to theCopyRowsone covers the #1153 sizes in both directions, equal and different block widths, a level that doesn't fit, and a zero block size.I couldn't run
shader_recompiler_compute_tests. It needs a Vulkan device, and my machine is aarch64 Linux with no Vulkan. Here is what I did instead:image.cppforx86_64-linux-gnuandx86_64-windows-gnu(mingw), and the updated test file forx86_64-linux-gnu, with clang (zig c++) at-Wall -Wextra. The changed lines add no warnings.CopyExtentcases (10) in a small native harness built fromimage.cpp. All pass. This only covers the extent math, not an actual copy on a GPU.src/lines are clean undersrc/.clang-format(clang-format 23.1.3)Not run with the validation layers or with the game.
AI assistance
An AI assistant helped me find the out-of-bounds copy from the issue log, write the change and the test, and run the builds above. I reviewed the code and the results.
@coderabbitai[bot] on GitHub · 2026-10-08 01:16 UTC · original · edited 2026-10-08 01:46 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
af216529-5f97-435d-8809-91fb488ff85f📥 Commits
Reviewing files that changed from the base of the PR and between
32ab50548bandc2f7a2651b.📒 Files selected for processing (2)
src/graphics/host_gpu/renderer/image/image.cpptests/ShaderRecompilerComputeTests.cpp🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.
📝 Walkthrough
Walkthrough
Image copies now calculate width and height using both images’ dimensions and texel-block sizes.
CopyImageandCopyImageWithBufferskip mip levels when either computed dimension is zero.CopyImagealso clamps layer counts for 2D-to-3D and 3D-to-2D copies. Tests cover extent bounds, block alignment, image edges, and zero block sizes.Priority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
c2f7aNo actionable risk introduced by this PR remains identified. Merge after normal checks.
🚥 Pre-merge checks | ✅ 4 | ❓ 1
❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 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 01:16 UTC · original · edited 2026-10-08 01:40 UTC
On
src/graphics/host_gpu/renderer/image/image.cppline 475 (outdated):🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Align extents when the destination truncates a compressed copy.
CopyExtentbounds the copy but can return a partial source block inside the source image. For example, source width 475, destination width 455, and four-texel blocks produce width 455. Vulkan requires a non-edge compressed source extent to be block-aligned. Correct both width and height without exceeding either image. (docs.vulkan.org)src/graphics/host_gpu/renderer/image/image.cpp#L474-L475: make the bounded extent respect the source block boundary when it does not reach the source edge.src/graphics/host_gpu/renderer/image/image.cpp#L360-L365: use the corrected extent for direct image copies.src/graphics/host_gpu/renderer/image/image.cpp#L524-L529: use the corrected extent for buffer-mediated copies.📍 Affects 1 file
src/graphics/host_gpu/renderer/image/image.cpp#L474-L475(this comment)src/graphics/host_gpu/renderer/image/image.cpp#L360-L365src/graphics/host_gpu/renderer/image/image.cpp#L524-L529✅ Confirmed as addressed by @overlordxrz-source
@coderabbitai[bot] on GitHub · 2026-10-08 01:16 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
ccda6d8b-8859-43d8-91e5-24898f247e27📥 Commits
Reviewing files that changed from the base of the PR and between
5a8808086aand32ab50548b.📒 Files selected for processing (3)
src/graphics/host_gpu/renderer/image/image.cppsrc/graphics/host_gpu/renderer/image/image.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@overlordxrz-source on GitHub · 2026-10-08 01:39 UTC · original
On
src/graphics/host_gpu/renderer/image/image.cppline 475 (outdated):Agreed, thanks. With 4x4 blocks, 475 -> 455 clamped to 455, which is neither a whole number of source blocks nor the source edge.
Fixed in "graphics: keep clamped compressed copies on block boundaries". CopyExtent now keeps a partial block only when the region reaches the source edge and the destination region is whole blocks or reaches the destination edge. Otherwise it rounds down to the source block size, so 475 -> 455 now copies 452. Width and height go through the same helper in CopyImage and CopyImageWithBuffer. CopyImageWithBuffer now skips a level with no whole block left, as CopyImage already did. In a case like this, the last partial block of the smaller image is not copied, because no valid region covers it.
I added compressed cases to the CopyExtent test. I also checked the helper against a brute-force model of the vkCmdCopyImage extent rules for every width up to 300 with 1x1/4x4 block pairs.
@coderabbitai[bot] on GitHub · 2026-10-08 01:40 UTC · original
On
src/graphics/host_gpu/renderer/image/image.cppline 475 (outdated):@overlordxrz-sourceThanks for the fix. The supplied code addresses this finding:CopyExtent(475, 455, 4, 4)returns452, and both copy paths skip levels with zero width or height.The source-edge exception also checks the destination block boundary or destination edge before retaining a partial block.
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.
@Nawid3333 on GitHub · 2026-10-18 19:07 UTC · original
I tested this PR's two commits (
32ab5054,c2f7a265), applied on763d3be3with my local test runners, on an RX 9070 XT and a Ryzen 7600X iGPU (AMD driver 26.8.1, Windows 11), with the Khronos validation layer from Vulkan SDK 1.4.363.0. Jusant (#1636) stops on the same kind of error: a 60x33x26 3D B10G11R11 image copied into a 54x33x26 one.TextureCache::FindImage, copied withTextureCache::CopyImage, and withImage::CopyImageWithBufferwhere the block sizes match). Without this PR the layer rejects 7 of 16 direct copies (vkCmdCopyImagedstOffset-00150, and dstOffset-00153 plus dstSubresource-07968 for a 16x16x8 3D image copied into a 2D array with 4 layers) and 6 of 11 buffer copies (vkCmdCopyBufferToImage07971 and 00207). The Jusant case gives the report's message word for word. With this PR it rejects none on either GPU, including the BC5 partial-block cases you weren't sure about (16x16 into 6x6, 6x6 into 16x16, 7x7 into 6x6), and in the four cases with a texel check the last copied texel matches the source.ImageTransitionStatewith your newCopyExtentchecks passes on the RX 9070 XT. On the iGPU it stops later, at "combined depth/stencil mip copy lost an aspect", with and without this PR (an AMD driver bug I reported to AMD).CopyExtentagainst a model of the Vulkan copy rules, for every pair of lengths from 1 to 1024 and block widths 1/1, 4/4, 4/1 and 1/4: the result is always valid for both images and the largest valid length.099de8de), it builds and passes the tests on Windows, Linux and macOS: https://github.com/Nawid3333/KytyPS5/actions/runs/39154477473Written with Claude Code (AI): it ran these tests on my PC; I read this before posting.
@Nawid3333 on GitHub · 2026-10-19 12:30 UTC · original
Main took a plain clamp for this in
440245f6(thestd::minof both extents inImage::CopyImage), so this PR conflicts with main now. I ran my copy matrix again one5f0ee7f, under the validation layer, on an RX 9070 XT and a Ryzen 7600X iGPU (same results on both):VUID-vkCmdCopyImage-srcImage-01728(the region ends inside a block). BC5 7x7 into 6x6 passed before440245f6.VUID-vkCmdCopyImage-dstOffset-00150(the block ratio is not applied).VUID-vkCmdCopyImage-dstOffset-00153andVUID-vkCmdCopyImage-dstSubresource-07968.CopyImageWithBuffer: the five cases that copy a 2D or 3D image into a smaller one (Jusant's images among them) and BC5 6x6 into 16x16, withVUID-vkCmdCopyBufferToImage-imageSubresource-07971ordstImage-00207.e5f0ee7f, with the conflict resolved by keeping yourCopyExtentcalls in place of the twostd::minlines: all of these pass, and every texel read back has the expected value.All cases (ok, or the number of the VUID the validation layer rejects the copy with; both GPUs give the same table)
07971(10 errors)07971079710172807971002070172800207,07971017280015000153,07968The buffer path is not run for the cases where the two formats have different block sizes:
CopyImageWithBufferstops on purpose there (itsEXIT_IFon different block sizes), with and without this PR.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.