cgv
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 
8 namespace 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 
19 namespace cgv {
20  namespace render {
21 
24 class CGV_API shader_program : public shader_program_base
25 {
26 protected:
27  bool show_code_errors : 1;
28  bool linked : 1;
29  bool state_out_of_date : 1;
30  int nr_attached_geometry_shaders : 13;
31 
32  std::vector<shader_code*> managed_codes;
34  bool attach_files(const context& ctx, const std::vector<std::string>& file_names, std::string defines = "");
36  void update_state(const context& ctx);
37 public:
39  static bool collect_file(const std::string& file_name, std::vector<std::string>& file_names);
42  static bool collect_files(const std::string& base_name, std::vector<std::string>& file_names);
47  static bool collect_dir(const std::string& dir_name, bool recursive, std::vector<std::string>& file_names);
66  static bool collect_program(const std::string& file_name, std::vector<std::string>& file_names);
68  static unsigned int get_max_nr_geometry_shader_output_vertices(const context& ctx);
71  shader_program(bool _show_code_errors = false);
73  ~shader_program();
75  bool create(const context& ctx);
77  void destruct(const context& ctx);
79  bool attach_code(const context& ctx, const shader_code& code);
81  bool detach_code(const context& ctx, const shader_code& code);
83  bool attach_code(const context& ctx, const std::string& source, ShaderType st);
85  bool attach_file(const context& ctx, const std::string& file_name, ShaderType st = ST_DETECT, std::string defines = "");
87  bool attach_files(const context& ctx, const std::string& base_name, std::string defines = "");
89  bool attach_dir(const context& ctx, const std::string& dir_name, bool recursive);
91  bool attach_program(const context& ctx, const std::string& file_name, bool show_error = false, std::string defines = "");
93  bool link(const context& ctx, bool show_error = false);
95  bool is_linked() const;
97  bool build_files(const context& ctx, const std::string& base_name, bool show_error = false);
99  bool build_dir(const context& ctx, const std::string& dir_name, bool recursive = false, bool show_error = false);
101  bool build_program(const context& ctx, const std::string& file_name, bool show_error = false, std::string defines = "");
103  void set_geometry_shader_info(PrimitiveType input_type, PrimitiveType output_type, int max_output_count = 0);
105  bool enable(context& ctx);
107  bool disable(context& ctx);
109  bool is_enabled() const { return shader_program_base::is_enabled; }
111  int get_uniform_location(const context& ctx, const std::string& name) const;
113  bool set_material_uniform(const context& ctx, const std::string& name, const cgv::media::illum::surface_material& material, bool generate_error = false);
115  bool set_textured_material_uniform(const context& ctx, const std::string& name, const textured_material& material, bool generate_error = false);
117  bool set_light_uniform(const context& ctx, const std::string& name, const cgv::media::illum::light_source& light, bool generate_error = false);
121  template <typename T>
122  bool set_uniform(const context& ctx, const std::string& name, const T& value, bool generate_error = false) {
123  int loc = ctx.get_uniform_location(*this, name);
124  if (loc == -1 && generate_error) {
125  ctx.error(std::string("shader_program::set_uniform() uniform <") + name + "> not found", this);
126  return false;
127  }
128  return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
129  }
131  template <typename T>
132  bool set_uniform_array(const context& ctx, const std::string& name, const T& array) {
133  int loc = ctx.get_uniform_location(*this, name);
134  if (loc == -1) {
135  ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
136  return false;
137  }
138  return ctx.set_uniform_array_void(*this, loc, array_descriptor_traits<T>::get_type_descriptor(array), array_descriptor_traits<T>::get_address(array), array_descriptor_traits<T>::get_nr_elements(array));
139  }
141  template <typename T>
142  bool set_uniform_array(const context& ctx, const std::string& name, const T* array, size_t nr_elements, bool generate_error = false) {
143  int loc = ctx.get_uniform_location(*this, name);
144  if (loc == -1 && generate_error) {
145  ctx.error(std::string("shader_program::set_uniform_array() uniform <") + name + "> not found", this);
146  return false;
147  }
148  return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array[0]), true), array, nr_elements);
149  }
153  template <typename T>
154  bool set_uniform(const context& ctx, int loc, const T& value) {
155  return ctx.set_uniform_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
156  }
158  template <typename T>
159  bool set_uniform_array(const context& ctx, int loc, const T& array) {
160  return ctx.set_uniform_array_void(*this, loc, array_descriptor_traits<T>::get_type_descriptor(), array_descriptor_traits<T>::get_address(array), array_descriptor_traits<T>::get_nr_elements(array));
161  }
163  template <typename T>
164  bool set_uniform_array(const context& ctx, int loc, const T* array, size_t nr_elements) {
165  return ctx.set_uniform_array_void(*this, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(array), true), array, nr_elements);
166  }
168  int get_attribute_location(const context& ctx, const std::string& name) const;
170  template <typename T>
171  bool set_attribute(const context& ctx, const std::string& name, const T& value) {
172  int loc = ctx.get_attribute_location(*this, name);
173  if (loc == -1) {
174  ctx.error(std::string("shader_program::set_attribute() attribute <") + name + "> not found", this);
175  return false;
176  }
177  return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
178  }
180  template <typename T>
181  bool set_attribute(const context& ctx, int loc, const T& value) {
182  if (loc == -1) {
183  ctx.error("shader_program::set_attribute() called with loc=-1", this);
184  return false;
185  }
186  return ctx.set_attribute_void(*this, loc, element_descriptor_traits<T>::get_type_descriptor(value), element_descriptor_traits<T>::get_address(value));
187  }
188 };
189 
190  }
191 }
192 
193 #include <cgv/config/lib_end.h>
cgv::media::illum::light_source
>simple class to hold the properties of a light source
Definition: light_source.hh:17
cgv::render::shader_program::set_uniform
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Definition: shader_program.h:122
cgv::render::type_descriptor
compact type description of data that can be sent to the context; convertible to int
Definition: context.h:36
cgv::render::ShaderType
ShaderType
different shader types
Definition: context.h:391
cgv::render::shader_program::set_attribute
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...
Definition: shader_program.h:171
cgv::render::shader_program::is_enabled
bool is_enabled() const
check whether program is currently enabled
Definition: shader_program.h:109
cgv::render::shader_program::set_uniform_array
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...
Definition: shader_program.h:132
cgv::render::textured_material
class that extends obj_material with the management of textures
Definition: textured_material.h:13
cgv::render::shader_program::set_uniform_array
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
Definition: shader_program.h:142
cgv::render::shader_program::set_uniform
bool set_uniform(const context &ctx, int loc, const T &value)
Definition: shader_program.h:154
cgv::render::shader_program
Definition: shader_program.h:25
cgv::media::illum::surface_material
simple class to hold the material properties of a phong material
Definition: surface_material.h:26
cgv::render::shader_program::set_attribute
bool set_attribute(const context &ctx, int loc, const T &value)
set constant default value of a vertex attribute by location index
Definition: shader_program.h:181
cgv::render::shader_code
Definition: shader_code.h:54
cgv::render::shader_program_base
base interface for shader programs
Definition: context.h:290
cgv::render::shader_program::set_uniform_array
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...
Definition: shader_program.h:159
cgv::render::shader_program::set_uniform_array
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
Definition: shader_program.h:164
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::render::context
Definition: context.h:525
cgv::render::PrimitiveType
PrimitiveType
different primitive types
Definition: context.h:177
cgv::render::context::error
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
Definition: context.cxx:183