Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
Buffer.hpp
1#pragma once
2
3#include <stdint.h>
4
5#include <Common/Containers/Bitset.hpp>
6
7#include "Formats.hpp"
8
9namespace Grindstone::GraphicsAPI {
10 enum class BufferUsage : uint8_t {
11 Vertex = 1,
12 Index = 1 << 1,
13 Uniform = 1 << 2,
14 Storage = 1 << 3,
15 Indirect = 1 << 4,
16 TransferSrc = 1 << 5,
17 TransferDst = 1 << 6
18 };
19}
20
21template <>
22struct EnumFlagsTraits<Grindstone::GraphicsAPI::BufferUsage> {
23 static constexpr const char* names[] = {
24 "Vertex",
25 "Index",
26 "Uniform",
27 "Storage",
28 "Indirect",
29 "TransferSrc",
30 "TransferDst"
31 };
32 static constexpr size_t size = 7;
33};
34
35inline Grindstone::GraphicsAPI::BufferUsage operator|(Grindstone::GraphicsAPI::BufferUsage a, const Grindstone::GraphicsAPI::BufferUsage b) {
36 using Underlying = uint8_t;
37 return static_cast<Grindstone::GraphicsAPI::BufferUsage>(static_cast<Underlying>(a) | static_cast<Underlying>(b));
38}
39
40inline Grindstone::GraphicsAPI::BufferUsage operator&(Grindstone::GraphicsAPI::BufferUsage a, const Grindstone::GraphicsAPI::BufferUsage b) {
41 using Underlying = uint8_t;
42 return static_cast<Grindstone::GraphicsAPI::BufferUsage>(static_cast<Underlying>(a) & static_cast<Underlying>(b));
43}
44
45namespace Grindstone::GraphicsAPI {
46 enum class MemUsage : uint8_t {
47 GPUOnly,
48 CPUOnly,
49 CPUToGPU,
50 GPUToCPU
51 };
52
56 class Buffer {
57 public:
58 struct CreateInfo {
59 const char* debugName;
60 const void* content;
61 size_t bufferSize;
63 MemUsage memoryUsage;
64 };
65
66 Buffer(const Grindstone::GraphicsAPI::Buffer::CreateInfo& createInfo) :
67 debugName(createInfo.debugName),
68 bufferUsage(createInfo.bufferUsage),
69 memoryUsage(createInfo.memoryUsage),
70 bufferSize(createInfo.bufferSize) {};
71
72 virtual ~Buffer() {}
73 virtual void* Map() = 0;
74 virtual void Unmap() = 0;
75 virtual void UploadData(const void* data, size_t size, size_t offset = 0) = 0;
76
77 void UploadData(const void* data) {
78 UploadData(data, bufferSize, 0);
79 }
80
81 BufferUsage GetBufferUsage() const {
82 return bufferUsage.GetValueEnum();
83 }
84
85 MemUsage GetMemoryUsage() const {
86 return memoryUsage;
87 }
88
89 size_t GetSize() const {
90 return bufferSize;
91 }
92
93 protected:
94 const char* debugName;
95 Grindstone::Containers::BitsetFlags<BufferUsage> bufferUsage;
96 MemUsage memoryUsage;
97 size_t bufferSize;
98 void* mappedMemoryPtr = nullptr;
99 };
100};
Definition Bitset.hpp:331
Definition Buffer.hpp:56
Definition EnumTraits.hpp:10