Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3624,6 +3624,10 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_geometry_views COMMAND d3d12_title_va_test)
set_tests_properties(d3d12_geometry_views PROPERTIES ENVIRONMENT "LEONIDA_GEOMETRY_MB=16" SKIP_RETURN_CODE 77)
set_tests_properties(d3d12_title_va PROPERTIES SKIP_RETURN_CODE 77)
# Past LEONIDA_TITLE_WINDOWS_MB a view of the title's memory is never its raw VA (#1537): with no other backing it
# is written zero-size, and a dispatch through it reads zeros.
add_test(NAME d3d12_title_window_cap COMMAND d3d12_title_va_test --window-cap)
set_tests_properties(d3d12_title_window_cap PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_TITLE_WINDOWS_MB=0")
add_test(NAME d3d12_api_descriptor_copy COMMAND d3d12_title_va_test --api-descriptor-copy)
set_tests_properties(d3d12_api_descriptor_copy PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_DESC_MIRROR=1")
add_test(NAME d3d12_api_descriptor_copy_off COMMAND d3d12_title_va_test --api-descriptor-copy-off)
Expand Down
106 changes: 106 additions & 0 deletions runtime/test/d3d12_title_va_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,95 @@ static std::uintptr_t source_tile, peer_tile;
void aliases_of_test_tile(std::uintptr_t va, std::vector<std::uintptr_t>& peers) {
if (va == source_tile) peers.push_back(peer_tile);
}
// LEONIDA_TITLE_WINDOWS_MB=0: no window fits the cap. A view of the title's memory must then never name the title's
// raw VA (one GPU read of that loses the device, #1537). The pages here are private, uncopied memory, so no geometry
// copy backs them either: both views are written zero-size (no base, no records), and a dispatch through one reads
// zeros, not the title's bytes.
int check_window_overflow(Context& c) {
constexpr UINT64 ring_bytes = 256 * 1024;
auto* ring = static_cast<std::uint32_t*>(VirtualAlloc(nullptr, ring_bytes, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
EXPECT(ring);
const auto ring_va = reinterpret_cast<std::uint64_t>(ring);
EXPECT(ring_va >= c.arena && !(ring_va & 0xFFFF));
for (UINT i = 0; i < ring_bytes / 4; ++i) ring[i] = 0xC0000000u + i * 7u; // never zero
auto& aliases = leonida::title_va_aliases();
aliases.add_title_memory(ring_va, ring_bytes);

D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 8,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
Ref<ID3D12DescriptorHeap> heap;
EXPECT(SUCCEEDED(c.device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap.p))));
const std::uint64_t cpu = reinterpret_cast<XboxHeapHandleFn>(slot(heap.p, 9))(heap.p);
const std::uint64_t gpu = reinterpret_cast<XboxHeapHandleFn>(slot(heap.p, 10))(heap.p);
const UINT inc = c.device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
const D3D12_CONSTANT_BUFFER_VIEW_DESC first{ring_va + 0x2000, 256}, second{ring_va + 0x30000, 256};
c.device->CreateConstantBufferView(&first, D3D12_CPU_DESCRIPTOR_HANDLE{cpu});
c.device->CreateConstantBufferView(&second, D3D12_CPU_DESCRIPTOR_HANDLE{cpu + inc});
const auto a = vsharp_base(cpu), b = vsharp_base(cpu + inc);
const auto records = [](std::uint64_t at) {
std::uint32_t w[4];
std::memcpy(w, reinterpret_cast<const void*>(at), sizeof(w));
return w[2];
};
const auto named = [&](std::uint64_t base, std::uint64_t raw) {
return base == raw ? "the raw VA" : !base ? "nothing (zero-size)" : base < c.arena ? "the arena" : "elsewhere";
};
std::printf("window cap 0: views of the title's memory named %s and %s\n", named(a, first.BufferLocation),
named(b, second.BufferLocation));
EXPECT(a != first.BufferLocation && b != second.BufferLocation);
EXPECT(a == 0 && records(cpu) == 0);
EXPECT(b == 0 && records(cpu + inc) == 0);
EXPECT(aliases.shader_va(first.BufferLocation) == first.BufferLocation); // no window was made

const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, readback_heap{D3D12_HEAP_TYPE_READBACK};
const auto uav = buffer_desc(kBytes, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
const auto readback_desc = buffer_desc(kBytes);
Ref<ID3D12Resource> out, readback;
EXPECT(SUCCEEDED(c.device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &uav,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr,
IID_PPV_ARGS(&out.p))));
EXPECT(SUCCEEDED(c.device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &readback_desc,
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&readback.p))));
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
EXPECT(SUCCEEDED(c.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
EXPECT(SUCCEEDED(c.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, c.pso,
IID_PPV_ARGS(&list.p))));
ID3D12DescriptorHeap* heaps[] = {heap.p};
list->SetDescriptorHeaps(1, heaps);
list->SetComputeRootSignature(c.root);
list->SetComputeRoot32BitConstant(0, 0, 0); // the CBV mode of check_title_memory: its first four words
list->SetComputeRoot32BitConstant(0, 0, 1);
list->SetComputeRootUnorderedAccessView(1, out->GetGPUVirtualAddress());
list->SetComputeRootDescriptorTable(2, D3D12_GPU_DESCRIPTOR_HANDLE{gpu});
list->Dispatch(1, 1, 1);
D3D12_RESOURCE_BARRIER to_copy{};
to_copy.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
to_copy.Transition.pResource = out.p;
to_copy.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
to_copy.Transition.StateBefore = D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
to_copy.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
list->ResourceBarrier(1, &to_copy);
list->CopyBufferRegion(readback.p, 0, out.p, 0, kBytes);
EXPECT(list->Close() == S_OK);
ID3D12CommandList* lists[] = {list.p};
c.queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(c.queue) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(c.queue->Signal(c.fence, ++c.fence_value)));
EXPECT(SUCCEEDED(c.fence->SetEventOnCompletion(c.fence_value, nullptr)));
void* mapped = nullptr;
EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped)));
const auto* got = static_cast<const std::uint32_t*>(mapped);
UINT zeros = 0;
for (UINT i = 0; i < kElements; ++i) zeros += got[i] == 0;
readback->Unmap(0, nullptr);
std::printf("window cap 0: a dispatch through the zero-size view read %u/%u zero words\n", zeros, kElements);
EXPECT(zeros == kElements);
aliases.remove_title_memory(ring_va);
VirtualFree(ring, 0, MEM_RELEASE);
return 0;
}
// These fixtures register their own pairs without XMem. Retire each before Wine can reuse either VA.
void remove_test_pair(std::uintptr_t gpu, std::uintptr_t cpu) {
auto& state = leonida::title_gpu_views::state();
Expand Down Expand Up @@ -905,6 +994,14 @@ int main(int argc, char** argv) {
const bool second_device = argc == 2 && std::strcmp(argv[1], "--rebound-second-device") == 0;
const bool api_copy = api_copy_off || (argc == 2 && std::strcmp(argv[1], "--api-descriptor-copy") == 0);
const bool rebind = api_copy || mirror_off || second_device || (argc == 2 && std::strcmp(argv[1], "--rebound-tables") == 0);
const bool window_cap = argc == 2 && std::strcmp(argv[1], "--window-cap") == 0;
if (window_cap) {
const char* cap = std::getenv("LEONIDA_TITLE_WINDOWS_MB");
if (!cap || std::strcmp(cap, "0")) {
std::fprintf(stderr, "FAIL: --window-cap needs LEONIDA_TITLE_WINDOWS_MB=0\n");
return 1;
}
}
const char* setting = std::getenv("LEONIDA_GEOMETRY_MB");
const bool geometry = setting && std::atoi(setting) > 0;
vktest::Device d;
Expand Down Expand Up @@ -1003,6 +1100,15 @@ int main(int argc, char** argv) {
EXPECT(d.validation_errors() == 0);
return 0;
}
if (window_cap) {
if (check_window_overflow(c)) return 1;
if (d.validation_errors()) {
std::fprintf(stderr, "%u validation errors\n", d.validation_errors());
return 1;
}
std::printf("title memory past the window cap: the views were zero-size and the shader read zeros\n");
return 0;
}
if (geometry && check_declined_imports(c)) return 1;
if (geometry && check_import_over_shadow(c)) return 1;
if (check_kind(c, "read-write", PAGE_READWRITE, checks)) return 1;
Expand Down
Loading