2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/VKMemory.h>
|
|
|
|
|
#include<hgl/graph/VKPhysicalDevice.h>
|
2019-06-26 15:18:31 +08:00
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-10-21 12:39:22 +08:00
|
|
|
|
GPUMemory *GPUDevice::CreateMemory(const VkMemoryRequirements &req,uint32_t properties)
|
2019-06-26 15:45:20 +08:00
|
|
|
|
{
|
|
|
|
|
uint32_t index;
|
|
|
|
|
|
2019-11-25 22:05:05 +08:00
|
|
|
|
if(!attr->physical_device->CheckMemoryType(req.memoryTypeBits,properties,&index))
|
2020-01-23 21:00:35 +08:00
|
|
|
|
return(nullptr);
|
2019-06-26 15:45:20 +08:00
|
|
|
|
|
|
|
|
|
VkMemoryAllocateInfo alloc_info;
|
|
|
|
|
|
|
|
|
|
alloc_info.sType =VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
|
|
|
alloc_info.pNext =nullptr;
|
|
|
|
|
alloc_info.memoryTypeIndex =index;
|
|
|
|
|
alloc_info.allocationSize =req.size;
|
|
|
|
|
|
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
|
|
2019-11-25 22:05:05 +08:00
|
|
|
|
if(vkAllocateMemory(attr->device,&alloc_info,nullptr,&memory)!=VK_SUCCESS)
|
2019-06-26 15:45:20 +08:00
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
return(new GPUMemory(attr->device,memory,req,index,properties));
|
2019-06-26 15:45:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 13:58:48 +08:00
|
|
|
|
GPUMemory::GPUMemory(VkDevice dev,VkDeviceMemory dm,const VkMemoryRequirements &mr,const uint32 i,const uint32_t p)
|
|
|
|
|
{
|
|
|
|
|
device=dev;
|
|
|
|
|
memory=dm;
|
|
|
|
|
req=mr;
|
|
|
|
|
index=i;
|
|
|
|
|
properties=p;
|
|
|
|
|
|
|
|
|
|
memory_range.sType =VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
|
|
|
|
memory_range.pNext =nullptr;
|
|
|
|
|
memory_range.memory =memory;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
GPUMemory::~GPUMemory()
|
2019-06-26 15:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
vkFreeMemory(device,memory,nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
void *GPUMemory::Map()
|
2019-06-26 15:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
|
|
if(vkMapMemory(device,memory,0,req.size,0,&result)==VK_SUCCESS)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
void *GPUMemory::Map(const VkDeviceSize offset,const VkDeviceSize size)
|
2019-06-26 15:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
if(offset<0||offset+size>=req.size)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
void *result;
|
|
|
|
|
|
|
|
|
|
if(vkMapMemory(device,memory,0,size,0,&result)==VK_SUCCESS)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
void GPUMemory::Unmap()
|
2019-06-26 15:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
vkUnmapMemory(device,memory);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 13:58:48 +08:00
|
|
|
|
void GPUMemory::Flush(VkDeviceSize offset,VkDeviceSize size)
|
|
|
|
|
{
|
|
|
|
|
memory_range.offset =offset;
|
|
|
|
|
memory_range.size =size;
|
|
|
|
|
|
|
|
|
|
vkFlushMappedMemoryRanges(device,1,&memory_range);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
bool GPUMemory::Write(const void *ptr,VkDeviceSize start,VkDeviceSize size)
|
2019-06-26 17:08:01 +08:00
|
|
|
|
{
|
|
|
|
|
if(!ptr)return(false);
|
|
|
|
|
|
|
|
|
|
void *dst;
|
|
|
|
|
|
|
|
|
|
if(vkMapMemory(device,memory,start,size,0,&dst)!=VK_SUCCESS)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
memcpy(dst,ptr,size);
|
|
|
|
|
vkUnmapMemory(device,memory);
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
bool GPUMemory::BindBuffer(VkBuffer buffer)
|
2019-06-26 17:08:01 +08:00
|
|
|
|
{
|
|
|
|
|
if(!buffer)return(false);
|
|
|
|
|
|
|
|
|
|
return(vkBindBufferMemory(device,buffer,memory,0)==VK_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:09:15 +08:00
|
|
|
|
bool GPUMemory::BindImage(VkImage image)
|
2019-06-26 15:18:31 +08:00
|
|
|
|
{
|
|
|
|
|
if(!image)return(false);
|
|
|
|
|
|
|
|
|
|
return(vkBindImageMemory(device,image,memory,0)==VK_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|