From bb510b685a23bceead58828016a13a4c5961c27d Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 18 Dec 2024 21:39:50 +0100 Subject: [PATCH] fix some crashes (how) and bitset wierdness --- src/bitset_image_loader.cpp | 14 ++++++------ src/texture_cache.hpp | 43 ++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/bitset_image_loader.cpp b/src/bitset_image_loader.cpp index baa2e26..f9d47bf 100644 --- a/src/bitset_image_loader.cpp +++ b/src/bitset_image_loader.cpp @@ -51,14 +51,13 @@ std::optional BitsetImageLoader::haveToTexture(TextureUploaderI& t TextureEntry new_entry; new_entry.timestamp_last_rendered = Message::getTimeMS(); - new_entry.current_texture = 0; new_entry.width = have.size_bits(); new_entry.height = 1; const auto n_t = tu.upload(static_cast(conv_surf->pixels), conv_surf->w, conv_surf->h, TextureUploaderI::RGBA); assert(n_t != 0); new_entry.textures.push_back(n_t); - new_entry.frame_duration.push_back(0); + new_entry.frame_duration.push_back(1); std::cout << "BIL: genereated bitset image o:" << entt::to_integral(o.entity()) << "\n"; @@ -74,13 +73,13 @@ BitsetImageLoader::BitsetImageLoader(void) { TextureLoaderResult BitsetImageLoader::load(TextureUploaderI& tu, ObjectHandle o) { if (!static_cast(o)) { std::cerr << "BIL error: trying to load invalid object\n"; - return {std::nullopt}; + return {}; } if (!o.any_of()) { // after completion, this is called until the texture times out - //std::cout << "BIL: no local have bitset\n"; - return {std::nullopt}; + //std::cout << "BIL: no have bitset\n"; + return {}; } if (o.all_of()) { @@ -91,7 +90,8 @@ TextureLoaderResult BitsetImageLoader::load(TextureUploaderI& tu, ObjectHandle o auto& list = o.get().others; if (list.empty()) { std::cout << "BIL: remote set list empty\n"; - return {std::nullopt}; + _tmp_bitset = {8}; + return {haveToTexture(tu, _tmp_bitset, o)}; } const auto& first_entry = list.begin()->second; @@ -118,7 +118,7 @@ TextureLoaderResult BitsetImageLoader::load(TextureUploaderI& tu, ObjectHandle o return {haveToTexture(tu, _tmp_bitset, o)}; } - return {std::nullopt}; + return {}; } std::optional BitsetImageLoader::load(TextureUploaderI& tu, ObjectContactSub ocs) { diff --git a/src/texture_cache.hpp b/src/texture_cache.hpp index 4b0b9c7..7bdefd0 100644 --- a/src/texture_cache.hpp +++ b/src/texture_cache.hpp @@ -5,8 +5,11 @@ #include #include -#include #include +#include +#include + +#include struct TextureEntry { uint32_t width {0}; @@ -163,7 +166,10 @@ struct TextureCache { const uint64_t ts_next = te.doAnimation(ts_now); te.rendered_this_frame = false; ts_min_next = std::min(ts_min_next, ts_next); - } else if (_cache.size() > min_count_before_purge && ts_now - te.timestamp_last_rendered >= ms_before_purge) { + } else if ( + _cache.size() > min_count_before_purge && + ts_now - te.timestamp_last_rendered >= ms_before_purge + ) { to_purge.push_back(key); } } @@ -178,31 +184,38 @@ struct TextureCache { void invalidate(const std::vector& to_purge) { for (const auto& key : to_purge) { + if (_to_load.count(key)) { + // TODO: only remove if not keep trying + _to_load.erase(key); + } if (_cache.count(key)) { for (const auto& tex_id : _cache.at(key).textures) { _tu.destroy(tex_id); } + _cache.erase(key); } - _cache.erase(key); - _to_load.erase(key); } } // returns true if there is still work queued up bool workLoadQueue(void) { - auto it = _to_load.begin(); - for (; it != _to_load.end(); it++) { + auto it = _to_load.cbegin(); + for (; it != _to_load.cend(); it++) { + auto new_entry_opt = _l.load(_tu, *it); if (_cache.count(*it)) { - auto new_entry_opt = _l.load(_tu, *it); if (new_entry_opt.texture.has_value()) { - auto old_entry = _cache.at(*it); + auto old_entry = _cache.at(*it); // copy + assert(!old_entry.textures.empty()); for (const auto& tex_id : old_entry.textures) { _tu.destroy(tex_id); } - auto [new_it, _] = _cache.insert_or_assign(*it, new_entry_opt.texture.value()); - //new_it->second.current_texture = old_entry.current_texture; // ?? - new_it->second.rendered_this_frame = old_entry.rendered_this_frame; - new_it->second.timestamp_last_rendered = old_entry.timestamp_last_rendered; + + _cache.erase(*it); + auto& new_entry = _cache[*it] = new_entry_opt.texture.value(); + // TODO: make update interface and let loader handle this + //new_entry.current_texture = old_entry.current_texture; // ?? + new_entry.rendered_this_frame = old_entry.rendered_this_frame; + new_entry.timestamp_last_rendered = old_entry.timestamp_last_rendered; it = _to_load.erase(it); @@ -213,9 +226,9 @@ struct TextureCache { it = _to_load.erase(it); } } else { - auto new_entry_opt = _l.load(_tu, *it); if (new_entry_opt.texture.has_value()) { _cache.emplace(*it, new_entry_opt.texture.value()); + _cache.at(*it).rendered_this_frame = true; // ? it = _to_load.erase(it); // TODO: not a good idea? @@ -227,8 +240,8 @@ struct TextureCache { } } - // peak - return it != _to_load.end(); + // peek + return it != _to_load.cend(); } };