cgv
Loading...
Searching...
No Matches
shader_code.h
1#pragma once
2
3#include <cgv/render/context.h>
4#include <set>
5
6#include "lib_begin.h"
7
8namespace cgv {
9 namespace render {
10
25struct CGV_API shader_config : public cgv::base::base
26{
28 std::string shader_path;
34 std::vector<std::string> shader_file_names;
36 std::vector<std::string> inserted_shader_file_names;
40 std::string get_type_name() const;
42 bool self_reflect(cgv::reflect::reflection_handler& srh);
43};
44
47
50
52typedef std::map<std::string, std::string> shader_define_map;
53
73 std::string id;
75 std::string content;
76};
77
91 std::vector<shader_code_snippet> snippets;
92
93 // Add constructors to allow implicit cast from defines and enable backwards compatibility.
96 shader_compile_options(const std::vector<shader_code_snippet>& snippets) : snippets(snippets) {}
97 shader_compile_options(const shader_define_map& defines, const std::vector<shader_code_snippet>& snippets) : defines(defines), snippets(snippets) {}
98};
99
103class CGV_API shader_code : public render_component
104{
105public:
106 template<typename T, typename std::enable_if<!std::is_enum<T>::value, bool>::type = true>
107 static void set_define(shader_define_map& defines, const std::string& name, const T& value, const T& default_value) {
108 if(value != default_value)
109 defines[name] = std::to_string(value);
110 else
111 defines.erase(name);
112 }
113
114 template<typename T, typename std::enable_if<std::is_enum<T>::value, bool>::type = true>
115 static void set_define(shader_define_map& defines, const std::string& name, const T& value, const T& default_value) {
116 if(value != default_value)
117 defines[name] = std::to_string((unsigned)value);
118 else
119 defines.erase(name);
120 }
121
122 static void set_define(shader_define_map& defines, const std::string& name, const std::string& value, const std::string& default_value) {
123 if(value != default_value)
124 defines[name] = value;
125 else
126 defines.erase(name);
127 }
128
129 static void set_define(shader_define_map& defines, const std::string& name, bool value, bool default_value) {
130 if(value != default_value)
131 defines[name] = value ? "1" : "0";
132 else
133 defines.erase(name);
134 }
135
136protected:
138 static std::map<std::string, std::string> shader_file_name_map;
142 static std::map<std::string, std::string> code_cache;
143
146
147public:
149 shader_code();
151 ~shader_code();
153 static void decode_if_base64(std::string& content);
168 static std::string find_file(const std::string& file_name, bool search_exhaustive = false);
170 static std::string get_last_error(const std::string& file_name, const std::string& last_error);
172 static std::string read_code_file(const std::string &file_name, std::string* _last_error = 0);
174 static std::string retrieve_code(const std::string& file_name, bool use_cache, std::string* _last_error);
182 static ShaderType detect_shader_type(const std::string& file_name);
184 void destruct(const context& ctx);
188 bool read_code(const context& ctx, const std::string &file_name, ShaderType st = ST_DETECT, const shader_compile_options& options = {});
190 bool set_code(const context& ctx, const std::string &source, ShaderType st);
192 ShaderType get_shader_type() const;
194 bool compile(const context& ctx);
198 bool read_and_compile(const context& ctx, const std::string& file_name, ShaderType st = ST_DETECT, const shader_compile_options& options = {}, bool show_error = true);
200 bool is_compiled() const;
201
202private:
204 static std::string resolve_includes(const std::string& source, bool use_cache, std::set<std::string>& included_file_names, std::string* _last_error = 0);
206 static std::string resolve_includes(const std::string& source, bool use_cache, std::string* _last_error = 0);
208 static void resolve_version(std::string& source);
210 static void set_defines_and_snippets(std::string& source, const shader_compile_options& options);
212 static void set_vertex_attrib_locations(std::string& source);
213};
214
215
216 }
217}
218
219#include <cgv/config/lib_end.h>
base class for all classes that can be registered with support for dynamic properties (see also secti...
Definition base.h:75
reference counted pointer, which can work together with types that are derived from ref_counted,...
Definition ref_ptr.h:160
the self reflection handler is passed to the virtual self_reflect() method of cgv::base::base.
base class for all drawables, which is independent of the used rendering API.
Definition context.h:626
base interface for all render components
Definition context.h:309
a shader code object holds a code fragment of a geometry vertex or fragment shader and can be added t...
static bool shader_file_name_map_initialized
whether the shader file name map is initialized
static std::map< std::string, std::string > code_cache
map that caches shader file contents indexed by their file name
ShaderType st
store the shader type
static std::map< std::string, std::string > shader_file_name_map
map that caches full shader file paths indexed by the shader file name
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
Definition shader_code.h:52
shader_config_ptr get_shader_config()
return a reference to the current shader configuration
cgv::data::ref_ptr< shader_config > shader_config_ptr
type of ref counted pointer to shader configuration
Definition shader_code.h:46
ShaderType
different shader types
Definition context.h:495
the cgv namespace
Definition print.h:11
a snippet of shader code in raw text form.
Definition shader_code.h:71
std::string content
the snippet content; can be any valid piece of shader code
Definition shader_code.h:75
std::string id
the snippet id used for matching snippet markers in shader code
Definition shader_code.h:73
holds options applied before and during shader compilation, such as preprocessor defines and code sni...
Definition shader_code.h:87
std::vector< shader_code_snippet > snippets
shader code snippets
Definition shader_code.h:91
shader_define_map defines
map of pre-processor define names to values
Definition shader_code.h:89
a globally unique shader config is registered by default when the cgv library is used.
Definition shader_code.h:26
std::vector< std::string > shader_file_names
mapping of shader index to file name
Definition shader_code.h:34
bool show_file_paths
whether to output full paths of read shaders
Definition shader_code.h:32
bool trace_file_names
whether to keep track of file names
Definition shader_code.h:30
std::string shader_path
the path used to find shaders with the cgv::utils::file::find_in_paths function
Definition shader_code.h:28
std::vector< std::string > inserted_shader_file_names
mapping of shader index to inserted files name
Definition shader_code.h:36