Skip to content

Commit

Permalink
fix some crashes (how) and bitset wierdness
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Dec 18, 2024
1 parent 876f482 commit bb510b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/bitset_image_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,13 @@ std::optional<TextureEntry> 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<uint8_t*>(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";

Expand All @@ -74,13 +73,13 @@ BitsetImageLoader::BitsetImageLoader(void) {
TextureLoaderResult BitsetImageLoader::load(TextureUploaderI& tu, ObjectHandle o) {
if (!static_cast<bool>(o)) {
std::cerr << "BIL error: trying to load invalid object\n";
return {std::nullopt};
return {};
}

if (!o.any_of<ObjComp::F::LocalHaveBitset, ObjComp::F::RemoteHaveBitset>()) {
// 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<ObjComp::F::LocalHaveBitset>()) {
Expand All @@ -91,7 +90,8 @@ TextureLoaderResult BitsetImageLoader::load(TextureUploaderI& tu, ObjectHandle o
auto& list = o.get<ObjComp::F::RemoteHaveBitset>().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;

Expand All @@ -118,7 +118,7 @@ TextureLoaderResult BitsetImageLoader::load(TextureUploaderI& tu, ObjectHandle o
return {haveToTexture(tu, _tmp_bitset, o)};
}

return {std::nullopt};
return {};
}

std::optional<TextureEntry> BitsetImageLoader::load(TextureUploaderI& tu, ObjectContactSub ocs) {
Expand Down
43 changes: 28 additions & 15 deletions src/texture_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include <entt/container/dense_map.hpp>
#include <entt/container/dense_set.hpp>

#include <iostream>
#include <optional>
#include <vector>
#include <cassert>

#include <iostream>

struct TextureEntry {
uint32_t width {0};
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -178,31 +184,38 @@ struct TextureCache {

void invalidate(const std::vector<KeyType>& 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);

Expand All @@ -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?
Expand All @@ -227,8 +240,8 @@ struct TextureCache {
}
}

// peak
return it != _to_load.end();
// peek
return it != _to_load.cend();
}
};

0 comments on commit bb510b6

Please sign in to comment.