]> git.tdb.fi Git - libs/gl.git/blob - source/core/datatype.cpp
Guard against hitting the end of the array in get_gl_type
[libs/gl.git] / source / core / datatype.cpp
1 #include <algorithm>
2 #include <stdexcept>
3 #include <msp/gl/extensions/arb_gpu_shader_fp64.h>
4 #include <msp/gl/extensions/nv_half_float.h>
5 #include <msp/gl/extensions/nv_non_square_matrices.h>
6 #include "datatype.h"
7
8 using namespace std;
9
10 namespace {
11
12 struct MappedType
13 {
14         Msp::GL::DataType type;
15         GLenum gl_type;
16 };
17
18 // Make sure this is sorted!
19 const MappedType type_map[] =
20 {
21         { Msp::GL::UNSIGNED_BYTE, GL_UNSIGNED_BYTE },
22         { Msp::GL::UNSIGNED_SHORT, GL_UNSIGNED_SHORT },
23         { Msp::GL::UNSIGNED_INT, GL_UNSIGNED_INT },
24         { Msp::GL::BYTE, GL_BYTE },
25         { Msp::GL::SHORT, GL_SHORT },
26         { Msp::GL::INT, GL_INT },
27         { Msp::GL::HALF_FLOAT, GL_HALF_FLOAT },
28         { Msp::GL::FLOAT, GL_FLOAT },
29         { Msp::GL::DOUBLE, GL_DOUBLE },
30         { Msp::GL::BOOL, GL_BOOL },
31         { Msp::GL::INT_VEC2, GL_INT_VEC2 },
32         { Msp::GL::FLOAT_VEC2, GL_FLOAT_VEC2 },
33         { Msp::GL::BOOL_VEC2, GL_BOOL_VEC2 },
34         { Msp::GL::INT_VEC3, GL_INT_VEC3 },
35         { Msp::GL::FLOAT_VEC3, GL_FLOAT_VEC3 },
36         { Msp::GL::BOOL_VEC3, GL_BOOL_VEC3 },
37         { Msp::GL::INT_VEC4, GL_INT_VEC4 },
38         { Msp::GL::FLOAT_VEC4, GL_FLOAT_VEC4 },
39         { Msp::GL::BOOL_VEC4, GL_BOOL_VEC4 },
40         { Msp::GL::FLOAT_MAT2, GL_FLOAT_MAT2 },
41         { Msp::GL::DOUBLE_MAT2, GL_DOUBLE_MAT2 },
42         { Msp::GL::FLOAT_MAT3, GL_FLOAT_MAT3 },
43         { Msp::GL::DOUBLE_MAT3, GL_DOUBLE_MAT3 },
44         { Msp::GL::FLOAT_MAT4, GL_FLOAT_MAT4 },
45         { Msp::GL::DOUBLE_MAT4, GL_DOUBLE_MAT4 },
46         { Msp::GL::FLOAT_MAT2x3, GL_FLOAT_MAT2x3 },
47         { Msp::GL::DOUBLE_MAT2x3, GL_DOUBLE_MAT2x3 },
48         { Msp::GL::FLOAT_MAT3x2, GL_FLOAT_MAT3x2 },
49         { Msp::GL::DOUBLE_MAT3x2, GL_DOUBLE_MAT3x2 },
50         { Msp::GL::FLOAT_MAT2x4, GL_FLOAT_MAT2x4 },
51         { Msp::GL::DOUBLE_MAT2x4, GL_DOUBLE_MAT2x4 },
52         { Msp::GL::FLOAT_MAT4x2, GL_FLOAT_MAT4x2 },
53         { Msp::GL::DOUBLE_MAT4x2, GL_DOUBLE_MAT4x2 },
54         { Msp::GL::FLOAT_MAT3x4, GL_FLOAT_MAT3x4 },
55         { Msp::GL::DOUBLE_MAT3x4, GL_DOUBLE_MAT3x4 },
56         { Msp::GL::FLOAT_MAT4x3, GL_FLOAT_MAT4x3 },
57         { Msp::GL::DOUBLE_MAT4x3, GL_DOUBLE_MAT4x3 },
58         { Msp::GL::IMAGE_1D, GL_IMAGE_1D },
59         { Msp::GL::IMAGE_2D, GL_IMAGE_2D },
60         { Msp::GL::IMAGE_3D, GL_IMAGE_3D },
61         { Msp::GL::IMAGE_CUBE, GL_IMAGE_CUBE },
62         { Msp::GL::IMAGE_1D_ARRAY, GL_IMAGE_1D_ARRAY },
63         { Msp::GL::IMAGE_2D_ARRAY, GL_IMAGE_2D_ARRAY },
64         { Msp::GL::IMAGE_CUBE_ARRAY, GL_IMAGE_CUBE_MAP_ARRAY },
65         { Msp::GL::SAMPLER_1D, GL_SAMPLER_1D },
66         { Msp::GL::SAMPLER_2D, GL_SAMPLER_2D },
67         { Msp::GL::SAMPLER_3D, GL_SAMPLER_3D },
68         { Msp::GL::SAMPLER_CUBE, GL_SAMPLER_CUBE },
69         { Msp::GL::SAMPLER_1D_ARRAY, GL_SAMPLER_1D_ARRAY },
70         { Msp::GL::SAMPLER_2D_ARRAY, GL_SAMPLER_2D_ARRAY },
71         { Msp::GL::SAMPLER_CUBE_ARRAY, GL_SAMPLER_CUBE_MAP_ARRAY },
72         { Msp::GL::SAMPLER_1D_SHADOW, GL_SAMPLER_1D_SHADOW },
73         { Msp::GL::SAMPLER_2D_SHADOW, GL_SAMPLER_2D_SHADOW },
74         { Msp::GL::SAMPLER_CUBE_SHADOW, GL_SAMPLER_CUBE_SHADOW },
75         { Msp::GL::SAMPLER_1D_ARRAY_SHADOW, GL_SAMPLER_1D_ARRAY_SHADOW },
76         { Msp::GL::SAMPLER_2D_ARRAY_SHADOW, GL_SAMPLER_2D_ARRAY_SHADOW },
77         { Msp::GL::SAMPLER_CUBE_ARRAY_SHADOW, GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW }
78 };
79 const unsigned type_map_size = sizeof(type_map)/sizeof(MappedType);
80
81 bool type_compare(const MappedType &mt, Msp::GL::DataType t)
82 { return mt.type<t; }
83
84 }
85
86 namespace Msp {
87 namespace GL {
88
89 GLenum get_gl_type(DataType type)
90 {
91         const MappedType *end = type_map+type_map_size;
92         const MappedType *ptr = lower_bound(type_map, end, type, type_compare);
93         if(ptr==end || ptr->type!=type)
94                 throw invalid_argument("get_gl_type");
95         return ptr->gl_type;
96 }
97
98 DataType from_gl_type(GLenum gl_type)
99 {
100         for(unsigned i=0; i<type_map_size; ++i)
101                 if(type_map[i].gl_type==gl_type)
102                         return type_map[i].type;
103         throw invalid_argument("from_gl_type");
104 }
105
106 void require_type(DataType type)
107 {
108         unsigned rows = ((type>>12)&3)+1;
109         unsigned cols = ((type>>14)&4)+1;
110         if(rows>1 && cols>1 && rows!=cols)
111                 static Require _req(NV_non_square_matrices);
112         if((type&0x200) && get_type_size(type)/(rows*cols)==8)
113                 static Require _req(ARB_gpu_shader_fp64);
114 }
115
116 } // namespace GL
117 } // namespace Msp