Skip to content

d3d12: validate CreateUnorderedAccessView descriptors for buffers and textures - #5327

Open
Taffyyy-ops wants to merge 2 commits into
ItsNotRexed:mainfrom
Taffyyy-ops:feat/d3d12-uav-validation
Open

Taffyyy-ops wants to merge 2 commits into
ItsNotRexed:mainfrom
Taffyyy-ops:feat/d3d12-uav-validation

Conversation

@Taffyyy-ops

@Taffyyy-ops Taffyyy-ops commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #5256 (comment) (new D3D item: CreateUnorderedAccessView descriptor validation)

What and why

Completes descriptor-creation validation for ID3D12Device::CreateUnorderedAccessView (alongside DSV #5163, RTV #5248, SRV #5275, and CBV #5242). All checks live directly in write_buffer_uav, CreateUnorderedAccessView, and write_image_descriptor without modifying image_descriptor, so this does not conflict with #5275.

Buffer UAVs (write_buffer_uav):

  • Reject unknown D3D12_BUFFER_UAV_FLAGS bits (Flags & ~D3D12_BUFFER_UAV_FLAG_RAW).
  • Reject D3D12_BUFFER_UAV_FLAG_RAW with DXGI_FORMAT_UNKNOWN or any format other than DXGI_FORMAT_R32_TYPELESS.
  • Reject non-zero StructureByteStride on raw and typed buffer UAVs (D3D12 requires StructureByteStride == 0 when not structured).
  • Reject NumElements == 0 (matching CreateShaderResourceView).
  • Use overflow-safe element-range and counter-offset bounds checks (view.NumElements > max_elements - view.FirstElement, c->desc().Width - view.CounterOffsetInBytes >= 4).

Texture UAVs (CreateUnorderedAccessView & write_image_descriptor):

  • Reject multisampled textures (SampleDesc.Count > 1).
  • Reject TYPELESS effective formats (both desc == nullptr on a TYPELESS texture and explicit TYPELESS desc->Format).
  • Reject sRGB (R8G8B8A8_UNORM_SRGB, B8G8R8A8_UNORM_SRGB, B8G8R8X8_UNORM_SRGB, BC*_UNORM_SRGB), depth-stencil, block-compressed (bf.block != 1), and 96-bit (fmt.rgb96) texture UAV formats.
  • Validate ViewDimension against r->desc().Dimension and reject unsupported default dimensions (TEXTURE3D, TEXTURE1D arrays) cleanly.
  • Validate MipSlice < MipLevels, PlaneSlice == 0, and for TEXTURE2DARRAY reject ArraySize == 0, FirstArraySlice >= DepthOrArraySize, or any ArraySize > DepthOrArraySize - FirstArraySlice (including UINT_MAX, since D3D12 only defines -1/UINT_MAX as an "all remaining slices" sentinel for D3D12_TEX3D_UAV::WSize, not D3D12_TEX2D_ARRAY_UAV::ArraySize) instead of silently clamping. Preserves explicit TEXTURE1D views on 1D texture arrays (slice 0, d3d12_texture1d_test).
  • Guard storage-view layer bounds in write_image_descriptor before calling image_descriptor so storage views cannot be silently clamped, return bool (failing without writing T# words if state_.bind_image fails on a bindless device for storage views), and only call state_.context->note_storage_image(r->id()) when a storage descriptor was actually written.

Tests (runtime/test/d3d12_uav_validation_test.cpp, registered as d3d12_uav_validation):

  • Verifies positive controls (structured with/without counter, raw, typed buffer UAVs; 1D, 1D-array slice-0, 2D, bounded 2D-array subranges, R8G8B8A8_TYPELESS -> R8G8B8A8_UINT, and R9G9B9E5_SHAREDEXP texture UAVs with bindless_hash_lookup != 0).
  • Verifies every rejected case (including TEXTURE2DARRAY with ArraySize == UINT_MAX) zeroes a 0xA5-pre-filled descriptor slot and allocates no bindless 2D image slot.

Testing

  • AI-assisted contribution.
  • Environment: Windows 11, MSVC 19.44 (VS 2022 Build Tools), Ninja (RelWithDebInfo), Vulkan device Intel(R) RaptorLake-S Mobile Graphics Controller (note: LEONIDA_VK_DEVICE=NVIDIA did not enumerate the laptop's NVIDIA dGPU in this Vulkan session).
  • Built cmake --build runtime/build (0 errors).
  • Ran ctest --test-dir runtime/build -R "^(d3d12_uav_validation|d3d12_descriptor|d3d12_uav_clear|d3d12_texture1d|d3d12_r9g9b9e5_uav)$" --output-on-failure: 5/5 passed (d3d12_texture1d, d3d12_uav_clear, d3d12_uav_validation, d3d12_r9g9b9e5_uav, d3d12_descriptor).
  • Ran python -I scripts/check_no_game_data.py: passed (exit code 0).
  • No game files used (LEONIDA_GAME_DIR and LEONIDA_MOVIE unset).

@thehollowedone

thehollowedone commented Nov 23, 2026 •

Copy link
Copy Markdown

Independent NVIDIA check of 05e7ac18 on 3faac557. Windows 11 x64 build 26300, RTX 3080 Ti / 621.35, MSVC 19.44.35230, Windows SDK 10.0.26100.0, Vulkan SDK 1.4.363.0, LLVM MC 23.1.6, RelWithDebInfo.

  • Stock d3d12_buffer_uav passed. Both synthetic modules passed spirv-val. All 21,394 values matched the CPU and native D3D12 references on the same adapter.
  • The focused d3d12_uav_validation test passed valid structured, raw, and typed buffer views and valid texture views, with exact GPU clear readback. The valid RGB9E5 descriptor used the mutable R32_UINT fallback.

Both CTests passed with zero skips or Vulkan validation errors. The scratch descriptor fixture ran the cases that this revision treated as valid, using the exact PR runtime code. That included the 2D-array slice-count case that Flyander flagged and the author corrected in a later revision. Review finding · Author fix

AI-assisted.

@Flyander

Copy link
Copy Markdown
Contributor

The positive UINT_MAX case appears to accept an invalid 2D-array UAV range.

Repro with the synthetic resource already created in this test (4 array slices):

  1. Set ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY and Texture2DArray = {0, 2, UINT_MAX, 0}.
  2. Call CreateUnorderedAccessView.
  3. The new test expects the descriptor to be valid with first_layer == 2 and layer_count == 2; the guard exempts UINT_MAX, and image_descriptor clamps it to the two remaining slices.

The D3D12 contract describes D3D12_TEX2D_ARRAY_UAV.ArraySize as a slice count. The explicit “all remaining slices” sentinel is documented for D3D12_TEX3D_UAV.WSize, not ArraySize (2D array UAV, 3D UAV). Here the requested count is UINT_MAX (4,294,967,295) with only two slices available, so this should follow the out-of-range rejection path rather than be accepted as an implicit remaining-slices sentinel.

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

AI-assisted independent readback at 05e7ac1. On the RTX 5070 Vulkan shim (driver 616.56) and native D3D12 WARP, ArraySize=UINT_MAX cleared exactly the two remaining layers from layer 2 of a four-layer R32_UINT texture. Both UINT_MAX and an explicit count of two matched all 672 expected texels, including neighboring layers and mips. WARP’s debug info queue was empty and Vulkan validation reported zero errors. This resolves observed behavior on these backends. A portable D3D12 guarantee remains unverified.

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

Configured b5327 with vcvars64 + Ninja, -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON -DVulkan_INCLUDE_DIR=C:/VulkanSDK/1.4.363.0/Include -DVulkan_LIBRARY=C:/VulkanSDK/1.4.363.0/Lib/vulkan-1.lib; built d3d12_uav_validation_test. RTX 5070 Ti, driver 621.24.

ctest -R '^d3d12_uav_validation$'                                                 -> 1/1 Passed (0.44 s)
LEONIDA_VK_DEVICE='RTX 5070 Ti' LEONIDA_VK_VALIDATION=1 d3d12_uav_validation_test.exe
  -> PASS (buffer & texture UAV validation + GPU clear readback), exit 0, no VUID
ctest -R '^(d3d12_vulkan_device|d3d12_uav_clear|d3d12_short_uav_clear)$'          -> 3/3 Passed
git diff --check                                                                  -> clean

The added rules follow D3D12 and the existing SRV checks: raw requires R32_TYPELESS and the RAW flag, non-structured requires StructureByteStride == 0, NumElements > 0, overflow-safe FirstElement/NumElements and counter ranges, and the texture side rejects MSAA, TYPELESS, sRGB, depth, block-compressed, 96-bit and B8G8R8X8 formats plus out-of-range mip/plane/array views and default 3D/1D-array dimensions. Invalid UAVs are checked for a zeroed slot and no bindless slot consumed; the UINT_MAX 2D-array positive case is clamped by image_descriptor (line 4023).

Verdict: good.

@Taffyyy-ops

Copy link
Copy Markdown
Contributor Author

Thanks for catching that, @Flyander! Good call — D3D12_TEX2D_ARRAY_UAV::ArraySize does not use UINT_MAX/-1 as an "all remaining slices" sentinel (unlike D3D12_TEX3D_UAV::WSize).

Fixed in d4c58549:

  • Removed the arr.ArraySize != UINT_MAX exemption in CreateUnorderedAccessView (D3D12_UAV_DIMENSION_TEXTURE2DARRAY), so ArraySize == UINT_MAX is rejected as out-of-range and leaves the descriptor slot zeroed.
  • Added a storage-view layer-bounds guard in write_image_descriptor before calling image_descriptor so storage views cannot be silently clamped.
  • Updated runtime/test/d3d12_uav_validation_test.cpp to move TEXTURE2DARRAY with ArraySize == UINT_MAX (both non-zero FirstArraySlice and slice 0) into the rejected cases (expect_rejected, checking zeroed slot + no bindless 2D slot allocated), while keeping bounded TEXTURE2DARRAY subranges ({1, 1, 2, 0} and {0, 2, 2, 0}) as positive controls.
  • Also cleaned up the PR description formatting.

Verified on Windows 11 (MSVC 19.44, Ninja RelWithDebInfo, Intel(R) RaptorLake-S Mobile Graphics Controller — LEONIDA_VK_DEVICE=NVIDIA did not enumerate the dGPU in this session):

  • ctest --test-dir runtime/build -R "^(d3d12_uav_validation|d3d12_descriptor|d3d12_uav_clear|d3d12_texture1d|d3d12_r9g9b9e5_uav)$" --output-on-failure: 5/5 passed
  • python -I scripts/check_no_game_data.py: passed

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested d4c5854 merged onto main 3faac55 on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 179/179.
  • d3d12_uav_validation: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

@Taffyyy-ops
Taffyyy-ops force-pushed the feat/d3d12-uav-validation branch from d4c5854 to 9543b2c Compare November 23, 2026 15:19
@Taffyyy-ops

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto upstream/main (1cc6e3c12 -> 9543b2c18), resolving the merge conflicts in runtime/shims/d3d12_vulkan_device.cpp (keeping rgba8_any_order, the note_shader_image rename, and the sparse_image() tracking in write_image_descriptor alongside our UAV validation checks).

Verified on Windows 11 (MSVC 19.44, Ninja RelWithDebInfo, NVIDIA GeForce RTX 5060 Laptop GPU):

  • cmake --build runtime/build: passed (0 errors)
  • ctest --test-dir runtime/build -R "^(d3d12_uav_validation|d3d12_descriptor|d3d12_uav_clear|d3d12_texture1d|d3d12_r9g9b9e5_uav|d3d12_view_slices)$" --output-on-failure: 6/6 passed
  • python -I scripts/check_no_game_data.py: passed

@465d6a0eac

Copy link
Copy Markdown
Contributor

Re-tested the rebased head 9543b2c18, merged onto main 8575d36ed, on Windows 11 (build 26300), RTX 3070 Ti (Ampere, driver 610.88): pass. Build: MSVC 19.44.35230, Windows SDK 10.0.26100, RelWithDebInfo with Ninja, Vulkan SDK 1.4.363. It merges cleanly and builds with no errors.

AI-assisted (Claude Code).

@NemesisDawn

Copy link
Copy Markdown

Independent Windows GPU check of 9543b2c: PASS.

Windows 11 x64, NVIDIA GeForce RTX 5080, MSVC 19.44,
Vulkan SDK 1.4.363.1.

CTest: d3d12_uav_validation passed (1/1), including buffer
and texture UAV validation and GPU clear readback.

Vulkan validation layers off. Synthetic inputs only.
AI-assisted setup and write-up.

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.

8 participants