From efb45a6851563cdb8077b6aad3ab92d4006d8790 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 20 Mar 2021 13:44:09 +0200 Subject: [PATCH] Decouple DataType from the OpenGL constants This allows defining the values so that manipulation of related types is easier. And I can't keep relying on the GL values anyway, with the goal of adding a Vulkan backend. --- source/core/batch.cpp | 6 ++++-- source/core/batch.h | 1 + source/core/datatype.cpp | 21 +++++++++++---------- source/core/datatype.h | 22 ++++++++++++---------- source/core/texture1d.cpp | 4 ++-- source/core/texture2d.cpp | 4 ++-- source/core/texture3d.cpp | 4 ++-- source/core/texturecube.cpp | 4 ++-- 8 files changed, 36 insertions(+), 30 deletions(-) diff --git a/source/core/batch.cpp b/source/core/batch.cpp index f14cc307..68e232ba 100644 --- a/source/core/batch.cpp +++ b/source/core/batch.cpp @@ -55,6 +55,7 @@ unsigned Batch::restart_index = 0; Batch::Batch(PrimitiveType t): prim_type(t), index_type(UNSIGNED_SHORT), + gl_index_type(get_gl_type(index_type)), max_index(0), restart(false) { } @@ -76,6 +77,7 @@ void Batch::set_index_type(DataType t) shrink(data); index_type = t; + gl_index_type = get_gl_type(t); update_offset(); dirty = true; } @@ -189,7 +191,7 @@ void Batch::draw() const BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER); const void *data_ptr = setup_draw(); - glDrawElements(prim_type, size(), index_type, data_ptr); + glDrawElements(prim_type, size(), gl_index_type, data_ptr); } void Batch::draw_instanced(unsigned count) const @@ -199,7 +201,7 @@ void Batch::draw_instanced(unsigned count) const BindRestore _bind_ibuf(get_buffer(), ELEMENT_ARRAY_BUFFER); const void *data_ptr = setup_draw(); - glDrawElementsInstanced(prim_type, size(), index_type, data_ptr, count); + glDrawElementsInstanced(prim_type, size(), gl_index_type, data_ptr, count); } const void *Batch::setup_draw() const diff --git a/source/core/batch.h b/source/core/batch.h index 2e4ad9ef..0435feec 100644 --- a/source/core/batch.h +++ b/source/core/batch.h @@ -34,6 +34,7 @@ public: private: PrimitiveType prim_type; DataType index_type; + GLenum gl_index_type; std::vector data; unsigned max_index; bool restart; diff --git a/source/core/datatype.cpp b/source/core/datatype.cpp index 1def3ca3..7488a68b 100644 --- a/source/core/datatype.cpp +++ b/source/core/datatype.cpp @@ -1,4 +1,5 @@ #include +#include #include "datatype.h" using namespace std; @@ -6,19 +7,19 @@ using namespace std; namespace Msp { namespace GL { -unsigned get_type_size(DataType type) +GLenum get_gl_type(DataType type) { switch(type) { - case BYTE: - case UNSIGNED_BYTE: return 1; - case SHORT: - case UNSIGNED_SHORT: - case HALF_FLOAT: return 2; - case INT: - case UNSIGNED_INT: - case FLOAT: return 4; - default: throw invalid_argument("get_type_size"); + case BYTE: return GL_BYTE; + case UNSIGNED_BYTE: return GL_UNSIGNED_BYTE; + case SHORT: return GL_SHORT; + case UNSIGNED_SHORT: return GL_UNSIGNED_SHORT; + case INT: return GL_INT; + case UNSIGNED_INT: return GL_UNSIGNED_INT; + case FLOAT: return GL_FLOAT; + case HALF_FLOAT: return GL_HALF_FLOAT; + default: throw invalid_argument("get_gl_type"); } } diff --git a/source/core/datatype.h b/source/core/datatype.h index 2d783f61..a343e8cf 100644 --- a/source/core/datatype.h +++ b/source/core/datatype.h @@ -2,24 +2,26 @@ #define MSP_GL_DATATYPE_H_ #include "gl.h" -#include namespace Msp { namespace GL { enum DataType { - BYTE = GL_BYTE, - UNSIGNED_BYTE = GL_UNSIGNED_BYTE, - SHORT = GL_SHORT, - UNSIGNED_SHORT = GL_UNSIGNED_SHORT, - INT = GL_INT, - UNSIGNED_INT = GL_UNSIGNED_INT, - FLOAT = GL_FLOAT, - HALF_FLOAT = GL_HALF_FLOAT + BYTE = 0x101, + UNSIGNED_BYTE = 0x001, + SHORT = 0x102, + UNSIGNED_SHORT = 0x002, + INT = 0x104, + UNSIGNED_INT = 0x004, + FLOAT = 0x304, + HALF_FLOAT = 0x302 }; -unsigned get_type_size(DataType); +inline unsigned get_type_size(DataType t) +{ return t&0xFF; } + +GLenum get_gl_type(DataType); } // namespace GL } // namespace Msp diff --git a/source/core/texture1d.cpp b/source/core/texture1d.cpp index 742857be..1e90b6f7 100644 --- a/source/core/texture1d.cpp +++ b/source/core/texture1d.cpp @@ -75,7 +75,7 @@ void Texture1D::image(unsigned level, const void *data) } PixelComponents comp = get_components(storage_fmt); - DataType type = get_component_type(storage_fmt); + GLenum type = get_gl_type(get_component_type(storage_fmt)); glTexImage1D(target, level, storage_fmt, w, 0, comp, type, data); allocated |= 1<