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>
11 #include "vertexarray.h"
12 #include "vertexsetup.h"
19 VertexSetup::VertexSetup():
25 static Require req(ARB_vertex_array_object);
26 if(ARB_direct_state_access)
27 glCreateVertexArrays(1, &id);
29 glGenVertexArrays(1, &id);
32 VertexSetup::~VertexSetup()
36 glDeleteVertexArrays(1, &id);
39 void VertexSetup::set_vertex_array(const VertexArray &a)
42 throw invalid_argument("VertexSetup::set_vertex_array");
45 update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
46 vertex_format = vertex_array->get_format();
49 void VertexSetup::set_instance_array(const VertexArray *a)
54 throw invalid_argument("VertexSetup::set_instance_array");
56 static Require req(ARB_instanced_arrays);
60 update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
61 inst_format = inst_array->get_format();
64 void VertexSetup::set_index_buffer(const Buffer &ibuf)
70 void VertexSetup::refresh()
72 if(vertex_array && vertex_array->get_format()!=vertex_format)
73 set_vertex_array(*vertex_array);
75 if(inst_array && inst_array->get_format()!=inst_format)
76 set_instance_array(inst_array);
79 unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
82 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
83 mask |= 1<<get_attribute_semantic(*a);
87 unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
89 unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
90 return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
93 void VertexSetup::update(unsigned mask) const
95 static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
96 if(!direct && current()!=this)
102 if(mask&UNUSED_ATTRIBS)
104 for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
108 glDisableVertexArrayAttrib(id, i);
110 glDisableVertexAttribArray(i);
114 if(mask&VERTEX_ARRAY)
115 update_vertex_array(*vertex_array, 0, 0, direct);
117 if((mask&INSTANCE_ARRAY) && inst_array)
118 update_vertex_array(*inst_array, 1, 1, direct);
120 if(mask&INDEX_BUFFER)
123 glVertexArrayElementBuffer(id, index_buffer->get_id());
125 glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
129 void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding, unsigned divisor, bool direct) const
131 Conditional<Bind> bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER);
133 const VertexFormat &fmt = array.get_format();
134 unsigned stride = fmt.stride()*sizeof(float);
137 glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride);
138 glVertexArrayBindingDivisor(id, binding, divisor);
142 for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
144 unsigned sem = get_attribute_semantic(*a);
145 unsigned sz = get_attribute_size(*a);
149 glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset);
151 glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset);
152 glVertexArrayAttribBinding(id, sem, binding);
153 glEnableVertexArrayAttrib(id, sem);
158 glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast<unsigned char *>(offset));
160 glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast<float *>(offset));
161 if(ARB_instanced_arrays)
162 glVertexAttribDivisor(sem, divisor);
163 glEnableVertexAttribArray(sem);
165 offset += sz*sizeof(float);
169 void VertexSetup::bind() const
171 if(!vertex_array || !index_buffer)
172 throw invalid_operation("VertexSetup::bind");
174 if(set_current(this))
176 vertex_array->refresh();
178 inst_array->refresh();
179 glBindVertexArray(id);
188 void VertexSetup::unbind()
191 glBindVertexArray(0);