]> git.tdb.fi Git - libs/gl.git/commitdiff
Move checking of vertex attribute indices to VertexSetup
authorMikko Rasa <tdb@tdb.fi>
Sun, 28 Mar 2021 13:48:48 +0000 (16:48 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 28 Mar 2021 13:48:48 +0000 (16:48 +0300)
This avoids issues when loading meshes with ResourceManager, since it's
not possible to read the value of GL_MAX_VERTEX_ATTRIBS from the loading
thread.

source/core/vertexformat.cpp
source/core/vertexsetup.cpp
source/core/vertexsetup.h

index 65bbea49887ad82f1cdf4097204861bba79e197b..908da99278defda2530f699aef308a90d116e99e 100644 (file)
@@ -3,9 +3,7 @@
 #include <msp/strings/lexicalcast.h>
 #include <msp/strings/utils.h>
 #include "error.h"
-#include "misc.h"
 #include "vertexformat.h"
-#include <msp/gl/extensions/arb_vertex_shader.h>
 
 using namespace std;
 
@@ -95,11 +93,7 @@ VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
        else if(attr<GENERIC1 || attr>GENERIC4)
                throw invalid_argument("make_indexed_attribute");
 
-       static int max_attribs = -1;
-       if(max_attribs<0)
-               max_attribs = get_i(GL_MAX_VERTEX_ATTRIBS);
-
-       if(static_cast<int>((base>>3)+index)>=max_attribs)
+       if(static_cast<int>((base>>3)+index)>=31)
                throw out_of_range("make_indexed_attribute");
 
        return static_cast<VertexAttribute>(base+index*8);
index 0cbe9a921a3dce33fc9190fbec59abc33654c719..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,6 +77,23 @@ 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;
index bae4c5f36c2245d194eacc6b08b1df25658dade7..dceb79876910cdf60d76990ecba25e73992d82d9 100644 (file)
@@ -46,6 +46,7 @@ public:
        const Buffer *get_index_buffer() const { return index_buffer; }
 
 private:
+       static bool verify_array(const VertexArray &);
        static unsigned get_attribs(const VertexFormat &);
        static unsigned get_update_mask(unsigned, const VertexFormat &, const VertexArray &);
        void update(unsigned) const;