Skip to content

Runtime: DEPTH_READ with a shader-resource state puts a set-0 texture in GENERAL - #7342

Closed
cynbun wants to merge 1 commit into
ProjectLeonida:mainfrom
cynbun:depth-read-srv-general
Closed

cynbun wants to merge 1 commit into
ProjectLeonida:mainfrom
cynbun:depth-read-srv-general

Conversation

@cynbun

@cynbun cynbun commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #7049 (comment)

What and why

A depth texture in the legacy state DEPTH_READ | PIXEL_SHADER_RESOURCE (or NON_PIXEL_SHADER_RESOURCE) was moved to VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL. With the bindless set 0, though, every sampled view is written in VK_IMAGE_LAYOUT_GENERAL. So a shader sampling that texture read an image that was not in its descriptor's layout (VUID-vkCmdDraw-None-09600 / VUID-vkCmdDispatch-None-09600).

  • Cause: runtime/src/vulkan_replay.h:115 (vulkan_access) merges depth_read with the shader-resource bits into DEPTH_STENCIL_READ_ONLY_OPTIMAL (line 170). image_layout (line 397) only moves SHADER_READ_ONLY_OPTIMAL to GENERAL when ResourceBinding::sampled_in_general is set. That flag is set on every texture when the bindless set exists (runtime/shims/d3d12_vulkan_device.cpp:2065), and set 0 writes every sampled view with GENERAL (d3d12_vulkan_device.cpp:1212). docs/d3d12_vulkan.md already listed this case as "not handled".

  • Spec: D3D12_RESOURCE_STATES, DEPTH_READ: "It can be combined with other read states (for example, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE), such that the resource can be used for the depth or stencil test, and accessed by a shader within the same draw call." Games keep their depth buffer in this state so they can test against it and also sample it (SSAO, fog, soft particles). That makes this part of the retail boot path once it renders.

  • Fix: a vulkan_access(states, const ResourceBinding&, conditional_rendering) overload. When sampled_in_general is set, any shader-resource bit makes the layout GENERAL. The legacy TransitionBarrier replay now calls it with the binding. These keep DEPTH_STENCIL_READ_ONLY_OPTIMAL:

    • DEPTH_READ alone;
    • a texture without set 0;
    • the enhanced-barrier DEPTH_STENCIL_READ layout. The enhanced barriers spec allows only ACCESS_DEPTH_STENCIL_READ in that layout; depth reads together with shader reads go through GENERIC_READ, which is GENERAL here already.

    The bool presentable form of vulkan_access is unchanged, and so is d3d12_state_map_test. The paragraph in docs/d3d12_vulkan.md is updated.

  • Not covered here: a draw that also binds the texture as its DSV. emit moves any bound DSV to DEPTH_STENCIL_ATTACHMENT_OPTIMAL for that draw. fix(runtime): support read-only DSV flags (READ_ONLY_DEPTH / READ_ONLY_STENCIL) #1644 makes a read-only DSV DEPTH_STENCIL_READ_ONLY_OPTIMAL instead. Sampling the same texture inside that draw would still need GENERAL (via image_layout) when the texture is in set 0. That belongs with fix(runtime): support read-only DSV flags (READ_ONLY_DEPTH / READ_ONLY_STENCIL) #1644, which this PR does not touch or conflict with.

Tested

