summaryrefslogtreecommitdiffhomepage
path: root/shaders
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-08-28 14:03:44 +0200
committerStanislaw Halik <sthalik@misaki.pl>2023-08-30 18:22:56 +0200
commita936a998fd6441679f123850bf4b62178ad4336e (patch)
treeb7aa1e8642355aaf2e6ef412c24f1c0988a56a20 /shaders
parentb38454d56373125fc4715c509e5204c564f9e332 (diff)
shaders/tuc: output stats every few seconds
Diffstat (limited to 'shaders')
-rw-r--r--shaders/texture-unit-cache.cpp27
-rw-r--r--shaders/texture-unit-cache.hpp14
2 files changed, 30 insertions, 11 deletions
diff --git a/shaders/texture-unit-cache.cpp b/shaders/texture-unit-cache.cpp
index 4c261b41..71d21a8f 100644
--- a/shaders/texture-unit-cache.cpp
+++ b/shaders/texture-unit-cache.cpp
@@ -1,6 +1,7 @@
#include "texture-unit-cache.hpp"
#include "compat/assert.hpp"
+#include <cstdio>
#include <Corrade/Containers/String.h>
#include <Magnum/GL/Texture.h>
@@ -39,7 +40,7 @@ int32_t texture_unit_cache::bind(GL::AbstractTexture* tex)
tex->bind((Int)unbound_id);
++cache_hit_count;
auto label = tex->label();
- Debug{Debug::Flag::NoSpace} << "binding '" << tex->label() << "' to " << unbound_id;
+ //Debug{Debug::Flag::NoSpace} << "binding '" << tex->label() << "' to " << unbound_id;
return (int32_t)unbound_id;
}
else
@@ -55,10 +56,10 @@ int32_t texture_unit_cache::bind(GL::AbstractTexture* tex)
}
}
fm_assert(min_index != invalid);
- ++rebind_count;
+ ++cache_miss_count;
units[min_index] = {tex, ++lru_counter};
tex->bind((Int)min_index);
- Debug{Debug::Flag::NoSpace} << "binding '" << tex->label() << "' to " << min_index;
+ //Debug{Debug::Flag::NoSpace} << "rebinding '" << tex->label() << "' to " << min_index;
return (int32_t)min_index;
}
}
@@ -73,7 +74,7 @@ void texture_unit_cache::invalidate()
{
units = {};
lru_counter = 0;
- rebind_count = 0;
+ cache_miss_count = 0;
}
void texture_unit_cache::lock(floormat::size_t i, GL::AbstractTexture* tex)
@@ -82,12 +83,23 @@ void texture_unit_cache::lock(floormat::size_t i, GL::AbstractTexture* tex)
units[i] = { .ptr = tex, .lru_val = (uint64_t)i, };
}
-void texture_unit_cache::unlock(size_t i, bool immediately)
+void texture_unit_cache::unlock(size_t i, bool reuse_immediately)
{
fm_assert(i < unit_count);
if (units[i].ptr == (GL::AbstractTexture*)-1)
- immediately = true;
- units[i] = { .ptr = units[i].ptr, .lru_val = immediately ? 0 : ++lru_counter };
+ reuse_immediately = true;
+ units[i] = { .ptr = units[i].ptr, .lru_val = reuse_immediately ? 0 : ++lru_counter };
+}
+
+void texture_unit_cache::output_stats() const
+{
+ auto total = cache_hit_count + cache_miss_count;
+
+ if (total > 0)
+ {
+ auto ratio = (double)cache_hit_count/(double)(cache_hit_count+cache_miss_count);
+ printf("texture-binding: hit rate %.2f%% (%zu binds total)\n", ratio*100, (size_t)total); std::fflush(stdout);
+ }
}
size_t texture_unit_cache::get_unit_count()
@@ -96,6 +108,7 @@ size_t texture_unit_cache::get_unit_count()
GLint value = 0;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &value);
fm_assert(value >= /*GL 3.3*/ 16);
+ //value = 16; // for performance testing
return value;
}();
return (size_t)ret;
diff --git a/shaders/texture-unit-cache.hpp b/shaders/texture-unit-cache.hpp
index 2647ea8c..5f09ecb9 100644
--- a/shaders/texture-unit-cache.hpp
+++ b/shaders/texture-unit-cache.hpp
@@ -11,6 +11,8 @@ struct texture_unit_cache final
fm_DECLARE_DELETED_COPY_ASSIGNMENT(texture_unit_cache);
fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(texture_unit_cache);
+ struct statistics { size_t cache_hit, cache_miss; };
+
texture_unit_cache();
[[nodiscard]] int32_t bind(GL::AbstractTexture* ptr);
@@ -18,17 +20,21 @@ struct texture_unit_cache final
void invalidate();
void lock(size_t i, GL::AbstractTexture* = (GL::AbstractTexture*)-1);
void lock(size_t i, GL::AbstractTexture& tex) { lock(i, &tex); }
- void unlock(size_t i, bool immediately = true);
+ void unlock(size_t i, bool reuse_immediately = true);
size_t reuse_count() const { return cache_hit_count; }
- size_t bind_count() const { return rebind_count; }
- void reset_stats() { rebind_count = cache_hit_count = 0; }
+ size_t bind_count() const { return cache_miss_count; }
+
+ void output_stats() const;
+ statistics stats() const { return statistics { cache_hit_count, cache_miss_count }; }
+
+ void reset_stats() { cache_miss_count = cache_hit_count = 0; }
private:
static size_t get_unit_count();
struct unit_data;
- size_t unit_count, lru_counter = 0, rebind_count = 0, cache_hit_count = 0;
+ size_t unit_count, lru_counter = 0, cache_miss_count = 0, cache_hit_count = 0;
Array<unit_data> units;
};