cgv
Loading...
Searching...
No Matches
renderer.cxx
1#include "renderer.h"
2#include "gl/gl_tools.h"
3
4namespace cgv {
5 namespace render {
6 render_style::~render_style()
7 {
8 }
10 void renderer::manage_singleton(context& ctx, const std::string& renderer_name, int& ref_count, int ref_count_change)
11 {
12 switch (ref_count_change) {
13 case 1:
14 if (ref_count == 0) {
15 if (!init(ctx))
16 ctx.error(std::string("unable to initialize ") + renderer_name + " singleton");
17 }
18 ++ref_count;
19 break;
20 case 0:
21 break;
22 case -1:
23 if (ref_count == 0)
24 ctx.error(std::string("attempt to decrease reference count of ") + renderer_name + " singleton below 0");
25 else {
26 if (--ref_count == 0)
27 clear(ctx);
28 }
29 break;
30 default:
31 ctx.error(std::string("invalid change reference count outside {-1,0,1} for ") + renderer_name + " singleton");
32 }
33 }
35 {
36 if (default_render_style)
37 delete default_render_style;
38 default_render_style = nullptr;
39 }
41 {
42 std::string prog_name = get_default_prog_name();
43 if(!prog_name.empty())
44 return prog.build_program(ctx, prog_name, options, true);
45 return false;
46 }
49 {
50 aam_ptr = &aam;
51 if (has_attribute(ctx, "position"))
52 has_positions = true;
53 if (has_attribute(ctx, "color"))
54 has_colors = true;
55 }
58 {
59 has_positions = false;
60 has_colors = false;
61 if (ctx.core_profile)
62 aam_ptr = &default_aam;
63 else
64 aam_ptr = 0;
65 }
67 {
68 if (_aam_ptr)
69 enable_attribute_array_manager(ctx, *_aam_ptr);
70 else
72 }
73 bool renderer::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)
74 {
75 int loc = get_prog_attribute_location(ctx, name);
76 if(loc < 0)
77 return false;
78 if (aam_ptr)
79 return aam_ptr->set_attribute_array(ctx, loc, element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
80 enabled_attribute_arrays.insert(loc);
81 return attribute_array_binding::set_global_attribute_array(ctx, loc, element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
82 }
83 bool renderer::remove_attribute_array(const context& ctx, const std::string& name)
84 {
85 int loc = get_prog_attribute_location(ctx, name);
86 if(loc < 0)
87 return false;
88 if(aam_ptr) {
89 aam_ptr->remove_attribute_array(ctx, loc);
90 return true;
91 }
92 enabled_attribute_arrays.erase(loc);
93 return true;
94 }
95 void renderer::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)
96 {
97 has_positions = true;
98 set_attribute_array(ctx, "position", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
99 }
101 has_positions = false;
102 remove_attribute_array(ctx, "position");
103 }
104 void renderer::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)
105 {
106 has_colors = true;
107 set_attribute_array(ctx, "color", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
108 }
110 has_colors = false;
111 remove_attribute_array(ctx, "color");
112 }
114 {
115 if (!has_indices())
116 return;
117 if (aam_ptr)
118 aam_ptr->remove_indices(ctx);
119 index_buffer_ptr = 0;
120 indices = 0;
121 index_count = 0;
122 index_type = cgv::type::info::TI_UNDEF;
123 }
125 {
126 // validate set attributes
127 if (!has_positions) {
128 ctx.error("renderer::enable() position attribute not set");
129 return false;
130 }
131 return true;
132 }
135 {
136 rs = &_rs;
137 }
138
141 {
142 auto* tmp = rs;
143 rs = &_rs;
146 bool res = build_shader_program(ctx, prog, options);
147 rs = tmp;
148 return res;
149 }
150
153 {
154 if (rs)
155 return rs;
156 if (default_render_style)
157 return default_render_style;
158 default_render_style = create_render_style();
159 return default_render_style;
160 }
163 {
164 prog_ptr = &one_shot_prog;
165 }
166
168 {
169 if (ctx.core_profile) {
170 default_aam.init(ctx);
171 if (!aam_ptr)
172 aam_ptr = &default_aam;
173 }
174
175 if (!default_render_style)
176 default_render_style = create_render_style();
177
178 if (!rs)
179 rs = default_render_style;
180
181 if(prog_ptr == &prog) {
182 update_shader_program_options(prog_options);
183 if(!build_shader_program(ctx, prog, prog_options))
184 return false;
185 last_prog_options = prog_options;
186 }
187 return default_render_style != 0;
188 }
189
192 {
193 if (validate_attributes(ctx))
194 return enable(ctx);
195 return false;
196 }
197
199 {
200 if(prog_ptr == &prog) {
201 update_shader_program_options(prog_options);
202 if(prog_options != last_prog_options) {
203 if(prog.is_created())
204 prog.destruct(ctx);
205 if(!build_shader_program(ctx, prog, prog_options))
206 return false;
207#ifndef _DEBUG
208 }
209#else
210 if(current_prog_render_count < 10)
211 std::cerr << "Performance warning: shader is rebuild! Consider using multiple instances of the renderer." << std::endl;
212 current_prog_render_count = 0;
213 }
214 ++current_prog_render_count;
215#endif
216 last_prog_options = prog_options;
217 }
218 bool res = ref_prog().enable(ctx);
219 if (aam_ptr)
220 res = aam_ptr->enable(ctx);
221 return res;
222 }
223
224 bool renderer::disable(context& ctx)
225 {
226 bool res = true;
227 if (has_aam())
228 res = aam_ptr->disable(ctx);
229 else {
230 if (ctx.core_profile) {
231 default_aam.disable(ctx);
232 for (int loc : enabled_attribute_arrays)
233 res = default_aam.aab.disable_array(ctx, loc) && res;
234 }
235 else {
236 for (int loc : enabled_attribute_arrays)
237 res = attribute_array_binding::disable_global_array(ctx, loc) && res;
238 }
239 enabled_attribute_arrays.clear();
240 has_colors = false;
241 has_positions = false;
242 index_count = 0;
243 }
244 res = ref_prog().disable(ctx) && res;
245 prog_ptr = &prog;
246 return res;
247 }
248 void renderer::draw_impl(context& ctx, PrimitiveType type, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index)
249 {
250 if (use_strips && has_indices()) {
251 glPrimitiveRestartIndex(strip_restart_index);
252 glEnable(GL_PRIMITIVE_RESTART);
253 }
254 if (type == PT_LINES) {
255 if (use_adjacency)
256 if (use_strips)
257 type = PT_LINE_STRIP_ADJACENCY;
258 else
259 type = PT_LINES_ADJACENCY;
260 else
261 if (use_strips)
262 type = PT_LINE_STRIP;
263 }
264 else if (type == PT_TRIANGLES)
265 if (use_adjacency)
266 if (use_strips)
267 type = PT_TRIANGLE_STRIP_ADJACENCY;
268 else
269 type = PT_TRIANGLES_ADJACENCY;
270 else
271 if (use_strips)
272 type = PT_TRIANGLE_STRIP;
273
274 GLenum pt = gl::map_to_gl(type);
275 if (has_indices()) {
276 bool aam_has_index_buffer = aam_ptr && aam_ptr->has_index_buffer();
277 bool use_index_buffer = index_buffer_ptr && !aam_has_index_buffer;
278 bool use_indices = !index_buffer_ptr && !aam_has_index_buffer;
279 if (use_index_buffer)
280 index_buffer_ptr->bind(ctx, VBT_INDICES);
281 const void* offset = reinterpret_cast<const uint8_t*>(use_indices ? indices : 0)
282 + start * cgv::type::info::get_type_size(index_type);
283 glDrawElements(pt, (GLsizei)count, gl::map_to_gl(index_type), offset);
284 if (use_index_buffer)
285 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
286 }
287 else
288 glDrawArrays(pt, (GLint)start, (GLsizei)count);
289 }
290 void renderer::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)
291 {
292 if (use_strips && has_indices()) {
293 glPrimitiveRestartIndex(strip_restart_index);
294 glEnable(GL_PRIMITIVE_RESTART);
295 }
296 if (type == PT_LINES) {
297 if (use_adjacency)
298 if (use_strips)
299 type = PT_LINE_STRIP_ADJACENCY;
300 else
301 type = PT_LINES_ADJACENCY;
302 else
303 if (use_strips)
304 type = PT_LINE_STRIP;
305 }
306 else if (type == PT_TRIANGLES)
307 if (use_adjacency)
308 if (use_strips)
309 type = PT_TRIANGLE_STRIP_ADJACENCY;
310 else
311 type = PT_TRIANGLES_ADJACENCY;
312 else
313 if (use_strips)
314 type = PT_TRIANGLE_STRIP;
315
316 GLenum pt = gl::map_to_gl(type);
317 if (has_indices()) {
318 bool aam_has_index_buffer = aam_ptr && aam_ptr->has_index_buffer();
319 bool use_index_buffer = index_buffer_ptr && !aam_has_index_buffer;
320 bool use_indices = !index_buffer_ptr && !aam_has_index_buffer;
321 if (use_index_buffer)
322 index_buffer_ptr->bind(ctx, VBT_INDICES);
323 const void* offset = reinterpret_cast<const uint8_t*>(use_indices ? indices : 0)
324 + start * cgv::type::info::get_type_size(index_type);
325 glDrawElementsInstanced(pt, (GLsizei)count, gl::map_to_gl(index_type), offset, (GLsizei)instance_count);
326 if (use_index_buffer)
327 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
328 }
329 else
330 glDrawArraysInstanced(pt, (GLint)start, (GLsizei)count, (GLsizei)instance_count);
331 }
332 void renderer::draw(context& ctx, size_t start, size_t count,
333 bool use_strips, bool use_adjacency, uint32_t strip_restart_index)
334 {
335 draw_impl(ctx, PT_TRIANGLES, start, count, use_strips, use_adjacency, strip_restart_index);
336 }
337 bool renderer::render(context& ctx, size_t start, size_t count, bool use_strips, bool use_adjacency, uint32_t strip_restart_index)
338 {
339 if (!validate_and_enable(ctx))
340 return false;
341 draw(ctx, start, count, use_strips, use_adjacency, strip_restart_index);
342 return disable(ctx);
343 }
344 void renderer::clear(const cgv::render::context& ctx)
345 {
346 prog.destruct(ctx);
347 }
348 }
349}
350
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...
attribute array manager used to upload arrays to gpu
base class for all drawables, which is independent of the used rendering API.
Definition context.h:627
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
Definition context.cxx:219
virtual bool is_created() const
return whether component has been created
Definition context.cxx:2065
void manage_singleton(context &ctx, const std::string &renderer_name, int &ref_count, int ref_count_change)
used by derived classes to manage singletons
Definition renderer.cxx:10
void remove_color_array(const context &ctx)
remove the color attribute
Definition renderer.cxx:109
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
Definition renderer.h:62
virtual bool enable(context &ctx)
enables renderer
Definition renderer.cxx:198
bool validate_and_enable(context &ctx)
validate attributes and if successful, enable renderer
Definition renderer.cxx:191
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
virtual bool init(context &ctx)
call init() once before using renderer
Definition renderer.cxx:167
virtual ~renderer()
destructor deletes default renderer style
Definition renderer.cxx:34
virtual bool build_shader_program(context &ctx, shader_program &prog, const shader_compile_options &options) const
overload to change default behaviour and build a custom shader program based on the passed options
Definition renderer.cxx:40
bool build_program(context &ctx, shader_program &prog, const render_style &rs)
build shader program for specific render style
Definition renderer.cxx:140
virtual void enable_attribute_array_manager(const context &ctx, attribute_array_manager &aam)
call this before setting attribute arrays to manage attribute array in given manager
Definition renderer.cxx:48
void remove_position_array(const context &ctx)
remove the position attribute
Definition renderer.cxx:100
virtual void clear(const context &ctx)
the clear function destructs the shader program
Definition renderer.cxx:344
renderer()
construct and init attribute tracking flags
Definition renderer.cxx:9
bool has_colors
track whether color attribute is defined
Definition renderer.h:90
bool has_positions
track whether position attribute is defined
Definition renderer.h:92
virtual void disable_attribute_array_manager(const context &ctx, attribute_array_manager &aam)
call this after last render/draw call to ensure that no other users of renderer change attribute arra...
Definition renderer.cxx:57
const render_style * get_style_ptr() const
access to render style
Definition renderer.cxx:152
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 bool validate_attributes(const context &ctx) const
call to validate, whether essential position attribute is defined
Definition renderer.cxx:124
void set_render_style(const render_style &rs)
reference given render style
Definition renderer.cxx:134
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
virtual void set_attribute_array_manager(const context &ctx, attribute_array_manager *_aam_ptr=0)
this function is deprecated, please use enable_attribute_array_manager() and disable_attribute_manage...
Definition renderer.cxx:66
void set_prog(shader_program &one_shot_prog)
set external shader program up to next call to disable() or render()
Definition renderer.cxx:162
bool has_indices() const
return whether indices have been defined
Definition renderer.h:271
void remove_indices(const context &ctx)
remove previously set indices
Definition renderer.cxx:113
Stores preprocessor options used for conditionally compiling shader programs.
Definition shader_code.h:73
void clear()
Clear everything, i.e. remove all defines and snippets.
Definition shader_code.h:83
a shader program combines several shader code fragments to a complete definition of the shading pipel...
void destruct(const context &ctx)
destruct shader program
bool build_program(const context &ctx, const std::string &file_name, bool show_error=false)
successively calls create, attach_program and link.
a vertex buffer is an unstructured memory block on the GPU.
PrimitiveType
different primitive types
Definition context.h:234
@ VBT_INDICES
The buffer contains indices and will be bound to GL_ELEMENT_ARRAY_BUFFER.
Definition context.h:428
unsigned int get_type_size(TypeId tid)
function that returns the size of a type specified through TypeId
Definition type_id.cxx:18
the cgv namespace
Definition print.h:11
bool core_profile
default: true
Definition context.h:566
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