mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-09 23:55:40 +12:00
doormat
This commit is contained in:
parent
255947b2fc
commit
e67b98f990
2 changed files with 40 additions and 32 deletions
|
@ -7,7 +7,7 @@
|
||||||
// Surface cache class that can fit "capacity" instances of the "SurfaceType" class of surfaces
|
// Surface cache class that can fit "capacity" instances of the "SurfaceType" class of surfaces
|
||||||
// SurfaceType *must* have all of the following.
|
// SurfaceType *must* have all of the following.
|
||||||
// - An "allocate" function that allocates GL resources for the surfaces. On overflow it will panic
|
// - An "allocate" function that allocates GL resources for the surfaces. On overflow it will panic
|
||||||
// if evict_on_overflow is false, or kick out the oldest item if it is true.
|
// if evictOnOverflow is false, or kick out the oldest item if it is true (like a ring buffer)
|
||||||
// - A "free" function that frees up all resources the surface is taking up
|
// - A "free" function that frees up all resources the surface is taking up
|
||||||
// - A "matches" function that, when provided with a SurfaceType object reference
|
// - A "matches" function that, when provided with a SurfaceType object reference
|
||||||
// Will tell us if the 2 surfaces match (Only as far as location in VRAM, format, dimensions, etc)
|
// Will tell us if the 2 surfaces match (Only as far as location in VRAM, format, dimensions, etc)
|
||||||
|
@ -15,7 +15,7 @@
|
||||||
// Including equality of the allocated OpenGL resources, which we don't want
|
// Including equality of the allocated OpenGL resources, which we don't want
|
||||||
// - A "valid" member that tells us whether the function is still valid or not
|
// - A "valid" member that tells us whether the function is still valid or not
|
||||||
// - A "location" member which tells us which location in 3DS memory this surface occupies
|
// - A "location" member which tells us which location in 3DS memory this surface occupies
|
||||||
template <typename SurfaceType, size_t capacity, bool evict_on_overflow=false>
|
template <typename SurfaceType, size_t capacity, bool evictOnOverflow = false>
|
||||||
class SurfaceCache {
|
class SurfaceCache {
|
||||||
// Vanilla std::optional can't hold actual references
|
// Vanilla std::optional can't hold actual references
|
||||||
using OptionalRef = std::optional<std::reference_wrapper<SurfaceType>>;
|
using OptionalRef = std::optional<std::reference_wrapper<SurfaceType>>;
|
||||||
|
@ -23,13 +23,13 @@ class SurfaceCache {
|
||||||
std::is_same<SurfaceType, Texture>(), "Invalid surface type");
|
std::is_same<SurfaceType, Texture>(), "Invalid surface type");
|
||||||
|
|
||||||
size_t size;
|
size_t size;
|
||||||
size_t eviction_index;
|
size_t evictionIndex;
|
||||||
std::array<SurfaceType, capacity> buffer;
|
std::array<SurfaceType, capacity> buffer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void reset() {
|
void reset() {
|
||||||
size = 0;
|
size = 0;
|
||||||
eviction_index=0;
|
evictionIndex = 0;
|
||||||
for (auto& e : buffer) { // Free the VRAM of all surfaces
|
for (auto& e : buffer) { // Free the VRAM of all surfaces
|
||||||
e.free();
|
e.free();
|
||||||
}
|
}
|
||||||
|
@ -54,34 +54,42 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds a surface object to the cache and returns it
|
// Adds a surface object to the cache and returns it
|
||||||
SurfaceType& add(const SurfaceType& surface) {
|
SurfaceType& add(const SurfaceType& surface) {
|
||||||
if (size >= capacity) {
|
if (size >= capacity) {
|
||||||
if(evict_on_overflow){
|
if (evictOnOverflow) { // Do a ring buffer if evictOnOverflow is true
|
||||||
auto & e = buffer[eviction_index % size];
|
auto& e = buffer[evictionIndex];
|
||||||
eviction_index++;
|
evictionIndex = (evictionIndex + 1) % capacity;
|
||||||
e.free();
|
|
||||||
e.valid = false;
|
|
||||||
e = surface;
|
|
||||||
e.allocate();
|
|
||||||
return e;
|
|
||||||
}else Helpers::panic("Surface cache full! Add emptying!");
|
|
||||||
}
|
|
||||||
size++;
|
|
||||||
|
|
||||||
// Find an invalid entry in the cache and overwrite it with the new surface
|
e.valid = false;
|
||||||
for (auto& e : buffer) {
|
e.free();
|
||||||
if (!e.valid) {
|
e = surface;
|
||||||
e = surface;
|
e.allocate();
|
||||||
e.allocate();
|
return e;
|
||||||
return e;
|
} else {
|
||||||
}
|
Helpers::panic("Surface cache full! Add emptying!");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This should be unreachable but helps to panic anyways
|
size++;
|
||||||
Helpers::panic("Couldn't add surface to cache\n");
|
|
||||||
}
|
// Find an invalid entry in the cache and overwrite it with the new surface
|
||||||
|
for (auto& e : buffer) {
|
||||||
|
if (!e.valid) {
|
||||||
|
e = surface;
|
||||||
|
e.allocate();
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should be unreachable but helps to panic anyways
|
||||||
|
Helpers::panic("Couldn't add surface to cache\n");
|
||||||
|
}
|
||||||
|
|
||||||
SurfaceType& operator[](size_t i) {
|
SurfaceType& operator[](size_t i) {
|
||||||
return buffer[i];
|
return buffer[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SurfaceType& operator[](size_t i) const {
|
||||||
|
return buffer[i];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -33,11 +33,11 @@ void Texture::setNewConfig(u32 cfg) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Texture::free() {
|
void Texture::free() {
|
||||||
valid = false;
|
valid = false;
|
||||||
|
|
||||||
if (texture.exists()){
|
if (texture.exists()) {
|
||||||
glDeleteTextures(1, &texture.m_handle);
|
texture.free();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 Texture::sizeInBytes() {
|
u64 Texture::sizeInBytes() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue