diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index e23985ecb3..7ef0ae49b8 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -3342,7 +3342,10 @@ Layer plan: COM object with the device (a PC `ID3D12Device` has no `IDXGIDevice`, so nothing is forwarded), and `GetAdapter` and `GetParent` return the adapter of the device behind it: the layer's own on the Vulkan device, the host adapter with the device's LUID on the host backend. The 14 slots are the public `dxgi.h` order, an assumption like the - queue's. `SetMaximumFrameLatency` and `SetGPUThreadPriority` are kept and read back only. `dxgi_device_test` + queue's. `SetMaximumFrameLatency` and `SetGPUThreadPriority` are kept and read back only, and + `QueryResourceResidency` checks non-null array/output arguments and verifies that each entry is an `ID3D12Pageable` + owned by the device before reporting `DXGI_RESIDENCY_FULLY_RESIDENT` (matching Xbox unified physical memory; + `CreateSurface` stays `E_NOTIMPL` with a log). `dxgi_device_test` walks it on both backends; the output itself is `EnumOutputs` (D3D-313). - `ALLOW_DISPLAY` committed resources are normal `Resource`s that the presenter may blit to a Vulkan swapchain image; `PRESENT` state maps per the resource-state table (`PRESENT_SRC_KHR` only for swapchain images). diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 60d7c62f30..328e49a6e0 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -740,8 +740,9 @@ HRESULT WINAPI dxgi_get_adapter(void* self, IDXGIAdapter** out) { note_dxgi("XboxIDXGIDevice::GetAdapter"); return dxgi_adapter(dxgi_owner(self), __uuidof(IDXGIAdapter), reinterpret_cast(out)); } -// CreateSurface and QueryResourceResidency are D3D10/11-era methods no D3D12 title has a use for; what the console -// answers is not measured, so they are refused and say so once. +// CreateSurface is a D3D10/11-era method no D3D12 title has a use for, so it stays refused and logs once. +// QueryResourceResidency validates its inputs and each ID3D12Pageable's owning device, reporting +// DXGI_RESIDENCY_FULLY_RESIDENT for resources/heaps belonging to this device (matching Xbox unified physical memory). HRESULT WINAPI dxgi_create_surface(void*, const DXGI_SURFACE_DESC*, UINT, DXGI_USAGE, const DXGI_SHARED_RESOURCE*, IDXGISurface** out) { static std::atomic logged{false}; @@ -750,12 +751,39 @@ HRESULT WINAPI dxgi_create_surface(void*, const DXGI_SURFACE_DESC*, UINT, DXGI_U if (out) *out = nullptr; return E_NOTIMPL; } -HRESULT WINAPI dxgi_query_resource_residency(void*, IUnknown* const*, DXGI_RESIDENCY*, UINT) { - static std::atomic logged{false}; - if (!logged.exchange(true, std::memory_order_relaxed)) - std::fprintf(stderr, - "[rt] d3d12_xs: IDXGIDevice::QueryResourceResidency is not implemented; returning E_NOTIMPL\n"); - return E_NOTIMPL; +bool same_com_identity(IUnknown* a, IUnknown* b) { + if (a == b) return a != nullptr; + if (!a || !b) return false; + IUnknown *ua = nullptr, *ub = nullptr; + const HRESULT ha = a->QueryInterface(__uuidof(IUnknown), reinterpret_cast(&ua)); + const HRESULT hb = b->QueryInterface(__uuidof(IUnknown), reinterpret_cast(&ub)); + const bool same = SUCCEEDED(ha) && SUCCEEDED(hb) && ua && ua == ub; + if (ua) ua->Release(); + if (ub) ub->Release(); + return same; +} +bool owns_pageable(DeviceProxy* owner, IUnknown* object) { + if (!owner || !object) return false; + ID3D12Pageable* pageable = nullptr; + if (FAILED(object->QueryInterface(__uuidof(ID3D12Pageable), reinterpret_cast(&pageable))) || !pageable) + return false; + ID3D12Device* dev = nullptr; + const HRESULT hr = pageable->GetDevice(__uuidof(ID3D12Device), reinterpret_cast(&dev)); + pageable->Release(); + if (FAILED(hr) || !dev) return false; + const bool owned = same_com_identity(dev, reinterpret_cast(owner)) || same_com_identity(dev, owner->real); + dev->Release(); + return owned; +} +HRESULT WINAPI dxgi_query_resource_residency(void* self, IUnknown* const* resources, DXGI_RESIDENCY* residency, + UINT count) { + if (!resources || !residency) return E_INVALIDARG; + if (count == 0) return S_OK; + DeviceProxy* owner = dxgi_owner(self); + for (UINT i = 0; i < count; ++i) + if (!owns_pageable(owner, resources[i])) return E_INVALIDARG; + for (UINT i = 0; i < count; ++i) residency[i] = DXGI_RESIDENCY_FULLY_RESIDENT; + return S_OK; } // The priority and the frame latency are kept and read back; nothing acts on them (the Xbox presenter's latency is // SetFrameIntervalX's). The ranges are the documented ones: -7..7, and 1..16 with 0 for the default of 3. diff --git a/runtime/test/dxgi_device_test.cpp b/runtime/test/dxgi_device_test.cpp index 0cde3d2a26..fe21ac3d0e 100644 --- a/runtime/test/dxgi_device_test.cpp +++ b/runtime/test/dxgi_device_test.cpp @@ -155,7 +155,73 @@ int check_backend(const char* label, ID3D12Device* device, ID3D12Device* real, b EXPECT(dxgi->GetGPUThreadPriority(nullptr) == E_POINTER); IDXGISurface* surface = reinterpret_cast(std::uintptr_t{1}); EXPECT(dxgi->CreateSurface(nullptr, 0, 0, nullptr, &surface) == E_NOTIMPL && !surface); - EXPECT(dxgi->QueryResourceResidency(nullptr, nullptr, 0) == E_NOTIMPL); + { + D3D12_HEAP_PROPERTIES buf_heap{D3D12_HEAP_TYPE_DEFAULT}; + D3D12_RESOURCE_DESC buf_desc{}; + buf_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buf_desc.Width = 256; + buf_desc.Height = 1; + buf_desc.DepthOrArraySize = 1; + buf_desc.MipLevels = 1; + buf_desc.SampleDesc.Count = 1; + buf_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + Ref resource; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&buf_heap, D3D12_HEAP_FLAG_NONE, &buf_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&resource.p)))); + D3D12_HEAP_DESC heap_desc{65536, {D3D12_HEAP_TYPE_DEFAULT}, 0, D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS}; + Ref heap; + EXPECT(SUCCEEDED(device->CreateHeap(&heap_desc, IID_PPV_ARGS(&heap.p)))); + + DXGI_RESIDENCY residency[2] = {DXGI_RESIDENCY(99), DXGI_RESIDENCY(99)}; + IUnknown* const good_list[2] = {resource.p, heap.p}; + IUnknown* const null_entry_list[2] = {resource.p, nullptr}; + IUnknown* const device_entry[1] = {device}; + IUnknown* const dxgi_entry[1] = {dxgi.p}; + IUnknown* const adapter_entry[1] = {adapter.p}; + EXPECT(dxgi->QueryResourceResidency(nullptr, nullptr, 0) == E_INVALIDARG); + EXPECT(dxgi->QueryResourceResidency(nullptr, residency, 1) == E_INVALIDARG); + EXPECT(dxgi->QueryResourceResidency(good_list, nullptr, 1) == E_INVALIDARG); + EXPECT(dxgi->QueryResourceResidency(good_list, residency, 0) == S_OK); + EXPECT(dxgi->QueryResourceResidency(null_entry_list, residency, 2) == E_INVALIDARG); + EXPECT(dxgi->QueryResourceResidency(device_entry, residency, 1) == E_INVALIDARG); + EXPECT(dxgi->QueryResourceResidency(dxgi_entry, residency, 1) == E_INVALIDARG); + EXPECT(dxgi->QueryResourceResidency(adapter_entry, residency, 1) == E_INVALIDARG); + EXPECT(residency[0] == DXGI_RESIDENCY(99) && residency[1] == DXGI_RESIDENCY(99)); + + Ref other_device; + if (vulkan) { + EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&other_device.p)))); + } else if (!GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "wine_get_version")) { + const LUID real_luid = real->GetAdapterLuid(); + Ref other_factory; + if (SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&other_factory.p)))) { + Ref warp; + DXGI_ADAPTER_DESC1 other_desc{}; + if (SUCCEEDED(other_factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p))) && + SUCCEEDED(warp->GetDesc1(&other_desc)) && !same_luid(other_desc.AdapterLuid, real_luid)) + D3D12CreateDevice(warp.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&other_device.p)); + for (UINT i = 0; !other_device.p; ++i) { + Ref hw; + if (other_factory->EnumAdapters1(i, &hw.p) == DXGI_ERROR_NOT_FOUND) break; + if (SUCCEEDED(hw->GetDesc1(&other_desc)) && !same_luid(other_desc.AdapterLuid, real_luid)) + D3D12CreateDevice(hw.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&other_device.p)); + } + } + } + if (other_device.p) { + Ref foreign; + EXPECT(SUCCEEDED(other_device->CreateCommittedResource(&buf_heap, D3D12_HEAP_FLAG_NONE, &buf_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&foreign.p)))); + IUnknown* const mixed[2] = {resource.p, foreign.p}; + EXPECT(dxgi->QueryResourceResidency(mixed, residency, 2) == E_INVALIDARG); + EXPECT(residency[0] == DXGI_RESIDENCY(99) && residency[1] == DXGI_RESIDENCY(99)); + } + + EXPECT(dxgi->QueryResourceResidency(good_list, residency, 2) == S_OK); + EXPECT(residency[0] == DXGI_RESIDENCY_FULLY_RESIDENT && residency[1] == DXGI_RESIDENCY_FULLY_RESIDENT); + } // Private data set through the DXGI interface is the device's, and the other way round. const std::uint32_t written = 0x12345678; @@ -331,6 +397,7 @@ int main() { info.device = d.device; info.queue = d.queue; info.queue_family = d.family; + info.buffer_device_address = true; ID3D12Device* inner = nullptr; EXPECT(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&inner)))); inner->AddRef(); // the backend's own reference; the proxy takes the other