Debian Codegraph @

Sources/nCine/Graphics/RHI/Vulkan/VulkanVertexFormat.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::Vulkan
12 {
13 class VulkanBufferObject;
14
15 /**
16 @brief Vertex layout description of the Vulkan 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 backend
20 builds the graphics pipeline's `VkVertexInputAttributeDescription`/`VkVertexInputBindingDescription`
21 from the recorded layout (fed by the offline shader reflection).
22 */
23 class VulkanVertexFormat
24 {
25 public:
26 /** @brief The maximum number of vertex attributes */
27 static constexpr std::uint32_t MaxAttributes = 16;
28
29 /** @brief A single vertex attribute within a vertex format */
30 class Attribute
31 {
32 friend class VulkanVertexFormat;
33
34 public:
35 Attribute()
36 : _vbo(nullptr), _pointer(nullptr), _index(0), _size(0), _type(0), _stride(0), _baseOffset(0), _enabled(false), _normalized(false) {}
37
38 void Init(std::uint32_t index, std::int32_t size, std::uint32_t type) {
39 _index = index;
40 _size = size;
41 _type = type;
42 _enabled = true;
43 }
44
45 // Mirrors the OpenGL backend's `Attribute::operator==` (only enabled attributes are compared in
46 // depth); the source buffer is compared by pointer identity, which is equivalent for this backend
47 // and avoids needing the complete `VulkanBufferObject` type in this header
48 bool operator==(const Attribute& other) const {
49 return ((other._enabled == false && _enabled == false) ||
50 ((other._enabled == true && _enabled == true) &&
51 other._vbo == _vbo &&
52 other._index == _index &&
53 other._size == _size &&
54 other._type == _type &&
55 other._normalized == _normalized &&
56 other._stride == _stride &&
57 other._pointer == _pointer &&
58 other._baseOffset == _baseOffset));
59 }
60 bool operator!=(const Attribute& other) const {
61 return !operator==(other);
62 }
63
64 inline bool IsEnabled() const {
65 return _enabled;
66 }
67 inline const VulkanBufferObject* GetVbo() const {
68 return _vbo;
69 }
70 inline std::uint32_t GetIndex() const {
71 return _index;
72 }
73 inline std::int32_t GetSize() const {
74 return _size;
75 }
76 inline std::uint32_t GetType() const {
77 return _type;
78 }
79 inline bool IsNormalized() const {
80 return _normalized;
81 }
82 inline std::int32_t GetStride() const {
83 return _stride;
84 }
85 inline const void* GetPointer() const {
86 return _pointer;
87 }
88 inline std::uint32_t GetBaseOffset() const {
89 return _baseOffset;
90 }
91
92 void SetVboParameters(std::int32_t stride, const void* pointer) {
93 _stride = stride;
94 _pointer = pointer;
95 }
96 inline void setVbo(const VulkanBufferObject* vbo) {
97 _vbo = vbo;
98 }
99 inline void SetBaseOffset(std::uint32_t baseOffset) {
100 _baseOffset = baseOffset;
101 }
102 inline void SetSize(std::int32_t size) {
103 _size = size;
104 }
105 inline void SetType(std::uint32_t type) {
106 _type = type;
107 }
108 inline void SetNormalized(bool normalized) {
109 _normalized = normalized;
110 }
111
112 private:
113 const VulkanBufferObject* _vbo;
114 const void* _pointer;
115 std::uint32_t _index;
116 std::int32_t _size;
117 std::uint32_t _type;
118 std::int32_t _stride;
119 std::uint32_t _baseOffset;
120 bool _enabled;
121 bool _normalized;
122 };
123
124 VulkanVertexFormat()
125 : _ibo(nullptr) {}
126
127 inline std::uint32_t GetAttributeCount() const {
128 return std::uint32_t(_attributes.size());
129 }
130
131 inline const VulkanBufferObject* GetIbo() const {
132 return _ibo;
133 }
134 inline void SetIbo(const VulkanBufferObject* ibo) {
135 _ibo = ibo;
136 }
137 /** @brief Applies the vertex format (no-op; the recorded layout is consumed at pipeline creation) */
138 void Define() {}
139 /** @brief Disables all attributes and clears the index buffer */
140 void Reset() {
141 _attributes.clear();
142 _ibo = nullptr;
143 }
144
145 std::uint64_t CalculateFingerprint() const {
146 return 0;
147 }
148
149 inline Attribute& operator[](std::uint32_t index) {
150 if (index >= _attributes.size()) {
151 _attributes.resize(index + 1);
152 }
153 return _attributes[index];
154 }
155 inline const Attribute& operator[](std::uint32_t index) const {
156 return _attributes[index];
157 }
158
159 // Mirrors the OpenGL backend's `GLVertexFormat::operator==`: equal index buffers and attribute sets
160 bool operator==(const VulkanVertexFormat& other) const {
161 if (other._ibo != _ibo || other._attributes.size() != _attributes.size()) {
162 return false;
163 }
164 for (std::uint32_t i = 0; i < _attributes.size(); i++) {
165 if (other._attributes[i] != _attributes[i]) {
166 return false;
167 }
168 }
169 return true;
170 }
171 bool operator!=(const VulkanVertexFormat& other) const {
172 return !operator==(other);
173 }
174
175 private:
176 SmallVector<Attribute, MaxAttributes> _attributes;
177 const VulkanBufferObject* _ibo;
178 };
179
180 /**
181 @brief Vertex array object stub of the Vulkan backend (aliased as `RHI::VertexArray`)
182
183 Vulkan has no vertex-array object; the vertex input state lives in the graphics pipeline and the
184 buffer bindings are set per draw with `vkCmdBindVertexBuffers`. This only satisfies the contract alias
185 with inert bind/label operations.
186 */
187 class VulkanVertexArray
188 {
189 public:
190 VulkanVertexArray() = default;
191 ~VulkanVertexArray() = default;
192
193 VulkanVertexArray(const VulkanVertexArray&) = delete;
194 VulkanVertexArray& operator=(const VulkanVertexArray&) = 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 }