From: Mikko Rasa Date: Sun, 28 Mar 2021 13:48:48 +0000 (+0300) Subject: Move checking of vertex attribute indices to VertexSetup X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6d33ca40415937790ed0cddc97148b5fbd90ccaf;p=libs%2Fgl.git Move checking of vertex attribute indices to VertexSetup 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. --- diff --git a/source/core/vertexformat.cpp b/source/core/vertexformat.cpp index 65bbea49..908da992 100644 --- a/source/core/vertexformat.cpp +++ b/source/core/vertexformat.cpp @@ -3,9 +3,7 @@ #include #include #include "error.h" -#include "misc.h" #include "vertexformat.h" -#include using namespace std; @@ -95,11 +93,7 @@ VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index) else if(attrGENERIC4) 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((base>>3)+index)>=max_attribs) + if(static_cast((base>>3)+index)>=31) throw out_of_range("make_indexed_attribute"); return static_cast(base+index*8); diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index 0cbe9a92..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,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(get_attribute_semantic(*a))>=max_attribs) + return false; + + return true; +} + unsigned VertexSetup::get_attribs(const VertexFormat &fmt) { unsigned mask = 0; diff --git a/source/core/vertexsetup.h b/source/core/vertexsetup.h index bae4c5f3..dceb7987 100644 --- a/source/core/vertexsetup.h +++ b/source/core/vertexsetup.h @@ -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;