Skip to content

dxgi: implement BackBufferResource::GetDevice and default-heap Map/Subresource errors - #4068

Open
cixotdev wants to merge 1 commit into
ItsNotRexed:mainfrom
cixotdev:fix/dxgi-backbuffer-getdevice-and-map
Open

cixotdev wants to merge 1 commit into
ItsNotRexed:mainfrom
cixotdev:fix/dxgi-backbuffer-getdevice-and-map

Conversation

@cixotdev

Copy link
Copy Markdown
Contributor

Summary

Implements ID3D12DeviceChild::GetDevice and the default-heap CPU-access overrides (Map, Unmap, WriteToSubresource, ReadFromSubresource) on BackBufferResource in runtime/shims/dxgi_com.cpp, matching Windows 11 host D3D12/DXGI behavior measured on NVIDIA GeForce RTX 4060.

Root Cause

BackBufferResource (returned by IDXGISwapChain::GetBuffer) inherited ID3D12ResourceStubs without overriding:

  • ID3D12DeviceChild::GetDevice(REFIID, void**): callers received E_NOTIMPL and triggered [rt] d3d12: unimplemented ID3D12DeviceChild::GetDevice whenever retrieving the owning ID3D12Device from a swapchain back buffer (including in common D3D12 helpers and resource-inspection paths).
  • Map, Unmap, WriteToSubresource, ReadFromSubresource: callers on default-heap back buffers received E_NOTIMPL instead of E_INVALIDARG.

Host Measurement (Windows 11 26200, RTX 4060, host d3d12.dll/dxgi.dll)

Measured against a host D3D12 swapchain back buffer:

  • BackBuffer::GetDevice: returns S_OK with the owning ID3D12Device COM identity (ID3D12Device, ID3D12Device4, IUnknown), E_POINTER when ppvDevice == nullptr, and E_NOINTERFACE with *ppvDevice = nullptr for unsupported IIDs (e.g., IDXGISwapChain3).
  • BackBuffer::Map(0, nullptr, &ptr) / Map(0, nullptr, nullptr): returns E_INVALIDARG (0x80070057) and leaves *ppData untouched.
  • BackBuffer::WriteToSubresource / ReadFromSubresource: return E_INVALIDARG (0x80070057).

Changes

  • runtime/shims/dxgi_com.cpp:
    • Extract a shared query_owning_device(IUnknown* owner, REFIID riid, void** out) helper used by both SwapChain::GetDevice and BackBufferResource.
    • Have BackBufferResource resolve and hold an owning device IUnknown* reference (released in ~BackBufferResource(), non-copyable), keeping GetDevice valid even if a back buffer outlives its SwapChain without leaking device references after the back buffer is released.
    • Implement BackBufferResource::GetDevice, Map, Unmap, WriteToSubresource, and ReadFromSubresource.
    • Pass device_ when constructing BackBufferResource instances in SwapChain::register_buffers() (used on both initial creation and ResizeBuffers).
  • runtime/test/dxgi_swapchain_test.cpp:
    • Assert bb->GetDevice inside run_clear_readback_present (covers initial buffers and post-ResizeBuffers buffers).
    • Add synthetic regression tests for BackBuffer::GetDevice (ID3D12Device, IUnknown COM identity distinct from queue identity, unsupported IID E_NOINTERFACE + *out = nullptr, null out E_POINTER), Map/Unmap/WriteToSubresource/ReadFromSubresource (E_INVALIDARG, *ppData preserved), and SwapChain-release lifetime / zero device refcount leak.

Verification & GPU Results (No Game Files)

  • Negative control (new tests against unpatched dxgi_com.cpp on origin/main aa2988cc):
    • dxgi_swapchain_test.exe failed with exit code 1:
      • [rt] d3d12: unimplemented ID3D12DeviceChild::GetDevice
      • FAIL: BackBuffer GetDevice did not return the owning device
  • After fix (Windows 11 10.0.26200, NVIDIA GeForce RTX 4060 8 GB driver 621.01, Vulkan SDK 1.4.363.0, MSVC 19.44.35230.0, CMake 4.4.4, Ninja 1.13.2):
    • dxgi_swapchain_test.exe (validation off): PASS, 0 validation errors, no unimplemented stub warnings for GetDevice/Map/Unmap/WriteToSubresource/ReadFromSubresource.
    • LEONIDA_VK_VALIDATION=1 dxgi_swapchain_test.exe (validation on): PASS, 0 validation errors.
    • ctest --test-dir runtime/build -R "^(d3d12_com|d3d12_private_data|d3d12_resource_desc|d3d12_method_variants|dxgi_swapchain|d3d12_present|xbox_present|d3d12_vulkan_device)$" --output-on-failure: 8/8 passed (100% tests passed, 0 tests failed) in 27.35 sec (zero skipped).
    • gta6rt + all test binaries compiled cleanly with /W4; git diff --check clean; LF-only line endings preserved (CRLF=0).

Coordination & Dependencies

AI-assisted with Argon (Antigravity).

…bresource errors

IDXGISwapChain::GetBuffer returns a BackBufferResource inheriting from
ID3D12ResourceStubs without overriding ID3D12DeviceChild::GetDevice or the
CPU-access methods (Map, Unmap, WriteToSubresource, ReadFromSubresource),
causing GetDevice calls on swapchain back buffers to log an unimplemented
stub and return E_NOTIMPL instead of the owning device, and Map /
WriteToSubresource / ReadFromSubresource to return E_NOTIMPL instead of
E_INVALIDARG.

Add a shared query_owning_device helper used by both SwapChain and
BackBufferResource so back buffers resolve and retain a reference to the
owning device (through SwapChain teardown until released), implement
GetDevice (S_OK, E_POINTER, E_NOINTERFACE) and default-heap Map/Unmap/
WriteToSubresource/ReadFromSubresource matching Windows 11 host D3D12
measurements on RTX 4060, and add synthetic regression tests in
dxgi_swapchain_test.cpp.

Claimed in ItsNotRexed#3918 (comment)
AI-assisted.
@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested dd5919b on Windows 11, AMD Radeon iGPU gfx1036 (driver 26.10.1) and RTX 5080 (driver 616.92): ctest -R "^(d3d12_com|d3d12_private_data|d3d12_resource_desc|d3d12_method_variants|dxgi_swapchain|d3d12_present|xbox_present|d3d12_vulkan_device)$" (MSVC, RelWithDebInfo, LEONIDA_VK_DEVICE set per run, device line confirmed, VK_LOADER_LAYERS_DISABLE=~implicit~, no validation layer installed) — pass on both, 8/8 each. dxgi_swapchain_test: PASS (... GetBuffer+GetDesc, clear+readback, Present, ResizeBuffers ...) on both.

No negative control run.

AI-assisted.

@oVolkra

oVolkra commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested dd5919b (merged onto main aa2988c) on RTX 3060, driver 617.14, Windows 11: pass.

What I ran: ctest, with and without LEONIDA_VK_VALIDATION=1; the *_test.exe binaries that ctest doesn't register; and scripts/run_gpu_tests.py, native and with LEONIDA_W64_EMULATE=1. No new failures versus main.

One test (ctest_val:winhttp_offline) failed on main and passed on this PR. I haven't checked whether it is flaky, so I am not counting it as a fix.

AI-assisted: this run was done with Claude Code (Haiku 5.5); the numbers above are its unedited output.

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.

3 participants