d3d12_vulkan: a texture destroyed inside the registry leaves gpu_placements at once

Review of #1798: the submit path reads gpu_placements under descriptor_mutex alone, so the
placement of a texture destroyed inside the registry must go before ~Resource frees it.
descriptor_mutex is never held there (~Resource takes it itself, in image_owner and
forget_image); only the title_textures erase waits for drain_titles_gone. log_title_census
skips an entry without a resource, and refresh_title_textures leaves the registry scope
before it unlocks.
This commit is contained in:
ImLele69
2026-11-25 16:57:17 +01:00
parent 5f549cf5f3
commit 152cee2550

View File

@ -2199,7 +2199,8 @@ struct DeviceState {
void title_texture_unmapped(const void* resource, std::uint64_t address); // the title wrote texels
void title_texture_gone(const void* resource, std::uint64_t address);
// Textures destroyed while their thread held title_texture_mutex (inside the registry, t_in_title_textures): a
// descriptor it overwrote held the last reference. title_texture_gone cannot take the mutex again there (MSVC's
// descriptor it overwrote held the last reference. Their GPU placement is removed at once; title_texture_gone
// cannot take the registry mutex again there (MSVC's
// std::mutex throws resource_deadlock_would_occur, which Wine's msvcp140 aborts on), so their entries only stop
// naming the resource and are removed by drain_titles_gone once the mutex is free. Under title_texture_mutex.
std::vector<std::pair<const void*, std::uint64_t>> titles_gone;
@ -10161,7 +10162,13 @@ void DeviceState::title_texture_unmapped(const void* resource, std::uint64_t add
}
void DeviceState::title_texture_gone(const void* resource, std::uint64_t address) {
if (t_in_title_textures) { // this thread holds title_texture_mutex, and perhaps descriptor_mutex
if (t_in_title_textures) { // this thread holds title_texture_mutex
{
// Never held here: ~Resource takes descriptor_mutex itself (image_owner, forget_image). The submit path
// reads gpu_placements under descriptor_mutex alone, so the placement must not outlive the resource.
std::lock_guard descriptors(descriptor_mutex);
gpu_placements.remove(address, resource);
}
const auto it = title_textures.find(address);
if (it != title_textures.end() && it->second->resource == resource) it->second->resource = nullptr;
titles_gone.push_back({resource, address});
@ -10181,10 +10188,6 @@ void DeviceState::drain_titles_gone() {
if (!titles_gone_pending.load(std::memory_order_acquire)) return;
std::lock_guard lock(title_texture_mutex);
for (const auto& [resource, address] : titles_gone) {
{
std::lock_guard descriptors(descriptor_mutex);
gpu_placements.remove(address, resource);
}
const auto it = title_textures.find(address);
if (it != title_textures.end() && !it->second->resource) title_textures.erase(it);
}
@ -11888,12 +11891,13 @@ public:
std::unique_lock lock(state_.title_texture_mutex, std::try_to_lock);
if (!lock.owns_lock()) return; // a slot-70 view is filling one; next time
title_refreshed_.store(ticks, std::memory_order_relaxed);
const InTitleTextures inside;
std::optional<InTitleTextures> inside(std::in_place);
TitleTextureStats& stats = title_stats_;
++stats.passes;
// A fill this pass makes can submit, and a submission destroys a retired image, whose destructor erases its
// registry entry (title_texture_gone). The registry mutex is recursive for its owner on Windows, so that
// erase lands inside this pass: every walk below visits a snapshot, never an iterator into the registry.
// A fill this pass makes can submit, and a submission destroys a retired image, whose destructor reaches
// title_texture_gone while this thread holds the registry mutex: the entry stops naming the resource and is
// erased by drain_titles_gone after the pass. Every walk below visits a snapshot, never an iterator into the
// registry, and skips an entry without a resource.
const auto entries = leonida::title_texture_snapshot(state_.title_textures);
title_pass_ = true;
std::uint64_t checked = 0, filled = 0, fill_pass_ns = 0;
@ -12025,6 +12029,7 @@ public:
stats.last_log = now;
log_title_census("refresh");
}
inside.reset();
lock.unlock();
state_.drain_titles_gone();
for (Resource* r : release) r->Release();
@ -12714,7 +12719,7 @@ private:
VkDeviceSize owned_bytes = 0, placed_bytes = 0, host_heap_bytes = 0;
for (const auto& [address, t] : state_.title_textures) {
(t->owned ? owned : slot133) += 1;
if (!t->owned && t->unbound_bytes) t->set_resource(t->resource);
if (!t->owned && t->resource && t->unbound_bytes) t->set_resource(t->resource);
mapped += t->mapped;
filled += t->fills > 0;
(t->owned ? owned_bytes : placed_bytes) += t->backing_bytes;