]> git.tdb.fi Git - libs/gl.git/commitdiff
Specify underlying type for format descriptor enums
authorMikko Rasa <tdb@tdb.fi>
Fri, 17 Sep 2021 23:56:23 +0000 (02:56 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 18 Sep 2021 00:23:25 +0000 (03:23 +0300)
This makes it possible to use them directory in the associated format
structs without increasing their size.

source/builders/primitivebuilder.cpp
source/builders/vertexarraybuilder.cpp
source/core/framebuffer.cpp
source/core/frameformat.cpp
source/core/frameformat.h
source/core/vertexformat.cpp
source/core/vertexformat.h
source/core/vertexsetup.cpp
source/render/rendertarget.cpp

index ca16fcde68f9f804026d27c05ec25dd412d77572..85c695c3cea402735d3c8501d297f844bb7f14e8 100644 (file)
@@ -59,10 +59,9 @@ PrimitiveType PrimitiveBuilder::get_type() const
 
 void PrimitiveBuilder::vertex_(const Vector4 &v)
 {
-       const VertexFormat &format = array.get_format();
-       for(const uint16_t *a=format.begin(); a!=format.end(); ++a)
+       for(VertexAttribute a: array.get_format())
        {
-               unsigned sem = get_attribute_semantic(*a);
+               unsigned sem = get_attribute_semantic(a);
                if(sem<attr.size())
                        vab.attrib(sem, attr[sem]);
        }
index 011a6c0515c59788e1db5675ee65c1f37c2ca9d2..c9a1f3bc8c5c5d8a751705fdfb3603ad196a7e6f 100644 (file)
@@ -13,13 +13,12 @@ VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
 void VertexArrayBuilder::vertex_(const Vector4 &vtx)
 {
        char *ptr = array.append();
-       const VertexFormat &format = array.get_format();
-       for(const uint16_t *a=format.begin(); a!=format.end(); ++a)
+       for(VertexAttribute a: array.get_format())
        {
-               unsigned sem = get_attribute_semantic(*a);
-               bool integer = is_integer_attribute(*a);
-               DataType type = get_attribute_source_type(*a);
-               unsigned cc = get_attribute_component_count(*a);
+               unsigned sem = get_attribute_semantic(a);
+               bool integer = is_integer_attribute(a);
+               DataType type = get_attribute_source_type(a);
+               unsigned cc = get_attribute_component_count(a);
 
                if(sem<attr.size())
                {
@@ -40,7 +39,7 @@ void VertexArrayBuilder::vertex_(const Vector4 &vtx)
                                store_attribute<float>(ptr, value, false, cc);
                }
 
-               ptr += get_attribute_size(*a);
+               ptr += get_attribute_size(a);
        }
 }
 
index e9d8863ca16e4e0379a11e9777def136cb596d2e..851b6431b2fb134d470b1116262411af471532be 100644 (file)
@@ -131,9 +131,9 @@ void Framebuffer::update() const
        vector<GLenum> color_bufs;
        color_bufs.reserve(format.size());
        unsigned i = 0;
-       for(const uint16_t *j=format.begin(); j!=format.end(); ++j, ++i)
+       for(FrameAttachment a: format)
        {
-               GLenum gl_attach_point = get_gl_attachment(static_cast<FrameAttachment>(*j));
+               GLenum gl_attach_point = get_gl_attachment(a);
                if(dirty&(1<<i))
                {
                        const Attachment &attch = attachments[i];
@@ -166,6 +166,8 @@ void Framebuffer::update() const
 
                if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT)
                        color_bufs.push_back(gl_attach_point);
+
+               ++i;
        }
 
        if(color_bufs.size()>1)
@@ -254,14 +256,17 @@ void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned l
                throw incompatible_data("Framebuffer::attach");
 
        unsigned i = 0;
-       for(const uint16_t *j=format.begin(); j!=format.end(); ++j, ++i)
-               if(*j==attch)
+       for(FrameAttachment a: format)
+       {
+               if(a==attch)
                {
                        attachments[i].set(tex, level, layer);
                        dirty |= 1<<i;
                        check_size();
                        return;
                }
+               ++i;
+       }
 
        throw incompatible_data("Framebuffer::attach");
 }
index 3a415766696cdf065cbe4670682a32a47f76bd85..3b114e7f2f0ebd01da02ea39ce5edd72f64e2be8 100644 (file)
@@ -36,8 +36,8 @@ FrameFormat FrameFormat::operator,(PixelFormat pf) const
                throw invalid_operation("FrameFormat::operator,");
 
        FrameFormat r = *this;
-       uint16_t &fa = r.attachments[r.count-1];
-       fa = make_typed_attachment(static_cast<FrameAttachment>(fa), pf);
+       FrameAttachment &fa = r.attachments[r.count-1];
+       fa = make_typed_attachment(fa, pf);
 
        return r;
 }
