Nebula
Loading...
Searching...
No Matches
pinnedbuffer.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include "buffer.h"
11namespace CoreGraphics
12{
13
14template <typename STORAGE>
16{
20 PinnedBuffer(const BufferCreateInfo& createInfo);
24 void operator=(PinnedBuffer&& rhs);
25
27 void Write(const STORAGE& item, IndexT offset);
29 void Write(const STORAGE* items, SizeT num, IndexT offset);
30
32 void Flush(const CoreGraphics::CmdBufferId buf);
33
39};
40
41//------------------------------------------------------------------------------
44template<typename STORAGE> inline
46 : iterator(0)
47{
48 n_assert(createInfo.sparse == true);
49 BufferCreateInfo hostBufferInfo = createInfo;
50 hostBufferInfo.mode = CoreGraphics::HostLocal;
52
53 this->hostBuffers = BufferSet(hostBufferInfo);
54 BufferCreateInfo deviceBufferInfo = createInfo;
55 deviceBufferInfo.mode = CoreGraphics::DeviceLocal;
57 this->deviceBuffer = CoreGraphics::CreateBuffer(deviceBufferInfo);
58}
59
60//------------------------------------------------------------------------------
63template<typename STORAGE> inline
65{
66 this->hostBuffers = std::move(rhs.hostBuffers);
67 if (this->deviceBuffer != InvalidBufferId)
68 DestroyBuffer(this->deviceBuffer);
69 this->deviceBuffer = std::move(rhs.deviceBuffer);
70 this->iterator = rhs.iterator;
71 rhs.iterator = 0;
72}
73
74//------------------------------------------------------------------------------
77template<typename STORAGE> inline void
79{
80 this->hostBuffers = std::move(rhs.hostBuffers);
81 if (this->deviceBuffer != InvalidBufferId)
82 DestroyBuffer(this->deviceBuffer);
83 this->deviceBuffer = std::move(rhs.deviceBuffer);
84 this->iterator = rhs.iterator;
85 rhs.iterator = 0;
86}
87
88//------------------------------------------------------------------------------
91template<typename STORAGE> inline void
92PinnedBuffer<STORAGE>::Write(const STORAGE& item, IndexT offset)
93{
94 this->fromCopies.Append(BufferCopy{ .offset = this->iterator });
95 this->toCopies.Append(BufferCopy{ .offset = offset });
96 this->sizes.Append(sizeof(STORAGE));
97 CoreGraphics::BufferUpdate(this->hostBuffers.Buffer(), item, this->iterator);
98 this->iterator += sizeof(STORAGE);
99}
100
101//------------------------------------------------------------------------------
104template<typename STORAGE> inline void
105PinnedBuffer<STORAGE>::Write(const STORAGE* items, SizeT num, IndexT offset)
106{
107 this->fromCopies.Append(BufferCopy{ .offset = this->iterator });
108 this->toCopies.Append(BufferCopy{ .offset = offset });
109 uint byteSize = sizeof(STORAGE) * num;
110 this->sizes.Append(byteSize);
111 CoreGraphics::BufferUpdate(this->hostBuffers.Buffer(), items, byteSize, this->iterator);
112 this->iterator += byteSize;
113}
114
115//------------------------------------------------------------------------------
118template<typename STORAGE> inline void
120{
121 N_CMD_SCOPE(buf, NEBULA_MARKER_TRANSFER, "PinnedBuffer Update");
122 for (auto size : this->sizes)
123 {
124 CoreGraphics::CmdCopy(buf, this->hostBuffers.Buffer(), this->fromCopies, this->deviceBuffer, this->toCopies, size);
125 }
126 this->fromCopies.Clear();
127 this->toCopies.Clear();
128 this->sizes.Clear();
129 this->iterator = 0;
130}
131
132} // namespace CoreGraphics
Nebula's dynamic array class.
Definition array.h:60
#define N_CMD_SCOPE(x, y, z)
Definition commandbuffer.h:441
#define n_assert(exp)
Definition debug.h:50
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
@ DeviceLocal
Definition buffer.h:28
@ HostLocal
Definition buffer.h:29
void CmdCopy(const CmdBufferId id, const CoreGraphics::TextureId fromTexture, const Util::Array< CoreGraphics::TextureCopy, 4 > &from, const CoreGraphics::TextureId toTexture, const Util::Array< CoreGraphics::TextureCopy, 4 > &to)
Copy between textures.
Definition vkcommandbuffer.cc:1224
const BufferId CreateBuffer(const BufferCreateInfo &info)
create buffer
Definition vkbuffer.cc:52
@ TransferBufferSource
Definition buffer.h:37
@ TransferBufferDestination
Definition buffer.h:38
void DestroyBuffer(const BufferId id)
destroy buffer
Definition vkbuffer.cc:281
@ Write
Definition filewatcher.h:47
#define NEBULA_MARKER_TRANSFER
Definition config.h:338
Definition commandbuffer.h:78
Definition buffer.h:65
BufferAccessMode mode
Definition buffer.h:83
bool sparse
Definition buffer.h:86
BufferUsageFlags usageFlags
Definition buffer.h:84
Definition buffer.h:23
Set of buffers which creates a buffer per each buffered frame.
Definition buffer.h:225
Definition commandbuffer.h:155
Definition pinnedbuffer.h:16
void Flush(const CoreGraphics::CmdBufferId buf)
Copy from storage unto device buffer.
Definition pinnedbuffer.h:119
CoreGraphics::BufferId deviceBuffer
Definition pinnedbuffer.h:38
Util::Array< uint > sizes
Definition pinnedbuffer.h:36
void operator=(PinnedBuffer &&rhs)
Move assignment.
Definition pinnedbuffer.h:78
Util::Array< CoreGraphics::BufferCopy > fromCopies
Definition pinnedbuffer.h:35
BufferSet hostBuffers
Definition pinnedbuffer.h:37
PinnedBuffer()
Default constructor.
Definition pinnedbuffer.h:18
uint iterator
Definition pinnedbuffer.h:34
void Write(const STORAGE &item, IndexT offset)
Append a copy of an element to a region.
Definition pinnedbuffer.h:92
Util::Array< CoreGraphics::BufferCopy > toCopies
Definition pinnedbuffer.h:35
int SizeT
Definition types.h:49
unsigned int uint
Definition types.h:31
int IndexT
Definition types.h:48