mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 17:28:37 +00:00
d3d12: reap a retired geometry view only after the replays that may name it
drain_geometry_releases retired released views at submitted_retire_ticket(), read in before_execute_. The submission about to run records its own ticket only after that, so it was not covered, and with an idle GPU (completed == submitted) drain_released reaped the view in the same call, before the lists recorded against it ran. A sparse bind in the same replay or another queue's replay can take tickets in between, so submitted + 1 is not enough either. - VulkanContext::set_after_execute: called at the end of every replay that called before_execute_, however it returns, once its submissions are recorded. - GeometryViews::execute_begin/execute_end track the replays in progress. A view retired at kAfterExecutes takes the serial of the last replay begun once it is unreachable, and gets its ticket (submitted_retire_ticket(), read under the views' lock) only when every replay up to that serial has ended. - before_execute_ drains before refresh_shadows(), so this submission's console heap slots are never translated to a view retired in the same call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -52,7 +52,8 @@ class GeometryViews {
|
||||
struct Retired {
|
||||
VkDeviceSize va{};
|
||||
std::size_t size{};
|
||||
std::uint64_t ticket{};
|
||||
std::uint64_t ticket{}; // kAfterExecutes until execute_end() stamps it
|
||||
std::uint64_t epoch{}; // the last execute begun when it was retired (kAfterExecutes only)
|
||||
};
|
||||
std::uint64_t cap_override_{};
|
||||
mutable std::mutex mutex_;
|
||||
@ -108,6 +109,14 @@ class GeometryViews {
|
||||
std::map<Address, Tile> tiles_;
|
||||
std::vector<Retired> retired_;
|
||||
std::atomic<std::size_t> retired_count_{0};
|
||||
// The ExecuteCommandLists replays in progress (execute_begin/execute_end), by the serial each began with. A view
|
||||
// retired at kAfterExecutes may be named by any of them: by a list recorded before the retirement, or by a console
|
||||
// heap slot their before_execute_ translated. It gets its retire ticket only once all of them have recorded their
|
||||
// submissions, so a ticket read then covers them, however many tickets a replay records and on whichever queue.
|
||||
std::mutex execute_mutex_; // a leaf lock
|
||||
std::uint64_t execute_serial_{};
|
||||
std::vector<std::uint64_t> executing_;
|
||||
std::atomic<bool> unstamped_{false};
|
||||
leonida::geometry_writes::ReleaseQueue releases_;
|
||||
std::atomic<std::uint64_t> free_generation_{0};
|
||||
std::uint64_t serial_{}, bytes_{}, cap_refusals_{}, allocation_failures_{}, ready_{}, uploads_{}, uploaded_{};
|
||||
@ -345,8 +354,45 @@ public:
|
||||
return va;
|
||||
}
|
||||
|
||||
// retire_range's ticket for a view that work already in flight may name: the ticket is taken by execute_end()
|
||||
// once every ExecuteCommandLists replay begun by then has recorded its submissions.
|
||||
static constexpr std::uint64_t kAfterExecutes = UINT64_MAX;
|
||||
|
||||
// An ExecuteCommandLists replay starts (before its before_execute_ work): its serial, for execute_end().
|
||||
std::uint64_t execute_begin() {
|
||||
std::lock_guard lock(execute_mutex_);
|
||||
executing_.push_back(++execute_serial_);
|
||||
return execute_serial_;
|
||||
}
|
||||
// The replay that began with `serial` has recorded its submissions. Views retired while only replays now finished
|
||||
// were in progress get submitted() (the context's submitted_retire_ticket(), read here, after those replays
|
||||
// recorded theirs) as their retire ticket. Lock order: mutex_ -> execute_mutex_, as in retire_range.
|
||||
template<class Submitted>
|
||||
void execute_end(std::uint64_t serial, Submitted&& submitted) {
|
||||
{
|
||||
std::lock_guard lock(execute_mutex_);
|
||||
std::erase(executing_, serial);
|
||||
}
|
||||
if (!unstamped_.load(std::memory_order_acquire)) return;
|
||||
std::lock_guard lock(mutex_);
|
||||
std::uint64_t oldest = kAfterExecutes;
|
||||
{
|
||||
std::lock_guard execute(execute_mutex_);
|
||||
for (const auto s : executing_) oldest = (std::min)(oldest, s);
|
||||
}
|
||||
const std::uint64_t ticket = submitted();
|
||||
bool left = false;
|
||||
for (auto& r : retired_) {
|
||||
if (r.ticket != kAfterExecutes) continue;
|
||||
if (r.epoch < oldest) r.ticket = ticket;
|
||||
else left = true;
|
||||
}
|
||||
unstamped_.store(left, std::memory_order_release);
|
||||
}
|
||||
|
||||
// Retires every live view overlapping [begin, end), unlinking it from lookup/refresh/watch immediately and
|
||||
// deferring its backing storage reclamation until GPU submissions through `retire_ticket` have completed.
|
||||
// deferring its backing storage reclamation until GPU submissions through `retire_ticket` have completed
|
||||
// (kAfterExecutes: through the submissions of every replay in progress now, see execute_end).
|
||||
template<class OnRetire = NoopRetire>
|
||||
std::size_t retire_range(Address begin, Address end, std::uint64_t retire_ticket = 0,
|
||||
OnRetire&& on_retire = {}) {
|
||||
@ -374,7 +420,9 @@ public:
|
||||
if (begin < key.first + key.second) stale_refused.push_back(key);
|
||||
}
|
||||
if (ids.empty() && stale_refused.empty()) return 0;
|
||||
if (retire_ticket == kAfterExecutes && !ids.empty()) unstamped_.store(true, std::memory_order_release);
|
||||
callbacks.reserve(ids.size());
|
||||
const std::size_t first_retired = retired_.size();
|
||||
for (const auto id : ids) {
|
||||
View& v = views_[id];
|
||||
detach_tiles_locked(id, v.source, v.size);
|
||||
@ -383,7 +431,7 @@ public:
|
||||
v.ready = false;
|
||||
}
|
||||
callbacks.push_back({v.source, v.size, v.va});
|
||||
retired_.push_back({v.va, v.size, retire_ticket});
|
||||
retired_.push_back({v.va, v.size, retire_ticket, 0});
|
||||
}
|
||||
if (!ids.empty()) {
|
||||
std::unique_lock structure(structure_mutex_);
|
||||
@ -399,6 +447,16 @@ public:
|
||||
for (const auto id : ids) fast_.erase({views_[id].source, views_[id].size});
|
||||
for (const auto& k : stale_refused) fast_.erase(k);
|
||||
}
|
||||
// No resolve() or find() answers these views from here on, so a replay that begins now cannot name them:
|
||||
// they wait only for the replays begun so far.
|
||||
if (retire_ticket == kAfterExecutes && !ids.empty()) {
|
||||
std::uint64_t epoch = 0;
|
||||
{
|
||||
std::lock_guard execute(execute_mutex_);
|
||||
epoch = execute_serial_;
|
||||
}
|
||||
for (std::size_t i = first_retired; i < retired_.size(); ++i) retired_[i].epoch = epoch;
|
||||
}
|
||||
for (const auto& k : stale_refused) refused_.erase(k);
|
||||
for (const auto id : ids) {
|
||||
free_ids_.push_back(id);
|
||||
@ -421,7 +479,7 @@ public:
|
||||
std::size_t keep = 0;
|
||||
for (std::size_t i = 0; i < retired_.size(); ++i) {
|
||||
const Retired& r = retired_[i];
|
||||
if (r.ticket <= completed_ticket) {
|
||||
if (r.ticket != kAfterExecutes && r.ticket <= completed_ticket) {
|
||||
pool_.free(device, r.va, r.size);
|
||||
bytes_ -= r.size;
|
||||
++reaped_;
|
||||
@ -646,6 +704,7 @@ public:
|
||||
free_ids_.clear();
|
||||
retired_.clear();
|
||||
retired_count_.store(0, std::memory_order_release);
|
||||
unstamped_.store(false, std::memory_order_release);
|
||||
live_views_ = 0;
|
||||
bytes_ = 0;
|
||||
ready_ = 0;
|
||||
|
||||
@ -2582,12 +2582,33 @@ struct DeviceState {
|
||||
aliases.remove(source, va);
|
||||
if (const auto twin = leonida::title_gpu_views::other_view(source)) aliases.remove(twin, va);
|
||||
}
|
||||
// Before the submission's console heap slots are translated (refresh_shadows): a view retired here is answered
|
||||
// by nothing from now on. It is freed only after every replay in progress has recorded its submissions and those
|
||||
// have completed (GeometryViews::kAfterExecutes, execute_end): this submission's own lists, recorded before, and
|
||||
// another queue's replay that translated its slots before this retirement may still name it. A ticket read here
|
||||
// would be one short of this submission, which records its own only after before_execute_ (and with an idle GPU
|
||||
// the view would be reaped in this very call).
|
||||
void drain_geometry_releases() {
|
||||
if (!context || !geometry.has_pending_reclaim()) return;
|
||||
const std::uint64_t retire_ticket = context->submitted_retire_ticket();
|
||||
const std::uint64_t completed_ticket =
|
||||
geometry.has_retired() || geometry.has_released() ? context->completed_retire_ticket() : 0;
|
||||
geometry.drain_released(*this, retire_ticket, completed_ticket, forget_geometry_alias);
|
||||
const std::uint64_t completed_ticket = geometry.has_retired() ? context->completed_retire_ticket() : 0;
|
||||
geometry.drain_released(*this, rt::d3d12::GeometryViews::kAfterExecutes, completed_ticket,
|
||||
forget_geometry_alias);
|
||||
}
|
||||
// ExecuteCommandLists replays in progress, for GeometryViews::kAfterExecutes (see drain_geometry_releases).
|
||||
static std::vector<std::uint64_t>& geometry_execute_serials() {
|
||||
thread_local std::vector<std::uint64_t> serials;
|
||||
return serials;
|
||||
}
|
||||
void geometry_execute_begin() {
|
||||
if (!geometry.cap()) return;
|
||||
geometry_execute_serials().push_back(geometry.execute_begin());
|
||||
}
|
||||
void geometry_execute_end() {
|
||||
auto& serials = geometry_execute_serials();
|
||||
if (!geometry.cap() || serials.empty()) return;
|
||||
const std::uint64_t serial = serials.back();
|
||||
serials.pop_back();
|
||||
geometry.execute_end(serial, [this] { return context->submitted_retire_ticket(); });
|
||||
}
|
||||
static rt::d3d12::GeometryCopy copy_geometry(std::uint8_t* to, const std::uint8_t* from, std::size_t size) {
|
||||
using Result = rt::d3d12::GeometryCopy;
|
||||
@ -13522,6 +13543,7 @@ public:
|
||||
}
|
||||
if (async_texture_refresh()) start_title_filler();
|
||||
state_.setup_console_heap_gpu(get_device); // LEONIDA_CONSOLE_HEAP_GPU: the context's prelude
|
||||
state_.context->set_after_execute([this] { state_.geometry_execute_end(); });
|
||||
state_.context->set_before_execute([this] {
|
||||
namespace phases = leonida::submit_phases;
|
||||
// LEONIDA_REFRESH_PER_FRAME (for runs that only need the title to go on): 1 = the slot-133 shadows, title
|
||||
@ -13534,13 +13556,14 @@ public:
|
||||
const char* v = std::getenv("LEONIDA_REFRESH_PER_FRAME");
|
||||
return v && *v ? std::atoi(v) : 0;
|
||||
}();
|
||||
state_.geometry_execute_begin();
|
||||
bool full = true;
|
||||
if (per_frame) {
|
||||
const std::uint64_t serial = phases::totals().present_serial.load(std::memory_order_relaxed);
|
||||
full = refreshed_present_.exchange(serial, std::memory_order_relaxed) != serial;
|
||||
}
|
||||
state_.refresh_shadows(full || per_frame < 2, full);
|
||||
state_.drain_geometry_releases();
|
||||
state_.refresh_shadows(full || per_frame < 2, full);
|
||||
// LEONIDA_GEOMETRY_ASYNC=1 (for runs that only need the title to go on): the geometry copies are brought
|
||||
// up to date on a worker of ours, started here and not waited for, instead of on the title's GPU submit
|
||||
// thread (speed-27: 230-330 ms a refresh after the load, a VirtualQuery and VirtualProtect per written
|
||||
|
||||
@ -1887,6 +1887,9 @@ public:
|
||||
return it == device_fault_->pipeline_names.end() ? nullptr : it->second[0];
|
||||
}
|
||||
void set_before_execute(std::function<void()> before) { before_execute_ = std::move(before); }
|
||||
// Called at the end of every ExecuteCommandLists replay that called before_execute_, however it returns: by then
|
||||
// the submissions it made are recorded, so submitted_retire_ticket() covers them.
|
||||
void set_after_execute(std::function<void()> after) { after_execute_ = std::move(after); }
|
||||
// GPU work the lists of a submission depend on, recorded at the start of its command buffer by a queue that can
|
||||
// run compute (the console heap translation pass, src/console_heap_gpu.h): `record` returns true when it recorded
|
||||
// something, and `commit` follows that submit (true: it reached the queue; the ticket is a retire ticket at or
|
||||
@ -2397,6 +2400,7 @@ private:
|
||||
resolve_passes_;
|
||||
std::map<std::tuple<VkImage, VkImage, VkImageView, VkImageView, VkRenderPass>, VkFramebuffer> resolve_framebuffers_;
|
||||
std::function<void()> before_execute_;
|
||||
std::function<void()> after_execute_;
|
||||
InputProbe input_probe_;
|
||||
TableScanner table_scanner_;
|
||||
GeometryProvider geometry_provider_;
|
||||
@ -3156,6 +3160,12 @@ private:
|
||||
if (context_.is_device_lost() || !wait_for_room()) return queue_unsupported(__LINE__);
|
||||
}
|
||||
if (context_.before_execute_) context_.before_execute_();
|
||||
struct AfterExecute {
|
||||
VulkanContext& context;
|
||||
~AfterExecute() {
|
||||
if (context.after_execute_) context.after_execute_();
|
||||
}
|
||||
} after_execute{context_};
|
||||
// The rest of this call is the replay phase; the submission is counted when it returns, however it returns.
|
||||
struct Replayed {
|
||||
std::int64_t start = submit_phases::now_ns();
|
||||
|
||||
Reference in New Issue
Block a user