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
96 changes: 96 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/include/HashSlotMap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#ifndef CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_HASHSLOTMAP_HPP
#define CORE_LIBS_PRX_LIBSCEAGCDRIVER_EXECUTION_INCLUDE_HASHSLOTMAP_HPP

#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>

namespace AgcDriver {

template<typename TValue>
class HashSlotMap {
public:
explicit HashSlotMap(std::size_t capacity = 64) { reset(capacity); }

std::size_t size() const { return count; }

void clear() { reset(16); }

TValue* find(std::uint64_t key) {
key = stored(key);
for (auto index = key & mask; keys[index] != 0; index = (index + 1) & mask) {
if (keys[index] == key) return &values[index];
}
return nullptr;
}

std::pair<TValue*, bool> try_emplace(std::uint64_t key) {
if (auto* found = find(key)) return {found, false};
if ((count + 1) * 2 > keys.size()) grow();
key = stored(key);
auto index = key & mask;
while (keys[index] != 0) index = (index + 1) & mask;
keys[index] = key;
values[index] = TValue{};
++count;
return {&values[index], true};
}

bool erase(std::uint64_t key) {
key = stored(key);
auto hole = key & mask;
while (keys[hole] != key) {
if (keys[hole] == 0) return false;
hole = (hole + 1) & mask;
}
for (auto next = (hole + 1) & mask; keys[next] != 0; next = (next + 1) & mask) {
const auto home = keys[next] & mask;
const bool stays = hole <= next ? hole < home && home <= next : hole < home || home <= next;
if (stays) continue;
keys[hole] = keys[next];
values[hole] = std::move(values[next]);
hole = next;
}
keys[hole] = 0;
values[hole] = TValue{};
--count;
return true;
}

private:
static std::uint64_t stored(std::uint64_t key) { return key != 0 ? key : 1; }

void reset(std::size_t capacity) {
std::size_t slots = 16;
while (slots < capacity) slots *= 2;
keys.assign(slots, 0);
values.clear();
values.resize(slots);
mask = slots - 1;
count = 0;
}

void grow() {
auto oldKeys = std::move(keys);
auto oldValues = std::move(values);
reset(oldKeys.size() * 2);
for (std::size_t old = 0; old < oldKeys.size(); ++old) {
if (oldKeys[old] == 0) continue;
auto index = oldKeys[old] & mask;
while (keys[index] != 0) index = (index + 1) & mask;
keys[index] = oldKeys[old];
values[index] = std::move(oldValues[old]);
++count;
}
}

std::vector<std::uint64_t> keys;
std::vector<TValue> values;
std::size_t mask = 0;
std::size_t count = 0;
};

}

