graphics: read depth images through integer T# with a shader-side conversion (CP2077 now can reach the main menu) #1799
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!1799
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1799/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?
@ArtyomZabroda on GitHub · 2026-10-21 18:58 UTC · original
ArtyomZabroda:pr/depth_as_uint→main· +1696 −1112 · 14 files · 2 commits · closed without mergingSorry about the AI generated description, most of the code was written by myself, I don't have time today to write the description myself. Feel free to ask questions about the code in comments.
This pr combined with #1765 should make cp2077 reach the main menu.
Fixes #182
Before: a shader can read a depth target through an integer T#: k16UInt over a D16 target, or k32UInt over a D32 target. The texture cache keeps the depth VkImage at that address, and Vulkan allows only a depth view on it.
For k16UInt, ValidateSampledDepthBinding exits with unsupported sampled depth image.
For k32UInt, FindView silently swaps the R32Uint view for the D32 view, while the shader still declares a uint image. This violates VUID-vkCmdDraw-format-07753.
Float reads (k16UNorm/k32Float) of a depth image ask for a color view (R16Unorm/R32Sfloat) and depend on the same silent swap in FindView.
After: an integer read of an existing depth image with the same element size selects a shader permutation. That permutation samples the depth view as float and converts the result in the shader:
k16UInt → uint(roundEven(x * 65535));
k32UInt → bitcast.
Float reads of a depth image request D16_UNORM/D32_SFLOAT explicitly. IsFormatDepthCompatible accepts only depth formats.
Relation to Cyberpunk 2077 (PPSA04029 v2.120)
Cyberpunk crashed right after the intro video, before the main menu:
unsupported sampled depth image: resource=1 encoding=1 view=0 class=1 numeric=2 dimension=3 mip_mode=0
read=1 written=0 atomic=0 compare=0 guest_format=11 swizzle=0x204 image_format=124 view_format=74
image_layers=1 descriptor_type=13 base_array=0 depth=0 descriptor_pitch=2048 target_pitch=2048
addr=0x0000001cd3e30000 size=0x0000000000800000
dwords=1cd3e300,c0b00000,01ffc1ff,d1800204,00000000,00700000,00280000,001cd463
in src/graphics/host_gpu/renderer/pipeline/descriptors.cpp:209
What the values mean:
The T# is k16UInt (guest_format=11). It is a 2048×2048 kColor2DArray (descriptor_type=13) with tile mode kDepth ((dword3 >> 20) & 0x1f = 0x18) and swizzle 0x204 = (R, 0, 0, 1).
The shader declares a uint image (numeric=2). It reads the raw 16-bit depth values of a depth buffer the game rendered earlier.
The texture cache already holds a D16_UNORM depth image (image_format=124) at that address. The renderer requested an R16_UINT view (view_format=74).
IsFormatDepthCompatible did not list R16Uint, so the view check failed (view=0) and the emulator exited.
The existing raw_d16_texture path (
22bdafec, PPSA04264) recreates a D16 image as a color R16Uint copy. It did not match this binding, so the depth image stayed bound.Adding R16Uint to IsFormatDepthCompatible only hides the error. FindView would then bind the D16 view, and the shader would read normalized floats in [0, 1] as uint bits. The pass would get wrong values (the menu then showed rendering artifacts), and the spec violation above would remain.
With this change, the permutation for that draw is specialized with DepthReadConversion::DepthAsUInt16:
the renderer binds the existing D16 image through a D16 view;
the shader samples it as float and recovers the exact 16-bit value with roundEven(x * 65535);
the swizzle (R, 0, 0, 1) is applied in the shader after the conversion, so the constant 1 stays an integer 1.
The game no longer stops at this error and reaches new-game creation. That run also had an unrelated local kernel fix needed to boot the game. A later crash during new-game creation, shader image has an invalid descriptor type or mip count for an MSAA storage image (spirvEmitterModule.cpp:223), is unrelated and not addressed here.
k32UInt over D32 is the same problem without a crash. It worked only because FindView silently swapped the view format. This PR handles it with the same mechanism.
How
IR. ShaderIR.h adds DepthReadConversion { None, DepthAsUInt16, DepthAsUInt32 } to ImageResource. ResourceMaterialization.h adds it to ResourceSpecialization::Image, so it becomes part of the permutation key.
Materialization. MaterializeResources takes HostResourceQueries. For a sampled read without depth compare, not 3D, with a k16UInt/k32UInt T#, it asks the texture cache whether a depth image with the same bytes per element starts at the T# address.
If yes, the image is declared Float, the T# swizzle moves into the shader, and paired samplers are forced to point filtering.
The decision uses the cache, not the tile mode: color render targets can also use kDepth tiling, and data that only the CPU or compute wrote lives in a color image, where a native uint read is already correct.
SPIR-V. UnpackImageTexel and UnpackImageGather convert D16 with FMul 65535 → RoundEven → ConvertFToU and D32 with OpBitcast, then apply the swizzle. The result is returned as uint.
Pipeline cache. PipelineCache receives the TextureCache and answers the query through the new TextureCache::DepthImageBytesPerBlock.
Renderer (ResolveTexture).
A conversion requests D16/D32.
A k16UNorm/k32Float read over an existing depth image of the same size also requests D16/D32. Without the cache check, a color texture with the same guest format would be recreated as depth.
The view swizzle is identity when the shader converts.
After FindImage, the renderer exits if the bound image is not a depth image of the expected size. The permutation is chosen before the draw's targets are resolved, so this catches a mismatch.
Views. IsFormatDepthCompatible accepts only D16_UNORM and D32_SFLOAT.
Not covered
FindView still replaces the view format with the image format. This is still needed for Z16 stored as D16S8, D24S8, D32S8 or D32.
Depth reads through a different-size or different format still exit: k8_8UNorm over Z16, k32SInt, k16SInt.
If the permutation is chosen before the depth image exists in the cache, the read stays uint. raw_d16_texture remains the fallback for k16UInt.
Testing
resource_materialization_tests. A new TestDepthReadConversionSpecialization covers:
k16UInt/Z16 → DepthAsUInt16 and k32UInt/Z32 → DepthAsUInt32;
no conversion when there is no depth image, the size differs, the T# is float, the read is 3D, or no host query is given;
the swizzle, point filtering, the binding kind, and a distinct permutation key.
shader_recompiler_compute_tests.
Updated cases that expected color views on depth images.
New R32 float depth-backed view.
New depth-tiled color sampled as color: a k32Float read of a color target with kDepth tiling stays color.
New color view on depth target.
Cyberpunk 2077 PPSA04029. The game passes the crash above.
Not tested: the bitcast and RoundEven values on a GPU. No dispatch test reads back converted values.
Before:


