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>
12 #include "vertexarray.h"
13 #include "vertexsetup.h"
20 VertexSetup::VertexSetup():
26 static Require req(ARB_vertex_array_object);
27 if(ARB_direct_state_access)
28 glCreateVertexArrays(1, &id);
30 glGenVertexArrays(1, &id);
33 VertexSetup::~VertexSetup()
37 glDeleteVertexArrays(1, &id);
40 void VertexSetup::set_vertex_array(const VertexArray &a)
43 throw invalid_argument("VertexSetup::set_vertex_array");
46 update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
47 vertex_format = vertex_array->get_format();
50 void VertexSetup::set_instance_array(const VertexArray *a)
55 throw invalid_argument("VertexSetup::set_instance_array");
57 static Require req(ARB_instanced_arrays);
61 update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
62 inst_format = inst_array->get_format();
65 void VertexSetup::set_index_buffer(const Buffer &ibuf)
71 void VertexSetup::refresh()
73 if(vertex_array && vertex_array->get_format()!=vertex_format)
74 set_vertex_array(*vertex_array);
76 if(inst_array && inst_array->get_format()!=inst_format)
77 set_instance_array(inst_array);
80 bool VertexSetup::verify_array(const VertexArray &array)
82 if(!array.get_buffer())
85 static int max_attribs = -1;
87 max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
89 const VertexFormat &fmt = array.get_format();
90 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
91 if(static_cast<int>(get_attribute_semantic(*a))>=max_attribs)
97 unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
100 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
101 mask |= 1<<get_attribute_semantic(*a);
105 unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
107 unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
108 return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
111 void VertexSetup::update(unsigned mask) const
113 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
114 if(!direct && current()!=this)
120 if(mask&UNUSED_ATTRIBS)
122 for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
126 glDisableVertexArrayAttrib(id, i);
128 glDisableVertexAttribArray(i);
132 if(mask&VERTEX_ARRAY)
133 update_vertex_array(*vertex_array, 0, 0, direct);
135 if((mask&INSTANCE_ARRAY) && inst_array)
136 update_vertex_array(*inst_array, 1, 1, direct);
138 if(mask&INDEX_BUFFER)
141 glVertexArrayElementBuffer(id, index_buffer->get_id());
143 glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
147 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
149 Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
151 const VertexFormat &fmt = array.get_format();
152 unsigned stride = fmt.stride()*sizeof(float);
155 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
156 glVertexArrayBindingDivisor(id, binding, divisor);
160 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
162 unsigned sem = get_attribute_semantic(*a);
163 unsigned sz = get_attribute_size(*a);
167 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
169 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
170 glVertexArrayAttribBinding(id, sem, binding);
171 glEnableVertexArrayAttrib(id, sem);
176 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
178 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
179 if(ARB_instanced_arrays)
180 glVertexAttribDivisor(sem, divisor);
181 glEnableVertexAttribArray(sem);
183 offset += sz*sizeof(float);
187 void VertexSetup::bind() const
189 if(!vertex_array || !index_buffer)
190 throw invalid_operation("VertexSetup::bind");
192 if(set_current(this))
194 vertex_array->refresh();
196 inst_array->refresh();
197 glBindVertexArray(id);
206 void VertexSetup::unbind()
209 glBindVertexArray(0);