cgv
renderer.h
1 #pragma once
2 
3 #include <cgv/render/context.h>
4 #include <cgv/render/shader_program.h>
5 #include <cgv/render/vertex_buffer.h>
6 #include <cgv/render/attribute_array_binding.h>
7 #include <cgv_gl/gl/gl_context.h>
8 
9 #include "gl/lib_begin.h"
10 
11 namespace cgv { // @<
12  namespace render { // @<
13 
15  struct CGV_API render_style : public render_types
16  {
17  virtual ~render_style();
18  };
21  {
22  protected:
28  std::map<int, vertex_buffer*> vbos;
30  friend class renderer;
32  template <typename T>
33  bool set_indices(const context& ctx, const T& array)
34  {
35  bool res;
36  vertex_buffer*& vbo_ptr = vbos[-1];
37  if (vbo_ptr) {
38  if (vbo_ptr->get_size_in_bytes() == array_descriptor_traits <T>::get_size(array))
39  res = vbo_ptr->replace(ctx, 0, array_descriptor_traits <T>::get_address(array), array_descriptor_traits < T>::get_nr_elements(array));
40  else {
41  vbo_ptr->destruct(ctx);
42  res = vbo_ptr->create(ctx, array);
43  }
44  }
45  else {
46  vbo_ptr = new vertex_buffer(VBT_INDICES, default_usage);
47  res = vbo_ptr->create(ctx, array);
48  }
49  if (res)
50  res = ctx.set_element_array(&aab, vbo_ptr);
51  return res;
52  }
54  template <typename T>
55  bool set_indices(const context& ctx, const T* array, size_t count)
56  {
57  bool res;
58  vertex_buffer*& vbo_ptr = vbos[-1];
59  if (vbo_ptr) {
60  if (vbo_ptr->get_size_in_bytes() == count * get_type_size(cgv::type::info::type_id<T>::get_id()))
61  res = vbo_ptr->replace(ctx, 0, array, count);
62  else {
63  vbo_ptr->destruct(ctx);
64  res = vbo_ptr->create(ctx, array, count);
65  }
66  }
67  else {
68  vbo_ptr = new vertex_buffer(VBT_INDICES, default_usage);
69  res = vbo_ptr->create(ctx, array, count);
70  }
71  if (res)
72  res = ctx.set_element_array(&aab, vbo_ptr);
73  return res;
74  }
76  bool has_index_buffer() const;
78  void remove_indices(const context& ctx);
80  template <typename T>
81  bool set_attribute_array(const context& ctx, int loc, const T& array) {
82  bool res;
83  vertex_buffer*& vbo_ptr = vbos[loc];
84  if (vbo_ptr) {
85  if (vbo_ptr->get_size_in_bytes() == array_descriptor_traits <T>::get_size(array))
86  res = vbo_ptr->replace(ctx, 0, array_descriptor_traits <T>::get_address(array), array_descriptor_traits < T>::get_nr_elements(array));
87  else {
88  vbo_ptr->destruct(ctx);
89  res = vbo_ptr->create(ctx, array);
90  }
91  }
92  else {
93  vbo_ptr = new vertex_buffer(VBT_VERTICES, default_usage);
94  res = vbo_ptr->create(ctx, array);
95  }
96  if(res)
97  res = ctx.set_attribute_array_void(&aab, loc, array_descriptor_traits <T>::get_type_descriptor(array), vbo_ptr, 0, array_descriptor_traits < T>::get_nr_elements(array));
98  return res;
99  }
101  template <typename T>
102  bool set_attribute_array(const context& ctx, int loc, const T* array_ptr, size_t nr_elements, unsigned stride) {
103  bool res;
104  vertex_buffer*& vbo_ptr = vbos[loc];
105  if (vbo_ptr) {
106  if (vbo_ptr->get_size_in_bytes() == nr_elements * sizeof(T))
107  res = vbo_ptr->replace(ctx, 0, array_ptr, nr_elements);
108  else {
109  vbo_ptr->destruct(ctx);
110  res = vbo_ptr->create(ctx, array_ptr, nr_elements);
111  }
112  }
113  else {
114  vbo_ptr = new vertex_buffer(VBT_VERTICES, default_usage);
115  res = vbo_ptr->create(ctx, array_ptr, nr_elements);
116  }
117  if (res)
118  res = ctx.set_attribute_array_void(&aab, loc, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(*array_ptr), true), vbo_ptr, 0, nr_elements);
119  return res;
120  }
122  bool set_attribute_array(const context& ctx, int loc, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
123 
124  template <typename C, typename T>
125  bool set_composed_attribute_array(const context& ctx, int loc, const C* array_ptr, size_t nr_elements, const T& elem) {
126  bool res;
127  vertex_buffer*& vbo_ptr = vbos[loc];
128  if (vbo_ptr) {
129  if (vbo_ptr->get_size_in_bytes() == nr_elements * sizeof(C))
130  res = vbo_ptr->replace(ctx, 0, array_ptr, nr_elements);
131  else {
132  vbo_ptr->destruct(ctx);
133  res = vbo_ptr->create(ctx, array_ptr, nr_elements);
134  }
135  }
136  else {
137  vbo_ptr = new vertex_buffer(VBT_VERTICES, default_usage);
138  res = vbo_ptr->create(ctx, array_ptr, nr_elements);
139  }
140  if (res)
141  res = ctx.set_attribute_array_void(&aab, loc,
142  type_descriptor(element_descriptor_traits<T>::get_type_descriptor(elem), true),
143  vbo_ptr,
144  reinterpret_cast<const void*>(reinterpret_cast<const cgv::type::uint8_type*>(&elem) - reinterpret_cast<const cgv::type::uint8_type*>(array_ptr)),
145  nr_elements, sizeof(C));
146  return res;
147  }
148  template <typename C, typename T>
149  bool ref_composed_attribute_array(const context& ctx, int loc, int loc_ref, const C* array_ptr, size_t nr_elements, const T& elem) {
150  vertex_buffer*& vbo_ptr = vbos[loc_ref];
151  if (!vbo_ptr)
152  return false;
153  return ctx.set_attribute_array_void(&aab, loc,
154  type_descriptor(element_descriptor_traits<T>::get_type_descriptor(elem), true),
155  vbo_ptr,
156  reinterpret_cast<const void*>(reinterpret_cast<const cgv::type::uint8_type*>(&elem) - reinterpret_cast<const cgv::type::uint8_type*>(array_ptr)),
157  nr_elements, sizeof(C));
158  }
159  public:
161  attribute_array_manager(VertexBufferUsage _default_usage = VBU_STREAM_DRAW);
163  ~attribute_array_manager();
165  bool has_attribute(const context& ctx, int loc) const;
167  bool init(context& ctx);
169  bool enable(context& ctx);
171  bool disable(context& ctx);
173  void destruct(const context& ctx);
174  };
176  class CGV_API renderer : public render_types
177  {
178  private:
180  shader_program prog;
182  shader_program* prog_ptr;
184  std::set<int> enabled_attribute_arrays;
186  mutable render_style* default_render_style;
188  const render_style* rs;
190  const void* indices;
192  const vertex_buffer* index_buffer_ptr;
194  cgv::type::info::TypeId index_type;
196  size_t index_count;
198  attribute_array_manager default_aam;
200  attribute_array_manager* aam_ptr;
201  protected:
203  bool has_aam() const { return aam_ptr != 0 && aam_ptr != &default_aam; }
205  bool has_attribute(const context& ctx, const std::string& attr_name) {
206  return aam_ptr->has_attribute(ctx, ref_prog().get_attribute_location(ctx, attr_name));
207  }
209  const render_style* get_style_ptr() const;
211  bool attributes_persist() const { return has_aam(); }
212  public:
214  shader_program& ref_prog() { return *prog_ptr; }
216  void set_prog(shader_program& one_shot_prog);
217  protected:
219  template <typename T>
220  const T& get_style() const { return *static_cast<const T*>(get_style_ptr()); }
222  mutable bool has_colors;
224  mutable bool has_positions;
226  virtual render_style* create_render_style() const = 0;
227 
228  template <typename T>
229  bool set_attribute_array(const context& ctx, int loc, const T& array) {
230  if (aam_ptr)
231  return aam_ptr->set_attribute_array(ctx, loc, array);
232  enabled_attribute_arrays.insert(loc);
234  }
235  template <typename T>
236  bool set_attribute_array(const context& ctx, int loc, const T* array_ptr, size_t nr_elements, unsigned stride) {
237  if (aam_ptr)
238  return aam_ptr->set_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
239  enabled_attribute_arrays.insert(loc);
240  return attribute_array_binding::set_global_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
241  }
242  bool set_attribute_array(const context& ctx, int loc, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
244  template <typename C, typename T>
245  bool set_composed_attribute_array(const context& ctx, int loc, const C* array_ptr, size_t nr_elements, const T& elem) {
246  if (aam_ptr)
247  return aam_ptr->set_composed_attribute_array(ctx, loc, array_ptr, nr_elements, elem);
248  enabled_attribute_arrays.insert(loc);
249  return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
250  }
252  template <typename C, typename T>
253  bool ref_composed_attribute_array(const context& ctx, int loc, int loc_ref, const C* array_ptr, size_t nr_elements, const T& elem) {
254  if (aam_ptr)
255  return aam_ptr->ref_composed_attribute_array(ctx, loc, loc_ref, array_ptr, nr_elements, elem);
256  enabled_attribute_arrays.insert(loc);
257  return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
258  }
260  void draw_impl(context& ctx, PrimitiveType pt, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index);
262  void draw_impl_instanced(context& ctx, PrimitiveType type, size_t start, size_t count, size_t instance_count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index);
263  public:
265  renderer();
267  virtual ~renderer();
269  void manage_singleton(context& ctx, const std::string& renderer_name, int& ref_count, int ref_count_change);
271  virtual void enable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
273  virtual void disable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
275  DEPRECATED("deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().")
276  virtual void set_attribute_array_manager(const context& ctx, attribute_array_manager* _aam_ptr = 0);
278  void set_render_style(const render_style& rs);
280  virtual bool init(context& ctx);
282  template <typename T>
283  void set_position(const context& ctx, const T& position) { has_positions = true; ref_prog().set_attribute(ctx, ref_prog().get_attribute_location(ctx, "position"), position); }
285  template <typename T>
286  void set_position_array(const context& ctx, const std::vector<T>& positions) { has_positions = true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx, "position"), positions); }
288  template <typename T>
289  void set_position_array(const context& ctx, const T* positions, size_t nr_elements, unsigned stride_in_bytes = 0) { has_positions = true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx, "position"), positions, nr_elements, stride_in_bytes); }
291  void set_position_array(const context& ctx, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
293  template <typename T>
294  void set_position_array(const context& ctx, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0) { set_position_array(ctx, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()), true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
296  template <typename T>
297  void set_color(const context& ctx, const T& color) { has_colors = true; ref_prog().set_attribute(ctx, ref_prog().get_attribute_location(ctx, "color"), color); }
299  template <typename T>
300  void set_color_array(const context& ctx, const std::vector<T>& colors) { has_colors = true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx, "color"), colors); }
302  template <typename T>
303  void set_color_array(const context& ctx, const T* colors, size_t nr_elements, unsigned stride_in_bytes = 0) { has_colors = true; set_attribute_array(ctx, ref_prog().get_attribute_location(ctx, "color"), colors, nr_elements, stride_in_bytes); }
305  void set_color_array(const context& ctx, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0);
307  template <typename T>
308  void set_color_array(const context& ctx, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes = 0) { set_color_array(ctx, type_descriptor(element_descriptor_traits<T>::get_type_descriptor(T()), true), vbo, offset_in_bytes, nr_elements, stride_in_bytes); }
319  template <typename T>
320  bool set_indices(const context& ctx, const std::vector<T>& indices, bool keep_on_cpu = false) {
321  this->indices = &indices.front();
322  index_buffer_ptr = 0;
323  index_count = indices.size();
325  if (!keep_on_cpu && aam_ptr)
326  return aam_ptr->set_indices(ctx, indices);
327  return true;
328  }
340  template <typename T>
341  bool set_indices(const context& ctx, const T* indices, size_t nr_indices, bool keep_on_cpu = false) {
342  this->indices = indices;
343  index_buffer_ptr = 0;
344  index_count = nr_indices;
346  if (!keep_on_cpu && aam_ptr)
347  return aam_ptr->set_indices(ctx, indices, nr_indices);
348  return true;
349  }
351 
361  template <typename T>
362  bool set_indices(const context& ctx, const vertex_buffer& vbo, size_t count) {
363  index_buffer_ptr = &vbo;
364  indices = 0;
365  index_count = count;
367  if (aam_ptr)
368  aam_ptr->remove_indices(ctx);
369  return true;
370  }
372  bool has_indices() const { return index_count > 0; }
374  void remove_indices(const context& ctx);
376  virtual bool validate_attributes(const context& ctx) const;
378  bool validate_and_enable(context& ctx);
380  virtual bool enable(context& ctx);
382 
393  virtual void draw(context& ctx, size_t start, size_t count,
394  bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
396  virtual bool disable(context& ctx);
398 
406  virtual bool render(context& ctx, size_t start, size_t count,
407  bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
409  virtual void clear(const context& ctx);
410 
411 
412  // TODO: should this be done this way?
413  int get_vbo(const context& ctx, const std::string attr_name) {
414 
415  if(aam_ptr) {
416  int loc = ref_prog().get_attribute_location(ctx, attr_name);
417  auto it = aam_ptr->vbos.find(loc);
418  if(it != aam_ptr->vbos.end()) {
419  vertex_buffer* vbo_ptr = aam_ptr->vbos[loc];
420  if(vbo_ptr->handle) {
421  return (const int&)vbo_ptr->handle - 1;
422  }
423  }
424  }
425 
426  return (const int&)-1;
427  }
428  };
429  }
430 }
431 
432 #include <cgv/config/lib_end.h>
cgv::render::attribute_array_binding
Definition: attribute_array_binding.h:20
cgv::render::attribute_array_binding::set_global_attribute_array
static bool set_global_attribute_array(const context &ctx, int loc, const vertex_buffer &vbo, type_descriptor td, size_t size, size_t offset, unsigned stride=0)
point array of vertex attribute at location loc to vertex buffer array array stored in CPU memory; in...
Definition: attribute_array_binding.cxx:64
cgv::render::render_style
base class for all render styles
Definition: renderer.h:16
cgv::render::renderer::set_indices
bool set_indices(const context &ctx, const T *indices, size_t nr_indices, bool keep_on_cpu=false)
Set the indices for indexed rendering from an array given as a pointer. If an attribute array manager...
Definition: renderer.h:341
cgv::render::attribute_array_manager::vbos
std::map< int, vertex_buffer * > vbos
store vertex buffers generated per attribute location
Definition: renderer.h:28
cgv::render::renderer::set_color_array
void set_color_array(const context &ctx, const T *colors, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the color attribute from a vector of colors of type T
Definition: renderer.h:303
cgv::render::vertex_buffer::replace
bool replace(const context &ctx, size_t buffer_offset_in_bytes, const T *array_ptr, size_t nr_elements)
replace part (starting at byte offset buffer_offset_in_bytes) or whole vertex buffer content from nr_...
Definition: vertex_buffer.h:42
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::renderer::set_position_array
void set_position_array(const context &ctx, const T *positions, size_t nr_elements, unsigned stride_in_bytes=0)
templated method to set the position attribute from a vector of positions of type T
Definition: renderer.h:289
cgv::render::renderer::set_composed_attribute_array
bool set_composed_attribute_array(const context &ctx, int loc, const C *array_ptr, size_t nr_elements, const T &elem)
in case that several attributes are stored interleaved, call this function for the first and ref_comp...
Definition: renderer.h:245
cgv::render::attribute_array_manager::has_attribute
bool has_attribute(const context &ctx, int loc) const
check whether the given attribute is available
Definition: renderer.cxx:13
cgv::type::info::TypeId
TypeId
ids for the different types and type constructs
Definition: type_id.h:12
cgv::type::uint8_type
unsigned char uint8_type
this type provides an 8 bit unsigned integer type
Definition: standard_types.h:16
cgv::render::VertexBufferUsage
VertexBufferUsage
different vertex buffer usages as defined in OpenGL
Definition: context.h:359
cgv::render::vertex_buffer
Definition: vertex_buffer.h:13
cgv::render::renderer::create_render_style
virtual render_style * create_render_style() const =0
virtual method that creates a default render style
cgv::render::renderer
abstract base class for all renderers that handles a shader program and position / color attribute
Definition: renderer.h:177
cgv::render::renderer::DEPRECATED
DEPRECATED("deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().") virtual void set_attribute_array_manager(const context &ctx
this function is deprecated, please use enable_attribute_array_manager() and disable_attribute_manage...
cgv::render::renderer::set_color_array
void set_color_array(const context &ctx, const vertex_buffer &vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the color attribute from a vertex buffer object, the element type must be give...
Definition: renderer.h:308
cgv::render::renderer::ref_prog
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition: renderer.h:214
cgv::render::renderer::get_style
const T & get_style() const
access to style
Definition: renderer.h:220
cgv::render::renderer::set_indices
bool set_indices(const context &ctx, const std::vector< T > &indices, bool keep_on_cpu=false)
Set the indices for indexed rendering from a vector. If an attribute array manager is enabled and kee...
Definition: renderer.h:320
cgv::render::renderer::ref_composed_attribute_array
bool ref_composed_attribute_array(const context &ctx, int loc, int loc_ref, const C *array_ptr, size_t nr_elements, const T &elem)
in case that several attributes are stored interleaved, call set_composed_attribute_array() for the f...
Definition: renderer.h:253
cgv::render::renderer::has_colors
bool has_colors
track whether color attribute is defined
Definition: renderer.h:222
cgv::render::attribute_array_manager
attribute array manager used to upload arrays to gpu
Definition: renderer.h:21
cgv::render::renderer::attributes_persist
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition: renderer.h:211
cgv::render::shader_program
Definition: shader_program.h:25
cgv::render::vertex_buffer::destruct
void destruct(const context &ctx)
destruct the render buffer
Definition: vertex_buffer.cxx:56
cgv::render::vertex_buffer::get_size_in_bytes
size_t get_size_in_bytes() const
return size in bytes
Definition: vertex_buffer.cxx:25
cgv::render::attribute_array_manager::default_usage
VertexBufferUsage default_usage
store default buffer usage
Definition: renderer.h:24
cgv::type::info::type_id
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition: type_id.h:86
cgv::render::renderer::has_indices
bool has_indices() const
return whether indices have been defined
Definition: renderer.h:372
cgv::render::attribute_array_manager::aab
attribute_array_binding aab
attribue array binding used to store array pointers
Definition: renderer.h:26
cgv::render::renderer::set_color
void set_color(const context &ctx, const T &color)
templated method to set the color attribute from a single color of type T
Definition: renderer.h:297
cgv::type::info::get_type_size
unsigned int get_type_size(TypeId tid)
function that returns the size of a type specified through TypeId
Definition: type_id.cxx:18
cgv::render::renderer::set_position_array
void set_position_array(const context &ctx, const vertex_buffer &vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes=0)
template method to set the position attribute from a vertex buffer object, the element type must be g...
Definition: renderer.h:294
cgv::render::renderer::set_color_array
void set_color_array(const context &ctx, const std::vector< T > &colors)
template method to set the color attribute from a vector of colors of type T
Definition: renderer.h:300
cgv::render::renderer::has_aam
bool has_aam() const
check for attribute array manager
Definition: renderer.h:203
cgv::render::renderer::set_indices
bool set_indices(const context &ctx, const vertex_buffer &vbo, size_t count)
Set the indices for indexed rendering from a GPU buffer. If an attribute array manager is enabled its...
Definition: renderer.h:362
cgv::render::renderer::has_attribute
bool has_attribute(const context &ctx, const std::string &attr_name)
check for attribute
Definition: renderer.h:205
cgv::render::renderer::has_positions
bool has_positions
track whether position attribute is defined
Definition: renderer.h:224
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::render::renderer::set_position_array
void set_position_array(const context &ctx, const std::vector< T > &positions)
templated method to set the position attribute from a vector of positions of type T
Definition: renderer.h:286
cgv::render::context
Definition: context.h:525
cgv::render::PrimitiveType
PrimitiveType
different primitive types
Definition: context.h:177
cgv::render::vertex_buffer::create
bool create(const context &ctx, size_t size_in_bytes)
create empty vertex buffer of size size given in bytes
Definition: vertex_buffer.cxx:37