Linux (kernel 7.2.8), NVIDIA GeForce RTX 5080, driver 615.78.08, GCC 16.2.1, -DLEONIDA_VULKAN_HEADERS_FETCH=ON -DLEONIDA_REQUIRE_VULKAN=ON. Validation runs used Khronos VK_LAYER_KHRONOS_validation 1.4.363 with LEONIDA_VK_VALIDATION=1, which turns on synchronization validation too.

  • New CPU test replay_depth_read_layout (no GPU). It runs a DEPTH_WRITE → X barrier and checks the emitted barrier and the tracked layout.
    • Fails on main for D32 → DEPTH_READ | PIXEL_SHADER_RESOURCE, D32 → DEPTH_READ | NON_PIXEL_SHADER_RESOURCE and D24S8 → DEPTH_READ | ALL_SHADER_RESOURCE: each goes to DEPTH_STENCIL_READ_ONLY_OPTIMAL where GENERAL is expected.
    • Passes with the fix.
    • Controls pass both before and after: PIXEL_SHADER_RESOURCE alone → GENERAL; DEPTH_READ alone → read-only depth; no set 0 → read-only depth.
  • New GPU part K in vk_command_replay_test. It replays a depth clear to 0.25 and a barrier into DEPTH_READ | NON_PIXEL_SHADER_RESOURCE on a D32 texture with sampled_in_general. Then a compute shader samples it through a plain SAMPLED_IMAGE descriptor written in GENERAL. The descriptor is not a runtime array, so the validation layer checks its layout. The shader is a hand-written GLSL compute shader (source in the comment), with its SPIR-V inline.
    • Before, validation reports vkCmdDispatch(): Cannot use VkImage ... with specific layout VK_IMAGE_LAYOUT_GENERAL (specified by sampled image descriptor ...) that doesn't match the previous known layout VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL (VUID-vkCmdDispatch-imageLayout-00344). The tracked-layout check fails as well, also without validation.
    • After: K: ... 16 texels sampled through a GENERAL descriptor, no validation errors; parts A–J unchanged.
  • Full ctest:
    • main efd37f878: 63/63 passed (shader_library_corpus and vk_sync_validation skipped).
    • This branch: 64/64 passed, same skips; the only difference is the new replay_depth_read_layout.
  • Under the validation layer (-R "^(vk_|replay_|d3d12_state_map)"):
  • python -I scripts/check_no_game_data.py on every changed file; tests/test_ctest_registration.py and the tests/test_docs_*.py checks pass.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

cc @shayderrr

🤖 Generated with Claude Code

… in GENERAL

A depth texture in the legacy state DEPTH_READ | (NON_)PIXEL_SHADER_RESOURCE was
moved to DEPTH_STENCIL_READ_ONLY_OPTIMAL, while every set 0 sampled view is
written in GENERAL (ResourceBinding::sampled_in_general), so a shader reading it
read an image outside its descriptor's layout (VUID-vkCmdDraw/Dispatch-None-09600).
D3D12 lets that state test against the depth buffer and sample it in the same
draw. vulkan_access now has an overload that takes the binding: with
sampled_in_general, any shader-resource bit makes the layout GENERAL. DEPTH_READ
alone, a texture without set 0, and the enhanced-barrier DEPTH_STENCIL_READ
layout (no shader-resource access) keep DEPTH_STENCIL_READ_ONLY_OPTIMAL.

replay_depth_read_layout_test checks the barrier and tracked layout without a
GPU; part K of vk_command_replay_test samples such a texture from a compute
shader through a GENERAL descriptor, which the validation layer flags before.

AI-assisted (Claude Code).

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

Copy link
Copy Markdown

Independent native Windows check of b65e86c1. RTX 3080 Ti, NVIDIA 621.35, MSVC 19.44, Vulkan SDK 1.4.363.0.

All six CPU replay_depth_read_layout cases passed. The focused part K of vk_command_replay transitioned the D32 image to GENERAL for DEPTH_READ | NON_PIXEL_SHADER_RESOURCE. All 16 texels read back as exactly 0.25 through a plain sampled-image descriptor in GENERAL. Both CTests passed with zero skips and zero Khronos core or synchronization validation errors.

AI-assisted.

@MartinIndra02

Copy link
Copy Markdown
Contributor

Independent Windows + GPU test of b65e86c13: pass.

Setup: Windows 11 x64, MSVC 19.44.35220 (RelWithDebInfo, Windows SDK 10.0.26100.0), NVIDIA GeForce RTX 3080 (driver 32.0.16.2124 / 621.24), Vulkan SDK 1.4.363.1, no game files.

  • replay_depth_read_layout_test.exe: OK (exit 0).
  • vk_command_replay_test.exe (both default and with LEONIDA_VK_VALIDATION=1): PASS (exit 0, 0 validation errors), including case K (D32 texture in DEPTH_READ | NON_PIXEL_SHADER_RESOURCE, 16 texels sampled through a GENERAL descriptor).

AI-assisted review/test.

@umnitus

umnitus commented Nov 23, 2026

Copy link
Copy Markdown

Independent native Windows GPU check at b65e86c on RTX 3060 12 GB, NVIDIA 621.14, clang-cl/xwin SDK 10.0.26100 and Khronos validation 1.4.363. replay_depth_read_layout and vk_command_replay both passed. Part K compared 16 D32 texels sampled through a GENERAL descriptor with zero validation errors. AI-assisted testing.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants