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;
40
42 bool attach_files(const context& ctx, const std::vector<std::string>& file_names, const shader_compile_options& options = {});
44 void update_state(const context& ctx);
46 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);
47public:
48
50 static bool collect_files_from_cache(const std::string& name, std::vector<std::string>& file_names, bool& added_files);
51
53 static bool collect_file(const std::string& file_name, bool use_cache, std::vector<std::string>& file_names);
56 static bool collect_files(const std::string& base_name, bool use_cache, std::vector<std::string>& file_names);
61 static bool collect_dir(const std::string& dir_name, bool recursive, std::vector<std::string>& file_names);
80 static bool collect_program(const std::string& file_name, bool use_cache, std::vector<std::string>& file_names);
82 static unsigned int get_max_nr_geometry_shader_output_vertices(const context& ctx);
85 shader_program(bool _show_code_errors = false);
89 bool create(const context& ctx);
91 void destruct(const context& ctx);
93 bool attach_code(const context& ctx, const shader_code& code);
95 bool detach_code(const context& ctx, const shader_code& code);
97 bool attach_code(const context& ctx, const std::string& source, ShaderType st);
99 bool attach_file(const context& ctx, const std::string& file_name, ShaderType st = ST_DETECT, const shader_compile_options& options = {});
101 bool attach_files(const context& ctx, const std::string& base_name, const shader_compile_options& options = {});
103 bool attach_dir(const context& ctx, const std::string& dir_name, bool recursive, const shader_compile_options& options = {});
105 bool attach_program(const context& ctx, std::string file_name, bool show_error = false, const shader_compile_options& options = {});
107 static std::vector<shader_define_map> extract_instances(std::string file_name);
109 bool link(const context& ctx, bool show_error = false);
111 bool is_linked() const;
113 bool build_files(const context& ctx, const std::string& base_name, bool show_error = false);
115 bool build_dir(const context& ctx, const std::string& dir_name, bool recursive = false, bool show_error = false);
117 bool build_program(const context& ctx, const std::string& file_name, bool show_error = false);
119 bool build_files(const context& ctx, const std::string& base_name, const shader_compile_options& options, bool show_error = false);
121 bool build_dir(const context& ctx, const std::string& dir_name, const shader_compile_options& options, bool recursive = false, bool show_error = false);
123 bool build_program(const context& ctx, const std::string& file_name, const shader_compile_options& options, bool show_error = false);
125 void set_geometry_shader_info(PrimitiveType input_type, PrimitiveType output_type, int max_output_count = 0);
127 bool enable(context& ctx);
129 bool disable(context& ctx);
131 bool is_enabled() const { return shader_program_base::is_enabled; }
133 const std::map<std::string, int>& get_uniform_locations() const;
135 int get_uniform_location(const context& ctx, const std::string& name) const;
137 bool set_material_uniform(const context& ctx, const std::string& name, const cgv::media::illum::surface_material& material, bool generate_error = false);
139 bool set_textured_material_uniform(const context& ctx, const std::string& name, const textured_material& material, bool generate_error = false);
141 bool set_light_uniform(const context& ctx, const std::string& name, const cgv::media::illum::light_source& light, bool generate_error = false);
145 template <typename T>
146 bool set_uniform(const context& ctx, const std::string& name, const T& value, bool generate_error = false) {
147 int loc = get_uniform_location(ctx, name);
148 if (loc == -1 && generate_error) {
149 ctx.error(std::string("shader_program::set_uniform() uniform <") + name + "> not found", this);
150 return false;
151 }
152 return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
153 }
155 template <typename T>
156 bool set_uniform_array(const context& ctx, const std::string& name, const T& array) {
157 int loc = get_uniform_location(ctx, name);
158 if (loc == -1) {
159 ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
160 return false;
161 }
163 }
165 template <typename T>
166 bool set_uniform_array(const context& ctx, const std::string& name, const T* array, size_t nr_elements, bool generate_error = false) {
167 int loc = get_uniform_location(ctx, name);
168 if (loc == -1 && generate_error) {
169 ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
170 return false;
171 }
172 return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array[0]), true), array, nr_elements);
173 }
177 template <typename T>
178 bool set_uniform(const context& ctx, int loc, const T& value) {
179 return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
180 }
182 template <typename T>
183 bool set_uniform_array(const context& ctx, int loc, const T& array) {
185 }
187 template <typename T>
188 bool set_uniform_array(const context& ctx, int loc, const T* array, size_t nr_elements) {
189 return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array), true), array, nr_elements);
190 }
194 bool set_uniform(const context& ctx, int loc, type_descriptor value_type, const void* value_ptr) {
195 return ctx.set_uniform_void(*this, loc, value_type, value_ptr);
196 }
198 int get_attribute_location(const context& ctx, const std::string& name) const;
200 template <typename T>
201 bool set_attribute(const context& ctx, const std::string& name, const T& value) {
202 int loc = ctx.get_attribute_location(*this, name);
203 if (loc == -1) {
204 ctx.error(std::string("shader_program::set_attribute() attribute <") + name + "> not found", this);
205 return false;
206 }
207 return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
208 }
210 template <typename T>
211 bool set_attribute(const context& ctx, int loc, const T& value) {
212 if (loc == -1) {
213 ctx.error("shader_program::set_attribute() called with loc=-1", this);
214 return false;
215 }
216 return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
217 }
218};
219
220 }
221}
222
223#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:626
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...
base interface for shader programs
Definition context.h:363
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
ShaderType
different shader types
Definition context.h:495
the cgv namespace
Definition print.h:11
holds options applied before and during shader compilation, such as preprocessor defines and code sni...
Definition shader_code.h:87
compact type description of data that can be sent to the context; convertible to int
Definition context.h:55