]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexformat.h
Use standard fixed-size integer types
[libs/gl.git] / source / core / vertexformat.h
1 #ifndef MSP_GL_VERTEXFORMAT_H_
2 #define MSP_GL_VERTEXFORMAT_H_
3
4 #include <cstdint>
5 #include <msp/strings/lexicalcast.h>
6 #include "datatype.h"
7
8 namespace Msp {
9 namespace GL {
10
11 /** A single vertex attribute.  Commonly used attributes are named by their
12 semantical meaning in the standard shaders.  Texture coordinates and generic
13 attributes can additionally be given an index.  There are four texture
14 coordinate attributes available.  The number of available generic attributes
15 depends on implementation limits, but is at least five.
16
17 RAW_ATTRIB is handled in a special way; creating an indexed attribute based on
18 it uses the index as raw attribute number.  Only use it if you know what you
19 are doing.
20
21 The values are bitfields laid as follows:
22
23 nnnn nn_f gsss iccc
24       │ │ │  │ │  └╴Number of components
25       │ │ │  │ └───╴Integer attribute flag
26       │ │ │  └─────╴Size of one component
27       │ │ └────────╴Signed flag
28       │ └──────────╴Floating-point flag
29       └────────────╴Attribute index (semantic)
30
31 This information is presented for internal documentation purposes only; it is
32 inadvisable for programs to rely on it.
33 */
34 enum VertexAttribute
35 {
36         VERTEX2 = 0x01C2,
37         VERTEX3 = 0x01C3,
38         VERTEX4 = 0x01C4,
39         COLOR3 = 0x05C3,
40         COLOR4 = 0x05C4,
41         NORMAL3 = 0x09C3,
42         TANGENT3 = 0x0DC3,
43         GROUP1 = 0x10C9,
44         GROUP2 = 0x10CA,
45         GROUP3 = 0x10CB,
46         GROUP4 = 0x10CC,
47         WEIGHT1 = 0x15C1,
48         WEIGHT2 = 0x15C2,
49         WEIGHT3 = 0x15C3,
50         WEIGHT4 = 0x15C4,
51         TEXCOORD1 = 0x19C1,
52         TEXCOORD2 = 0x19C2,
53         TEXCOORD3 = 0x19C3,
54         TEXCOORD4 = 0x19C4,
55         GENERIC1 = 0x29C1,
56         GENERIC2 = 0x29C2,
57         GENERIC3 = 0x29C3,
58         GENERIC4 = 0x29C4,
59         GENERIC_I1 = 0x28C9,
60         GENERIC_I2 = 0x28CA,
61         GENERIC_I3 = 0x28CB,
62         GENERIC_I4 = 0x28CC,
63         RAW_ATTRIB1 = 0xFDC1,
64         RAW_ATTRIB2 = 0xFDC2,
65         RAW_ATTRIB3 = 0xFDC3,
66         RAW_ATTRIB4 = 0xFDC4,
67         RAW_ATTRIB_I1 = 0xFCC9,
68         RAW_ATTRIB_I2 = 0xFCCA,
69         RAW_ATTRIB_I3 = 0xFCCB,
70         RAW_ATTRIB_I4 = 0xFCCC
71 };
72
73 class VertexFormat
74 {
75 private:
76         enum { MAX_ATTRIBUTES = 15 };
77
78         std::uint8_t count;
79         std::uint16_t attributes[MAX_ATTRIBUTES];
80
81 public:
82         VertexFormat();
83         VertexFormat(VertexAttribute);
84
85         VertexFormat operator,(VertexAttribute) const;
86         VertexFormat operator,(DataType) const;
87         VertexFormat operator,(unsigned) const;
88         bool operator==(const VertexFormat &) const;
89         bool operator!=(const VertexFormat &other) const { return !(*this==other); }
90
91         bool empty() const { return !count; }
92         const std::uint16_t *begin() const { return attributes; }
93         const std::uint16_t *end() const { return attributes+count; }
94         unsigned stride() const;
95         int offset(VertexAttribute) const;
96 };
97
98 inline VertexFormat operator,(VertexAttribute a1, VertexAttribute a2)
99 { return (VertexFormat(a1), a2); }
100
101 VertexAttribute make_typed_attribute(VertexAttribute, DataType);
102
103 inline VertexAttribute operator,(VertexAttribute a, DataType t)
104 { return make_typed_attribute(a, t); }
105
106 VertexAttribute make_indexed_attribute(VertexAttribute, unsigned);
107
108 inline VertexAttribute operator,(VertexAttribute a, unsigned i)
109 { return make_indexed_attribute(a, i); }
110
111 inline unsigned get_attribute_semantic(std::uint16_t a)
112 { return a>>10; }
113
114 inline DataType get_attribute_source_type(std::uint16_t a)
115 { return static_cast<DataType>((a&0x70)>>4 | (a&0x180)<<1); }
116
117 inline unsigned get_attribute_component_count(std::uint16_t a)
118 { return a&7; }
119
120 inline unsigned get_attribute_size(std::uint16_t a)
121 { return get_attribute_component_count(a)*get_type_size(get_attribute_source_type(a)); }
122
123 inline bool is_integer_attribute(std::uint16_t a)
124 { return a&8; }
125
126 void operator>>(const LexicalConverter &, VertexAttribute &);
127
128 } // namespace GL
129 } // namespace Msp
130
131 #endif