Karma Engine
Loading...
Searching...
No Matches
Buffer.h
1#pragma once
2
3#include "krpch.h"
4
5#include "glm/gtc/type_ptr.hpp"
6#include "stb_image.h"
7
8namespace Karma
9{
10 enum class ShaderDataType
11 {
12 None = 0,
13 Float,
14 Float2,
15 Float3,
16 Float4,
17 Mat3,
18 Mat4,
19 Int,
20 Int2,
21 Int3,
22 Int4,
23 Bool
24 };
25
26 static uint32_t ShaderDataTypeSize(ShaderDataType type)
27 {
28 switch (type)
29 {
30 case ShaderDataType::Float:
31 return 4;
32 case ShaderDataType::Float2:
33 return 4 * 2;
34 case ShaderDataType::Float3:
35 return 4 * 3;
36 case ShaderDataType::Float4:
37 return 4 * 4;
38 case ShaderDataType::Mat3:
39 return 4 * 3 * 3;
40 case ShaderDataType::Mat4:
41 return 4 * 4 * 4;
42 case ShaderDataType::Int:
43 return 4;
44 case ShaderDataType::Int2:
45 return 4 * 2;
46 case ShaderDataType::Int3:
47 return 4 * 3;
48 case ShaderDataType::Int4:
49 return 4 * 4;
50 case ShaderDataType::Bool:
51 return 4;
52 case ShaderDataType::None:
53 KR_CORE_WARN("ShaderDataType is none. Size shall be considered 0.");
54 return 0;
55 }
56
57 KR_CORE_ASSERT(false, "Unknown ShaderDataType");
58 return 0;
59 }
60
62 {
63 std::string Name;
64 uint64_t Offset;
65 uint32_t Size;
66 ShaderDataType Type;
67 bool Normalized;
68
69 // May need to think about the custom default offsets
70 BufferElement(ShaderDataType type, const std::string& name, bool normalized = false, uint64_t offset = 0)
71 : Name(name), Type(type), Size(ShaderDataTypeSize(type)), Offset(offset), Normalized(normalized)
72 {
73 }
74
75 uint32_t GetComponentCount() const
76 {
77 switch (Type)
78 {
79 case Karma::ShaderDataType::None:
80 return 0;
81 case Karma::ShaderDataType::Float:
82 return 1;
83 case Karma::ShaderDataType::Float2:
84 return 2;
85 case Karma::ShaderDataType::Float3:
86 return 3;
87 case Karma::ShaderDataType::Float4:
88 return 4;
89 case Karma::ShaderDataType::Mat3:
90 return 3 * 3;
91 case Karma::ShaderDataType::Mat4:
92 return 4 * 4;
93 case Karma::ShaderDataType::Int:
94 return 1;
95 case Karma::ShaderDataType::Int2:
96 return 2;
97 case Karma::ShaderDataType::Int3:
98 return 3;
99 case Karma::ShaderDataType::Int4:
100 return 4;
101 case Karma::ShaderDataType::Bool:
102 return 1;
103 }
104
105 KR_CORE_ASSERT(false, "Unknown ShaderDataType!");
106 return 0;
107 }
108 };
109
110 class KARMA_API BufferLayout
111 {
112 public:
114 {
115 }
116
117 BufferLayout(const std::initializer_list<BufferElement>& elements)
118 : m_Elements(elements)
119 {
120 CalculateOffsetsAndStride();
121 }
122
123 inline uint32_t GetStride() const { return m_Stride; }
124 inline const std::vector<BufferElement>& GetElements() const { return m_Elements; }
125
126 inline void PushElement(const BufferElement& element) { m_Elements.push_back(element); CalculateOffsetsAndStride(); }
127
128 std::vector<BufferElement>::iterator begin()
129 {
130 return m_Elements.begin();
131 }
132
133 std::vector<BufferElement>::iterator end()
134 {
135 return m_Elements.end();
136 }
137
138 std::vector<BufferElement>::const_iterator begin() const
139 {
140 return m_Elements.begin();
141 }
142
143 std::vector<BufferElement>::const_iterator end() const
144 {
145 return m_Elements.end();
146 }
147
148 private:
149 void CalculateOffsetsAndStride()
150 {
151 uint32_t offset = 0;
152 m_Stride = 0;
153 for (auto& element : m_Elements)
154 {
155 element.Offset = offset;
156 offset += element.Size;
157 m_Stride += element.Size;
158 }
159 }
160
161 private:
162 std::vector<BufferElement> m_Elements;
163 uint32_t m_Stride = 0;
164 };
165
166 class KARMA_API VertexBuffer
167 {
168 public:
169 virtual ~VertexBuffer() = default;
170
171 virtual void Bind() const = 0;
172 virtual void UnBind() const = 0;
173
174 virtual const BufferLayout& GetLayout() const = 0;
175 virtual void SetLayout(const BufferLayout& layout) = 0;
176
177 static VertexBuffer* Create(float* vertices, uint32_t size);
178 };
179
180 class KARMA_API IndexBuffer
181 {
182 public:
183 virtual ~IndexBuffer() = default;
184
185 virtual void Bind() const = 0;
186 virtual void UnBind() const = 0;
187
188 virtual uint32_t GetCount() const = 0;
189
190 static IndexBuffer* Create(uint32_t* vertices, uint32_t size);
191 };
192
193 class KARMA_API ImageBuffer
194 {
195 public:
196 static ImageBuffer* Create(const char* filename);
197 uint32_t GetBindingPointIndex() const
198 {
199 return m_BindingPoint;
200 }
201 protected:
202 uint32_t m_BindingPoint;
203 };
204
205 class KARMA_API UBODataPointer
206 {
207 public:
208 UBODataPointer(const void* data) : m_DataPointer(data)
209 {}
210
211 const void* GetDataPointer() const
212 {
213 return m_DataPointer;
214 }
215 private:
216 const void* m_DataPointer;
217 };
218
219 struct KARMA_API UniformBufferObject
220 {
221 static UniformBufferObject* Create(std::vector<ShaderDataType> dataTypes, uint32_t bindingPointIndex);
222
223 UniformBufferObject(std::vector<ShaderDataType> dataTypes, uint32_t bindingPointIndex);
224 virtual ~UniformBufferObject() = default;
225
226 template<typename... T>
227 void UpdateUniforms(T&&... uniforms)
228 {
229 m_UniformList = { uniforms... };
230 }
231
232 virtual void UploadUniformBuffer() {}
233
234 uint32_t GetBufferSize() const
235 {
236 return m_BufferSize;
237 }
238
239 const std::vector<UBODataPointer>& GetUniformList() const
240 {
241 return m_UniformList;
242 }
243
244 const std::vector<ShaderDataType>& GetUniformDataType() const
245 {
246 return m_UniformDataType;
247 }
248
249 const std::vector<uint32_t>& GetAlignedOffsets() const
250 {
251 return m_UniformAlignedOffsets;
252 }
253
254 const std::vector<uint32_t>& GetUniformSize() const
255 {
256 return m_UniformSizes;
257 }
258
259 uint32_t GetBindingPointIndex() const
260 {
261 return m_BindingPoint;
262 }
263
264 protected:
265 void CalculateOffsetsAndBufferSize();
266
267 protected:
268 uint32_t m_BufferSize;
269 uint32_t m_BindingPoint;
270 std::vector<UBODataPointer> m_UniformList;
271 std::vector<ShaderDataType> m_UniformDataType;
272 std::vector<uint32_t> m_UniformAlignedOffsets;
273 std::vector<uint32_t> m_UniformSizes;
274
275 private:
276 uint32_t ComputeBaseAlignment(ShaderDataType dataType);
277 };
278}
Definition Buffer.h:111
Definition Buffer.h:194
Definition Buffer.h:181
Definition Buffer.h:206
Definition Buffer.h:167
@ None
Definition KarmaTypes.h:33
Definition Buffer.h:62
Definition Buffer.h:220