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