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 class CGV_API renderer
23 {
24 private:
26 shader_program prog;
28 shader_program* prog_ptr = &prog;
30 shader_compile_options prog_options;
32 shader_compile_options last_prog_options;
33//#ifdef _DEBUG
34 /* TODO: FIXME: Find out why _DEBUG is sometimes not set in CMake Ninja builds under Linux, causing crashes
35 due to inconsistent object layout in memory between different modules using the renderers */
38 int current_prog_render_count = 10;
39//#endif
41 std::set<int> enabled_attribute_arrays;
43 mutable render_style* default_render_style = nullptr;
45 const render_style* rs = nullptr;
47 const void* indices = nullptr;
49 const vertex_buffer* index_buffer_ptr = nullptr;
51 cgv::type::info::TypeId index_type = cgv::type::info::TI_UNDEF;
53 size_t index_count = 0;
55 attribute_array_manager default_aam;
58 protected:
60 bool has_aam() const { return aam_ptr != 0 && aam_ptr != &default_aam; }
62 bool has_attribute(const context& ctx, const std::string& name) {
63 return aam_ptr->has_attribute(ctx, get_prog_attribute_location(ctx, name, false));
64 }
66 virtual render_style* create_render_style() const = 0;
68 const render_style* get_style_ptr() const;
70 bool attributes_persist() const { return has_aam(); }
74 virtual bool build_shader_program(context& ctx, shader_program& prog, const shader_compile_options& options) const;
75 public:
77 shader_compile_options& ref_shader_options() { return prog_options; }
79 shader_program& ref_prog() { return *prog_ptr; }
81 void set_prog(shader_program& one_shot_prog);
82 protected:
84 virtual std::string get_default_prog_name() const = 0;
85
87 template <typename T>
88 const T& get_style() const { return *static_cast<const T*>(get_style_ptr()); }
90 mutable bool has_colors = false;
92 mutable bool has_positions = false;
93
94 int get_prog_attribute_location(const context& ctx, const std::string & name, bool error_check = true) {
95 int loc = ref_prog().get_attribute_location(ctx, name);
96#ifdef _DEBUG
97 if(loc < 0 && error_check)
98 std::cerr << "Warning: cgv_gl::renderer attribute " << name << " not supported by current shader program" << std::endl;
99#endif
100 return loc;
101 }
102
103 template <typename T>
104 bool set_attribute_array(const context& ctx, const std::string& name, const T& array) {
105 int loc = get_prog_attribute_location(ctx, name);
106 if(loc < 0)
107 return false;
108 if(aam_ptr)
109 return aam_ptr->set_attribute_array(ctx, loc, array);
110 enabled_attribute_arrays.insert(loc);
111 return attribute_array_binding::set_global_attribute_array(ctx, loc, array);
112 }
113 template <typename T>
114 bool set_attribute_array(const context& ctx, const std::string& name, const T* array_ptr, size_t nr_elements, unsigned stride) {
115 int loc = get_prog_attribute_location(ctx, name);
116 if(loc < 0)
117 return false;
118 if (aam_ptr)
119 return aam_ptr->set_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
120 enabled_attribute_arrays.insert(loc);
121 return attribute_array_binding::set_global_attribute_array(ctx, loc, array_ptr, nr_elements, stride);
122 }
123 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);
124 //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);
126 template <typename C, typename T>
127 bool set_composed_attribute_array(const context& ctx, const std::string& name, const C* array_ptr, size_t nr_elements, const T& elem) {
128 int loc = get_prog_attribute_location(ctx, name);
129 if(loc < 0)
130 return false;
131 if (aam_ptr)
132 return aam_ptr->set_composed_attribute_array(ctx, loc, array_ptr, nr_elements, elem);
133 enabled_attribute_arrays.insert(loc);
134 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
135 }
137 template <typename C, typename T>
138 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) {
139 int loc = get_prog_attribute_location(ctx, name);
140 int loc_ref = get_prog_attribute_location(ctx, name_ref);
141 if(loc < 0 || loc_ref < 0)
142 return false;
143 if (aam_ptr)
144 return aam_ptr->ref_composed_attribute_array(ctx, loc, loc_ref, array_ptr, nr_elements, elem);
145 enabled_attribute_arrays.insert(loc);
146 return attribute_array_binding::set_global_attribute_array(ctx, loc, &elem, nr_elements, sizeof(C));
147 }
148 bool remove_attribute_array(const context& ctx, const std::string& name);
149 public:
151 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);
153 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);
155 renderer();
157 virtual ~renderer();
159 void manage_singleton(context& ctx, const std::string& renderer_name, int& ref_count, int ref_count_change);
161 virtual void enable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
163 virtual void disable_attribute_array_manager(const context& ctx, attribute_array_manager& aam);
165 DEPRECATED("deprecated, use enable_attribute_array_manager() paired with disable_attribute_manager instead().")
166 virtual void set_attribute_array_manager(const context& ctx, attribute_array_manager* _aam_ptr = 0);
168 void set_render_style(const render_style& rs);
170 bool build_program(context& ctx, shader_program& prog, const render_style& rs);
172
175 virtual bool init(context& ctx);
177 template <typename T>
178 void set_position(const context& ctx, const T& position) { has_positions = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "position"), position); }
180 template <typename T>
181 void set_position_array(const context& ctx, const std::vector<T>& positions) { has_positions = true; set_attribute_array(ctx, "position", positions); }
183 template <typename T>
184 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); }
186 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);
188 template <typename T>
189 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); }
191 void remove_position_array(const context& ctx);
193 template <typename T>
194 void set_color(const context& ctx, const T& color) { has_colors = true; ref_prog().set_attribute(ctx, get_prog_attribute_location(ctx, "color"), color); }
196 template <typename T>
197 void set_color_array(const context& ctx, const std::vector<T>& colors) { has_colors = true; set_attribute_array(ctx, "color", colors); }
199 template <typename T>
200 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); }
202 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);
204 template <typename T>
205 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); }
207 void remove_color_array(const context& ctx);
218 template <typename T>
219 bool set_indices(const context& ctx, const std::vector<T>& indices, bool keep_on_cpu = false) {
220 this->indices = &indices.front();
221 index_buffer_ptr = 0;
222 index_count = indices.size();
224 if (!keep_on_cpu && aam_ptr)
225 return aam_ptr->set_indices(ctx, indices);
226 return true;
227 }
239 template <typename T>
240 bool set_indices(const context& ctx, const T* indices, size_t nr_indices, bool keep_on_cpu = false) {
241 this->indices = indices;
242 index_buffer_ptr = 0;
243 index_count = nr_indices;
245 if (!keep_on_cpu && aam_ptr)
246 return aam_ptr->set_indices(ctx, indices, nr_indices);
247 return true;
248 }
250
260 template <typename T>
261 bool set_indices(const context& ctx, const vertex_buffer& vbo, size_t count) {
262 index_buffer_ptr = &vbo;
263 indices = 0;
264 index_count = count;
266 if (aam_ptr)
267 aam_ptr->remove_indices(ctx);
268 return true;
269 }
271 bool has_indices() const {
272 if(aam_ptr && aam_ptr->has_index_buffer())
273 return true;
274 return index_count > 0;
275 }
277 void remove_indices(const context& ctx);
281 const vertex_buffer* get_vertex_buffer_ptr(const context& ctx, const attribute_array_manager& aam, const std::string& attr_name) {
282 return aam.get_buffer_ptr(get_prog_attribute_location(ctx, attr_name));
283 }
288 return aam.get_buffer_ptr(-1);
289 }
291 virtual bool validate_attributes(const context& ctx) const;
293 bool validate_and_enable(context& ctx);
295
297 virtual bool enable(context& ctx);
299
310 virtual void draw(context& ctx, size_t start, size_t count,
311 bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
313 virtual bool disable(context& ctx);
315
323 virtual bool render(context& ctx, size_t start, size_t count,
324 bool use_strips = false, bool use_adjacency = false, uint32_t strip_restart_index = -1);
326 virtual void clear(const context& ctx);
327 };
328 }
329}
330
331#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 ...
bool has_attribute(const context &ctx, int loc) const
check whether the given attribute is available
base class for all drawables, which is independent of the used rendering API.
Definition context.h:627
abstract base class for all renderers that handles a shader program and position / color attribute
Definition renderer.h:23
shader_compile_options & ref_shader_options()
access to shader program compile options to update settings not handled by render style
Definition renderer.h:77
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:240
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:261
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
Definition renderer.h:62
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:138
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:72
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:127
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:219
shader_program & ref_prog()
derived renderer classes have access to shader program
Definition renderer.h:79
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:189
bool has_aam() const
check for attribute array manager
Definition renderer.h:60
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:194
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:200
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:181
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:197
const vertex_buffer * get_vertex_buffer_ptr(const context &ctx, const attribute_array_manager &aam, const std::string &attr_name)
Definition renderer.h:281
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:184
const vertex_buffer * get_index_buffer_ptr(const attribute_array_manager &aam)
Definition renderer.h:287
bool attributes_persist() const
return whether attributes persist after a call to disable
Definition renderer.h:70
bool has_indices() const
return whether indices have been defined
Definition renderer.h:271
const T & get_style() const
access to style
Definition renderer.h:88
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:205
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:234
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:56
template with a static member function get_id() of type TypeId returning the TypeId of the template a...
Definition type_id.h:86