Skip to content
Closed
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 @@ -710,6 +710,7 @@ struct VulkanDevice::State {
copiedWriters->clear();
resourceCache.Clear();
Graphics::ClearCachedTextures(device);
Graphics::ClearSnapshotCopies(device);
Graphics::ClearImageMirrors(device);
Graphics::ClearHostImports(device);
patternBuffers.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ struct MirrorStats {
std::uint64_t sparseBinds = 0;
};
MirrorStats MirrorCounters();
void ClearSnapshotCopies(VkDevice device);
std::vector<std::uint64_t> AddressTableKey(std::span<const std::pair<std::uint64_t, std::uint64_t>> writes, std::span<const ShaderRecompiler::BdaAbi::Range> own);
void ClearImageMirrors(VkDevice device);
void ClearHostImports(VkDevice device);

Expand Down Expand Up @@ -392,8 +394,8 @@ class GuestBufferMemory {
// The cached address space this build maps through (its lease pins the ranges); `regions` then
// holds only the regions outside it (V#s, snapshots, ranges copied per build).
std::shared_ptr<const AddressSpace> space;
mutable std::uint64_t writeTableSerial = 0;
mutable std::shared_ptr<const std::vector<ShaderRecompiler::BdaAbi::Range>> writeTableRanges;
mutable std::uint64_t tableSerial = 0;
mutable std::shared_ptr<const std::vector<ShaderRecompiler::BdaAbi::Range>> tableRanges;
// Import registry epoch when `direct` pointers were taken at acquire time; they are reused while
// no import was destroyed since.
std::uint64_t importsEpoch = 0;
Expand Down
130 changes: 114 additions & 16 deletions core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <bit>
#include <condition_variable>
#include <iterator>
#include <list>
#include <map>
#include <mutex>
#include <set>
Expand Down Expand Up @@ -112,7 +113,13 @@ struct GuestBufferMemory::AddressSpace {
std::vector<ShaderRecompiler::BdaAbi::Range> ranges;
std::shared_ptr<const std::vector<std::pair<std::uint64_t, std::uint64_t>>> directReads;
mutable std::mutex tableMutex;
mutable std::map<std::vector<std::pair<std::uint64_t, std::uint64_t>>, std::pair<std::uint64_t, std::shared_ptr<const std::vector<ShaderRecompiler::BdaAbi::Range>>>> writeTables;
struct Table {
std::uint64_t serial;
std::shared_ptr<const std::vector<ShaderRecompiler::BdaAbi::Range>> ranges;
std::list<const std::vector<std::uint64_t>*>::iterator recent;
};
mutable std::map<std::vector<std::uint64_t>, Table> tables;
mutable std::list<const std::vector<std::uint64_t>*> tableRecency;
};

namespace {
Expand Down Expand Up @@ -1236,6 +1243,64 @@ ImageMirrors& Mirrors() {
return mirrors;
}

struct SnapshotCopies {
using Key = std::tuple<std::uint64_t, std::size_t, VkBufferUsageFlags>;
static constexpr std::size_t Budget = std::size_t{16} << 20u;
static constexpr std::size_t MaxEntries = 1024;
std::mutex mutex;
VkDevice device = VK_NULL_HANDLE;
struct Entry {
std::shared_ptr<Buffer> buffer;
std::vector<std::byte> bytes;
std::list<Key>::iterator recent;
};
std::map<Key, Entry> entries;
std::list<Key> recency;
std::size_t bytes = 0;
void clear() {
entries.clear();
recency.clear();
bytes = 0;
}
};

SnapshotCopies& SnapshotCopyCache() {
static SnapshotCopies copies;
return copies;
}

std::shared_ptr<Buffer> snapshotCopy(const Context& context, std::uint64_t address, std::span<const std::byte> bytes, VkBufferUsageFlags usage) {
auto& copies = SnapshotCopyCache();
std::lock_guard lock(copies.mutex);
if (copies.device != context.device) {
copies.clear();
copies.device = context.device;
}
const SnapshotCopies::Key key{address, bytes.size(), usage};
if (const auto found = copies.entries.find(key); found != copies.entries.end()) {
if (std::memcmp(found->second.bytes.data(), bytes.data(), bytes.size()) == 0) {
copies.recency.splice(copies.recency.end(), copies.recency, found->second.recent);
return found->second.buffer;
}
copies.recency.erase(found->second.recent);
copies.bytes -= bytes.size();
copies.entries.erase(found);
}
auto buffer = std::make_shared<Buffer>(context, bytes.size(), usage);
std::memcpy(buffer->Bytes().data(), bytes.data(), bytes.size());
if (bytes.size() > SnapshotCopies::Budget) return buffer;
while (!copies.recency.empty() && (copies.bytes + bytes.size() > SnapshotCopies::Budget || copies.entries.size() >= SnapshotCopies::MaxEntries)) {
const auto oldest = copies.entries.find(copies.recency.front());
copies.bytes -= std::get<1>(oldest->first);
copies.entries.erase(oldest);
copies.recency.pop_front();
}
copies.recency.push_back(key);
copies.entries.emplace(key, SnapshotCopies::Entry{buffer, {bytes.begin(), bytes.end()}, std::prev(copies.recency.end())});
copies.bytes += bytes.size();
return buffer;
}

bool mirrorsEnabled() {
static const bool disabled = std::getenv("APS5_NO_LEASE_MIRROR") != nullptr;
return !disabled;
Expand Down Expand Up @@ -2072,6 +2137,23 @@ MirrorStats MirrorCounters() {
return stats;
}

std::vector<std::uint64_t> AddressTableKey(std::span<const std::pair<std::uint64_t, std::uint64_t>> writes, std::span<const ShaderRecompiler::BdaAbi::Range> own) {
std::vector<std::uint64_t> key;
key.reserve(writes.size() * 2u + 1u + own.size() * 4u);
for (const auto& [begin, end] : writes) key.insert(key.end(), {begin, end});
key.push_back(std::numeric_limits<std::uint64_t>::max());
for (const auto& range : own) key.insert(key.end(), {range.begin, range.end, range.deviceAddress, (static_cast<std::uint64_t>(range.reserved) << 32u) | range.permissions});
return key;
}

void ClearSnapshotCopies(VkDevice device) {
auto& copies = SnapshotCopyCache();
std::lock_guard lock(copies.mutex);
if (copies.device != device) return;
copies.clear();
copies.device = VK_NULL_HANDLE;
}

void ClearImageMirrors(VkDevice device) {
auto space = Spaces().current.load();
if (space != nullptr && space->device == device) {
Expand Down Expand Up @@ -3514,7 +3596,9 @@ void GuestBufferMemory::copyRegion(Region& region, bool addressable) {
}
const auto allocated = region.bufferEnd != 0 ? region.bufferEnd - region.begin : bytes;
// A buffer made by UploadPrepare for a GPU copy that fell back here (its import went) is kept.
if (region.buffer == nullptr) region.buffer = std::make_shared<Buffer>(context, static_cast<std::size_t>(allocated), usage);
const bool shared = region.buffer == nullptr && !region.hostBacked && !region.writable && !region.sparse && region.bufferEnd == 0 && region.snapshot.size() == bytes;
if (shared) region.buffer = snapshotCopy(context, region.begin, region.snapshot, usage);
else if (region.buffer == nullptr) region.buffer = std::make_shared<Buffer>(context, static_cast<std::size_t>(allocated), usage);
const auto readStart = std::chrono::steady_clock::now();
if (profile) allocateUs.fetch_add(microsecondsSince(allocateStart), std::memory_order_relaxed);
// Named for the [hooksync] attribution: the reads below go through the flush hook.
Expand Down Expand Up @@ -3544,7 +3628,7 @@ void GuestBufferMemory::copyRegion(Region& region, bool addressable) {
}
}
else {
std::memcpy(region.buffer->Bytes().data(), region.snapshot.data(), region.snapshot.size());
if (!shared) std::memcpy(region.buffer->Bytes().data(), region.snapshot.data(), region.snapshot.size());
readBytes += region.snapshot.size();
}
region.snapshot.clear();
Expand Down Expand Up @@ -3840,31 +3924,45 @@ std::vector<ShaderRecompiler::BdaAbi::Range> GuestBufferMemory::AddressRanges()
}

std::optional<GuestBufferMemory::CachedTable> GuestBufferMemory::CachedAddressTable() const {
if (space == nullptr || !regions.empty()) return std::nullopt;
if (space == nullptr) return std::nullopt;
Require(uploaded && !committed, "guest GPU address ranges are not available");
if (writes.empty()) return CachedTable{space->serial, &space->ranges};
if (writeTableRanges != nullptr) return CachedTable{writeTableSerial, writeTableRanges.get()};
if (writes.empty() && regions.empty()) return CachedTable{space->serial, &space->ranges};
if (tableRanges != nullptr) return CachedTable{tableSerial, tableRanges.get()};
const auto& spaceRanges = space->ranges;
const bool inWritable = std::all_of(writes.begin(), writes.end(), [&](const auto& written) {
const bool inWritable = regions.empty() && std::all_of(writes.begin(), writes.end(), [&](const auto& written) {
auto at = std::partition_point(spaceRanges.begin(), spaceRanges.end(), [&](const auto& range) { return range.end <= written.first; });
for (; at != spaceRanges.end() && at->begin < written.second; ++at) {
if ((at->permissions & ShaderRecompiler::BdaAbi::Write) == 0u) return false;
}
return true;
});
if (inWritable) return CachedTable{space->serial, &space->ranges};
std::vector<ShaderRecompiler::BdaAbi::Range> own;
own.reserve(regions.size());
for (const auto& region : regions) own.push_back(addressRange(region));
auto key = AddressTableKey(writes, own);
static const bool verify = std::getenv("APS5_VERIFY_BDA_TABLES") != nullptr;
std::lock_guard lock(space->tableMutex);
const auto found = space->writeTables.find(writes);
if (found != space->writeTables.end()) {
writeTableSerial = found->second.first;
writeTableRanges = found->second.second;
const auto found = space->tables.find(key);
if (found != space->tables.end()) {
space->tableRecency.splice(space->tableRecency.end(), space->tableRecency, found->second.recent);
tableSerial = found->second.serial;
tableRanges = found->second.ranges;
if (verify) {
const auto built = AddressRanges();
Require(built.size() == tableRanges->size() && (built.empty() || std::memcmp(built.data(), tableRanges->data(), built.size() * sizeof(built.front())) == 0), "a cached BDA table differs from the ranges of the build that found it");
}
} else {
writeTableRanges = std::make_shared<const std::vector<ShaderRecompiler::BdaAbi::Range>>(AddressRanges());
writeTableSerial = Spaces().serials.fetch_add(1, std::memory_order_relaxed) + 1;
if (space->writeTables.size() >= 64u) space->writeTables.erase(space->writeTables.begin());
space->writeTables.emplace(writes, std::pair{writeTableSerial, writeTableRanges});
tableRanges = std::make_shared<const std::vector<ShaderRecompiler::BdaAbi::Range>>(AddressRanges());
tableSerial = Spaces().serials.fetch_add(1, std::memory_order_relaxed) + 1;
if (space->tables.size() >= 64u) {
space->tables.erase(*space->tableRecency.front());
space->tableRecency.pop_front();
}
const auto inserted = space->tables.emplace(std::move(key), AddressSpace::Table{tableSerial, tableRanges, {}}).first;
inserted->second.recent = space->tableRecency.insert(space->tableRecency.end(), &inserted->first);
}
return CachedTable{writeTableSerial, writeTableRanges.get()};
return CachedTable{tableSerial, tableRanges.get()};
}

bool GuestBufferMemory::HasCopiedWrites() const {
Expand Down
Loading
Loading