diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 72bf02bace..c8ea7ac884 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -2420,27 +2420,31 @@ struct DeviceState { if (!leonida::title_va_aliases().title_memory(va, lo, hi) || va >= hi) return; const std::uint64_t end = va + std::min(bytes, hi - va); std::lock_guard lock(title_write_back_mutex); - auto it = title_write_back.lower_bound(va); - if (it != title_write_back.begin()) { - auto previous = std::prev(it); - if (previous->second >= va) { - if (previous->second < end) previous->second = end; - return; - } - } - if (it != title_write_back.end() && it->first <= end) { - it->second = std::max(it->second, end); - return; - } - if (title_write_back.size() >= kTitleWriteBackRanges || title_write_back_bytes >= kTitleWriteBackBytes) { + auto first = title_write_back.lower_bound(va); + if (first != title_write_back.begin() && std::prev(first)->second >= va) --first; + if (first != title_write_back.end() && first->first <= va && first->second >= end) return; + + // Charge only newly covered bytes, even if this write joins several queued ranges. + std::uint64_t begin = va, merged_end = end, replaced_bytes = 0; + auto last = first; + while (last != title_write_back.end() && last->first <= merged_end) { + begin = std::min(begin, last->first); + merged_end = std::max(merged_end, last->second); + replaced_bytes += last->second - last->first; + ++last; + } + const std::uint64_t added_bytes = (merged_end - begin) - replaced_bytes; + if ((first == last && title_write_back.size() >= kTitleWriteBackRanges) || + added_bytes > kTitleWriteBackBytes - title_write_back_bytes) { if (!title_write_back_refused++) log_once("past LEONIDA_TITLE_WRITEBACK's caps: reached ranges are not copied back this submission; " "ranges refused:", 1); return; } - title_write_back_bytes += end - va; - title_write_back.emplace(va, end); + auto kept = title_write_back.insert_or_assign(first, begin, merged_end); + title_write_back.erase(std::next(kept), last); + title_write_back_bytes += added_bytes; } // What the GPU wrote into those copies goes back to the title's pages, so a later read by the title or the next diff --git a/runtime/test/d3d12_title_va_test.cpp b/runtime/test/d3d12_title_va_test.cpp index 162af852fb..cfab8987c0 100644 --- a/runtime/test/d3d12_title_va_test.cpp +++ b/runtime/test/d3d12_title_va_test.cpp @@ -152,6 +152,7 @@ D3D12_SHADER_RESOURCE_VIEW_DESC raw_srv(UINT first_word, UINT words) { return desc; } +constexpr UINT kCodeWord = 6000; // word 1 of the synthetic shader container constexpr UINT kElements = 64, kBytes = kElements * 4; constexpr UINT64 kPages = 64 * 1024; constexpr UINT kSrvWord = 256; // the V# reads title words 256..319 @@ -532,10 +533,12 @@ struct WriteBack { ID3D12Resource* out; ID3D12DescriptorHeap* heap; UINT64 gpu; + ID3D12CommandQueue* queue = nullptr; // the context's when null }; -// One submission that dispatches through a root UAV at `va` and waits for it. False on a failed call. -int submit_write(WriteBack& s, std::uint64_t va) { +// One submission that dispatches through a root UAV at `va` (and at `va2` too, when given) and waits for it. With +// `wait` false it is only executed (no signal: the caller knows when it is done). False on a failed call. +int submit_write(WriteBack& s, std::uint64_t va, std::uint64_t va2 = 0, bool wait = true) { EXPECT(s.allocator->Reset() == S_OK); EXPECT(s.list->Reset(s.allocator, s.c->pso) == S_OK); ID3D12DescriptorHeap* heaps[] = {s.heap}; @@ -546,6 +549,10 @@ int submit_write(WriteBack& s, std::uint64_t va) { s.list->SetComputeRootUnorderedAccessView(1, va); s.list->SetComputeRootDescriptorTable(2, D3D12_GPU_DESCRIPTOR_HANDLE{s.gpu}); s.list->Dispatch(1, 1, 1); + if (va2) { + s.list->SetComputeRootUnorderedAccessView(1, va2); + s.list->Dispatch(1, 1, 1); + } D3D12_RESOURCE_BARRIER to_copy{}; to_copy.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; to_copy.Transition.pResource = s.out; @@ -555,9 +562,11 @@ int submit_write(WriteBack& s, std::uint64_t va) { s.list->ResourceBarrier(1, &to_copy); EXPECT(s.list->Close() == S_OK); ID3D12CommandList* lists[] = {s.list}; - s.c->queue->ExecuteCommandLists(1, lists); - EXPECT(rt::d3d12::last_execute_result(s.c->queue) == leonida::ReplayResult::success); - EXPECT(SUCCEEDED(s.c->queue->Signal(s.c->fence, ++s.c->fence_value))); + ID3D12CommandQueue* queue = s.queue ? s.queue : s.c->queue; + queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(queue) == leonida::ReplayResult::success); + if (!wait) return 0; + EXPECT(SUCCEEDED(queue->Signal(s.c->fence, ++s.c->fence_value))); s.c->fence->SetEventOnCompletion(s.c->fence_value, nullptr); // a null event waits, as D3D12 does return 0; } @@ -566,13 +575,14 @@ int submit_write(WriteBack& s, std::uint64_t va) { // reads back): the pages are not imported here, so the buffer is a shadow, and the write of a root UAV over the // title's VA must reach the pages at the next submission - and the title's own newer words must still win. int check_placed_write_back(Context& c, bool geometry) { - void* pages = VirtualAlloc(nullptr, kPages, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); + constexpr UINT64 allocation_bytes = 2 * kPages; + void* pages = VirtualAlloc(nullptr, allocation_bytes, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); EXPECT(pages); const auto title_va = reinterpret_cast(pages); auto* words = static_cast(pages); - for (UINT i = 0; i < kPages / 4; ++i) words[i] = 0xA0000000u + i * 3u; // never zero, unlike the shader's rows + for (UINT i = 0; i < allocation_bytes / 4; ++i) words[i] = 0xA0000000u + i * 3u; // never zero, unlike the shader's rows auto& aliases = leonida::title_va_aliases(); - aliases.add_title_memory(title_va, kPages); // what the xmem shims do for the title's allocations + aliases.add_title_memory(title_va, allocation_bytes); // what the xmem shims do for the title's allocations const D3D12_RESOURCE_DESC desc = buffer_desc(kPages); Ref placed; EXPECT(SUCCEEDED( @@ -636,6 +646,38 @@ int check_placed_write_back(Context& c, bool geometry) { std::printf("placed buffer write back: the title's own words survived %u/%u\n", kept, kElements); EXPECT(kept == kElements); + // Two distinct placed buffers share one title allocation. Record both before either range retires. + auto* upper_pages = static_cast(pages) + kPages; + const UINT64 upper_va = title_va + kPages; + Ref upper; + EXPECT(SUCCEEDED(rt::d3d12::create_buffer_at_host_address(c.inner, upper_pages, desc, 0xFFFFFFFFu, + IID_PPV_ARGS(&upper.p)))); + EXPECT(upper->GetGPUVirtualAddress() == upper_va); + const UINT64 cpu = reinterpret_cast(slot(heap.p, 9))(heap.p); + EXPECT(cpu); + constexpr UINT page_words = UINT(kPages / sizeof(*words)); + for (bool upper_first : {true, false}) { + const UINT64 first = upper_first ? upper_va : title_va; + const UINT64 second = upper_first ? title_va : upper_va; + // cs_user_data adds the table's four words, its code word and the lane index. + // Point it past the output in the first buffer so each ordering has a different result. + const UINT64 table_data = aliases.shader_va(first) + kBytes; + std::memcpy(reinterpret_cast(cpu), &table_data, sizeof(table_data)); + EXPECT(submit_write(s, first, second) == 0); + EXPECT(submit_write(s, first, second) == 0); // retire the first submission's writes + const UINT source_word = (upper_first ? page_words : 0u) + kElements; + for (UINT i = 0; i < allocation_bytes / sizeof(*words); ++i) { + const UINT lane = i % page_words; + const UINT expected = lane < kElements ? 0xA0000000u + (source_word + lane % 4) * 3u + kCodeWord + lane + : 0xA0000000u + i * 3u; + if (words[i] != expected) { + std::fprintf(stderr, "adjacent buffers (%s first): word %u got %08x, expected %08x\n", + upper_first ? "upper" : "lower", i, words[i], expected); + return 1; + } + } + } + upper.reset(); placed.reset(); // the shadow goes, the pages stay writable aliases.remove_title_memory(title_va); VirtualFree(pages, 0, MEM_RELEASE); @@ -1202,7 +1244,7 @@ int main(int argc, char** argv) { EXPECT(device_status == vktest::DeviceStatus::ready); std::vector fssm = {'F', 'S', 'S', 'M'}; - put32(fssm, 6000); + put32(fssm, kCodeWord); for (std::uint32_t i = 0; i < 14; ++i) put32(fssm, 0xbf810000u + i); const auto container = shader_container(fssm); const testfiles::ScratchDir scratch("leonida_title_va_test");