Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLVertexFormat.h

raw
1 #pragma once
2
3 #ifndef DOXYGEN_GENERATING_OUTPUT
4 #define NCINE_INCLUDE_OPENGL
5 #include "../../../CommonHeaders.h"
6 #endif
7
8 #include <Containers/SmallVector.h>
9
10 using namespace Death::Containers;
11
12 namespace nCine::RHI::GL
13 {
14 class GLBufferObject;
15
16 /**
17 @brief Describes the format of a vertex
18
19 Collects all the state that specifies the format of a vertex: a set of @ref Attribute entries
20 (each describing a component count, type, normalization, stride, offset and source VBO) plus an
21 optional index buffer. @ref Define() applies the format to the currently bound program and VAO by
22 issuing the vertex attribute pointer calls, while @ref Reset() disables all attributes.
23 */
24 class GLVertexFormat
25 {
26 public:
27 /** @brief The minimum guaranteed value for `GL_MAX_VERTEX_ATTRIBS` */
28 static constexpr std::uint32_t MaxAttributes = 16;
29
30 /**
31 * @brief A single vertex attribute within a vertex format
32 *
33 * Describes one vertex attribute: its shader location index, component count, GL type,
34 * normalization, stride, offset and the source vertex buffer object.
35 */
36 class Attribute
37 {
38 friend class GLVertexFormat;
39
40 public:
41 Attribute();
42
43 /** @brief Initializes the attribute as enabled with the given index, component count and type */
44 void Init(std::uint32_t index, GLint size, GLenum type);
45 bool operator==(const Attribute& other) const;
46 bool operator!=(const Attribute& other) const;
47
48 /** @brief Returns `true` if the attribute is enabled */
49 inline bool IsEnabled() const {
50 return _enabled;
51 }
52 /** @brief Returns the source vertex buffer object */
53 inline const GLBufferObject* GetVbo() const {
54 return _vbo;
55 }
56 /** @brief Returns the shader location index of the attribute */
57 inline std::uint32_t GetIndex() const {
58 return _index;
59 }
60 /** @brief Returns the number of components per attribute */
61 inline GLint GetSize() const {
62 return _size;
63 }
64 /** @brief Returns the data type of each component (e.g., `GL_FLOAT`) */
65 inline GLenum GetType() const {
66 return _type;
67 }
68 /** @brief Returns `true` if integer values are normalized to a floating-point range */
69 inline bool IsNormalized() const {
70 return _normalized == GL_TRUE;
71 }
72 /** @brief Returns the byte offset between consecutive attributes */
73 inline GLsizei GetStride() const {
74 return _stride;
75 }
76 /** @brief Returns the byte offset of the first component within the buffer */
77 inline const GLvoid* GetPointer() const {
78 return _pointer;
79 }
80 /** @brief Returns the additional base vertex byte offset */
81 inline std::uint32_t GetBaseOffset() const {
82 return _baseOffset;
83 }
84
85 /** @brief Sets the stride and the byte offset of the attribute within the buffer */
86 void SetVboParameters(GLsizei stride, const GLvoid* pointer);
87 /** @brief Sets the source vertex buffer object */
88 inline void setVbo(const GLBufferObject* vbo) {
89 _vbo = vbo;
90 }
91 /** @brief Sets the additional base vertex byte offset */
92 inline void SetBaseOffset(std::uint32_t baseOffset) {
93 _baseOffset = baseOffset;
94 }
95
96 /** @brief Sets the number of components per attribute */
97 inline void SetSize(GLint size) {
98 _size = size;
99 }
100 /** @brief Sets the data type of each component */
101 inline void SetType(GLenum type) {
102 _type = type;
103 }
104 /** @brief Sets whether integer values are normalized to a floating-point range */
105 inline void SetNormalized(bool normalized) {
106 _normalized = normalized;
107 }
108
109 private:
110 const GLBufferObject* _vbo;
111 const GLvoid* _pointer;
112 std::uint32_t _index;
113 GLint _size;
114 GLenum _type;
115 GLsizei _stride;
116 /** @brief Used to simulate the missing `glDrawElementsBaseVertex()` on OpenGL ES 3.0 */
117 std::uint32_t _baseOffset;
118 bool _enabled;
119 GLboolean _normalized;
120 };
121
122 GLVertexFormat();
123 GLVertexFormat(const GLVertexFormat& other) = default;
124 GLVertexFormat& operator=(const GLVertexFormat& other) = default;
125
126 /** @brief Returns the number of attributes in the format */
127 inline std::uint32_t GetAttributeCount() const {
128 return std::uint32_t(_attributes.size());
129 }
130
131 /** @brief Returns the index buffer object, or `nullptr` if none */
132 inline const GLBufferObject* GetIbo() const {
133 return _ibo;
134 }
135 /** @brief Sets the index buffer object */
136 inline void SetIbo(const GLBufferObject* ibo) {
137 _ibo = ibo;
138 }
139 /** @brief Applies the vertex format to the currently bound program and VAO */
140 void Define();
141 /** @brief Disables all attributes and clears the index buffer */
142 void Reset();
143
144 /**
145 * @brief Calculates a hash of the format for fast inequality checks
146 *
147 * Formats that compare equal have equal fingerprints, so a fingerprint mismatch proves
148 * inequality, while a match still has to be confirmed with `operator==()`.
149 */
150 std::uint64_t CalculateFingerprint() const;
151
152 /** @brief Returns the attribute at the given index */
153 inline Attribute& operator[](std::uint32_t index) {
154 return _attributes[index];
155 }
156 /** @brief Returns the attribute at the given index */
157 inline const Attribute& operator[](std::uint32_t index) const {
158 return _attributes[index];
159 }
160
161 bool operator==(const GLVertexFormat& other) const;
162 bool operator!=(const GLVertexFormat& other) const;
163
164 private:
165 SmallVector<Attribute, MaxAttributes> _attributes;
166 const GLBufferObject* _ibo;
167 };
168 }