Skip to content

D3D12: validate depth-stencil views like render-target views - #3594

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
s0las16:devin/desc-bug-f847748c
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
s0las16:devin/desc-bug-f847748c

Conversation

@s0las16

@s0las16 s0las16 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

claiming: CreateDepthStencilView missing view validation (new item; not in the current coordination issue #3381; open PRs #334 (SRV/UAV/CBV GFX10 descriptors) and #1644 (read-only DSVs) do not cover it). This account can't comment on the coordination issue, so the claim is here.

What

ID3D12Device::CreateDepthStencilView only bounds-checked the 1D/1DArray dimensions (via line_target_view) and rejected A8_UNORM. The TEXTURE2D, TEXTURE2DARRAY, TEXTURE2DMS, TEXTURE2DMSARRAY and null-desc paths wrote a leonida::TargetView straight from the caller's mip and slice values: no mip or layer bounds, no resource-dimension match, no MSAA match, no D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL requirement and no view-format legality. An invalid depth view was silently recorded as valid; the sibling CreateRenderTargetView already makes all of these checks (d3d12_vulkan_device.cpp ~4889-4908).

Fix

Before storing the view, refuse (write a null TargetView) when:

  • the resource lacks D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL
  • a 2D view dimension is used on a non-TEXTURE2D resource
  • an MS view is created on a non-MSAA texture, or a non-MS view on an MSAA texture
  • the mip or layer range is out of bounds
  • the view format isn't a concrete depth format of the resource's own family (D16_UNORM, D32_FLOAT, D24_UNORM_S8_UINT, D32_FLOAT_S8X24_UINT for typed resources, also via UNKNOWN; or that depth format for the matching typeless resource)

Test

runtime/test/d3d12_dsv_test.cpp builds a standalone device and DSV heap, creates mipmapped/arrayed depth textures, a 4xMSAA depth texture, a colour texture and an R32_TYPELESS texture, then asserts valid views write a non-null TargetView while refused ones write a null one (out-of-bounds mip/slice, MSAA and dimension mismatch, non-depth resource, wrong-family format). It fails on main (out-of-bounds mip DSV should be null) and passes with this change.

Tested

Cross-compiled on Linux with clang-cl 21 + lld-link against a Windows SDK 10.0.26100 winsysroot (xwin + NuGet SDK packages); ran under Wine 11.0 on Mesa llvmpipe (LLVM 15, 256-bit): d3d12_dsv FAIL -> PASS. MSVC/Visual Studio was not run (no Windows toolchain here) and no real GPU was tested. No game data used: all resources and view descs are hand-built. AI-assisted (Devin).

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 (n/a: no translator change).

The TEXTURE2D/TEXTURE2DARRAY/TEXTURE2DMS/TEXTURE2DMSARRAY and default
paths wrote a TargetView with the caller's mip and slice range without
any of the checks the render-target-view path makes: no mip or layer
bounds, no dimension or sample-count match, no ALLOW_DEPTH_STENCIL
requirement and no view-format legality. An invalid view was silently
recorded as valid.

Give every depth view the same checks: the resource must be a
depth-capable texture, the view dimension must match the texture's
dimension and sample count, the mip and layer range must fit inside it
and the view format must be a concrete depth format of the resource's
own family (D16/D32/D24S8/D32S8X24, or that depth format for the
matching typeless resource). A refused view writes a null TargetView.

Adds runtime/test/d3d12_dsv_test.cpp covering valid TEXTURE2D,
TEXTURE2DARRAY, TEXTURE2DMS, typeless and default views plus refused
out-of-bounds mip/slice, MSAA/non-MSAA mismatch, non-depth resource and
wrong-family format views.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

@Desamodeth Desamodeth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 00a0441 on Windows 11 build 26300, MSVC 19.44 Release, RTX 5070 (621.24). On main f847748 the same d3d12_dsv test fails: mip 3 of a three-mip texture produces a non-null DSV. On the PR all 15 descriptor cases pass. I also ran d3d12_depth_resolve with Vulkan validation enabled: depth/stencil readbacks matched native D3D12 in 31/31 comparisons on the default adapter and 26/26 on WARP. Both CTests passed. Synthetic resources only; no game files. AI-assisted.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 00a0441 on NVIDIA GeForce RTX 4090 (Ada), 621.14, Windows 11 (26200): the new d3d12_dsv test passes, but the change makes the existing d3d12_vulkan_device test fail.

MSVC 19.44 (VS 2022), Ninja, RelWithDebInfo. The branch is one commit on main f847748. For the comparison I swapped only runtime/shims/d3d12_vulkan_device.cpp between main's and this PR's version in the same tree and rebuilt, so everything else (including the new test) is identical.

main's d3d12_vulkan_device.cpp this PR's
d3d12_dsv_test.exe exit 1: FAIL: out-of-bounds mip DSV should be null (the negative control works) exit 0, PASS; also exit 0 with LEONIDA_VK_VALIDATION=1
d3d12_vulkan_device_test.exe, run alone, twice each exit 0, exit 0 exit 1, exit 1
ctest -R "^(d3d12|vk_|depth24|replay)" -j 6 (77/77 on the neighbouring branches of the same base) 76 of 77, d3d12_vulkan_device Failed

What fails, the same both times:

[rt] d3d12: depth-stencil view refused (format, flags, dimension, samples or range) 40
line 5928: list->Close() == S_OK

That is runtime/test/d3d12_vulkan_device_test.cpp:5923-5930, the multisampled depth-array case: a TEXTURE2DMSARRAY view of a 4-slice D32_FLOAT texture with FirstArraySlice = 3, ArraySize = 2. The test's comment above it states the current contract: "a range past the last slice is refused when the list runs", and it expects Close() == S_OK followed by ReplayResult::invalid_subresource from ExecuteCommandLists. The new check in this PR (view.layer_count > rd.DepthOrArraySize - view.first_layer, in the block added at d3d12_vulkan_device.cpp ~4949) now writes a null view when the view is created, so the ClearDepthStencilView on that slot makes Close() fail instead.

So the two disagree about when an out-of-range slice range is refused. Either that existing case is updated to the new behaviour (with what the host does for it; the test already compares many cases with the host on this machine), or the layer-range part of the new check leaves this case to the replay as before. I have not checked which of the two the PC runtime does.

d3d12_vulkan_device does not depend on the GPU for this path, so it should reproduce anywhere: ctest --test-dir runtime/build -C RelWithDebInfo -R "^d3d12_vulkan_device$" --output-on-failure.

Side note, unrelated to the PR: link.exe gave a transient LNK1163 on unrelated objects twice while rebuilding this tree; a plain rebuild got past it each time.

No game files. AI-assisted check (Claude Code).

@MatyEjsr

Copy link
Copy Markdown

Tested 00a04419 on NVIDIA GeForce RTX 3090 (Ampere, driver 32.0.16.1742 / 617.42), Windows 11 (10.0.26200), VS 2022 Build Tools (MSVC 19.44.35230.0, Ninja RelWithDebInfo), Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0: fail (d3d12_vulkan_device).

  • Incremental scripts\build_runtime.ps1 (147 actions): clean build on MSVC 19.44, exit 0.
  • Targeted ctest -R ^(d3d12_dsv|d3d12_depth_resolve|d3d12_d24_support|d3d12_typeless_rtv|d3d12_stencil_copy)$ (validation off): 5/5 pass (d3d12_dsv 0.88s).
  • Targeted ctest -R ^(vk_sync_validation|d3d12_dsv|d3d12_depth_resolve|d3d12_d24_support|d3d12_typeless_rtv|d3d12_stencil_copy)$ with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=~implicit~: 6/6 pass, vk_sync_validation confirmed active, 0 validation errors.
  • Full ctest (validation off, no game files): 132 passed, 1 failed (d3d12_vulkan_device), 8 skipped, 1 disabled (gameinput) (114.3s).

Failure details

d3d12_vulkan_device failed at runtime/test/d3d12_vulkan_device_test.cpp:5928:

[rt] d3d12: depth-stencil view refused (format, flags, dimension, samples or range) 40
line 5928: list->Close() == S_OK

Lines 5924–5931 of runtime/test/d3d12_vulkan_device_test.cpp intentionally create an out-of-bounds TEXTURE2DMSARRAY DSV (FirstArraySlice = 3, ArraySize = 2 on a 4-slice DXGI_FORMAT_D32_FLOAT texture), call ClearDepthStencilView(dsv0, ...), and expect list->Close() == S_OK followed by last_execute_result(queue.p) == ReplayResult::invalid_subresource. With the new CreateDepthStencilView bounds check refusing that view and writing a null TargetView, ClearDepthStencilView rejects the null view during recording and list->Close() fails at line 5928.

That existing test block in d3d12_vulkan_device_test.cpp will need to be updated to match the new view-creation refusal (and any replay-side invalid_subresource coverage tested directly if still desired).

AI-assisted.

@zzEmmazz

Copy link
Copy Markdown
Contributor

Tested 00a04419, applied alone on main f847748c: fails. The new test passes, but the change breaks d3d12_vulkan_device on both GPUs, and it refuses two kinds of view that Windows D3D12 accepts.
Windows 11 (26200), MSVC 14.50 (VS 2026), Ninja RelWithDebInfo. Two GPUs: Ryzen 7 9700X iGPU (RDNA2, AMD 32.0.21042.62) and RTX 3070 (NVIDIA 610.74), picked with LEONIDA_VK_DEVICE.

  • New test and its CMake lines only, on main's d3d12_vulkan_device.cpp: d3d12_dsv fails with FAIL: out-of-bounds mip DSV should be null (hr=0x00000000) on both GPUs. So the test guards the change.
  • Whole PR, full build with MSVC (no errors), ctest -R "^d3d12_": 58 tests per GPU. d3d12_dsv passes on both. d3d12_vulkan_device fails on both, twice each (in the full run and run again on its own), with line 5928: list->Close() == S_OK. On clean f847748c the same test passes on both GPUs.
    • That check (d3d12_vulkan_device_test.cpp, the TEXTURE2DMSARRAY block around line 5920) creates a DSV whose slice range runs past the texture and expects the list to close and then be refused when it runs (ReplayResult::invalid_subresource). With this PR the view is null at creation, so the ClearDepthStencilView through it makes Close fail instead. The PR and that test disagree about where an out-of-range slice range is refused; one of them has to change.
    • The other 56 d3d12_* tests: passed or skipped as on main (d3d12_first_light skipped on both, d3d12_rgb9e5_rtv skipped on the RTX 3070).
  • Host check, independent of the layer: a small D3D12 program with the debug layer on (no layer of ours), same textures as the new test, counting the error messages each CreateDepthStencilView leaves in the info queue. RTX 3070, Radeon iGPU and WARP gave the same answers:
view host debug layer this PR
test cases 0-4 (valid ones) accepted written
5 mip 3 of 3; 6 first slice 4 of 4; 7 five slices of 4; 13 MSAA first slice 2 of 2 error (message id 48) null
9 TEXTURE2D on an MSAA texture error (46) null
10 DSV on a render-target texture; 11 D24S8 on D32; 12 D24S8 on R32_TYPELESS error (47) null
14 TEXTURE1D on a 2D texture error (49) null
8 TEXTURE2DMS on a non-MSAA texture accepted, no message null
TEXTURE2DARRAY, first slice 0 or 1, ArraySize = -1 accepted, no message null
TEXTURE2DMSARRAY, first slice 0, ArraySize = -1 accepted, no message null
null description on an MSAA depth texture accepted written
null description on R32_TYPELESS error (46) null

The "this PR" column for the rows outside the test was measured by adding those calls to a local copy of d3d12_dsv_test (same result on both GPUs). The accepted rows were each run alone on a fresh device, and GetDeviceRemovedReason stayed S_OK; an erroring call (case 5) also removes the device under the debug layer.

  • So three refusals go further than the host: ArraySize = -1 ("all remaining slices") on both array dimensions, and TEXTURE2DMS on a single-sample texture, which the new test asserts as case 8. On main these views are written (read from the code, not run).
  • CreateRenderTargetView on main has the same layer_count > DepthOrArraySize - first_layer rule this copies; the host also accepts a TEXTURE2DARRAY RTV with ArraySize = -1 (0 messages), so that one is in main already.

Not checked: Wine or llvmpipe; LEONIDA_VK_VALIDATION=1; tests outside ^d3d12_; what a draw or clear through the three host-accepted views does on the host.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested 00a04419 on Windows 11, MSVC 19.44.35228, RTX 5070 Ti.

Configuration: the -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF -DBUILD_TESTING=OFF recipe does not generate d3d12_dsv_test (runtime/CMakeLists.txt:11-12). Reconfigured the same private build with both flags ON.

New test: d3d12_dsv_test -> 15 checks, 15 pass, 0 fail, ctest 1/1. The 5 accepted / 10 refused cases match the documented rules (four depth families, typeless rejected, UNKNOWN -> resource format, ALLOW_DEPTH_STENCIL, 2D dimension/sample match, mip/layer bounds). D3D12_DEPTH_STENCIL_VIEW_DESC has no PlaneSlice, so the "plane slice" case in the description does not exist; no positive 1D or positive UNKNOWN case either.

Blocking regression in d3d12_vulkan_device_test: runtime/shims/d3d12_vulkan_device.cpp:4974-4975 rejects a TEXTURE2DMSARRAY range at creation (FirstArraySlice=3, ArraySize=2 of 4), but runtime/test/d3d12_vulkan_device_test.cpp:5924-5931 expects that descriptor to be stored and refused at replay. The test now fails at :5928 list->Close() == S_OK; d3d12_vulkan_device_test.exe exits 1 and ctest is 1/2 (50%). The equivalent typed-RGBA8 RTV at :5995-6003 still defers because the RTV range check at d3d12_vulkan_device.cpp:4874-4913 only bounds-checks the typeless/cast branch, so the new DSV check is also stricter than the RTV parity the description claims.

Please either move the DSV range check to replay like the RTV path, or update the existing contract test and align the RTV check with it. The new DSV test itself is green and needs no change; no game files.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 00a0441 on Windows 11, MSVC (VS 2022 Build Tools) RelWithDebInfo, RTX 5080 (driver 616.92) and AMD Radeon iGPU gfx1036 (driver 26.10.1): ctest -R "^(d3d12_dsv|d3d12_vulkan_device|d3d12_depth.*)$" (LEONIDA_VK_DEVICE set per run, device line confirmed, VK_LOADER_LAYERS_DISABLE=~implicit~, no validation layer installed) — fail on both, 2/3 each.

  • d3d12_dsv: PASS: d3d12_dsv_test (CreateDepthStencilView validation) on both GPUs. This is the first MSVC build and real-GPU run of it.
  • d3d12_depth_resolve: passed on both.
  • d3d12_vulkan_device: failed on both, with the same single line:
    line 5928: list->Close() == S_OK
    
    That is the existing TEXTURE2DMSARRAY block in d3d12_vulkan_device_test.cpp. It creates a DSV with FirstArraySlice = 3, ArraySize = 2 on a 4-slice 4x depth array and expects the view to be captured, the list to close, and the out-of-range clear to be refused at execute (invalid_subresource). With this PR the view is nulled in CreateDepthStencilView, so the clear makes Close() fail first. On main f847748 the same test passes on both GPUs on this machine. Either that block's expectation or the new mip/layer check needs to change.

No negative control run.

AI-assisted.

@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 00a0441 (on its base main f847748) on NVIDIA GeForce RTX 5070, driver 621.01, Windows 11 Pro (26300), MSVC 14.44 (VS 2022 Build Tools, Ninja, RelWithDebInfo), Vulkan SDK 1.4.363, LEONIDA_VK_VALIDATION=1: one failure, d3d12_vulkan_device, which passes on main f847748 on the same machine.

  • Build: exit 0.
  • ctest -E loader -j 4: 128/129. The new d3d12_dsv and d3d12_draw pass; d3d12_vulkan_device fails:
    [rt] d3d12: depth-stencil view refused (format, flags, dimension, samples or range) 40
    line 5928: list->Close() == S_OK
    
  • Cause: the block at runtime/test/d3d12_vulkan_device_test.cpp ~5896–5930 ("a range past the last slice is refused when the list runs") makes a TEXTURE2DMSARRAY DSV of slices 3 and 4 of a 4-slice 4x D32 texture. It expects the view to be accepted at CreateDepthStencilView and the execute to fail with invalid_subresource. With this PR the range check (view.layer_count > rd.DepthOrArraySize - view.first_layer) refuses the view at creation. The clear then records nothing, and Close returns E_INVALIDARG instead of S_OK.
  • One of the two needs to change: update that block to the new behaviour (refused at creation, Close fails), or leave the slice-range check to execute time as main does. The format, flag, dimension and sample checks don't touch it.

AI-assisted (Claude Code).

@ricardotanaka

Copy link
Copy Markdown

Also tested 00a04419 (merged onto main f847748c) on AMD Radeon RX 7600 (RDNA3), AMD driver 26.11.2, Windows 11 26200.9457, MSVC 19.44 RelWithDebInfo: same result as the checks above. d3d12_dsv passes; d3d12_vulkan_device fails with line 5928: list->Close() == S_OK (full ctest 140/141). One more GPU vendor and generation for the record; nothing new beyond that.

No game files. AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 718f105 into ItsNotRexed:main Nov 23, 2026
2 of 4 checks passed
@ItsNotRexed

Copy link
Copy Markdown
Owner

Merged and then reverted on main (34af82b): refusing the view in CreateDepthStencilView makes the existing d3d12_vulkan_device_test fail. It records a DSV over slices 3-4 of a 4-slice MS array and expects Close() to succeed and ExecuteCommandLists to report invalid_subresource (line 6154). Please reconcile the two, by measuring what the PC runtime does or by updating that test with the evidence, and open the PR again on top of main.

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
This reverts commit 718f105, reversing
changes made to 9aa16b2.
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.

10 participants