cgv
Loading...
Searching...
No Matches
shader_program.h
1#pragma once
2
3#include <set>
4#include "element_traits.h"
5#include "shader_code.h"
6#include "textured_material.h"
7
8namespace cgv {
9 namespace media {
10 namespace illum {
11 class surface_material;
12 class light_source;
13 }
14 }
15}
16
17#include "lib_begin.h"
18
19namespace cgv {
20 namespace utils {
21 struct line;
22 }
23 namespace render {
24
27class CGV_API shader_program : public shader_program_base
28{
29protected:
31 static std::map<std::string, std::string> program_file_cache;
32 static std::map<std::string, std::vector<std::string>> files_cache;
33
34 bool show_code_errors : 1;
35 bool linked : 1;
36 bool state_out_of_date : 1;
37 int nr_attached_geometry_shaders : 13;
38
39 std::vector<shader_code*> managed_codes;
41 bool attach_files(const context& ctx, const std::vector<std::string>& file_names, const shader_define_map& defines = shader_define_map());
43 void update_state(const context& ctx);
45 static bool open_program_file(std::string& file_name, bool use_cache, std::string& content, std::vector<cgv::utils::line>& lines, std::string* last_error_ptr = 0);
46public:
47
48
50 static bool collect_files_from_cache(const std::string& name, std::vector<std::string>& file_names, bool& added_files);
51
52
54 static bool collect_file(const std::string& file_name, bool use_cache, std::vector<std::string>& file_names);
57 static bool collect_files(const std::string& base_name, bool use_cache, std::vector<std::string>& file_names);
62 static bool collect_dir(const std::string& dir_name, bool recursive, std::vector<std::string>& file_names);
81 static bool collect_program(const std::string& file_name, bool use_cache, std::vector<std::string>& file_names);
83 static unsigned int get_max_nr_geometry_shader_output_vertices(const context& ctx);
86 shader_program(bool _show_code_errors = false);
90 bool create(const context& ctx);
92 void destruct(const context& ctx);
94 bool attach_code(const context& ctx, const shader_code& code);
96 bool detach_code(const context& ctx, const shader_code& code);
98 bool attach_code(const context& ctx, const std::string& source, ShaderType st);
100 bool attach_file(const context& ctx, const std::string& file_name, ShaderType st = ST_DETECT, const shader_define_map& defines = shader_define_map());
102 bool attach_files(const context& ctx, const std::string& base_name, const shader_define_map& defines = shader_define_map());
104 bool attach_dir(const context& ctx, const std::string& dir_name, bool recursive);
106 bool attach_program(const context& ctx, std::string file_name, bool show_error = false, const shader_define_map& defines = shader_define_map());
108 static std::vector<shader_define_map> extract_instances(std::string file_name);
110 bool link(const context& ctx, bool show_error = false);
112 bool is_linked() const;
114 bool build_files(const context& ctx, const std::string& base_name, bool show_error = false, const shader_define_map& defines = shader_define_map());
116 bool build_dir(const context& ctx, const std::string& dir_name, bool recursive = false, bool show_error = false);
118 bool build_program(const context& ctx, const std::string& file_name, bool show_error = false, const shader_define_map& defines = shader_define_map());
120 void set_geometry_shader_info(PrimitiveType input_type, PrimitiveType output_type, int max_output_count = 0);
122 bool enable(context& ctx);
124 bool disable(context& ctx);
126 bool is_enabled() const { return shader_program_base::is_enabled; }
128 int get_uniform_location(const context& ctx, const std::string& name) const;
130 bool set_material_uniform(const context& ctx, const std::string& name, const cgv::media::illum::surface_material& material, bool generate_error = false);
132 bool set_textured_material_uniform(const context& ctx, const std::string& name, const textured_material& material, bool generate_error = false);
134 bool set_light_uniform(const context& ctx, const std::string& name, const cgv::media::illum::light_source& light, bool generate_error = false);
138 template <typename T>
139 bool set_uniform(const context& ctx, const std::string& name, const T& value, bool generate_error = false) {
140 int loc = ctx.get_uniform_location(*this, name);
141 if (loc == -1 && generate_error) {
142 ctx.error(std::string("shader_program::set_uniform() uniform <") + name + "> not found", this);
143 return false;
144 }
145 return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
146 }
148 template <typename T>
149 bool set_uniform_array(const context& ctx, const std::string& name, const T& array) {
150 int loc = ctx.get_uniform_location(*this, name);
151 if (loc == -1) {
152 ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
153 return false;
154 }
156 }
158 template <typename T>
159 bool set_uniform_array(const context& ctx, const std::string& name, const T* array, size_t nr_elements, bool generate_error = false) {
160 int loc = ctx.get_uniform_location(*this, name);
161 if (loc == -1 && generate_error) {
162 ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
163 return false;
164 }
165 return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array[0]), true), array, nr_elements);
166 }
170 template <typename T>
171 bool set_uniform(const context& ctx, int loc, const T& value) {
172 return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
173 }
175 template <typename T>
176 bool set_uniform_array(const context& ctx, int loc, const T& array) {
178 }
180 template <typename T>
181 bool set_uniform_array(const context& ctx, int loc, const T* array, size_t nr_elements) {
182 return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array), true), array, nr_elements);
183 }
185 int get_attribute_location(const context& ctx, const std::string& name) const;
187 template <typename T>
188 bool set_attribute(const context& ctx, const std::string& name, const T& value) {
189 int loc = ctx.get_attribute_location(*this, name);
190 if (loc == -1) {
191 ctx.error(std::string("shader_program::set_attribute() attribute <") + name + "> not found", this);
192 return false;
193 }
194 return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
195 }
197 template <typename T>
198 bool set_attribute(const context& ctx, int loc, const T& value) {
199 if (loc == -1) {
200 ctx.error("shader_program::set_attribute() called with loc=-1", this);
201 return false;
202 }
203 return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
204 }
205};
206
207 }
208}
209
210#include <cgv/config/lib_end.h>
>simple class to hold the properties of a light source
simple class to hold the material properties of a phong material
base class for all drawables, which is independent of the used rendering API.
Definition context.h:621
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
Definition context.cxx:219
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
base interface for shader programs
Definition context.h:355
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool set_uniform_array(const context &ctx, const std::string &name, const T *array, size_t nr_elements, bool generate_error=false)
set uniform array from an array with nr_elements elements of type T pointed to by array
static std::map< std::string, std::string > program_file_cache
maps used to cache program file contents and valid file names indexed by their respective file name
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
bool set_uniform_array(const context &ctx, const std::string &name, const T &array)
set uniform array from array array where number elements can be derived from array through array_desc...
bool set_uniform(const context &ctx, int loc, const T &value)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
bool set_attribute(const context &ctx, const std::string &name, const T &value)
set constant default value of a vertex attribute by attribute name, if name does not specify an attri...
bool set_uniform_array(const context &ctx, int loc, const T &array)
set uniform array from array array where number elements can be derived from array through array_desc...
bool is_enabled() const
check whether program is currently enabled
bool set_attribute(const context &ctx, int loc, const T &value)
set constant default value of a vertex attribute by location index
bool set_uniform_array(const context &ctx, int loc, const T *array, size_t nr_elements)
set uniform array from an array with nr_elements elements of type T pointed to by array
class that extends obj_material with the management of textures
std::map< std::string, std::string > shader_define_map
typedef for shader define map data structure
Definition shader_code.h:52
ShaderType
different shader types
Definition context.h:485
PrimitiveType
different primitive types
Definition context.h:225
the cgv namespace
Definition print.h:11
compact type description of data that can be sent to the context; convertible to int
Definition context.h:47