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