diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index c5f12dc05..8034928c7 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -387,7 +387,8 @@ void local_mirror_table(const DeviceProxy* device, std::uint64_t gpu_handle) { const std::uint64_t offset = (gpu_handle - gpu_start) / kLocalDescriptorBytes * kLocalDescriptorBytes; const std::uint64_t slots = (std::min)(kLocalMirrorSlots, (bytes - offset) / kLocalDescriptorBytes); const SIZE_T copy_bytes = slots * kLocalDescriptorBytes; - if (!local_accessible(range.real_start + offset, copy_bytes, true)) return; + // On Wine, VirtualQuery can report a live vkMapMemory span as MEM_FREE (as in local_write_through). + if (!range.mapped && !local_accessible(range.real_start + offset, copy_bytes, true)) return; const auto copied = copy_title_slots(reinterpret_cast(range.real_start + offset), range.custom_start + offset, copy_bytes); static std::atomic said{0}; @@ -5036,6 +5037,9 @@ void proxy_create_sampler_x(DeviceProxy* self, const D3D12_SAMPLER_DESC* desc, s // A destination in the title's own descriptor range is translated to the real heap's, as device slots 17 to 24 do. handle = translate_cpu_handle(self, D3D12_CPU_DESCRIPTOR_HANDLE{static_cast(dest)}); self->real->CreateSampler(desc, handle); + // And stored at the title's address for a title-backed heap, as slot 22 does: a title that copies its samplers + // from that memory would otherwise copy zeros. + local_write_through(self, D3D12_CPU_DESCRIPTOR_HANDLE{static_cast(dest)}, 1); } // The Xbox slot 67 creates a compute pipeline from native GPU code: (desc, riid, out), named diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index e80825623..ba59d2a5e 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -89,6 +89,8 @@ constexpr std::uint64_t kFenceHandle = 0x1000, kResourceHandle = 0x2000; // the constexpr std::uint64_t kCompletedValue = 7; // what a mock fence's slot 8 returns constexpr UINT kHeapMarker = 0x26262626; // the VisibleNodeMask a mock device's slot 26 answers constexpr SIZE_T kMockHeapCpuStart = 0x55550000ull; +// The CPU start a mock descriptor heap reports; a test that needs the descriptors in memory points it at a buffer. +SIZE_T g_mock_heap_cpu_start = kMockHeapCpuStart; constexpr UINT64 kMockHeapGpuStart = 0x66660000ull; constexpr UINT kMockDescriptorInc = 32; // {5d1e0c4a-2b7f-4e61-9a3d-8c4b2f1e6a70}: an interface the mock queue implements that the proxy does not model. @@ -552,8 +554,8 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { *reinterpret_cast(a3) = g_mock_mapped; } if (m->kind == Kind::heap && slot == 9) { - if (a1) reinterpret_cast(a1)->ptr = kMockHeapCpuStart; - return kMockHeapCpuStart; + if (a1) reinterpret_cast(a1)->ptr = g_mock_heap_cpu_start; + return g_mock_heap_cpu_start; } if (m->kind == Kind::heap && slot == 10) { if (a1) reinterpret_cast(a1)->ptr = kMockHeapGpuStart; @@ -3423,6 +3425,84 @@ int check_descriptor_heap_hooks() { return 0; } +// ---- Title-backed descriptor heaps hold what the real heap holds (RT-XS-DESCRIPTOR-MIRROR-GAPS) ---- +// Both heaps are memory here: the title's backing (from the XMem hook) and the real heap's CPU range (the mock's). +// 1. Slot 61, CreateSamplerX for a title caller, stores the sampler at the title's address too, as slot 22 does. +// 2. A descriptor table bound on a list (slots 35/36) is filled from the title's backing when the real range is a +// mapped one (the descriptor mapping provider says so). On Wine VirtualQuery may call such memory free; here it is +// ordinary memory, so this checks the mapped path works, not the Wine case itself. +bool g_mirror_mapped = false; +bool mock_descriptor_mapping(ID3D12DescriptorHeap*, SIZE_T, SIZE_T) { return g_mirror_mapped; } + +int check_descriptor_mirror() { + alignas(64) static std::uint8_t title[8 * kMockDescriptorInc]; + alignas(64) static std::uint8_t real_heap[8 * kMockDescriptorInc]; + std::memset(title, 0, sizeof(title)); + std::memset(real_heap, 0, sizeof(real_heap)); + g_heap_hook = {}; + xs::set_descriptor_heap_allocator(&mock_heap_alloc, &mock_heap_free); + g_mock_heap_cpu_start = reinterpret_cast(real_heap); + Mock* mock = nullptr; + void* device = make_device(mock, 9, true, true, true); + EXPECT(device); + xs::set_descriptor_mapping_provider(device, &mock_descriptor_mapping); + const IID iid_heap = __uuidof(ID3D12DescriptorHeap); + const SIZE_T title_base = reinterpret_cast(title); + + // 1. A sampler heap backed by `title`; the "driver" writes the sampler into the real heap's slot 3. + g_mirror_mapped = false; + g_heap_hook.next_ptr = title; + D3D12_DESCRIPTOR_HEAP_DESC desc{}; + desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER; + desc.NumDescriptors = 8; + void* heap = nullptr; + EXPECT(SUCCEEDED(hr_of(raw(device, 14, word(&desc), word(&iid_heap), word(&heap)))) && heap); + EXPECT(raw(heap, 9) == title_base); + const SIZE_T off3 = 3 * kMockDescriptorInc; + for (unsigned i = 0; i < kMockDescriptorInc; ++i) real_heap[off3 + i] = std::uint8_t(0x40 + i); + D3D12_SAMPLER_DESC sampler{}; + sampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; + sampler.AddressU = sampler.AddressV = sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP; + xs::set_console_caller([](std::uint64_t) { return true; }); + raw(device, 61, word(&sampler), title_base + off3); + xs::set_console_caller(nullptr); + EXPECT(mock->args[22][1] == reinterpret_cast(real_heap) + off3); // the real heap's slot 3 + EXPECT(std::memcmp(title + off3, real_heap + off3, kMockDescriptorInc) == 0); + EXPECT(title[off3 - 1] == 0 && title[off3 + kMockDescriptorInc] == 0); // that slot only + EXPECT(raw(heap, 2) == 0); + + // 2. A shader-visible heap whose real range is mapped: the title writes slot 2 in its own memory, and binding a + // table that covers it copies the slot into the real heap. + std::memset(title, 0, sizeof(title)); + std::memset(real_heap, 0, sizeof(real_heap)); + g_mirror_mapped = true; + g_heap_hook.next_ptr = title; + desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; + desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + heap = nullptr; + EXPECT(SUCCEEDED(hr_of(raw(device, 14, word(&desc), word(&iid_heap), word(&heap)))) && heap); + const SIZE_T off2 = 2 * kMockDescriptorInc; + for (unsigned i = 0; i < kMockDescriptorInc; ++i) title[off2 + i] = std::uint8_t(0x80 + i); + const IID allocator_iid = __uuidof(ID3D12CommandAllocator); + const IID gcl_iid = __uuidof(ID3D12GraphicsCommandList); + void* allocator = nullptr; + EXPECT(hr_of(raw(device, 9, 0, word(&allocator_iid), word(&allocator))) == S_OK); + void* list = nullptr; + EXPECT(hr_of(raw(device, 12, 0, D3D12_COMMAND_LIST_TYPE_DIRECT, word(allocator), 0, word(&gcl_iid), + word(&list))) == S_OK); + raw(list, 36, 0, kMockHeapGpuStart); // SetGraphicsRootDescriptorTable(0, heap start) + EXPECT(std::memcmp(real_heap + off2, title + off2, kMockDescriptorInc) == 0); + EXPECT(release(list) == 0 && release(allocator) == 0 && raw(heap, 2) == 0); + + g_mirror_mapped = false; + g_mock_heap_cpu_start = kMockHeapCpuStart; + xs::set_descriptor_heap_allocator(nullptr, nullptr); + EXPECT(release(device) == 0 && mock->refs == 0); + std::printf("%s: slot 61 samplers are stored at a title-backed heap's address, and a mapped real range takes a " + "bound table's descriptors from the title's memory\n", g_label); + return 0; +} + // ---- Committed resources lease their size through the title's XMem hooks (RT-5 stop 17) ---- // Slots 76 and 127 allocate the slot-25 size of the resource through the installed hooks with page-size field 2, the // resource holds the lease, and the hooks' free runs once, when the resource is destroyed. @@ -5386,6 +5466,7 @@ int main(int argc, char** argv) { check_fence_completed_value() || check_create_failures() || check_open_shared() || check_alloc_failures() || check_allocators() || check_xbox_slots() || check_lists() || check_signalx_detection() || check_list_tail_names() || check_console_command_list() || + check_descriptor_mirror() || check_custom_heap_properties() || check_resource_allocation_info() || check_slot70() || check_slot70_raw_buffer() || check_slot57_native_graphics() || check_committed_resource3_x() ||