#endif
27 changes: 14 additions & 13 deletions core/libs/prx/libSceAgcDriver/Graphics/include/ShaderResources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "prx/libSceAgcDriver/Graphics/include/BdaResources.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Texture.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Sampler.hpp"
#include "prx/libSceAgcDriver/Execution/include/HashSlotMap.hpp"
#include "Recompiler.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Shaders.hpp"
#include <array>
Expand Down Expand Up @@ -703,22 +704,22 @@ class ResourceCache {
static constexpr std::size_t MaximumEntries = 16384;

private:
void erase(const Key& key);
struct Entry {
Key key;
std::uint64_t hash;
std::shared_ptr<ShaderResources> resources;
};
using Entries = std::list<Entry>;
void noteMiss(const Key& key);
void dropReleasedTextures(std::vector<std::shared_ptr<ShaderResources>>* evicted);
void noteEvicted(const Key& key);
void noteReturn(const Key& key);
void noteUse(const Key& key);
struct KeyHash {
std::size_t operator()(const Key& key) const noexcept {
std::uint64_t hash = 14695981039346656037ull;
for (const auto word : key) hash = (hash ^ word) * 1099511628211ull;
return static_cast<std::size_t>(hash);
}
};
void noteEvicted(std::uint64_t hash);
void noteReturn(std::uint64_t hash);
void noteUse(std::uint64_t hash);
static std::uint64_t hashKey(const Key& key);
Entries::iterator* lookup(std::uint64_t hash, const Key& key);
mutable std::mutex mutex;
std::list<std::pair<Key, std::shared_ptr<ShaderResources>>> entries;
std::unordered_map<Key, decltype(entries)::iterator, KeyHash> index;
Entries entries;
HashSlotMap<Entries::iterator> index;
std::size_t capacity = 0;
bool fixedCapacity = false;
std::list<std::size_t> evictedOrder;
Expand Down
117 changes: 67 additions & 50 deletions core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3552,23 +3552,45 @@ bool chargeShaderKey(ChurnCounts& profile, const ResourceCache::Key& key, std::s

}

std::uint64_t ResourceCache::hashKey(const Key& key) {
constexpr std::uint64_t prime = 1099511628211ull;
std::uint64_t lanes[4] = {14695981039346656037ull, 0x9e3779b97f4a7c15ull, 0xc2b2ae3d27d4eb4full, 0x165667b19e3779f9ull};
const auto pair = [&](std::size_t at) { return key[at] | static_cast<std::uint64_t>(key[at + 1]) << 32u; };
std::size_t at = 0;
for (; at + 8 <= key.size(); at += 8) {
lanes[0] = (lanes[0] ^ pair(at)) * prime;
lanes[1] = (lanes[1] ^ pair(at + 2)) * prime;
lanes[2] = (lanes[2] ^ pair(at + 4)) * prime;
lanes[3] = (lanes[3] ^ pair(at + 6)) * prime;
}
for (; at < key.size(); ++at) lanes[at & 3u] = (lanes[at & 3u] ^ key[at]) * prime;
std::uint64_t hash = key.size();
for (const auto lane : lanes) hash = (hash ^ lane ^ (lane >> 29u)) * 0x9e3779b97f4a7c15ull;
return hash ^ (hash >> 32u);
}

ResourceCache::Entries::iterator* ResourceCache::lookup(std::uint64_t hash, const Key& key) {
auto* found = index.find(hash);
return found != nullptr && (*found)->key == key ? found : nullptr;
}

std::shared_ptr<ShaderResources> ResourceCache::Find(const Key& key) {
resourceCacheFinds.fetch_add(1, std::memory_order_relaxed);
const auto hash = hashKey(key);
std::lock_guard lock(mutex);
const auto found = index.find(key);
if (found == index.end()) {
noteReturn(key);
auto* found = lookup(hash, key);
if (found == nullptr) {
noteReturn(hash);
noteMiss(key);
return nullptr;
}
entries.splice(entries.begin(), entries, found->second);
noteUse(key);
return found->second->second;
entries.splice(entries.begin(), entries, *found);
noteUse(hash);
return (*found)->resources;
}

void ResourceCache::noteEvicted(const Key& key) {
void ResourceCache::noteEvicted(std::uint64_t hash) {
if (fixedCapacity) return;
const auto hash = KeyHash{}(key);
if (const auto found = evictedKeys.find(hash); found != evictedKeys.end()) evictedOrder.erase(found->second);
evictedOrder.push_front(hash);
evictedKeys[hash] = evictedOrder.begin();
Expand All @@ -3590,9 +3612,9 @@ void ResourceCache::noteEvicted(const Key& key) {
std::fprintf(stderr, "[rescache] capacity %zu -> %zu entries: keys it evicted keep coming back\n", previous, capacity);
}

void ResourceCache::noteUse(const Key& key) {
void ResourceCache::noteUse(std::uint64_t hash) {
if (fixedCapacity || capacity <= baseEntries) return;
usedKeys.insert(KeyHash{}(key));
usedKeys.insert(hash);
if (++usesSinceCheck < capacity * shrinkWindow) return;
const auto used = usedKeys.size();
const auto uses = usesSinceCheck;
Expand All @@ -3607,9 +3629,9 @@ void ResourceCache::noteUse(const Key& key) {
std::fprintf(stderr, "[rescache] capacity %zu -> %zu entries: %zu keys served the last %zu uses\n", previous, capacity, used, uses);
}

void ResourceCache::noteReturn(const Key& key) {
void ResourceCache::noteReturn(std::uint64_t hash) {
if (fixedCapacity) return;
const auto found = evictedKeys.find(KeyHash{}(key));
const auto found = evictedKeys.find(hash);
if (found == evictedKeys.end()) return;
evictedOrder.erase(found->second);
evictedKeys.erase(found);
Expand Down Expand Up @@ -3734,87 +3756,89 @@ void ResourceCache::noteMiss(const Key& key) {
}

void ResourceCache::Insert(const Key& key, std::shared_ptr<ShaderResources> resources, std::vector<std::shared_ptr<ShaderResources>>* evicted) {
const auto hash = hashKey(key);
std::lock_guard lock(mutex);
dropReleasedTextures(evicted);
if (!resources->HasCachedStorageImages() || resources->HoldsReleasedTexture()) return;
if (const auto found = index.find(key); found != index.end()) {
if (evicted != nullptr) evicted->push_back(std::move(found->second->second));
entries.erase(found->second);
index.erase(found);
auto [slot, inserted] = index.try_emplace(hash);
if (!inserted) {
if (evicted != nullptr) evicted->push_back(std::move((*slot)->resources));
entries.erase(*slot);
}
entries.emplace_front(key, std::move(resources));
index.emplace(key, entries.begin());
entries.push_front({key, hash, std::move(resources)});
*slot = entries.begin();
if (capacity == 0) {
const char* value = std::getenv("APS5_RESOURCE_CACHE_ENTRIES");
const auto parsed = value ? std::strtoull(value, nullptr, 10) : 0ull;
fixedCapacity = parsed != 0;
capacity = fixedCapacity ? static_cast<std::size_t>(parsed) : baseEntries;
}
noteUse(key);
noteUse(hash);
while (entries.size() > capacity) {
noteEvicted(entries.back().first);
if (evicted != nullptr) evicted->push_back(std::move(entries.back().second));
index.erase(entries.back().first);
noteEvicted(entries.back().hash);
if (evicted != nullptr) evicted->push_back(std::move(entries.back().resources));
index.erase(entries.back().hash);
entries.pop_back();
}
if (entries.front().second->TableElements() == 0) return;
if (entries.front().resources->TableElements() == 0) return;
std::uint64_t tables = 0;
for (const auto& entry : entries) tables += entry.second->TableElements();
for (const auto& entry : entries) tables += entry.resources->TableElements();
for (auto it = std::prev(entries.end()); tables > TableElementBudget() && it != entries.begin();) {
const auto elements = it->second->TableElements();
const auto elements = it->resources->TableElements();
if (elements == 0) {
--it;
continue;
}
tables -= elements;
if (evicted != nullptr) evicted->push_back(std::move(it->second));
index.erase(it->first);
if (evicted != nullptr) evicted->push_back(std::move(it->resources));
index.erase(it->hash);
it = std::prev(entries.erase(it));
}
}

void ResourceCache::Remove(const Key& key, const ShaderResources* object) {
const auto hash = hashKey(key);
std::lock_guard lock(mutex);
if (object != nullptr) {
const auto found = index.find(key);
if (found == index.end() || found->second->second.get() != object) return;
}
erase(key);
auto* found = lookup(hash, key);
if (found == nullptr || (object != nullptr && (*found)->resources.get() != object)) return;
entries.erase(*found);
index.erase(hash);
}

void ResourceCache::RemoveImage(const StorageTexture* image, std::vector<std::shared_ptr<ShaderResources>>* evicted) {
std::lock_guard lock(mutex);
for (auto it = entries.begin(); it != entries.end();) {
if (!it->second->ReadsImage(image)) {
if (!it->resources->ReadsImage(image)) {
++it;
continue;
}
if (evicted != nullptr) evicted->push_back(std::move(it->second));
index.erase(it->first);
if (evicted != nullptr) evicted->push_back(std::move(it->resources));
index.erase(it->hash);
it = entries.erase(it);
}
}

void ResourceCache::dropReleasedTextures(std::vector<std::shared_ptr<ShaderResources>>* evicted) {
if (!TexturesReleased().exchange(false, std::memory_order_acq_rel)) return;
for (auto it = entries.begin(); it != entries.end();) {
if (!it->second->HoldsReleasedTexture()) {
if (!it->resources->HoldsReleasedTexture()) {
++it;
continue;
}
if (evicted != nullptr) evicted->push_back(std::move(it->second));
index.erase(it->first);
if (evicted != nullptr) evicted->push_back(std::move(it->resources));
index.erase(it->hash);
it = entries.erase(it);
}
}

bool ResourceCache::Touch(const Key& key) {
resourceCacheTouches.fetch_add(1, std::memory_order_relaxed);
const auto hash = hashKey(key);
std::lock_guard lock(mutex);
const auto found = index.find(key);
if (found == index.end()) return false;
entries.splice(entries.begin(), entries, found->second);
noteUse(key);
auto* found = lookup(hash, key);
if (found == nullptr) return false;
entries.splice(entries.begin(), entries, *found);
noteUse(hash);
return true;
}

Expand Down Expand Up @@ -3869,17 +3893,10 @@ std::uint64_t ResourceCache::TableElementBudget() {
std::uint64_t ResourceCache::TableElements() const {
std::lock_guard lock(mutex);
std::uint64_t tables = 0;
for (const auto& entry : entries) tables += entry.second->TableElements();
for (const auto& entry : entries) tables += entry.resources->TableElements();
return tables;
}

void ResourceCache::erase(const Key& key) {
const auto found = index.find(key);
if (found == index.end()) return;
entries.erase(found->second);
index.erase(found);
}

ResourceCache& SharedResourceCache() {
static auto* cache = new ResourceCache();
return *cache;
Expand Down
Loading
Loading