Cogs.Core
IBuffers.h
1#pragma once
2
3#include "VertexFormat.h"
4#include "Flags.h"
5#include "IContext.h"
6
7namespace Cogs
8{
12 struct IBuffers
13 {
17 virtual void annotate(BufferHandle handle, const StringView& name) { (void)handle; (void)name; }
18
22 virtual void annotate(VertexBufferHandle handle, const StringView& name) { (void)handle; (void)name; }
23
33 virtual VertexBufferHandle loadVertexBuffer(const void * vertexData, const size_t count, const VertexFormat & vertexFormat) = 0;
34 virtual VertexBufferHandle loadVertexBuffer(const void * vertexData, const size_t count, VertexFormatHandle vertexFormatHandle) = 0;
35
41 virtual void releaseVertexBuffer(VertexBufferHandle vertexBufferHandle) = 0;
42
52 virtual IndexBufferHandle loadIndexBuffer(const void * indexData, const size_t count, const size_t indexSize) = 0;
53
59 virtual void releaseIndexBuffer(IndexBufferHandle indexBufferHandle) = 0;
60
77 const VertexBufferHandle* vertexBufferHandles, const size_t vertexBufferCount,
78 const VertexFormat* const* vertexFormats = nullptr, const uint32_t* vertexBufferStrides = nullptr, const uint32_t* vertexBufferOffsets = nullptr,
79 const IndexBufferHandle indexBufferHandle = IndexBufferHandle::NoHandle, uint32_t indexBufferStride = 0) = 0;
80
86 virtual void releaseVertexArrayObject(VertexArrayObjectHandle vertexArrayObjectHandle) = 0;
87
100 virtual InputLayoutHandle loadInputLayout(const VertexFormatHandle * vertexFormats, const size_t count, EffectHandle effectHandle) = 0;
101
107 virtual void releaseInputLayout(InputLayoutHandle inputLayoutHandle) = 0;
108
120 virtual BufferHandle loadBuffer(const void * data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride = 0) = 0;
121
122 virtual VertexFormatHandle createVertexFormat(const VertexElement * elements, size_t count) = 0;
123 virtual const VertexFormat * getVertexFormat(VertexFormatHandle handle) = 0;
124
132 virtual void retrieveSubBuffer(void * data, BufferHandle source, const size_t offset, const size_t size) = 0;
133
139 virtual void releaseBuffer(BufferHandle bufferHandle) = 0;
140
147 virtual void * getNativeHandle(BufferHandle bufferHandle) = 0;
148
152 virtual void releaseResources() = 0;
153 };
154
158 template<typename MappedDataType>
160 {
168 MappedBuffer(struct IContext * context, BufferHandle bufferHandle, MapMode::EMapMode mapMode = MapMode::Read, size_t size = 0) :
169 context(context),
170 bufferHandle(bufferHandle),
171 size(size)
172 {
173 data = reinterpret_cast<MappedDataType *>(context->map(bufferHandle, mapMode, &stride));
174 }
175
177 MappedBuffer(const MappedBuffer & other) = delete;
178
183 {
184 if (data) {
185 context->unmap(bufferHandle);
186 }
187 }
188
194 MappedDataType * get()
195 {
196 return data;
197 }
198
200 MappedDataType * operator->() { return data; }
201
203 const MappedDataType * operator->() const { return data; }
204
209 operator MappedDataType *() { return data; }
210
224 explicit operator bool() const { return data != nullptr; }
225
232 MappedDataType & operator[](size_t index)
233 {
234 if (size != 0) {
235 assert(index < size && "Index out of bounds.");
236 }
237
238 return data[index];
239 }
240
247 const MappedDataType & operator[](size_t index) const
248 {
249 if (size != 0) {
250 assert(index < size && "Index out of bounds.");
251 }
252
253 return data[index];
254 }
255
257 uint32_t getStride() const { return stride; }
258
259 private:
260 struct IContext * context = nullptr;
261 BufferHandle bufferHandle;
262 MappedDataType * data = nullptr;
263 size_t size = 0;
264 uint32_t stride = 0;
265 };
266}
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
static const Handle_t NoHandle
Represents a handle to nothing.
Definition: Common.h:77
Provides buffer management functionality.
Definition: IBuffers.h:13
virtual void releaseVertexBuffer(VertexBufferHandle vertexBufferHandle)=0
Release the vertex buffer with the given handle.
virtual void annotate(VertexBufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
Definition: IBuffers.h:22
virtual VertexArrayObjectHandle loadVertexArrayObject(const EffectHandle effectHandle, const VertexBufferHandle *vertexBufferHandles, const size_t vertexBufferCount, const VertexFormat *const *vertexFormats=nullptr, const uint32_t *vertexBufferStrides=nullptr, const uint32_t *vertexBufferOffsets=nullptr, const IndexBufferHandle indexBufferHandle=IndexBufferHandle::NoHandle, uint32_t indexBufferStride=0)=0
Create a vertex array object that encapsulates binding of a set of vertex buffers and an optional ind...
virtual void annotate(BufferHandle handle, const StringView &name)
Associate a name with an object for use in graphics debugging.
Definition: IBuffers.h:17
virtual void retrieveSubBuffer(void *data, BufferHandle source, const size_t offset, const size_t size)=0
Retrieves the contents of a buffer.
virtual void * getNativeHandle(BufferHandle bufferHandle)=0
Get the device-specific handle (D3D buffer pointer, OpenGL buffer ID etc) associated with the given b...
virtual InputLayoutHandle loadInputLayout(const VertexFormatHandle *vertexFormats, const size_t count, EffectHandle effectHandle)=0
Loads a new input layout to map vertex flow between vertex buffers with the given vertexFormats to ef...
virtual void releaseResources()=0
Releases all allocated buffer resources.
virtual IndexBufferHandle loadIndexBuffer(const void *indexData, const size_t count, const size_t indexSize)=0
Loads a new index buffer and populates it with the given indexData.
virtual void releaseInputLayout(InputLayoutHandle inputLayoutHandle)=0
Releases the input layout with the given inputLayoutHandle.
virtual void releaseIndexBuffer(IndexBufferHandle indexBufferHandle)=0
Releases the index buffer with the given handle.
virtual BufferHandle loadBuffer(const void *data, const size_t size, Usage::EUsage usage, uint32_t accessMode, uint32_t bindFlags, uint32_t stride=0)=0
Loads a new buffer using the given data to populate the buffer.
virtual void releaseBuffer(BufferHandle bufferHandle)=0
Releases the buffer with the given bufferHandle.
virtual VertexBufferHandle loadVertexBuffer(const void *vertexData, const size_t count, const VertexFormat &vertexFormat)=0
Loads a new vertex buffer and populates it with the given data.
virtual void releaseVertexArrayObject(VertexArrayObjectHandle vertexArrayObjectHandle)=0
Releases the vertex array object with the given handle.
Represents a graphics device context which can receive rendering commands.
Definition: IContext.h:43
virtual void * map(BufferHandle bufferHandle, MapMode::EMapMode mapMode, uint32_t *stride=nullptr)=0
Maps the given buffer so it can be accessed.
virtual void unmap(BufferHandle bufferHandle)=0
Unmaps the given buffer, applying any synchronization necessary to reflect changes in the mapped memo...
EMapMode
Mapping mode enumeration.
Definition: Flags.h:93
Provides RAII style mapping of a buffer resource.
Definition: IBuffers.h:160
const MappedDataType & operator[](size_t index) const
Indexing operator provided to perform optional boundary checks on the mapped data.
Definition: IBuffers.h:247
MappedBuffer(struct IContext *context, BufferHandle bufferHandle, MapMode::EMapMode mapMode=MapMode::Read, size_t size=0)
Constructs a mapped buffer, initializing the mapped contents from the context and buffer handle given...
Definition: IBuffers.h:168
~MappedBuffer()
Destructs the mapped buffer.
Definition: IBuffers.h:182
MappedDataType * get()
Get the raw pointer to the mapped data.
Definition: IBuffers.h:194
const MappedDataType * operator->() const
Const overload of pointer operator.
Definition: IBuffers.h:203
MappedBuffer(const MappedBuffer &other)=delete
Deleted copy constructor.
MappedDataType * operator->()
Pointer operator to allow using the mapped structure as if it was a raw pointer to the data.
Definition: IBuffers.h:200
MappedDataType & operator[](size_t index)
Indexing operator provided to perform optional boundary checks on the mapped data.
Definition: IBuffers.h:232
uint32_t getStride() const
Get the stride of the mapped resource in bytes.
Definition: IBuffers.h:257
EUsage
Usage enumeration.
Definition: Flags.h:24
Vertex element structure used to describe a single data element in a vertex for the input assembler.
Definition: VertexFormat.h:38
Vertex format structure used to describe a single vertex for the input assembler.
Definition: VertexFormat.h:60