if(allow_gl_calls)
{
obj.check_buffers(VERTEX_BUFFER);
- obj.vtx_setup.set_vertex_array(obj.vertices);
+ obj.vtx_setup.refresh();
}
}
else if(phase==1)
{
mesh.resize_buffers();
- mesh.vtx_setup.set_vertex_array(vertices);
+ mesh.vtx_setup.refresh();
vertex_updater = mesh.vertices.refresh_async();
if(!mesh.batches.empty())
index_updater = mesh.batches.front().refresh_async();
throw invalid_argument("VertexSetup::set_vertex_array");
vertex_array = &a;
- update(VERTEX_ARRAY);
+ update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
+ vertex_format = vertex_array->get_format();
}
void VertexSetup::set_instance_array(const VertexArray *a)
}
inst_array = a;
- update(INSTANCE_ARRAY);
+ update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
+ inst_format = inst_array->get_format();
}
void VertexSetup::set_index_buffer(const Buffer &ibuf)
update(INDEX_BUFFER);
}
+void VertexSetup::refresh()
+{
+ if(vertex_array && vertex_array->get_format()!=vertex_format)
+ set_vertex_array(*vertex_array);
+
+ if(inst_array && inst_array->get_format()!=inst_format)
+ set_instance_array(inst_array);
+}
+
+unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
+{
+ unsigned mask = 0;
+ for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
+ {
+ unsigned t = get_component_type(*c);
+ if(t>=get_component_type(ATTRIB1))
+ t -= get_component_type(ATTRIB1);
+ mask |= 1<<t;
+ }
+ return mask;
+}
+
+unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
+{
+ unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
+ return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
+}
+
void VertexSetup::update(unsigned mask) const
{
static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
return;
}
+ if(mask&UNUSED_ATTRIBS)
+ {
+ for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
+ if(am&1)
+ {
+ if(direct)
+ glDisableVertexArrayAttrib(id, i);
+ else
+ glDisableVertexAttribArray(i);
+ }
+ }
+
if(mask&VERTEX_ARRAY)
update_vertex_array(*vertex_array, 0, 0, direct);
- if(mask&INSTANCE_ARRAY)
- {
- if(inst_array)
- update_vertex_array(*inst_array, 1, 1, direct);
- }
+ if((mask&INSTANCE_ARRAY) && inst_array)
+ update_vertex_array(*inst_array, 1, 1, direct);
if(mask&INDEX_BUFFER)
{
#define MSP_GL_VERTEXSETUP_H_
#include "bindable.h"
+#include "vertexformat.h"
namespace Msp {
namespace GL {
{
VERTEX_ARRAY = 1,
INSTANCE_ARRAY = 2,
- INDEX_BUFFER = 4
+ INDEX_BUFFER = 4,
+ UNUSED_ATTRIBS = 8,
+ ATTRIB_SHIFT = 4
};
unsigned id;
mutable unsigned dirty;
const VertexArray *vertex_array;
+ VertexFormat vertex_format;
const VertexArray *inst_array;
+ VertexFormat inst_format;
const Buffer *index_buffer;
public:
void set_vertex_array(const VertexArray &);
void set_instance_array(const VertexArray *);
void set_index_buffer(const Buffer &);
+ void refresh();
const VertexArray *get_vertex_array() const { return vertex_array; }
const VertexArray *get_instance_array() const { return inst_array; }
const Buffer *get_index_buffer() const { return index_buffer; }
private:
+ static unsigned get_attribs(const VertexFormat &);
+ static unsigned get_update_mask(unsigned, const VertexFormat &, const VertexArray &);
void update(unsigned) const;
void update_vertex_array(const VertexArray &, unsigned, unsigned, bool) const;