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