CoriEngine
Loading...
Searching...
No Matches
Buffers.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace Cori {
4 namespace Graphics {
12
13 static constexpr size_t ShaderDataTypeSize(ShaderDataType type) {
14 constexpr size_t sizes[] = {
15 sizeof(void*), // None
16 sizeof(float), // Float
17 sizeof(float) * 2, // Vec2
18 sizeof(float) * 3, // Vec3
19 sizeof(float) * 4, // Vec4
20 sizeof(float) * 3 * 3, // Mat3
21 sizeof(float) * 4 * 4, // Mat4
22 sizeof(int32_t), // Int
23 sizeof(int32_t) * 2, // Int2
24 sizeof(int32_t) * 3, // Int3
25 sizeof(int32_t) * 4, // Int4
26 sizeof(uint32_t), // UInt
27 sizeof(uint32_t) * 2, // UInt2
28 sizeof(uint32_t) * 3, // UInt3
29 sizeof(uint32_t) * 4, // UInt4
30 sizeof(bool), // Bool
31 };
32
33 static_assert(sizeof(sizes) / sizeof(size_t) == static_cast<size_t>(ShaderDataType::Bool) + 1, "ShaderDataTypeSize: Size array is out of sync with ShaderDataType enum");
34
35 if (CORI_CORE_CHECK(static_cast<size_t>(type) < sizeof(sizes) / sizeof(size_t), "ShaderDataTypeSize: Unknown shader data type '{}'", static_cast<int>(type))) { return 0; }
36
37 return sizes[static_cast<size_t>(type)];
38 }
39
40 static constexpr size_t ShaderDataTypeComponentCount(ShaderDataType type) {
41 constexpr size_t sizes[] = {
42 0, // None
43 1, // Float
44 2, // Vec2
45 3, // Vec3
46 4, // Vec4
47 3, // Mat3
48 4, // Mat4
49 1, // Int
50 2, // Int2
51 3, // Int3
52 4, // Int4
53 1, // UInt
54 2, // UInt2
55 3, // UInt3
56 4, // UInt4
57 1, // Bool
58 };
59
60 static_assert(sizeof(sizes) / sizeof(size_t) == static_cast<size_t>(ShaderDataType::Bool) + 1, "ShaderDataTypeComponentCount: Size array is out of sync with ShaderDataType enum");
61
62 if (CORI_CORE_CHECK(static_cast<size_t>(type) < sizeof(sizes) / sizeof(size_t), "ShaderDataTypeComponentCount: Unknown shader data type '{}'", static_cast<int>(type))) { return 0; }
63
64 return sizes[static_cast<size_t>(type)];
65 }
66
67 class VBElement {
68 public:
69 VBElement(const ShaderDataType type, const std::string& name, const uint32_t divisor = 0, const bool normalized = false) : m_Name(name), m_Type(type), m_Divisor(divisor), m_Normalized(normalized), m_Size(ShaderDataTypeSize(type)), m_RuntimeID(s_NextRuntimeID.fetch_add(1, std::memory_order_relaxed)) {}
70
71 std::string m_Name;
73 uint32_t m_Divisor;
75 uint32_t m_Size;
76 uint32_t m_Offset{0};
77
79
80 bool operator==(const VBElement& other) const {
81 return m_RuntimeID == other.m_RuntimeID;
82 }
83
84 bool operator!=(const VBElement& other) const {
85 return m_RuntimeID != other.m_RuntimeID;
86 }
87
88 VBElement(const VBElement& other)
89 : m_Name(other.m_Name),
90 m_Type(other.m_Type),
91 m_Divisor(other.m_Divisor),
93 m_Size(other.m_Size),
94 m_Offset(other.m_Offset),
95 m_RuntimeID(s_NextRuntimeID.fetch_add(1, std::memory_order_relaxed)) {}
96
97 VBElement(VBElement&& other) noexcept
98 : m_Name(std::move(other.m_Name)),
99 m_Type(other.m_Type),
100 m_Divisor(other.m_Divisor),
101 m_Normalized(other.m_Normalized),
102 m_Size(other.m_Size),
103 m_Offset(other.m_Offset),
104 m_RuntimeID(other.m_RuntimeID) {}
105
106 VBElement& operator=(VBElement&& other) noexcept {
107 if (this != &other) {
108 m_Name = std::move(other.m_Name);
109 m_Type = other.m_Type;
110 m_Divisor = other.m_Divisor;
111 m_Normalized = other.m_Normalized;
112 m_Size = other.m_Size;
113 m_Offset = other.m_Offset;
114 }
115
116 return *this;
117 }
118
119 private:
120 const uint32_t m_RuntimeID{0};
121 inline static std::atomic<uint32_t> s_NextRuntimeID{1};
122 };
123
124 class VBLayout {
125 public:
126 VBLayout() = default;
127
128 VBLayout(const std::initializer_list<VBElement>& elements) : m_Elements(elements) {
129 for (auto& element : m_Elements) {
130 element.m_Offset = m_Stride;
131 m_Stride += element.m_Size;
132 }
133 }
134
135 VBLayout(const VBLayout& other)
136 : m_Elements(other.m_Elements),
137 m_Stride(other.m_Stride) {}
138
139 VBLayout& operator=(const VBLayout& other) {
140 if (this == &other) {
141 return *this;
142 }
143
144 m_Elements.clear();
145 m_Elements.reserve(other.m_Elements.size());
146
147 for (const auto& src_element : other.m_Elements) {
148 m_Elements.emplace_back(src_element);
149 }
150
151 m_Stride = other.m_Stride;
152
153 return *this;
154 }
155
156 VBLayout(VBLayout&& other) noexcept
157 : m_Elements(std::move(other.m_Elements)),
158 m_Stride(other.m_Stride) {
159 other.m_Stride = 0;
160 }
161
162 VBLayout& operator=(VBLayout&& other) noexcept {
163 if (this == &other) {
164 return *this;
165 }
166
167 m_Elements = std::move(other.m_Elements);
168 m_Stride = other.m_Stride;
169 other.m_Stride = 0;
170
171 return *this;
172 }
173
174 uint32_t GetStride() const { return m_Stride; }
175 const std::vector<VBElement>& GetElements() const { return m_Elements; }
176
177 VBElement& front() { return m_Elements.front(); }
178 const VBElement& front() const { return m_Elements.front(); }
179 VBElement& back() { return m_Elements.back(); }
180 const VBElement& back() const { return m_Elements.back(); }
181
182 std::vector<VBElement>::iterator begin() { return m_Elements.begin(); }
183 std::vector<VBElement>::iterator end() { return m_Elements.end(); }
184 std::vector<VBElement>::const_iterator begin() const { return m_Elements.begin(); }
185 std::vector<VBElement>::const_iterator end() const { return m_Elements.end(); }
186
187 private:
188 std::vector<VBElement> m_Elements;
189 uint32_t m_Stride{ 0 };
190 };
191
193 public:
194 enum class DRAW_TYPE {
197 };
198
199 virtual ~VertexBuffer() = default;
200 virtual void Init(const float* vertices, uint32_t size, DRAW_TYPE drawTyPpe) = 0;
201 virtual void Bind() const = 0;
202 virtual void Unbind() const = 0;
203
204 virtual void SetLayout(const VBLayout& layout) = 0;
205 [[nodiscard]] virtual const VBLayout& GetLayout() const = 0;
206
207 virtual void SetData(const void* data, uint32_t size) const = 0;
208
209 [[nodiscard]] static std::shared_ptr<VertexBuffer> Create();
210 };
211
213 public:
214 virtual ~IndexBuffer() = default;
215 virtual void Bind() const = 0;
216 virtual void Unbind() const = 0;
217
218 [[nodiscard]] virtual uint32_t GetCount() const = 0;
219
220 [[nodiscard]] static std::shared_ptr<IndexBuffer> Create(uint32_t* indices, uint32_t count);
221 };
222 }
223}
#define CORI_CORE_CHECK(x,...)
Definition Logger.hpp:1042
virtual ~IndexBuffer()=default
virtual void Unbind() const =0
virtual uint32_t GetCount() const =0
virtual void Bind() const =0
static std::shared_ptr< IndexBuffer > Create(uint32_t *indices, uint32_t count)
Definition Buffers.cpp:29
size_t GetComponentCount() const
Definition Buffers.hpp:78
bool operator!=(const VBElement &other) const
Definition Buffers.hpp:84
VBElement(VBElement &&other) noexcept
Definition Buffers.hpp:97
bool operator==(const VBElement &other) const
Definition Buffers.hpp:80
VBElement & operator=(VBElement &&other) noexcept
Definition Buffers.hpp:106
VBElement(const ShaderDataType type, const std::string &name, const uint32_t divisor=0, const bool normalized=false)
Definition Buffers.hpp:69
VBElement(const VBElement &other)
Definition Buffers.hpp:88
ShaderDataType m_Type
Definition Buffers.hpp:72
const VBElement & front() const
Definition Buffers.hpp:178
std::vector< VBElement >::const_iterator end() const
Definition Buffers.hpp:185
const std::vector< VBElement > & GetElements() const
Definition Buffers.hpp:175
VBLayout & operator=(const VBLayout &other)
Definition Buffers.hpp:139
VBLayout & operator=(VBLayout &&other) noexcept
Definition Buffers.hpp:162
VBLayout(const VBLayout &other)
Definition Buffers.hpp:135
std::vector< VBElement >::iterator begin()
Definition Buffers.hpp:182
const VBElement & back() const
Definition Buffers.hpp:180
std::vector< VBElement >::const_iterator begin() const
Definition Buffers.hpp:184
uint32_t GetStride() const
Definition Buffers.hpp:174
std::vector< VBElement >::iterator end()
Definition Buffers.hpp:183
VBLayout(VBLayout &&other) noexcept
Definition Buffers.hpp:156
VBLayout(const std::initializer_list< VBElement > &elements)
Definition Buffers.hpp:128
virtual void Init(const float *vertices, uint32_t size, DRAW_TYPE drawTyPpe)=0
virtual void Unbind() const =0
virtual void Bind() const =0
static std::shared_ptr< VertexBuffer > Create()
Definition Buffers.cpp:7
virtual void SetData(const void *data, uint32_t size) const =0
virtual void SetLayout(const VBLayout &layout)=0
virtual ~VertexBuffer()=default
virtual const VBLayout & GetLayout() const =0
Almost everything connected to graphics is in this namespace.
Definition Window.hpp:7
static constexpr size_t ShaderDataTypeComponentCount(ShaderDataType type)
Definition Buffers.hpp:40
static constexpr size_t ShaderDataTypeSize(ShaderDataType type)
Definition Buffers.hpp:13
Global engine namespace.