2021-03-25 20:00:19 +08:00
|
|
|
|
#ifndef HGL_GRAPH_VULKAN_ARRAY_BUFFER_INCLUDE
|
2021-02-10 18:53:07 +08:00
|
|
|
|
#define HGL_GRAPH_VULKAN_ARRAY_BUFFER_INCLUDE
|
|
|
|
|
|
|
|
|
|
#include<hgl/graph/VKBuffer.h>
|
2021-03-25 20:00:19 +08:00
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
2021-04-26 20:36:56 +08:00
|
|
|
|
#include<hgl/graph/VKMemoryAllocator.h>
|
2021-04-07 21:17:24 +08:00
|
|
|
|
#include<hgl/type/Collection.h>
|
2021-02-10 18:53:07 +08:00
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
namespace graph
|
|
|
|
|
{
|
|
|
|
|
/**
|
2021-03-25 20:00:19 +08:00
|
|
|
|
* GPU数据阵列缓冲区<br>
|
|
|
|
|
* 它用于储存多份相同格式的数据,常用于多物件渲染,instance等
|
2021-02-10 18:53:07 +08:00
|
|
|
|
*/
|
2023-03-22 21:18:46 +08:00
|
|
|
|
class GPUArrayBuffer
|
2021-02-10 18:53:07 +08:00
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
|
2021-04-26 20:36:56 +08:00
|
|
|
|
GPUDevice *device;
|
|
|
|
|
VkBufferUsageFlags buffer_usage_flags;
|
2023-03-22 21:18:46 +08:00
|
|
|
|
|
|
|
|
|
uint item_length; ///<单个数据长度
|
|
|
|
|
|
2021-06-15 15:03:09 +08:00
|
|
|
|
VKMemoryAllocator *vk_ma;
|
|
|
|
|
|
|
|
|
|
uint32_t ubo_offset_alignment;
|
2021-02-10 18:53:07 +08:00
|
|
|
|
|
2021-05-08 18:14:44 +08:00
|
|
|
|
Collection *coll;
|
2021-02-10 18:53:07 +08:00
|
|
|
|
|
2021-04-26 20:36:56 +08:00
|
|
|
|
public:
|
2021-04-06 19:13:53 +08:00
|
|
|
|
|
2023-03-22 21:18:46 +08:00
|
|
|
|
GPUArrayBuffer(GPUDevice *dev,VkBufferUsageFlags flags,const uint il)
|
2021-04-07 21:17:24 +08:00
|
|
|
|
{
|
2021-04-26 20:36:56 +08:00
|
|
|
|
device=dev;
|
|
|
|
|
buffer_usage_flags=flags;
|
2023-03-22 21:18:46 +08:00
|
|
|
|
item_length=il;
|
2021-04-26 20:36:56 +08:00
|
|
|
|
|
2021-05-08 18:14:44 +08:00
|
|
|
|
{
|
2021-06-22 21:33:47 +08:00
|
|
|
|
ubo_offset_alignment=device->GetUBOAlign();
|
2021-06-15 15:03:09 +08:00
|
|
|
|
|
2023-03-22 21:18:46 +08:00
|
|
|
|
const uint32_t unit_size=hgl_align<uint32_t>(item_length,ubo_offset_alignment);
|
2021-05-08 19:02:08 +08:00
|
|
|
|
|
2021-06-22 21:33:47 +08:00
|
|
|
|
vk_ma=new VKMemoryAllocator(device,buffer_usage_flags,unit_size); // construct function is going to set AllocUnitSize by minUniformOffsetAlignment
|
|
|
|
|
MemoryBlock *mb=new MemoryBlock(vk_ma);
|
|
|
|
|
|
2021-05-08 18:14:44 +08:00
|
|
|
|
coll=new Collection(unit_size,mb);
|
|
|
|
|
}
|
2021-04-07 21:17:24 +08:00
|
|
|
|
}
|
2021-04-06 19:13:53 +08:00
|
|
|
|
|
2021-04-26 20:36:56 +08:00
|
|
|
|
virtual ~GPUArrayBuffer()
|
|
|
|
|
{
|
|
|
|
|
delete coll;
|
|
|
|
|
}
|
2021-04-06 19:13:53 +08:00
|
|
|
|
|
2021-06-15 15:03:09 +08:00
|
|
|
|
const uint32_t GetOffsetAlignment()const
|
|
|
|
|
{
|
|
|
|
|
return ubo_offset_alignment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uint32_t GetUnitSize()const
|
|
|
|
|
{
|
|
|
|
|
return coll->GetUnitBytes();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 17:52:35 +08:00
|
|
|
|
DeviceBuffer *GetBuffer()
|
2021-06-15 15:03:09 +08:00
|
|
|
|
{
|
|
|
|
|
return vk_ma->GetBuffer();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 20:36:56 +08:00
|
|
|
|
uint32 Alloc(const uint32 max_count) ///<预分配空间
|
|
|
|
|
{
|
|
|
|
|
if(!coll->Alloc(max_count))
|
|
|
|
|
return(0);
|
2021-02-10 18:53:07 +08:00
|
|
|
|
|
2021-04-26 20:36:56 +08:00
|
|
|
|
return coll->GetAllocCount();
|
|
|
|
|
}
|
2021-02-10 18:53:07 +08:00
|
|
|
|
|
2021-04-26 20:36:56 +08:00
|
|
|
|
void Clear()
|
|
|
|
|
{
|
|
|
|
|
coll->Clear();
|
|
|
|
|
}
|
2021-03-25 20:00:19 +08:00
|
|
|
|
|
2023-03-22 21:18:46 +08:00
|
|
|
|
void *Map(const uint32 start,const uint32 count)
|
2021-04-26 20:36:56 +08:00
|
|
|
|
{
|
2023-03-22 21:18:46 +08:00
|
|
|
|
return coll->Map(start,count);
|
2021-04-26 20:36:56 +08:00
|
|
|
|
}
|
2021-06-22 21:33:47 +08:00
|
|
|
|
|
|
|
|
|
void Flush(const uint32 count)
|
|
|
|
|
{
|
|
|
|
|
vk_ma->Flush(count*GetUnitSize());
|
|
|
|
|
}
|
2021-02-10 18:53:07 +08:00
|
|
|
|
};//class GPUArrayBuffer
|
|
|
|
|
}//namespace graph
|
|
|
|
|
}//namespace hgl
|
|
|
|
|
#endif//HGL_GRAPH_VULKAN_ARRAY_BUFFER_INCLUDE
|