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
57class CGV_API shader_code : public render_component
58{
59public:
60 template<typename T, typename std::enable_if<!std::is_enum<T>::value, bool>::type = true>
61 static void set_define(shader_define_map& defines, const std::string& name, const T& value, const T& default_value) {
62 if(value != default_value)
63 defines[name] = std::to_string(value);
64 else
65 defines.erase(name);
66 }
67
68 template<typename T, typename std::enable_if<std::is_enum<T>::value, bool>::type = true>
69 static void set_define(shader_define_map& defines, const std::string& name, const T& value, const T& default_value) {
70 if(value != default_value)
71 defines[name] = std::to_string((unsigned)value);
72 else
73 defines.erase(name);
74 }
75
76 static void set_define(shader_define_map& defines, const std::string& name, const std::string& value, const std::string& default_value) {
77 if(value != default_value)
78 defines[name] = value;
79 else
80 defines.erase(name);
81 }
82
83 static void set_define(shader_define_map& defines, const std::string& name, bool value, bool default_value) {
84 if(value != default_value)
85 defines[name] = value ? "1" : "0";
86 else
87 defines.erase(name);
88 }
89
90protected:
92 static std::map<std::string, std::string> shader_file_name_map;
96 static std::map<std::string, std::string> code_cache;
97
100
101public:
103 shader_code();
105 ~shader_code();
107 static void decode_if_base64(std::string& content);
122 static std::string find_file(const std::string& file_name, bool search_exhaustive = false);
124 static std::string get_last_error(const std::string& file_name, const std::string& last_error);
126 static std::string read_code_file(const std::string &file_name, std::string* _last_error = 0);
128 static std::string retrieve_code(const std::string& file_name, bool use_cache, std::string* _last_error);
136 static ShaderType detect_shader_type(const std::string& file_name);
138 static std::string resolve_includes(const std::string& source, bool use_cache, std::set<std::string>& included_file_names, std::string* _last_error = 0);
140 inline static std::string resolve_includes(const std::string& source, bool use_cache, std::string* _last_error = 0) {
141 std::set<std::string> dummy;
142 return resolve_includes(source, use_cache, dummy, _last_error);
143 }
145 void destruct(const context& ctx);
149 bool read_code(const context& ctx, const std::string &file_name, ShaderType st = ST_DETECT, const shader_define_map& defines = shader_define_map());
151 bool set_code(const context& ctx, const std::string &source, ShaderType st);
153 void set_defines(std::string& source, const shader_define_map& defines);
155 void set_vertex_attrib_locations(std::string& source);
157 ShaderType get_shader_type() const;
159 bool compile(const context& ctx);
163 bool read_and_compile(const context& ctx, const std::string &file_name, ShaderType st = ST_DETECT, bool show_error = true, const shader_define_map& defines = shader_define_map());
165 bool is_compiled() const;
166};
167
168
169 }
170}
171
172#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:621
base interface for all render components
Definition context.h:301
a shader code object holds a code fragment of a geometry vertex or fragment shader and can be added t...
Definition shader_code.h:58
static bool shader_file_name_map_initialized
whether the shader file name map is initialized
Definition shader_code.h:94
static std::map< std::string, std::string > code_cache
map that caches shader file contents indexed by their file name
Definition shader_code.h:96
static std::string resolve_includes(const std::string &source, bool use_cache, std::string *_last_error=0)
search for include directives in the given source code, replace them by the included file contents an...
ShaderType st
store the shader type
Definition shader_code.h:99
static std::map< std::string, std::string > shader_file_name_map
map that caches full shader file paths indexed by the shader file name
Definition shader_code.h:92
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:485
the cgv namespace
Definition print.h:11
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