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/ext_gpu_shader4.h>
9 #include <msp/gl/extensions/khr_debug.h>
11 #include "deviceinfo.h"
15 #include "vertexarray.h"
16 #include "vertexsetup.h"
23 VertexSetup::VertexSetup():
28 index_type(UNSIGNED_SHORT)
30 static Require req(ARB_vertex_array_object);
31 if(ARB_direct_state_access)
32 glCreateVertexArrays(1, &id);
34 glGenVertexArrays(1, &id);
37 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");
54 void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
56 if(!verify_format(vfmt) || !verify_format(ifmt))
57 throw invalid_argument("VertexSetup::set_format");
58 if(!vertex_format.empty())
59 throw invalid_operation("VertexSetup::set_format");
68 void VertexSetup::set_vertex_array(const VertexArray &a)
70 if(vertex_format.empty())
71 throw invalid_operation("VertexSetup::set_vertex_array");
72 if(a.get_format()!=vertex_format)
73 throw incompatible_data("VertexSetup::set_vertex_array");
75 throw invalid_argument("VertexSetup::set_vertex_array");
78 dirty |= VERTEX_ARRAY;
81 void VertexSetup::set_instance_array(const VertexArray &a)
83 if(inst_format.empty())
84 throw invalid_operation("VertexSetup::set_instance_array");
85 if(a.get_format()!=inst_format)
86 throw incompatible_data("VertexSetup::set_instance_array");
88 throw invalid_argument("VertexSetup::set_instance_array");
90 static Require req(ARB_instanced_arrays);
93 dirty |= INSTANCE_ARRAY;
96 void VertexSetup::set_index_buffer(const Buffer &ibuf, DataType itype)
100 dirty |= INDEX_BUFFER;
103 bool VertexSetup::verify_format(const VertexFormat &fmt)
108 unsigned max_attribs = Limits::get_global().max_vertex_attributes;
110 for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
111 if(get_attribute_semantic(*a)>=max_attribs)
117 void VertexSetup::require_format(const VertexFormat &fmt)
119 bool has_int = false;
120 for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
121 has_int = has_int | is_integer_attribute(*a);
124 static Require _req(EXT_gpu_shader4);
127 void VertexSetup::update() const
129 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
131 if(dirty&VERTEX_ARRAY)
132 update_vertex_array(*vertex_array, 0, 0, direct);
134 if(dirty&INSTANCE_ARRAY)
135 update_vertex_array(*inst_array, 1, 1, direct);
137 if(dirty&INDEX_BUFFER)
140 glVertexArrayElementBuffer(id, index_buffer->get_id());
142 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
148 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
151 glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
153 const VertexFormat &fmt = array.get_format();
154 unsigned stride = fmt.stride();
157 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
158 glVertexArrayBindingDivisor(id, binding, divisor);
162 for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a)
164 unsigned sem = get_attribute_semantic(*a);
165 bool integer = is_integer_attribute(*a);
166 GLenum type = get_gl_type(get_attribute_source_type(*a));
167 unsigned cc = get_attribute_component_count(*a);
171 glVertexArrayAttribIFormat(id, sem, cc, type, offset);
173 glVertexArrayAttribFormat(id, sem, cc, type, true, offset);
174 glVertexArrayAttribBinding(id, sem, binding);
175 glEnableVertexArrayAttrib(id, sem);
180 glVertexAttribIPointer(sem, cc, type, stride, reinterpret_cast<void *>(offset));
182 glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast<void *>(offset));
183 if(ARB_instanced_arrays)
184 glVertexAttribDivisor(sem, divisor);
185 glEnableVertexAttribArray(sem);
187 offset += get_attribute_size(*a);
191 glBindBuffer(GL_ARRAY_BUFFER, 0);
194 void VertexSetup::unload()
196 if(ARB_direct_state_access)
198 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
199 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
200 glVertexArrayElementBuffer(id, 0);
204 glBindVertexArray(id);
205 glBindBuffer(GL_ARRAY_BUFFER, 0);
207 for(const UInt16 *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
209 unsigned sem = get_attribute_semantic(*a);
210 glDisableVertexAttribArray(sem);
211 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
213 for(const UInt16 *a=inst_format.begin(); a!=inst_format.end(); ++a)
215 unsigned sem = get_attribute_semantic(*a);
216 glDisableVertexAttribArray(sem);
217 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
220 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
224 vertex_format = VertexFormat();
226 inst_format = VertexFormat();
230 void VertexSetup::set_debug_name(const string &name)
234 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());