Skip to content

d3d12_xs: committed render and depth textures with the console initial state 0x6 are created in RENDER_TARGET / DEPTH_WRITE (retail boot) - #7356

Open
tedin7 wants to merge 3 commits into
ItsNotRexed:mainfrom
tedin7:fast-rt-state
Open

tedin7 wants to merge 3 commits into
ItsNotRexed:mainfrom
tedin7:fast-rt-state

Conversation

@tedin7

@tedin7 tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in #7231 (comment)

@ItsNotRexed: an exe PR, as you asked.

What

The Xbox device proxy now maps the console initial resource state 0x6 for committed textures. When the description is a texture (not a buffer) and the initial state is exactly 0x6, the PC device gets RENDER_TARGET (flag ALLOW_RENDER_TARGET) or DEPTH_WRITE (flag ALLOW_DEPTH_STENCIL) instead. Slots 27 (CreateCommittedResource), 53 (CreateCommittedResource1) and 76 (the Xbox resource creation, which ends in the PC CreateCommittedResource) share one helper. Every other state and every buffer goes on unchanged. The arguments are copied; the title's memory is never written. Slots 27 and 53 are handled after the Xbox route table, so the LEONIDA_XS_*_SLOT overrides still win.

Why

A retail exe integration run (rafaelpssz) showed the title creating committed render-target and depth textures with state 0x6, which the PC CreateCommittedResource refuses for a texture. Mapping by the resource flags gets past it.

Tested

  • xs_proxy_mock_test (Wine, p2): PASS. New check: an RT texture with 0x6 reaches the mock PC device as RENDER_TARGET, a DS texture as DEPTH_WRITE (slots 27, 53, 76); a buffer with 0x6, an RT with state 0, a texture without a use flag and an RT with state 0x4 are unchanged.
  • Negative control on main (same test, old shim): fails at the new RT check (line 811).

Credit: rafaelpssz for the finding.

…so the state map is checked with the riid and output in place
@MartinIndra02

Copy link
Copy Markdown

Independent Windows test of 1056811d5: fail on MSVC (test failure in xs_proxy_mock_test).

Setup: Windows 11 x64, MSVC 19.44.35220 (RelWithDebInfo, Windows SDK 10.0.26100.0), no game files.

  • xs_proxy_mock_test.exe exited 1:
    xs_proxy_mock line 851: mock->args[27][3] == kRender
    
  • Cause: proxy_create_resource (slot 76) calls self->real->CreateCommittedResource(..., pc_initial_state(&pc_desc, initial_state), ...). On Windows x64, initial_state is the 5th parameter (on the stack) and is a 32-bit enum (D3D12_RESOURCE_STATES), so MSVC only stores the low 32 bits of the 8-byte stack slot, leaving uninitialized upper-32-bit stack garbage. mock_slot reads a4 as a 64-bit std::uint64_t, so comparing the full 64-bit mock->args[27][3] == kRender fails (masking to static_cast<std::uint32_t>(mock->args[27][3]) on lines 851/853/855 would fix the test).
  • Minor MSVC warning: d3d12_xs_device.cpp(1611): warning C4702: unreachable code (the if constexpr (Slot == 27 || Slot == 53) branch can use else for the generic forwarder).

AI-assisted review/test.

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified: builds and passes all 6 xs_proxy_mock test suites. Initial state 0x6 on committed textures correctly maps to RENDER_TARGET (when ALLOW_RENDER_TARGET) or DEPTH_WRITE (when ALLOW_DEPTH_STENCIL) for slots 27, 53, and 76 while preserving buffers and other states unchanged.

…VC leaves the upper half of the stack slot), and else-branch the generic forwarder (C4702); found by MartinIndra02 on ItsNotRexed#7356
@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@MartinIndra02 thanks, exact. D3D12_RESOURCE_STATES is a 32-bit enum, and MSVC writes only the low half of its stack slot. Fixed in b3b9bb7: the test compares the recorded state through a state_of() that takes the low 32 bits, in every slot 27/53/76 check. The generic forwarder is now the else branch of the if constexpr, so C4702 is gone. xs_proxy_mock_test passes under Wine on our RTX 4090 pod. @devnull34, thanks for the review. AI-assisted.

