add VKMemoryAllocator
This commit is contained in:
parent
4af5735c25
commit
e1aaaa0b31
2
CMCore
2
CMCore
@ -1 +1 @@
|
||||
Subproject commit f52b7ac971305bc1e3e987a7de919e891fdc4d6e
|
||||
Subproject commit 91d408d10cb6f934522f6c544ae05694315b90fe
|
@ -1 +1 @@
|
||||
Subproject commit ea2aaf3126ad6d64a4999c9b493c7dd560f79bc0
|
||||
Subproject commit 2d5c094bbfb69841512007e1eb88c0c6bc1b5c28
|
@ -182,7 +182,7 @@ private:
|
||||
VK_SAMPLER_ADDRESS_MODE_REPEAT,
|
||||
0.0f,
|
||||
VK_TRUE,
|
||||
device->GetGPUPhysicalDevice()->GetMaxSamplerAnisotropy(),
|
||||
device->GetPhysicalDevice()->GetMaxSamplerAnisotropy(),
|
||||
false,
|
||||
VK_COMPARE_OP_NEVER,
|
||||
0.0f,
|
||||
|
@ -106,7 +106,7 @@ private:
|
||||
VK_SAMPLER_ADDRESS_MODE_REPEAT,
|
||||
0.0f,
|
||||
VK_TRUE,
|
||||
device->GetGPUPhysicalDevice()->GetMaxSamplerAnisotropy(),
|
||||
device->GetPhysicalDevice()->GetMaxSamplerAnisotropy(),
|
||||
false,
|
||||
VK_COMPARE_OP_NEVER,
|
||||
0.0f,
|
||||
|
@ -1,38 +1,39 @@
|
||||
#ifndef HGL_GRAPH_VULKAN_ARRAY_BUFFER_INCLUDE
|
||||
#ifndef HGL_GRAPH_VULKAN_ARRAY_BUFFER_INCLUDE
|
||||
#define HGL_GRAPH_VULKAN_ARRAY_BUFFER_INCLUDE
|
||||
|
||||
#include<hgl/graph/VKBuffer.h>
|
||||
#include<hgl/graph/VKDevice.h>
|
||||
namespace hgl
|
||||
{
|
||||
namespace graph
|
||||
{
|
||||
/**
|
||||
* GPU数据阵列缓冲区<br>
|
||||
* 它用于储存多份相同格式的数据,常用于多物件渲染,instance等
|
||||
* GPU数据阵列缓冲区<br>
|
||||
* 它用于储存多份相同格式的数据,常用于多物件渲染,instance等
|
||||
*/
|
||||
class GPUArrayBuffer
|
||||
{
|
||||
protected:
|
||||
|
||||
uint32_t item_size; ///<单个数据长度
|
||||
uint32_t alloc_count; ///<总计分配的数据个数
|
||||
uint32_t count; ///<实际使用的数据个数
|
||||
GPUDevice * device;
|
||||
|
||||
GPUBuffer * buf_gpu; ///<实际数据GPU缓冲区
|
||||
uint32_t item_size; ///<单个数据长度
|
||||
uint32_t alloc_count; ///<总计分配的数据个数
|
||||
uint32_t count; ///<实际使用的数据个数
|
||||
uint32_t total_bytes; ///<总字节数
|
||||
|
||||
GPUBuffer * buf_gpu; ///<实际数据GPU缓冲区
|
||||
uint8 * buf_cpu;
|
||||
uint32_t * offset; ///<数据偏移地址
|
||||
uint32_t * offset; ///<数据偏移地址
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* 本类构造函数
|
||||
* @param s 单个数据长度
|
||||
* @param c 数据个数
|
||||
*/
|
||||
GPUArrayBuffer(const uint32_t s=0,const uint32_t c=0);
|
||||
GPUArrayBuffer(GPUDevice *,const uint32_t s=0,const uint32_t c=0);
|
||||
virtual ~GPUArrayBuffer();
|
||||
|
||||
void Clear(); ///<清空缓冲区
|
||||
void Clear(); ///<清空缓冲区
|
||||
|
||||
bool Init(const uint32_t); ///<初始化并分配空间
|
||||
};//class GPUArrayBuffer
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef HGL_GRAPH_RENDER_SURFACE_INCLUDE
|
||||
#define HGL_GRAPH_RENDER_SURFACE_INCLUDE
|
||||
#ifndef HGL_GRAPH_VULKAN_DEVICE_INCLUDE
|
||||
#define HGL_GRAPH_VULKAN_DEVICE_INCLUDE
|
||||
|
||||
#include<hgl/type/List.h>
|
||||
#include<hgl/type/String.h>
|
||||
@ -30,7 +30,7 @@ class GPUDevice
|
||||
{
|
||||
GPUDeviceAttribute *attr;
|
||||
|
||||
GPUQueue *textureSQ;
|
||||
GPUQueue *texture_queue;
|
||||
TextureCmdBuffer *texture_cmd_buf;
|
||||
|
||||
Swapchain *swapchain;
|
||||
@ -60,7 +60,7 @@ public:
|
||||
|
||||
VkSurfaceKHR GetSurface () {return attr->surface;}
|
||||
VkDevice GetDevice ()const {return attr->device;}
|
||||
const GPUPhysicalDevice * GetGPUPhysicalDevice()const {return attr->physical_device;}
|
||||
const GPUPhysicalDevice * GetPhysicalDevice ()const {return attr->physical_device;}
|
||||
|
||||
VkDescriptorPool GetDescriptorPool () {return attr->desc_pool;}
|
||||
VkPipelineCache GetPipelineCache () {return attr->pipeline_cache;}
|
||||
@ -253,4 +253,4 @@ bool CreateAttachmentDescription( List<VkAttachmentDescription> &color_output_de
|
||||
|
||||
GPUDevice *CreateRenderDevice(VulkanInstance *inst,Window *win,const GPUPhysicalDevice *physical_device=nullptr);
|
||||
VK_NAMESPACE_END
|
||||
#endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE
|
||||
#endif//HGL_GRAPH_VULKAN_DEVICE_INCLUDE
|
||||
|
34
inc/hgl/graph/VKMemoryAllocator.h
Normal file
34
inc/hgl/graph/VKMemoryAllocator.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef HGL_GRAPH_VULKAN_MEMORY_ALLOCATOR_INCLUDE
|
||||
#define HGL_GRAPH_VULKAN_MEMORY_ALLOCATOR_INCLUDE
|
||||
|
||||
#include<hgl/graph/VK.h>
|
||||
#include<hgl/type/MemoryAllocator.h>
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
class VKMemoryAllocator:public AbstractMemoryAllocator
|
||||
{
|
||||
GPUDevice *device;
|
||||
|
||||
uint32_t buffer_usage_flag_bits;
|
||||
|
||||
GPUBuffer *gpu_buffer;
|
||||
|
||||
protected:
|
||||
|
||||
bool Alloc() override;
|
||||
|
||||
public:
|
||||
|
||||
const bool CanRealloc ()const override{return false;}
|
||||
|
||||
const uint32_t GetBufferUsageFlagBits ()const{return buffer_usage_flag_bits;}
|
||||
|
||||
public:
|
||||
|
||||
VKMemoryAllocator(GPUDevice *,const uint32_t);
|
||||
~VKMemoryAllocator();
|
||||
|
||||
void Free() override {/* DON'T RUN ANY OPERATION.*/}
|
||||
};//class VKMemoryAllocator:public AbstractMemoryAllocator
|
||||
VK_NAMESPACE_END
|
||||
#endif//HGL_GRAPH_VULKAN_MEMORY_ALLOCATOR_INCLUDE
|
@ -37,7 +37,9 @@ public:
|
||||
|
||||
public:
|
||||
|
||||
const uint32_t GetConstantSize()const{return properties.limits.maxPushConstantsSize;}
|
||||
const uint32_t GetUBORange ()const{return properties.limits.maxUniformBufferRange;}
|
||||
const uint32_t GetSSBORange ()const{return properties.limits.maxStorageBufferRange;}
|
||||
const uint32_t GetConstantSize ()const{return properties.limits.maxPushConstantsSize;}
|
||||
|
||||
public:
|
||||
|
||||
|
@ -1,16 +1,19 @@
|
||||
#include<hgl/graph/VKArrayBuffer.h>
|
||||
#include<hgl/graph/VKArrayBuffer.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
namespace graph
|
||||
{
|
||||
/**
|
||||
* 本类构造函数
|
||||
* @param s 单个数据长度
|
||||
* @param c 数据个数
|
||||
* 本类构造函数
|
||||
* @param d 设备指针
|
||||
* @param s 单个数据长度
|
||||
* @param c 数据个数
|
||||
*/
|
||||
GPUArrayBuffer::GPUArrayBuffer(const uint32_t s,const uint32_t c)
|
||||
GPUArrayBuffer::GPUArrayBuffer(GPUDevice *d,const uint32_t s,const uint32_t c)
|
||||
{
|
||||
device=d;
|
||||
|
||||
item_size=s;
|
||||
count=c;
|
||||
alloc_count=power_to_2(c);
|
||||
@ -22,6 +25,7 @@ namespace hgl
|
||||
|
||||
GPUArrayBuffer::~GPUArrayBuffer()
|
||||
{
|
||||
SAFE_CLEAR_ARRAY(offset);
|
||||
SAFE_CLEAR(buf_gpu);
|
||||
}
|
||||
|
||||
@ -30,6 +34,27 @@ namespace hgl
|
||||
count=0;
|
||||
}
|
||||
|
||||
bool GPUArrayBuffer::Init(const uint32_t c)
|
||||
{
|
||||
if(c<=0)return(false);
|
||||
|
||||
if(!buf_gpu)
|
||||
{
|
||||
count=c;
|
||||
alloc_count=power_to_2(count);
|
||||
|
||||
total_bytes=item_size*alloc_count;
|
||||
|
||||
if(total_bytes<=0)return(false);
|
||||
|
||||
buf_gpu=device->CreateUBO(total_bytes);
|
||||
buf_cpu=(uint8 *)(buf_gpu->Map());
|
||||
|
||||
offset=new uint32_t[alloc_count];
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
|
@ -15,7 +15,7 @@ GPUDevice::GPUDevice(GPUDeviceAttribute *da)
|
||||
{
|
||||
attr=da;
|
||||
|
||||
textureSQ=nullptr;
|
||||
texture_queue=nullptr;
|
||||
texture_cmd_buf=nullptr;
|
||||
|
||||
swapchain=nullptr;
|
||||
@ -28,7 +28,7 @@ GPUDevice::~GPUDevice()
|
||||
SAFE_CLEAR(swapchainRT);
|
||||
SAFE_CLEAR(swapchain);
|
||||
|
||||
SAFE_CLEAR(textureSQ);
|
||||
SAFE_CLEAR(texture_queue);
|
||||
SAFE_CLEAR(texture_cmd_buf);
|
||||
|
||||
delete attr;
|
||||
@ -39,7 +39,7 @@ bool GPUDevice::Resize(const VkExtent2D &extent)
|
||||
SAFE_CLEAR(swapchainRT);
|
||||
SAFE_CLEAR(swapchain);
|
||||
|
||||
SAFE_CLEAR(textureSQ);
|
||||
SAFE_CLEAR(texture_queue);
|
||||
SAFE_CLEAR(texture_cmd_buf);
|
||||
|
||||
attr->Refresh();
|
||||
@ -48,7 +48,7 @@ bool GPUDevice::Resize(const VkExtent2D &extent)
|
||||
return(false);
|
||||
|
||||
texture_cmd_buf=CreateTextureCommandBuffer();
|
||||
textureSQ=new GPUQueue(this,attr->graphics_queue,1);
|
||||
texture_queue=new GPUQueue(this,attr->graphics_queue,1);
|
||||
|
||||
swapchainRT=new SwapchainRenderTarget(this,swapchain);
|
||||
|
||||
|
@ -426,9 +426,9 @@ bool GPUDevice::SubmitTexture(const VkCommandBuffer *cmd_bufs,const uint32_t cou
|
||||
if(!cmd_bufs||count<=0)
|
||||
return(false);
|
||||
|
||||
textureSQ->Submit(cmd_bufs,count,nullptr,nullptr);
|
||||
// textureSQ->WaitQueue();
|
||||
textureSQ->WaitFence();
|
||||
texture_queue->Submit(cmd_bufs,count,nullptr,nullptr);
|
||||
// texture_queue->WaitQueue();
|
||||
texture_queue->WaitFence();
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
40
src/SceneGraph/Vulkan/VKMemoryAllocator.cpp
Normal file
40
src/SceneGraph/Vulkan/VKMemoryAllocator.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
#include<hgl/graph/VKMemoryAllocator.h>
|
||||
#include<hgl/graph/VKDevice.h>
|
||||
#include<hgl/graph/VKPhysicalDevice.h>
|
||||
#include<hgl/graph/VKBuffer.h>
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
VKMemoryAllocator::VKMemoryAllocator(GPUDevice *d,const uint32_t flags)
|
||||
{
|
||||
device=d;
|
||||
buffer_usage_flag_bits=flags;
|
||||
gpu_buffer=nullptr;
|
||||
|
||||
const GPUPhysicalDevice *pd=device->GetPhysicalDevice();
|
||||
|
||||
SetAllocUnitSize(pd->GetConstantSize()); //据说push constant容量就是GPU的最小访问单位
|
||||
}
|
||||
|
||||
VKMemoryAllocator::~VKMemoryAllocator()
|
||||
{
|
||||
SAFE_CLEAR(gpu_buffer);
|
||||
}
|
||||
|
||||
bool VKMemoryAllocator::Alloc()
|
||||
{
|
||||
if(gpu_buffer)
|
||||
delete gpu_buffer;
|
||||
|
||||
gpu_buffer=device->CreateBuffer(buffer_usage_flag_bits,alloc_size);
|
||||
|
||||
if(!gpu_buffer)
|
||||
{
|
||||
memory_block=nullptr;
|
||||
return(false);
|
||||
}
|
||||
|
||||
memory_block=gpu_buffer->Map();
|
||||
|
||||
return(true);
|
||||
}
|
||||
VK_NAMESPACE_END
|
Loading…
x
Reference in New Issue
Block a user