]> git.tdb.fi Git - libs/gl.git/blob - source/core/datatype.h
Eliminate the polymorphic Uniform class hierarchy
[libs/gl.git] / source / core / datatype.h
1 #ifndef MSP_GL_DATATYPE_H_
2 #define MSP_GL_DATATYPE_H_
3
4 #include <msp/linal/matrix.h>
5 #include <msp/linal/vector.h>
6 #include "gl.h"
7
8 namespace Msp {
9 namespace GL {
10
11 /**
12 Identifies a data type.  The values are bitfields laid as follows:
13
14 __ds addd ccrr _bfg ssss ssss
15   ││ │  │  │ │  │││         └╴Size (bytes)
16   ││ │  │  │ │  ││└──────────╴Signed flag
17   ││ │  │  │ │  │└───────────╴Floating-point flag
18   ││ │  │  │ │  └────────────╴Boolean flag
19   ││ │  │  │ └───────────────╴Vector or matrix rows minus one
20   ││ │  │  └─────────────────╴Matrix columns minus one
21   ││ │  └────────────────────╴Image dimensions (4 = cube)
22   ││ └───────────────────────╴Image array flag
23   │└─────────────────────────╴Sampled image flag
24   └──────────────────────────╴Shadow sampler flag
25
26 This information is presented for internal documentation purposes only; it is
27 inadvisable for programs to rely on it.
28 */
29 enum DataType
30 {
31         VOID = 0,
32         BOOL = 0x401,
33         BYTE = 0x101,
34         UNSIGNED_BYTE = 0x001,
35         SHORT = 0x102,
36         UNSIGNED_SHORT = 0x002,
37         INT = 0x104,
38         UNSIGNED_INT = 0x004,
39         FLOAT = 0x304,
40         HALF_FLOAT = 0x302,
41         DOUBLE = 0x308,
42
43         FLOAT_VEC2 = 0x1308,
44         FLOAT_VEC3 = 0x230C,
45         FLOAT_VEC4 = 0x3310,
46         INT_VEC2 = 0x1108,
47         INT_VEC3 = 0x210C,
48         INT_VEC4 = 0x3110,
49         BOOL_VEC2 = 0x1402,
50         BOOL_VEC3 = 0x2403,
51         BOOL_VEC4 = 0x3404,
52
53         FLOAT_MAT2 = 0x5310,
54         FLOAT_MAT3 = 0xA324,
55         FLOAT_MAT4 = 0xF340,
56         FLOAT_MAT2x3 = 0x6318,
57         FLOAT_MAT3x2 = 0x9318,
58         FLOAT_MAT2x4 = 0x7320,
59         FLOAT_MAT4x2 = 0xD320,
60         FLOAT_MAT3x4 = 0xB330,
61         FLOAT_MAT4x3 = 0xE330,
62
63         DOUBLE_MAT2 = 0x5320,
64         DOUBLE_MAT3 = 0xA348,
65         DOUBLE_MAT4 = 0xF380,
66         DOUBLE_MAT2x3 = 0x6330,
67         DOUBLE_MAT3x2 = 0x7330,
68         DOUBLE_MAT2x4 = 0x7340,
69         DOUBLE_MAT4x2 = 0xD340,
70         DOUBLE_MAT3x4 = 0xB360,
71         DOUBLE_MAT4x3 = 0xE360,
72
73         IMAGE_1D = 0x10304,
74         IMAGE_2D = 0x20304,
75         IMAGE_3D = 0x30304,
76         IMAGE_CUBE = 0x40304,
77         IMAGE_1D_ARRAY = 0x90304,
78         IMAGE_2D_ARRAY = 0xA0304,
79         IMAGE_CUBE_ARRAY = 0xC0304,
80         SAMPLER_1D = 0x110304,
81         SAMPLER_2D = 0x120304,
82         SAMPLER_3D = 0x130304,
83         SAMPLER_CUBE = 0x140304,
84         SAMPLER_1D_ARRAY = 0x190304,
85         SAMPLER_2D_ARRAY = 0x1A0304,
86         SAMPLER_CUBE_ARRAY = 0x1C0304,
87         SAMPLER_1D_SHADOW = 0x310304,
88         SAMPLER_2D_SHADOW = 0x320304,
89         SAMPLER_CUBE_SHADOW = 0x340304,
90         SAMPLER_1D_ARRAY_SHADOW = 0x390304,
91         SAMPLER_2D_ARRAY_SHADOW = 0x3A0304,
92         SAMPLER_CUBE_ARRAY_SHADOW = 0x3C0304
93 };
94
95 inline unsigned get_type_size(DataType t) { return t&0xFF; }
96 inline bool is_float(DataType t) { return t&0x200; }
97 inline bool is_matrix(DataType t) { return t&0xC000; }
98 inline bool is_vector(DataType t) { return !is_matrix(t) && (t&0x3000); }
99 inline bool is_image(DataType t) { return t&0x70000; }
100
101 inline DataType get_matrix_column_type(DataType t)
102 {
103         unsigned cols = ((t&0xC000)>>14)+1;
104         return static_cast<DataType>((t&~0xC0FF) | (get_type_size(t)/cols));
105 }
106
107 inline DataType get_element_type(DataType t)
108 {
109         unsigned elems = (((t&0xC000)>>14)+1)*(((t&0x3000)>>12)+1);
110         return static_cast<DataType>((t&~0xC0FF) | (get_type_size(t)/elems));
111 }
112
113 template<typename T> struct TypeTraits;
114 template<> struct TypeTraits<bool> { static const DataType type = BOOL; };
115 template<> struct TypeTraits<char> { static const DataType type = BYTE; };
116 template<> struct TypeTraits<unsigned char> { static const DataType type = UNSIGNED_BYTE; };
117 template<> struct TypeTraits<short> { static const DataType type = SHORT; };
118 template<> struct TypeTraits<unsigned short> { static const DataType type = UNSIGNED_SHORT; };
119 template<> struct TypeTraits<int> { static const DataType type = INT; };
120 template<> struct TypeTraits<unsigned> { static const DataType type = UNSIGNED_INT; };
121 template<> struct TypeTraits<float> { static const DataType type = FLOAT; };
122 template<> struct TypeTraits<double> { static const DataType type = DOUBLE; };
123
124 template<typename T, unsigned N>
125 struct TypeTraits<LinAl::Vector<T, N>>
126 {
127         static const DataType type = static_cast<DataType>((TypeTraits<T>::type&0xF00) | ((TypeTraits<T>::type&0xFF)*N) | ((N-1)<<12));
128 };
129
130 template<typename T, unsigned N, unsigned M>
131 struct TypeTraits<LinAl::Matrix<T, N, M>>
132 {
133         static const DataType type = static_cast<DataType>((TypeTraits<T>::type&0xF00) | ((TypeTraits<T>::type&0xFF)*N*M) | ((N-1)<<12) | ((M-1)<<14));
134 };
135
136 GLenum get_gl_type(DataType);
137 DataType from_gl_type(GLenum);
138
139 void require_type(DataType);
140
141 } // namespace GL
142 } // namespace Msp
143
144 #endif