1 #include <msp/strings/format.h>
3 #include "vertexformat.h"
10 VertexFormat::VertexFormat():
14 VertexFormat::VertexFormat(VertexAttribute a):
20 VertexFormat VertexFormat::operator,(VertexAttribute a) const
22 if(count>=MAX_ATTRIBUTES)
23 throw invalid_operation("VertexFormat::operator,");
25 VertexFormat r = *this;
26 r.attributes[r.count++] = a;
31 VertexFormat VertexFormat::operator,(DataType t) const
34 throw invalid_operation("VertexFormat::operator,");
36 VertexFormat r = *this;
37 VertexAttribute &a = r.attributes[r.count-1];
38 a = make_typed_attribute(a, t);
43 VertexFormat VertexFormat::operator,(unsigned i) const
46 throw invalid_operation("VertexFormat::operator,");
48 VertexFormat r = *this;
49 VertexAttribute &a = r.attributes[r.count-1];
50 a = make_indexed_attribute(a, i);
55 bool VertexFormat::operator==(const VertexFormat &other) const
57 if(count!=other.count)
59 return equal(attributes, attributes+count, other.attributes);
62 unsigned VertexFormat::stride() const
65 for(VertexAttribute a: *this)
66 s += get_attribute_size(a);
70 int VertexFormat::offset(VertexAttribute attr) const
72 unsigned sem = get_attribute_semantic(attr);
74 for(VertexAttribute a: *this)
76 if(get_attribute_semantic(a)==sem)
78 if(get_attribute_source_type(a)==get_attribute_source_type(attr) &&
79 get_attribute_component_count(a)>=get_attribute_component_count(attr))
85 offs += get_attribute_size(a);
92 VertexAttribute make_typed_attribute(VertexAttribute attr, DataType type)
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");
99 return static_cast<VertexAttribute>((attr&0xFC0F) | (type&0x0F)<<4 | (type&0x300)>>1);
102 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
104 VertexAttribute base = attr;
105 if(get_attribute_semantic(attr)==get_attribute_semantic(TEXCOORD1))
108 throw out_of_range("make_indexed_attribute");
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");
115 if(get_attribute_semantic(base)+index>=63)
116 throw out_of_range("make_indexed_attribute");
118 return static_cast<VertexAttribute>(base+(index<<10));
122 bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
125 for(; (j!=end && *j!='_' && *name); ++name, ++j)
129 attr = make_typed_attribute(attr, type);
135 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
137 auto i = str.begin();
138 for(; *name; ++name, ++i)
142 VertexAttribute result = base_attr;
144 if(i!=str.end() && *i=='_')
148 result = static_cast<VertexAttribute>(result|8);
151 if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
153 result = static_cast<VertexAttribute>(result+(*i++-'0'-min_size));
157 if(*i!='_' || ++i==str.end())
163 for(; (i!=str.end() && isdigit(*i)); ++i)
164 index = index*10+(*i-'0');
165 result = make_indexed_attribute(result, index);
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))
183 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
185 const string &str = conv.get();
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))
201 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));