diff options
-rw-r--r-- | editor/draw.cpp | 18 | ||||
-rw-r--r-- | shaders/texture-unit-cache.cpp | 27 | ||||
-rw-r--r-- | shaders/texture-unit-cache.hpp | 14 |
3 files changed, 47 insertions, 12 deletions
diff --git a/editor/draw.cpp b/editor/draw.cpp index f54d6b49..097a9deb 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -8,11 +8,14 @@ #include "src/camera-offset.hpp" #include "src/world.hpp" #include "character.hpp" + #include "rotation.inl" +#include "src/RTree-search.hpp" + +#include <chrono> #include <Magnum/Math/Color.h> #include <Magnum/Math/Vector3.h> #include <Magnum/GL/Renderer.h> -#include "src/RTree-search.hpp" namespace floormat { @@ -191,6 +194,19 @@ void app::draw() draw_cursor(); draw_ui(); render_menu(); + + using namespace std::chrono_literals; + { + constexpr auto print_every = 4s; + + static auto t0 = std::chrono::high_resolution_clock::now(); + auto t = std::chrono::high_resolution_clock::now(); + if (t - t0 >= print_every) + { + t0 = t; + M->texture_unit_cache().output_stats(); + } + } } clickable* app::find_clickable_scenery(const Optional<Vector2i>& pixel) 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; }; |