diff --git a/include/blt/gfx/renderer/texture_manager.h b/include/blt/gfx/renderer/texture_manager.h index efbd48c..bb23534 100644 --- a/include/blt/gfx/renderer/texture_manager.h +++ b/include/blt/gfx/renderer/texture_manager.h @@ -31,17 +31,29 @@ namespace blt::gfx { std::string path; std::string name; + + loadable_texture() = default; + + loadable_texture(std::string path, std::string name): path(std::move(path)), name(std::move(name)) + {} }; class texture_resource_manager { private: - blt::thread_pool pool; + std::mutex load_lock; + std::mutex save_lock; std::vector textures_to_load; + std::vector loaded_textures; HASHMAP textures_2d; public: texture_resource_manager() = default; - ~texture_resource_manager(); + + void enqueue(std::string path, std::string name = ""); + + void load_resources(std::size_t threads = 8); + + void delete_resources(); }; } diff --git a/src/blt/gfx/renderer/texture_manager.cpp b/src/blt/gfx/renderer/texture_manager.cpp index 1c7eedd..216a985 100644 --- a/src/blt/gfx/renderer/texture_manager.cpp +++ b/src/blt/gfx/renderer/texture_manager.cpp @@ -18,8 +18,63 @@ #include #include -blt::gfx::texture_resource_manager::~texture_resource_manager() +namespace blt::gfx { - for (const auto& p : textures_2d) - delete p.second; + + void texture_resource_manager::delete_resources() + { + for (const auto& p : textures_2d) + delete p.second; + } + + void texture_resource_manager::enqueue(std::string path, std::string name) + { + textures_to_load.emplace_back(std::move(path), std::move(name)); + } + + void texture_resource_manager::load_resources(std::size_t threads) + { + blt::thread_pool pool; + pool.execute([&]() { + loadable_texture texture; + { + std::scoped_lock lock(load_lock); + texture = textures_to_load.back(); + textures_to_load.pop_back(); + } + auto* file = new texture_file(texture.path, texture.name); + { + std::scoped_lock lock(save_lock); + loaded_textures.push_back(file); + } + }); + do + { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + { + std::scoped_lock lock(load_lock); + if (textures_to_load.empty()) + break; + } + { + std::scoped_lock lock(save_lock); + while (!loaded_textures.empty()) + { + auto* back = loaded_textures.back(); + textures_2d.insert({back->getName(), new texture_gl2D(back->texture())}); + BLT_DEBUG("Loaded Texture %s", back->getName().c_str()); + delete back; + loaded_textures.pop_back(); + } + } + } while (true); + pool.stop(); + while (!pool.complete()); + while (!loaded_textures.empty()) + { + auto* back = loaded_textures.back(); + textures_2d.insert({back->getName(), new texture_gl2D(back->texture())}); + loaded_textures.pop_back(); + } + } }