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_format(const VertexFormat &vfmt)
43 if(!verify_format(vfmt))
44 throw invalid_argument("VertexSetup::set_format");
45 if(!vertex_format.empty())
46 throw invalid_operation("VertexSetup::set_format");
51 void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
53 if(!verify_format(vfmt) || !verify_format(ifmt))
54 throw invalid_argument("VertexSetup::set_format");
55 if(!vertex_format.empty())
56 throw invalid_operation("VertexSetup::set_format");
62 void VertexSetup::set_vertex_array(const VertexArray &a)
64 if(vertex_format.empty())
65 throw invalid_operation("VertexSetup::set_vertex_array");
66 if(a.get_format()!=vertex_format)
67 throw incompatible_data("VertexSetup::set_vertex_array");
69 throw invalid_argument("VertexSetup::set_vertex_array");
75 void VertexSetup::set_instance_array(const VertexArray &a)
77 if(inst_format.empty())
78 throw invalid_operation("VertexSetup::set_instance_array");
79 if(a.get_format()!=inst_format)
80 throw incompatible_data("VertexSetup::set_instance_array");
82 throw invalid_argument("VertexSetup::set_instance_array");
84 static Require req(ARB_instanced_arrays);
87 update(INSTANCE_ARRAY);
90 void VertexSetup::set_index_buffer(const Buffer &ibuf)
96 bool VertexSetup::verify_format(const VertexFormat &fmt)
101 static int max_attribs = -1;
103 max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
105 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
106 if(static_cast<int>(get_attribute_semantic(*a))>=max_attribs)
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&VERTEX_ARRAY)
122 update_vertex_array(*vertex_array, 0, 0, direct);
124 if((mask&INSTANCE_ARRAY) && inst_array)
125 update_vertex_array(*inst_array, 1, 1, direct);
127 if(mask&INDEX_BUFFER)
130 glVertexArrayElementBuffer(id, index_buffer->get_id());
132 glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
136 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
138 Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
140 const VertexFormat &fmt = array.get_format();
141 unsigned stride = fmt.stride()*sizeof(float);
144 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
145 glVertexArrayBindingDivisor(id, binding, divisor);
149 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
151 unsigned sem = get_attribute_semantic(*a);
152 unsigned sz = get_attribute_size(*a);
156 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
158 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
159 glVertexArrayAttribBinding(id, sem, binding);
160 glEnableVertexArrayAttrib(id, sem);
165 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
167 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
168 if(ARB_instanced_arrays)
169 glVertexAttribDivisor(sem, divisor);
170 glEnableVertexAttribArray(sem);
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);
201 void VertexSetup::unload()
203 if(ARB_direct_state_access)
205 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
206 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
207 glVertexArrayElementBuffer(id, 0);
211 BindRestore _bind(*this);
212 Buffer::unbind_from(ARRAY_BUFFER);
214 for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
216 unsigned sem = get_attribute_semantic(*a);
217 glDisableVertexAttribArray(sem);
218 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
220 for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a)
222 unsigned sem = get_attribute_semantic(*a);
223 glDisableVertexAttribArray(sem);
224 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
227 glBindBuffer(ELEMENT_ARRAY_BUFFER, 0);
231 vertex_format = VertexFormat();
233 inst_format = VertexFormat();
237 void VertexSetup::set_debug_name(const string &name)
241 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());