1 #ifndef MSP_GL_VERTEXFORMAT_H_
2 #define MSP_GL_VERTEXFORMAT_H_
5 #include <msp/strings/lexicalcast.h>
12 A single vertex attribute. Commonly used attributes are named by their
13 semantical meaning in the standard shaders. Texture coordinates and generic
14 attributes can additionally be given an index. There are four texture
15 coordinate attributes available. The number of available generic attributes
16 depends on implementation limits, but is at least five.
18 RAW_ATTRIB is handled in a special way; creating an indexed attribute based on
19 it uses the index as raw attribute number. Only use it if you know what you
22 The values are bitfields laid as follows:
25 │ │ │ │ │ └╴Number of components
26 │ │ │ │ └───╴Integer attribute flag
27 │ │ │ └─────╴Size of one component
28 │ │ └────────╴Signed flag
29 │ └──────────╴Floating-point flag
30 └────────────╴Attribute index (semantic)
32 This information is presented for internal documentation purposes only; it is
33 inadvisable for applications to rely on it.
35 enum VertexAttribute: std::uint16_t
72 RAW_ATTRIB_I1 = 0xFCC9,
73 RAW_ATTRIB_I2 = 0xFCCA,
74 RAW_ATTRIB_I3 = 0xFCCB,
75 RAW_ATTRIB_I4 = 0xFCCC
79 Describes the attributes of a vertex. Up to 15 attributes are allowed.
84 static constexpr unsigned MAX_ATTRIBUTES = 15;
87 std::uint8_t count = 0;
88 VertexAttribute attributes[MAX_ATTRIBUTES];
91 VertexFormat() = default;
92 VertexFormat(VertexAttribute);
94 VertexFormat operator,(VertexAttribute) const;
95 VertexFormat operator,(DataType) const;
96 VertexFormat operator,(unsigned) const;
97 bool operator==(const VertexFormat &) const;
98 bool operator!=(const VertexFormat &other) const { return !(*this==other); }
100 bool empty() const { return !count; }
101 const VertexAttribute *begin() const { return attributes; }
102 const VertexAttribute *end() const { return attributes+count; }
104 /** Returns the displacement from one vertex to the next. */
105 unsigned stride() const;
107 /** Returns the offset of an attribute within a vertex. A stored attribute
108 must have the same semantic and type and at least as many components as
109 requested to be considered a match. */
110 int offset(VertexAttribute) const;
113 inline VertexFormat operator,(VertexAttribute a1, VertexAttribute a2)
114 { return (VertexFormat(a1), a2); }
116 VertexAttribute make_typed_attribute(VertexAttribute, DataType);
118 inline VertexAttribute operator,(VertexAttribute a, DataType t)
119 { return make_typed_attribute(a, t); }
121 VertexAttribute make_indexed_attribute(VertexAttribute, unsigned);
123 inline VertexAttribute operator,(VertexAttribute a, unsigned i)
124 { return make_indexed_attribute(a, i); }
126 inline unsigned get_attribute_semantic(VertexAttribute a)
129 inline DataType get_attribute_source_type(VertexAttribute a)
130 { return static_cast<DataType>((a&0x70)>>4 | (a&0x180)<<1); }
132 inline unsigned get_attribute_component_count(VertexAttribute a)
135 inline unsigned get_attribute_size(VertexAttribute a)
136 { return get_attribute_component_count(a)*get_type_size(get_attribute_source_type(a)); }
138 inline bool is_integer_attribute(VertexAttribute a)
141 inline bool is_padding(VertexAttribute a)
142 { return get_attribute_semantic(a)==get_attribute_semantic(PADDING1); }
144 void operator>>(const LexicalConverter &, VertexAttribute &);