@@ -48,8 +48,8 @@ FrameFormat FrameFormat::operator,(unsigned index) const
                throw invalid_operation("FrameFormat::operator,");
 
        FrameFormat r = *this;
-       uint16_t &fa = r.attachments[r.count-1];
-       fa = make_indexed_attachment(static_cast<FrameAttachment>(fa), index);
+       FrameAttachment &fa = r.attachments[r.count-1];
+       fa = make_indexed_attachment(fa, index);
 
        return r;
 }
@@ -104,7 +104,7 @@ FrameAttachment make_indexed_attachment(FrameAttachment fa, unsigned i)
                throw invalid_argument("make_indexed_attachment");
 }
 
-PixelFormat get_attachment_pixelformat(uint16_t fa)
+PixelFormat get_attachment_pixelformat(FrameAttachment fa)
 {
        PixelComponents comp;
        if(get_attach_point(fa)==get_attach_point(DEPTH_ATTACHMENT))
index 35d982ba8839f1489b75efab9489560a7aa2d6e3..c80f63e0dd02518e00aa85f9f1eae75947beb564 100644 (file)
@@ -20,7 +20,7 @@ nnnn nn_f _sss _ccc
 This information is presented for internal documentation purposes only; it is
 inadvisable for programs to rely on it.
 */
-enum FrameAttachment
+enum FrameAttachment: std::uint16_t
 {
        COLOR_ATTACHMENT = 0x0014,
        DEPTH_ATTACHMENT = 0xF941,
@@ -38,7 +38,7 @@ private:
 
        std::uint8_t count;
        std::uint8_t samples;
-       std::uint16_t attachments[MAX_ATTACHMENTS];
+       FrameAttachment attachments[MAX_ATTACHMENTS];
 
 public:
        FrameFormat();
@@ -53,8 +53,8 @@ public:
 
        unsigned size() const { return count; }
        bool empty() const { return !count; }
-       const std::uint16_t *begin() const { return attachments; }
-       const std::uint16_t *end() const { return attachments+count; }
+       const FrameAttachment *begin() const { return attachments; }
+       const FrameAttachment *end() const { return attachments+count; }
        int index(FrameAttachment) const;
 };
 
@@ -71,10 +71,10 @@ FrameAttachment make_indexed_attachment(FrameAttachment, unsigned);
 inline FrameAttachment operator,(FrameAttachment fa, unsigned i)
 { return make_indexed_attachment(fa, i); }
 
-inline unsigned get_attach_point(std::uint16_t fa)
+inline unsigned get_attach_point(FrameAttachment fa)
 { return fa>>10; }
 
-PixelFormat get_attachment_pixelformat(std::uint16_t);
+PixelFormat get_attachment_pixelformat(FrameAttachment);
 
 GLenum get_gl_attachment(FrameAttachment);
 
index 9bfca5d295ad9688303d575434999700a368240e..8e358acd2d0394992925f05b26acd632b4427845 100644 (file)
@@ -38,8 +38,8 @@ VertexFormat VertexFormat::operator,(DataType t) const
                throw invalid_operation("VertexFormat::operator,");
 
        VertexFormat r = *this;
-       uint16_t &a = r.attributes[r.count-1];
-       a = make_typed_attribute(static_cast<VertexAttribute>(a), t);
+       VertexAttribute &a = r.attributes[r.count-1];
+       a = make_typed_attribute(a, t);
 
        return r;
 }
@@ -50,8 +50,8 @@ VertexFormat VertexFormat::operator,(unsigned i) const
                throw invalid_operation("VertexFormat::operator,");
 
        VertexFormat r = *this;
-       uint16_t &a = r.attributes[r.count-1];
-       a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
+       VertexAttribute &a = r.attributes[r.count-1];
+       a = make_indexed_attribute(a, i);
 
        return r;
 }
@@ -66,8 +66,8 @@ bool VertexFormat::operator==(const VertexFormat &other) const
 unsigned VertexFormat::stride() const
 {
        unsigned s = 0;
-       for(const uint16_t *i=begin(); i!=end(); ++i)
-               s += get_attribute_size(*i);
+       for(VertexAttribute a: *this)
+               s += get_attribute_size(a);
        return s;
 }
 
