X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fvertexsetup.cpp;h=f6f0f58b65397cabdba6f9cbdc42bf027503689d;hb=86721a55699193e63c76e8a0a7b0ced0416c1cce;hp=d50406a1ab70ce44e7caf645d46009a308c8e86f;hpb=7aaec9a70b8d7733429bec043f8e33e02956f266;p=libs%2Fgl.git diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index d50406a1..f6f0f58b 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -8,6 +8,7 @@ #include "buffer.h" #include "error.h" #include "gl.h" +#include "misc.h" #include "vertexarray.h" #include "vertexsetup.h" @@ -38,7 +39,7 @@ VertexSetup::~VertexSetup() void VertexSetup::set_vertex_array(const VertexArray &a) { - if(!a.get_buffer()) + if(!verify_array(a)) throw invalid_argument("VertexSetup::set_vertex_array"); vertex_array = &a; @@ -50,7 +51,7 @@ void VertexSetup::set_instance_array(const VertexArray *a) { if(a) { - if(!a->get_buffer()) + if(!verify_array(*a)) throw invalid_argument("VertexSetup::set_instance_array"); static Require req(ARB_instanced_arrays); @@ -76,16 +77,28 @@ void VertexSetup::refresh() set_instance_array(inst_array); } +bool VertexSetup::verify_array(const VertexArray &array) +{ + if(!array.get_buffer()) + return false; + + static int max_attribs = -1; + if(max_attribs<0) + max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS); + + const VertexFormat &fmt = array.get_format(); + for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a) + if(static_cast(get_attribute_semantic(*a))>=max_attribs) + return false; + + return true; +} + 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< bind_vbuf(!direct, array.get_buffer(), ARRAY_BUFFER); const VertexFormat &fmt = array.get_format(); - unsigned stride = get_stride(fmt)*sizeof(float); + unsigned stride = fmt.stride()*sizeof(float); if(direct) { glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride); @@ -144,30 +157,28 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding } unsigned offset = 0; - for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) + for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a) { - unsigned t = get_component_type(*c); - if(t>=get_component_type(ATTRIB1)) - t -= get_component_type(ATTRIB1); - unsigned sz = get_component_size(*c); + unsigned sem = get_attribute_semantic(*a); + unsigned sz = get_attribute_size(*a); if(direct) { - if(*c==COLOR4_UBYTE) - glVertexArrayAttribFormat(id, t, 4, GL_UNSIGNED_BYTE, true, offset); + if(*a==COLOR4_UBYTE) + glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset); else - glVertexArrayAttribFormat(id, t, sz, GL_FLOAT, false, offset); - glVertexArrayAttribBinding(id, t, binding); - glEnableVertexArrayAttrib(id, t); + glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset); + glVertexArrayAttribBinding(id, sem, binding); + glEnableVertexArrayAttrib(id, sem); } else { - if(*c==COLOR4_UBYTE) - glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast(offset)); + if(*a==COLOR4_UBYTE) + glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast(offset)); else - glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, reinterpret_cast(offset)); + glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast(offset)); if(ARB_instanced_arrays) - glVertexAttribDivisor(t, divisor); - glEnableVertexAttribArray(t); + glVertexAttribDivisor(sem, divisor); + glEnableVertexAttribArray(sem); } offset += sz*sizeof(float); }