Debian Codegraph @

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