BLT-With-Graphics-Template/include/blt/gfx/renderer/resource_manager.h

184 lines
6.7 KiB
C
Raw Normal View History

2023-12-30 03:26:06 -05:00
/*
* <Short Description>
* Copyright (C) 2023 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2024-01-01 22:15:30 -05:00
#ifndef BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H
#define BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H
2023-12-30 03:26:06 -05:00
#include <blt/std/hashmap.h>
#include <blt/std/string.h>
#include <blt/meta/config_generator.h>
2023-12-30 03:26:06 -05:00
#include <blt/gfx/texture.h>
#include <mutex>
#include <vector>
#include <optional>
2023-12-30 03:26:06 -05:00
#include <string>
#include "blt/compatibility.h"
2024-10-08 19:43:21 -04:00
#include "blt/iterator/enumerate.h"
2023-12-30 03:26:06 -05:00
namespace blt::gfx
{
struct texture_info
{
public:
texture_info() = default;
explicit texture_info(std::string path, std::string name): path(std::move(path)), name(std::move(name))
{}
BLT_MAKE_GETTER(std::string, path);
BLT_MAKE_GETTER(std::string, name);
BLT_MAKE_GETTER_AND_SETTER(blt::i32, desired_width);
BLT_MAKE_GETTER_AND_SETTER(blt::i32, desired_height);
2024-09-17 01:07:27 -04:00
BLT_MAKE_GETTER_AND_SETTER(blt::i32, desired_channels);
private:
std::string path;
std::string name;
blt::i32 desired_width = 0;
blt::i32 desired_height = 0;
2024-09-17 01:07:27 -04:00
blt::i32 desired_channels = 0;
};
2023-12-30 03:26:06 -05:00
class texture_array
2023-12-30 03:26:06 -05:00
{
public:
2024-09-17 01:07:27 -04:00
texture_array(std::string name, i32 width, i32 height, blt::i32 channels = 4):
name(std::move(name)), width(width), height(height), channels(channels)
{}
void add_texture(std::string path, std::string t_name = "")
{
2024-09-17 01:07:27 -04:00
textures.push_back(texture_info{std::move(path), std::move(t_name)}.set_desired_width(width).set_desired_height(height)
.set_desired_channels(channels));
}
BLT_MAKE_GETTER(std::vector<texture_info>, textures);
BLT_MAKE_GETTER(std::string, name);
private:
std::vector<texture_info> textures;
std::string name;
i32 width;
i32 height;
2024-09-17 01:07:27 -04:00
i32 channels;
2023-12-30 03:26:06 -05:00
};
2024-01-01 22:15:30 -05:00
class resource_manager
2023-12-30 03:26:06 -05:00
{
private:
struct texture_destination
{
std::vector<texture_file*>* vec = nullptr;
std::optional<blt::size_t> order = {};
texture_destination() = default;
explicit texture_destination(std::vector<texture_file*>* vec): vec(vec)
{}
explicit texture_destination(std::vector<texture_file*>* vec, blt::size_t order): vec(vec), order(order)
{}
void add(texture_file* file) const
{
2024-09-17 01:07:27 -04:00
// if order is present the vec is assumed to be resized. otherwise assume order doesn't matter and push to an empty vec
if (order)
2024-09-17 01:07:27 -04:00
(*vec)[*order] = file;
else
vec->push_back(file);
}
};
2023-12-30 17:05:41 -05:00
std::mutex load_lock;
std::mutex save_lock;
std::vector<std::pair<texture_destination, texture_info>> textures_to_load;
2023-12-30 17:05:41 -05:00
std::vector<texture_file*> loaded_textures;
std::vector<std::pair<std::string, std::vector<texture_file*>>> loaded_arrays;
2024-03-23 19:52:57 -04:00
blt::hashmap_t<std::string, texture_gl2D*> textures_2d;
blt::hashmap_t<std::string, gl_texture2D_array*> texture_arrays_2d;
std::string resource_prefix;
void load_to_gl();
2024-09-17 01:07:27 -04:00
2023-12-30 03:26:06 -05:00
public:
2024-01-01 22:15:30 -05:00
resource_manager() = default;
2023-12-30 17:05:41 -05:00
explicit resource_manager(std::string_view resource_prefix): resource_prefix(resource_prefix)
{
if (!blt::string::ends_with(this->resource_prefix, "/"))
this->resource_prefix += '/';
}
void enqueue(std::string path, std::string name = "")
{
textures_to_load.emplace_back(texture_destination{&loaded_textures}, texture_info{std::move(path), std::move(name)});
}
void with(texture_info info)
{
textures_to_load.emplace_back(texture_destination{&loaded_textures}, std::move(info));
}
void with(texture_array array)
{
loaded_arrays.emplace_back(array.get_name(), std::vector<texture_file*>{});
auto& dest = loaded_arrays.back();
2024-09-17 01:07:27 -04:00
dest.second.reserve(array.get_textures().size());
dest.second.resize(array.get_textures().size());
for (const auto& [index, texture] : blt::enumerate(array.get_textures()))
textures_to_load.emplace_back(texture_destination{&dest.second, index}, std::move(texture));
}
2023-12-30 17:05:41 -05:00
void load_resources(std::size_t threads = 8);
inline std::optional<texture_gl2D*> get(const std::string& name)
2024-01-01 22:15:30 -05:00
{
if (name.empty() || textures_2d.find(name) == textures_2d.end())
return {};
2024-01-01 22:15:30 -05:00
return textures_2d[name];
}
/**
* Directly sets the internal representation of the texture. Can be used to set custom, non file system loaded textures.
* Note: this function takes ownership of the ptr.
*/
inline void set(std::string_view name, texture_gl2D* texture)
{
textures_2d[name] = texture;
}
/**
* Sets the directory used to prefix all asset loading with
*/
inline void setPrefixDirectory(std::string path)
{
if (!blt::string::ends_with(resource_prefix, "/"))
resource_prefix += '/';
resource_prefix = std::move(path);
}
2024-01-01 22:15:30 -05:00
void cleanup();
2023-12-30 03:26:06 -05:00
};
}
2024-01-01 22:15:30 -05:00
#endif //BLT_WITH_GRAPHICS_RESOURCE_MANAGER_H