Debian Codegraph @

Sources/nCine/Graphics/RHI/Software/SwVertexFormat.h

raw
1 #pragma once
2
3 #include <cstddef>
4 #include <cstdint>
5
6 #include <Containers/SmallVector.h>
7 #include <Containers/StringView.h>
8
9 using namespace Death::Containers;
10
11 namespace nCine::RHI::Software
12 {
13 class SwBuffer;
14
15 /**
16 @brief Vertex layout description of the software backend (aliased as `RHI::VertexFormat`)
17
18 Records the set of vertex attributes (component count, type, stride, offset and source buffer) the
19 way the OpenGL backend does, so the pipeline's attribute setup code compiles unchanged. The
20 software effects that reproduce the sprite/tile shaders synthesize their corners from the draw
21 index and read per-draw data from uniforms, so the recorded layout is currently informational —
22 @ref Define() and @ref Reset() have nothing to apply.
23 */
24 class SwVertexFormat
25 {
26 public:
27 /** @brief The maximum number of vertex attributes */
28 static constexpr std::uint32_t MaxAttributes = 16;
29
30 /** @brief A single vertex attribute within a vertex format */
31 class Attribute
32 {
33 friend class SwVertexFormat;
34
35 public:
36 Attribute()
37 : _vbo(nullptr), _pointer(nullptr), _index(0), _size(0), _type(0), _stride(0), _baseOffset(0), _enabled(false), _normalized(false) {}
38
39 void Init(std::uint32_t index, std::int32_t size, std::uint32_t type) {
40 _index = index;
41 _size = size;
42 _type = type;
43 _enabled = true;
44 }
45
46 // Mirrors the OpenGL backend's `Attribute::operator==` (only enabled attributes are compared in
47 // depth); the source buffer is compared by pointer identity, which is equivalent for the software
48 // backend and avoids needing the complete `SwBuffer` type in this header
49 bool operator==(const Attribute& other) const {
50 return ((other._enabled == false && _enabled == false) ||
51 ((other._enabled == true && _enabled == true) &&
52 other._vbo == _vbo &&
53 other._index == _index &&
54 other._size == _size &&
55 other._type == _type &&
56 other._normalized == _normalized &&
57 other._stride == _stride &&
58 other._pointer == _pointer &&
59 other._baseOffset == _baseOffset));
60 }
61 bool operator!=(const Attribute& other) const {
62 return !operator==(other);
63 }
64
65 inline bool IsEnabled() const {
66 return _enabled;
67 }
68 inline const SwBuffer* GetVbo() const {
69 return _vbo;
70 }
71 inline std::uint32_t GetIndex() const {
72 return _index;
73 }
74 inline std::int32_t GetSize() const {
75 return _size;
76 }
77 inline std::uint32_t GetType() const {
78 return _type;
79 }
80 inline bool IsNormalized() const {
81 return _normalized;
82 }
83 inline std::int32_t GetStride() const {
84 return _stride;
85 }
86 inline const void* GetPointer() const {
87 return _pointer;
88 }
89 inline std::uint32_t GetBaseOffset() const {
90 return _baseOffset;
91 }
92
93 void SetVboParameters(std::int32_t stride, const void* pointer) {
94 _stride = stride;
95 _pointer = pointer;
96 }
97 inline void setVbo(const SwBuffer* vbo) {
98 _vbo = vbo;
99 }
100 inline void SetBaseOffset(std::uint32_t baseOffset) {
101 _baseOffset = baseOffset;
102 }
103 inline void SetSize(std::int32_t size) {
104 _size = size;
105 }
106 inline void SetType(std::uint32_t type) {
107 _type = type;
108 }
109 inline void SetNormalized(bool normalized) {
110 _normalized = normalized;
111 }
112
113 private:
114 const SwBuffer* _vbo;
115 const void* _pointer;
116 std::uint32_t _index;
117 std::int32_t _size;
118 std::uint32_t _type;
119 std::int32_t _stride;
120 std::uint32_t _baseOffset;
121 bool _enabled;
122 bool _normalized;
123 };
124
125 SwVertexFormat()
126 : _ibo(nullptr) {}
127
128 inline std::uint32_t GetAttributeCount() const {
129 return std::uint32_t(_attributes.size());
130 }
131
132 inline const SwBuffer* GetIbo() const {
133 return _ibo;
134 }
135 inline void SetIbo(const SwBuffer* ibo) {
136 _ibo = ibo;
137 }
138 /** @brief Applies the vertex format (no-op for the software backend) */
139 void Define() {}
140 /** @brief Disables all attributes and clears the index buffer */
141 void Reset() {
142 _attributes.clear();
143 _ibo = nullptr;
144 }
145
146 std::uint64_t CalculateFingerprint() const {
147 return 0;
148 }
149
150 inline Attribute& operator[](std::uint32_t index) {
151 if (index >= _attributes.size()) {
152 _attributes.resize(index + 1);
153 }
154 return _attributes[index];
155 }
156 inline const Attribute& operator[](std::uint32_t index) const {
157 return _attributes[index];
158 }
159
160 // Mirrors the OpenGL backend's `GLVertexFormat::operator==`: equal index buffers and attribute sets
161 bool operator==(const SwVertexFormat& other) const {
162 if (other._ibo != _ibo || other._attributes.size() != _attributes.size()) {
163 return false;
164 }
165 for (std::uint32_t i = 0; i < _attributes.size(); i++) {
166 if (other._attributes[i] != _attributes[i]) {
167 return false;
168 }
169 }
170 return true;
171 }
172 bool operator!=(const SwVertexFormat& other) const {
173 return !operator==(other);
174 }
175
176 private:
177 SmallVector<Attribute, MaxAttributes> _attributes;
178 const SwBuffer* _ibo;
179 };
180
181 /**
182 @brief Vertex array object stub of the software backend (aliased as `RHI::VertexArray`)
183
184 The software backend has no vertex-array state to capture, so this only satisfies the contract
185 alias with inert bind/label operations.
186 */
187 class SwVertexArray
188 {
189 public:
190 SwVertexArray() = default;
191 ~SwVertexArray() = default;
192
193 SwVertexArray(const SwVertexArray&) = delete;
194 SwVertexArray& operator=(const SwVertexArray&) = delete;
195
196 inline std::uint32_t GetGLHandle() const {
197 return 0;
198 }
199 bool Bind() const {
200 return true;
201 }
202 static bool Unbind() {
203 return true;
204 }
205 void SetObjectLabel(StringView label) {
206 static_cast<void>(label);
207 }
208 };
209 }