2 #include <msp/io/print.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/lexicalcast.h>
5 #include <msp/strings/utils.h>
7 #include "vertexformat.h"
14 VertexFormat::VertexFormat():
18 VertexFormat::VertexFormat(VertexAttribute a):
24 VertexFormat VertexFormat::operator,(VertexAttribute a) const
26 if(count>=MAX_ATTRIBUTES)
27 throw invalid_operation("VertexFormat::operator,");
29 VertexFormat r = *this;
30 r.attributes[r.count++] = a;
35 VertexFormat VertexFormat::operator,(DataType t) const
38 throw invalid_operation("VertexFormat::operator,");
40 VertexFormat r = *this;
41 VertexAttribute &a = r.attributes[r.count-1];
42 a = make_typed_attribute(a, t);
47 VertexFormat VertexFormat::operator,(unsigned i) const
50 throw invalid_operation("VertexFormat::operator,");
52 VertexFormat r = *this;
53 VertexAttribute &a = r.attributes[r.count-1];
54 a = make_indexed_attribute(a, i);
59 bool VertexFormat::operator==(const VertexFormat &other) const
61 if(count!=other.count)
63 return equal(attributes, attributes+count, other.attributes);
66 unsigned VertexFormat::stride() const
69 for(VertexAttribute a: *this)
70 s += get_attribute_size(a);
74 int VertexFormat::offset(VertexAttribute attr) const
76 unsigned sem = get_attribute_semantic(attr);
78 for(VertexAttribute a: *this)
80 if(get_attribute_semantic(a)==sem)
82 if(get_attribute_source_type(a)==get_attribute_source_type(attr) &&
83 get_attribute_component_count(a)>=get_attribute_component_count(attr))
89 offs += get_attribute_size(a);
96 VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
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");
103 return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
106 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
108 VertexAttribute base = attr;
109 if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
112 throw out_of_range("make_indexed_attribute");
114 else if(get_attribute_semantic(attr)==get_attribute_semantic(RAW_ATTRIB1))
115 base = static_cast<VertexAttribute>(base&0x3FF);
116 else if(get_attribute_semantic(attr)!=get_attribute_semantic(GENERIC1))
117 throw invalid_argument("make_indexed_attribute");
119 if(get_attribute_semantic(base)+index>=63)
120 throw out_of_range("make_indexed_attribute");
122 return static_cast<VertexAttribute>(base+(index<<10));
126 bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
129 for(; (j!=end && *j!='_' && *name); ++name, ++j)
133 attr = make_typed_attribute(attr, type);
139 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
141 auto i = str.begin();
142 for(; *name; ++name, ++i)
146 VertexAttribute result = base_attr;
148 if(i!=str.end() && *i=='_')
152 result = static_cast<VertexAttribute>(result|8);
155 if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
157 result = static_cast<VertexAttribute>(result+(*i++-'0'-min_size));
161 if(*i!='_' || ++i==str.end())
167 for(; (i!=str.end() && isdigit(*i)); ++i)
168 index = index*10+(*i-'0');
169 result = make_indexed_attribute(result, index);
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))
187 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
189 const string &str = conv.get();
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))
205 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));