@@ -75,18 +75,18 @@ int VertexFormat::offset(VertexAttribute attr) const
 {
        unsigned sem = get_attribute_semantic(attr);
        unsigned offs = 0;
-       for(const uint16_t *i=begin(); i!=end(); ++i)
+       for(VertexAttribute a: *this)
        {
-               if(get_attribute_semantic(*i)==sem)
+               if(get_attribute_semantic(a)==sem)
                {
-                       if(get_attribute_source_type(*i)==get_attribute_source_type(attr) &&
-                               get_attribute_component_count(*i)>=get_attribute_component_count(attr))
+                       if(get_attribute_source_type(a)==get_attribute_source_type(attr) &&
+                               get_attribute_component_count(a)>=get_attribute_component_count(attr))
                                return offs;
                        else
                                return -1;
                }
                else
-                       offs += get_attribute_size(*i);
+                       offs += get_attribute_size(a);
        }
 
        return -1;
@@ -105,14 +105,14 @@ VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
 
 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
 {
-       unsigned base = attr;
+       VertexAttribute base = attr;
        if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
        {
                if(index>=4)
                        throw out_of_range("make_indexed_attribute");
        }
        else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1))
-               base &= 0x3FF;
+               base = static_cast<VertexAttribute>(base&0x3FF);
        else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1))
                throw invalid_argument("make_indexed_attribute");
 
index 85d525f9571e90d276cff19e9928c9bfb9d1cc3e..f9f99398fc7fe66186a74ad7836e2567fc02cf0c 100644 (file)
@@ -31,7 +31,7 @@ nnnn nn_f gsss iccc
 This information is presented for internal documentation purposes only; it is
 inadvisable for programs to rely on it.
 */
-enum VertexAttribute
+enum VertexAttribute: std::uint16_t
 {
        VERTEX2 = 0x01C2,
        VERTEX3 = 0x01C3,
@@ -76,7 +76,7 @@ private:
        enum { MAX_ATTRIBUTES = 15 };
 
        std::uint8_t count;
-       std::uint16_t attributes[MAX_ATTRIBUTES];
+       VertexAttribute attributes[MAX_ATTRIBUTES];
 
 public:
        VertexFormat();
@@ -89,8 +89,8 @@ public:
        bool operator!=(const VertexFormat &other) const { return !(*this==other); }
 
        bool empty() const { return !count; }
-       const std::uint16_t *begin() const { return attributes; }
-       const std::uint16_t *end() const { return attributes+count; }
+       const VertexAttribute *begin() const { return attributes; }
+       const VertexAttribute *end() const { return attributes+count; }
        unsigned stride() const;
        int offset(VertexAttribute) const;
 };
@@ -108,19 +108,19 @@ VertexAttribute make_indexed_attribute(VertexAttribute, unsigned);
 inline VertexAttribute operator,(VertexAttribute a, unsigned i)
 { return make_indexed_attribute(a, i); }
 
-inline unsigned get_attribute_semantic(std::uint16_t a)
+inline unsigned get_attribute_semantic(VertexAttribute a)
 { return a>>10; }
 
-inline DataType get_attribute_source_type(std::uint16_t a)
+inline DataType get_attribute_source_type(VertexAttribute a)
 { return static_cast<DataType>((a&0x70)>>4 | (a&0x180)<<1); }
 
-inline unsigned get_attribute_component_count(std::uint16_t a)
+inline unsigned get_attribute_component_count(VertexAttribute a)
 { return a&7; }
 
-inline unsigned get_attribute_size(std::uint16_t a)
+inline unsigned get_attribute_size(VertexAttribute a)
 { return get_attribute_component_count(a)*get_type_size(get_attribute_source_type(a)); }
 
-inline bool is_integer_attribute(std::uint16_t a)
+inline bool is_integer_attribute(VertexAttribute a)
 { return a&8; }
 
 void operator>>(const LexicalConverter &, VertexAttribute &);
index 75d3ad8bc11017966d249adbe4762e2b235cab0f..0971aed1c62f08c8c2823e873bcf86ce5ba15ba4 100644 (file)
@@ -107,8 +107,8 @@ bool VertexSetup::verify_format(const VertexFormat &fmt)
 
        unsigned max_attribs = Limits::get_global().max_vertex_attributes;
 
-       for(const uint16_t *a=fmt.begin(); a!=fmt.end(); ++a)
-               if(get_attribute_semantic(*a)>=max_attribs)
+       for(VertexAttribute a: fmt)
+               if(get_attribute_semantic(a)>=max_attribs)
                        return false;
 
        return true;
