Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
VertexArray.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_VERTEXARRAY
19#define INCLUDED_VERTEXARRAY
20
21#include "lib/debug.h"
27
28#include <cstddef>
29#include <cstdint>
30#include <vector>
31
32namespace Renderer::Backend { class IDeviceCommandContext; }
33
34// Iterator
35template<typename T>
37{
38public:
39 typedef T Type;
40
41public:
43 m_Data(0), m_Stride(0)
44 {
45 }
46
47 VertexArrayIterator(char* data, size_t stride) :
48 m_Data(data), m_Stride(stride)
49 {
50 }
51
54 {
55 }
56
58 {
59 m_Data = rhs.m_Data;
60 m_Stride = rhs.m_Stride;
61 return *this;
62 }
63
64 // Accessors
65 T& operator*() const { return *(T*)m_Data; }
66 T* operator->() const { return (T*)m_Data; }
67 T& operator[](size_t idx) const { return *(T*)(m_Data + idx*m_Stride); }
68
69 // Walking
71 {
73 return *this;
74 }
76 {
77 VertexArrayIterator tmp = *this;
79 return tmp;
80 }
82 {
84 return *this;
85 }
87 {
88 VertexArrayIterator tmp = *this;
90 return tmp;
91 }
92
94 {
95 m_Data += rhs*m_Stride;
96 return *this;
97 }
99 {
100 m_Data -= rhs*m_Stride;
101 return *this;
102 }
103
105 {
106 VertexArrayIterator tmp = *this;
107 tmp.m_Data += rhs*m_Stride;
108 return tmp;
109 }
111 {
112 VertexArrayIterator tmp = *this;
113 tmp.m_Data -= rhs*m_Stride;
114 return tmp;
115 }
116
117 // Accessors for raw buffer data, for performance-critical code
118 char* GetData() const
119 {
120 return m_Data;
121 }
122 size_t GetStride() const
123 {
124 return m_Stride;
125 }
126
127private:
128 char* m_Data;
129 size_t m_Stride;
130};
131
132
133// Manage a vertex array with a runtime-determined set of attributes.
134//
135// Purpose: Different rendering paths sometimes require different sets of
136// attributes (e.g. normal vector vs. color data), which is difficult to
137// support with hardcoded vertex structures.
138// This class chooses the vertex layout at runtime, based on the attributes
139// that are actually needed.
140//
141// Note that this class will not allocate any backend resources until one
142// of the Upload functions is called.
144{
145public:
147 {
149
150 // Offset (in bytes) into a vertex structure (filled in by Layout())
151 uint32_t offset = 0;
152
154
156
157 // Get an iterator over the backing store for the given attribute that
158 // initially points at the first vertex.
159 // Supported types T: CVector3D, CVector4D, float[2], SColor4ub,
160 // std::uint16_t, std::uint16_t[2], std::uint8_t[4], short, short[2].
161 // This function verifies at runtime that the requested type T matches
162 // the attribute definition passed to AddAttribute().
163 template<typename T>
165 };
166
167public:
169 const Renderer::Backend::IBuffer::Type type, const uint32_t usage);
170 ~VertexArray();
171
172 // Set the number of vertices stored in the array
173 void SetNumberOfVertices(const size_t numberOfVertices);
174 // Add vertex attributes
175 void AddAttribute(Attribute* attr);
176
177 // Sets the minimum alignment for each attribute to have offsets multiples
178 // of `minimumAttributeAlignment`.
179 void SetMinimumAttributeAlignment(const uint32_t minimumAttributeAlignment);
180
181 size_t GetNumberOfVertices() const { return m_NumberOfVertices; }
182 uint32_t GetStride() const { return m_Stride; }
183
184 // Layout the vertex array format and create backing buffer in RAM.
185 // You must call Layout() after changing the number of vertices or
186 // attributes.
187 // All vertex data is lost when a vertex array is re-layouted.
188 void Layout();
189 // (Re-)Upload the attributes of the vertex array from the backing store to
190 // the underlying buffer.
191 void Upload();
192 // Make this vertex array's data available for the next series of calls to Bind
193 void PrepareForRendering();
194
196
197 // If you know for certain that you'll never have to change the data again,
198 // call this to free some memory.
199 void FreeBackingStore();
200
202
203 uint32_t GetOffset() const { return m_VB ? m_VB->m_Index : 0; }
204
205private:
206 void Free();
207
208 template<typename T>
210 {
213 }
214
216 uint32_t m_Usage{0};
219 std::vector<Attribute*> m_Attributes;
220
222 uint32_t m_Stride{0};
223 char* m_BackingStore{nullptr}; // 16-byte aligned, to allow fast SSE access
224};
225
226/**
227 * A VertexArray that is specialised to handle 16-bit array indices.
228 * Call UploadIfNeeded() before use in Draw/DrawIndexed.
229 */
231{
232public:
233 VertexIndexArray(const uint32_t usage);
234
235 /// Gets the iterator over the (only) attribute in this array, i.e. a std::uint16_t.
237
238private:
240};
241
242#endif // INCLUDED_VERTEXARRAY
Definition: VertexBufferManager.h:53
Renderer::Backend::IBuffer * GetBuffer()
Definition: VertexBuffer.h:139
Definition: IBuffer.h:32
Type
Definition: IBuffer.h:35
Definition: IDeviceCommandContext.h:46
Definition: VertexArray.h:37
T * operator->() const
Definition: VertexArray.h:66
char * m_Data
Definition: VertexArray.h:128
VertexArrayIterator(const VertexArrayIterator &rhs)
Definition: VertexArray.h:52
VertexArrayIterator operator-(ssize_t rhs) const
Definition: VertexArray.h:110
T & operator[](size_t idx) const
Definition: VertexArray.h:67
VertexArrayIterator operator++(int)
Definition: VertexArray.h:75
VertexArrayIterator & operator=(const VertexArrayIterator &rhs)
Definition: VertexArray.h:57
VertexArrayIterator(char *data, size_t stride)
Definition: VertexArray.h:47
VertexArrayIterator & operator--()
Definition: VertexArray.h:81
size_t GetStride() const
Definition: VertexArray.h:122
VertexArrayIterator operator+(ssize_t rhs) const
Definition: VertexArray.h:104
VertexArrayIterator()
Definition: VertexArray.h:42
char * GetData() const
Definition: VertexArray.h:118
size_t m_Stride
Definition: VertexArray.h:129
VertexArrayIterator operator--(int)
Definition: VertexArray.h:86
VertexArrayIterator & operator+=(ssize_t rhs)
Definition: VertexArray.h:93
VertexArrayIterator & operator-=(ssize_t rhs)
Definition: VertexArray.h:98
VertexArrayIterator & operator++()
Definition: VertexArray.h:70
T Type
Definition: VertexArray.h:39
T & operator*() const
Definition: VertexArray.h:65
Definition: VertexArray.h:144
uint32_t GetOffset() const
Definition: VertexArray.h:203
char * m_BackingStore
Definition: VertexArray.h:223
VertexArray(const Renderer::Backend::IBuffer::Type type, const uint32_t usage)
Definition: VertexArray.cpp:76
uint32_t m_MinimumAttributeAlignment
Definition: VertexArray.h:217
CVertexBufferManager::Handle m_VB
Definition: VertexArray.h:221
void SetNumberOfVertices(const size_t numberOfVertices)
Definition: VertexArray.cpp:97
Renderer::Backend::IBuffer * GetBuffer()
Definition: VertexArray.h:201
uint32_t m_Stride
Definition: VertexArray.h:222
size_t m_NumberOfVertices
Definition: VertexArray.h:218
void UploadIfNeeded(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: VertexArray.cpp:300
void AddAttribute(Attribute *attr)
Definition: VertexArray.cpp:117
void PrepareForRendering()
Definition: VertexArray.cpp:274
~VertexArray()
Definition: VertexArray.cpp:82
void FreeBackingStore()
Definition: VertexArray.cpp:307
VertexArrayIterator< T > MakeIterator(const Attribute *attr)
Definition: VertexArray.h:209
uint32_t m_Usage
Definition: VertexArray.h:216
void SetMinimumAttributeAlignment(const uint32_t minimumAttributeAlignment)
Definition: VertexArray.cpp:106
void Layout()
Definition: VertexArray.cpp:242
uint32_t GetStride() const
Definition: VertexArray.h:182
void Free()
Definition: VertexArray.cpp:88
Renderer::Backend::IBuffer::Type m_Type
Definition: VertexArray.h:215
size_t GetNumberOfVertices() const
Definition: VertexArray.h:181
std::vector< Attribute * > m_Attributes
Definition: VertexArray.h:219
void Upload()
Definition: VertexArray.cpp:281
A VertexArray that is specialised to handle 16-bit array indices.
Definition: VertexArray.h:231
VertexArrayIterator< std::uint16_t > GetIterator() const
Gets the iterator over the (only) attribute in this array, i.e. a std::uint16_t.
Definition: VertexArray.cpp:323
Attribute m_Attr
Definition: VertexArray.h:239
VertexIndexArray(const uint32_t usage)
Definition: VertexArray.cpp:316
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
Definition: Canvas2D.h:30
Format
Definition: Format.h:28
#define T(string_literal)
Definition: secure_crt.cpp:81
CVertexBuffer * m_Owner
Definition: VertexBuffer.h:71
size_t m_Index
Definition: VertexBuffer.h:73
Definition: VertexArray.h:147
VertexArray * vertexArray
Definition: VertexArray.h:153
VertexArrayIterator< T > GetIterator() const
Renderer::Backend::Format format
Definition: VertexArray.h:148
uint32_t offset
Definition: VertexArray.h:151
Attribute()
Definition: VertexArray.h:155
intptr_t ssize_t
Definition: wposix_types.h:36