Debian Codegraph @

Sources/nCine/Graphics/RHI/RSX/RsxVertexFormat.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::RSX
12 {
13 class RsxBufferObject;
14
15 /**
16 @brief Vertex layout description of the RSX 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. Unlike the
20 sceGxm backend, which has to bake a layout into a patched vertex program, the RSX takes each
21 attribute's format and source at draw time through `rsxBindVertexArrayAttrib()` - so the recorded
22 layout is simply replayed by @ref RsxDevice::DrawCommon() and nothing is compiled from it.
23 */
24 class RsxVertexFormat
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 RsxVertexFormat;
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 this backend
48 // and avoids needing the complete `RsxBufferObject` 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 RsxBufferObject* 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 RsxBufferObject* 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 RsxBufferObject* _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 RsxVertexFormat()
126 : _ibo(nullptr) {}
127
128 inline std::uint32_t GetAttributeCount() const {
129 return std::uint32_t(_attributes.size());
130 }
131
132 inline const RsxBufferObject* GetIbo() const {
133 return _ibo;
134 }
135 inline void SetIbo(const RsxBufferObject* ibo) {
136 _ibo = ibo;
137 }
138 /** @brief Applies the vertex format (no-op; the recorded layout is consumed at input-layout creation) */
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 /**
147 @brief Returns a value identifying this layout, used by `RenderVaoPool` to reuse a vertex array
148
149 The RSX compiles nothing from a layout, so this backend needs no cache of its own - but the
150 shared `RenderVaoPool` keys on this digest across every backend, so it is provided with the same
151 meaning it has elsewhere: which attribute slots are enabled, and each one's component count,
152 component type, normalization, within-vertex offset and stream stride. The source buffer and its
153 base offset are deliberately left out, so two draws out of different ring-buffer regions match.
154 */
155 std::uint64_t CalculateFingerprint() const {
156 std::uint64_t hash = 0xcbf29ce484222325ull; // FNV-1a, 64-bit
157 const auto mix = [&hash](std::uint64_t value) {
158 hash = (hash ^ value) * 0x100000001b3ull;
159 };
160 mix(_attributes.size());
161 for (std::uint32_t i = 0; i < _attributes.size(); i++) {
162 const Attribute& attribute = _attributes[i];
163 if (!attribute.IsEnabled()) {
164 mix(0);
165 continue;
166 }
167 mix(std::uint64_t(attribute.GetIndex()) | (std::uint64_t(std::uint32_t(attribute.GetSize())) << 8) |
168 (std::uint64_t(attribute.GetType()) << 16) | (std::uint64_t(attribute.IsNormalized() ? 1 : 0) << 48));
169 mix(std::uint64_t(std::uint32_t(attribute.GetStride())) |
170 (std::uint64_t(reinterpret_cast<std::uintptr_t>(attribute.GetPointer())) << 32));
171 }
172 return hash;
173 }
174
175 inline Attribute& operator[](std::uint32_t index) {
176 if (index >= _attributes.size()) {
177 _attributes.resize(index + 1);
178 }
179 return _attributes[index];
180 }
181 inline const Attribute& operator[](std::uint32_t index) const {
182 return _attributes[index];
183 }
184
185 // Mirrors the OpenGL backend's `GLVertexFormat::operator==`: equal index buffers and attribute sets
186 bool operator==(const RsxVertexFormat& other) const {
187 if (other._ibo != _ibo || other._attributes.size() != _attributes.size()) {
188 return false;
189 }
190 for (std::uint32_t i = 0; i < _attributes.size(); i++) {
191 if (other._attributes[i] != _attributes[i]) {
192 return false;
193 }
194 }
195 return true;
196 }
197 bool operator!=(const RsxVertexFormat& other) const {
198 return !operator==(other);
199 }
200
201 private:
202 SmallVector<Attribute, MaxAttributes> _attributes;
203 const RsxBufferObject* _ibo;
204 };
205
206 /**
207 @brief Vertex array object stub of the RSX backend (aliased as `RHI::VertexArray`)
208
209 The RSX has no vertex-array object: every attribute's format, stride and source offset is programmed
210 per draw. This only satisfies the contract alias with inert bind/label operations.
211 */
212 class RsxVertexArray
213 {
214 public:
215 RsxVertexArray() = default;
216 ~RsxVertexArray() = default;
217
218 RsxVertexArray(const RsxVertexArray&) = delete;
219 RsxVertexArray& operator=(const RsxVertexArray&) = delete;
220
221 inline std::uint32_t GetGLHandle() const {
222 return 0;
223 }
224 bool Bind() const {
225 return true;
226 }
227 static bool Unbind() {
228 return true;
229 }
230 void SetObjectLabel(StringView label) {
231 static_cast<void>(label);
232 }
233 };
234 }