Cogs.Core
Buffer.h
1#pragma once
2
3#include "ResourceBase.h"
4#include "Resources.h"
5
6#include "Foundation/Memory/MemoryBuffer.h"
7#include "Foundation/Reflection/Name.h"
8
9#include <glm/vec3.hpp>
10#include <glm/vec4.hpp>
11
12#include <OsoMemoryProfiler/CogsMemoryProfile.h>
13
14namespace Cogs::Core
15{
18 {
20 enum EBufferFlags : uint16_t
21 {
23 None = 0,
25 Dirty = 1 << 0,
27 Mapped = 1 << 1,
28 };
29 };
30
33 {
35 enum EBufferBindFlags : uint16_t
36 {
38 None = 0,
40 VertexBuffer = 1 << 0,
42 IndexBuffer = 1 << 1,
43 };
44 };
45
49 struct COGSCORE_DLL_API BufferResource : public ResourceBase
50 {
52 BufferResource() : bytes(MemBlockType::BufferResource) {}
53
56 void set(size_t offset, size_t size, const uint8_t * data);
57
59 void invalidate(size_t offset, size_t size);
60
62 size_t size() const { return bytes.size(); }
63
65 bool empty() const { return size() == 0; }
66
68 void resize(size_t size);
69
71 void reset(size_t size, Memory::Allocator * allocator);
72
74 void * data() { return bytes.data(); }
75
77 const void * data() const { return bytes.data(); }
78
80 void * map(uint32_t flags);
81
83 void unmap();
84
86 void clear() { bytes.clear(); setChanged(); }
87
89 void setBindFlags(uint16_t flags) { bindFlags |= flags; setChanged(); }
90
92 uint16_t getBufferFlags() const { return bufferFlags; }
93
95 uint16_t getBindFlags() const { return bindFlags; }
96
98 bool isVertexBuffer() const { return bindFlags & BufferBindFlags::VertexBuffer; }
99
101 bool isIndexBuffer() const { return bindFlags & BufferBindFlags::IndexBuffer; }
102
103 private:
104 // \cond
106 uint16_t bufferFlags = BufferFlags::None;
107 uint16_t bindFlags = BufferBindFlags::None;
108 // \endcond
109 };
110
116 template<typename T>
118 {
120 BufferView() = default;
121
124
127
129 size_t size() const { return resolve()->size() / sizeof(T); };
130
132 bool empty() const { return resolve()->empty(); }
133
135 void resize(size_t size) { resolve()->resize(size * sizeof(T)); }
136
138 T * data() { return reinterpret_cast<T *>(resolve()->data()); }
139 const T * data() const { return reinterpret_cast<const T *>(resolve()->data()); }
140
142 T * map() { return reinterpret_cast<T *>(resolve()->map(0)); }
143
145 void unmap() { resolve()->unmap(); }
146
148 T * begin() { return data(); }
149
151 T * end() { return data() + size(); }
152
154 uint8_t getGeneration() const { auto r = resolve(); return r ? static_cast<uint8_t>(r->getGeneration()) : 0; }
155
157 T & operator[](size_t index)
158 {
159 assert(index * sizeof(T) < resolve()->size() && "Index out of range.");
160 return *(data() + index);
161 }
162
164 const T & operator[](size_t index) const
165 {
166 assert(index * sizeof(T) < resolve()->size() && "Index out of range.");
167 return *(data() + index);
168 }
169
170 private:
171 // Hidden to avoid confusion between size()/data() in buffer and buffer-view by mixing
172 // the use of . and operator->().
173 using ResourceBufferHandle::operator->;
174 };
175}
176
177#if defined( _WIN32 )
178#if __cplusplus < 199711L
179// VS2022 build fails if including this workaround.
180template<typename T>
181inline Cogs::StringView getNameImpl(Cogs::Core::BufferView<T> *) { return "ResourceBufferHandle"; }
182#endif
183#endif
184
185template<>
186inline Cogs::StringView getNameImpl(Cogs::Core::BufferView<uint32_t> *) { return "BufferView<uint32_t>"; }
187template<>
188inline Cogs::StringView getNameImpl(Cogs::Core::BufferView<float> *) { return "BufferView<float>"; }
189template<>
190inline Cogs::StringView getNameImpl(Cogs::Core::BufferView<glm::vec3> *) { return "BufferView<glm::vec3>"; }
191template<>
192inline Cogs::StringView getNameImpl(Cogs::Core::BufferView<glm::vec4> *) { return "BufferView<glm::vec4>"; }
193
194#if defined( __linux__ )
195template<typename T>
196inline Cogs::StringView getNameImpl(Cogs::Core::BufferView<T> *) { return "ResourceBufferHandle"; }
197#endif
Base allocator implementation.
Definition: Allocator.h:30
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains the Engine, Renderer, resource managers and other systems needed to run Cogs....
void setChanged(Cogs::Core::Context *context, Cogs::ComponentModel::Component *component, Reflection::FieldId fieldId)
Must be Called after changing a Component field. Mark field changed. Request engine update.
Definition: FieldSetter.h:25
Buffer binding flags.
Definition: Buffer.h:33
EBufferBindFlags
Buffer bind flags enumeration.
Definition: Buffer.h:36
@ None
Buffer may not be bound as GPU resource.
Definition: Buffer.h:38
@ VertexBuffer
Buffer can be bound as vertex buffer.
Definition: Buffer.h:40
@ IndexBuffer
Buffer can be bound as index buffer.
Definition: Buffer.h:42
Buffer state flags.
Definition: Buffer.h:18
EBufferFlags
Buffer state flags enumeration.
Definition: Buffer.h:21
@ Mapped
Buffer data is mapped by client code.
Definition: Buffer.h:27
@ Dirty
Buffer contents are dirty.
Definition: Buffer.h:25
@ None
Default state.
Definition: Buffer.h:23
Buffer resource.
Definition: Buffer.h:50
void unmap()
Unmap the buffer, signaling writes to mapped buffer memory are done.
Definition: Buffer.cpp:44
bool empty() const
If the buffer is empty. Note that an empty buffer may still have reserved storage.
Definition: Buffer.h:65
const void * data() const
Get a pointer to the buffer data.
Definition: Buffer.h:77
void * map(uint32_t flags)
Map the buffer data backing storage, returning a writable pointer.
Definition: Buffer.cpp:35
void setBindFlags(uint16_t flags)
Set the bind flags for the buffer determining how the buffer data may be uploaded to GPU.
Definition: Buffer.h:89
void resize(size_t size)
Resize the buffer to accomodate the given number of bytes.
Definition: Buffer.cpp:17
bool isVertexBuffer() const
Gets if the buffer data is usable as a vertex data.
Definition: Buffer.h:98
size_t size() const
Size of the buffer in bytes.
Definition: Buffer.h:62
uint16_t getBufferFlags() const
Get the buffer flags.
Definition: Buffer.h:92
void clear()
Clear the buffer contents. Note that the buffer may retain reserved storage after clearing.
Definition: Buffer.h:86
void * data()
Get a pointer to the buffer data.
Definition: Buffer.h:74
bool isIndexBuffer() const
Gets if the buffer data is usable as index buffer data.
Definition: Buffer.h:101
BufferResource()
Construct an empty buffer resource.
Definition: Buffer.h:52
uint16_t getBindFlags() const
Get the bind flags.
Definition: Buffer.h:95
BufferView provides a typed, reference-counted, span of an underlying buffer resource.
Definition: Buffer.h:118
BufferView(const ResourceBufferHandle &other)
Construct a buffer view from the given untyped handle.
Definition: Buffer.h:126
uint8_t getGeneration() const
Get the generation counter.
Definition: Buffer.h:154
T * end()
Get an iterator pointing to one past the final element of type T in the buffer.
Definition: Buffer.h:151
BufferView()=default
Default constructor. A default view has no underlying resource.
void unmap()
Unmap the buffer storage.
Definition: Buffer.h:145
T & operator[](size_t index)
Access the element of type T at index.
Definition: Buffer.h:157
BufferView(BufferResource *b)
Construct a buffer view to the given buffer resource b.
Definition: Buffer.h:123
void resize(size_t size)
Resize the buffer to accomodate size number of elements of type T.
Definition: Buffer.h:135
T * map()
Map the buffer contents, providing write access until unmapped.
Definition: Buffer.h:142
bool empty() const
Gets if the buffer is empty.
Definition: Buffer.h:132
T * begin()
Get an iterator to the first element of type T in the buffer.
Definition: Buffer.h:148
T * data()
Get a pointer to the underlying data. Returns nullptr if no storage is allocated.
Definition: Buffer.h:138
size_t size() const
Gets the size of the buffer in number of elements of type T.
Definition: Buffer.h:129
const T & operator[](size_t index) const
Access the element of type T at index.
Definition: Buffer.h:164
Base class for engine resources.
Definition: ResourceBase.h:107
ResourceHandle_t & operator=(const ResourceHandle_t &other)
Copy assign the handle from the given other handle.
BufferResource * resolve() const
Resolve the handle, returning a pointer to the actual resource.