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>
8 #include <msp/gl/extensions/khr_debug.h>
13 #include "vertexarray.h"
14 #include "vertexsetup.h"
21 VertexSetup::VertexSetup():
27 static Require req(ARB_vertex_array_object);
28 if(ARB_direct_state_access)
29 glCreateVertexArrays(1, &id);
31 glGenVertexArrays(1, &id);
34 VertexSetup::~VertexSetup()
38 glDeleteVertexArrays(1, &id);
41 void VertexSetup::set_vertex_array(const VertexArray &a)
44 throw invalid_argument("VertexSetup::set_vertex_array");
47 update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
48 vertex_format = vertex_array->get_format();
51 void VertexSetup::set_instance_array(const VertexArray *a)
56 throw invalid_argument("VertexSetup::set_instance_array");
58 static Require req(ARB_instanced_arrays);
62 update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
63 inst_format = inst_array->get_format();
66 void VertexSetup::set_index_buffer(const Buffer &ibuf)
72 void VertexSetup::refresh()
74 if(vertex_array && vertex_array->get_format()!=vertex_format)
75 set_vertex_array(*vertex_array);
77 if(inst_array && inst_array->get_format()!=inst_format)
78 set_instance_array(inst_array);
81 bool VertexSetup::verify_array(const VertexArray &array)
83 if(!array.get_buffer())
86 static int max_attribs = -1;
88 max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
90 const VertexFormat &fmt = array.get_format();
91 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
92 if(static_cast<int>(get_attribute_semantic(*a))>=max_attribs)
98 unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
101 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
102 mask |= 1<<get_attribute_semantic(*a);
106 unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
108 unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
109 return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
112 void VertexSetup::update(unsigned mask) const
114 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
115 if(!direct && current()!=this)
121 if(mask&UNUSED_ATTRIBS)
123 for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
127 glDisableVertexArrayAttrib(id, i);
129 glDisableVertexAttribArray(i);
133 if(mask&VERTEX_ARRAY)
134 update_vertex_array(*vertex_array, 0, 0, direct);
136 if((mask&INSTANCE_ARRAY) && inst_array)
137 update_vertex_array(*inst_array, 1, 1, direct);
139 if(mask&INDEX_BUFFER)
142 glVertexArrayElementBuffer(id, index_buffer->get_id());
144 glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
148 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
150 Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
152 const VertexFormat &fmt = array.get_format();
153 unsigned stride = fmt.stride()*sizeof(float);
156 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
157 glVertexArrayBindingDivisor(id, binding, divisor);
161 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
163 unsigned sem = get_attribute_semantic(*a);
164 unsigned sz = get_attribute_size(*a);
168 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
170 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
171 glVertexArrayAttribBinding(id, sem, binding);
172 glEnableVertexArrayAttrib(id, sem);
177 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
179 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
180 if(ARB_instanced_arrays)
181 glVertexAttribDivisor(sem, divisor);
182 glEnableVertexAttribArray(sem);
184 offset += sz*sizeof(float);
188 void VertexSetup::bind() const
190 if(!vertex_array || !index_buffer)
191 throw invalid_operation("VertexSetup::bind");
193 if(set_current(this))
195 vertex_array->refresh();
197 inst_array->refresh();
198 glBindVertexArray(id);
207 void VertexSetup::unbind()
210 glBindVertexArray(0);
213 void VertexSetup::set_debug_name(const string &name)
217 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());