From a4d83d3748cdde8cf30683d36a040d3dfacfd693 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 18 Sep 2021 02:56:23 +0300 Subject: [PATCH] Specify underlying type for format descriptor enums This makes it possible to use them directory in the associated format structs without increasing their size. --- source/builders/primitivebuilder.cpp | 5 ++--- source/builders/vertexarraybuilder.cpp | 13 ++++++------ source/core/framebuffer.cpp | 13 ++++++++---- source/core/frameformat.cpp | 10 ++++----- source/core/frameformat.h | 12 +++++------ source/core/vertexformat.cpp | 26 ++++++++++++------------ source/core/vertexformat.h | 18 ++++++++--------- source/core/vertexsetup.cpp | 28 +++++++++++++------------- source/render/rendertarget.cpp | 16 +++++++-------- 9 files changed, 71 insertions(+), 70 deletions(-) diff --git a/source/builders/primitivebuilder.cpp b/source/builders/primitivebuilder.cpp index ca16fcde..85c695c3 100644 --- a/source/builders/primitivebuilder.cpp +++ b/source/builders/primitivebuilder.cpp @@ -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(ptr, value, false, cc); } - ptr += get_attribute_size(*a); + ptr += get_attribute_size(a); } } diff --git a/source/core/framebuffer.cpp b/source/core/framebuffer.cpp index e9d8863c..851b6431 100644 --- a/source/core/framebuffer.cpp +++ b/source/core/framebuffer.cpp @@ -131,9 +131,9 @@ void Framebuffer::update() const vector 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(*j)); + GLenum gl_attach_point = get_gl_attachment(a); if(dirty&(1<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<(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(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)) diff --git a/source/core/frameformat.h b/source/core/frameformat.h index 35d982ba..c80f63e0 100644 --- a/source/core/frameformat.h +++ b/source/core/frameformat.h @@ -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); diff --git a/source/core/vertexformat.cpp b/source/core/vertexformat.cpp index 9bfca5d2..8e358acd 100644 --- a/source/core/vertexformat.cpp +++ b/source/core/vertexformat.cpp @@ -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(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(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(base&0x3FF); else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1)) throw invalid_argument("make_indexed_attribute"); diff --git a/source/core/vertexformat.h b/source/core/vertexformat.h index 85d525f9..f9f99398 100644 --- a/source/core/vertexformat.h +++ b/source/core/vertexformat.h @@ -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((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 &); diff --git a/source/core/vertexsetup.cpp b/source/core/vertexsetup.cpp index 75d3ad8b..0971aed1 100644 --- a/source/core/vertexsetup.cpp +++ b/source/core/vertexsetup.cpp @@ -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); } diff --git a/source/render/rendertarget.cpp b/source/render/rendertarget.cpp index 302b983f..b2dbef2c 100644 --- a/source/render/rendertarget.cpp +++ b/source/render/rendertarget.cpp @@ -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(*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(*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; -- 2.43.0