In the middle of multithreading parsers.
This commit is contained in:
@@ -1,27 +1,39 @@
|
||||
#ifndef INCLUDE_DYNAMIC_MATERIAL_DATA_IMPLEMENTATION
|
||||
# error Never include this file directly include 'dynamic_material_buffer.hpp'
|
||||
#ifndef INCLUDE_MATERIAL_DATA_IMPLEMENTATION
|
||||
# error Never include this file directly include 'material_data.hpp'
|
||||
#endif
|
||||
|
||||
inline std::optional<assets::material_components::ambient_filter>& assets::material_data::specular_filter()
|
||||
{
|
||||
return std::get<material_components::indices::ambient_filter>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::diffuse_filter>& assets::material_data::diffuse_filter()
|
||||
{
|
||||
return std::get<material_components::indices::diffuse_filter>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::specular_filter>& assets::material_data::specular_filter()
|
||||
{
|
||||
return std::get<material_components::indices::specular_filter>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::shininess>& assets::material_data::shininess()
|
||||
{
|
||||
return std::get<material_components::indices::shininess>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::alpha>& assets::material_data::alpha()
|
||||
{
|
||||
return std::get<material_components::indices::alpha>(components);
|
||||
}
|
||||
|
||||
inline std::optional<assets::material_components::surface_properties>& assets::material_data::surface_properties()
|
||||
inline std::optional<assets::material_components::ambient_filter_texture>& assets::material_data::specular_filter_texture_id()
|
||||
{
|
||||
return std::get<material_components::indices::surface_properties>(components);
|
||||
return std::get<material_components::indices::ambient_filter_texture>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::transparency>& assets::material_data::transparency()
|
||||
inline std::optional<assets::material_components::diffuse_filter_texture>& assets::material_data::diffuse_filter_texture_id()
|
||||
{
|
||||
return std::get<material_components::indices::transparency>(components);
|
||||
return std::get<material_components::indices::diffuse_filter_texture>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::ambient_color_texture>& assets::material_data::ambient_color_texture_id()
|
||||
inline std::optional<assets::material_components::specular_filter_texture>& assets::material_data::specular_filter_texture_id()
|
||||
{
|
||||
return std::get<material_components::indices::ambient_color_texture>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::diffuse_color_texture>& assets::material_data::diffuse_color_texture_id()
|
||||
{
|
||||
return std::get<material_components::indices::diffuse_color_texture>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::specular_color_texture>& assets::material_data::specular_color_texture_id()
|
||||
{
|
||||
return std::get<material_components::indices::specular_color_texture>(components);
|
||||
return std::get<material_components::indices::specular_filter_texture>(components);
|
||||
}
|
||||
inline std::optional<assets::material_components::shininess_texture>& assets::material_data::shininess_texture_id()
|
||||
{
|
||||
@@ -36,29 +48,40 @@ inline std::optional<assets::material_components::bump_texture>& assets::materia
|
||||
return std::get<material_components::indices::bump_texture(components);
|
||||
}
|
||||
|
||||
inline const std::optional<assets::material_components::surface_properties>& assets::material_data::surface_properties() const
|
||||
inline const std::optional<assets::material_components::ambient_filter>& assets::material_data::specular_filter() const
|
||||
{
|
||||
return std::get<material_components::indices::surface_properties>(components);
|
||||
return std::get<material_components::indices::ambient_filter>(components);
|
||||
}
|
||||
inline const std::optional<assets::material_components::diffuse_filter>& assets::material_data::diffuse_filter() const
|
||||
{
|
||||
return std::get<material_components::indices::diffuse_filter>(components);
|
||||
}
|
||||
inline const std::optional<assets::material_components::specular_filter>& assets::material_data::specular_filter() const
|
||||
{
|
||||
return std::get<material_components::indices::specular_filter>(components);
|
||||
}
|
||||
inline const std::optional<assets::material_components::shininess>& assets::material_data::shininess() const
|
||||
{
|
||||
return std::get<material_components::indices::shininess>(components);
|
||||
}
|
||||
inline const std::optional<assets::material_components::alpha>& assets::material_data::alpha() const
|
||||
{
|
||||
return std::get<material_components::indices::alpha>(components);
|
||||
}
|
||||
|
||||
inline const std::optional<assets::material_components::transparency>& assets::material_data::transparency() const
|
||||
inline const std::optional<assets::material_components::ambient_filter_texture>& assets::material_data::specular_filter_texture_id() const
|
||||
{
|
||||
return std::get<material_components::indices::transparency>(components);
|
||||
return std::get<material_components::indices::ambient_filter_texture>(components);
|
||||
}
|
||||
|
||||
inline const std::optional<assets::material_components::ambient_color_texture>& assets::material_data::ambient_color_texture_id() const
|
||||
inline const std::optional<assets::material_components::diffuse_filter_texture>& assets::material_data::diffuse_filter_texture_id() const
|
||||
{
|
||||
return std::get<material_components::indices::ambient_color_texture>(components);
|
||||
return std::get<material_components::indices::diffuse_filter_texture>(components);
|
||||
}
|
||||
|
||||
inline const std::optional<assets::material_components::diffuse_color_texture>& assets::material_data::diffuse_color_texture_id() const
|
||||
inline const std::optional<assets::material_components::specular_filter_texture>& assets::material_data::specular_filter_texture_id() const
|
||||
{
|
||||
return std::get<material_components::indices::diffuse_color_texture>(components);
|
||||
}
|
||||
|
||||
inline const std::optional<assets::material_components::specular_color_texture>& assets::material_data::specular_color_texture_id() const
|
||||
{
|
||||
return std::get<material_components::indices::specular_color_texture>(components);
|
||||
return std::get<material_components::indices::specular_filter_texture>(components);
|
||||
}
|
||||
|
||||
inline const std::optional<assets::material_components::shininess_texture>& assets::material_data::shininess_texture_id() const
|
||||
@@ -76,17 +99,6 @@ inline const std::optional<assets::material_components::bump_texture>& assets::m
|
||||
return std::get<material_components::indices::bump_texture>(components);
|
||||
}
|
||||
|
||||
|
||||
inline assets::material_components::surface_properties& assets::material_data::initialized_surface_properties()
|
||||
{
|
||||
auto& surface_properties_opt = surface_properties();
|
||||
if (not surface_properties_opt)
|
||||
{
|
||||
surface_properties_opt = material_components::surface_properties{};
|
||||
}
|
||||
return *surface_properties_opt;
|
||||
}
|
||||
|
||||
inline void assets::material_data::clear()
|
||||
{
|
||||
clear_components();
|
||||
|
||||
@@ -230,13 +230,13 @@ assets::detail::generic_point_cloud_store<Ts...>::array_counts() const
|
||||
|
||||
template<typename... Ts>
|
||||
typename assets::detail::generic_point_cloud_store<Ts...>::id_type assets::detail::generic_point_cloud_store<Ts...>::add(
|
||||
const point_cloud_data& point_cloud
|
||||
const id_type id,
|
||||
const data_type& point_cloud
|
||||
) {
|
||||
|
||||
const auto id = id_type{ m_next_data_id.index++ };
|
||||
m_ids.push_back(id);
|
||||
|
||||
const auto& vertices = point_cloud.vertices;
|
||||
const auto& vertices = point_cloud.component_arrays;
|
||||
|
||||
auto component_flags = component_flag_type{};
|
||||
auto min_component_count = count_type{};
|
||||
@@ -403,14 +403,3 @@ typename assets::detail::generic_point_cloud_store<Ts...>::const_iterator assets
|
||||
{
|
||||
return const_cast<const generic_point_cloud_store*>(this)->end();
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
typename assets::detail::generic_point_cloud_store<Ts...>::view_type assets::detail::generic_point_cloud_store<Ts...>::view()
|
||||
{
|
||||
return { begin(), end() };
|
||||
}
|
||||
template<typename... Ts>
|
||||
typename assets::detail::generic_point_cloud_store<Ts...>::const_view_type assets::detail::generic_point_cloud_store<Ts...>::view() const
|
||||
{
|
||||
return { begin(), end() };
|
||||
}
|
||||
|
||||
289
source/assets/data_stores/pose_list_store.ipp
Normal file
289
source/assets/data_stores/pose_list_store.ipp
Normal file
@@ -0,0 +1,289 @@
|
||||
#ifndef INCLUDE_POSE_LIST_STORE_IMPLEMENTATION
|
||||
# error Never include this file directly include 'pose_list_store.hpp'
|
||||
#endif
|
||||
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <tuple>
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char>::pose_list_store_iterator(
|
||||
id_iterator_type ids,
|
||||
pose_iterator_type poses,
|
||||
length_iterator_type lengths,
|
||||
std::size_t index,
|
||||
const offset_type& offset
|
||||
) :
|
||||
m_ids{ ids },
|
||||
m_poses{ poses },
|
||||
m_lengths{ lengths },
|
||||
m_index{ index },
|
||||
m_offset{ offset } {}
|
||||
|
||||
|
||||
template<typename Char>
|
||||
typename assets::pose_list_store_iterator<Char>::reference assets::pose_list_store_iterator<Char>::operator*() const {
|
||||
return dereference(std::index_sequence_for<Char>{});
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char>& assets::pose_list_store_iterator<Char>::operator++() {
|
||||
adjust_offsets(std::index_sequence_for<Char>{}, 1);
|
||||
++m_index;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char> assets::pose_list_store_iterator<Char>::operator++(int) {
|
||||
pose_list_store_iterator tmp = *this;
|
||||
++(*this);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char>& assets::pose_list_store_iterator<Char>::operator--() {
|
||||
adjust_offsets(std::index_sequence_for<Char>{}, -1);
|
||||
--m_index;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char> assets::pose_list_store_iterator<Char>::operator--(int) {
|
||||
auto tmp = *this;
|
||||
--(*this);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char>& assets::pose_list_store_iterator<Char>::operator+=(const difference_type n)
|
||||
{
|
||||
adjust_offsets(std::index_sequence_for<Char>{}, n);
|
||||
m_index += n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char>& assets::pose_list_store_iterator<Char>::operator-=(const difference_type n)
|
||||
{
|
||||
return (*this) += -n;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char> assets::pose_list_store_iterator<Char>::operator+(const difference_type n) const
|
||||
{
|
||||
auto tmp = *this;
|
||||
return tmp += n; // TODO clion says n is unused
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
assets::pose_list_store_iterator<Char> assets::pose_list_store_iterator<Char>::operator-(const difference_type n) const
|
||||
{
|
||||
auto tmp = *this;
|
||||
return tmp -= n; // TODO clion says n is unused
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
typename assets::pose_list_store_iterator<Char>::difference_type
|
||||
assets::pose_list_store_iterator<Char>::operator-(const pose_list_store_iterator& other) const
|
||||
{
|
||||
return static_cast<difference_type>(m_index) - static_cast<difference_type>(other.m_index);
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
typename assets::pose_list_store_iterator<Char>::reference assets::pose_list_store_iterator<Char>::operator[](
|
||||
const difference_type n
|
||||
) const {
|
||||
return *(*this + n);
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
bool assets::pose_list_store_iterator<Char>::operator==(const pose_list_store_iterator& other) const
|
||||
{
|
||||
return m_ids == other.m_ids and m_index == other.m_index;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
bool assets::pose_list_store_iterator<Char>::operator!=(const pose_list_store_iterator& other) const
|
||||
{
|
||||
return not (*this == other);
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
bool assets::pose_list_store_iterator<Char>::operator<(const pose_list_store_iterator& other) const
|
||||
{
|
||||
return m_index < other.m_index;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
bool assets::pose_list_store_iterator<Char>::operator<=(const pose_list_store_iterator& other) const
|
||||
{
|
||||
return m_index <= other.m_index;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
bool assets::pose_list_store_iterator<Char>::operator>(const pose_list_store_iterator& other) const
|
||||
{
|
||||
return m_index > other.m_index;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
bool assets::pose_list_store_iterator<Char>::operator>=(const pose_list_store_iterator& other) const
|
||||
{
|
||||
return m_index >= other.m_index;
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
void assets::pose_list_store_iterator<Char>::calc_offset(
|
||||
difference_type n
|
||||
) {
|
||||
|
||||
const auto negative = n < difference_type{ 0 };
|
||||
const auto positive = n > difference_type{ 0 };
|
||||
const auto step = difference_type{ positive } - difference_type{ negative };
|
||||
n = negative ? -n : n;
|
||||
|
||||
// TODO template optimize for single steps
|
||||
|
||||
while (n--)
|
||||
{
|
||||
const auto& count = m_lengths[m_index];
|
||||
m_offset += step * count;
|
||||
m_index += step;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
typename assets::pose_list_store_iterator<Char>::reference
|
||||
assets::pose_list_store_iterator<Char>::dereference() const
|
||||
{
|
||||
return std::make_pair(
|
||||
m_ids[m_index],
|
||||
pose_list_view{
|
||||
m_poses[m_offset],
|
||||
m_lengths[m_index]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
assets::pose_list_store::id_type assets::pose_list_store::add(
|
||||
const pose_list_id id,
|
||||
const pose_list_data& pose_list
|
||||
) {
|
||||
|
||||
m_ids.push_back(id);
|
||||
|
||||
m_poses.insert(m_poses.end(), pose_list.begin(), pose_list.end());
|
||||
|
||||
m_lengths.push_back(pose_list.size());
|
||||
return id;
|
||||
}
|
||||
|
||||
std::pair<assets::pose_list_store::iterator_type, bool> assets::pose_list_store::find(id_type id)
|
||||
{
|
||||
const auto id_it = std::ranges::upper_bound(m_ids, id);
|
||||
|
||||
const auto match = (
|
||||
id_it != m_ids.begin() and
|
||||
*std::prev(id_it) == id
|
||||
);
|
||||
|
||||
const auto index = id_it - m_ids.begin() - match;
|
||||
|
||||
auto it = begin();
|
||||
it += index;
|
||||
|
||||
return { it, match };
|
||||
}
|
||||
|
||||
std::pair<assets::pose_list_store::const_iterator, bool> assets::pose_list_store::find(id_type id) const
|
||||
{
|
||||
const auto id_it = std::ranges::upper_bound(m_ids, id);
|
||||
|
||||
const auto match = (
|
||||
id_it != m_ids.begin() and
|
||||
*std::prev(id_it) == id
|
||||
);
|
||||
|
||||
const auto index = id_it - m_ids.begin() - match;
|
||||
|
||||
auto it = begin();
|
||||
it += index;
|
||||
|
||||
return { it, match };
|
||||
}
|
||||
|
||||
void assets::pose_list_store::remove(const iterator_type& it)
|
||||
{
|
||||
m_ids.erase(m_ids.begin() + it.m_index);
|
||||
|
||||
const auto begin = m_poses.begin() + it.m_offset;
|
||||
const auto end = begin + it.m_lengths[it.m_index];
|
||||
m_poses.erase(begin, end);
|
||||
|
||||
m_lengths.erase(m_lengths.begin() + it.m_index);
|
||||
}
|
||||
|
||||
void assets::pose_list_store::clear()
|
||||
{
|
||||
m_ids.clear();
|
||||
m_poses.clear();
|
||||
m_lengths.clear();
|
||||
}
|
||||
|
||||
assets::pose_list_store::iterator_type assets::pose_list_store::begin()
|
||||
{
|
||||
return iterator_type{
|
||||
m_ids.data(),
|
||||
m_poses.data(),
|
||||
m_lengths.data(),
|
||||
0,
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
||||
assets::pose_list_store::iterator_type assets::pose_list_store::end()
|
||||
{
|
||||
return iterator_type{
|
||||
m_ids.data(),
|
||||
m_poses.data(),
|
||||
m_lengths.data(),
|
||||
m_lengths.size(),
|
||||
m_poses.size()
|
||||
};
|
||||
}
|
||||
|
||||
assets::pose_list_store::const_iterator assets::pose_list_store::begin() const
|
||||
{
|
||||
return const_iterator{
|
||||
m_ids.data(),
|
||||
m_poses.data(),
|
||||
m_lengths.data(),
|
||||
0,
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
||||
assets::pose_list_store::const_iterator assets::pose_list_store::end() const
|
||||
{
|
||||
return const_iterator{
|
||||
m_ids.data(),
|
||||
m_poses.data(),
|
||||
m_lengths.data(),
|
||||
m_lengths.size(),
|
||||
m_poses.size()
|
||||
};
|
||||
}
|
||||
|
||||
assets::pose_list_store::const_iterator assets::pose_list_store::cbegin() const
|
||||
{
|
||||
return this->begin();
|
||||
}
|
||||
|
||||
assets::pose_list_store::const_iterator assets::pose_list_store::cend() const
|
||||
{
|
||||
return this->end();
|
||||
}
|
||||
@@ -9,17 +9,21 @@
|
||||
#include <type_traits>
|
||||
#include <algorithm>
|
||||
|
||||
#include "opengl/metadata/shader_source_metadata.hpp"
|
||||
|
||||
template<typename Char>
|
||||
assets::shader_source_store_iterator<Char>::shader_source_store_iterator(
|
||||
id_iterator_type ids,
|
||||
string_iterator_type strings,
|
||||
length_iterator_type lengths,
|
||||
metadata_iterator_type metadata,
|
||||
std::size_t index,
|
||||
const offset_type& offset
|
||||
) :
|
||||
m_ids{ ids },
|
||||
m_strings{ strings },
|
||||
m_lengths{ lengths },
|
||||
m_metadata{ metadata },
|
||||
m_index{ index },
|
||||
m_offset{ offset } {}
|
||||
|
||||
@@ -156,25 +160,23 @@ void assets::shader_source_store_iterator<Char>::calc_offset(
|
||||
}
|
||||
|
||||
template<typename Char>
|
||||
template<std::size_t... Is>
|
||||
typename assets::shader_source_store_iterator<Char>::reference
|
||||
assets::shader_source_store_iterator<Char>::dereference(std::index_sequence<Is...>) const
|
||||
assets::shader_source_store_iterator<Char>::dereference() const
|
||||
{
|
||||
return std::make_pair(
|
||||
m_ids[m_index],
|
||||
shader_source_view(
|
||||
m_strings[m_offset],
|
||||
m_lengths[m_index]
|
||||
)
|
||||
shader_source_view{
|
||||
.source = { m_strings[m_offset], m_lengths[m_index] },
|
||||
.meta = m_metadata[m_index]
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
assets::shader_source_store::id_type assets::shader_source_store::add(
|
||||
const shader_source_id id,
|
||||
const shader_source_data& shader_source
|
||||
) {
|
||||
|
||||
const auto id = id_type{ m_next_data_id.index++ };
|
||||
m_ids.push_back(id);
|
||||
|
||||
m_strings.reserve(m_strings.size() + shader_source.source.size() + 1);
|
||||
@@ -183,6 +185,8 @@ assets::shader_source_store::id_type assets::shader_source_store::add(
|
||||
|
||||
m_lengths.push_back(shader_source.source.size());
|
||||
|
||||
m_metadata.push_back(shader_source.meta);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -229,6 +233,7 @@ void assets::shader_source_store::remove(const iterator_type& it)
|
||||
m_strings.erase(begin, end);
|
||||
|
||||
m_lengths.erase(m_lengths.begin() + it.m_index);
|
||||
m_metadata.erase(m_metadata.begin() + it.m_index);
|
||||
}
|
||||
|
||||
void assets::shader_source_store::clear()
|
||||
@@ -236,6 +241,7 @@ void assets::shader_source_store::clear()
|
||||
m_ids.clear();
|
||||
m_strings.clear();
|
||||
m_lengths.clear();
|
||||
m_metadata.clear();
|
||||
}
|
||||
|
||||
assets::shader_source_store::iterator_type assets::shader_source_store::begin()
|
||||
@@ -244,6 +250,7 @@ assets::shader_source_store::iterator_type assets::shader_source_store::begin()
|
||||
m_ids.data(),
|
||||
m_strings.data(),
|
||||
m_lengths.data(),
|
||||
m_metadata.data(),
|
||||
0,
|
||||
{}
|
||||
};
|
||||
@@ -255,6 +262,7 @@ assets::shader_source_store::iterator_type assets::shader_source_store::end()
|
||||
m_ids.data(),
|
||||
m_strings.data(),
|
||||
m_lengths.data(),
|
||||
m_metadata.data(),
|
||||
m_lengths.size(),
|
||||
m_strings.size()
|
||||
};
|
||||
@@ -266,6 +274,7 @@ assets::shader_source_store::const_iterator assets::shader_source_store::begin()
|
||||
m_ids.data(),
|
||||
m_strings.data(),
|
||||
m_lengths.data(),
|
||||
m_metadata.data(),
|
||||
0,
|
||||
{}
|
||||
};
|
||||
@@ -277,6 +286,7 @@ assets::shader_source_store::const_iterator assets::shader_source_store::end() c
|
||||
m_ids.data(),
|
||||
m_strings.data(),
|
||||
m_lengths.data(),
|
||||
m_metadata.data(),
|
||||
m_lengths.size(),
|
||||
m_strings.size()
|
||||
};
|
||||
@@ -284,20 +294,10 @@ assets::shader_source_store::const_iterator assets::shader_source_store::end() c
|
||||
|
||||
assets::shader_source_store::const_iterator assets::shader_source_store::cbegin() const
|
||||
{
|
||||
return const_cast<const shader_source_store*>(this)->begin();
|
||||
return this->begin();
|
||||
}
|
||||
|
||||
assets::shader_source_store::const_iterator assets::shader_source_store::cend() const
|
||||
{
|
||||
return const_cast<const shader_source_store*>(this)->end();
|
||||
}
|
||||
|
||||
assets::shader_source_store::view_type assets::shader_source_store::view()
|
||||
{
|
||||
return { begin(), end() };
|
||||
}
|
||||
|
||||
assets::shader_source_store::const_view_type assets::shader_source_store::view() const
|
||||
{
|
||||
return { begin(), end() };
|
||||
return this->end();
|
||||
}
|
||||
|
||||
29
source/assets/fallback_data/default_texture_data.cpp
Normal file
29
source/assets/fallback_data/default_texture_data.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "assets/fallback_data/fallback_texture.hpp"
|
||||
|
||||
assets::texture_data assets::generate_fallback_texture(
|
||||
const int width,
|
||||
const int height,
|
||||
const std::span<const fallback_color> colors
|
||||
) {
|
||||
const auto pixel_count = width * height;
|
||||
|
||||
auto data = std::make_unique<texture_data::value_type[]>(pixel_count * sizeof(fallback_color));
|
||||
|
||||
auto pixels = reinterpret_cast<fallback_color*>(data.get());
|
||||
|
||||
for (int y{}; y != height; ++y)
|
||||
{
|
||||
for (int x{}; x != width; ++x)
|
||||
{
|
||||
const auto index = (x + y) % colors.size();
|
||||
*pixels++ = colors[index];
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
std::move(data),
|
||||
width,
|
||||
height,
|
||||
fallback_color::components
|
||||
};
|
||||
}
|
||||
8
source/assets/fallback_data/fallback_material.cpp
Normal file
8
source/assets/fallback_data/fallback_material.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "assets/fallback_data/fallback_material.hpp"
|
||||
|
||||
assets::material_data generate_fallback_material()
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
#include "../../../include/assets/data_parsers"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
std::error_code glsl_loader::load(
|
||||
const std::filesystem::path& filename,
|
||||
std::string& source
|
||||
) {
|
||||
|
||||
auto file = std::ifstream(filename);
|
||||
if (not file.is_open())
|
||||
{
|
||||
return std::make_error_code(std::errc::no_such_file_or_directory);
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
const auto size = file.tellg();
|
||||
|
||||
if (size == 0 or size == std::numeric_limits<std::streamsize>::max())
|
||||
{
|
||||
return std::make_error_code(std::errc::invalid_seek);
|
||||
}
|
||||
|
||||
source.reserve(size);
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
source.assign(
|
||||
std::istreambuf_iterator<char>(file),
|
||||
std::istreambuf_iterator<char>()
|
||||
);
|
||||
|
||||
file.close();
|
||||
|
||||
return {};
|
||||
}
|
||||
463
source/assets/file_parsers/glsl_parser.cpp
Normal file
463
source/assets/file_parsers/glsl_parser.cpp
Normal file
@@ -0,0 +1,463 @@
|
||||
#include "assets/file_parsers/glsl_parser.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <numeric>
|
||||
|
||||
#include "assets/components/mesh_shader_components.hpp"
|
||||
#include "assets/components/point_cloud_shader_components.hpp"
|
||||
#include "util/logger.hpp"
|
||||
#include <mutex>
|
||||
#include <execution>
|
||||
|
||||
namespace assets::language
|
||||
{
|
||||
enum class declaration_type : std::size_t
|
||||
{
|
||||
stage = 0,
|
||||
geometry = 1,
|
||||
features = 2,
|
||||
static_enable = 3,
|
||||
dynamic_enable = 4,
|
||||
invalid = std::numeric_limits<std::size_t>::max()
|
||||
};
|
||||
|
||||
using stage = shader_components::stage;
|
||||
|
||||
inline constexpr auto declaration_prefix = std::string_view("\n#pragma ");
|
||||
inline constexpr auto title_separator = ':';
|
||||
inline constexpr auto value_separator = ' ';
|
||||
|
||||
inline auto declaration_lookup = ztu::string_lookup<declaration_type>{
|
||||
{ "STAGE", declaration_type::stage },
|
||||
{ "GEOMETRY", declaration_type::geometry },
|
||||
{ "FEATURES", declaration_type::features },
|
||||
{ "STATIC_ENABLE", declaration_type::static_enable },
|
||||
{ "DYNAMIC_ENABLE", declaration_type::dynamic_enable }
|
||||
};
|
||||
|
||||
inline auto stage_lookup = ztu::string_lookup<shader_components::stage>{
|
||||
{ "VERTEX", stage::vertex },
|
||||
{ "TESSELATION_CONTROL", stage::tesselation_control },
|
||||
{ "TESSELATION_EVALUATION", stage::tesselation_evaluation },
|
||||
{ "GEOMETRY", stage::geometry },
|
||||
{ "FRAGMENT", stage::fragment },
|
||||
};
|
||||
|
||||
inline auto geometry_lookup = ztu::string_lookup<model_geometry::types>{
|
||||
{ "MESH", model_geometry::types::mesh },
|
||||
{ "POINT_CLOUD", model_geometry::types::point_cloud }
|
||||
};
|
||||
|
||||
inline auto mesh_feature_lookup = ztu::string_lookup<shader_components::flags>{
|
||||
{ "FACE", static_cast<shader_components::flags>(mesh_shader_components::flags::face) },
|
||||
{ "LINE", static_cast<shader_components::flags>(mesh_shader_components::flags::line) },
|
||||
{ "POINT", static_cast<shader_components::flags>(mesh_shader_components::flags::point) },
|
||||
{ "V_L", static_cast<shader_components::flags>(mesh_shader_components::flags::luminance) },
|
||||
{ "V_RGB", static_cast<shader_components::flags>(mesh_shader_components::flags::color) },
|
||||
{ "V_R", static_cast<shader_components::flags>(mesh_shader_components::flags::alpha) },
|
||||
{ "COLOR_TEXTURE", static_cast<shader_components::flags>(mesh_shader_components::flags::color_texture) },
|
||||
{ "U_LIGHTING", static_cast<shader_components::flags>(mesh_shader_components::flags::uniform_lighting) },
|
||||
{ "T_LIGHTING", static_cast<shader_components::flags>(mesh_shader_components::flags::textured_lighting) },
|
||||
{ "U_RGBA", static_cast<shader_components::flags>(mesh_shader_components::flags::uniform_color) }
|
||||
};
|
||||
|
||||
inline auto point_cloud_feature_lookup = ztu::string_lookup<shader_components::flags>{
|
||||
{ "SQUARE", static_cast<shader_components::flags>(point_cloud_shader_components::flags::square) },
|
||||
{ "LIGHTING", static_cast<shader_components::flags>(point_cloud_shader_components::flags::lighting) },
|
||||
{ "V_L", static_cast<shader_components::flags>(point_cloud_shader_components::flags::luminance) },
|
||||
{ "V_RGB", static_cast<shader_components::flags>(point_cloud_shader_components::flags::color) },
|
||||
{ "V_A", static_cast<shader_components::flags>(point_cloud_shader_components::flags::alpha) },
|
||||
{ "U_RGBA", static_cast<shader_components::flags>(point_cloud_shader_components::flags::uniform_color) },
|
||||
{ "RAINBOW", static_cast<shader_components::flags>(point_cloud_shader_components::flags::rainbow) }
|
||||
};
|
||||
}
|
||||
|
||||
assets::glsl_parser::parser_context::parser_context(
|
||||
store_type& m_store,
|
||||
std::mutex& m_store_mutex
|
||||
) : m_store{ &m_store }, m_store_mutex{ &m_store_mutex }
|
||||
{
|
||||
m_buffer.source.reserve(4096);
|
||||
m_value_buffer.reserve(64);
|
||||
m_declaration_value_count_buffer.reserve(8);
|
||||
}
|
||||
|
||||
void assets::glsl_parser::parser_context::reset()
|
||||
{
|
||||
m_buffer.clear();
|
||||
m_value_buffer.clear();
|
||||
m_declaration_value_count_buffer.clear();
|
||||
std::ranges::fill(
|
||||
m_declaration_type_index_buffer,
|
||||
static_cast<std::size_t>(language::declaration_type::invalid)
|
||||
);
|
||||
}
|
||||
|
||||
void assets::glsl_parser::parser_context::operator()(lookup_type::const_pointer entry) noexcept
|
||||
{
|
||||
const auto& [ filename, id ] = *entry;
|
||||
|
||||
if (const auto e = read_file(filename, m_buffer.source))
|
||||
{
|
||||
ztu::logger::warn("Could not load shader source %: %.", filename, e.message());
|
||||
return;
|
||||
}
|
||||
|
||||
tokenize_declarations();
|
||||
|
||||
if (not parse_metadata_from_tokens())
|
||||
{
|
||||
ztu::logger::warn("Ignoring shader % as it contains malformed metadata.", id);
|
||||
return;
|
||||
}
|
||||
|
||||
remove_metadata_declarations();
|
||||
|
||||
{
|
||||
auto lock = std::lock_guard{ *m_store_mutex };
|
||||
m_store->add(id, m_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void assets::glsl_parser::parser_context::tokenize_declarations()
|
||||
{
|
||||
auto source_rest = std::string_view( m_buffer.source.data(), m_buffer.source.size() );
|
||||
auto offset = std::string_view::size_type{};
|
||||
|
||||
while ((offset = source_rest.find(language::declaration_prefix)) != std::string_view::npos)
|
||||
{
|
||||
const auto current_token_count = m_value_buffer.size();
|
||||
|
||||
auto line_end = source_rest.find('\n', offset);
|
||||
if (line_end == std::string_view::npos)
|
||||
{
|
||||
line_end = source_rest.length();
|
||||
}
|
||||
|
||||
auto declaration = source_rest.substr(offset, line_end - offset);
|
||||
|
||||
if ((offset = declaration.find(language::title_separator)) == std::string_view::npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto title = declaration.substr(0, offset);
|
||||
if (const auto it = language::declaration_lookup.find(title); it != language::declaration_lookup.end())
|
||||
{
|
||||
const auto declaration_type = static_cast<std::size_t>(it->second);
|
||||
m_declaration_type_index_buffer[declaration_type] = m_declaration_value_count_buffer.size();
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
declaration = declaration.substr(offset);
|
||||
|
||||
if (not declaration.empty() and declaration.front() == language::value_separator)
|
||||
{
|
||||
declaration = declaration.substr(sizeof(language::value_separator), declaration.length());
|
||||
}
|
||||
|
||||
while ((offset = declaration.find(language::value_separator)) != std::string_view::npos)
|
||||
{
|
||||
m_value_buffer.emplace_back(declaration.substr(0, offset));
|
||||
declaration = declaration.substr(offset + sizeof(language::value_separator));
|
||||
}
|
||||
|
||||
if (not declaration.empty())
|
||||
{
|
||||
m_value_buffer.emplace_back(declaration);
|
||||
}
|
||||
|
||||
m_declaration_value_count_buffer.emplace_back(
|
||||
m_value_buffer.size() - current_token_count
|
||||
);
|
||||
|
||||
// Preserve line break
|
||||
source_rest = source_rest.substr(line_end);
|
||||
}
|
||||
}
|
||||
|
||||
bool assets::glsl_parser::parser_context::parse_metadata_from_tokens()
|
||||
{
|
||||
using namespace std::string_view_literals;
|
||||
|
||||
for (const auto [ type, name, parser ] :
|
||||
{
|
||||
std::make_tuple(language::declaration_type::stage, "stage"sv, &parse_stage_declaration),
|
||||
std::make_tuple(language::declaration_type::geometry, "geometry"sv, &parse_geometry_declaration),
|
||||
std::make_tuple(language::declaration_type::features, "features"sv, &parse_components_declaration),
|
||||
std::make_tuple(language::declaration_type::static_enable, "static_enable"sv, &parse_static_enable_declaration),
|
||||
std::make_tuple(language::declaration_type::dynamic_enable, "dynamic_enable"sv, &parse_dynamic_enable_declaration)
|
||||
}
|
||||
) {
|
||||
const auto index = m_declaration_type_index_buffer[static_cast<std::size_t>(type)];
|
||||
|
||||
if (index == static_cast<std::size_t>(language::declaration_type::invalid))
|
||||
{
|
||||
ztu::logger::warn("Shader metadata error: Missing % declaration.", name);
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto value_token_offset = std::accumulate(
|
||||
m_declaration_value_count_buffer.begin(),
|
||||
m_declaration_value_count_buffer.begin() + index,
|
||||
std::size_t{}
|
||||
);
|
||||
const auto value_token_count = m_declaration_value_count_buffer[index];
|
||||
|
||||
if (not parser(std::span(m_value_buffer).subspan(value_token_offset, value_token_count), m_buffer))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void assets::glsl_parser::parser_context::remove_metadata_declarations()
|
||||
{
|
||||
|
||||
if (m_declaration_value_count_buffer.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. For every declaration expand the first value view to the size of the line.
|
||||
// 2. Delete all other value views as they are not needed anymore.
|
||||
// 3. Delete all declaration lines by overwriting them with the following source code.
|
||||
// 4. Resize the source vector to the new character count.
|
||||
|
||||
const auto source_view = std::string_view{ m_buffer.source.data(), m_buffer.source.size() };
|
||||
const auto value_span = std::span(m_value_buffer);
|
||||
|
||||
auto offset = std::size_t{};
|
||||
for (auto [ index, count ] : std::ranges::views::enumerate(m_declaration_value_count_buffer))
|
||||
{
|
||||
const auto values = value_span.subspan(offset, count);
|
||||
const auto value = values.front();
|
||||
|
||||
std::size_t begin_pos = value.data() - source_view.data();
|
||||
std::size_t end_pos = begin_pos + value.size();
|
||||
|
||||
begin_pos = source_view.rfind('\n', begin_pos);
|
||||
if (begin_pos == std::string_view::npos)
|
||||
{
|
||||
begin_pos = 0;
|
||||
}
|
||||
|
||||
end_pos = source_view.find('\n', end_pos);
|
||||
if (end_pos == std::string_view::npos)
|
||||
{
|
||||
end_pos = source_view.length();
|
||||
}
|
||||
else
|
||||
{
|
||||
end_pos += sizeof('\n');
|
||||
}
|
||||
|
||||
m_value_buffer[index] = source_view.substr(begin_pos, end_pos - begin_pos);
|
||||
|
||||
offset += count;
|
||||
}
|
||||
|
||||
const auto lines = std::span(m_value_buffer.data(), m_declaration_value_count_buffer.size());
|
||||
|
||||
const auto& first_line = lines.front();
|
||||
auto source_end = first_line.begin();
|
||||
auto line_end = first_line.end();
|
||||
|
||||
for (const auto& line : lines.subspan(1))
|
||||
{
|
||||
std::copy(line_end, line.begin(), source_end);
|
||||
source_end += line.size();
|
||||
line_end = line.end();
|
||||
}
|
||||
|
||||
const auto source_rest_size = m_buffer.source.end().base() - line_end;
|
||||
std::copy_n(line_end, source_rest_size, source_end);
|
||||
|
||||
source_end += source_rest_size;
|
||||
|
||||
m_buffer.source.resize(source_end - m_buffer.source.data());
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::error_code assets::glsl_parser::read_file(
|
||||
const std::filesystem::path& filename,
|
||||
std::vector<char>& source
|
||||
) {
|
||||
auto file = std::ifstream{ filename };
|
||||
|
||||
if (not file.is_open())
|
||||
{
|
||||
return std::make_error_code(std::errc::no_such_file_or_directory);
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::end);
|
||||
const std::streampos size = file.tellg();
|
||||
|
||||
if (size == 0 or size == std::numeric_limits<std::streampos>::max())
|
||||
{
|
||||
return std::make_error_code(std::errc::invalid_seek);
|
||||
}
|
||||
|
||||
file.seekg(0, std::ios::beg);
|
||||
|
||||
source.clear();
|
||||
source.reserve(sizeof('\n') + size);
|
||||
source.push_back('\n');
|
||||
|
||||
std::copy(
|
||||
std::istreambuf_iterator<char>(file),
|
||||
std::istreambuf_iterator<char>(),
|
||||
std::back_inserter(source)
|
||||
);
|
||||
|
||||
file.close();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::error_code assets::glsl_parser::prefetch(
|
||||
path_id_lookups&
|
||||
) {
|
||||
// Nothing to prefetch
|
||||
return {};
|
||||
}
|
||||
|
||||
std::error_code assets::glsl_parser::load(
|
||||
const path_id_lookups& lookups,
|
||||
shader_source_store& store,
|
||||
bool
|
||||
) {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
m_path_buffer.clear();
|
||||
lookups.shader_sources.by_extension(".glsl", m_path_buffer);
|
||||
|
||||
auto store_mutex = std::mutex{};
|
||||
|
||||
std::for_each(
|
||||
std::execution::parallel_unsequenced_policy{},
|
||||
m_path_buffer.begin(),
|
||||
m_path_buffer.end(),
|
||||
parser_context{ store, store_mutex }
|
||||
);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
bool assets::glsl_parser::parse_geometry_declaration(
|
||||
const std::span<const std::string_view> values,
|
||||
model_geometry::types& geometry_type
|
||||
) {
|
||||
|
||||
if (values.size() != 1)
|
||||
{
|
||||
ztu::logger::warn("Invalid geometry declaration: Expected exactly one token but got %.", values.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto value = values.front();
|
||||
|
||||
if (const auto it = language::geometry_lookup.find(value); it != language::geometry_lookup.end())
|
||||
{
|
||||
geometry_type = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::warn("Invalid geometry declaration: Unknown geometry %.", value);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool assets::glsl_parser::parse_stage_declaration(
|
||||
const std::span<const std::string_view> values,
|
||||
shader_components::stage& stage
|
||||
) {
|
||||
|
||||
if (values.size() != 1)
|
||||
{
|
||||
ztu::logger::warn("Invalid stage declaration: Expected exactly one token but got %.", values.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto value = values.front();
|
||||
|
||||
if (const auto it = language::stage_lookup.find(value); it != language::stage_lookup.end())
|
||||
{
|
||||
stage = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::warn("Invalid stage declaration: Unknown stage %.", value);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool assets::glsl_parser::parse_components_declaration(
|
||||
const std::span<const std::string_view> values,
|
||||
shader_source_data& buffer
|
||||
) {
|
||||
return parse_component_tokens(values, buffer.geometry_type, buffer.components);
|
||||
}
|
||||
|
||||
bool assets::glsl_parser::parse_static_enable_declaration(
|
||||
const std::span<const std::string_view> values,
|
||||
shader_source_data& buffer
|
||||
) {
|
||||
return parse_component_tokens(values, buffer.geometry_type, buffer.static_enable);
|
||||
}
|
||||
|
||||
bool assets::glsl_parser::parse_dynamic_enable_declaration(
|
||||
const std::span<const std::string_view> values,
|
||||
shader_source_data& buffer
|
||||
) {
|
||||
return parse_component_tokens(values, buffer.geometry_type, buffer.dynamic_enable);
|
||||
}
|
||||
|
||||
bool assets::glsl_parser::parse_component_tokens(
|
||||
const std::span<const std::string_view> values,
|
||||
const model_geometry::types geometry_type,
|
||||
shader_components::flags& components
|
||||
) {
|
||||
components = {};
|
||||
|
||||
const ztu::string_lookup<shader_components::flags>* component_lookup{};
|
||||
|
||||
switch (geometry_type)
|
||||
{
|
||||
case model_geometry::types::mesh:
|
||||
component_lookup = &language::mesh_feature_lookup;
|
||||
break;
|
||||
case model_geometry::types::point_cloud:
|
||||
component_lookup = &language::point_cloud_feature_lookup;
|
||||
break;
|
||||
default:
|
||||
ztu::logger::warn("Internal error: Unknown geometry index %.", static_cast<int>(geometry_type));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
for (const auto value : values)
|
||||
{
|
||||
if (const auto it = component_lookup->find(value); it != component_lookup->end())
|
||||
{
|
||||
components |= it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::warn("Ignoring unknown feature token %.", value);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -1,301 +0,0 @@
|
||||
#include "../../../include/assets/data_parsers"
|
||||
|
||||
#include "glm/glm.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <fstream>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
#include "assets/components/point_cloud_vertex_components.hpp"
|
||||
#include "util/binary_ifstream.hpp"
|
||||
#include "util/logger.hpp"
|
||||
|
||||
|
||||
ztu::result<std::string_view> kitti_loader::parent_directory(const std::string_view path)
|
||||
{
|
||||
const auto sep_index = path.rfind(std::filesystem::path::preferred_separator);
|
||||
|
||||
if (sep_index == std::string_view::npos)
|
||||
{
|
||||
return std::unexpected(std::make_error_code(std::errc::no_such_file_or_directory));
|
||||
}
|
||||
|
||||
return path.substr(0, sep_index);
|
||||
};
|
||||
|
||||
std::error_code kitti_loader::prefetch(
|
||||
const file_dir_list& paths,
|
||||
prefetch_queue& queue
|
||||
) {
|
||||
|
||||
// Directories can simply be passed on
|
||||
queue.kitti_pose_queue.directories.push_back(paths.directories);
|
||||
|
||||
// For files, we just forward the files directory
|
||||
for (const auto file : queue.kitti_pose_queue.files)
|
||||
{
|
||||
if (const auto base_directory = parent_directory(file).and_then(parent_directory))
|
||||
{
|
||||
queue.kitti_pose_queue.directories.push_back(*base_directory);
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO remove from list
|
||||
ztu::logger::error("Malformed kitti file path: %.", file);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::error_code kitti_loader::load(
|
||||
dynamic_point_cloud_buffer& buffer,
|
||||
const file_dir_list& paths,
|
||||
prefetch_lookup& id_lookup,
|
||||
dynamic_shader_source_store& store,
|
||||
bool
|
||||
) {
|
||||
namespace fs = std::filesystem;
|
||||
std::error_code error;
|
||||
|
||||
std::vector<dynamic_pose_store::iterator_type> pose_its;
|
||||
pose_its.reserve(paths.files.size());
|
||||
|
||||
auto processed_filenames = ztu::string_list{};
|
||||
|
||||
auto path_buffer = fs::path{};
|
||||
|
||||
const auto preprocess_filename = [&](
|
||||
std::string_view path,
|
||||
const auto& directory,
|
||||
std::string_view filename,
|
||||
const pose_prefetch_lookup::directory_iterator& dir_it
|
||||
) {
|
||||
const auto pose_index = frame_id_from_filename(filename);
|
||||
if (not pose_index) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("Could not parse frame id from kitti file path: %.", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto [ index_it, pose_id_match ] = id_lookup.poses.find_index(dir_it, *pose_index);
|
||||
|
||||
if (not pose_id_match) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("No matching pose index (%) found in directory (%).", directory, *pose_index);
|
||||
return;
|
||||
}
|
||||
|
||||
const auto [ pose_it, pose_match ] = store.poses.find(pose_id_match);
|
||||
if (not pose_id_match) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("No matching pose found for id: %.", pose_id_match);
|
||||
return;
|
||||
}
|
||||
|
||||
processed_filenames.push_back(path);
|
||||
pose_its.push_back(pose_it);
|
||||
};
|
||||
|
||||
for (const auto file : paths.files)
|
||||
{
|
||||
path_buffer.assign(file.begin(), file.end());
|
||||
|
||||
if (not fs::is_regular_file(path_buffer))
|
||||
{
|
||||
ztu::logger::error("Given kitti file does not exist: %.", path_buffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto sep_index = file.rfind(fs::path::preferred_separator);
|
||||
|
||||
if (sep_index == std::string_view::npos) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("Could not parse frame directory from kitti file path: %.", file);
|
||||
continue;
|
||||
}
|
||||
|
||||
// TODO find out if the path splitting is consistent
|
||||
const auto base_directory = parent_directory(file.substr(0, sep_index));
|
||||
if (not base_directory) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("Could not parse base directory from kitti file path: %.", file);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto [ dir_it, dir_match ] = id_lookup.poses.find_directory(*base_directory);
|
||||
if (not dir_match) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("No matching pose directory found for %.", file);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto filename = file.substr(sep_index + 1);
|
||||
|
||||
preprocess_filename(
|
||||
file,
|
||||
*base_directory,
|
||||
filename,
|
||||
dir_it
|
||||
);
|
||||
}
|
||||
|
||||
for (const auto directory : paths.directories)
|
||||
{
|
||||
path_buffer.assign(directory.begin(), directory.end());
|
||||
|
||||
const auto [ dir_it, dir_match ] = id_lookup.poses.find_directory(path_buffer);
|
||||
if (not dir_match) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("No matching pose directory found for %.", path_buffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
path_buffer /= frame_folder;
|
||||
|
||||
if (not fs::is_directory(path_buffer))
|
||||
{
|
||||
ztu::logger::error("Given kitti directory does not exist: %.", directory);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const auto& file : fs::directory_iterator{ path_buffer })
|
||||
{
|
||||
const auto file_path = std::string_view{ file.path().c_str() };
|
||||
|
||||
if (not file_path.ends_with(".bin"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
auto filename_begin = file_path.rfind(fs::path::preferred_separator);
|
||||
filename_begin = filename_begin == std::string_view::npos ? 0 : filename_begin + 1;
|
||||
|
||||
const auto filename_only = file_path.substr(filename_begin);
|
||||
|
||||
const auto pose_index = frame_id_from_filename(filename_only);
|
||||
if (not pose_index) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("Could not parse frame id from kitti filename: %.", filename_only);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto [ index_it, pose_id_match ] = id_lookup.poses.find_index(dir_it, *pose_index);
|
||||
|
||||
if (not pose_id_match) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("No matching pose index (%) found in directory (%).", directory, *pose_index);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto [ pose_it, pose_match ] = store.poses.find(pose_id_match);
|
||||
if (not pose_id_match) [[unlikely]]
|
||||
{
|
||||
ztu::logger::error("No matching pose found for id: %.", pose_id_match);
|
||||
continue;
|
||||
}
|
||||
|
||||
processed_filenames.push_back(file_path);
|
||||
pose_its.push_back(pose_it);
|
||||
|
||||
preprocess_filename(
|
||||
file_path,
|
||||
directory,
|
||||
filename_only,
|
||||
dir_it
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (const auto [ filename, pose_it ] : std::ranges::views::zip(processed_filenames, pose_its))
|
||||
{
|
||||
buffer.clear();
|
||||
|
||||
if ((error = load_point_file(filename, buffer)))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
transform_point_cloud(buffer.positions(), *pose_it);
|
||||
|
||||
store.point_clouds.add(buffer);
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void kitti_loader::transform_point_cloud(
|
||||
std::span<point_cloud_vertex_components::position> points,
|
||||
const glm::mat4& pose
|
||||
) {
|
||||
for (auto& [ x, y, z ] : points) {
|
||||
auto vec = glm::vec4{ x, y, z, 1.0f };
|
||||
vec = pose * vec;
|
||||
x = vec.x;
|
||||
y = vec.y;
|
||||
z = vec.z;
|
||||
}
|
||||
}
|
||||
|
||||
std::error_code kitti_loader::load_point_file(
|
||||
const std::filesystem::path& filename,
|
||||
dynamic_point_cloud_buffer& point_cloud
|
||||
) {
|
||||
|
||||
auto in = binary_ifstream{};
|
||||
|
||||
auto error = std::error_code{};
|
||||
|
||||
if ((error == in.open(filename, true)))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
const auto read_vector = [&in](auto& vector) -> std::error_code
|
||||
{
|
||||
for (auto& component : vector)
|
||||
{
|
||||
float component32;
|
||||
if (const auto e = in.read_ieee754<std::endian::little>(component32))
|
||||
{
|
||||
return e;
|
||||
}
|
||||
component = component32;
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
point_cloud_vertex_components::position position;
|
||||
|
||||
auto& positions = point_cloud.positions();
|
||||
|
||||
while (not ((error = read_vector(position)))) {
|
||||
positions.push_back(position);
|
||||
if ((error = in.skip<float>())) // TODO what am I skipping here?!?
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (static_cast<std::errc>(error.value()) != std::errc::result_out_of_range)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ztu::result<std::size_t> kitti_loader::frame_id_from_filename(
|
||||
std::string_view filename
|
||||
) {
|
||||
std::size_t id;
|
||||
const auto result = std::from_chars(filename.cbegin(), filename.cend(), id);
|
||||
|
||||
if (result.ec != std::errc{})
|
||||
{
|
||||
return std::unexpected(std::make_error_code(result.ec));
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
239
source/assets/file_parsers/kitti_parser.cpp
Normal file
239
source/assets/file_parsers/kitti_parser.cpp
Normal file
@@ -0,0 +1,239 @@
|
||||
#include "assets/file_parsers/kitti_parser.hpp"
|
||||
|
||||
#include "glm/glm.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <fstream>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
#include "assets/components/point_cloud_vertex_components.hpp"
|
||||
#include "assets/data/pose_data.hpp"
|
||||
#include "util/binary_ifstream.hpp"
|
||||
#include "util/logger.hpp"
|
||||
#include <execution>
|
||||
|
||||
|
||||
assets::kitti_parser::parser_context::parser_context(
|
||||
const pose_list_id_lookup& pose_list_lookup,
|
||||
const pose_list_store& pose_list_store,
|
||||
store_type& m_store,
|
||||
std::mutex& m_store_mutex
|
||||
) :
|
||||
m_pose_list_lookup{ &pose_list_lookup },
|
||||
m_pose_list_store{ &pose_list_store },
|
||||
m_store{ &m_store },
|
||||
m_store_mutex{ &m_store_mutex }
|
||||
{
|
||||
m_buffer.positions().reserve(8192);
|
||||
m_buffer.normals().reserve(8192);
|
||||
m_buffer.colors().reserve(8192);
|
||||
}
|
||||
|
||||
void assets::kitti_parser::parser_context::reset()
|
||||
{
|
||||
m_buffer.clear();
|
||||
}
|
||||
|
||||
void assets::kitti_parser::parser_context::operator()(lookup_type::const_pointer entry) noexcept
|
||||
{
|
||||
const auto& [ filename, id ] = *entry;
|
||||
|
||||
pose_data pose;
|
||||
|
||||
if (const auto pose_path = get_pose_path(filename); not pose_path)
|
||||
{
|
||||
if (pose_path != m_last_pose_path)
|
||||
{
|
||||
if (const auto pose_list_id_it = m_pose_list_lookup->find(*pose_path); pose_list_id_it != m_pose_list_lookup->end())
|
||||
{
|
||||
m_last_pose_path = *pose_path;
|
||||
const auto pose_list_id = pose_list_id_it->second;
|
||||
|
||||
if (const auto [ pose_list_it, found ] = m_pose_list_store->find(pose_list_id); found)
|
||||
{
|
||||
m_last_pose_path = *pose_path;
|
||||
m_last_pose_list = pose_list_it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::error("No matching pose found in store for %.", *pose_path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::error("No matching pose registered in lookup %.", *pose_path);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::error("Malformed kitti file path %.", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
clear();
|
||||
|
||||
if (const auto e = load_point_file(filename, m_buffer))
|
||||
{
|
||||
ztu::logger::error("Could not load kitti file %: %", filename, e.message());
|
||||
return;
|
||||
}
|
||||
|
||||
transform_point_cloud(m_buffer.positions(), pose);
|
||||
|
||||
{
|
||||
auto lock = std::lock_guard{ *m_store_mutex };
|
||||
m_store->add(id, m_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
ztu::result<std::filesystem::path> assets::kitti_parser::parent_directory(
|
||||
const std::filesystem::path& path
|
||||
) {
|
||||
try
|
||||
{
|
||||
return path.parent_path();
|
||||
}
|
||||
catch (const std::exception&)
|
||||
{
|
||||
return std::unexpected(std::make_error_code(std::errc::no_such_file_or_directory));
|
||||
}
|
||||
};
|
||||
|
||||
ztu::result<std::filesystem::path> assets::kitti_parser::get_pose_path(
|
||||
const std::filesystem::path& path
|
||||
) {
|
||||
return parent_directory(path)
|
||||
.and_then(parent_directory)
|
||||
.and_then(
|
||||
[](const auto& base_dir)
|
||||
{
|
||||
return base_dir / "pose.txt";
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
std::error_code assets::kitti_parser::prefetch(
|
||||
path_id_lookups& lookups
|
||||
) {
|
||||
m_path_buffer.clear();
|
||||
lookups.point_clouds.by_extension(".bin", m_path_buffer);
|
||||
|
||||
for (const auto entry : m_path_buffer)
|
||||
{
|
||||
|
||||
if (const auto pose_path = pose_path(entry->second))
|
||||
{
|
||||
lookups.poses.try_emplace(std::filesystem::absolute(pose_path));
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::error("Malformed kitti file path %.", entry->second);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::error_code assets::kitti_parser::load(
|
||||
path_id_lookups& lookups,
|
||||
data_stores& stores,
|
||||
bool pedantic
|
||||
) {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
m_path_buffer.clear();
|
||||
lookups.point_clouds.by_extension(".bin", m_path_buffer);
|
||||
|
||||
auto store_mutex = std::mutex{};
|
||||
|
||||
std::for_each(
|
||||
std::execution::parallel_unsequenced_policy{},
|
||||
m_path_buffer.begin(),
|
||||
m_path_buffer.end(),
|
||||
parser_context{
|
||||
lookups.pose_lists,
|
||||
stores.pose_lists,
|
||||
stores.point_clouds,
|
||||
store_mutex
|
||||
}
|
||||
);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void assets::kitti_parser::transform_point_cloud(
|
||||
std::span<point_cloud_vertex_components::position> points,
|
||||
const glm::mat4& pose
|
||||
) {
|
||||
for (auto& point : points)
|
||||
{
|
||||
point = pose * glm::vec4{ point, 1.0f };
|
||||
}
|
||||
}
|
||||
|
||||
std::error_code assets::kitti_parser::load_point_file(
|
||||
const std::filesystem::path& filename,
|
||||
point_cloud_data& point_cloud
|
||||
) {
|
||||
|
||||
auto in = binary_ifstream{};
|
||||
|
||||
if (const auto e = in.open(filename, true))
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
const auto read_vector = [&in](auto& vector) -> std::error_code
|
||||
{
|
||||
for (auto& component : vector)
|
||||
{
|
||||
float component32;
|
||||
if (const auto e = in.read_ieee754<std::endian::little>(component32))
|
||||
{
|
||||
return e;
|
||||
}
|
||||
component = component32;
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
point_cloud_vertex_components::position position;
|
||||
|
||||
auto& positions = point_cloud.positions();
|
||||
|
||||
auto parsing_error = std::error_code{};
|
||||
|
||||
while (not ((parsing_error = read_vector(position))))
|
||||
{
|
||||
positions.push_back(position);
|
||||
|
||||
if ((parsing_error = in.skip<float>())) // TODO what am I skipping here?!?
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (static_cast<std::errc>(parsing_error.value()) != std::errc::result_out_of_range)
|
||||
{
|
||||
return parsing_error;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ztu::result<std::size_t> assets::kitti_parser::frame_id_from_filename(
|
||||
const std::string_view filename
|
||||
) {
|
||||
std::size_t id;
|
||||
const auto result = std::from_chars(filename.cbegin(), filename.cend(), id);
|
||||
|
||||
if (result.ec != std::errc{})
|
||||
{
|
||||
return std::unexpected(std::make_error_code(result.ec));
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
#include "../../../include/assets/data_parsers"
|
||||
|
||||
#include "../../../include/assets/read_buffers"
|
||||
#include <fstream>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include "util/logger.hpp"
|
||||
|
||||
inline std::error_code kitti_pose_loader::parse_pose(
|
||||
std::ifstream& in,
|
||||
dynamic_pose_buffer& pose
|
||||
) {
|
||||
for (dynamic_pose_buffer::length_type row{}; row != 3; ++row)
|
||||
{
|
||||
for (dynamic_pose_buffer::length_type col{}; col != 4; ++col)
|
||||
{
|
||||
if (not (in >> pose[row][col]))
|
||||
{
|
||||
return std::make_error_code(std::errc::result_out_of_range);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
std::error_code kitti_pose_loader::prefetch(
|
||||
const file_dir_list& paths,
|
||||
prefetch_queue& queue
|
||||
) {
|
||||
// Nothing to be done here
|
||||
}
|
||||
|
||||
std::error_code kitti_pose_loader::load(
|
||||
dynamic_pose_buffer& buffer,
|
||||
const file_dir_list& paths,
|
||||
prefetch_lookup& id_lookup,
|
||||
dynamic_shader_source_store& store,
|
||||
bool pedantic
|
||||
) {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
auto path_buffer = fs::path{};
|
||||
auto in = std::ifstream{}; // TODO disable exceptions (for other loaders as well)
|
||||
|
||||
// Needed to initialize 4th row and col
|
||||
buffer = glm::identity<glm::mat4>();
|
||||
|
||||
auto processed_filenames = ztu::string_list{};
|
||||
|
||||
processed_filenames.reserve(
|
||||
paths.files.character_count() +
|
||||
paths.directories.character_count() +
|
||||
paths.directories.size() * pose_filename.size(),
|
||||
paths.files.size() + paths.directories.size()
|
||||
);
|
||||
|
||||
const auto preprocess_file = [&]()
|
||||
{
|
||||
if (not fs::is_regular_file(path_buffer))
|
||||
{
|
||||
ztu::logger::error("Kitti pose file does not exist: %", path_buffer);
|
||||
return;
|
||||
}
|
||||
processed_filenames.push_back(path_buffer.c_str());
|
||||
};
|
||||
|
||||
for (const auto directory : paths.directories) {
|
||||
path_buffer.assign(directory.begin(), directory.end());
|
||||
path_buffer /= pose_filename;
|
||||
preprocess_file();
|
||||
}
|
||||
|
||||
for (const auto file : paths.files) {
|
||||
path_buffer.assign(file.begin(), file.end());
|
||||
preprocess_file();
|
||||
}
|
||||
|
||||
for (const auto filename : processed_filenames)
|
||||
{
|
||||
// TODO if (not) removing the path separator creates issues.
|
||||
const auto directory = filename.substr(0, filename.length() - pose_filename.length());
|
||||
|
||||
auto [ dir_it, dir_match ] = id_lookup.poses.find_directory(directory);
|
||||
|
||||
if (not dir_match) [[unlikely]]
|
||||
{
|
||||
dir_it = id_lookup.poses.emplace_dir(dir_it, directory);
|
||||
}
|
||||
|
||||
in.open(filename.data()); // Safe because string list adds null terminator
|
||||
if (not in.is_open())
|
||||
{
|
||||
ztu::logger::error("Cannot open kitti pose file %", path_buffer);
|
||||
continue;
|
||||
}
|
||||
|
||||
in >> std::skipws;
|
||||
|
||||
for (auto i = pose_prefetch_lookup::index_type{}; in.peek() != std::ifstream::traits_type::eof(); ++i)
|
||||
{
|
||||
if (const auto error = parse_pose(in, buffer))
|
||||
{
|
||||
ztu::logger::error(
|
||||
"Error occurred while parsing kitti pose % in file %: [%] %",
|
||||
i,
|
||||
path_buffer,
|
||||
error.category().name(),
|
||||
error.message()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto id = store.poses.add(buffer);
|
||||
id_lookup.poses.emplace_hint_dir(dir_it, i, id);
|
||||
}
|
||||
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
125
source/assets/file_parsers/kitti_pose_parser.cpp
Normal file
125
source/assets/file_parsers/kitti_pose_parser.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "assets/file_parsers/kitti_pose_parser.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <execution>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include "util/logger.hpp"
|
||||
|
||||
|
||||
assets::kitti_pose_parser::parser_context::parser_context(
|
||||
store_type& m_store,
|
||||
std::mutex& m_store_mutex
|
||||
) : m_store{ &m_store }, m_store_mutex{ &m_store_mutex }
|
||||
{
|
||||
m_buffer.reserve(128);
|
||||
}
|
||||
|
||||
void assets::kitti_pose_parser::parser_context::reset()
|
||||
{
|
||||
m_buffer.clear();
|
||||
}
|
||||
|
||||
void assets::kitti_pose_parser::parser_context::operator()(lookup_type::const_pointer entry) noexcept
|
||||
{
|
||||
const auto& [ filename, id ] = *entry;
|
||||
|
||||
if (const auto e = parse_file(filename, m_buffer))
|
||||
{
|
||||
ztu::logger::warn("Could not load pose_list %: %.", filename, e.message());
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
auto lock = std::lock_guard{ *m_store_mutex };
|
||||
m_store->add(id, m_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::error_code assets::kitti_pose_parser::prefetch(
|
||||
path_id_lookups& lookups
|
||||
) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::error_code assets::kitti_pose_parser::parse(
|
||||
const path_id_lookups& lookups,
|
||||
store_type& store,
|
||||
bool
|
||||
) {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
auto path_buffer = fs::path{};
|
||||
auto in = std::ifstream{}; // TODO disable exceptions (for other loaders as well)
|
||||
|
||||
m_path_buffer.clear();
|
||||
lookups.pose_lists.by_extension(".glsl", m_path_buffer);
|
||||
|
||||
auto store_mutex = std::mutex{};
|
||||
|
||||
std::for_each(
|
||||
std::execution::parallel_unsequenced_policy{},
|
||||
m_path_buffer.begin(),
|
||||
m_path_buffer.end(),
|
||||
parser_context{ store, store_mutex }
|
||||
);
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
std::error_code assets::kitti_pose_parser::parse_file(
|
||||
const std::filesystem::path& filename,
|
||||
data_type &poses
|
||||
) {
|
||||
|
||||
auto in = std::ifstream{};
|
||||
|
||||
in.open(filename);
|
||||
if (not in.is_open())
|
||||
{
|
||||
return std::make_error_code(std::errc::no_such_file_or_directory);
|
||||
}
|
||||
|
||||
in >> std::skipws;
|
||||
|
||||
while (in.peek() != std::ifstream::traits_type::eof())
|
||||
{
|
||||
auto pose = glm::identity<pose_data>();
|
||||
|
||||
if (const auto error = parse_pose(in, pose))
|
||||
{
|
||||
ztu::logger::error(
|
||||
"Error occurred while parsing kitti pose file %: [%] %",
|
||||
filename,
|
||||
error.category().name(),
|
||||
error.message()
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
poses.emplace_back(pose);
|
||||
}
|
||||
|
||||
in.close();
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
std::error_code assets::kitti_pose_parser::parse_pose(
|
||||
std::ifstream& in,
|
||||
pose_data& pose
|
||||
) {
|
||||
for (pose_data::length_type row{}; row != 3; ++row)
|
||||
{
|
||||
for (pose_data::length_type col{}; col != 4; ++col)
|
||||
{
|
||||
if (not (in >> pose[row][col]))
|
||||
{
|
||||
return std::make_error_code(std::errc::result_out_of_range);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../../../include/assets/data_parsers"
|
||||
#include "assets/file_parsers/mtl_loader.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <fstream>
|
||||
@@ -435,15 +435,14 @@ std::error_code mtl_loader::load(
|
||||
}),
|
||||
ztu::make_line_parser("Ka ", ztu::is_not_repeating, [&](const auto& param)
|
||||
{
|
||||
if (surface_properties_disabled) return codes::ok;
|
||||
|
||||
auto& properties = material.initialized_surface_properties();
|
||||
if (parse_numeric_vector(param, properties.ambient_filter) != std::errc{}) [[unlikely]]
|
||||
material_components::ambient_filter ambient_filter;
|
||||
if (parse_numeric_vector(param, ambient_filter) != std::errc{}) [[unlikely]]
|
||||
{
|
||||
return codes::malformed_ambient_color;
|
||||
return codes::malformed_ambient_color; // TODO rename to filter
|
||||
}
|
||||
|
||||
material.components() |= flags::surface_properties;
|
||||
material.ambient_filter = mbient_filter;
|
||||
material.components() |= flags::ambient_filter;
|
||||
|
||||
return codes::ok;
|
||||
}),
|
||||
@@ -511,7 +510,7 @@ std::error_code mtl_loader::load(
|
||||
if (textures_disabled) return codes::ok;
|
||||
|
||||
load_texture(param, "ambient color", [&](const auto id) {
|
||||
material.ambient_color_texture_id() = id;
|
||||
material.specular_filter_texture_id() = id;
|
||||
material.components() |= flags::ambient_filter_texture;
|
||||
});
|
||||
|
||||
@@ -522,7 +521,7 @@ std::error_code mtl_loader::load(
|
||||
if (textures_disabled) return codes::ok;
|
||||
|
||||
load_texture(param, "diffuse color", [&](const auto id) {
|
||||
material.diffuse_color_texture_id() = id;
|
||||
material.diffuse_filter_texture_id() = id;
|
||||
material.components() |= flags::diffuse_filter_texture;
|
||||
});
|
||||
|
||||
@@ -533,7 +532,7 @@ std::error_code mtl_loader::load(
|
||||
if (textures_disabled) return codes::ok;
|
||||
|
||||
load_texture(param, "specular color", [&](const auto id) {
|
||||
material.specular_color_texture_id() = id;
|
||||
material.specular_filter_texture_id() = id;
|
||||
material.components() |= flags::specular_filter_texture;
|
||||
});
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include "../../../include/assets/data_parsers"
|
||||
#include "assets/file_parsers/obj_loader.hpp"
|
||||
|
||||
#include <charconv>
|
||||
#include <fstream>
|
||||
#include <array>
|
||||
|
||||
#include "assets/components/mesh_vertex_components.hpp"
|
||||
#include "../../../include/assets/data_loaders"
|
||||
#include "assets/data_loaders/"
|
||||
|
||||
#include "util/logger.hpp"
|
||||
#include "util/for_each.hpp"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "../../../include/assets/data_parsers"
|
||||
#include "assets/file_parsers/stl_loader.hpp"
|
||||
|
||||
#include "util/binary_ifstream.hpp"
|
||||
#include "util/unroll_bool_template.hpp"
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
#include "../../../include/assets/data_parsers"
|
||||
#include "assets/file_parsers/threedtk_pose_loader.hpp"
|
||||
|
||||
|
||||
#include "../../../include/assets/read_buffers"
|
||||
|
||||
#include <fstream>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
#include <glm/gtx/euler_angles.hpp>
|
||||
|
||||
#include "util/logger.hpp"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
|
||||
|
||||
inline std::error_code threedtk_pose_loader::parse_transform_info(
|
||||
|
||||
@@ -13,9 +13,9 @@ struct prioritized_metadata_comparator
|
||||
|
||||
bool operator()(const type& a, const type& b) const noexcept
|
||||
{
|
||||
if (a.geometry != b.geometry)
|
||||
if (a.geometry_type != b.geometry_type)
|
||||
{
|
||||
return a.geometry > b.geometry;
|
||||
return a.geometry_type > b.geometry_type;
|
||||
}
|
||||
|
||||
if (a.stage != b.stage)
|
||||
@@ -48,24 +48,24 @@ std::optional<std::pair<zgl::shader_metadata, zgl::shader_handle>> zgl::shader_m
|
||||
) {
|
||||
auto shader_it = std::ranges::lower_bound(
|
||||
m_shader_lookup,
|
||||
std::pair{ requirements.geometry, requirements.stage },
|
||||
std::pair{ requirements.geometry_type, requirements.stage },
|
||||
std::greater{},
|
||||
[](const shader_lookup_entry_type& entry)
|
||||
[](const entry_type& entry)
|
||||
{
|
||||
const auto& meta = entry.first;
|
||||
return std::pair{ meta.geometry, meta.stage };
|
||||
return std::pair{ meta.geometry_type, meta.stage };
|
||||
}
|
||||
);
|
||||
|
||||
while (
|
||||
shader_it != m_shader_lookup.end() and
|
||||
shader_it->first.geometry == requirements.geometry and
|
||||
shader_it->first.geometry_type == requirements.geometry_type and
|
||||
shader_it->first.stage == requirements.stage
|
||||
) {
|
||||
const auto& [ meta, data ] = *shader_it;
|
||||
|
||||
const auto unwanted_static_features = meta.static_enabled & ~requirements.features;
|
||||
const auto required_dynamic_features = requirements.features & ~meta.static_enabled;
|
||||
const auto unwanted_static_features = meta.static_enabled & ~requirements.components;
|
||||
const auto required_dynamic_features = requirements.components & ~meta.static_enabled;
|
||||
const auto missing_dynamic_features = required_dynamic_features & ~meta.dynamic_enable;
|
||||
|
||||
if (unwanted_static_features == 0 and missing_dynamic_features == 0)
|
||||
@@ -84,9 +84,9 @@ void zgl::shader_manager::process(const store_type& shader_sources)
|
||||
m_preprocessor.process(shader_sources);
|
||||
}
|
||||
|
||||
void zgl::shader_manager::get_handles(
|
||||
void zgl::shader_manager::fetch(
|
||||
const assets::shader_source_store& shader_sources,
|
||||
std::span<const shading::shader_set_requirements> requirements,
|
||||
std::span<const shading::shader_program_requirements> requirements,
|
||||
std::span<shader_set_metadata> metadata,
|
||||
std::span<shader_handle_set> shader_sets
|
||||
) {
|
||||
@@ -99,14 +99,14 @@ void zgl::shader_manager::get_handles(
|
||||
shader_set_meta = {};
|
||||
|
||||
auto shader_req = shading::shader_requirements{
|
||||
.geometry = req.geometry,
|
||||
.geometry_type = req.geometry_type,
|
||||
.stage = {},
|
||||
.features = req.features
|
||||
.components = req.components
|
||||
};
|
||||
|
||||
for (auto [ stage_index, handle ] : std::ranges::views::enumerate(shader_set.stages))
|
||||
{
|
||||
shader_req.stage = static_cast<shading::stage::types>(stage_index);
|
||||
shader_req.stage = static_cast<assets::shader_components::stage>(stage_index);
|
||||
|
||||
if (auto shader_match = find_shader(shader_req))
|
||||
{
|
||||
@@ -127,7 +127,7 @@ void zgl::shader_manager::get_handles(
|
||||
m_preprocessed_shader_source_metadata_buffer.resize(m_source_requirement_buffer.size());
|
||||
m_source_strings_buffer.clear();
|
||||
|
||||
m_preprocessor.get_shader_sources(
|
||||
m_preprocessor.fetch(
|
||||
shader_sources,
|
||||
m_source_requirement_buffer,
|
||||
m_preprocessed_shader_source_metadata_buffer,
|
||||
@@ -161,7 +161,7 @@ void zgl::shader_manager::get_handles(
|
||||
)) {
|
||||
handle = shader.handle;
|
||||
auto shader_meta = shader_metadata{
|
||||
.geometry = source_req_it->geometry,
|
||||
.geometry_type = source_req_it->geometry_type,
|
||||
.stage = source_req_it->stage,
|
||||
.static_enabled = source_meta_it->static_enabled,
|
||||
.dynamic_enable = source_meta_it->dynamic_enable
|
||||
@@ -194,14 +194,14 @@ void zgl::shader_manager::get_handles(
|
||||
std::ranges::sort(
|
||||
new_shaders,
|
||||
prioritized_metadata_comparator{},
|
||||
&shader_lookup_entry_type::first
|
||||
&entry_type::first
|
||||
);
|
||||
|
||||
std::ranges::inplace_merge(
|
||||
m_shader_lookup,
|
||||
m_shader_lookup.begin() + prev_shader_count,
|
||||
prioritized_metadata_comparator{},
|
||||
&shader_lookup_entry_type::first
|
||||
&entry_type::first
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,12 @@ struct prioritized_metadata_comparator
|
||||
|
||||
bool operator()(const type& a, const type& b) const noexcept
|
||||
{
|
||||
if (a.geometry != b.geometry)
|
||||
if (a.geometry_type != b.geometry_type)
|
||||
{
|
||||
return a.geometry > b.geometry;
|
||||
return a.geometry_type > b.geometry_type;
|
||||
}
|
||||
|
||||
static constexpr auto more_features = std::popcount<zgl::shading::features::generic::type>;
|
||||
static constexpr auto more_features = std::popcount<assets::shader_components::stage>;
|
||||
|
||||
return std::ranges::lexicographical_compare(
|
||||
std::array{ a.dynamic_enable, a.static_enabled },
|
||||
@@ -29,7 +29,7 @@ void zgl::shader_program_manager::process(const store_type& shader_sources)
|
||||
m_shader_manager.process(shader_sources);
|
||||
}
|
||||
|
||||
void zgl::shader_program_manager::get_handles(
|
||||
void zgl::shader_program_manager::fetch(
|
||||
const assets::shader_source_store& shader_sources,
|
||||
std::span<const shading::shader_program_requirements> requirements,
|
||||
std::span<shader_program_metadata> metadata,
|
||||
@@ -55,8 +55,8 @@ void zgl::shader_program_manager::get_handles(
|
||||
program_meta = {};
|
||||
program_handle = {};
|
||||
m_shader_requirements_buffer.emplace_back(
|
||||
req.geometry,
|
||||
req.features
|
||||
req.geometry_type,
|
||||
req.components
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ void zgl::shader_program_manager::get_handles(
|
||||
m_shader_metadata_buffer.resize(m_shader_requirements_buffer.size());
|
||||
shader_set_buffer.clear();
|
||||
|
||||
m_shader_manager.get_handles(
|
||||
m_shader_manager.fetch(
|
||||
shader_sources,
|
||||
m_shader_requirements_buffer,
|
||||
m_shader_metadata_buffer,
|
||||
@@ -92,7 +92,7 @@ void zgl::shader_program_manager::get_handles(
|
||||
program_handle = program.handle;
|
||||
|
||||
program_meta = shader_program_metadata{
|
||||
.geometry = shader_set_req_it->geometry,
|
||||
.geometry_type = shader_set_req_it->geometry_type,
|
||||
.static_enabled = shader_set_meta_it->static_enabled,
|
||||
.dynamic_enable = shader_set_meta_it->dynamic_enable
|
||||
};
|
||||
@@ -113,14 +113,14 @@ void zgl::shader_program_manager::get_handles(
|
||||
std::ranges::sort(
|
||||
new_shader_programs,
|
||||
prioritized_metadata_comparator{},
|
||||
&shader_program_lookup_entry_type::first
|
||||
&entry_type::first
|
||||
);
|
||||
|
||||
std::ranges::inplace_merge(
|
||||
m_shader_program_lookup,
|
||||
m_shader_program_lookup.begin() + prev_shader_program_count,
|
||||
prioritized_metadata_comparator{},
|
||||
&shader_program_lookup_entry_type::first
|
||||
&entry_type::first
|
||||
);
|
||||
}
|
||||
|
||||
@@ -130,23 +130,23 @@ std::optional<std::pair<zgl::shader_program_metadata, zgl::shader_program_handle
|
||||
|
||||
auto shader_program_it = std::ranges::lower_bound(
|
||||
m_shader_program_lookup,
|
||||
requirements.geometry,
|
||||
requirements.geometry_type,
|
||||
std::greater{},
|
||||
[](const shader_program_lookup_entry_type& entry)
|
||||
[](const entry_type& entry)
|
||||
{
|
||||
const auto& meta = entry.first;
|
||||
return meta.geometry;
|
||||
return meta.geometry_type;
|
||||
}
|
||||
);
|
||||
|
||||
while (
|
||||
shader_program_it != m_shader_program_lookup.end() and
|
||||
shader_program_it->first.geometry == requirements.geometry
|
||||
shader_program_it->first.geometry_type == requirements.geometry_type
|
||||
) {
|
||||
const auto& [ meta, data ] = *shader_program_it;
|
||||
|
||||
const auto unwanted_static_features = meta.static_enabled & ~requirements.features;
|
||||
const auto required_dynamic_features = requirements.features & ~meta.static_enabled;
|
||||
const auto unwanted_static_features = meta.static_enabled & ~requirements.components;
|
||||
const auto required_dynamic_features = requirements.components & ~meta.static_enabled;
|
||||
const auto missing_dynamic_features = required_dynamic_features & ~meta.dynamic_enable;
|
||||
|
||||
if (unwanted_static_features == 0 and missing_dynamic_features == 0)
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
|
||||
#include "opengl/shading/shader_metadata_language.hpp"
|
||||
|
||||
static auto mesh_feature_defines = std::array{
|
||||
static auto mesh_component_defines = std::array{
|
||||
"#define FACE\n",
|
||||
"#define LINE\n",
|
||||
"#define POINT\n",
|
||||
@@ -19,7 +20,7 @@ static auto mesh_feature_defines = std::array{
|
||||
"#define U_RGBA\n",
|
||||
};
|
||||
|
||||
static auto point_cloud_feature_defines = std::array{
|
||||
static auto point_cloud_component_defines = std::array{
|
||||
"#define SQUARE\n",
|
||||
"#define LIGHTING\n",
|
||||
"#define V_L\n",
|
||||
@@ -29,34 +30,15 @@ static auto point_cloud_feature_defines = std::array{
|
||||
"#define RAINBOW\n"
|
||||
};
|
||||
|
||||
|
||||
struct prioritized_feature_set_comparator
|
||||
{
|
||||
using type = zgl::shader_features_set<zgl::shading::features::generic::type>;
|
||||
|
||||
bool operator()(const type& a, const type& b) const noexcept
|
||||
{
|
||||
static constexpr auto more_features = std::popcount<zgl::shading::features::generic::type>;
|
||||
|
||||
return std::ranges::lexicographical_compare(
|
||||
std::array{ a.dynamic_enable, a.features, a.static_enable },
|
||||
std::array{ b.dynamic_enable, b.features, b.static_enable },
|
||||
std::greater{},
|
||||
more_features,
|
||||
more_features
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
struct prioritized_metadata_comparator
|
||||
{
|
||||
using type = zgl::shader_source_metadata;
|
||||
|
||||
bool operator()(const type& a, const type& b) const noexcept
|
||||
{
|
||||
if (a.geometry != b.geometry)
|
||||
if (a.geometry_type != b.geometry_type)
|
||||
{
|
||||
return a.geometry > b.geometry;
|
||||
return a.geometry_type > b.geometry_type;
|
||||
}
|
||||
|
||||
if (a.stage != b.stage)
|
||||
@@ -64,62 +46,56 @@ struct prioritized_metadata_comparator
|
||||
return a.stage > b.stage;
|
||||
}
|
||||
|
||||
const auto features_a = a.generic_feature_set();
|
||||
const auto features_b = b.generic_feature_set();
|
||||
static constexpr auto more_components = std::popcount<assets::shader_components::flags>;
|
||||
|
||||
return feature_set_comparator(features_a, features_b);
|
||||
// Sort by dynamic components first to make sure when compatible components are found
|
||||
// the compiled shader will have maximum dynamic compatibility.
|
||||
|
||||
return std::ranges::lexicographical_compare(
|
||||
std::array{ a.dynamic_enable, a.components, a.static_enable },
|
||||
std::array{ b.dynamic_enable, b.components, b.static_enable },
|
||||
std::greater{},
|
||||
more_components,
|
||||
more_components
|
||||
);
|
||||
}
|
||||
|
||||
private:
|
||||
prioritized_feature_set_comparator feature_set_comparator{};
|
||||
};
|
||||
|
||||
|
||||
|
||||
void zgl::shader_source_manager::process(const store_type& shader_sources)
|
||||
{
|
||||
namespace language = shading::shader_metadata_language;
|
||||
|
||||
for (const auto& [ id, shader_source ] : shader_sources)
|
||||
{
|
||||
m_value_token_buffer.clear();
|
||||
m_declaration_token_count_buffer.clear();
|
||||
std::ranges::fill(
|
||||
m_declaration_type_index_buffer,
|
||||
static_cast<std::size_t>(language::declaration_type::invalid)
|
||||
);
|
||||
|
||||
tokenize_declarations(shader_source);
|
||||
|
||||
const auto metadata = parse_metadata_from_tokens();
|
||||
|
||||
if (not metadata)
|
||||
{
|
||||
ztu::logger::warn("Ignoring shader % as it contains malformed metadata.", id);
|
||||
continue;
|
||||
}
|
||||
const auto meta = metadata_type{
|
||||
.stage = shader_source.stage,
|
||||
.components = shader_source.components,
|
||||
.static_enable = shader_source.static_enable,
|
||||
.dynamic_enable = shader_source.dynamic_enable
|
||||
};
|
||||
|
||||
// Sorted insert should be faster than std::sort and std::unique
|
||||
// for small numbers of elements and high numbers of duplicates.
|
||||
const auto it = std::ranges::upper_bound(
|
||||
m_shader_source_lookup,
|
||||
*metadata,
|
||||
meta,
|
||||
prioritized_metadata_comparator{},
|
||||
&std::pair<shader_source_metadata, dynamic_shader_source_store::id_type>::first
|
||||
&entry_type::first
|
||||
);
|
||||
|
||||
if (it != m_shader_source_lookup.end() and it->first == *metadata)
|
||||
if (it != m_shader_source_lookup.end() and it->first == meta)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
m_shader_source_lookup.emplace(it, *metadata, id);
|
||||
m_shader_source_lookup.emplace(it, meta, id);
|
||||
}
|
||||
}
|
||||
|
||||
void zgl::shader_source_manager::get_shader_sources(
|
||||
void zgl::shader_source_manager::fetch(
|
||||
const assets::shader_source_store& shader_sources,
|
||||
std::span<const shading::shader_source_requirements> requirements,
|
||||
std::span<const shading::shader_requirements> requirements,
|
||||
std::span<preprocessed_shader_source_metadata> metadata,
|
||||
std::vector<const char*>& shader_strings
|
||||
) {
|
||||
@@ -127,8 +103,8 @@ void zgl::shader_source_manager::get_shader_sources(
|
||||
assert(requirements.size() == metadata.size());
|
||||
|
||||
static constexpr auto max_shader_strings = std::max(
|
||||
mesh_feature_defines.size(),
|
||||
point_cloud_feature_defines.size()
|
||||
mesh_component_defines.size(),
|
||||
point_cloud_component_defines.size()
|
||||
) + 1;
|
||||
|
||||
shader_strings.reserve(max_shader_strings);
|
||||
@@ -136,43 +112,42 @@ void zgl::shader_source_manager::get_shader_sources(
|
||||
std::ranges::transform(
|
||||
requirements,
|
||||
metadata.begin(),
|
||||
[&](const shading::shader_source_requirements& req)
|
||||
[&](const shading::shader_requirements& req)
|
||||
{
|
||||
auto res = preprocessed_shader_source_metadata{};
|
||||
|
||||
auto source_it = std::ranges::lower_bound(
|
||||
m_shader_source_lookup,
|
||||
std::pair{ req.geometry, req.stage },
|
||||
std::pair{ req.geometry_type, req.stage },
|
||||
std::greater{},
|
||||
[](const source_lookup_entry_type& entry)
|
||||
[](const entry_type& entry)
|
||||
{
|
||||
const auto& meta = entry.first;
|
||||
return std::pair{ meta.geometry, meta.stage };
|
||||
return std::pair{ meta.geometry_type, meta.stage };
|
||||
}
|
||||
);
|
||||
|
||||
assets::shader_source_store::id_type source_id{};
|
||||
shading::features::generic::type to_be_enabled{};
|
||||
assets::shader_components::flags to_be_enabled{};
|
||||
|
||||
while (
|
||||
source_it != m_shader_source_lookup.end() and
|
||||
source_it->first.geometry == req.geometry and
|
||||
source_it->first.geometry == req.geometry_type and
|
||||
source_it->first.stage == req.stage
|
||||
) {
|
||||
const auto& [ meta, id ] = *source_it;
|
||||
const auto& [ features, static_enable, dynamic_enable ] = meta.generic_feature_set();
|
||||
|
||||
const auto missing_features = req.features & ~features;
|
||||
const auto unwanted_features = ~req.features & features;
|
||||
const auto fixed_unwanted_features = unwanted_features & ~static_enable & ~dynamic_enable;
|
||||
const auto missing_components = req.components & ~meta.components;
|
||||
const auto unwanted_components = ~req.components & meta.components;
|
||||
const auto fixed_unwanted_components = unwanted_components & ~meta.static_enable & ~meta.dynamic_enable;
|
||||
|
||||
if (missing_features == 0 and fixed_unwanted_features == 0)
|
||||
if (missing_components == 0 and fixed_unwanted_components == 0)
|
||||
{
|
||||
to_be_enabled = req.features & static_enable;
|
||||
to_be_enabled = req.components & meta.static_enable;
|
||||
source_id = id;
|
||||
|
||||
res.static_enabled = features & ~dynamic_enable & ~unwanted_features;
|
||||
res.dynamic_enable = dynamic_enable;
|
||||
res.static_enabled = meta.commponents & ~meta.dynamic_enable & ~unwanted_components;
|
||||
res.dynamic_enable = meta.dynamic_enable;
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -186,7 +161,7 @@ void zgl::shader_source_manager::get_shader_sources(
|
||||
if (source_found)
|
||||
{
|
||||
get_define_strings(
|
||||
req.geometry,
|
||||
req.geometry_type,
|
||||
to_be_enabled,
|
||||
res.string_count,
|
||||
shader_strings
|
||||
@@ -200,276 +175,34 @@ void zgl::shader_source_manager::get_shader_sources(
|
||||
}
|
||||
|
||||
void zgl::shader_source_manager::get_define_strings(
|
||||
const shading::model_geometry::types geometry,
|
||||
shading::features::generic::type features,
|
||||
shading::features::generic::type& feature_count,
|
||||
const assets::model_geometry::types geometry_type,
|
||||
assets::shader_components::flags components,
|
||||
assets::shader_components::flags& component_count,
|
||||
std::vector<const char*>& defines
|
||||
) {
|
||||
std::span<const char*> all_defines;
|
||||
|
||||
switch (geometry)
|
||||
switch (geometry_type)
|
||||
{
|
||||
case shading::model_geometry::types::mesh:
|
||||
all_defines = mesh_feature_defines;
|
||||
all_defines = mesh_component_defines;
|
||||
break;
|
||||
case shading::model_geometry::types::point_cloud:
|
||||
all_defines = point_cloud_feature_defines;
|
||||
all_defines = point_cloud_component_defines;
|
||||
break;
|
||||
default:
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
auto index = std::size_t{};
|
||||
while (features != 0)
|
||||
while (components != 0)
|
||||
{
|
||||
if ((features & 1) != 0)
|
||||
if ((components & 1) != 0)
|
||||
{
|
||||
defines.push_back(all_defines[index]);
|
||||
++feature_count;
|
||||
++component_count;
|
||||
}
|
||||
features >>= 1;
|
||||
components >>= 1;
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void zgl::shader_source_manager::tokenize_declarations(
|
||||
std::string_view source_rest
|
||||
) {
|
||||
namespace language = shading::shader_metadata_language;
|
||||
|
||||
auto offset = std::string_view::size_type{};
|
||||
|
||||
auto keyword = language::declaration_prefix;
|
||||
|
||||
while ((offset = source_rest.find(keyword)) != std::string_view::npos)
|
||||
{
|
||||
const auto current_token_count = m_value_token_buffer.size();
|
||||
|
||||
auto line_end = source_rest.find('\n', offset);
|
||||
if (line_end == std::string_view::npos)
|
||||
{
|
||||
line_end = source_rest.length();
|
||||
}
|
||||
|
||||
auto declaration = source_rest.substr(offset, line_end - offset);
|
||||
|
||||
if ((offset = declaration.find(language::title_separator)) == std::string_view::npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto title = declaration.substr(0, offset);
|
||||
if (const auto it = language::declaration_lookup.find(title); it != language::declaration_lookup.end())
|
||||
{
|
||||
const auto declaration_type = static_cast<std::size_t>(it->second);
|
||||
m_declaration_type_index_buffer[declaration_type] = m_declaration_token_count_buffer.size();
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
declaration = declaration.substr(offset);
|
||||
|
||||
if (not declaration.empty() and declaration.front() == language::value_separator)
|
||||
{
|
||||
declaration = declaration.substr(sizeof(language::value_separator), declaration.length());
|
||||
}
|
||||
|
||||
while ((offset = declaration.find(language::value_separator)) != std::string_view::npos)
|
||||
{
|
||||
m_value_token_buffer.emplace_back(declaration.substr(0, offset));
|
||||
declaration = declaration.substr(offset + sizeof(language::value_separator));
|
||||
}
|
||||
|
||||
if (not declaration.empty())
|
||||
{
|
||||
m_value_token_buffer.emplace_back(declaration);
|
||||
}
|
||||
|
||||
m_declaration_token_count_buffer.emplace_back(
|
||||
m_value_token_buffer.size() - current_token_count
|
||||
);
|
||||
source_rest = source_rest.substr(line_end + sizeof('\n'));
|
||||
keyword = language::declaration_prefix.substr(sizeof('\n'));
|
||||
}
|
||||
}
|
||||
|
||||
bool zgl::shader_source_manager::parse_stage_declaration(
|
||||
std::span<const std::string_view> values,
|
||||
shader_source_metadata& metadata
|
||||
) {
|
||||
namespace language = shading::shader_metadata_language;
|
||||
|
||||
if (values.size() != 1)
|
||||
{
|
||||
ztu::logger::warn("Invalid stage declaration: Expected exactly one token but got %.", values.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto value = values.front();
|
||||
|
||||
if (const auto it = language::stage_lookup.find(value); it != language::stage_lookup.end())
|
||||
{
|
||||
metadata.stage = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::warn("Invalid stage declaration: Unknown stage %.", value);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool zgl::shader_source_manager::parse_geometry_declaration(
|
||||
std::span<const std::string_view> values,
|
||||
shader_source_metadata& metadata
|
||||
) {
|
||||
namespace language = shading::shader_metadata_language;
|
||||
|
||||
if (values.size() != 1)
|
||||
{
|
||||
ztu::logger::warn("Invalid geometry declaration: Expected exactly one token but got %.", values.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto value = values.front();
|
||||
|
||||
if (const auto it = language::geometry_lookup.find(value); it != language::geometry_lookup.end())
|
||||
{
|
||||
metadata.geometry = it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::warn("Invalid geometry declaration: Unknown geometry %.", value);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void zgl::shader_source_manager::parse_feature_tokens(
|
||||
std::span<const std::string_view> values,
|
||||
const ztu::string_lookup<T>& feature_lookup,
|
||||
T& features
|
||||
) {
|
||||
features = {};
|
||||
|
||||
for (const auto value : values)
|
||||
{
|
||||
if (const auto it = feature_lookup.find(value); it != feature_lookup.end())
|
||||
{
|
||||
features |= it->second;
|
||||
}
|
||||
else
|
||||
{
|
||||
ztu::logger::warn("Ignoring unknown feature token %.", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool zgl::shader_source_manager::parse_features_declaration(
|
||||
std::span<const std::string_view> values,
|
||||
shader_source_metadata& metadata
|
||||
) {
|
||||
namespace language = shading::shader_metadata_language;
|
||||
|
||||
switch (metadata.geometry)
|
||||
{
|
||||
case shading::model_geometry::types::mesh:
|
||||
parse_feature_tokens(values, language::mesh_feature_lookup, metadata.feature_set.mesh.features);
|
||||
break;
|
||||
case shading::model_geometry::types::point_cloud:
|
||||
parse_feature_tokens(values, language::point_cloud_feature_lookup, metadata.feature_set.point_cloud.features);
|
||||
break;
|
||||
default:
|
||||
ztu::logger::warn("Internal error: Unknown geometry index %.", static_cast<int>(metadata.geometry));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool zgl::shader_source_manager::parse_static_enable_declaration(
|
||||
std::span<const std::string_view> values,
|
||||
shader_source_metadata& metadata
|
||||
) {
|
||||
namespace language = shading::shader_metadata_language;
|
||||
|
||||
switch (metadata.geometry)
|
||||
{
|
||||
case shading::model_geometry::types::mesh:
|
||||
parse_feature_tokens(values, language::mesh_feature_lookup, metadata.feature_set.mesh.static_enable);
|
||||
break;
|
||||
case shading::model_geometry::types::point_cloud:
|
||||
parse_feature_tokens(values, language::point_cloud_feature_lookup, metadata.feature_set.point_cloud.static_enable);
|
||||
break;
|
||||
default:
|
||||
ztu::logger::warn("Internal error: Unknown geometry index %.", static_cast<int>(metadata.geometry));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool zgl::shader_source_manager::parse_dynamic_enable_declaration(
|
||||
std::span<const std::string_view> values,
|
||||
shader_source_metadata& metadata
|
||||
) {
|
||||
namespace language = shading::shader_metadata_language;
|
||||
|
||||
switch (metadata.geometry)
|
||||
{
|
||||
case shading::model_geometry::types::mesh:
|
||||
parse_feature_tokens(values, language::mesh_feature_lookup, metadata.feature_set.mesh.dynamic_enable);
|
||||
break;
|
||||
case shading::model_geometry::types::point_cloud:
|
||||
parse_feature_tokens(values, language::point_cloud_feature_lookup, metadata.feature_set.point_cloud.dynamic_enable);
|
||||
break;
|
||||
default:
|
||||
ztu::logger::warn("Internal error: Unknown geometry index %.", static_cast<int>(metadata.geometry));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<zgl::shader_source_metadata> zgl::shader_source_manager::parse_metadata_from_tokens()
|
||||
{
|
||||
namespace language = shading::shader_metadata_language;
|
||||
using namespace std::string_view_literals;
|
||||
|
||||
shader_source_metadata data;
|
||||
|
||||
for (const auto [ type, name, parser ] :
|
||||
{
|
||||
std::make_tuple(language::declaration_type::stage, "stage"sv, &parse_stage_declaration),
|
||||
std::make_tuple(language::declaration_type::geometry, "geometry"sv, &parse_geometry_declaration),
|
||||
std::make_tuple(language::declaration_type::features, "features"sv, &parse_features_declaration),
|
||||
std::make_tuple(language::declaration_type::static_enable, "static_enable"sv, &parse_static_enable_declaration),
|
||||
std::make_tuple(language::declaration_type::dynamic_enable, "dynamic_enable"sv, &parse_dynamic_enable_declaration)
|
||||
}
|
||||
) {
|
||||
const auto index = m_declaration_type_index_buffer[static_cast<std::size_t>(type)];
|
||||
|
||||
if (index == static_cast<std::size_t>(language::declaration_type::invalid))
|
||||
{
|
||||
ztu::logger::warn("Shader metadata error: Missing % declaration.", name);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
const auto value_token_offset = std::accumulate(
|
||||
m_declaration_token_count_buffer.begin(),
|
||||
m_declaration_token_count_buffer.begin() + index,
|
||||
std::size_t{}
|
||||
);
|
||||
const auto value_token_count = m_declaration_token_count_buffer[index];
|
||||
|
||||
if (not parser(std::span(m_value_token_buffer).subspan(value_token_offset, value_token_count), data))
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user