1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_instanced_arrays.h>
4 #include <msp/gl/extensions/arb_vertex_array_object.h>
5 #include <msp/gl/extensions/arb_vertex_attrib_binding.h>
6 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
7 #include <msp/gl/extensions/arb_vertex_shader.h>
11 #include "vertexarray.h"
12 #include "vertexsetup.h"
19 VertexSetup::VertexSetup():
25 static Require req(ARB_vertex_array_object);
26 if(ARB_direct_state_access)
27 glCreateVertexArrays(1, &id);
29 glGenVertexArrays(1, &id);
32 VertexSetup::~VertexSetup()
36 glDeleteVertexArrays(1, &id);
39 void VertexSetup::set_vertex_array(const VertexArray &a)
42 throw invalid_argument("VertexSetup::set_vertex_array");
45 update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
46 vertex_format = vertex_array->get_format();
49 void VertexSetup::set_instance_array(const VertexArray *a)
54 throw invalid_argument("VertexSetup::set_instance_array");
56 static Require req(ARB_instanced_arrays);
60 update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
61 inst_format = inst_array->get_format();
64 void VertexSetup::set_index_buffer(const Buffer &ibuf)
70 void VertexSetup::refresh()
72 if(vertex_array && vertex_array->get_format()!=vertex_format)
73 set_vertex_array(*vertex_array);
75 if(inst_array && inst_array->get_format()!=inst_format)
76 set_instance_array(inst_array);
79 unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
82 for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
84 unsigned t = get_component_type(*c);
85 if(t>=get_component_type(ATTRIB1))
86 t -= get_component_type(ATTRIB1);
92 unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
94 unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
95 return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
98 void VertexSetup::update(unsigned mask) const
100 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
101 if(!direct && current()!=this)
107 if(mask&UNUSED_ATTRIBS)
109 for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
113 glDisableVertexArrayAttrib(id, i);
115 glDisableVertexAttribArray(i);
119 if(mask&VERTEX_ARRAY)
120 update_vertex_array(*vertex_array, 0, 0, direct);
122 if((mask&INSTANCE_ARRAY) && inst_array)
123 update_vertex_array(*inst_array, 1, 1, direct);
125 if(mask&INDEX_BUFFER)
128 glVertexArrayElementBuffer(id, index_buffer->get_id());
130 glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
134 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
136 Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
138 const VertexFormat &fmt = array.get_format();
139 unsigned stride = get_stride(fmt)*sizeof(float);
142 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
143 glVertexArrayBindingDivisor(id, binding, divisor);
147 for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
149 unsigned t = get_component_type(*c);
150 if(t>=get_component_type(ATTRIB1))
151 t -= get_component_type(ATTRIB1);
152 unsigned sz = get_component_size(*c);
156 glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset);
158 glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset);
159 glVertexArrayAttribBinding(id, t, binding);
160 glEnableVertexArrayAttrib(id, t);
165 glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
167 glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
168 if(ARB_instanced_arrays)
169 glVertexAttribDivisor(t, divisor);
170 glEnableVertexAttribArray(t);
172 offset += sz*sizeof(float);
176 void VertexSetup::bind() const
178 if(!vertex_array || !index_buffer)
179 throw invalid_operation("VertexSetup::bind");
181 if(set_current(this))
183 vertex_array->refresh();
185 inst_array->refresh();
186 glBindVertexArray(id);
195 void VertexSetup::unbind()
198 glBindVertexArray(0);