cgv
Loading...
Searching...
No Matches
renderer.h
1#pragma once
2
3#include <cgv/render/context.h>
4#include <cgv/render/shader_program.h>
5#include <cgv_gl/gl/gl_context.h>
6
7#include "attribute_array_manager.h"
8
9#include "gl/lib_begin.h"
10
11namespace cgv { // @<
12 namespace render { // @<
13
15 struct CGV_API render_style
16 {
17 // Make destructor virtual to allow deletion of inherited class instances through pointer to this base class.
18 virtual ~render_style();
19 };
20
22
74 class CGV_API renderer
75 {
76 private:
78 shader_program prog;
80 shader_program* prog_ptr = &prog;
82 shader_compile_options prog_options;
84 shader_compile_options last_prog_options;
85//#ifdef _DEBUG
86 /* TODO: FIXME: Find out why _DEBUG is sometimes not set in CMake Ninja builds under Linux, causing crashes
87 due to inconsistent object layout in memory between different modules using the renderers */
90 int current_prog_render_count = 10;
91//#endif
93 std::set<int> enabled_attribute_arrays;
95 mutable render_style* default_render_style = nullptr;
97 const render_style* rs = nullptr;
99 const void* indices = nullptr;
101 const vertex_buffer* index_buffer_ptr = nullptr;
103 cgv::type::info::TypeId index_type = cgv::type::info::TI_UNDEF;
105 size_t index_count = 0;
107 attribute_array_manager default_aam;
110 protected:
112 bool has_aam() const { return aam_ptr != 0 && aam_ptr != &default_aam; }
114 bool has_attribute(const context& ctx, const std::string& name);
116 virtual render_style* create_render_style() const = 0;
118 const render_style* get_style_ptr() const;
120 bool attributes_persist() const { return has_aam(); }
124 virtual bool build_shader_program(context& ctx, shader_program& prog, const shader_compile_options& options) const;
125 public:
127 shader_compile_options& ref_shader_options() { return prog_options; }
129 shader_program& ref_prog() { return *prog_ptr; }
131 void set_prog(shader_program& one_shot_prog);
132 protected:
134 virtual std::string get_default_prog_name() const = 0;
135
137 template <typename T>
138 const T& get_style() const { return *static_cast<const T*>(get_style_ptr()); }
140 mutable bool has_colors = false;
142 mutable bool has_positions = false;
143
144 int get_prog_attribute_location(const context& ctx, const std::string & name, bool error_check = true) {
145 int loc = ref_prog().get_attribute_location(ctx, name);
146#ifdef _DEBUG
147 if(loc < 0 && error_check)
148 std::cerr << "Warning: cgv_gl::renderer attribute " << name << " not supported by current shader program" << std::endl;
149#endif
150 return loc;
151 }
152
153 template <typename T>
154 bool set_attribute_array(const context& ctx, const std::string& name, const T& array) {
155 int loc = get_prog_attribute_location(ctx, name);
156 if(loc < 0)
157 return false;
158 if(aam_ptr)
159 return aam_ptr->set_attribute_array(ctx, loc, array);
160 enabled_attribute_arrays.insert(loc);
161 return attribute_array_binding::set_global_attribute_array(ctx, loc, array);
162 }
163 template <typename T>
164 bool set_attribute_array(const context& ctx, const std::string& name, const T* array_ptr, size_t nr_elements, unsigned stride) {
165 int loc = get_prog_attribute_location(ctx, name);
166 if(loc < 0)
167 return false;
168 if (aam_ptr)
169 return aam_ptr->set_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
170 enabled_attribute_arrays.insert(loc);
171 return attribute_array_binding::set_global_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
172 }
173 bool set_attribute_array(const context& ctx, const std::string& name, type_descriptor element_type, const vertex_buffer& vbo, size_t offset_in_bytes, size_t nr_elements, unsigned stride_in_bytes);
174 //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);
176 template <typename C, typename T>
177 bool set_composed_attribute_array(const context& ctx, const std::string& name, const C* array_ptr, size_t nr_elements, const T& elem) {
178 int loc = get_prog_attribute_location(ctx, name);
179 if(loc < 0)
180 return false;
181 if (aam_ptr)
182 return aam_ptr->set_composed_attribute_array(ctx, loc, array_ptr, nr_elements, elem);
183 enabled_attribute_arrays.insert(loc);
184 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
185 }
187 template <typename C, typename T>
188 bool ref_composed_attribute_array(const context& ctx, const std::string& name, const std::string& name_ref, const C* array_ptr, size_t nr_elements, const T& elem) {
189 int loc = get_prog_attribute_location(ctx, name);
190 int loc_ref = get_prog_attribute_location(ctx, name_ref);
191 if(loc < 0 || loc_ref < 0)
192 return false;
193 if (aam_ptr)
194 return aam_ptr->ref_composed_attribute_array(ctx, loc, loc_ref, array_ptr, nr_elements, elem);
195 enabled_attribute_arrays.insert(loc);
196 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
197 }
198 bool remove_attribute_array(const context& ctx, const std::string& name);
199 public:
201 void draw_impl(context& ctx, PrimitiveType pt, size_t start, size_t count, bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
203 void draw_impl_instanced(context& ctx, PrimitiveType type, size_t start, size_t count, size_t instance_count, bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
205 renderer();
207 virtual ~renderer();
209 void manage_singleton(context& ctx, const std::string& renderer_name, int& ref_count, int ref_count_change);
211 virtual void enable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
213 virtual void disable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
215 DEPRECATED("deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().")
216 virtual void set_attribute_array_manager(const context& ctx, attribute_array_manager* _aam_ptr = 0);
218 void set_render_style(const render_style& rs);
220 bool build_program(context& ctx, shader_program& prog, const render_style& rs);
222
225 virtual bool init(context& ctx);
227 template <typename T>
228 void set_position(const context& ctx, const T& position) { has_positions = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "position"), position); }
230 template <typename T>
231 void set_position_array(const context& ctx, const std::vector<T>& positions) { has_positions = true; set_attribute_array(ctx, "position", positions); }
233 template <typename T>
234 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, "position", positions, nr_elements, stride_in_bytes); }
236 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);
238 template <typename T>
239 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); }
241 void remove_position_array(const context& ctx);
243 template <typename T>
244 void set_color(const context& ctx, const T& color) { has_colors = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "color"), color); }
246 template <typename T>
247 void set_color_array(const context& ctx, const std::vector<T>& colors) { has_colors = true; set_attribute_array(ctx, "color", colors); }
249 template <typename T>
250 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, "color", colors, nr_elements, stride_in_bytes); }
252 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);
254 template <typename T>
255 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); }
257 void remove_color_array(const context& ctx);
268 template <typename T>
269 bool set_indices(const context& ctx, const std::vector<T>& indices, bool keep_on_cpu = false) {
270 this->indices = &indices.front();
271 index_buffer_ptr = 0;
272 index_count = indices.size();
274 if (!keep_on_cpu && aam_ptr)
275 return aam_ptr->set_indices(ctx, indices);
276 return true;
277 }
289 template <typename T>
290 bool set_indices(const context& ctx, const T* indices, size_t nr_indices, bool keep_on_cpu = false) {
291 this->indices = indices;
292 index_buffer_ptr = 0;
293 index_count = nr_indices;
295 if (!keep_on_cpu && aam_ptr)
296 return aam_ptr->set_indices(ctx, indices, nr_indices);
297 return true;
298 }
300
310 template <typename T>
311 bool set_indices(const context& ctx, const vertex_buffer& vbo, size_t count) {
312 index_buffer_ptr = &vbo;
313 indices = 0;
314 index_count = count;
316 if (aam_ptr)
317 aam_ptr->remove_indices(ctx);
318 return true;
319 }
321 bool has_indices() const {
322 if(aam_ptr && aam_ptr->has_index_buffer())
323 return true;
324 return index_count > 0;
325 }
327 void remove_indices(const context& ctx);
331 const vertex_buffer* get_vertex_buffer_ptr(const context& ctx, const attribute_array_manager& aam, const std::string& attr_name) {
332 return aam.get_buffer_ptr(get_prog_attribute_location(ctx, attr_name));
333 }
338 return aam.get_buffer_ptr(-1);
339 }
341 virtual bool validate_attributes(const context& ctx) const;
343 bool validate_and_enable(context& ctx);
345
347 virtual bool enable(context& ctx);
349
360 virtual void draw(context& ctx, size_t start, size_t count,
361 bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
363 virtual bool disable(context& ctx);
365
373 virtual bool render(context& ctx, size_t start, size_t count,
374 bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
376 virtual void clear(const context& ctx);
377 };
378 }
379}
380
381#include <cgv/config/lib_end.h>
attribute array manager used to upload arrays to gpu
bool has_index_buffer() const
whether aam contains an index buffer
const vertex_buffer * get_buffer_ptr(int loc) const
returns the pointer to the vertex buffer as managed by this attribute array manager if specified and ...
base class for all drawables, which is independent of the used rendering API.
Definition context.h:670
Abstract base class for all renderers that manage a render data and the rendering process.
Definition renderer.h:75
shader_compile_options & ref_shader_options()
access to shader program compile options to update settings not handled by render style
Definition renderer.h:127
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.
Definition renderer.h:290
bool set_indices(const context &ctx, const vertex_buffer &vbo, size_t count)
Set the indices for indexed rendering from a GPU buffer.
Definition renderer.h:311
bool ref_composed_attribute_array(const context &ctx, const std::string &name, const std::string &name_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:188
virtual void update_shader_program_options(shader_compile_options &options) const
overload to update the shader program compile options based on the current render style; only called ...
Definition renderer.h:122
bool set_composed_attribute_array(const context &ctx, const std::string &name, 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:177
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.
Definition renderer.h:269
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:129
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:239
bool has_aam() const
check for attribute array manager
Definition renderer.h:112
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:244
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:250
virtual render_style * create_render_style() const =0
implement this method to create a default render style
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:231
virtual std::string get_default_prog_name() const =0
implement this method to return the name of the default shader program; return an empty string if the...
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:247
const vertex_buffer * get_vertex_buffer_ptr(const context &ctx, const attribute_array_manager &aam, const std::string &attr_name)
Definition renderer.h:331
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:234
const vertex_buffer * get_index_buffer_ptr(const attribute_array_manager &aam)
Definition renderer.h:337
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:120
bool has_indices() const
return whether indices have been defined
Definition renderer.h:321
const T & get_style() const
access to style
Definition renderer.h:138
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:255
Stores preprocessor options used for conditionally compiling shader programs.
Definition shader_code.h:73
a shader program combines several shader code fragments to a complete definition of the shading pipel...
a vertex buffer is an unstructured memory block on the GPU.
PrimitiveType
different primitive types
Definition context.h:277
TypeId
ids for the different types and type constructs
Definition type_id.h:12
the cgv namespace
Definition print.h:11
base class for all render styles
Definition renderer.h:16
compact type description of data that can be sent to the context; convertible to int
Definition context.h:57
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition type_id.h:86