@umnitus

umnitus commented Nov 23, 2026

Copy link
Copy Markdown

Independent native Windows check at b3b9bb7 with clang-cl/xwin SDK 10.0.26100. All six xs_proxy_mock CTests passed, including the corrected state-0x6 mapping and unchanged control cases. AI-assisted testing.

@L-696

L-696 commented Nov 23, 2026

Copy link
Copy Markdown

Independent native Windows GPU check of #7356 on RTX 4060 Laptop

Base: 115f3b26113e31f0510c0b906993158be3885714 (PR base reported by GitHub). PR head: b3b9bb7759f8df4608d976fff8a12a326fa40ffc. Windows 11 Pro 24H2 build 26100.7171, i7-13650HX, RTX 4060 Laptop (10de:28e0), NVIDIA 621.01, native D3D12 feature level 12_2. MSVC x64 toolset 14.50.35717, Windows SDK 10.0.26100.

The same synthetic harness linked the unmodified Xbox device proxy source from each revision. It selected the NVIDIA adapter by PCI IDs, wrapped the native D3D12 device, and created 64x64 RGBA8 render-target and D32 depth textures through slots 27, 53 and 76. For each successful creation it issued an RTV or DSV clear on the native device, submitted the command list and waited for the GPU fence with a 5 s timeout. Standard PC states were controls.

Cases Base PR head
Xbox initial state 0x6, color and depth on each of 3 slots 0/6, all E_INVALIDARG (0x80070057) 6/6, S_OK and clear fence completed
PC RENDER_TARGET 0x4 and DEPTH_WRITE 0x10 controls 6/6 6/6
Upstream xs_proxy_mock test invocations (default plus five variants) 6/6 6/6

The native harness exited 1 on the base and 0 on the PR head. A second run per revision produced identical stdout. This verifies the compatibility fix at the native D3D12 resource-creation boundary and confirms the resulting resources accepted GPU clear commands. The test does not read back pixel or depth values. D3D12GetDebugInterface returned 0x887A002D on this machine, so the Debug Layer was unavailable. This test used native D3D12, so it provides no Vulkan validation or retail boot coverage.

Reproduce

Use the same inline native.cpp for both revisions. From each revision's root in an x64 MSVC developer shell:

cl /nologo /permissive- /utf-8 /EHsc /std:c++20 /O2 /DWIN32_LEAN_AND_MEAN /DNOMINMAX /I. path\to\native.cpp runtime\shims\d3d12_xs_device.cpp dxgi.lib d3d12.lib /Fe:native.exe
native.exe
cl /nologo /W4 /permissive- /utf-8 /EHsc /std:c++20 /O2 /DWIN32_LEAN_AND_MEAN /DNOMINMAX runtime\test\xs_proxy_mock_test.cpp runtime\shims\d3d12_xs_device.cpp /Fe:xs_proxy_mock_test.exe

Run the mock executable with no arguments, then separately with --fail-virtualalloc, --fail-virtualprotect, --fail-veh, --env-slots, and --env-slots-invalid. Selected repository sources and headers were verified against their Git blob IDs before compilation. Synthetic textures only. AI-assisted harness, execution and reporting.

native.cpp
#include "runtime/shims/d3d12_xs_device.h"
#include <dxgi1_4.h>
#include <d3d12sdklayers.h>
#include <wrl/client.h>
#include <cstdio>
#include <initializer_list>
using Microsoft::WRL::ComPtr;

HRESULT create_texture(void* device, unsigned slot, const D3D12_HEAP_PROPERTIES& heap,
                       D3D12_RESOURCE_DESC& desc, unsigned state, ID3D12Resource** output) {
    using Slot = HRESULT (WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, unsigned,
        D3D12_RESOURCE_DESC*, unsigned, const void*, const void*, const void*, void*);
    auto call = reinterpret_cast<Slot>((*reinterpret_cast<void***>(device))[slot]);
    const IID iid = __uuidof(ID3D12Resource);
    return call(device, &heap, 0, &desc, state, nullptr, slot == 53 ? nullptr : &iid,
                slot == 53 ? static_cast<const void*>(&iid) : output, output);
}

