Further shader compilation development.

This commit is contained in:
zy4n
2025-03-22 17:40:08 +01:00
parent e01b8c2e09
commit 510398423a
45 changed files with 1567 additions and 1097 deletions

View File

@@ -9,195 +9,60 @@
#include "assets/dynamic_data_stores/dynamic_shader_source_store.hpp"
#include "opengl/shader_program_lookup.hpp"
#include "opengl/handles/shader_handle.hpp"
#include "opengl/handles/shader_program_handle.hpp"
#include "shader_program/metadata_type.hpp"
#include "shader_program/features/mesh_features.hpp"
#include "shader_program/features/point_cloud_features.hpp"
#include "util/string_lookup.hpp"
#include "shader_program/metadata_type.hpp"
#include "shader_preprocessor.hpp"
#include "opengl/metadata/shader_metadata.hpp"
#include "opengl/data/shader_data.hpp"
#include "opengl/handles/shader_handle_set.hpp"
#include "opengl/shading/requirements/shader_requirements.hpp"
#include "opengl/metadata/shader_set_metadata.hpp"
#include "opengl/shading/requirements/shader_set_requirements.hpp"
namespace zgl
{
class shader_program_compiler
{
using shader_lookup_entry_type = std::pair<shader_metadata, shader_data>;
static constexpr auto gl_shader_types = std::array<GLenum, shading::stage::count>{
GL_VERTEX_SHADER,
GL_TESS_CONTROL_SHADER,
GL_TESS_EVALUATION_SHADER,
GL_GEOMETRY_SHADER,
GL_FRAGMENT_SHADER
};
public:
void register_shader_sources(
void preprocess(
const dynamic_shader_source_store& shader_sources
);
void compile_shaders(
const dynamic_shader_source_store& shader_sources,
std::span<const shader_program::metadata_type> required_capabilities,
std::vector<shader_handle>& shader_handles
std::span<const shading::shader_set_requirements> requirements,
std::span<shader_set_metadata> metadata,
std::span<shader_handle_set> shader_sets
);
protected:
void tokenize_declarations(
std::string_view source,
std::vector<std::string_view> tokens,
std::vector<std::size_t> declaration_token_counts,
std::span<std::size_t> declaration_type_indices
shader_handle find_shader(
const shading::shader_requirements& requirements
);
std::optional<shader_program::metadata_type> parse_metadata_from_tokens(
std::span<const std::string_view> tokens,
std::span<const std::size_t> declaration_token_counts,
std::span<const std::size_t> declaration_type_indices
);
[[nodiscard]] static bool parse_stage_declaration(
std::span<const std::string_view> tokens,
shader_program::metadata_type& metadata
);
[[nodiscard]] static bool parse_geometry_declaration(
std::span<const std::string_view> tokens,
shader_program::metadata_type& metadata
);
[[nodiscard]] static bool parse_features_declaration(
std::span<const std::string_view> tokens,
shader_program::metadata_type& metadata
);
[[nodiscard]] static bool parse_static_enable_declaration(
std::span<const std::string_view> tokens,
shader_program::metadata_type& metadata
);
[[nodiscard]] static bool parse_dynamic_enable_declaration(
std::span<const std::string_view> tokens,
shader_program::metadata_type& metadata
);
template<typename T>
static void parse_feature_tokens(
std::span<const std::string_view> tokens,
const ztu::string_lookup<T>& feature_lookup,
T& features
);
std::optional<dynamic_shader_source_store::id_type> zgl::shader_program_compiler::find_compatible_shader_source(
shader_program::metadata_type& requirements
);
template<typename T>
void add_required_feature_defines(
T toggle_flags,
std::span<const std::string> defines,
std::vector<const char*>& shader_strings
bool compile_shader(
GLenum shader_type,
std::span<const char*> source_strings,
shader_data& shader
);
private:
enum class metadata_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()
};
shader_preprocessor m_preprocessor{};
inline static auto declaration_lookup = ztu::string_lookup<metadata_declaration_type>{
{ "STAGE", metadata_declaration_type::stage },
{ "GEOMETRY", metadata_declaration_type::geometry },
{ "FEATURES", metadata_declaration_type::features },
{ "STATIC_ENABLE", metadata_declaration_type::static_enable }
{ "DYNAMIC_ENABLE", metadata_declaration_type::dynamic_enable }
};
inline static auto stage_lookup = ztu::string_lookup<shader_program::stage::types>{
{ "VERTEX", shader_program::stage::types::vertex },
{ "GEOMETRY", shader_program::stage::types::geometry },
{ "FRAGMENT", shader_program::stage::types::fragment },
};
inline static auto geometry_lookup = ztu::string_lookup<shader_program::geometry::types>{
{ "MESH", shader_program::geometry::types::mesh },
{ "POINT_CLOUD", shader_program::geometry::types::point_cloud }
};
inline static auto mesh_feature_lookup = []
{
using namespace shader_program::features::mesh;
auto lookup = ztu::string_lookup<flags>{};
lookup.reserve(all.size());
constexpr auto all_flags = std::array{
flags::face, flags::line, flags::point,
flags::luminance, flags::color, flags::alpha,
flags::lighting, flags::texture, flags::uniform_color
};
for (const auto& [ define, flag ] : std::ranges::views::zip(defines, all_flags))
{
lookup.emplace(std::string(define), flag);
}
return lookup;
}();
inline static auto point_cloud_feature_lookup = []
{
using namespace shader_program::features::point_cloud;
auto lookup = ztu::string_lookup<flags>{};
lookup.reserve(all.size());
constexpr auto all_flags = std::array{
flags::square, flags::lighting, flags::luminance,
flags::color, flags::alpha, flags::uniform_color,
flags::rainbow
};
for (const auto& [ define, flag ] : std::ranges::views::zip(defines, all_flags))
{
lookup.emplace(std::string(define), flag);
}
return lookup;
}();
inline static auto mesh_feature_defines = []
{
using namespace shader_program::features::mesh;
auto statements = std::array<std::string, all.size()>{};
std::ranges::transform(
defines,
statements.begin(),
[](const auto& name) {
return std::format("#define {}\n", name);
}
);
return statements;
}();
inline static auto point_cloud_feature_defines = []
{
using namespace shader_program::features::point_cloud;
auto statements = std::array<std::string, all.size()>{};
std::ranges::transform(
defines,
statements.begin(),
[](const auto& name) {
return std::format("#define {}\n", name);
}
);
return statements;
}();
std::vector<std::pair<shader_program::metadata_type, dynamic_shader_source_store::id_type>> shader_lookup;
std::vector<shading::shader_source_requirements> m_source_requirement_buffer{};
std::vector<preprocessed_shader_source_metadata> m_preprocessed_shader_source_metadata_buffer;
std::vector<const char*> m_source_strings_buffer;
std::vector<shader_lookup_entry_type> m_shader_lookup;
};
}

View File

@@ -0,0 +1,110 @@
#pragma once
#include <string_view>
#include <vector>
#include <optional>
#include <span>
#include "util/string_lookup.hpp"
#include "../metadata/shader_source_metadata.hpp"
#include "opengl/shading/requirements/shader_source_requirements.hpp"
#include "opengl/metadata/preprocessed_shader_source_metadata.hpp"
#include "assets/dynamic_data_stores/dynamic_shader_source_store.hpp"
#include "opengl/shading/shader_source_set.hpp"
namespace zgl {
class shader_preprocessor {
public:
void preprocess(
const dynamic_shader_source_store& shader_sources
);
void fetch_shader_sources(
const dynamic_shader_source_store& shader_sources,
std::span<const shading::shader_source_requirements> requirements,
std::span<preprocessed_shader_source_metadata> metadata,
std::vector<const char*>& shader_strings
);
protected:
void tokenize_declarations(std::string_view source);
std::optional<shader_source_metadata> parse_metadata_from_tokens();
[[nodiscard]] static bool parse_stage_declaration(
std::span<const std::string_view> values,
shader_source_metadata& metadata
);
[[nodiscard]] static bool parse_geometry_declaration(
std::span<const std::string_view> tokens,
shader_source_metadata& metadata
);
[[nodiscard]] static bool parse_features_declaration(
std::span<const std::string_view> values,
shader_source_metadata& metadata
);
[[nodiscard]] static bool parse_static_enable_declaration(
std::span<const std::string_view> values,
shader_source_metadata& metadata
);
[[nodiscard]] static bool parse_dynamic_enable_declaration(
std::span<const std::string_view> values,
shader_source_metadata& metadata
);
template<typename T>
static void parse_feature_tokens(
std::span<const std::string_view> values,
const ztu::string_lookup<T>& feature_lookup,
T& features
);
static void get_define_strings(
shading::model_geometry::types geometry,
shading::features::generic::type features,
shading::features::generic::type& feature_count,
std::vector<const char*>& defines
);
private:
inline static auto mesh_feature_defines = std::array{
"#define FACE\n",
"#define LINE\n",
"#define POINT\n",
"#define V_L\n",
"#define V_RGB\n",
"#define V_A\n",
"#define LIGHTING\n",
"#define TEXTURE\n",
"#define U_RGBA\n",
};
inline static auto point_cloud_feature_defines = std::array{
"#define SQUARE\n",
"#define LIGHTING\n",
"#define V_L\n",
"#define V_RGB\n",
"#define V_A\n",
"#define U_RGBA\n",
"#define RAINBOW\n"
};
std::vector<std::string_view> m_value_token_buffer;
std::vector<std::size_t> m_declaration_token_count_buffer;
std::array<std::size_t, 4> m_declaration_type_index_buffer;
using source_lookup_entry_type = std::pair<shader_source_metadata, dynamic_shader_source_store::id_type>;
using source_lookup_type = std::vector<source_lookup_entry_type>;
source_lookup_type m_shader_source_lookup;
};
}

View File

@@ -3,13 +3,18 @@
#include <unordered_map>
#include "assets/dynamic_data_stores/dynamic_shader_source_store.hpp"
#include "opengl/shader_program_lookup.hpp"
#include "opengl/handles/shader_handle.hpp"
#include "opengl/handles/shader_program_handle.hpp"
#include "../metadata/shader_source_metadata.hpp"
#include "shading/shader_program_requirements.hpp"
namespace zgl
{
template<typename Capabilities>
class shader_program_compiler
{
public:
public:
// compile shader programs for given requirements
@@ -22,21 +27,24 @@ class shader_program_compiler
const dynamic_shader_source_store& shader_sources
);
void compile_shaders(
void compile_shader_programs(
const dynamic_shader_source_store& shader_sources,
std::span<const shader_program::metadata_type> required_capabilities,
std::vector<shader_handle>& shader_handles
std::span<const shading::shader_program_metadata> requirements,
std::vector<shader_program_handle>& shader_handles
);
// create metadata for all sources
// get
ś
protected:
private:
std::unordered_map<dynamic_shader_source_store::id_type, Capabilities> shader_capabilities;
std::vector<shading::compiled_shader_metadata_type, shader_handle> shader_lookup;
std::vector<shading::shader_program_metadata, shader_program_handle> shader_program_lookup;
};
}