]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexformat.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / vertexformat.cpp
1 #include <msp/strings/format.h>
2 #include "error.h"
3 #include "vertexformat.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 VertexFormat::VertexFormat(VertexAttribute a):
11         count(1)
12 {
13         attributes[0] = a;
14 }
15
16 VertexFormat VertexFormat::operator,(VertexAttribute a) const
17 {
18         if(count>=MAX_ATTRIBUTES)
19                 throw invalid_operation("VertexFormat::operator,");
20
21         VertexFormat r = *this;
22         r.attributes[r.count++] = a;
23
24         return r;
25 }
26
27 VertexFormat VertexFormat::operator,(DataType t) const
28 {
29         if(!count)
30                 throw invalid_operation("VertexFormat::operator,");
31
32         VertexFormat r = *this;
33         VertexAttribute &a = r.attributes[r.count-1];
34         a = make_typed_attribute(a, t);
35
36         return r;
37 }
38
39 VertexFormat VertexFormat::operator,(unsigned i) const
40 {
41         if(!count)
42                 throw invalid_operation("VertexFormat::operator,");
43
44         VertexFormat r = *this;
45         VertexAttribute &a = r.attributes[r.count-1];
46         a = make_indexed_attribute(a, i);
47
48         return r;
49 }
50
51 bool VertexFormat::operator==(const VertexFormat &other) const
52 {
53         if(count!=other.count)
54                 return false;
55         return equal(attributes, attributes+count, other.attributes);
56 }
57
58 unsigned VertexFormat::stride() const
59 {
60         unsigned s = 0;
61         for(VertexAttribute a: *this)
62                 s += get_attribute_size(a);
63         return s;
64 }
65
66 int VertexFormat::offset(VertexAttribute attr) const
67 {
68         unsigned sem = get_attribute_semantic(attr);
69         unsigned offs = 0;
70         for(VertexAttribute a: *this)
71         {
72                 if(get_attribute_semantic(a)==sem)
73                 {
74                         if(get_attribute_source_type(a)==get_attribute_source_type(attr) &&
75                                 get_attribute_component_count(a)>=get_attribute_component_count(attr))
76                                 return offs;
77                         else
78                                 return -1;
79                 }
80                 else
81                         offs += get_attribute_size(a);
82         }
83
84         return -1;
85 }
86
87
88 VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
89 {
90         if(is_matrix(type) || is_vector(type) || is_image(type))
91                 throw invalid_argument("make_typed_attribute");
92         if(is_integer_attribute(attr) && is_float(type))
93                 throw invalid_argument("make_typed_attribute");
94
95         return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
96 }
97
98 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
99 {
100         VertexAttribute base = attr;
101         if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
102         {
103                 if(index>=4)
104                         throw out_of_range("make_indexed_attribute");
105         }
106         else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1))
107                 base = static_cast<VertexAttribute>(base&0x3FF);
108         else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1))
109                 throw invalid_argument("make_indexed_attribute");
110
111         if(get_attribute_semantic(base)+index>=63)
112                 throw out_of_range("make_indexed_attribute");
113
114         return static_cast<VertexAttribute>(base+(index<<10));
115 }
116
117
118 bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
119 {
120         auto j = i;
121         for(; (j!=end && *j!='_' && *name); ++name, ++j)
122                 if(*j!=*name)
123                         return false;
124
125         attr = make_typed_attribute(attr, type);
126         i = j;
127
128         return true;
129 }
130
131 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
132 {
133         auto i = str.begin();
134         for(; *name; ++name, ++i)
135                 if(*i!=*name)
136                         return false;
137
138         VertexAttribute result = base_attr;
139
140         if(i!=str.end() && *i=='_')
141         {
142                 if(*(++i)++!='I')
143                         return false;
144                 result = static_cast<VertexAttribute>(result|8);
145         }
146
147         if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
148                 return false;
149         result = static_cast<VertexAttribute>(result+(*i++-'0'-min_size));
150
151         while(i!=str.end())
152         {
153                 if(*i!='_' || ++i==str.end())
154                         return false;
155
156                 if(isdigit(*i))
157                 {
158                         unsigned index = 0;
159                         for(; (i!=str.end() && isdigit(*i)); ++i)
160                                 index = index*10+(*i-'0');
161                         result = make_indexed_attribute(result, index);
162                 }
163                 else if(convert_attribute_type(i, str.end(), "UBYTE", result, UNSIGNED_BYTE) ||
164                         convert_attribute_type(i, str.end(), "BYTE", result, BYTE) ||
165                         convert_attribute_type(i, str.end(), "USHORT", result, UNSIGNED_SHORT) ||
166                         convert_attribute_type(i, str.end(), "SHORT", result, SHORT) ||
167                         convert_attribute_type(i, str.end(), "UINT", result, UNSIGNED_INT) ||
168                         convert_attribute_type(i, str.end(), "INT", result, INT) ||
169                         convert_attribute_type(i, str.end(), "FLOAT", result, FLOAT))
170                         ;
171                 else
172                         return false;
173         }
174
175         attr = result;
176         return true;
177 }
178
179 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
180 {
181         const string &str = conv.get();
182         try
183         {
184                 if(convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) ||
185                         convert_attribute(str, "COLOR", 3, 4, a, COLOR3) ||
186                         convert_attribute(str, "NORMAL", 3, 3, a, NORMAL3) ||
187                         convert_attribute(str, "TANGENT", 3, 3, a, TANGENT3) ||
188                         convert_attribute(str, "GROUP", 1, 4, a, GROUP1) ||
189                         convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) ||
190                         convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) ||
191                         convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1) ||
192                         convert_attribute(str, "PADDING", 1, 4, a, PADDING1))
193                         return;
194         }
195         catch(...)
196         { }
197
198         throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
199 }
200
201 } // namespace GL
202 } // namespace Msp