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