]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/vertexsetup.cpp
Move checking of vertex attribute indices to VertexSetup
[libs/gl.git] / source / core / vertexsetup.cpp
index 6a41397a8135a63931cffaec766d73f55a032476..f6f0f58b65397cabdba6f9cbdc42bf027503689d 100644 (file)
@@ -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<int>(get_attribute_semantic(*a))>=max_attribs)
+                       return false;
+
+       return true;
+}
+
 unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
 {
        unsigned mask = 0;
        for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
-       {
-               unsigned sem = get_attribute_semantic(*a);
-               if(sem>=get_attribute_semantic(ATTRIB1))
-                       sem -= get_attribute_semantic(ATTRIB1);
-               mask |= 1<<sem;
-       }
+               mask |= 1<<get_attribute_semantic(*a);
        return mask;
 }
 
@@ -147,8 +160,6 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding
        for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a)
        {
                unsigned sem = get_attribute_semantic(*a);
-               if(sem>=get_attribute_semantic(ATTRIB1))
-                       sem -= get_attribute_semantic(ATTRIB1);
                unsigned sz = get_attribute_size(*a);
                if(direct)
                {