@@ -117,8 +117,8 @@ bool VertexSetup::verify_format(const VertexFormat &fmt)
 void VertexSetup::require_format(const VertexFormat &fmt)
 {
        bool has_int = false;
-       for(const uint16_t *a=fmt.begin(); a!=fmt.end(); ++a)
-               has_int = has_int | is_integer_attribute(*a);
+       for(VertexAttribute a: fmt)
+               has_int = has_int | is_integer_attribute(a);
 
        if(has_int)
                static Require _req(EXT_gpu_shader4);
@@ -162,12 +162,12 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding
        }
 
        unsigned offset = 0;
-       for(const uint16_t *a=fmt.begin(); a!=fmt.end(); ++a)
+       for(VertexAttribute a: fmt)
        {
-               unsigned sem = get_attribute_semantic(*a);
-               bool integer = is_integer_attribute(*a);
-               GLenum type = get_gl_type(get_attribute_source_type(*a));
-               unsigned cc = get_attribute_component_count(*a);
+               unsigned sem = get_attribute_semantic(a);
+               bool integer = is_integer_attribute(a);
+               GLenum type = get_gl_type(get_attribute_source_type(a));
+               unsigned cc = get_attribute_component_count(a);
                if(direct)
                {
                        if(integer)
@@ -187,7 +187,7 @@ void VertexSetup::update_vertex_array(const VertexArray &array, unsigned binding
                                glVertexAttribDivisor(sem, divisor);
                        glEnableVertexAttribArray(sem);
                }
-               offset += get_attribute_size(*a);
+               offset += get_attribute_size(a);
        }
 
        if(!direct)
@@ -207,15 +207,15 @@ void VertexSetup::unload()
                glBindVertexArray(id);
                glBindBuffer(GL_ARRAY_BUFFER, 0);
 
-               for(const uint16_t *a=vertex_format.begin(); a!=vertex_format.end(); ++a)
+               for(VertexAttribute a: vertex_format)
                {
-                       unsigned sem = get_attribute_semantic(*a);
+                       unsigned sem = get_attribute_semantic(a);
                        glDisableVertexAttribArray(sem);
                        glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
                }
-               for(const uint16_t *a=inst_format.begin(); a!=inst_format.end(); ++a)
+               for(VertexAttribute a: inst_format)
                {
-                       unsigned sem = get_attribute_semantic(*a);
+                       unsigned sem = get_attribute_semantic(a);
                        glDisableVertexAttribArray(sem);
                        glVertexAttribPointer(sem, 1, GL_FLOAT, false, 0, 0);
                }
index 302b983f478ecd34a1d67ebc92fd3ecec33f7d43..b2dbef2c9f498b35c34e478aa9894bce12b29090 100644 (file)
@@ -17,23 +17,22 @@ RenderTarget::RenderTarget(unsigned w, unsigned h, const FrameFormat &f):
 {
        textures.reserve(f.size());
        unsigned samples = f.get_samples();
-       for(const uint16_t *i=f.begin(); i!=f.end(); ++i)
+       for(FrameAttachment a: f)
        {
-               FrameAttachment fa = static_cast<FrameAttachment>(*i);
-               PixelFormat pf = get_attachment_pixelformat(*i);
+               PixelFormat pf = get_attachment_pixelformat(a);
 
                if(samples>1)
                {
                        Texture2DMultisample *tex2d_ms = new Texture2DMultisample;
                        tex2d_ms->storage(pf, width, height, samples);
-                       fbo.attach(fa, *tex2d_ms);
+                       fbo.attach(a, *tex2d_ms);
                        textures.push_back(tex2d_ms);
                }
                else
                {
                        Texture2D *tex2d = new Texture2D;
                        tex2d->storage(pf, width, height, 1);
-                       fbo.attach(fa, *tex2d);
+                       fbo.attach(a, *tex2d);
                        textures.push_back(tex2d);
                }
        }
@@ -68,11 +67,10 @@ void RenderTarget::set_debug_name(const string &name)
 {
 #ifdef DEBUG
        fbo.set_debug_name(name+" [FBO]");
-       const FrameFormat &fmt = fbo.get_format();
        unsigned i = 0;
-       for(const uint16_t *j=fmt.begin(); j!=fmt.end(); ++i, ++j)
+       for(FrameAttachment a: fbo.get_format())
        {
-               unsigned attach_pt = get_attach_point(static_cast<FrameAttachment>(*j));
+               unsigned attach_pt = get_attach_point(a);
 
                string tex_name;
                if(attach_pt==get_attach_point(DEPTH_ATTACHMENT))
@@ -82,7 +80,7 @@ void RenderTarget::set_debug_name(const string &name)
                else
                        tex_name = Msp::format("%s/color%d", name, attach_pt);
 
-               textures[i]->set_debug_name(tex_name+".tex2d");
+               textures[i++]->set_debug_name(tex_name+".tex2d");
        }
 #else
        (void)name;