Skip to content

d3d12: reject null clear rects with count > 0 and validate CBV alignment/size - #5242

Open
DenRakEiw wants to merge 1 commit into
ItsNotRexed:mainfrom
DenRakEiw:fix/d3d12-clear-rects-cbv-validation
Open

DenRakEiw wants to merge 1 commit into
ItsNotRexed:mainfrom
DenRakEiw:fix/d3d12-clear-rects-cbv-validation

Conversation

@DenRakEiw

Copy link
Copy Markdown
Contributor

Claim: #5156 (comment)

Summary

  1. runtime/shims/d3d12_com.cpp (ClearRenderTargetView, ClearDepthStencilView):
    • Guard count && !rects with return check(false); so calling either clear with NumRects > 0 and pRects == nullptr marks the command list invalid (Close() returns E_INVALIDARG) instead of letting to_rects(count, nullptr) return an empty vector and silently trigger a whole-view clear.
  2. runtime/shims/d3d12_vulkan_device.cpp (CreateConstantBufferView):
    • Enforce D3D12 CBV placement and size rules: reject desc == nullptr, BufferLocation == 0, BufferLocation not 256-byte aligned (D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT), SizeInBytes == 0, SizeInBytes not 256-byte aligned, or SizeInBytes > 65536 (D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT * 16) by leaving the descriptor slot all-zero (null V#).
  3. runtime/test/d3d12_clear_cbv_parity_test.cpp & runtime/CMakeLists.txt:
    • Registered d3d12_clear_cbv_parity (SKIP_RETURN_CODE 77, FAIL_REGULAR_EXPRESSION "Validation Error;VUID-").
    • Verifies invalid ClearRenderTargetView(..., 1, nullptr) and ClearDepthStencilView(..., 1, nullptr) fail Close() with E_INVALIDARG and preserve previously cleared target pixels via GPU readback.
    • Verifies valid rect clears (NumRects = 1, valid pRects) succeed, preserve outside pixels, and match native Windows WARP byte-for-byte.
    • Verifies valid CBVs (256B-aligned address, 256..65536B 256B-aligned size) decode cleanly as GFX10 V# descriptors and all invalid CBV cases zero the full 32-byte slot.

Verification (Windows 11, LEONIDA_VK_VALIDATION=1, 0 Vulkan validation errors)

  • NVIDIA GeForce RTX 5090:
    • d3d12_clear_cbv_parity: Passed (0.75 s, 0 Vulkan validation errors)
    • d3d12_com: Passed (0.31 s)
    • d3d12_descriptor: Passed
    • d3d12_vulkan_device: Passed
    • d3d12_compute: Passed (0.55 s)
  • AMD Radeon(TM) Graphics (LEONIDA_VK_DEVICE=AMD):
    • d3d12_clear_cbv_parity: Passed (0.55 s, 0 Vulkan validation errors)
    • d3d12_com: Passed (0.28 s)
    • d3d12_descriptor: Passed (0.57 s)

AI-assisted (Gemini 4 Neon).

@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.

Head 7e88a76 · Windows 11, RTX 5070 Ti, LEONIDA_VK_DEVICE='RTX 5070 Ti', LEONIDA_VK_VALIDATION=1

  • Configured runtime with Ninja/RelWithDebInfo + BUILD_TESTING + LEONIDA_BUILD_WINDOWS_RUNTIME and Vulkan 1.4.363.0; built d3d12_clear_cbv_parity_test
  • ./d3d12_clear_cbv_parity_test.exe -> exit 0: "invalid clear rects rejected (targets preserved), valid rect clears match WARP, and CBV alignment/size validation passed (0 Vulkan validation errors)"
  • Regression: d3d12_compute_test -> exit 0 (CBV V# 64/64, OOB and negative controls unchanged); d3d12_com_test -> exit 0 (16 colour/depth/stencil texels matched)
  • git diff --check -> clean

The guards match D3D12 rules: count && !rects makes Close() return E_INVALIDARG without touching the target, and CBV creation rejects a null/zero/misaligned address and a zero, misaligned, or >65536-byte size. Existing tests only use 256-byte CBVs, so I found no caller depending on the previously accepted values.

Caveat: the test needs a Vulkan device (returns 77 otherwise), and the new validation-error counting only runs with LEONIDA_VK_VALIDATION=1, like neighbouring tests.

Verdict: merge-ready.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 7e88a76 on Intel UHD 770, driver 32.0.101.7088, Windows 11, MSVC 19.44, Khronos validation layer loaded (the device line reads "Intel(R) UHD Graphics 770 (validation on)"): d3d12_clear_cbv_parity_test builds and passes with 0 validation errors, "invalid clear rects rejected (targets preserved), valid rect clears match WARP, and CBV alignment/size validation passed (0 Vulkan validation errors)" (ctest d3d12_clear_cbv_parity, Test #119, 0.33 sec). It also prints "CreateConstantBufferView: 64/64 dwords matched for CBV A, 32 in-bounds + 32 OOB-zero matched at +128". ctest d3d12_com, d3d12_compute, d3d12_descriptor and d3d12_vulkan_device all pass. The AMD and NVIDIA runs from the description were not reproduced, and this GPU has a single queue.

AI-assisted.

@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.

Reviewed head 7e88a76 (vs main 3faac55).

Commands:

  • cmake -S runtime -B runtime/build-5242 -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON -DVulkan_INCLUDE_DIR=... -DVulkan_LIBRARY=...
  • ninja -C runtime/build-5242 d3d12_clear_cbv_parity_test
  • LEONIDA_VK_DEVICE='RTX 5070 Ti' d3d12_clear_cbv_parity_test.exe -> exit 0, prints "invalid clear rects rejected (targets preserved), valid rect clears match WARP, and CBV alignment/size validation passed (0 Vulkan validation errors)"
  • ctest -R d3d12_clear_cbv_parity -> 1/1 passed
  • Regression: ninja d3d12_compute_test d3d12_clear_test; compute exit 0 (CBV A/B and null-CBV slot checks unchanged).
  • git diff --check: clean.

Notes: d3d12_clear_test.exe fails with "5 color/depth format(s) differ from WARP", but it fails identically after swapping main's d3d12_com.cpp/d3d12_vulkan_device.cpp into this tree, so that is pre-existing and unrelated. The test reads back real pixels and fails Close() with E_INVALIDARG for count>0/null rects, so the guard cannot silently do nothing.

Verdict: pass, no blockers.

@SirKingg

Copy link
Copy Markdown
Contributor

Independent Windows + GPU verification of 7e88a76419f77c31d54bd0b623294c861b88e481 (Windows 11 Pro build 26300; NVIDIA GeForce RTX 4070 Ti, driver 610.88; VS 2022 Build Tools/MSVC 19.44.35230; Ninja 1.13.2; Vulkan SDK 1.4.363.0). AI-assisted validation; no game data used.

Configured runtime with Ninja/RelWithDebInfo, BUILD_TESTING=ON, and LEONIDA_BUILD_WINDOWS_RUNTIME=ON; built d3d12_clear_cbv_parity_test, d3d12_com_test, d3d12_descriptor_test, d3d12_vulkan_device_test, and d3d12_compute_test. Then ran CTest with LEONIDA_VK_VALIDATION=1 and LEONIDA_VK_DEVICE='NVIDIA GeForce RTX 4070 Ti' for d3d12_clear_cbv_parity|d3d12_com|d3d12_descriptor|d3d12_vulkan_device|d3d12_compute: 5/5 passed, no skips.

d3d12_clear_cbv_parity reported validation enabled and zero Vulkan validation errors. The test verified invalid RTV/DSV rect calls fail Close() with E_INVALIDARG and readback preserves the previous clears; valid rect readbacks matched expected inside/outside pixels. It also checked valid 256/512/65536-byte CBVs (including aligned address offsets), and null, zero, misaligned, and >64 KiB inputs produce zeroed descriptors. I separately confirmed EnumWarpAdapter and D3D12CreateDevice succeed on this host; the test's conditional WARP readback comparison therefore ran and passed. d3d12_com matched 16 color/depth/stencil texels; d3d12_compute reported CBV V# readback 64/64 with OOB and negative controls unchanged.

@XFlawless1

Copy link
Copy Markdown
Contributor

Adding the wider regression run: 7e88a764 merged onto main 3faac557 (clean merge), Windows 11 Pro 26300, MSVC 19.44, full scripts/build_runtime.ps1 build (RelWithDebInfo, LLVM_MC set so the shader-based tests are built), RTX 5080 (621.14), LEONIDA_VK_VALIDATION=1.

  • ctest -R "^d3d12_" -j 2: 69/69 passed, 0 Vulkan validation lines in the whole log. So the stricter clear-rect and CBV checks reject nothing that the other D3D12 tests use.
  • The 2 skips are environmental and unrelated: d3d12_rgb9e5_rtv (no RGB9E5 render targets on this adapter) and d3d12_first_light (needs LEONIDA_FIRST_LIGHT_DIR).
  • d3d12_clear_cbv_parity_test run directly: "invalid clear rects rejected (targets preserved), valid rect clears match WARP, and CBV alignment/size validation passed (0 Vulkan validation errors)".

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

@sh1shu1

sh1shu1 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7e88a76 on NVIDIA RTX 4070 SUPER (driver 616.92), Windows 11 26200, MSVC 14.44, Vulkan SDK 1.4.363, RelWithDebInfo: pass, no regression against main 3faac55.

  • ctest: 176/178 passed; dxgi_device, dxgi_swapchain fails on main 3faac55 too (not from this PR), same 9 skips as main 3faac55
  • d3d12_clear_cbv_parity: 1/1
  • run_gpu_tests (LEONIDA_VK_VALIDATION=1): 170/217 native, 201/217 with LEONIDA_W64_EMULATE=1, 0 failed; identical to main 3faac55
    AI-assisted (Claude Code, Claude Fable 5.1).

@Codejustcode12

Copy link
Copy Markdown

Tested 7e88a76 (base 3faac55, no other PRs) on NVIDIA GeForce GTX 1050 Ti (Pascal), driver 560.94, Windows 10 Enterprise LTSC 21H2 (19044), MSVC 14.44, Windows SDK 10.0.26100, Vulkan SDK 1.4.363, RelWithDebInfo + Ninja: pass. Synthetic only, no game files.

d3d12_clear_cbv_parity, d3d12_com, d3d12_compute and d3d12_descriptor: exit 0 in all of these runs: twice without and twice with LEONIDA_VK_VALIDATION=1 (device line "(validation on)"), 0 VUIDs. The new test prints "invalid clear rects rejected (targets preserved), valid rect clears match WARP, and CBV alignment/size validation passed".

Negative control, same tree, the new test rebuilt against main's layer:

  • both runtime/shims/d3d12_com.cpp and d3d12_vulkan_device.cpp from 3faac55: exit 1 at line 290, list->Close() == E_INVALIDARG (the null-rects clear is accepted)
  • only d3d12_vulkan_device.cpp from 3faac55, the clear guard kept: exit 1 at lines 429 and 477 (expect_null_cbv: the invalid CBV slot is not zeroed)
  • PR files restored: exit 0

So each half of the fix is caught by the test. The test stops at its first failure, so without the second control the CBV half would never have run on the old layer.

How: test executables run directly (not ctest).

AI-assisted (Claude Code).

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.

7 participants