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 struct 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
25
28class CGV_API shader_program : public shader_program_base
29{
30protected:
32 static std::map<std::string, std::string> program_file_cache;
33 static std::map<std::string, std::vector<std::string>> files_cache;
34
35 bool show_code_errors : 1;
36 bool linked : 1;
37 bool state_out_of_date : 1;
38 int nr_attached_geometry_shaders : 13;
39
40 std::vector<shader_code*> managed_codes;
41
43 bool attach_files(const context& ctx, const std::vector<std::string>& file_names, const shader_compile_options& options = {});
45 void update_state(const context& ctx);
47 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);
48public:
49
51 static bool collect_files_from_cache(const std::string& name, std::vector<std::string>& file_names, bool& added_files);
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_compile_options& options = {});
102 bool attach_files(const context& ctx, const std::string& base_name, const shader_compile_options& options = {});
104 bool attach_dir(const context& ctx, const std::string& dir_name, bool recursive, const shader_compile_options& options = {});
106 bool attach_program(const context& ctx, std::string file_name, bool show_error = false, const shader_compile_options& options = {});
108 static std::vector<shader_compile_options> extract_instances(std::string file_name);
110 bool link(const context& ctx, bool show_error = false);
112 bool is_linked() const;
121 void inspect_program_variables(const context& ctx, cgv::render::ProgramVariableKind kind, std::vector<cgv::render::program_variable_info>& Vs,
122 bool get_location = true, bool get_value = true);
124 bool build_files(const context& ctx, const std::string& base_name, bool show_error = false);
126 bool build_dir(const context& ctx, const std::string& dir_name, bool recursive = false, bool show_error = false);
128 bool build_program(const context& ctx, const std::string& file_name, bool show_error = false);
130 bool build_files(const context& ctx, const std::string& base_name, const shader_compile_options& options, bool show_error = false);
132 bool build_dir(const context& ctx, const std::string& dir_name, const shader_compile_options& options, bool recursive = false, bool show_error = false);
134 bool build_program(const context& ctx, const std::string& file_name, const shader_compile_options& options, bool show_error = false);
136 void set_geometry_shader_info(PrimitiveType input_type, PrimitiveType output_type, int max_output_count = 0);
138 bool enable(context& ctx);
140 bool disable(context& ctx);
142 bool is_enabled() const { return shader_program_base::is_enabled; }
144 const std::map<std::string, int>& get_uniform_locations() const;
146 int get_uniform_location(const context& ctx, const std::string& name) const;
148 bool set_material_uniform(const context& ctx, const std::string& name, const cgv::media::illum::surface_material& material, bool generate_error = false);
150 bool set_textured_material_uniform(const context& ctx, const std::string& name, const textured_material& material, bool generate_error = false);
152 bool set_light_uniform(const context& ctx, const std::string& name, const cgv::media::illum::light_source& light, bool generate_error = false);
156 template <typename T>
157 bool set_uniform(const context& ctx, const std::string& name, const T& value, bool generate_error = false) {
158 int loc = get_uniform_location(ctx, name);
159 if (loc == -1) {
160 if (generate_error)
161 ctx.error(std::string("shader_program::set_uniform() uniform <") + name + "> not found", this);
162 return false;
163 }
164 return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
165 }
167 template <typename T>
168 bool set_uniform_array(const context& ctx, const std::string& name, const T& array) {
169 int loc = get_uniform_location(ctx, name);
170 if (loc == -1) {
171 ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
172 return false;
173 }
175 }
177 template <typename T>
178 bool set_uniform_array(const context& ctx, const std::string& name, const T* array, size_t nr_elements, bool generate_error = false) {
179 int loc = get_uniform_location(ctx, name);
180 if (loc == -1) {
181 if (generate_error)
182 ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
183 return false;
184 }
185 return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array[0]), true), array, nr_elements);
186 }
190 template <typename T>
191 bool set_uniform(const context& ctx, int loc, const T& value) {
192 return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
193 }
195 template <typename T>
196 bool set_uniform_array(const context& ctx, int loc, const T& array) {
198 }
200 template <typename T>
201 bool set_uniform_array(const context& ctx, int loc, const T* array, size_t nr_elements) {
202 return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array), true), array, nr_elements);
203 }
207 bool set_uniform(const context& ctx, int loc, type_descriptor value_type, const void* value_ptr) {
208 return ctx.set_uniform_void(*this, loc, value_type, value_ptr);
209 }
211 int get_attribute_location(const context& ctx, const std::string& name) const;
213 template <typename T>
214 bool set_attribute(const context& ctx, const std::string& name, const T& value) {
215 int loc = ctx.get_attribute_location(*this, name);
216 if (loc == -1) {
217 ctx.error(std::string("shader_program::set_attribute() attribute <") + name + "> not found", this);
218 return false;
219 }
220 return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
221 }
223 template <typename T>
224 bool set_attribute(const context& ctx, int loc, const T& value) {
225 if (loc == -1) {
226 ctx.error("shader_program::set_attribute() called with loc=-1", this);
227 return false;
228 }
229 return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
230 }
231};
232
233 }
234}
235
236#include <cgv/config/lib_end.h>
>simple class to hold the properties of a light source
base class for all drawables, which is independent of the used rendering API.
Definition context.h:672
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
Definition context.cxx:306
a shader code object holds a code fragment of a geometry vertex or fragment shader and can be added t...
Stores preprocessor options used for conditionally compiling shader programs.
Definition shader_code.h:73
base interface for shader programs
Definition context.h:409
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 location, where the type can be any of int, unsigned,...
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(const context &ctx, int loc, type_descriptor value_type, const void *value_ptr)
Set the value of a uniform by location, where the value is defined by a type descriptor and address.
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
ProgramVariableKind
enumerates different kinds of shader program variables
Definition context.h:89
ShaderType
different shader types
Definition context.h:541
the cgv namespace
Definition print.h:11
Stores properties of a surface material.
compact type description of data that can be sent to the context; convertible to int
Definition context.h:59