// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #pragma once #include "GPUBuffer.h" /// /// Dynamic GPU buffer that allows to update and use GPU data (index/vertex/other) during single frame (supports dynamic resizing) /// class FLAXENGINE_API DynamicBuffer : public NonCopyable { protected: GPUBuffer* _buffer; String _name; uint32 _stride; public: /// /// Init /// /// Initial capacity of the buffer (in bytes) /// Stride in bytes /// Buffer name DynamicBuffer(uint32 initialCapacity, uint32 stride, const String& name); /// /// Destructor /// virtual ~DynamicBuffer(); public: /// /// The data container (raw bytes storage). /// Array Data; /// /// Gets buffer (may be null since it's using 'late init' feature) /// /// Buffer FORCE_INLINE GPUBuffer* GetBuffer() const { return _buffer; } public: /// /// Clear data (begin for writing) /// FORCE_INLINE void Clear() { Data.Clear(); } /// /// Write bytes to the buffer /// /// Data to write template FORCE_INLINE void Write(const T& data) { Data.Add((byte*)&data, sizeof(T)); } /// /// Write bytes to the buffer /// /// Pointer to data to write /// Amount of data to write (in bytes) FORCE_INLINE void Write(void* bytes, int32 size) { Data.Add((byte*)bytes, size); } /// /// Unlock buffer and flush data with a buffer (it will be ready for an immediate draw). /// void Flush(); /// /// Unlock buffer and flush data with a buffer (it will be ready for a during next frame draw). /// /// The GPU command list context to use for data uploading. void Flush(class GPUContext* context); /// /// Disposes the buffer resource and clears the used memory. /// void Dispose(); protected: virtual void InitDesc(GPUBufferDescription& desc, int32 numElements) = 0; }; /// /// Dynamic vertex buffer that allows to render any vertices during single frame (supports dynamic resizing) /// class FLAXENGINE_API DynamicVertexBuffer : public DynamicBuffer { public: /// /// Init /// /// Initial capacity of the buffer (in bytes) /// Stride in bytes /// Buffer name DynamicVertexBuffer(uint32 initialCapacity, uint32 stride, const String& name = String::Empty) : DynamicBuffer(initialCapacity, stride, name) { } protected: // [DynamicBuffer] void InitDesc(GPUBufferDescription& desc, int32 numElements) override { desc = GPUBufferDescription::Vertex(_stride, numElements, GPUResourceUsage::Dynamic); } }; /// /// Dynamic index buffer that allows to render any indices during single frame (supports dynamic resizing) /// class FLAXENGINE_API DynamicIndexBuffer : public DynamicBuffer { public: /// /// Init /// /// Initial capacity of the buffer (in bytes) /// Stride in bytes /// Buffer name DynamicIndexBuffer(uint32 initialCapacity, uint32 stride, const String& name = String::Empty) : DynamicBuffer(initialCapacity, stride, name) { } protected: // [DynamicBuffer] void InitDesc(GPUBufferDescription& desc, int32 numElements) override { desc = GPUBufferDescription::Index(_stride, numElements, GPUResourceUsage::Dynamic); } };