]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexformat.cpp
Make VertexFormat capable of storing type information
[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
101         return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
102 }
103
104 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
105 {
106         unsigned base = attr;
107         if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
108         {
109                 if(index>=4)
110                         throw out_of_range("make_indexed_attribute");
111         }
112         else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1))
113                 base &= 0x3FF;
114         else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1))
115                 throw invalid_argument("make_indexed_attribute");
116
117         if(get_attribute_semantic(base)+index>=63)
118                 throw out_of_range("make_indexed_attribute");
119
120         return static_cast<VertexAttribute>(base+(index<<10));
121 }
122
123
124 bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
125 {
126         string::const_iterator j = i;
127         for(; (j!=end && *j!='_' && *name); ++name, ++j)
128                 if(*j!=*name)
129                         return false;
130
131         attr = make_typed_attribute(attr, type);
132         i = j;
133
134         return true;
135 }
136
137 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
138 {
139         string::const_iterator i = str.begin();
140         for(; *name; ++name, ++i)
141                 if(*i!=*name)
142                         return false;
143         if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
144                 return false;
145         VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i++-'0'-min_size));
146
147         while(i!=str.end())
148         {
149                 if(*i!='_' || ++i==str.end())
150                         return false;
151
152                 if(isdigit(*i))
153                 {
154                         unsigned index = 0;
155                         for(; (i!=str.end() && isdigit(*i)); ++i)
156                                 index = index*10+(*i-'0');
157                         result = make_indexed_attribute(result, index);
158                 }
159                 else if(convert_attribute_type(i, str.end(), "UBYTE", result, UNSIGNED_BYTE) ||
160                         convert_attribute_type(i, str.end(), "BYTE", result, BYTE) ||
161                         convert_attribute_type(i, str.end(), "USHORT", result, UNSIGNED_SHORT) ||
162                         convert_attribute_type(i, str.end(), "SHORT", result, SHORT) ||
163                         convert_attribute_type(i, str.end(), "UINT", result, UNSIGNED_INT) ||
164                         convert_attribute_type(i, str.end(), "INT", result, INT) ||
165                         convert_attribute_type(i, str.end(), "FLOAT", result, FLOAT))
166                         ;
167                 else
168                         return false;
169         }
170
171         attr = result;
172         return true;
173 }
174
175 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
176 {
177         const string &str = conv.get();
178         try
179         {
180                 if(convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) ||
181                         convert_attribute(str, "COLOR", 3, 4, a, COLOR3) ||
182                         convert_attribute(str, "NORMAL", 3, 3, a, NORMAL3) ||
183                         convert_attribute(str, "TANGENT", 3, 3, a, TANGENT3) ||
184                         convert_attribute(str, "GROUP", 1, 4, a, GROUP1) ||
185                         convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) ||
186                         convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) ||
187                         convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
188                         return;
189         }
190         catch(...)
191         { }
192
193         throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
194 }
195
196 } // namespace GL
197 } // namespace Msp