1 #include <msp/strings/format.h>
3 #include "vertexformat.h"
10 VertexFormat::VertexFormat(VertexAttribute a):
16 VertexFormat VertexFormat::operator,(VertexAttribute a) const
18 if(count>=MAX_ATTRIBUTES)
19 throw invalid_operation("VertexFormat::operator,");
21 VertexFormat r = *this;
22 r.attributes[r.count++] = a;
27 VertexFormat VertexFormat::operator,(DataType t) const
30 throw invalid_operation("VertexFormat::operator,");
32 VertexFormat r = *this;
33 VertexAttribute &a = r.attributes[r.count-1];
34 a = make_typed_attribute(a, t);
39 VertexFormat VertexFormat::operator,(unsigned i) const
42 throw invalid_operation("VertexFormat::operator,");
44 VertexFormat r = *this;
45 VertexAttribute &a = r.attributes[r.count-1];
46 a = make_indexed_attribute(a, i);
51 bool VertexFormat::operator==(const VertexFormat &other) const
53 if(count!=other.count)
55 return equal(attributes, attributes+count, other.attributes);
58 unsigned VertexFormat::stride() const
61 for(VertexAttribute a: *this)
62 s += get_attribute_size(a);
66 int VertexFormat::offset(VertexAttribute attr) const
68 unsigned sem = get_attribute_semantic(attr);
70 for(VertexAttribute a: *this)
72 if(get_attribute_semantic(a)==sem)
74 if(get_attribute_source_type(a)==get_attribute_source_type(attr) &&
75 get_attribute_component_count(a)>=get_attribute_component_count(attr))
81 offs += get_attribute_size(a);
88 VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
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");
95 return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
98 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
100 VertexAttribute base = attr;
101 if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
104 throw out_of_range("make_indexed_attribute");
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");
111 if(get_attribute_semantic(base)+index>=63)
112 throw out_of_range("make_indexed_attribute");
114 return static_cast<VertexAttribute>(base+(index<<10));
118 bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
121 for(; (j!=end && *j!='_' && *name); ++name, ++j)
125 attr = make_typed_attribute(attr, type);
131 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
133 auto i = str.begin();
134 for(; *name; ++name, ++i)
138 VertexAttribute result = base_attr;
140 if(i!=str.end() && *i=='_')
144 result = static_cast<VertexAttribute>(result|8);
147 if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
149 result = static_cast<VertexAttribute>(result+(*i++-'0'-min_size));
153 if(*i!='_' || ++i==str.end())
159 for(; (i!=str.end() && isdigit(*i)); ++i)
160 index = index*10+(*i-'0');
161 result = make_indexed_attribute(result, index);
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))
179 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
181 const string &str = conv.get();
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))
198 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));