]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/datatype_backend.cpp
Store query pool size separately, in the common part of the class
[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::INT_VEC2, GL_INT_VEC2 },
34         { Msp::GL::FLOAT_VEC2, GL_FLOAT_VEC2 },
35         { Msp::GL::BOOL_VEC2, GL_BOOL_VEC2 },
36         { Msp::GL::INT_VEC3, GL_INT_VEC3 },
37         { Msp::GL::FLOAT_VEC3, GL_FLOAT_VEC3 },
38         { Msp::GL::BOOL_VEC3, GL_BOOL_VEC3 },
39         { Msp::GL::INT_VEC4, GL_INT_VEC4 },
40         { Msp::GL::FLOAT_VEC4, GL_FLOAT_VEC4 },
41         { Msp::GL::BOOL_VEC4, GL_BOOL_VEC4 },
42         { Msp::GL::FLOAT_MAT2, GL_FLOAT_MAT2 },
43         { Msp::GL::DOUBLE_MAT2, GL_DOUBLE_MAT2 },
44         { Msp::GL::FLOAT_MAT3, GL_FLOAT_MAT3 },
45         { Msp::GL::DOUBLE_MAT3, GL_DOUBLE_MAT3 },
46         { Msp::GL::FLOAT_MAT4, GL_FLOAT_MAT4 },
47         { Msp::GL::DOUBLE_MAT4, GL_DOUBLE_MAT4 },
48         { Msp::GL::FLOAT_MAT2x3, GL_FLOAT_MAT2x3 },
49         { Msp::GL::DOUBLE_MAT2x3, GL_DOUBLE_MAT2x3 },
50         { Msp::GL::FLOAT_MAT3x2, GL_FLOAT_MAT3x2 },
51         { Msp::GL::DOUBLE_MAT3x2, GL_DOUBLE_MAT3x2 },
52         { Msp::GL::FLOAT_MAT2x4, GL_FLOAT_MAT2x4 },
53         { Msp::GL::DOUBLE_MAT2x4, GL_DOUBLE_MAT2x4 },
54         { Msp::GL::FLOAT_MAT4x2, GL_FLOAT_MAT4x2 },
55         { Msp::GL::DOUBLE_MAT4x2, GL_DOUBLE_MAT4x2 },
56         { Msp::GL::FLOAT_MAT3x4, GL_FLOAT_MAT3x4 },
57         { Msp::GL::DOUBLE_MAT3x4, GL_DOUBLE_MAT3x4 },
58         { Msp::GL::FLOAT_MAT4x3, GL_FLOAT_MAT4x3 },
59         { Msp::GL::DOUBLE_MAT4x3, GL_DOUBLE_MAT4x3 },
60         { Msp::GL::IMAGE_1D, GL_IMAGE_1D },
61         { Msp::GL::IMAGE_2D, GL_IMAGE_2D },
62         { Msp::GL::IMAGE_3D, GL_IMAGE_3D },
63         { Msp::GL::IMAGE_CUBE, GL_IMAGE_CUBE },
64         { Msp::GL::IMAGE_1D_ARRAY, GL_IMAGE_1D_ARRAY },
65         { Msp::GL::IMAGE_2D_ARRAY, GL_IMAGE_2D_ARRAY },
66         { Msp::GL::IMAGE_CUBE_ARRAY, GL_IMAGE_CUBE_MAP_ARRAY },
67         { Msp::GL::SAMPLER_1D, GL_SAMPLER_1D },
68         { Msp::GL::SAMPLER_2D, GL_SAMPLER_2D },
69         { Msp::GL::SAMPLER_3D, GL_SAMPLER_3D },
70         { Msp::GL::SAMPLER_CUBE, GL_SAMPLER_CUBE },
71         { Msp::GL::SAMPLER_1D_ARRAY, GL_SAMPLER_1D_ARRAY },
72         { Msp::GL::SAMPLER_2D_ARRAY, GL_SAMPLER_2D_ARRAY },
73         { Msp::GL::SAMPLER_CUBE_ARRAY, GL_SAMPLER_CUBE_MAP_ARRAY },
74         { Msp::GL::SAMPLER_1D_SHADOW, GL_SAMPLER_1D_SHADOW },
75         { Msp::GL::SAMPLER_2D_SHADOW, GL_SAMPLER_2D_SHADOW },
76         { Msp::GL::SAMPLER_CUBE_SHADOW, GL_SAMPLER_CUBE_SHADOW },
77         { Msp::GL::SAMPLER_1D_ARRAY_SHADOW, GL_SAMPLER_1D_ARRAY_SHADOW },
78         { Msp::GL::SAMPLER_2D_ARRAY_SHADOW, GL_SAMPLER_2D_ARRAY_SHADOW },
79         { Msp::GL::SAMPLER_CUBE_ARRAY_SHADOW, GL_SAMPLER_CUBE_MAP_ARRAY_SHADOW }
80 };
81 const unsigned type_map_size = sizeof(type_map)/sizeof(MappedType);
82
83 bool type_compare(const MappedType &mt, Msp::GL::DataType t)
84 { return mt.type<t; }
85
86 }
87
88 namespace Msp {
89 namespace GL {
90
91 unsigned get_gl_type(DataType type)
92 {
93         const MappedType *end = type_map+type_map_size;
94         const MappedType *ptr = lower_bound(type_map, end, type, type_compare);
95         if(ptr==end || ptr->type!=type)
96                 throw invalid_argument("get_gl_type");
97         return ptr->gl_type;
98 }
99
100 DataType from_gl_type(unsigned gl_type)
101 {
102         for(unsigned i=0; i<type_map_size; ++i)
103                 if(type_map[i].gl_type==gl_type)
104                         return type_map[i].type;
105         throw invalid_argument("from_gl_type");
106 }
107
108 void require_type(DataType type)
109 {
110         unsigned rows = ((type>>12)&3)+1;
111         unsigned cols = ((type>>14)&4)+1;
112         if(rows>1 && cols>1 && rows!=cols)
113                 static Require _req(NV_non_square_matrices);
114         if((type&0x200) && get_type_size(type)/(rows*cols)==8)
115                 static Require _req(ARB_gpu_shader_fp64);
116 }
117
118 } // namespace GL
119 } // namespace Msp