8 #include "fillpolicy.h"
9 #include "formatutils.h"
10 #include "renderutils.h"
11 #include "bufferdata.h"
14 #include <nap/resourceptr.h>
15 #include <glm/glm.hpp>
16 #include <nap/logger.h>
17 #include <vulkan/vulkan_core.h>
18 #include <utility/dllexport.h>
19 #include <nap/signalslot.h>
21 #include <utility/errorstate.h>
92 virtual VkBuffer getBuffer()
const;
97 virtual const BufferData& getBufferData()
const;
107 virtual uint getCount()
const = 0;
112 virtual size_t getSize()
const = 0;
117 virtual uint32 getElementSize()
const = 0;
129 void ensureUsage(VkBufferUsageFlags usage) { mUsageFlags |= usage; }
135 virtual bool isInitialized()
const = 0;
141 void asyncGetData(std::function<
void(
const void*,
size_t)> copyFunction);
167 bool setDataInternal(
const void* data,
size_t size,
size_t reservedSize,
utility::ErrorState& errorState);
179 int mCurrentRenderBufferIndex = 0;
180 int mCurrentStagingBufferIndex = 0;
184 using BufferReadCallback = std::function<void(
void* data,
size_t sizeInBytes)>;
187 bool setDataInternalStatic(
const void* data,
size_t size,
utility::ErrorState& errorState);
190 bool setDataInternalDynamic(
const void* data,
size_t size,
size_t reservedSize,
utility::ErrorState& errorState);
194 void upload(VkCommandBuffer commandBuffer);
197 void download(VkCommandBuffer commandBuffer);
200 void clear(VkCommandBuffer commandBuffer);
203 void notifyDownloadReady(
int frameIndex);
206 void clearDownloads();
208 std::vector<BufferReadCallback> mReadCallbacks;
209 VkBufferUsageFlags mUsageFlags = 0;
232 GPUBuffer(core), mFormat(format), mElementSize(elementSize)
242 GPUBuffer(core, usage), mFormat(format), mElementSize(elementSize)
258 virtual size_t getSize()
const override {
return mCount * mElementSize; };
280 virtual bool setData(
const void* data,
size_t elementCount,
size_t reservedElementCount,
utility::ErrorState& errorState);
285 VkFormat mFormat = VK_FORMAT_UNDEFINED;
359 bool mStorage =
true;
360 bool mInitialized =
false;
499 VkBufferUsageFlags req_usage = mStorage ? VK_BUFFER_USAGE_STORAGE_BUFFER_BIT : 0;
500 this->ensureUsage(req_usage);
507 "Cannot allocate a non-DynamicWrite buffer with zero elements."))
511 uint32 buffer_size = mCount *
sizeof(T);
514 if (!allocateInternal(buffer_size, errorState))
518 if (mFillPolicy !=
nullptr)
523 auto staging_buffer = std::make_unique<T[]>(mCount);
524 mFillPolicy->fill(mCount, staging_buffer.get());
527 if (!setDataInternal(staging_buffer.get(), buffer_size, buffer_size, errorState))
533 nap::Logger::warn(utility::stringFormat(
"%s: The configured fill policy was ignored as the buffer usage is DynamicRead", mID.c_str()).c_str());
550 if (!setDataInternal(data.data(), data.size() *
sizeof(T), data.capacity() *
sizeof(T), errorState))
554 mCount = data.size();
561 this->ensureUsage(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
unsigned int uint
Definition: numeric.h:23
virtual bool init(utility::ErrorState &errorState) override
Definition: gpubuffer.h:496
Definition: gpubuffer.h:441
nap::Signal bufferChanged
Definition: gpubuffer.h:146
IndexBuffer(Core &core)
Definition: gpubuffer.h:446
TypedGPUBufferNumeric(Core &core)
Definition: gpubuffer.h:316
VertexBuffer(Core &core)
Definition: gpubuffer.h:395
virtual uint getCount() const override
Definition: gpubuffer.h:248
@ Static
Buffer data is uploaded only once from the CPU to the GPU.
@ DynamicRead
Buffer data is uploaded only once from the CPU to the GPU, and frequently read from GPU to CPU.
bool check(bool successCondition, T &&errorMessage)
Definition: errorstate.h:36
VertexBuffer(Core &core, EMemoryUsage usage, bool storage)
Definition: gpubuffer.h:407
Definition: gpubuffer.h:222
bool mClear
Property: 'Clear' If no fill policy is set, performs an initial clear-to-zero transfer operation on t...
Definition: gpubuffer.h:355
Definition: objectptr.h:154
EMemoryUsage
Definition: gpubuffer.h:41
NAPAPI VkFormat getVulkanFormat(nap::rtti::TypeInfo type)
std::vector< BufferData > mRenderBuffers
Render accessible buffers.
Definition: gpubuffer.h:175
Definition: errorstate.h:19
IndexBuffer(Core &core, EMemoryUsage usage, bool storage)
Definition: gpubuffer.h:456
TypedGPUBufferNumeric(Core &core, EMemoryUsage usage, bool storage)
Definition: gpubuffer.h:328
virtual bool init(utility::ErrorState &errorState) override
uint32_t uint32
Definition: numeric.h:20
bool setData(const std::vector< T > &data, utility::ErrorState &errorState)
Definition: gpubuffer.h:548
virtual VkFormat getFormat() const
Definition: gpubuffer.h:263
virtual bool isInitialized() const override
Definition: gpubuffer.h:353
virtual void setCount(uint32 count)
Definition: gpubuffer.h:269
Definition: signalslot.h:28
std::vector< int > mDownloadStagingBufferIndices
Staging buffer indices associated with a frameindex.
Definition: gpubuffer.h:181
virtual bool init(utility::ErrorState &errorState) override
Definition: gpubuffer.h:559
Definition: renderservice.h:275
std::vector< BufferData > mStagingBuffers
Staging buffers, used when uploading or downloading data.
Definition: gpubuffer.h:176
@ DynamicWrite
Buffer data is updated more than once from the CPU to the GPU.
void ensureUsage(VkBufferUsageFlags usage)
Definition: gpubuffer.h:129
Definition: bufferdata.h:22
virtual uint32 getElementSize() const override
Definition: gpubuffer.h:253
Definition: templateapp.h:17
Definition: gpubuffer.h:64
Definition: gpubuffer.h:308
virtual VkBufferUsageFlags getBufferUsageFlags() const
Definition: gpubuffer.h:122
Definition: resource.h:19
Definition: gpubuffer.h:387
virtual size_t getSize() const override
Definition: gpubuffer.h:258
GPUBufferNumeric(Core &core, VkFormat format, uint32 elementSize, EMemoryUsage usage)
Definition: gpubuffer.h:241
ResourcePtr< FillPolicy< T > > mFillPolicy
Property: 'FillPolicy' Optional fill policy to fill the buffer with on initialization.
Definition: gpubuffer.h:356
GPUBufferNumeric(Core &core, VkFormat format, uint32 elementSize)
Definition: gpubuffer.h:231