After:
@coderabbitai[bot] on GitHub · 2026-10-21 19:07 UTC · original · edited 2026-10-21 19:37 UTC
📝 Walkthrough
The change adds host depth-image metadata queries to resource specialization. Eligible sampled images can use D16 or D32 depth-read conversions, with corresponding descriptor view selection and unsigned shader-result conversion. Tests cover depth format resolution, conversion selection, filtering, and rejection cases.
Priority: ⬆️ High
Estimated code review effort:
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk: 🔵 Low · up to
18f7dShaders that mix these image types in one indirect table may fail to compile or return an incorrect value. The risk is narrow, but the emission path should be corrected before relying on it.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 5.47% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 128 functions across 13 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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.@ -760,0 +847,4 @@: img.numeric_class;};const bool mixed = std::ranges::any_of(image.indirect_resources, [&](uint32_t resource) {return result_class(resource) != result_class(mem.resource);@coderabbitai[bot] on GitHub · 2026-10-21 19:07 UTC · original
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42433
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 16155
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11607
Include native image type and depth conversion in
mixed.When an indirect table contains a plain unsigned image and a depth-converted image in different binding groups, both resources have result class
Uint, but their native result types differ.EmitIndexSwitchthen creates anOpPhiwithFloatandUintvalues, which violates SPIR-V type rules. The homogeneous path can also apply the root resource's conversion to a candidate with a different conversion mode.🐛 Suggested fix
📝 Committable suggestion
@coderabbitai[bot] on GitHub · 2026-10-21 19:07 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
cf9df07b-95a9-4876-8893-bb29115cb678📥 Commits
Reviewing files that changed from the base of the PR and between
c07ad621f2and18f7dd1d6f.📒 Files selected for processing: 14
src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/graphics/host_gpu/renderer/image/imageView.hsrc/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/host_gpu/renderer/pipeline/pipelineCache.cppsrc/graphics/host_gpu/renderer/pipeline/pipelineCache.hsrc/graphics/host_gpu/renderer/renderContext.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppsrc/graphics/shader/recompiler/ir/ShaderIR.hsrc/graphics/shader/recompiler/ir/Value.hsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.cppsrc/graphics/shader/recompiler/ir/passes/ResourceMaterialization.htests/ResourceMaterializationTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@Nmzik on GitHub · 2026-10-22 20:13 UTC · original
@ArtyomZabroda Credited Thanks!
Pull request closed