2#include "gl/gl_tools.h"
6 render_style::~render_style()
15 switch (ref_count_change) {
19 ctx.
error(std::string(
"unable to initialize ") + renderer_name +
" singleton");
27 ctx.
error(std::string(
"attempt to decrease reference count of ") + renderer_name +
" singleton below 0");
34 ctx.
error(std::string(
"invalid change reference count outside {-1,0,1} for ") + renderer_name +
" singleton");
39 if (default_render_style)
40 delete default_render_style;
41 default_render_style =
nullptr;
46 if(!prog_name.empty())
53 return aam_ptr->
has_attribute(ctx, get_prog_attribute_location(ctx, name,
false));
70 aam_ptr = &default_aam;
81 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)
83 int loc = get_prog_attribute_location(ctx, name);
87 return aam_ptr->set_attribute_array(ctx, loc, element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
88 enabled_attribute_arrays.insert(loc);
91 bool renderer::remove_attribute_array(
const context& ctx,
const std::string& name)
93 int loc = get_prog_attribute_location(ctx, name);
97 aam_ptr->remove_attribute_array(ctx, loc);
100 enabled_attribute_arrays.erase(loc);
106 set_attribute_array(ctx,
"position", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
110 remove_attribute_array(ctx,
"position");
115 set_attribute_array(ctx,
"color", element_type, vbo, offset_in_bytes, nr_elements, stride_in_bytes);
119 remove_attribute_array(ctx,
"color");
126 aam_ptr->remove_indices(ctx);
127 index_buffer_ptr = 0;
130 index_type = cgv::type::info::TI_UNDEF;
136 ctx.
error(
"renderer::enable() position attribute not set");
164 if (default_render_style)
165 return default_render_style;
167 return default_render_style;
172 prog_ptr = &one_shot_prog;
178 default_aam.init(ctx);
180 aam_ptr = &default_aam;
183 if (!default_render_style)
187 rs = default_render_style;
189 if(prog_ptr == &prog) {
193 last_prog_options = prog_options;
195 return default_render_style != 0;
208 if(prog_ptr == &prog) {
210 if(prog_options != last_prog_options) {
218 if(current_prog_render_count < 10)
219 std::cerr <<
"Performance warning: shader is rebuild! Consider using multiple instances of the renderer." << std::endl;
220 current_prog_render_count = 0;
222 ++current_prog_render_count;
224 last_prog_options = prog_options;
226 bool res = ref_prog().enable(ctx);
228 res = aam_ptr->enable(ctx);
236 res = aam_ptr->disable(ctx);
239 for (
int loc : enabled_attribute_arrays)
240 res = default_aam.aab.disable_array(ctx, loc) && res;
243 for (
int loc : enabled_attribute_arrays)
244 res = attribute_array_binding::disable_global_array(ctx, loc) && res;
246 enabled_attribute_arrays.
clear();
248 has_positions =
false;
251 res = ref_prog().disable(ctx) && res;
255 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)
257 if (use_strips && has_indices()) {
258 glPrimitiveRestartIndex(strip_restart_index);
259 glEnable(GL_PRIMITIVE_RESTART);
261 if (type == PT_LINES) {
264 type = PT_LINE_STRIP_ADJACENCY;
266 type = PT_LINES_ADJACENCY;
269 type = PT_LINE_STRIP;
271 else if (type == PT_TRIANGLES)
274 type = PT_TRIANGLE_STRIP_ADJACENCY;
276 type = PT_TRIANGLES_ADJACENCY;
279 type = PT_TRIANGLE_STRIP;
281 GLenum pt = gl::map_to_gl(type);
283 bool aam_has_index_buffer = aam_ptr && aam_ptr->has_index_buffer();
284 bool use_index_buffer = index_buffer_ptr && !aam_has_index_buffer;
285 bool use_indices = !index_buffer_ptr && !aam_has_index_buffer;
286 if (use_index_buffer)
288 const void* offset =
reinterpret_cast<const uint8_t*
>(use_indices ? indices : 0)
290 glDrawElements(pt, (GLsizei)count, gl::map_to_gl(index_type), offset);
291 if (use_index_buffer)
292 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
295 glDrawArrays(pt, (GLint)start, (GLsizei)count);
297 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)
299 if (use_strips && has_indices()) {
300 glPrimitiveRestartIndex(strip_restart_index);
301 glEnable(GL_PRIMITIVE_RESTART);
303 if (type == PT_LINES) {
306 type = PT_LINE_STRIP_ADJACENCY;
308 type = PT_LINES_ADJACENCY;
311 type = PT_LINE_STRIP;
313 else if (type == PT_TRIANGLES)
316 type = PT_TRIANGLE_STRIP_ADJACENCY;
318 type = PT_TRIANGLES_ADJACENCY;
321 type = PT_TRIANGLE_STRIP;
323 GLenum pt = gl::map_to_gl(type);
325 bool aam_has_index_buffer = aam_ptr && aam_ptr->has_index_buffer();
326 bool use_index_buffer = index_buffer_ptr && !aam_has_index_buffer;
327 bool use_indices = !index_buffer_ptr && !aam_has_index_buffer;
328 if (use_index_buffer)
330 const void* offset =
reinterpret_cast<const uint8_t*
>(use_indices ? indices : 0)
332 glDrawElementsInstanced(pt, (GLsizei)count, gl::map_to_gl(index_type), offset, (GLsizei)instance_count);
333 if (use_index_buffer)
334 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
337 glDrawArraysInstanced(pt, (GLint)start, (GLsizei)count, (GLsizei)instance_count);
339 void renderer::draw(
context& ctx,
size_t start,
size_t count,
340 bool use_strips,
bool use_adjacency, uint32_t strip_restart_index)
342 draw_impl(ctx, PT_TRIANGLES, start, count, use_strips, use_adjacency, strip_restart_index);
344 bool renderer::render(
context& ctx,
size_t start,
size_t count,
bool use_strips,
bool use_adjacency, uint32_t strip_restart_index)
346 if (!validate_and_enable(ctx))
348 draw(ctx, start, count, use_strips, use_adjacency, strip_restart_index);
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
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.
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
virtual bool is_created() const
return whether component has been created
void manage_singleton(context &ctx, const std::string &renderer_name, int &ref_count, int ref_count_change)
used by derived classes to manage singletons
void remove_color_array(const context &ctx)
remove the color attribute
bool has_attribute(const context &ctx, const std::string &name)
check for attribute
virtual bool enable(context &ctx)
enables renderer
bool validate_and_enable(context &ctx)
validate attributes and if successful, enable renderer
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 ...
virtual bool init(context &ctx)
call init() once before using renderer
virtual ~renderer()
destructor deletes default renderer style
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
bool build_program(context &ctx, shader_program &prog, const render_style &rs)
build shader program for specific render style
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
void remove_position_array(const context &ctx)
remove the position attribute
virtual void clear(const context &ctx)
the clear function destructs the shader program
renderer()
construct and init attribute tracking flags
bool has_colors
track whether color attribute is defined
bool has_positions
track whether position attribute is defined
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...
const render_style * get_style_ptr() const
access to render style
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
virtual bool validate_attributes(const context &ctx) const
call to validate, whether essential position attribute is defined
void set_render_style(const render_style &rs)
reference given render style
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
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...
void set_prog(shader_program &one_shot_prog)
set external shader program up to next call to disable() or render()
bool has_indices() const
return whether indices have been defined
void remove_indices(const context &ctx)
remove previously set indices
Stores preprocessor options used for conditionally compiling shader programs.
void clear()
Clear everything, i.e. remove all defines and snippets.
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
@ VBT_INDICES
The buffer contains indices and will be bound to GL_ELEMENT_ARRAY_BUFFER.
unsigned int get_type_size(TypeId tid)
function that returns the size of a type specified through TypeId
this header is dependency free
bool core_profile
default: true
base class for all render styles
compact type description of data that can be sent to the context; convertible to int