This commit is contained in:
ZY4N
2024-12-22 16:58:40 +01:00
parent 2704814de2
commit db8db8f9d7
161 changed files with 17102 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
#include "assets/dynamic_data_loaders/dynamic_mesh_loader.hpp"
std::error_code dynamic_mesh_loader::prefetch(
loader_id_type loader_id,
const ztu::string_list& directories,
prefetch_queue& queue
) {
return this->invoke_with_matching_loader(
loader_id,
[&](auto& loader)
{
return loader.prefetch(
directories,
queue
);
}
);
}
std::error_code dynamic_mesh_loader::load(
loader_id_type loader_id,
const ztu::string_list& directories,
dynamic_mesh_store& store,
mesh_prefetch_lookup& id_lookup,
const bool pedantic
) {
return this->invoke_with_matching_loader(
loader_id,
[&](auto& loader)
{
return loader.load(
m_buffer,
directories,
store,
id_lookup,
pedantic
);
}
);
}

View File

@@ -0,0 +1,40 @@
#include "assets/dynamic_data_loaders/dynamic_point_cloud_loader.hpp"
std::error_code dynamic_point_cloud_loader::prefetch(
const loader_id_type loader_id,
const ztu::string_list& directories,
prefetch_queue& queue
) {
return this->invoke_with_matching_loader(
loader_id,
[&](auto& loader)
{
return loader.prefetch(
directories,
queue
);
}
);
}
std::error_code dynamic_point_cloud_loader::load(
const loader_id_type loader_id,
const ztu::string_list& directories,
dynamic_point_cloud_store& store,
point_cloud_prefetch_lookup& id_lookup,
const bool pedantic
) {
return this->invoke_with_matching_loader(
loader_id,
[&](auto& loader)
{
return loader.load(
m_buffer,
directories,
store,
id_lookup,
pedantic
);
}
);
}

View File

@@ -0,0 +1,122 @@
#include "assets/dynamic_data_loaders/dynamic_texture_loader.hpp"
#if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC system_header
#elif defined(_MSC_VER)
#pragma warning(push, 0)
#endif
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_STATIC
#include "stb_image.h"
#if defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
#pragma warning(pop)
#endif
#include "util/logger.hpp"
dynamic_texture_loader::dynamic_texture_loader(components::texture::flags enabled_components) :
m_enabled_components{ enabled_components },
m_loader_id_lookup{
{ "jpg", loader_id_type{ 0 } },
{ "png", loader_id_type{ 1 } },
{ "tga", loader_id_type{ 2 } },
{ "bmp", loader_id_type{ 3 } },
{ "psd", loader_id_type{ 4 } },
{ "gif", loader_id_type{ 5 } },
{ "hdr", loader_id_type{ 6 } },
{ "pic", loader_id_type{ 7 } }
} {}
std::optional<dynamic_texture_loader::loader_id_type> dynamic_texture_loader::find_loader(const std::string_view& name)
{
const auto it = m_loader_id_lookup.find(name);
if (it != m_loader_id_lookup.end())
{
return it->second;
}
return std::nullopt;
}
std::error_code dynamic_texture_loader::prefetch(
const loader_id_type loader_id,
const ztu::string_list& directories,
prefetch_queue& queue
) {
// Nothing to prefetch...
return {};
}
std::error_code dynamic_texture_loader::load(
const loader_id_type loader_id,
const ztu::string_list& directories,
dynamic_texture_store& store,
texture_prefetch_lookup& id_lookup,
const bool pedantic
) {
stbi_set_flip_vertically_on_load(true);
int width, height, channels;
for (const auto filename : directories)
{
const auto id_it = id_lookup.find(filename);
if (id_it != id_lookup.end()) [[unlikely]]
{
continue;
}
auto ptr = reinterpret_cast<std::uint8_t*>(stbi_load(
filename.data(), // Null terminated by string_list
&width,
&height,
&channels,
0
));
if (ptr == nullptr) {
return std::make_error_code(std::errc::no_such_file_or_directory);
}
auto data = std::unique_ptr<std::uint8_t[]>(ptr);
using flags = components::texture::flags;
auto components = flags{};
switch (channels)
{
case 1:
components = flags::luminance;
break;
case 2:
components = flags::luminance | flags::alpha;
break;
case 3:
components = flags::red | flags::green | flags::blue;
break;
case 4:
components = flags::red | flags::green | flags::blue | flags::alpha;
break;
default: [[unlikely]]
ztu::logger::error("Unsupported pixel component composition %", static_cast<int>(components));
continue;
}
const auto id = store.add(dynamic_texture_buffer(
std::move(data),
width,
height,
components
));
id_lookup.emplace_hint(id_it, filename, id);
}
return {};
}

View File

@@ -0,0 +1,92 @@
#ifndef INCLUDE_BASE_DYNAMIC_LOADER_IMPLEMENTATION
# error Never include this file directly include 'base_dynamic_loader.hpp'
#endif
#include "util/for_each.hpp"
template<typename C, class... Loaders>
base_dynamic_loader<C, Loaders...>::base_dynamic_loader(const C enabled_components) :
m_enabled_components{ enabled_components }
{
[&]<std::size_t... Is>(std::index_sequence<Is...>) {
m_loader_id_lookup = { { std::string{ Loaders::name }, { Is } }... };
}(std::index_sequence_for<Loaders...>());
}
template<typename C, class... Loaders>
std::optional<typename base_dynamic_loader<C, Loaders...>::loader_id_type> base_dynamic_loader<C, Loaders...>::find_loader(
std::string_view name
) {
const auto it = m_loader_id_lookup.find(name);
if (it != m_loader_id_lookup.end())
{
return it->second;
}
return std::nullopt;
}
template<typename C, class... Loaders>
consteval std::optional<typename base_dynamic_loader<C, Loaders...>::loader_id_type> base_dynamic_loader<C, Loaders...>::find_loader_static(
std::string_view name
) {
constexpr auto invalid_index = std::numeric_limits<typename loader_id_type::index_type>::max();
auto index = invalid_index;
ztu::for_each::indexed_type<Loaders...>([&]<auto Index, typename Loader>()
{
if (name == Loader::name)
{
index = Index;
return true;
}
return false;
});
return index == invalid_index ? std::nullopt : loader_id_type{ index };
}
template<typename C, class... Loaders>
template<typename base_dynamic_loader<C, Loaders...>::loader_id_type ID>
auto& base_dynamic_loader<C, Loaders...>::get_loader()
{
return std::get<ID.index>(m_loaders);
}
template<typename C, class... Loaders>
template<typename F>
ztu::result<dynamic_point_cloud_store::id_type> base_dynamic_loader<C, Loaders...>::invoke_with_matching_loader(
const loader_id_type loader_id, F&& f
) {
return std::apply(
[&](Loaders&... loaders)
{
return [&]<std::size_t... Is>(std::index_sequence<Is...>)
{
std::error_code error;
const auto found_parser = (
[&](auto& loader, const std::size_t index)
{
if (loader_id == index)
{
error = f(loader);
return true;
}
return false;
} (loaders, Is)
or ...
);
if (not found_parser)
{
error = std::make_error_code(std::errc::invalid_argument);
}
return error;
}(std::index_sequence_for<Loaders...>());
},
m_loaders
);
}