]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/datatype.cpp
Guard against hitting the end of the array in get_gl_type
[libs/gl.git] / source / core / datatype.cpp
index 1def3ca31fa7363ff724448198f751cf6a8688b8..4dd0b89fc2f8a2e7bd6c86bd75e9fd3465c4cbc3 100644 (file)
+#include <algorithm>
 #include <stdexcept>
+#include <msp/gl/extensions/arb_gpu_shader_fp64.h>
+#include <msp/gl/extensions/nv_half_float.h>
+#include <msp/gl/extensions/nv_non_square_matrices.h>
 #include "datatype.h"
 
 using namespace std;
 
+namespace {
+
+struct MappedType
+{
+       Msp::GL::DataType type;
+       GLenum gl_type;
+};
+
+// Make sure this is sorted!
+const MappedType type_map[] =
+{
+       { Msp::GL::UNSIGNED_BYTE, GL_UNSIGNED_BYTE },
+       { Msp::GL::UNSIGNED_SHORT, GL_UNSIGNED_SHORT },
+       { Msp::GL::UNSIGNED_INT, GL_UNSIGNED_INT },
+       { Msp::GL::BYTE, GL_BYTE },
+       { Msp::GL::SHORT, GL_SHORT },
+       { Msp::GL::INT, GL_INT },
+       { Msp::GL::HALF_FLOAT, GL_HALF_FLOAT },
+       { Msp::GL::FLOAT, GL_FLOAT },
+       { Msp::GL::DOUBLE, GL_DOUBLE },
+       { Msp::GL::BOOL, GL_BOOL },
+       { Msp::GL::INT_VEC2, GL_INT_VEC2 },
+       { Msp::GL::FLOAT_VEC2, GL_FLOAT_VEC2 },
+       { Msp::GL::BOOL_VEC2, GL_BOOL_VEC2 },
+       { Msp::GL::INT_VEC3, GL_INT_VEC3 },
+       { Msp::GL::FLOAT_VEC3, GL_FLOAT_VEC3 },
+       { Msp::GL::BOOL_VEC3, GL_BOOL_VEC3 },
+       { Msp::GL::INT_VEC4, GL_INT_VEC4 },
+       { Msp::GL::FLOAT_VEC4, GL_FLOAT_VEC4 },
+       { Msp::GL::BOOL_VEC4, GL_BOOL_VEC4 },
+       { Msp::GL::FLOAT_MAT2, GL_FLOAT_MAT2 },
+       { Msp::GL::DOUBLE_MAT2, GL_DOUBLE_MAT2 },
+       { Msp::GL::FLOAT_MAT3, GL_FLOAT_MAT3 },
+       { Msp::GL::DOUBLE_MAT3, GL_DOUBLE_MAT3 },
+       { Msp::GL::FLOAT_MAT4, GL_FLOAT_MAT4 },
+       { Msp::GL::DOUBLE_MAT4, GL_DOUBLE_MAT4 },
+       { Msp::GL::FLOAT_MAT2x3, GL_FLOAT_MAT2x3 },
+       { Msp::GL::DOUBLE_MAT2x3, GL_DOUBLE_MAT2x3 },
+       { Msp::GL::FLOAT_MAT3x2, GL_FLOAT_MAT3x2 },
+       { Msp::GL::DOUBLE_MAT3x2, GL_DOUBLE_MAT3x2 },
+       { Msp::GL::FLOAT_MAT2x4, GL_FLOAT_MAT2x4 },
+       { Msp::GL::DOUBLE_MAT2x4, GL_DOUBLE_MAT2x4 },
+       { Msp::GL::FLOAT_MAT4x2, GL_FLOAT_MAT4x2 },
+       { Msp::GL::DOUBLE_MAT4x2, GL_DOUBLE_MAT4x2 },
+       { Msp::GL::FLOAT_MAT3x4, GL_FLOAT_MAT3x4 },
+       { Msp::GL::DOUBLE_MAT3x4, GL_DOUBLE_MAT3x4 },
+       { Msp::GL::FLOAT_MAT4x3, GL_FLOAT_MAT4x3 },
+       { Msp::GL::DOUBLE_MAT4x3, GL_DOUBLE_MAT4x3 },
+       { Msp::GL::IMAGE_1D, GL_IMAGE_1D },
+       { Msp::GL::IMAGE_2D, GL_IMAGE_2D },
+       { Msp::GL::IMAGE_3D, GL_IMAGE_3D },
+       { Msp::GL::IMAGE_CUBE, GL_IMAGE_CUBE },
+       { Msp::GL::IMAGE_1D_ARRAY, GL_IMAGE_1D_ARRAY },
+       { Msp::GL::IMAGE_2D_ARRAY, GL_IMAGE_2D_ARRAY },
+       { Msp::GL::IMAGE_CUBE_ARRAY, GL_IMAGE_CUBE_MAP_ARRAY },
+       { Msp::GL::SAMPLER_1D, GL_SAMPLER_1D },
+       { Msp::GL::SAMPLER_2D, GL_SAMPLER_2D },
+       { Msp::GL::SAMPLER_3D, GL_SAMPLER_3D },
+       { Msp::GL::SAMPLER_CUBE, GL_SAMPLER_CUBE },
+       { Msp::GL::SAMPLER_1D_ARRAY, GL_SAMPLER_1D_ARRAY },
+       { Msp::GL::SAMPLER_2D_ARRAY, GL_SAMPLER_2D_ARRAY },
+       { Msp::GL::SAMPLER_CUBE_ARRAY, GL_SAMPLER_CUBE_MAP_ARRAY },
+       { Msp::GL::SAMPLER_1D_SHADOW, GL_SAMPLER_1D_SHADOW },
+       { Msp::GL::SAMPLER_2D_SHADOW, GL_SAMPLER_2D_SHADOW },
+       { Msp::GL::SAMPLER_CUBE_SHADOW, GL_SAMPLER_CUBE_SHADOW },
+       { Msp::GL::SAMPLER_1D_ARRAY_SHADOW, GL_SAMPLER_1D_ARRAY_SHADOW },
+       { Msp::GL::SAMPLER_2D_ARRAY_SHADOW, GL_SAMPLER_2D_ARRAY_SHADOW },
+       { Msp::GL::SAMPLER_CUBE_ARRAY_SHADOW, GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW }
+};
+const unsigned type_map_size = sizeof(type_map)/sizeof(MappedType);
+
+bool type_compare(const MappedType &mt, Msp::GL::DataType t)
+{ return mt.type<t; }
+
+}
+
 namespace Msp {
 namespace GL {
 
-unsigned get_type_size(DataType type)
+GLenum get_gl_type(DataType type)
+{
+       const MappedType *end = type_map+type_map_size;
+       const MappedType *ptr = lower_bound(type_map, end, type, type_compare);
+       if(ptr==end || ptr->type!=type)
+               throw invalid_argument("get_gl_type");
+       return ptr->gl_type;
+}
+
+DataType from_gl_type(GLenum gl_type)
+{
+       for(unsigned i=0; i<type_map_size; ++i)
+               if(type_map[i].gl_type==gl_type)
+                       return type_map[i].type;
+       throw invalid_argument("from_gl_type");
+}
+
+void require_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");
-       }
+       unsigned rows = ((type>>12)&3)+1;
+       unsigned cols = ((type>>14)&4)+1;
+       if(rows>1 && cols>1 && rows!=cols)
+               static Require _req(NV_non_square_matrices);
+       if((type&0x200) && get_type_size(type)/(rows*cols)==8)
+               static Require _req(ARB_gpu_shader_fp64);
 }
 
 } // namespace GL