bool clear_on_gpu(ID3D12Device* device, ID3D12Resource* resource, bool depth) {
    const auto type = depth ? D3D12_DESCRIPTOR_HEAP_TYPE_DSV : D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
    const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{type, 1};
    const D3D12_COMMAND_QUEUE_DESC queue_desc{};
    ComPtr<ID3D12DescriptorHeap> heap;
    ComPtr<ID3D12CommandQueue> queue;
    ComPtr<ID3D12CommandAllocator> allocator;
    ComPtr<ID3D12GraphicsCommandList> list;
    ComPtr<ID3D12Fence> fence;
    if (FAILED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap))) ||
        FAILED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue))) ||
        FAILED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator))) ||
        FAILED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.Get(), nullptr, IID_PPV_ARGS(&list))) ||
        FAILED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)))) return false;
    const auto handle = heap->GetCPUDescriptorHandleForHeapStart();
    if (depth) {
        device->CreateDepthStencilView(resource, nullptr, handle);
        list->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_DEPTH, 0.5f, 0, 0, nullptr);
    } else {
        const float color[4] = {0.25f, 0.5f, 0.75f, 1.0f};
        device->CreateRenderTargetView(resource, nullptr, handle);
        list->ClearRenderTargetView(handle, color, 0, nullptr);
    }
    if (FAILED(list->Close())) return false;
    ID3D12CommandList* lists[] = {list.Get()};
    queue->ExecuteCommandLists(1, lists);
    HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
    if (!event) return false;
    const bool complete = SUCCEEDED(queue->Signal(fence.Get(), 1)) &&
        SUCCEEDED(fence->SetEventOnCompletion(1, event)) &&
        WaitForSingleObject(event, 5000) == WAIT_OBJECT_0 &&
        SUCCEEDED(device->GetDeviceRemovedReason());
    CloseHandle(event);
    return complete;
}

int main() {
    ComPtr<ID3D12Debug> debug;
    HRESULT debug_hr = D3D12GetDebugInterface(IID_PPV_ARGS(&debug));
    if (debug) debug->EnableDebugLayer();
    std::printf("D3D12 Debug Layer: %08lx\n", static_cast<unsigned long>(debug_hr));
    ComPtr<IDXGIFactory1> factory;
    if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return 2;
    ComPtr<ID3D12Device> device;
    for (UINT i = 0; ; ++i) {
        ComPtr<IDXGIAdapter1> adapter;
        if (factory->EnumAdapters1(i, &adapter) != S_OK) break;
        DXGI_ADAPTER_DESC1 desc{};
        if (FAILED(adapter->GetDesc1(&desc)) || desc.VendorId != 0x10de || desc.DeviceId != 0x28e0) continue;
        if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_2, IID_PPV_ARGS(&device)))) {
            std::printf("Device: %ls (%04x:%04x)\n", desc.Description, desc.VendorId, desc.DeviceId);
            break;
        }
    }
    if (!device) return 3;
    ComPtr<IUnknown> proxy;
    device->AddRef(); // proxy takes one reference to the native device
    if (FAILED(rt::d3d12_xs::create_device_proxy(device.Get(), __uuidof(ID3D12Device),
                                                reinterpret_cast<void**>(proxy.GetAddressOf())))) return 4;
    const D3D12_HEAP_PROPERTIES heap{D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
                                    D3D12_MEMORY_POOL_UNKNOWN, 1, 1};
    unsigned passed = 0, total = 0;
    for (unsigned slot : {27u, 53u, 76u}) {
        for (bool depth : {false, true}) {
            for (bool console : {false, true}) {
                D3D12_RESOURCE_DESC desc{};
                desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
                desc.Width = desc.Height = 64;
                desc.DepthOrArraySize = desc.MipLevels = desc.SampleDesc.Count = 1;
                desc.Format = depth ? DXGI_FORMAT_D32_FLOAT : DXGI_FORMAT_R8G8B8A8_UNORM;
                desc.Flags = depth ? D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL : D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
                const unsigned state = console ? 6 : depth ? D3D12_RESOURCE_STATE_DEPTH_WRITE : D3D12_RESOURCE_STATE_RENDER_TARGET;
                ComPtr<ID3D12Resource> resource;
                HRESULT hr = create_texture(proxy.Get(), slot, heap, desc, state, resource.GetAddressOf());
                const bool cleared = resource && clear_on_gpu(device.Get(), resource.Get(), depth);
                std::printf("slot=%u kind=%s state=0x%x HRESULT=0x%08lx GPU_clear_fence=%s\n",
                            slot, depth ? "depth" : "color", state, static_cast<unsigned long>(hr), cleared ? "pass" : "no");
                ++total;
                passed += SUCCEEDED(hr) && cleared;
            }
        }
    }
    std::printf("Pass %u/%u\n", passed, total);
    return passed == total ? 0 : 1;
}

