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():
27 index_type(UNSIGNED_SHORT)
29 static Require req(ARB_vertex_array_object);
30 if(ARB_direct_state_access)
31 glCreateVertexArrays(1, &id);
33 glGenVertexArrays(1, &id);
36 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");
72 dirty |= 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 dirty |= INSTANCE_ARRAY;
90 void VertexSetup::set_index_buffer(const Buffer &ibuf, DataType itype)
94 dirty |= INDEX_BUFFER;
97 bool VertexSetup::verify_format(const VertexFormat &fmt)
102 unsigned max_attribs = Limits::get_global().max_vertex_attributes;
104 for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
105 if(get_attribute_semantic(*a)>=max_attribs)
111 void VertexSetup::update() const
113 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
115 if(dirty&VERTEX_ARRAY)
116 update_vertex_array(*vertex_array, 0, 0, direct);
118 if(dirty&INSTANCE_ARRAY)
119 update_vertex_array(*inst_array, 1, 1, direct);
121 if(dirty&INDEX_BUFFER)
124 glVertexArrayElementBuffer(id, index_buffer->get_id());
126 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
132 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
135 glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
137 const VertexFormat &fmt = array.get_format();
138 unsigned stride = fmt.stride();
141 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
142 glVertexArrayBindingDivisor(id, binding, divisor);
146 for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
148 unsigned sem = get_attribute_semantic(*a);
149 GLenum type = get_gl_type(get_attribute_source_type(*a));
150 unsigned cc = get_attribute_component_count(*a);
153 glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
154 glVertexArrayAttribBinding(id, sem, binding);
155 glEnableVertexArrayAttrib(id, sem);
159 glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
160 if(ARB_instanced_arrays)
161 glVertexAttribDivisor(sem, divisor);
162 glEnableVertexAttribArray(sem);
164 offset += get_attribute_size(*a);
168 glBindBuffer(GL_ARRAY_BUFFER, 0);
171 void VertexSetup::unload()
173 if(ARB_direct_state_access)
175 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
176 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
177 glVertexArrayElementBuffer(id, 0);
181 glBindVertexArray(id);
182 glBindBuffer(GL_ARRAY_BUFFER, 0);
184 for(const UInt16 *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
186 unsigned sem = get_attribute_semantic(*a);
187 glDisableVertexAttribArray(sem);
188 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
190 for(const UInt16 *a=inst_format.begin(); a!=inst_format.end(); ++a)
192 unsigned sem = get_attribute_semantic(*a);
193 glDisableVertexAttribArray(sem);
194 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
197 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
201 vertex_format = VertexFormat();
203 inst_format = VertexFormat();
207 void VertexSetup::set_debug_name(const string &name)
211 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());