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()
37 glDeleteVertexArrays(1, &id);
40 void VertexSetup::set_format(const VertexFormat &vfmt)
42 if(!verify_format(vfmt))
43 throw invalid_argument("VertexSetup::set_format");
44 if(!vertex_format.empty())
45 throw invalid_operation("VertexSetup::set_format");
50 void VertexSetup::set_format_instanced(const VertexFormat &vfmt, const VertexFormat &ifmt)
52 if(!verify_format(vfmt) || !verify_format(ifmt))
53 throw invalid_argument("VertexSetup::set_format");
54 if(!vertex_format.empty())
55 throw invalid_operation("VertexSetup::set_format");
61 void VertexSetup::set_vertex_array(const VertexArray &a)
63 if(vertex_format.empty())
64 throw invalid_operation("VertexSetup::set_vertex_array");
65 if(a.get_format()!=vertex_format)
66 throw incompatible_data("VertexSetup::set_vertex_array");
68 throw invalid_argument("VertexSetup::set_vertex_array");
71 dirty |= VERTEX_ARRAY;
74 void VertexSetup::set_instance_array(const VertexArray &a)
76 if(inst_format.empty())
77 throw invalid_operation("VertexSetup::set_instance_array");
78 if(a.get_format()!=inst_format)
79 throw incompatible_data("VertexSetup::set_instance_array");
81 throw invalid_argument("VertexSetup::set_instance_array");
83 static Require req(ARB_instanced_arrays);
86 dirty |= INSTANCE_ARRAY;
89 void VertexSetup::set_index_buffer(const Buffer &ibuf)
92 dirty |= INDEX_BUFFER;
95 bool VertexSetup::verify_format(const VertexFormat &fmt)
100 unsigned max_attribs = Limits::get_global().max_vertex_attributes;
102 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
103 if(get_attribute_semantic(*a)>=max_attribs)
109 void VertexSetup::update() const
111 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
113 if(dirty&VERTEX_ARRAY)
114 update_vertex_array(*vertex_array, 0, 0, direct);
116 if(dirty&INSTANCE_ARRAY)
117 update_vertex_array(*inst_array, 1, 1, direct);
119 if(dirty&INDEX_BUFFER)
122 glVertexArrayElementBuffer(id, index_buffer->get_id());
124 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
130 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
133 glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id());
135 const VertexFormat &fmt = array.get_format();
136 unsigned stride = fmt.stride()*sizeof(float);
139 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
140 glVertexArrayBindingDivisor(id, binding, divisor);
144 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
146 unsigned sem = get_attribute_semantic(*a);
147 unsigned sz = get_attribute_size(*a);
151 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
153 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
154 glVertexArrayAttribBinding(id, sem, binding);
155 glEnableVertexArrayAttrib(id, sem);
160 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
162 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
163 if(ARB_instanced_arrays)
164 glVertexAttribDivisor(sem, divisor);
165 glEnableVertexAttribArray(sem);
167 offset += sz*sizeof(float);
171 glBindBuffer(GL_ARRAY_BUFFER, 0);
174 void VertexSetup::unload()
176 if(ARB_direct_state_access)
178 glVertexArrayVertexBuffer(id, 0, 0, 0, 0);
179 glVertexArrayVertexBuffer(id, 1, 0, 0, 0);
180 glVertexArrayElementBuffer(id, 0);
184 glBindVertexArray(id);
185 glBindBuffer(GL_ARRAY_BUFFER, 0);
187 for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
189 unsigned sem = get_attribute_semantic(*a);
190 glDisableVertexAttribArray(sem);
191 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
193 for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a)
195 unsigned sem = get_attribute_semantic(*a);
196 glDisableVertexAttribArray(sem);
197 glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
200 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
204 vertex_format = VertexFormat();
206 inst_format = VertexFormat();
210 void VertexSetup::set_debug_name(const string &name)
214 glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());