From 3556bc28c06a76362e83da64a555e5d3994d5e17 Mon Sep 17 00:00:00 2001 From: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com> Date: Tue, 24 Nov 2026 21:40:21 +0100 Subject: [PATCH] startup: create committed upload and readback resources in their required initial states Device slot 127 created every resource in COMMON. D3D12 requires upload-heap resources to start in GENERIC_READ and readback-heap resources in COPY_DEST; the Windows 10 runtime refuses COMMON there with E_INVALIDARG. Other heaps keep COMMON. startup_committed_heap_state_{warp,hardware} create default, upload and readback buffers through slot 127; the upload-texture control asks for a 2D texture on an upload heap, which no runtime creates. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 12 ++ src/startup_stubs/d3d12_xs_device.cpp | 13 ++- tests/startup_committed_heap_state_check.cpp | 113 +++++++++++++++++++ 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 tests/startup_committed_heap_state_check.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b93421a476..3e5aa071f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1066,6 +1066,18 @@ foreach(adapter IN ITEMS warp hardware) add_test(NAME startup_plane_size_device_${adapter} COMMAND startup_plane_size_device_check ${adapter}) endforeach() set_tests_properties(startup_plane_size_device_hardware PROPERTIES SKIP_RETURN_CODE 77) +# Committed buffers through the device wrapper (slot 127) on the default, upload and readback heaps: +# upload and readback must start in GENERIC_READ and COPY_DEST, which the Windows 10 runtime enforces. +# The mutated run asks for a 2D texture on an upload heap, which no runtime creates (negative control). +add_executable(startup_committed_heap_state_check tests/startup_committed_heap_state_check.cpp) +target_compile_features(startup_committed_heap_state_check PRIVATE cxx_std_17) +target_link_libraries(startup_committed_heap_state_check PRIVATE d3d12_xs_shim d3d12 dxgi) +foreach(adapter IN ITEMS warp hardware) + add_test(NAME startup_committed_heap_state_${adapter} COMMAND startup_committed_heap_state_check ${adapter}) +endforeach() +add_test(NAME startup_committed_heap_state_upload_texture_control COMMAND startup_committed_heap_state_check warp --mutate upload-texture) +set_tests_properties(startup_committed_heap_state_hardware PROPERTIES SKIP_RETURN_CODE 77) +set_tests_properties(startup_committed_heap_state_upload_texture_control PROPERTIES WILL_FAIL TRUE) # Opt-in BC7 4 KiB standard surfaces (src/startup_stubs/bc7_4k_s.h): accepted descriptions, padded # sizes, block order and detile; the mutated run (x-first bit order) is the negative control. add_executable(startup_bc7_4k_s_check tests/startup_bc7_4k_s_check.cpp) diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index ce5d96ed41..a51e9cfe43 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -3133,7 +3133,9 @@ HRESULT __stdcall device_create_derived_pipeline(Wrapper*, void* base, UINT coun // heap flags (8, ALLOW_DISPLAY, for the 4K render targets), resource desc, an initial state or // layout, clear value, then three zero words (protected session, castable count and formats in the // desktop method), riid and ppv. Desktop: CreateCommittedResource with the sanitized desc in -// COMMON; when desktop rejects the clear value it is retried without one. +// COMMON, GENERIC_READ on an upload heap and COPY_DEST on a readback heap (the initial states D3D12 +// requires there; the Windows 10 runtime refuses COMMON with E_INVALIDARG). When desktop rejects the +// clear value it is retried without one. HRESULT __stdcall device_create_committed_x(Wrapper* self, const D3D12_HEAP_PROPERTIES* props, UINT heap_flags, const D3D12_RESOURCE_DESC* desc, UINT state, const D3D12_CLEAR_VALUE* clear, void* session, UINT castable_count, const void* castable, const GUID* iid, @@ -3149,14 +3151,15 @@ HRESULT __stdcall device_create_committed_x(Wrapper* self, const D3D12_HEAP_PROP check_device("Device before slot", 127); void* const title_memory = info.SizeInBytes != ~0ull ? title_resource_memory(info.SizeInBytes, info.Alignment) : nullptr; ID3D12Resource* resource = nullptr; + const D3D12_RESOURCE_STATES initial = props->Type == D3D12_HEAP_TYPE_UPLOAD ? D3D12_RESOURCE_STATE_GENERIC_READ + : props->Type == D3D12_HEAP_TYPE_READBACK ? D3D12_RESOURCE_STATE_COPY_DEST + : D3D12_RESOURCE_STATE_COMMON; HRESULT hr = kind_for(*iid) == resource_kind - ? device_of(self)->CreateCommittedResource(props, flags, ©, D3D12_RESOURCE_STATE_COMMON, clear, - IID_PPV_ARGS(&resource)) + ? device_of(self)->CreateCommittedResource(props, flags, ©, initial, clear, IID_PPV_ARGS(&resource)) : E_NOINTERFACE; bool cleared = clear != nullptr; if (FAILED(hr) && clear && kind_for(*iid) == resource_kind) { - hr = device_of(self)->CreateCommittedResource(props, flags, ©, D3D12_RESOURCE_STATE_COMMON, nullptr, - IID_PPV_ARGS(&resource)); + hr = device_of(self)->CreateCommittedResource(props, flags, ©, initial, nullptr, IID_PPV_ARGS(&resource)); cleared = false; } static LONG calls; diff --git a/tests/startup_committed_heap_state_check.cpp b/tests/startup_committed_heap_state_check.cpp new file mode 100644 index 0000000000..4b7e78e2ed --- /dev/null +++ b/tests/startup_committed_heap_state_check.cpp @@ -0,0 +1,113 @@ +// Device check for committed resources made through the startup device wrapper (slot 127, the +// CreateCommittedResource3 argument order). D3D12 requires an upload-heap resource to start in +// GENERIC_READ and a readback-heap resource in COPY_DEST; the Windows 10 runtime refuses COMMON there +// with E_INVALIDARG. Buffers on the default, upload and readback heaps, passed as a title passes them +// (initial layout 0xffffffff, no clear value), must all be created. +// usage: startup_committed_heap_state_check warp|hardware [--mutate upload-texture] +// The mutated run asks for a 2D texture on an upload heap, which no runtime creates: the negative control. +#include "xmem_test_device.h" + +#include + +extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); + +namespace { +HRESULT create_wrapper(IUnknown* adapter, REFIID iid, void** out) +{ + unsigned char parameters[64]{}; + return D3D12XboxCreateDevice(adapter, parameters, iid, out); +} + +// Console device slot 127: heap properties, heap flags, desc, initial state or layout, clear value, +// protected session, castable format count and formats, riid, ppv. +using Committed127 = HRESULT (*)(void*, const D3D12_HEAP_PROPERTIES*, UINT, const D3D12_RESOURCE_DESC*, UINT, + const D3D12_CLEAR_VALUE*, void*, UINT, const void*, const GUID*, void**); +constexpr UINT layout_undefined = 0xffffffff; + +Committed127 slot127(ID3D12Device* device) +{ + return reinterpret_cast((*reinterpret_cast(device))[127]); +} + +D3D12_RESOURCE_DESC buffer(UINT64 bytes) +{ + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + desc.Width = bytes; + desc.Height = 1; + desc.DepthOrArraySize = 1; + desc.MipLevels = 1; + desc.SampleDesc.Count = 1; + desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + return desc; +} + +D3D12_RESOURCE_DESC texture(UINT width, UINT height) +{ + D3D12_RESOURCE_DESC desc{}; + desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + desc.Width = width; + desc.Height = height; + desc.DepthOrArraySize = 1; + desc.MipLevels = 1; + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.SampleDesc.Count = 1; + return desc; +} + +const char* heap_name(D3D12_HEAP_TYPE type) +{ + return type == D3D12_HEAP_TYPE_DEFAULT ? "default" : type == D3D12_HEAP_TYPE_UPLOAD ? "upload" : "readback"; +} + +// Returns 0 when slot 127 creates the resource and the device stays alive. +int check(ID3D12Device* wrapper, D3D12_HEAP_TYPE type, const D3D12_RESOURCE_DESC& desc) +{ + const D3D12_HEAP_PROPERTIES props{type}; + const GUID iid = __uuidof(ID3D12Resource); + void* created = nullptr; + const HRESULT hr = slot127(wrapper)(wrapper, &props, 0, &desc, layout_undefined, nullptr, nullptr, 0, nullptr, &iid, &created); + const HRESULT removed = wrapper->GetDeviceRemovedReason(); + std::printf("%s heap, %s of 0x%llx: slot 127 -> 0x%08lx\n", heap_name(type), + desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? "buffer" : "2D texture", + static_cast(desc.Width), static_cast(hr)); + const int result = FAILED(hr) || !created || FAILED(removed) ? 1 : 0; + if (result) + std::printf("FAIL %s heap: hr 0x%08lx object %p device 0x%08lx\n", heap_name(type), static_cast(hr), created, + static_cast(removed)); + if (created) static_cast(created)->Release(); + return result; +} +} // namespace + +int main(int argc, char** argv) +{ + if (argc != 2 && !(argc == 4 && std::strcmp(argv[2], "--mutate") == 0 && std::strcmp(argv[3], "upload-texture") == 0)) return 1; + const bool warp = std::strcmp(argv[1], "warp") == 0; + const bool mutated = argc == 4; + bool unmatched = false; + auto desktop = create_device(warp, unmatched); + if (!desktop) return warp || unmatched ? 1 : 77; + + // What this runtime does with COMMON on an upload heap: recorded, not checked (Windows 10 refuses it). + const D3D12_HEAP_PROPERTIES upload{D3D12_HEAP_TYPE_UPLOAD}; + const D3D12_RESOURCE_DESC probe = buffer(0x10000); + Microsoft::WRL::ComPtr common; + const HRESULT common_hr = desktop->CreateCommittedResource(&upload, D3D12_HEAP_FLAG_NONE, &probe, D3D12_RESOURCE_STATE_COMMON, + nullptr, IID_PPV_ARGS(&common)); + std::printf("this runtime, upload buffer in COMMON: 0x%08lx (%s)\n", static_cast(common_hr), + SUCCEEDED(common_hr) ? "accepted" : "refused"); + + SetEnvironmentVariableW(L"X2W_D3D12_PERMISSIVE", L"1"); + auto wrapper = create_device(warp, unmatched, create_wrapper); + if (!wrapper) return 1; + int failures = 0; + // Sizes from a startup run: 150 and 320 entries of 768 bytes on an upload heap, one entry on a readback heap. + failures += check(wrapper.Get(), D3D12_HEAP_TYPE_DEFAULT, buffer(0x10000)); + failures += check(wrapper.Get(), D3D12_HEAP_TYPE_UPLOAD, mutated ? texture(64, 64) : buffer(150 * 768)); + failures += check(wrapper.Get(), D3D12_HEAP_TYPE_UPLOAD, buffer(320 * 768)); + failures += check(wrapper.Get(), D3D12_HEAP_TYPE_READBACK, buffer(768)); + if (failures) return 1; + std::puts("startup_committed_heap_state: PASS (default, upload and readback buffers created through slot 127)"); + return 0; +}