Nebula
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include "config.h"
11#include "ids/id.h"
12#include "ids/idpool.h"
13#include "util/stringatom.h"
14#include "coregraphics/config.h"
16#include "gpubuffertypes.h"
17#include "ids/idallocator.h"
18
19namespace CoreGraphics
20{
21
22struct CmdBufferId;
25
27{
28 DeviceLocal, // Buffer can only be used by the GPU
29 HostLocal, // Buffer memory is coherent on the CPU
30 HostCached, // Buffer memory is cached on the CPU and requires flush to update CPU->GPU or invalidate to update GPU->CPU
31 DeviceAndHost // Buffer memory lives on both CPU and GPU and requires a flush/invalidate like HostCached
32};
33
35{
47 AccelerationStructureData = 0x400, // The buffer holding acceleration structures
48 AccelerationStructureScratch = 0x800, // Scratch buffer for building and updating acceleration structures
49 AccelerationStructureInput = 0x1000, // Input to acceleration structure, vertices and indices
50 AccelerationStructureInstances = 0x2000, // Acceleration structure instances buffer
51 ShaderTable = 0x4000
52};
54
63
65{
67 : name(""_atm)
68 , size(1)
69 , elementSize(1)
70 , byteSize(0)
74 , sparse(false)
75 , data(nullptr)
76 , dataSize(0)
77 {}
78
80 SizeT size; // this should be the number of items, for vertex buffers, this is vertex count
81 SizeT elementSize; // this should be the size of each item, for vertex buffers, this is the vertex byte size as received from the vertex layout
82 SizeT byteSize; // if set, uses this for size, otherwise calculates the size from size and elementSize
86 bool sparse;
87 const void* data;
89};
90
96
97
99const BufferId CreateBuffer(const BufferCreateInfo& info);
101void DestroyBuffer(const BufferId id);
102
106const SizeT BufferGetSize(const BufferId id);
108const SizeT BufferGetElementSize(const BufferId id);
110const SizeT BufferGetByteSize(const BufferId id);
113
115void* BufferMap(const BufferId id);
117template <class T> T* BufferMap(const BufferId id);
119void BufferUnmap(const BufferId id);
120
122void BufferUpdate(const BufferId id, const void* data, const uint size, const uint offset = 0);
123
125void BufferUpload(const CoreGraphics::CmdBufferId cmdBuf, const BufferId id, const void* data, const uint size, const uint offset);
127template<class TYPE> void BufferUpdate(const BufferId id, const TYPE& data, const uint offset = 0);
129template<class TYPE> void BufferUpdateArray(const BufferId id, const TYPE* data, const uint count, const uint offset = 0);
131template<class TYPE> void BufferUpdateArray(const BufferId id, const Util::Array<TYPE>& data, const uint offset = 0);
133template<class TYPE> void BufferUpdateArray(const BufferId id, const Util::FixedArray<TYPE>& data, const uint offset = 0);
135template<class TYPE> void BufferUpload(const CoreGraphics::CmdBufferId cmdBuf, const BufferId id, const TYPE* data, const uint count = 1, const uint offset = 0);
136
138void BufferFill(const CoreGraphics::CmdBufferId cmdBuf, const BufferId id, char pattern);
139
141void BufferFlush(const BufferId id, IndexT offset = 0, SizeT size = NEBULA_WHOLE_BUFFER_SIZE);
143void BufferInvalidate(const BufferId id, IndexT offset = 0, SizeT size = NEBULA_WHOLE_BUFFER_SIZE);
144
146void BufferSparseEvict(const BufferId id, IndexT pageIndex);
148void BufferSparseMakeResident(const BufferId id, IndexT pageIndex);
155
157CoreGraphics::DeviceAddress BufferGetDeviceAddress(const BufferId id);
158
160void BufferCopyWithStaging(const CoreGraphics::BufferId dest, const uint offset, const void* data, const uint size);
161
162//------------------------------------------------------------------------------
165template <class T>
166inline T*
168{
169 return reinterpret_cast<T*>(BufferMap(id));
170}
171
172//------------------------------------------------------------------------------
175template<class TYPE>
176inline void
177BufferUpdate(const BufferId id, const TYPE& data, const uint offset)
178{
179 BufferUpdate(id, (const void*)&data, sizeof(TYPE), offset);
180}
181
182//------------------------------------------------------------------------------
185template<class TYPE>
186inline void
187BufferUpdateArray(const BufferId id, const TYPE* data, const uint count, const uint offset)
188{
189 BufferUpdate(id, (const void*)data, sizeof(TYPE) * count, offset);
190}
191
192//------------------------------------------------------------------------------
195template<class TYPE>
196void BufferUpdateArray(const BufferId id, const Util::Array<TYPE>& data, const uint offset)
197{
198 BufferUpdate(id, (const void*)data.Begin(), data.ByteSize(), offset);
199}
200
201//------------------------------------------------------------------------------
204template<class TYPE>
205void BufferUpdateArray(const BufferId id, const Util::FixedArray<TYPE>& data, const uint offset)
206{
207 BufferUpdate(id, (const void*)data.Begin(), data.ByteSize(), offset);
208}
209
210//------------------------------------------------------------------------------
213template<class TYPE>
214inline void
215BufferUpload(const CoreGraphics::CmdBufferId cmdBuf, const BufferId id, const TYPE* data, const uint count, const uint offset)
216{
217 BufferUpload(cmdBuf, id, (const void*)data, sizeof(TYPE) * count, offset);
218}
219
220//------------------------------------------------------------------------------
225{
229 BufferSet(const BufferCreateInfo& createInfo);
231 BufferSet(BufferSet&& rhs);
233 void operator=(BufferSet&& rhs);
234
237
239 ~BufferSet();
240
242};
243
244//------------------------------------------------------------------------------
272
273
274
275} // namespace CoreGraphics
Nebula's dynamic array class.
Definition array.h:60
const SizeT ByteSize() const
return the byte size of the array.
Definition array.h:890
Iterator Begin() const
return iterator to beginning of array
Definition array.h:1245
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
Iterator Begin() const
get iterator to first element
Definition fixedarray.h:598
const SizeT ByteSize() const
get total byte size
Definition fixedarray.h:541
A StringAtom.
Definition stringatom.h:22
#define _DECL_ACQUIRE_RELEASE(ty)
Definition idallocator.h:88
#define ID_24_8_TYPE(x)
Definition id.h:132
Acceleration structures are used to enable ray tracing on the GPU by dividing the scene into a BVH.
Definition accelerationstructure.h:24
void BufferUpdate(const BufferId id, const void *data, const uint size, const uint offset=0)
update buffer data
Definition vkbuffer.cc:361
void BufferUpdateArray(const BufferId id, const TYPE *data, const uint count, const uint offset=0)
update buffer data as array
Definition buffer.h:187
void * BufferMap(const BufferId id)
map memory
Definition vkbuffer.cc:341
void BufferUpload(const CoreGraphics::CmdBufferId cmdBuf, const BufferId id, const void *data, const uint size, const uint offset)
update buffer directly on command buffer during frame update, asserts if size is too big
Definition vkbuffer.cc:377
void BufferSparseCommitChanges(const BufferId id)
Commit sparse bindings.
Definition vkbuffer.cc:523
BufferAccessMode
Definition buffer.h:27
@ DeviceLocal
Definition buffer.h:28
@ HostCached
Definition buffer.h:30
@ HostLocal
Definition buffer.h:29
@ DeviceAndHost
Definition buffer.h:31
const SizeT BufferGetElementSize(const BufferId id)
get buffer element size, this is the size of a single element, like a vertex or index,...
Definition vkbuffer.cc:314
void BufferSparseMakeResident(const BufferId id, IndexT pageIndex)
Make a page resident.
Definition vkbuffer.cc:465
void BufferCopyWithStaging(const CoreGraphics::BufferId dest, const uint offset, const void *data, const uint size)
Shortcut for creating a staging buffer and copy.
Definition vkbuffer.cc:564
const BufferId CreateBuffer(const BufferCreateInfo &info)
create buffer
Definition vkbuffer.cc:52
void BufferInvalidate(const BufferId id, IndexT offset=0, SizeT size=NEBULA_WHOLE_BUFFER_SIZE)
invalidate buffer CPU side, such that any GPU changes will be made visible
Definition vkbuffer.cc:421
const SizeT BufferGetByteSize(const BufferId id)
get buffer total byte size
Definition vkbuffer.cc:323
uint BufferUsageFlags
Definition buffer.h:53
SizeT BufferSparseGetPageSize(const BufferId id)
Get the buffer page size.
Definition vkbuffer.cc:510
const SizeT BufferGetSize(const BufferId id)
get buffer size, which is the number of elements
Definition vkbuffer.cc:305
BufferUsageFlag
Definition buffer.h:35
@ AccelerationStructureScratch
Definition buffer.h:48
@ ConstantTexelBuffer
Definition buffer.h:40
@ TransferBufferSource
Definition buffer.h:37
@ AccelerationStructureInput
Definition buffer.h:49
@ InvalidBufferUsage
Definition buffer.h:36
@ IndexBuffer
Definition buffer.h:44
@ IndirectBuffer
Definition buffer.h:45
@ ShaderTable
Definition buffer.h:51
@ VertexBuffer
Definition buffer.h:43
@ AccelerationStructureData
Definition buffer.h:47
@ ConstantBuffer
Definition buffer.h:39
@ TransferBufferDestination
Definition buffer.h:38
@ ReadWriteTexelBuffer
Definition buffer.h:42
@ ReadWriteBuffer
Definition buffer.h:41
@ AccelerationStructureInstances
Definition buffer.h:50
@ ShaderAddress
Definition buffer.h:46
const SizeT BufferGetUploadMaxSize()
get maximum size of upload for BufferUpload
Definition vkbuffer.cc:332
BufferQueueSupport
Definition buffer.h:56
@ ComputeQueueSupport
Definition buffer.h:59
@ AutomaticQueueSupport
Definition buffer.h:57
@ TransferQueueSupport
Definition buffer.h:60
@ GraphicsQueueSupport
Definition buffer.h:58
uint BufferQueueSupportFlags
Definition buffer.h:62
void BufferSparseEvict(const BufferId id, IndexT pageIndex)
Evict a page.
Definition vkbuffer.cc:432
void BufferUnmap(const BufferId id)
unmap memory
Definition vkbuffer.cc:352
void BufferFlush(const BufferId id, IndexT offset=0, SizeT size=NEBULA_WHOLE_BUFFER_SIZE)
flush any changes done to the buffer on the CPU side so they are visible on the GPU
Definition vkbuffer.cc:410
void DestroyBuffer(const BufferId id)
destroy buffer
Definition vkbuffer.cc:281
IndexT BufferSparseGetPageIndex(const BufferId id, SizeT offset)
Get the page index given an offset.
Definition vkbuffer.cc:495
CoreGraphics::DeviceAddress BufferGetDeviceAddress(const BufferId id)
Get buffer device address.
Definition vkbuffer.cc:548
const BufferUsageFlags BufferGetType(const BufferId id)
get type of buffer
Definition vkbuffer.cc:296
void BufferFill(const CoreGraphics::CmdBufferId cmdBuf, const BufferId id, char pattern)
fill buffer with data much like memset
Definition vkbuffer.cc:387
Compile time configuration options for the CoreGraphics subsystem.
#define NEBULA_WHOLE_BUFFER_SIZE
Definition config.h:16
Definition memory.h:41
Definition buffer.h:65
BufferAccessMode mode
Definition buffer.h:83
SizeT byteSize
Definition buffer.h:82
bool sparse
Definition buffer.h:86
BufferQueueSupportFlags queueSupport
Definition buffer.h:85
SizeT size
Definition buffer.h:80
SizeT elementSize
Definition buffer.h:81
BufferUsageFlags usageFlags
Definition buffer.h:84
const void * data
Definition buffer.h:87
Util::StringAtom name
Definition buffer.h:79
uint dataSize
Definition buffer.h:88
BufferCreateInfo()
Definition buffer.h:66
Definition buffer.h:23
Set of buffers which creates a buffer per each buffered frame.
Definition buffer.h:225
Util::FixedArray< CoreGraphics::BufferId > buffers
Definition buffer.h:241
~BufferSet()
Destructor.
Definition buffer.cc:63
const CoreGraphics::BufferId Buffer()
Get current buffer.
Definition buffer.cc:55
void operator=(BufferSet &&rhs)
Move assignment.
Definition buffer.cc:41
BufferSet()
Default constructor.
Definition buffer.h:227
Definition buffer.h:92
SizeT offset
Definition buffer.h:93
CoreGraphics::Alloc alloc
Definition buffer.h:94
Definition buffer.h:248
const CoreGraphics::BufferId DeviceBuffer()
Get device buffer.
Definition buffer.cc:116
void Flush(const CoreGraphics::CmdBufferId cmdBuf, SizeT numBytes)
Flush changes on staging buffer to device.
Definition buffer.cc:134
BufferSet hostBuffers
Definition buffer.h:270
~BufferWithStaging()
Destructor.
Definition buffer.cc:145
BufferWithStaging()
Default constructor.
Definition buffer.h:250
const CoreGraphics::BufferId HostBuffer()
Get staging buffer for this frame.
Definition buffer.cc:125
CoreGraphics::BufferId deviceBuffer
Definition buffer.h:269
void operator=(BufferWithStaging &&rhs)
Move assignment.
Definition buffer.cc:104
Definition commandbuffer.h:155
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31
int IndexT
Definition types.h:48