Skip to content

feat(runtime): implement buffer CreateShaderResourceView (raw, structured, typed GFX10 V#) - #1773

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:ag/d3d12-buffer-srv
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:ag/d3d12-buffer-srv

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: claim

What and why

ID3D12Device::CreateShaderResourceView previously hit if (!r->image()) return stub("ID3D12Device::CreateShaderResourceView(buffer)");, leaving descriptor heap slots zeroed for all buffer SRVs (raw ByteAddressBuffer, StructuredBuffer, and typed Buffer<T>).

This implements D3D12_SRV_DIMENSION_BUFFER in runtime/shims/d3d12_vulkan_device.cpp:

  • Moves the D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE guard ahead of the buffer branch so it applies to buffers as well as textures.
  • Encodes a 16-byte GFX10 V# descriptor via leonida::gfx10::encode(Buffer) into the first half of the 32-byte slot (trailing 16 bytes zeroed):
    • Raw (D3D12_BUFFER_SRV_FLAG_RAW, DXGI_FORMAT_R32_TYPELESS, StructureByteStride == 0): stride = 0, num_records = NumElements * 4, GFX10_FORMAT_32_UINT, OobSelect::raw.
    • Structured (StructureByteStride > 0, DXGI_FORMAT_UNKNOWN, stride <= 16383): stride = StructureByteStride, num_records = NumElements, GFX10_FORMAT_32_UINT, OobSelect::structured.
    • Typed (StructureByteStride == 0, uncompressed color format including R32G32B32_* RGB96): stride = element bytes, num_records = NumElements, format_from_vulkan(named), Shader4ComponentMapping applied to format_swizzle(named), OobSelect::structured.
  • Rejects nullptr desc on buffer, DENY_SHADER_RESOURCE, invalid format/stride/flag combinations, and out-of-bounds FirstElement/NumElements past rd.Width (leaving all 32 bytes zeroed as a null V#).

Reproduction before the fix

Running the new test in d3d12_compute_test.exe against unmodified upstream/main (89b047cd) logged [rt] d3d12: unimplemented ID3D12Device::CreateShaderResourceView(buffer) and failed (exit=1) because the descriptor slot remained zeroed.

Tested

Commit 18472630 on upstream/main 89b047cd, Windows 11, MSVC 19.44, Ninja, VK_LOADER_LAYERS_DISABLE=~implicit~ (synthetic scene only; no game data):

  • NVIDIA GeForce RTX 5080 (driver 616.92, Vulkan 1.4.351):
    • d3d12_compute_test.exe: PASS (exit=0)
    • CPU gfx10::decode round-trip verified for raw (slots 1–2), structured (slots 3–4), typed R32G32B32A32_FLOAT with custom B,G,R,1 Shader4ComponentMapping (slot 5), and typed RGB96 R32G32B32_FLOAT (stride = 12, slot 6), plus 7 invalid/null cases leaving all 32 bytes zeroed.
    • GPU dispatches through shader-visible CBV_SRV_UAV descriptor table (SetComputeRootDescriptorTable):
      • Raw A (FirstElement = 0, NumElements = 32): 32 in-bounds dwords matched + 32 OOB-zero dwords.
      • Structured A (stride = 16, FirstElement = 0, NumElements = 32): 32 in-bounds structs matched + 32 OOB-zero.
      • Typed (R32G32B32A32_FLOAT, FirstElement = 8, NumElements = 24): 24 in-bounds elements matched + 40 OOB-zero.
      • Negative controls: Raw B (FirstElement = 64) flipped 32/32 in-bounds dwords; Structured B (FirstElement = 16) flipped 32/32 in-bounds elements; Null/refused SRV flipped 32/32 in-bounds dwords to 0.
  • AMD Radeon(TM) Graphics (driver 26.10.1, Vulkan 1.4.315):
    • d3d12_compute_test.exe: PASS (exit=0) — exact same positive, OOB, and negative-control results.
  • Note: VK_LAYER_KHRONOS_validation is not installed locally; CI runs Vulkan validation on this commit.

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. (No translator change.)

AI Assisted.

@whattheflop

Copy link
Copy Markdown
Contributor

AI-assisted independent verification of 184726305f19adcdf68002b733c66c7c3b3e2add against 89b047cd8fab50675a43d76722a6a8a881f55dc2. The latter was both the merge base and live upstream main. Source inspection confirmed that main still takes the buffer SRV stub path.

The exact stock d3d12_compute_test built on Windows 11 25H2 (26200.9974) with MSVC 19.38 x64, Windows SDK 10.0.26100.0, Ninja, and glslang 16.6.0. The generated shader passed a separate spirv-val --target-env vulkan1.3 invocation (SPIRV-Tools 2026.4). GPU execution used a disposable positive-only C++ fixture with the same compiled runtime library and the same SPIR-V, verified by hash.

Adapter Driver and Vulkan Result
GeForce RTX 3080 616.64, Vulkan 1.4.351 Exit 0, zero validation errors through teardown
Radeon(TM) Graphics iGPU Windows driver 32.0.21030.2001, Vulkan 1.4.315 Exit 0, zero validation errors through teardown

I ran the adapters serially, using one process-local ICD manifest per run, an empty implicit-layer search path, and explicit VK_LAYER_KHRONOS_validation 1.4.363 with synchronization validation enabled at instance creation.

Both GPUs produced the same readbacks through an SRV descriptor table and the ordinary ID3D12Device::CreateComputePipelineState path. Each dispatch wrote 64 output dwords.

View and first element Loaded dwords matched Shader guard zeros matched
Raw, 0 and 64 32/32 each 32/32 each
Structured, 0 and 16, stride 16 32/32 each 32/32 each
RGBA32F, 8, stride 16 24/24 40/40

Both offset selections changed all 32 loaded dwords relative to their respective zero-offset selections. The entire input buffer was initialized. I checked the GLSL and generated SPIR-V for the raw byte-range and structured/typed record-count branches. Inactive lanes write zero without issuing a source load. The shader reads the first stored dword at each calculated address. CPU decoding separately confirmed RGBA32F's B,G,R,1 mapping, RGB96's 12-byte stride, and a zero trailing half in all six valid descriptor slots. RGB96 and the mapped channel values were CPU-only checks.

The disposable fixture used ordinary heap handles and an SRV root range. It omitted the Xbox-ABI calls, legacy CBV/code-base dispatches and lifetime checks, rejected root signature/PSO/command calls, all seven invalid/null/refused SRV creations, and the null dispatch. The tested command list used a single queue.

This verifies the positive path for Vonsek’s D3D-229 claim. #1779 overlaps on buffer SRVs.

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

Built the runtime from this tree and ran it on the RTX 5090 (621.24, Windows 11):

1/1 Test #86: d3d12_compute ... Passed 0.37 sec

The buffer-SRV path goes through the compute test with the same shader the game uses for user data, so this is the right shape to prove the view reaches the shader as a typed buffer rather than a raw one. No concerns.

@CowBoYdu76

Copy link
Copy Markdown
Contributor

Tested 1847263 on RTX 4070 SUPER (NVIDIA 616.64) and Intel UHD 770 (101.7092), Windows 11, MSVC 19.44: pass

This adds an Intel Xe-LP iGPU and the validation layers to the runs above.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51): no regression from this PR. RTX 4080 SUPER: 100/101 pass. d3d12_draw fails on both GPUs, but it fails the same way on every PR built on current main (bundle blend pixel (0, 0): got 255,0,0,255, expected red 64), so it's a main regression, not from this PR. I'm bisecting it now and will report in the coordination issue. Other Intel UHD 770 failures are the known ones in #1548.

@mokkymiah

Copy link
Copy Markdown
Contributor

Rebased 18472630 onto current main (003e7812) locally: clean, no conflicts. The GitHub "dirty" state is only the old base; a plain git rebase upstream/main is enough to make it mergeable again.

Tested the rebased head on NVIDIA GeForce RTX 3070, driver 621.01, Vulkan 1.4.357, Windows 11 x64, with LEONIDA_VK_VALIDATION=1:

  • d3d12_compute_test.exe: PASS.
  • CreateShaderResourceView(buffer): raw 32 in-bounds + 32 OOB-zero, structured 32 + 32 OOB-zero, typed 24 + 40 OOB-zero matched.
  • Negative controls: raw B 32/32 flipped, structured B 32/32, null SRV 32/32 to zero.
  • No Vulkan validation messages and no unimplemented ... CreateShaderResourceView(buffer) line.
  • The only build warning is the pre-existing C4456 in vulkan_replay.h.

This also confirms the approach is the right one after #1779 was reverted: #1779's placeholder records clashed with #1729's CBV, while this PR writes the GFX10 V# words directly. No game data used. AI-assisted.

@Spaghetih

Copy link
Copy Markdown
Contributor

Tested 18472630 on NVIDIA GeForce RTX 4070 Ti, driver 621.24, Windows 11, MSVC (VS 2022), Vulkan SDK 1.4.363, llvm-mc 21.1.8 in WSL: pass.

No regression against main on the same machine (ctest 97/98 with the same d3d12_draw failure; GPU suite 127/166 passed, 39 skipped, 0 failed).

AI-assisted (Claude Code).

@TheHoracio

Copy link
Copy Markdown
Contributor

RDNA3 run: pass. Tested 18472630 on an AMD Radeon RX 7800 XT (Navi 32), driver 26.10.1 (32.0.32015.7029), Windows 11 Pro (26300), MSVC 14.44, Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja RelWithDebInfo, LEONIDA_VK_VALIDATION=1. d3d12_compute_test exits 0 with 0 validation messages: raw (32 in-bounds + 32 OOB-zero), structured (32 + 32 OOB-zero), typed (24 + 40 OOB-zero) matched; negative controls flipped 32/32 (raw B), 32/32 (struct B), 32/32 (null), and the stock two-dispatch check matches 64/64. Synthetic fixtures only. AI-assisted.

@mokkymiah

Copy link
Copy Markdown
Contributor

Rebased onto current main (89c2d9ac) and run on the RTX 3070. Branch: https://github.com/mokkymiah/Leonida/tree/pr1773

main gained the CreateConstantBufferView test in the same d3d12_compute_test block, so this needed a real merge, not a side pick:

  • the CBV block (main) and the buffer SRV block both run in order;
  • the descriptor heap grows from 4 to 8 slots (slot 7 is the refused-SRV slot 7; slots 1-3 are the CBV slots);
  • the SRV block uses fence value 3 because main's CBV block already uses 2;
  • cs_user_data.comp has both V# modes: s1 == 0 is the CBV path, s1 == 0xFFFFFFFF the buffer SRV path, and the default arithmetic path is unchanged.

Tested

Windows 11 x64, NVIDIA GeForce RTX 3070, driver 621.01, Vulkan 1.4.357, LEONIDA_VK_VALIDATION=1. No game data. AI-assisted.

d3d12_compute_test.exe passes:

CreateConstantBufferView: 64/64 dwords matched for CBV A, 32 in-bounds + 32 OOB-zero matched at +128, negative controls flipped 64/64 (CBV B) and 64/64 (null CBV) CreateShaderResourceView(buffer): raw (32 in-bounds + 32 OOB-zero), structured (32 + 32 OOB-zero), typed (24 + 40 OOB-zero) matched; negative controls flipped 32/32 (raw B), 32/32 (struct B), 32/32 (null) compute: root constants, a root UAV and a descriptor table reached the shader through the GPU address arena; 64 elements matched in each of three dispatches

No validation messages. The branch is ready if you want to update the PR head; I have not pushed to your branch.

…ured, typed GFX10 V#)

Implement D3D12_SRV_DIMENSION_BUFFER in ID3D12Device::CreateShaderResourceView,
writing a 16-byte GFX10 V# descriptor into the 32-byte CBV_SRV_UAV slot:
- Raw (D3D12_BUFFER_SRV_FLAG_RAW, R32_TYPELESS, stride 0): OobSelect::raw,
  num_records = NumElements * 4, GFX10_FORMAT_32_UINT.
- Structured (StructureByteStride > 0, DXGI_FORMAT_UNKNOWN): OobSelect::structured,
  stride = StructureByteStride, num_records = NumElements, GFX10_FORMAT_32_UINT.
- Typed (stride 0, uncompressed color format incl. RGB96): OobSelect::structured,
  stride = element bytes, num_records = NumElements, format_from_vulkan(named),
  Shader4ComponentMapping applied to format_swizzle(named).
- Rejects DENY_SHADER_RESOURCE, null desc on buffer, invalid format/stride/flag
  combinations, and out-of-bounds ranges (leaving the 32-byte slot zeroed).

AI-assisted.
@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto upstream/main (16282101) as 07e2293e (merging alongside the landed CBV test block in d3d12_compute_test and cs_user_data.comp, bumping the SRV pass fence to 3). (Antigravity)

Re-verified on 07e2293e (Windows 11, MSVC 19.44, Ninja RelWithDebInfo, VK_LOADER_LAYERS_DISABLE=~implicit~, synthetic scene)

  • NVIDIA GeForce RTX 5080: d3d12_compute_test.exe PASS (exit=0) — both CreateConstantBufferView and CreateShaderResourceView(buffer) (32 in-bounds + 32 OOB-zero raw, 32 + 32 OOB-zero structured, 24 + 40 OOB-zero typed; 32/32 negative-control flips) passed.
  • AMD Radeon(TM) Graphics (LEONIDA_VK_DEVICE=radeon): d3d12_compute_test.exe PASS (exit=0) — identical exact matches and negative-control flips.

AI-assisted.

@Vonsek
Vonsek force-pushed the ag/d3d12-buffer-srv branch from 1847263 to 07e2293 Compare November 22, 2026 19:38
@ItsNotRexed
ItsNotRexed merged commit 7fded18 into ItsNotRexed:main Nov 22, 2026
2 of 4 checks passed
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.

9 participants