]> git.tdb.fi Git - libs/gl.git/commitdiff
Decouple DataType from the OpenGL constants
authorMikko Rasa <tdb@tdb.fi>
Sat, 20 Mar 2021 11:44:09 +0000 (13:44 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 20 Mar 2021 11:44:09 +0000 (13:44 +0200)
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
source/core/batch.h
source/core/datatype.cpp
source/core/datatype.h
source/core/texture1d.cpp
source/core/texture2d.cpp
source/core/texture3d.cpp
source/core/texturecube.cpp

index f14cc307ff71b92deb212e9f759ac70f1c8d69f0..68e232ba29de5447fd9050d27894000f2765e86f 100644 (file)
@@ -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<UInt32, UInt16>(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
index 2e4ad9ef3005d9d01c15dbdc4d119ce79f570460..0435feec29eb6ccfa874c44530823f53c564e3b7 100644 (file)
@@ -34,6 +34,7 @@ public:
 private:
        PrimitiveType prim_type;
        DataType index_type;
+       GLenum gl_index_type;
        std::vector<UInt8> data;
        unsigned max_index;
        bool restart;
index 1def3ca31fa7363ff724448198f751cf6a8688b8..7488a68bd4e7334c217877dda705575828cf65b1 100644 (file)
@@ -1,4 +1,5 @@
 #include <stdexcept>
+#include <msp/gl/extensions/nv_half_float.h>
 #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");
        }
 }
 
index 2d783f6142cdc1dadfb0e8acb5411ddaa41c4221..a343e8cf2be517399130f8f40fe0f57f644cbb00 100644 (file)
@@ -2,24 +2,26 @@
 #define MSP_GL_DATATYPE_H_
 
 #include "gl.h"
-#include <msp/gl/extensions/nv_half_float.h>
 
 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
index 742857be725b4ed205b369c43d823f5b0812af14..1e90b6f7264d56d1f37c32afc9255157eca743b3 100644 (file)
@@ -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<<level;
@@ -102,7 +102,7 @@ void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
        allocate(level);
 
        PixelComponents comp = get_components(storage_fmt);
-       DataType type = get_component_type(storage_fmt);
+       GLenum type = get_gl_type(get_component_type(storage_fmt));
        if(ARB_direct_state_access)
                glTextureSubImage1D(id, level, x, wd, comp, type, data);
        else
index 0e76128c7bc5666da5828f11fe98f5398ecaf380..d2addda70fc4d6e35de11e2df472bf98b89d9940 100644 (file)
@@ -106,7 +106,7 @@ void Texture2D::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));
        glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
 
        allocated |= 1<<level;
@@ -133,7 +133,7 @@ void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht
        allocate(level);
 
        PixelComponents comp = get_components(storage_fmt);
-       DataType type = get_component_type(storage_fmt);
+       GLenum type = get_gl_type(get_component_type(storage_fmt));
        if(ARB_direct_state_access)
                glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
        else
index 03c58d0afdf05e6273cfbbea2892ea2d7e067ca9..1681f83c6f528b949be627adb9fddf311556806a 100644 (file)
@@ -94,7 +94,7 @@ void Texture3D::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));
        glTexImage3D(target, level, storage_fmt, width, height, depth, 0, comp, type, data);
 
        allocated |= 1<<level;
@@ -121,7 +121,7 @@ void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsi
        allocate(level);
 
        PixelComponents comp = get_components(storage_fmt);
-       DataType type = get_component_type(storage_fmt);
+       GLenum type = get_gl_type(get_component_type(storage_fmt));
        if(ARB_direct_state_access)
                glTextureSubImage3D(id, level, x, y, z, wd, ht, dp, comp, type, data);
        else
index 6ec3662523dea02903e024bb9113b91125e4b711..d006e383cc998af09fa478b18bb8bc4ce047ecfc 100644 (file)
@@ -109,7 +109,7 @@ void TextureCube::image(TextureCubeFace face, 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));
        glTexImage2D(face, level, storage_fmt, s, s, 0, comp, type, data);
 
        if(level==0)
@@ -151,7 +151,7 @@ void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y,
        allocate(level);
 
        PixelComponents comp = get_components(storage_fmt);
-       DataType type = get_component_type(storage_fmt);
+       GLenum type = get_gl_type(get_component_type(storage_fmt));
        glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
 
        if(auto_gen_mipmap && level==0)