From: Mikko Rasa Date: Wed, 15 Sep 2021 11:11:54 +0000 (+0300) Subject: Make VertexFormat capable of storing type information X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=d493917dbc215cd6ba3f8773a52a289d9f14380d;p=libs%2Fgl.git Make VertexFormat capable of storing type information --- diff --git a/source/builders/primitivebuilder.cpp b/source/builders/primitivebuilder.cpp index 1b369430..1a10af4b 100644 --- a/source/builders/primitivebuilder.cpp +++ b/source/builders/primitivebuilder.cpp @@ -60,7 +60,7 @@ PrimitiveType PrimitiveBuilder::get_type() const void PrimitiveBuilder::vertex_(const Vector4 &v) { const VertexFormat &format = array.get_format(); - for(const unsigned char *a=format.begin(); a!=format.end(); ++a) + for(const UInt16 *a=format.begin(); a!=format.end(); ++a) { unsigned sem = get_attribute_semantic(*a); if(sem=attr.size()) - ptr += sz; - else if(*a==COLOR4_UBYTE) + DataType type = get_attribute_source_type(*a); + unsigned cc = get_attribute_component_count(*a); + + if(sem(attr[sem].x*255); - u.c[1] = static_cast(attr[sem].y*255); - u.c[2] = static_cast(attr[sem].z*255); - u.c[3] = static_cast(attr[sem].w*255); - *ptr++ = u.f; + const Vector4 &value = (sem==0 ? vtx : attr[sem]); + if(type==UNSIGNED_BYTE) + store_attribute(ptr, value, cc); + else if(type==BYTE) + store_attribute(ptr, value, cc); + else if(type==UNSIGNED_SHORT) + store_attribute(ptr, value, cc); + else if(type==SHORT) + store_attribute(ptr, value, cc); + else if(type==UNSIGNED_INT) + store_attribute(ptr, value, cc); + else if(type==INT) + store_attribute(ptr, value, cc); + else if(type==FLOAT) + store_attribute(ptr, value, cc); } + + ptr += get_attribute_size(*a); + } +} + +template +void VertexArrayBuilder::store_attribute(char *ptr, const Vector4 &value, unsigned count) +{ + T *tptr = reinterpret_cast(ptr); + for(unsigned i=0; i::is_integer) + *tptr++ = value[i]; + else if(numeric_limits::is_signed) + *tptr++ = round(min(max(value[i], -1.0f), 1.0f)*numeric_limits::max()); else - { - const Vector4 &v = (sem==0 ? vtx : attr[sem]); - *ptr++ = v.x; - if(sz>=2) *ptr++ = v.y; - if(sz>=3) *ptr++ = v.z; - if(sz>=4) *ptr++ = v.w; - } + *tptr++ = round(min(max(value[i], 0.0f), 1.0f)*numeric_limits::max()); } } diff --git a/source/builders/vertexarraybuilder.h b/source/builders/vertexarraybuilder.h index 575a4753..aa723540 100644 --- a/source/builders/vertexarraybuilder.h +++ b/source/builders/vertexarraybuilder.h @@ -21,6 +21,9 @@ public: private: virtual void vertex_(const Vector4 &); + + template + void store_attribute(char *, const Vector4 &, unsigned); }; } // namespace GL diff --git a/source/builders/vertexbuilder.h b/source/builders/vertexbuilder.h index cfb4b31b..49a20b5c 100644 --- a/source/builders/vertexbuilder.h +++ b/source/builders/vertexbuilder.h @@ -132,7 +132,7 @@ public: { color(Color(r, g, b, a)); } void color(const Color &c) - { attrib(get_attribute_semantic(COLOR4_FLOAT), Vector4(c.r, c.g, c.b, c.a)); } + { attrib(get_attribute_semantic(COLOR4), Vector4(c.r, c.g, c.b, c.a)); } void group(int g0) { group(g0, 0, 0, 0); } diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index 77da81eb..de05102a 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -107,7 +107,7 @@ unsigned Mesh::get_n_vertices() const return vertices.size(); } -float *Mesh::modify_vertex(unsigned i) +char *Mesh::modify_vertex(unsigned i) { if(vertices.get_format().empty()) throw invalid_operation("Mesh::modify_vertex"); diff --git a/source/core/mesh.h b/source/core/mesh.h index 57ffce47..0111bb77 100644 --- a/source/core/mesh.h +++ b/source/core/mesh.h @@ -91,7 +91,7 @@ public: const VertexSetup &get_vertex_setup() const { return vtx_setup; } const Buffer *get_index_buffer() const { return ibuf; } unsigned get_n_vertices() const; - float *modify_vertex(unsigned); + char *modify_vertex(unsigned); void add_batch(const Batch &b); const std::vector &get_batches() const { return batches; } diff --git a/source/core/vertexarray.cpp b/source/core/vertexarray.cpp index b3bcb481..a13d79cc 100644 --- a/source/core/vertexarray.cpp +++ b/source/core/vertexarray.cpp @@ -40,7 +40,7 @@ void VertexArray::reserve(unsigned n) data.reserve(n*stride); } -float *VertexArray::append() +char *VertexArray::append() { if(format.empty()) throw invalid_operation("VertexArray::append"); @@ -50,7 +50,7 @@ float *VertexArray::append() return &*(data.end()-stride); } -float *VertexArray::modify(unsigned i) +char *VertexArray::modify(unsigned i) { if(format.empty()) throw invalid_operation("VertexArray::modify"); @@ -60,7 +60,7 @@ float *VertexArray::modify(unsigned i) unsigned VertexArray::get_data_size() const { - return data.size()*sizeof(float); + return data.size(); } diff --git a/source/core/vertexarray.h b/source/core/vertexarray.h index 107d1851..55ee8d24 100644 --- a/source/core/vertexarray.h +++ b/source/core/vertexarray.h @@ -37,7 +37,7 @@ public: private: VertexFormat format; - std::vector data; + std::vector data; unsigned stride; VertexArray(const VertexArray &); @@ -60,18 +60,18 @@ public: void reserve(unsigned); /// Append a new vertex at the end of the array and return its location. - float *append(); + char *append(); /// Returns the location of a vertex for modification. - float *modify(unsigned); + char *modify(unsigned); private: virtual unsigned get_data_size() const; virtual const void *get_data_pointer() const { return &data[0]; } public: unsigned size() const { return data.size()/stride; } - const std::vector &get_data() const { return data; } - const float *operator[](unsigned i) const { return &data[0]+i*stride; } + const std::vector &get_data() const { return data; } + const char *operator[](unsigned i) const { return &data[0]+i*stride; } }; } // namespace GL diff --git a/source/core/vertexformat.cpp b/source/core/vertexformat.cpp index e529f4eb..5d0b078f 100644 --- a/source/core/vertexformat.cpp +++ b/source/core/vertexformat.cpp @@ -32,13 +32,25 @@ VertexFormat VertexFormat::operator,(VertexAttribute a) const return r; } +VertexFormat VertexFormat::operator,(DataType t) const +{ + if(!count) + throw invalid_operation("VertexFormat::operator,"); + + VertexFormat r = *this; + UInt16 &a = r.attributes[r.count-1]; + a = make_typed_attribute(static_cast(a), t); + + return r; +} + VertexFormat VertexFormat::operator,(unsigned i) const { if(!count) throw invalid_operation("VertexFormat::operator,"); VertexFormat r = *this; - unsigned char &a = r.attributes[r.count-1]; + UInt16 &a = r.attributes[r.count-1]; a = make_indexed_attribute(static_cast(a), i); return r; @@ -54,7 +66,7 @@ bool VertexFormat::operator==(const VertexFormat &other) const unsigned VertexFormat::stride() const { unsigned s = 0; - for(const unsigned char *i=begin(); i!=end(); ++i) + for(const UInt16 *i=begin(); i!=end(); ++i) s += get_attribute_size(*i); return s; } @@ -62,13 +74,13 @@ unsigned VertexFormat::stride() const int VertexFormat::offset(VertexAttribute attr) const { unsigned sem = get_attribute_semantic(attr); - unsigned sz = get_attribute_size(attr); unsigned offs = 0; - for(const unsigned char *i=begin(); i!=end(); ++i) + for(const UInt16 *i=begin(); i!=end(); ++i) { if(get_attribute_semantic(*i)==sem) { - if(get_attribute_size(*i)>=sz) + if(get_attribute_source_type(*i)==get_attribute_source_type(attr) && + get_attribute_component_count(*i)>=get_attribute_component_count(attr)) return offs; else return -1; @@ -81,26 +93,47 @@ int VertexFormat::offset(VertexAttribute attr) const } +VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type) +{ + if(is_matrix(type) || is_vector(type) || is_image(type)) + throw invalid_argument("make_typed_attribute"); + + return static_cast((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1); +} + VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index) { unsigned base = attr; - if(attr>=TEXCOORD1 && attr<=TEXCOORD4) + if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1)) { if(index>=4) throw out_of_range("make_indexed_attribute"); } - else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4) - base &= 7; - else if(attrGENERIC4) + else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1)) + base &= 0x3FF; + else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1)) throw invalid_argument("make_indexed_attribute"); - if(static_cast((base>>3)+index)>=31) + if(get_attribute_semantic(base)+index>=63) throw out_of_range("make_indexed_attribute"); - return static_cast(base+index*8); + return static_cast(base+(index<<10)); } +bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type) +{ + string::const_iterator j = i; + for(; (j!=end && *j!='_' && *name); ++name, ++j) + if(*j!=*name) + return false; + + attr = make_typed_attribute(attr, type); + i = j; + + return true; +} + bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr) { string::const_iterator i = str.begin(); @@ -109,21 +142,30 @@ bool convert_attribute(const string &str, const char *name, int min_size, int ma return false; if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size) return false; - VertexAttribute result = static_cast(base_attr+(*i-'0'-min_size)); + VertexAttribute result = static_cast(base_attr+(*i++-'0'-min_size)); - if(++i!=str.end()) + while(i!=str.end()) { if(*i!='_' || ++i==str.end()) return false; - try + + if(isdigit(*i)) { - result = make_indexed_attribute(result, lexical_cast(string(i, str.end()))); + unsigned index = 0; + for(; (i!=str.end() && isdigit(*i)); ++i) + index = index*10+(*i-'0'); + result = make_indexed_attribute(result, index); } - catch(...) - { - // The operator>> will throw a more appropriate exception + else if(convert_attribute_type(i, str.end(), "UBYTE", result, UNSIGNED_BYTE) || + convert_attribute_type(i, str.end(), "BYTE", result, BYTE) || + convert_attribute_type(i, str.end(), "USHORT", result, UNSIGNED_SHORT) || + convert_attribute_type(i, str.end(), "SHORT", result, SHORT) || + convert_attribute_type(i, str.end(), "UINT", result, UNSIGNED_INT) || + convert_attribute_type(i, str.end(), "INT", result, INT) || + convert_attribute_type(i, str.end(), "FLOAT", result, FLOAT)) + ; + else return false; - } } attr = result; @@ -133,25 +175,22 @@ bool convert_attribute(const string &str, const char *name, int min_size, int ma void operator>>(const LexicalConverter &conv, VertexAttribute &a) { const string &str = conv.get(); - if(str=="NORMAL3") - a = NORMAL3; - else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT")) - a = static_cast(COLOR3_FLOAT+(str[5]-'3')); - else if(str=="COLOR4_UBYTE") - a = COLOR4_UBYTE; - else if(str=="TANGENT3") - a = TANGENT3; - else if(str=="BINORMAL3") + try { - IO::print(IO::cerr, "BINORMAL3 attribute is deprecated\n"); - a = make_indexed_attribute(GENERIC3, 5); + if(convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) || + convert_attribute(str, "COLOR", 3, 4, a, COLOR3) || + convert_attribute(str, "NORMAL", 3, 3, a, NORMAL3) || + convert_attribute(str, "TANGENT", 3, 3, a, TANGENT3) || + convert_attribute(str, "GROUP", 1, 4, a, GROUP1) || + convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) || + convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) || + convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1)) + return; } - else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) && - !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) && - !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) && - !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) && - !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1)) - throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); + catch(...) + { } + + throw lexical_error(format("conversion of '%s' to VertexAttribute", str)); } } // namespace GL diff --git a/source/core/vertexformat.h b/source/core/vertexformat.h index 4c485b04..a503aff8 100644 --- a/source/core/vertexformat.h +++ b/source/core/vertexformat.h @@ -1,7 +1,9 @@ #ifndef MSP_GL_VERTEXFORMAT_H_ #define MSP_GL_VERTEXFORMAT_H_ +#include #include +#include "datatype.h" namespace Msp { namespace GL { @@ -14,37 +16,49 @@ depends on implementation limits, but is at least five. RAW_ATTRIB is handled in a special way; creating an indexed attribute based on it uses the index as raw attribute number. Only use it if you know what you -are doing. */ +are doing. + +The values are bitfields laid as follows: + +nnnn nn_f gsss _ccc + │ │ │ │ └╴Number of components + │ │ │ └─────╴Size of one component + │ │ └────────╴Signed flag + │ └──────────╴Floating-point flag + └────────────╴Attribute index (semantic) + +This information is presented for internal documentation purposes only; it is +inadvisable for programs to rely on it. +*/ enum VertexAttribute { - VERTEX2 = 1, - VERTEX3, - VERTEX4, - COLOR4_UBYTE = 8, - COLOR3_FLOAT = 10, - COLOR4_FLOAT, - NORMAL3 = 18, - TANGENT3 = 26, - GROUP1 = 32, - GROUP2, - GROUP3, - GROUP4, - WEIGHT1 = 40, - WEIGHT2, - WEIGHT3, - WEIGHT4, - TEXCOORD1 = 48, - TEXCOORD2, - TEXCOORD3, - TEXCOORD4, - GENERIC1 = 80, - GENERIC2, - GENERIC3, - GENERIC4, - RAW_ATTRIB1 = 248, - RAW_ATTRIB2, - RAW_ATTRIB3, - RAW_ATTRIB4 + VERTEX2 = 0x01C2, + VERTEX3 = 0x01C3, + VERTEX4 = 0x01C4, + COLOR3 = 0x05C3, + COLOR4 = 0x05C4, + NORMAL3 = 0x09C3, + TANGENT3 = 0x0DC3, + GROUP1 = 0x11C1, + GROUP2 = 0x11C2, + GROUP3 = 0x11C3, + GROUP4 = 0x11C4, + WEIGHT1 = 0x15C1, + WEIGHT2 = 0x15C2, + WEIGHT3 = 0x15C3, + WEIGHT4 = 0x15C4, + TEXCOORD1 = 0x19C1, + TEXCOORD2 = 0x19C2, + TEXCOORD3 = 0x19C3, + TEXCOORD4 = 0x19C4, + GENERIC1 = 0x29C1, + GENERIC2 = 0x29C2, + GENERIC3 = 0x29C3, + GENERIC4 = 0x29C4, + RAW_ATTRIB1 = 0xFDC1, + RAW_ATTRIB2 = 0xFDC2, + RAW_ATTRIB3 = 0xFDC3, + RAW_ATTRIB4 = 0xFDC4 }; class VertexFormat @@ -52,21 +66,22 @@ class VertexFormat private: enum { MAX_ATTRIBUTES = 15 }; - unsigned char count; - unsigned char attributes[MAX_ATTRIBUTES]; + UInt8 count; + UInt16 attributes[MAX_ATTRIBUTES]; public: VertexFormat(); VertexFormat(VertexAttribute); VertexFormat operator,(VertexAttribute) const; + VertexFormat operator,(DataType) const; VertexFormat operator,(unsigned) const; bool operator==(const VertexFormat &) const; bool operator!=(const VertexFormat &other) const { return !(*this==other); } bool empty() const { return !count; } - const unsigned char *begin() const { return attributes; } - const unsigned char *end() const { return attributes+count; } + const UInt16 *begin() const { return attributes; } + const UInt16 *end() const { return attributes+count; } unsigned stride() const; int offset(VertexAttribute) const; }; @@ -74,16 +89,27 @@ public: inline VertexFormat operator,(VertexAttribute a1, VertexAttribute a2) { return (VertexFormat(a1), a2); } +VertexAttribute make_typed_attribute(VertexAttribute, DataType); + +inline VertexAttribute operator,(VertexAttribute a, DataType t) +{ return make_typed_attribute(a, t); } + VertexAttribute make_indexed_attribute(VertexAttribute, unsigned); inline VertexAttribute operator,(VertexAttribute a, unsigned i) { return make_indexed_attribute(a, i); } -inline unsigned get_attribute_semantic(unsigned char a) -{ return a>>3; } +inline unsigned get_attribute_semantic(UInt16 a) +{ return a>>10; } + +inline DataType get_attribute_source_type(UInt16 a) +{ return static_cast((a&0x70)>>4 | (a&0x180)<<1); } + +inline unsigned get_attribute_component_count(UInt16 a) +{ return a&7; } -inline unsigned get_attribute_size(unsigned char a) -{ return (a&3)+1; } +inline unsigned get_attribute_size(UInt16 a) +{ return get_attribute_component_count(a)*get_type_size(get_attribute_source_type(a)); } void operator>>(const LexicalConverter &, VertexAttribute &); diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index 86c9f0f2..8498e043 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -101,7 +101,7 @@ bool VertexSetup::verify_format(const VertexFormat &fmt) unsigned max_attribs = Limits::get_global().max_vertex_attributes; - for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a) + for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a) if(get_attribute_semantic(*a)>=max_attribs) return false; @@ -135,7 +135,7 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding glBindBuffer(GL_ARRAY_BUFFER, array.get_buffer()->get_id()); const VertexFormat &fmt = array.get_format(); - unsigned stride = fmt.stride()*sizeof(float); + unsigned stride = fmt.stride(); if(direct) { glVertexArrayVertexBuffer(id, binding, array.get_buffer()->get_id(), 0, stride); @@ -143,30 +143,25 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding } unsigned offset = 0; - for(const unsigned char *a=fmt.begin(); a!=fmt.end(); ++a) + for(const UInt16 *a=fmt.begin(); a!=fmt.end(); ++a) { unsigned sem = get_attribute_semantic(*a); - unsigned sz = get_attribute_size(*a); + GLenum type = get_gl_type(get_attribute_source_type(*a)); + unsigned cc = get_attribute_component_count(*a); if(direct) { - if(*a==COLOR4_UBYTE) - glVertexArrayAttribFormat(id, sem, 4, GL_UNSIGNED_BYTE, true, offset); - else - glVertexArrayAttribFormat(id, sem, sz, GL_FLOAT, false, offset); + glVertexArrayAttribFormat(id, sem, cc, type, true, offset); glVertexArrayAttribBinding(id, sem, binding); glEnableVertexArrayAttrib(id, sem); } else { - if(*a==COLOR4_UBYTE) - glVertexAttribPointer(sem, 4, GL_UNSIGNED_BYTE, true, stride, reinterpret_cast(offset)); - else - glVertexAttribPointer(sem, sz, GL_FLOAT, false, stride, reinterpret_cast(offset)); + glVertexAttribPointer(sem, cc, type, true, stride, reinterpret_cast(offset)); if(ARB_instanced_arrays) glVertexAttribDivisor(sem, divisor); glEnableVertexAttribArray(sem); } - offset += sz*sizeof(float); + offset += get_attribute_size(*a); } if(!direct) @@ -186,13 +181,13 @@ void VertexSetup::unload() glBindVertexArray(id); glBindBuffer(GL_ARRAY_BUFFER, 0); - for(const unsigned char *a=vertex_format.begin(); a!=vertex_format.end(); ++a) + for(const UInt16 *a=vertex_format.begin(); a!=vertex_format.end(); ++a) { unsigned sem = get_attribute_semantic(*a); glDisableVertexAttribArray(sem); glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0); } - for(const unsigned char *a=inst_format.begin(); a!=inst_format.end(); ++a) + for(const UInt16 *a=inst_format.begin(); a!=inst_format.end(); ++a) { unsigned sem = get_attribute_semantic(*a); glDisableVertexAttribArray(sem); diff --git a/source/render/instancearray.cpp b/source/render/instancearray.cpp index 42797612..293fa6cd 100644 --- a/source/render/instancearray.cpp +++ b/source/render/instancearray.cpp @@ -110,9 +110,9 @@ void InstanceArray::update_instance_matrix(unsigned index) const Matrix &m = *instances[index]->get_matrix(); - float *d = instance_data->modify(instances.size()-1); + float *d = reinterpret_cast(instance_data->modify(instances.size()-1)+matrix_offset); for(unsigned i=0; i<12; ++i) - d[matrix_offset+i] = m(i/4, i%4); + d[i] = m(i/4, i%4); } void InstanceArray::render(Renderer &renderer, Tag tag) const diff --git a/source/render/object.cpp b/source/render/object.cpp index 38d2e6f2..067686d2 100644 --- a/source/render/object.cpp +++ b/source/render/object.cpp @@ -97,8 +97,8 @@ void Object::update_bounding_sphere() points.reserve(points.size()+n_vertices); for(unsigned j=0; j(vertices[j]+offset); + points.push_back(Vector3(v[0], v[1], (three ? v[2] : 0.0f))); } } diff --git a/source/render/text.cpp b/source/render/text.cpp index b43061c5..b3d85deb 100644 --- a/source/render/text.cpp +++ b/source/render/text.cpp @@ -102,7 +102,7 @@ void Text::set_alignment(float h, float v) unsigned n_vertices = mesh.get_n_vertices(); for(unsigned i=0; i(mesh.modify_vertex(i)+pos_offset); pos[0] += horz_adjust; pos[1] += vert_adjust; }