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
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ class Recorder {
// After the calling worker recorded (or dropped) its queued labels: its ranges are cleared and
// the entries of its dwords still without a batch (stored by the CPU, or dropped) leave the table.
static void ForgetQueuedLabels();
static std::uint64_t ThreadClearAccesses();
static bool QueuedLabelsPending();
// The function the flush hook calls, on the accessing thread (which may hold the GPU mutex),
// when an access overlaps one of that thread's queued labels; it records them. Set once.
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class StorageTexture : public std::enable_shared_from_this<StorageTexture> {
// as it was, and one that finds it unchanged since an earlier read knows the registry is what
// it was then.
static std::uint64_t PendingSerial();
static bool PendingClearOnThread(std::uint64_t address, std::size_t bytes);
// One registry scan for several ranges (the fast Revalidate): each query's `overlaps` receives
// whether an image other than `except` has results pending, or is being stored right now, over
// [begin, end), and `found` the image FindPending would return for the range (null: none);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ std::size_t PublishShadow(std::uint64_t address, std::size_t bytes, PublishScope
// hook-free readers and a build's stage A.
bool AnyShadowedOverlaps(std::uint64_t address, std::size_t bytes);
bool AnyShadowedOverlaps(std::span<const std::pair<std::uint64_t, std::uint64_t>> ranges);
bool LiveUnitsClear(std::uint64_t address, std::size_t bytes);
bool AnyPublishInProgress();
std::vector<std::pair<std::uint64_t, std::uint64_t>> LiveShadowedRanges(std::uint64_t address, std::size_t bytes, PublishScope scope);

Expand Down
49 changes: 45 additions & 4 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "prx/libSceAgcDriver/Execution/include/ProfileCounter.hpp"
#include "prx/libc/include/HostThreadLocal.hpp"
#include "prx/libc/include/HostThreadSlot.hpp"
#include "prx/libSceAgcDriver/Execution/include/ThreadScratch.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Recorder.hpp"
#include "prx/libSceAgcDriver/Graphics/include/BdaResources.hpp"
#include "prx/libSceAgcDriver/Graphics/include/ReferenceStorage.hpp"
Expand Down Expand Up @@ -579,7 +580,9 @@ Recorder* labelTableOwner = nullptr;
// ForgetQueuedLabels): the flush hook, on the same thread, has them recorded before an access that
// overlaps one, through the function the driver installed.
struct QueuedLabelRangesTag {};
auto& QueuedLabelRanges() { return HostThreadLocal<std::vector<std::pair<std::uint64_t, std::uint64_t>>, QueuedLabelRangesTag>(); }
auto& QueuedLabelRanges() { return ThreadScratch<std::vector<std::pair<std::uint64_t, std::uint64_t>>, QueuedLabelRangesTag>(); }
struct QueuedLabelsOnThreadTag {};
using QueuedLabelsOnThread = HostThreadSlot<bool, QueuedLabelsOnThreadTag>;
std::atomic<void (*)()> queuedLabelRecorder{nullptr};
// The dwords (with their stamps) this thread noted since its last CloseLabelGroup (see Recorder::CloseLabelGroup).
struct LabelGroupDwordsTag {};
Expand Down Expand Up @@ -694,6 +697,34 @@ bool SnapshotOverlaps(std::uint64_t address, std::size_t bytes) {
return SnapshotOverlaps(snapshot.get(), address, bytes);
}

bool HookClearAccess() {
static const bool enabled = std::getenv("APS5_NO_HOOK_CLEAR_ACCESS") == nullptr;
return enabled;
}

struct ThreadWriteRanges {
std::uint64_t generation = ~std::uint64_t{0};
std::shared_ptr<const WriteRanges> ranges;
};
struct ThreadWriteRangesTag {};

bool ThreadSnapshotOverlaps(std::uint64_t address, std::size_t bytes) {
auto& copy = ThreadScratch<ThreadWriteRanges, ThreadWriteRangesTag>();
const auto generation = publishGeneration.load(std::memory_order_acquire);
if (copy.generation != generation) {
copy.ranges = pendingWrites.load(std::memory_order_acquire);
copy.generation = generation;
}
return SnapshotOverlaps(copy.ranges.get(), address, bytes);
}

struct ClearAccessesTag {};
using ClearAccesses = HostThreadSlot<std::uint64_t, ClearAccessesTag>;

bool ClearAccess(std::uint64_t address, std::size_t bytes) {
return StorageTexture::PendingClearOnThread(address, bytes) && LiveUnitsClear(address, bytes) && !ThreadSnapshotOverlaps(address, bytes);
}

// Whether one merged range of the snapshot contains [address, end) entirely.
bool SnapshotCovers(const WriteRanges* snapshot, std::uint64_t address, std::uint64_t end) {
if (snapshot == nullptr || snapshot->empty()) return false;
Expand Down Expand Up @@ -1044,22 +1075,26 @@ class RecordedStoreSyncScope {
// work first; then storage image results pending for the range are stored. The recorder is only
// dereferenced under the GpuMutex (it is destroyed with its device under that lock).
void FlushForAccess(std::uint64_t address, std::size_t bytes) {
hookCalls.fetch_add(1, std::memory_order_relaxed);
if (DrawProfiled()) hookCalls.fetch_add(1, std::memory_order_relaxed);
// A label this worker queued but has not recorded writes the range: in queue order the label
// precedes this access, so the group is recorded first (it then notes the range as a pending
// write of the open batch, and the check below syncs on it like any recorded store). Not from
// inside a completion action (a write-back store made while a batch is reaped): the record
// would reap nested in the reap in progress, landing later batches' write-backs before the
// rest of this one's, and this queue's queued label follows every batch of its own in flight
// anyway, so the write-back landing first is the order the hardware gives.
if (!QueuedLabelRanges().empty() && QueuedLabelOverlaps(address, bytes)) {
if (QueuedLabelsOnThread::Get() && QueuedLabelOverlaps(address, bytes)) {
if (completionDepth() != 0 && HookCompletionGuard()) {
queuedLabelHookInCompletion.fetch_add(1, std::memory_order_relaxed);
} else if (auto* record = queuedLabelRecorder.load(std::memory_order_acquire); record != nullptr) {
queuedLabelHookRecords.fetch_add(1, std::memory_order_relaxed);
record();
}
}
if (HookClearAccess() && HookSnapshotEnabled() && !HookFlushCpuBlocks() && ClearAccess(address, bytes)) {
ClearAccesses::Set(ClearAccesses::Get() + 1);
return;
}
if (!HookSnapshotEnabled() || SnapshotOverlaps(address, bytes)) {
hookLocks.fetch_add(1, std::memory_order_relaxed);
GuestMemory::TagGpuLockSite(GuestMemory::GpuLockSite::Hook);
Expand Down Expand Up @@ -1806,6 +1841,7 @@ void Recorder::NoteQueuedLabel(std::uint64_t address, std::span<const std::byte>
// The range before the table: the hook covers the group whether or not a table exists to
// enter it in (no owner: the callback records it outright, by a CPU store when need be).
QueuedLabelRanges().emplace_back(address, address + bytes.size());
QueuedLabelsOnThread::Set(true);
queuedLabelsNoted.fetch_add(1, std::memory_order_relaxed);
std::lock_guard tableLock(labelTableMutex);
if (labelTableOwner == nullptr) return;
Expand All @@ -1821,9 +1857,10 @@ void Recorder::NoteQueuedLabel(std::uint64_t address, std::span<const std::byte>
}

void Recorder::ForgetQueuedLabels() {
if (QueuedLabelRanges().empty()) return;
if (!QueuedLabelsOnThread::Get()) return;
auto ranges = std::move(QueuedLabelRanges());
QueuedLabelRanges().clear();
QueuedLabelsOnThread::Set(false);
std::lock_guard tableLock(labelTableMutex);
if (labelTableOwner == nullptr) return;
auto& recorded = labelTableOwner->labels;
Expand All @@ -1839,6 +1876,10 @@ void Recorder::ForgetQueuedLabels() {
labelTableOwner->recordedLabels.store(recorded.size(), std::memory_order_relaxed);
}

std::uint64_t Recorder::ThreadClearAccesses() {
return ClearAccesses::Get();
}

bool Recorder::QueuedLabelsPending() {
std::lock_guard tableLock(labelTableMutex);
if (labelTableOwner == nullptr) return false;
Expand Down
17 changes: 17 additions & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "prx/libSceAgcDriver/Graphics/include/BufferPool.hpp"
#include "prx/libSceAgcDriver/Execution/include/DeviceFault.hpp"
#include "prx/libc/include/HostThreadSlot.hpp"
#include "prx/libSceAgcDriver/Execution/include/ThreadScratch.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Resources.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Recorder.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
Expand Down Expand Up @@ -1880,6 +1881,17 @@ class PendingList {
const auto current = published.load(std::memory_order_acquire);
return current != nullptr && current->version == changes.load(std::memory_order_acquire) && !Overlaps(current->ranges, address, bytes);
}
bool ClearOnThread(std::uint64_t address, std::size_t bytes) const {
struct CopyTag {};
auto& copy = ThreadScratch<std::shared_ptr<const Snapshot>, CopyTag>();
const auto version = changes.load(std::memory_order_acquire);
if (copy == nullptr || copy->version != version) {
auto current = published.load(std::memory_order_acquire);
if (current == nullptr || current->version != version) return false;
copy = std::move(current);
}
return !Overlaps(copy->ranges, address, bytes);
}

private:
struct Snapshot {
Expand Down Expand Up @@ -5894,6 +5906,11 @@ std::uint64_t StorageTexture::PendingSerial() {
return pendingSerial.load(std::memory_order_acquire);
}

bool StorageTexture::PendingClearOnThread(std::uint64_t address, std::size_t bytes) {
auto& pending = Pending();
return pending.textures.ClearOnThread(address, bytes) && pending.flushingCount.load(std::memory_order_acquire) == 0;
}

void StorageTexture::NoteProved() const {
provedPresent.store(Recorder::Presents(), std::memory_order_relaxed);
}
Expand Down
63 changes: 62 additions & 1 deletion core/libs/prx/libSceAgcDriver/Graphics/src/UnitShadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "prx/libSceAgcDriver/Graphics/include/Texture.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include "prx/libc/include/HostThreadLocal.hpp"
#include "prx/libc/include/general/AtomicSharedPtr.hpp"
#include "prx/libSceAgcDriver/Execution/include/ThreadScratch.hpp"
#include <algorithm>
#include <array>
#include <atomic>
Expand Down Expand Up @@ -127,6 +129,18 @@ Shadows& Registry() {
return shadows;
}

struct LiveRanges {
std::uint64_t serial = 0;
std::vector<std::pair<std::uint64_t, std::uint64_t>> ranges;
};
struct LiveRangesTag {};
alignas(64) std::atomic<std::uint64_t> liveSerial{0};
AtomicSharedPtr<const LiveRanges> publishedLive;

void liveChanged() {
liveSerial.fetch_add(1, std::memory_order_release);
}

VkDeviceSize SlabBytes(const ShadowSlab& slab) {
return static_cast<VkDeviceSize>(slab.units) * UnitBytes;
}
Expand Down Expand Up @@ -171,6 +185,7 @@ std::shared_ptr<UnitShadow> findOrCreate(Shadows& registry, const Context& conte
--registry.liveSlabs;
}
registry.byBase.erase(found);
liveChanged();
}
auto shadow = std::make_shared<UnitShadow>();
shadow->context = context;
Expand Down Expand Up @@ -438,6 +453,7 @@ std::size_t publishUnits(const std::shared_ptr<UnitShadow>& shadow, std::uint64_
++units;
}
shadow->liveUnits -= ended;
if (ended != 0) liveChanged();
}
if (units == 0) return 0;
Stats().published[static_cast<std::size_t>(reason)].fetch_add(1, std::memory_order_relaxed);
Expand Down Expand Up @@ -504,6 +520,7 @@ bool evictOne(Shadows& registry) {
victim->published[unit] = 0;
}
victim->Recount();
liveChanged();
registry.liveBytes -= SlabBytes(*slab);
--registry.liveSlabs;
slab.reset();
Expand Down Expand Up @@ -613,6 +630,42 @@ bool anyShadowedLocked(const Shadows& registry, std::uint64_t address, std::size
});
}

std::shared_ptr<const LiveRanges> currentLiveRanges() {
auto& registry = Registry();
std::lock_guard lock(registry.mutex);
const auto serial = liveSerial.load(std::memory_order_relaxed);
if (auto published = publishedLive.load(std::memory_order_acquire); published != nullptr && published->serial == serial) return published;
auto built = std::make_shared<LiveRanges>();
built->serial = serial;
auto& ranges = built->ranges;
for (const auto& [base, shadow] : registry.byBase) {
if (shadow->liveUnits == 0) continue;
for (std::uint64_t unit = 0; unit < shadow->Units(); ++unit) {
if (!shadow->Live(unit)) continue;
const auto [begin, end] = shadow->UnitRange(unit);
if (end <= begin) continue;
if (!ranges.empty() && ranges.back().second >= begin) ranges.back().second = std::max(ranges.back().second, end);
else ranges.emplace_back(begin, end);
}
}
std::shared_ptr<const LiveRanges> result = std::move(built);
publishedLive.store(result, std::memory_order_release);
return result;
}

}

bool LiveUnitsClear(std::uint64_t address, std::size_t bytes) {
if (!UnitShadowEnabled() || bytes == 0) return true;
auto& copy = ThreadScratch<std::shared_ptr<const LiveRanges>, LiveRangesTag>();
const auto serial = liveSerial.load(std::memory_order_acquire);
if (copy == nullptr || copy->serial != serial) {
auto published = publishedLive.load(std::memory_order_acquire);
copy = published != nullptr && published->serial == serial ? std::move(published) : currentLiveRanges();
}
const auto end = bytes > std::numeric_limits<std::uint64_t>::max() - address ? std::numeric_limits<std::uint64_t>::max() : address + bytes;
const auto it = std::upper_bound(copy->ranges.begin(), copy->ranges.end(), address, [](std::uint64_t value, const std::pair<std::uint64_t, std::uint64_t>& range) { return value < range.second; });
return it == copy->ranges.end() || it->first >= end;
}

bool AnyPublishInProgress() {
Expand Down Expand Up @@ -852,6 +905,7 @@ void MarkShadowed(const HostImport& import, std::span<const ShadowedRange> range
const auto shadow = find(registry, import);
if (shadow == nullptr) return;
const auto present = Recorder::Presents();
bool turnedLive = false;
for (const auto& [begin, end, slab] : ranges) {
if (end <= begin) continue;
bytes += end - begin;
Expand All @@ -863,12 +917,16 @@ void MarkShadowed(const HostImport& import, std::span<const ShadowedRange> range
++lost;
continue;
}
if (!shadow->Live(unit)) ++shadow->liveUnits;
if (!shadow->Live(unit)) {
++shadow->liveUnits;
turnedLive = true;
}
shadow->generation[unit] = generation;
shadow->published[unit] = 0;
current->lastUse = present;
}
}
if (turnedLive) liveChanged();
}
if (lost != 0) {
Stats().slabLost.fetch_add(lost, std::memory_order_relaxed);
Expand Down Expand Up @@ -908,6 +966,7 @@ void RetireShadow(VkDevice device, const HostImport& import, const std::function
}
}
shadow->Recount();
if (dropped != 0) liveChanged();
}
if (dropped != 0) Stats().droppedOnRetire.fetch_add(dropped, std::memory_order_relaxed);
std::size_t published = 0;
Expand All @@ -931,6 +990,7 @@ void RetireShadow(VkDevice device, const HostImport& import, const std::function
++registry.retired;
if (TraceEnabled()) std::fprintf(stderr, "[shadow] retire import 0x%llx+0x%llx: %zu units published, %llu dropped (memory no longer registered)\n", static_cast<unsigned long long>(import.base), static_cast<unsigned long long>(import.bytes), published, static_cast<unsigned long long>(dropped));
registry.byBase.erase(found);
liveChanged();
}

void PublishAllShadows(const Context& context, PublishReason reason) {
Expand Down Expand Up @@ -960,6 +1020,7 @@ void DestroyShadows(VkDevice device) {
--registry.liveSlabs;
}
it = registry.byBase.erase(it);
liveChanged();
}
}

Expand Down
Loading
Loading