| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #include "mesh_decoder.h"
- #include "mesh_codec.hpp"
- #include "third_party/static_block.hpp"
- #include "impl/main_impl.h"
- #include <unordered_map>
- using namespace mesh_codec;
- namespace mesh_decoder_impl {
- using mesh_map_type =
- std::unordered_map<size_t, mesh_ptr>;
- mesh_map_type mesh_pool;
- static_block {
- register_cleanup_func([] { mesh_pool.clear(); });
- }
- using hasher_type = std::hash<std::string>;
- hasher_type name_hasher;
- mesh_ptr get_no_transfer_mesh(size_t name) {
- assert(mesh_pool.contains(name));
- return mesh_pool.at(name);
- }
- }
- using namespace mesh_decoder_impl;
- struct mesh_decoder::impl {
- using cache_map_type =
- std::unordered_map<hash_type, mesh_ptr>;
- cache_map_type cache_map;
- static mesh_ptr decode_mesh(const data_type &data) {
- auto ret = mesh_type::raw_info_type();
- auto reader = network_reader(data);
- reader >> ret.num_triangles;
- auto vbo_size = reader.read_value<size_t>();
- ret.vbo_data = reader.read_data(vbo_size);
- auto ebo_size = reader.read_value<size_t>();
- ret.ebo_data = reader.read_data(ebo_size);
- assert(reader.empty());
- return mesh_type::from_raw(ret);
- }
- mesh_ptr decode(const data_type &data) {
- auto reader = network_reader(data);
- auto flag = reader.read_value<flag_type>();
- if (flag == FLAG_NO_TRANSFER) {
- auto name = reader.read_value<size_t>();
- return get_no_transfer_mesh(name);
- }
- auto id = reader.read_value<hash_type>();
- if (cache_map.contains(id)) {
- return cache_map.at(id);
- }
- // assert(!reader.empty());
- if (reader.empty()) return nullptr; // TODO: mesh IDR may not be handled???
- auto mesh = decode_mesh(reader.read_remain());
- if (flag != FLAG_NO_CACHE) {
- assert(!cache_map.contains(id));
- cache_map.emplace(id, mesh);
- }
- return mesh;
- }
- };
- mesh_decoder::mesh_decoder(create_config _)
- : pimpl(std::make_unique<impl>()) {
- }
- mesh_decoder::~mesh_decoder() = default;
- mesh_ptr mesh_decoder::decode(const data_type &data) {
- return pimpl->decode(data);
- }
- void register_mesh_file(const std::string &name,
- const std::string &path) {
- auto mesh = mesh_type::create({.path = path});
- auto name_id = name_hasher(name);
- assert(!mesh_pool.contains(name_id));
- mesh_pool.emplace(name_id, mesh);
- }
- void register_mesh_list(const YAML::Node &conf_list) {
- for (auto &conf: conf_list) {
- register_mesh_file(LOAD_STR("name"), LOAD_STR("path"));
- }
- }
|