]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/vertexsetup.cpp
Add debug name capability to more classes
[libs/gl.git] / source / core / vertexsetup.cpp
index 6a41397a8135a63931cffaec766d73f55a032476..4b2e2a5f0e5b8d3ccfb1bb435772f58557c15efb 100644 (file)
@@ -5,9 +5,11 @@
 #include <msp/gl/extensions/arb_vertex_attrib_binding.h>
 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
 #include <msp/gl/extensions/arb_vertex_shader.h>
+#include <msp/gl/extensions/khr_debug.h>
 #include "buffer.h"
 #include "error.h"
 #include "gl.h"
+#include "misc.h"
 #include "vertexarray.h"
 #include "vertexsetup.h"
 
@@ -38,7 +40,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 +52,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 +78,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 +161,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)
                {
@@ -198,5 +210,15 @@ void VertexSetup::unbind()
                glBindVertexArray(0);
 }
 
+void VertexSetup::set_debug_name(const string &name)
+{
+#ifdef DEBUG
+       if(KHR_debug)
+               glObjectLabel(GL_VERTEX_ARRAY, id, name.size(), name.c_str());
+#else
+       (void)name;
+#endif
+}
+
 } // namespace GL
 } // namespace Msp