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>
10 #include "deviceinfo.h"
14 #include "vertexarray.h"
15 #include "vertexsetup.h"
22 VertexSetup::VertexSetup():
28 static Require req(ARB_vertex_array_object);
29 if(ARB_direct_state_access)
30 glCreateVertexArrays(1, &id);
32 glGenVertexArrays(1, &id);
35 VertexSetup::~VertexSetup()
39 glDeleteVertexArrays(1, &id);
42 void VertexSetup::set_format(const VertexFormat &vfmt)
44 if(!verify_format(vfmt))
45 throw invalid_argument("VertexSetup::set_format");
46 if(!vertex_format.empty())
47 throw invalid_operation("VertexSetup::set_format");
52 void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
54 if(!verify_format(vfmt) || !verify_format(ifmt))
55 throw invalid_argument("VertexSetup::set_format");
56 if(!vertex_format.empty())
57 throw invalid_operation("VertexSetup::set_format");
63 void VertexSetup::set_vertex_array(const VertexArray &a)
65 if(vertex_format.empty())
66 throw invalid_operation("VertexSetup::set_vertex_array");
67 if(a.get_format()!=vertex_format)
68 throw incompatible_data("VertexSetup::set_vertex_array");
70 throw invalid_argument("VertexSetup::set_vertex_array");
76 void VertexSetup::set_instance_array(const VertexArray &a)
78 if(inst_format.empty())
79 throw invalid_operation("VertexSetup::set_instance_array");
80 if(a.get_format()!=inst_format)
81 throw incompatible_data("VertexSetup::set_instance_array");
83 throw invalid_argument("VertexSetup::set_instance_array");
85 static Require req(ARB_instanced_arrays);
88 update(INSTANCE_ARRAY);
91 void VertexSetup::set_index_buffer(const Buffer &ibuf)
97 bool VertexSetup::verify_format(const VertexFormat &fmt)
102 unsigned max_attribs = Limits::get_global().max_vertex_attributes;
104 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
105 if(get_attribute_semantic(*a)>=max_attribs)
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&VERTEX_ARRAY)
121 update_vertex_array(*vertex_array, 0, 0, direct);
123 if((mask&INSTANCE_ARRAY) && inst_array)
124 update_vertex_array(*inst_array, 1, 1, direct);
126 if(mask&INDEX_BUFFER)
129 glVertexArrayElementBuffer(id, index_buffer->get_id());
131 glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
135 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
137 Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
139 const VertexFormat &fmt = array.get_format();
140 unsigned stride = fmt.stride()*sizeof(float);
143 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
144 glVertexArrayBindingDivisor(id, binding, divisor);
148 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
150 unsigned sem = get_attribute_semantic(*a);
151 unsigned sz = get_attribute_size(*a);
155 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
157 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
158 glVertexArrayAttribBinding(id, sem, binding);
159 glEnableVertexArrayAttrib(id, sem);
164 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
166 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
167 if(ARB_instanced_arrays)
168 glVertexAttribDivisor(sem, divisor);
169 glEnableVertexAttribArray(sem);
171 offset += sz*sizeof(float);
175 void VertexSetup::bind() const
177 if(!vertex_array || !index_buffer)
178 throw invalid_operation("VertexSetup::bind");
180 if(set_current(this))
182 vertex_array->refresh();
184 inst_array->refresh();
185 glBindVertexArray(id);
194 void VertexSetup::unbind()
197 glBindVertexArray(0);
200 void VertexSetup::unload()
202 if(ARB_direct_state_access)
204 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
205 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
206 glVertexArrayElementBuffer(id, 0);
210 BindRestore _bind(*this);
211 Buffer::unbind_from(ARRAY_BUFFER);
213 for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
215 unsigned sem = get_attribute_semantic(*a);
216 glDisableVertexAttribArray(sem);
217 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
219 for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a)
221 unsigned sem = get_attribute_semantic(*a);
222 glDisableVertexAttribArray(sem);
223 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
226 glBindBuffer(ELEMENT_ARRAY_BUFFER, 0);
230 vertex_format = VertexFormat();
232 inst_format = VertexFormat();
236 void VertexSetup::set_debug_name(const string &name)
240 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());