From d31444234e91a2feb38aa0b7e4625a0a6f0b6b68 Mon Sep 17 00:00:00 2001 From: Exotic Date: Wed, 25 Nov 2026 03:51:18 +0100 Subject: [PATCH] d3d12: break title buffer device reference cycle --- runtime/shims/d3d12_vulkan_device.cpp | 9 +++++++++ runtime/test/d3d12_draw_test.cpp | 26 +++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index c272e03e97..6ce6444504 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -10264,6 +10264,8 @@ class Device final state_.context->observe_result(state_.context->vk.vkDeviceWaitIdle(state_.info.device), "vkDeviceWaitIdle"); } + for (auto* buffer : title_buffers_) buffer->Release(); + title_buffers_.clear(); state_.context->drain_deleted_resources(UINT64_MAX); state_.context->set_bindless_release(nullptr, nullptr); state_.context->set_bindless_current(nullptr); // the retired generations go with state_.bindless below @@ -13232,6 +13234,13 @@ class Device final log_once("a buffer over the title's memory for a vertex or index view could not be made; HRESULT", int(hr)); return; } + // Internal buffers are owned by the device and must not keep it alive. + auto* resource = Resource::from(made); + if (!resource) { + made->Release(); + return; + } + resource->attach_device(nullptr); title_buffers_.push_back(made); title_buffer_bytes_ += end - start; if (title_buffers_.size() <= 8 || !(title_buffers_.size() & (title_buffers_.size() - 1))) diff --git a/runtime/test/d3d12_draw_test.cpp b/runtime/test/d3d12_draw_test.cpp index d107161584..22ecd074e8 100644 --- a/runtime/test/d3d12_draw_test.cpp +++ b/runtime/test/d3d12_draw_test.cpp @@ -77,7 +77,7 @@ static_assert(offsetof(Vertex, wrong_color) == 16); } // namespace -int main(int argc, char** argv) { +static int run_test(int argc, char** argv) { const bool title_index = argc == 2 && std::strcmp(argv[1], "--title-index-window") == 0; const bool title_vertex = argc == 2 && std::strcmp(argv[1], "--title-vertex-window") == 0; const bool title_window = title_index || title_vertex; @@ -544,6 +544,16 @@ int main(int argc, char** argv) { EXPECT(aliases.shader_va(title_va + 65536) == aliases.shader_va(title_va) + 65536); EXPECT(aliases.shader_va(title_va + 65536) != title_va + 65536); readback->Unmap(0, nullptr); + for (auto* r : {&vertex_buffer, &upload, &readback, &target, &uint_target}) r->reset(); + for (auto* p : {&mrt, &instanced, &refused, &unfed, &xor_pso, &fullscreen}) p->reset(); + pso.reset(); + root.reset(); + rtv_heap.reset(); + list.reset(); + allocator.reset(); + queue.reset(); + fence.reset(); + EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); EXPECT(d.validation_errors() == 0); return 0; } @@ -1242,12 +1252,7 @@ int main(int argc, char** argv) { allocator.reset(); queue.reset(); fence.reset(); - // The device keeps the buffers it made over the title's vertex pages until it goes (make_title_buffer), and - // each holds a reference: the large batch leaves seven of them. - if (std::getenv("LEONIDA_TEST_GEOMETRY_BATCH")) - EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) >= 1); - else - EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); + EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == 1); } unsigned mismatches = 0; @@ -1400,3 +1405,10 @@ int main(int argc, char** argv) { size * size, logic_op ? "an XOR logic op" : "(no logicOp on this GPU)"); return 0; } + +int main(int argc, char** argv) { + const int result = run_test(argc, argv); + if (result != 0) return result; + EXPECT(vktest::validation_error_count() == 0); + return 0; +}