@TheHoracio

Copy link
Copy Markdown
Contributor

Tested b3b9bb775 on AMD Radeon RX 7800 XT (RDNA3), Windows 11, MSVC 2022 RelWithDebInfo: pass. scripts/build_runtime.ps1 and the device/proxy subset of ctest (-R "proxy|xs_|d3d12_xs|registry|unimplemented|xgameruntime|cpu_emulation|existing_heap|mesh|bindless|file_system|gameinput"): 26/26. No game files. (AI-assisted run.)

Integration note: on top of #7240 this conflicts in slot 76's CreateCommittedResource return (#7240 adds the lease after it). Applying the mapping as pc_initial_state(&pc_desc, initial_state) in #7240's version is a one-line change, and your test ports to #7240's slot-27 mock (it records the state in args[27][3] there too); details in #7049.

@myokex

myokex commented Nov 23, 2026

Copy link
Copy Markdown

Tested b3b9bb775 on Windows 11, AMD Radeon RX 7800 XT (RDNA3, driver 32.0.32029.2010), MSVC 2022 RelWithDebInfo, CMake 4.4.4, Vulkan SDK 1.4.363.0; merged alone onto main 042c5f378, full ctest -j8; synthetic, no game files: pass. xs_proxy_mock and its five variants (_no_reserve, _no_protect, _no_handler, _env_slots, _env_slots_invalid) pass. The 5 failures main itself has on this card (vk_queue_query_sync, d3d12_vulkan_device, d3d12_texture_tiles, d3d12_indirect_dispatch_count, d3d12_pipeline_library_host, see #166) fail the same way with and without this PR. d3d12_bindless_churn (or _validated) failed once in this run; it is a flake under parallel ctest -j8 here, not this PR: it also failed with unrelated PRs, and on main it passes 10/10 run alone (--repeat until-fail:10). AI-assisted (Claude Code).

@dracbat

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A data point from a Windows retail boot on an RX 5700 (RDNA1, AMD driver 32.0.21045.29003, own 1.0.2.1 copy). It's a local integration with #7075's slot-133 stand-in, #7052/#7235 and this PR.

The state-0x6 textures did not reach this PR's mapping here, because they come through device slot 133 (CreatePlacedResourceX), not 27, 53 or 76.

  • The two refused creations were a 1×1 R8G8B8A8_UNORM texture with ALLOW_RENDER_TARGET and a 1×1 R32G8X24_TYPELESS texture with ALLOW_DEPTH_STENCIL. Both were on a DEFAULT heap, with a clear value and initial state 0x6.
  • I resolved the caller with the PDB. It is d3d12_xs: device slot 133 places a resource as a committed DEFAULT one (CreatePlacedResourceX stand-in, depends on #6953) #7075's committed stand-in for slot 133 (proxy_create_placed_resource_x), which passes any state other than all-ones through unchanged.
  • The layer refuses the state ("initial state of a texture (buffer-only or undefined bits)"). The title then asserts three times and faults.
  • With the same mapping applied to that slot-133 path locally, both textures are created. The boot goes on to the first ExecuteCommandListsX (queue slot 35) and queue slot 32.

So pc_initial_state probably belongs on slot 133 as well, in whichever of #7075 or #7218 lands. AI-assisted (Claude Code).

@3L1A5-1

3L1A5-1 commented Nov 23, 2026

Copy link
Copy Markdown

Second machine for @dracbat's slot-133 result above, and the state that comes next. Windows 11 26300, MSVC 14.44, RTX 2060 SUPER (617.14), Intel i5-9400F (#7111 needed), LEONIDA_D3D12=vulkan, own 1.0.2.1 copy. Local merge of main@511e3826b with this PR (b3b9bb775), #7075 (cac1fc0cd) for slot 133 and the boot stack listed in #7154 (comment) plus #7516, #7330, #7201, #6891, #7076 and #2490ef9d075. Flag values and counts only.

  • Reproduced: with this PR alone, the two state-0x6 textures (flags 0x1 and 0x2, DEFAULT heap) still reach the layer unmapped through d3d12_xs: device slot 133 places a resource as a committed DEFAULT one (CreatePlacedResourceX stand-in, depends on #6953) #7075's proxy_create_placed_resource_x, and the layer refuses them.
  • With pc_initial_state(&pc_desc, state) on that slot-133 call (one line, local): both are created. The boot goes on from 1,657 to 2,307 D3D12 calls: depth-stencil views, WaitFrameEventX, list and queue slots (queue slot 32 included) and PresentX ready.
  • The next refusal is another console state: initial state 0x3 on a 2D texture with flags 0x5 (ALLOW_RENDER_TARGET | ALLOW_UNORDERED_ACCESS), R16G16B16A16_FLOAT, DEFAULT heap. On the PC 0x3 reads as VERTEX_AND_CONSTANT_BUFFER | INDEX_BUFFER, which the layer refuses on a texture. What 0x3 means on the console is not measured here. I'm not taking this; it extends this PR's mapping, so it's yours if you want it.

AI-assisted (Claude Code).

zneel added a commit to zneel/Leonida that referenced this pull request Nov 23, 2026
Slot 76 keeps ItsNotRexed#7240's lease and maps the initial state as ItsNotRexed#7356 does. The mock's
slot 27 entry from ItsNotRexed#7240 also records the words after the fifth for ItsNotRexed#7356's
check, and creates a resource only when riid and output are real pointers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @dracbat and @3L1A5-1: the state-0x6 textures coming through slot 133 (#7075, #7218) explains why this PR alone does not clear them. pc_initial_state is a plain helper in d3d12_xs_desc_clean.h, so slot 133 can call it in whichever of #7075/#7218 lands; I will add the one-line call there once it is on main, and look at the next console state (0x3 on a render target + UAV texture) in a separate PR with the measured numbers. AI-assisted.

@dracbat

dracbat commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A correction to my comment above. On slot 133 the 0x6 is a D3D12_BARRIER_LAYOUT (SHADER_RESOURCE), not a resource state. The same call passes 0x3 (UNORDERED_ACCESS) for a 2560×1440 RT+UAV texture and 0xFFFFFFFF (UNDEFINED) for others, which fits CreatePlacedResource2's InitialLayout. Mapping it as a layout creates every slot-133 texture, and the boot runs a steady frame loop on an RX 5700. Details: #8047 (comment). Whether slots 27, 53 and 76 also pass layouts I haven't measured. AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @dracbat for the correction: slot 133's third argument is a D3D12_BARRIER_LAYOUT (CreatePlacedResource2 InitialLayout), not a resource state, so this PR's state mapping is not the right place for it (slots 27/53/76 pass states as measured). I will not extend this PR to slot 133; the layout mapping belongs with #7075/#7218. AI-assisted.

tibzejoker added a commit to tibzejoker/Leonida that referenced this pull request Nov 23, 2026
Slots 27/53 map the console initial state 0x6 (pc_initial_state_word); slot 76 maps it too and keeps ItsNotRexed#7240's lease. No same-slot clash: 27/53 were plain forwards. The mock keeps one slot-27 entry (creating, with a words-only mode for this PR's checks) and the PR's entry for 53.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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