2020-10-21 11:43:18 +08:00
# include < hgl / graph / VKDevice . h >
# include<hgl / graph / VKBuffer.h>
2019-04-27 21:49:22 +08:00
VK_NAMESPACE_BEGIN
2020-10-21 12:09:15 +08:00
bool RenderDevice : : CreateBuffer ( BufferData * buf , VkBufferUsageFlags buf_usage , VkDeviceSize size , const void * data , SharingMode sharing_mode )
2019-04-27 21:49:22 +08:00
{
2020-09-27 20:58:25 +08:00
BufferCreateInfo buf_info ;
2020-09-02 18:36:24 +08:00
buf_info . usage = buf_usage ;
buf_info . size = size ;
buf_info . queueFamilyIndexCount = 0 ;
buf_info . pQueueFamilyIndices = nullptr ;
buf_info . sharingMode = VkSharingMode ( sharing_mode ) ;
2019-11-25 22:05:05 +08:00
if ( vkCreateBuffer ( attr - > device , & buf_info , nullptr , & buf - > buffer ) ! = VK_SUCCESS )
return ( false ) ;
2019-04-27 21:49:22 +08:00
2019-11-25 22:05:05 +08:00
VkMemoryRequirements mem_reqs ;
2019-04-27 21:49:22 +08:00
2019-11-25 22:05:05 +08:00
vkGetBufferMemoryRequirements ( attr - > device , buf - > buffer , & mem_reqs ) ;
2019-04-27 21:49:22 +08:00
2020-10-21 12:09:15 +08:00
GPUMemory * dm = CreateMemory ( mem_reqs , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ) ;
2019-04-27 21:49:22 +08:00
2019-11-25 22:05:05 +08:00
if ( dm & & dm - > BindBuffer ( buf - > buffer ) )
{
buf - > info . buffer = buf - > buffer ;
buf - > info . offset = 0 ;
buf - > info . range = size ;
2019-06-26 18:38:35 +08:00
2019-11-25 22:05:05 +08:00
buf - > memory = dm ;
2019-04-27 21:49:22 +08:00
2019-11-25 22:05:05 +08:00
if ( ! data )
2019-06-26 17:08:01 +08:00
return ( true ) ;
2019-11-25 22:05:05 +08:00
dm - > Write ( data , 0 , size ) ;
return ( true ) ;
2019-04-27 21:49:22 +08:00
}
2019-11-25 22:05:05 +08:00
delete dm ;
vkDestroyBuffer ( attr - > device , buf - > buffer , nullptr ) ;
return ( false ) ;
}
2019-04-27 21:49:22 +08:00
2020-10-21 12:09:15 +08:00
VAB * RenderDevice : : CreateVAB ( VkFormat format , uint32_t count , const void * data , SharingMode sharing_mode )
2019-04-27 21:49:22 +08:00
{
const uint32_t stride = GetStrideByFormat ( format ) ;
if ( stride = = 0 )
{
2020-10-21 12:09:15 +08:00
std : : cerr < < " format[ " < < format < < " ] stride length is 0,please use \" RenderDevice::CreateBuffer(VkBufferUsageFlags,VkDeviceSize,VkSharingMode) \" function. " ;
2019-04-27 21:49:22 +08:00
return ( nullptr ) ;
}
const VkDeviceSize size = stride * count ;
2019-11-25 22:05:05 +08:00
BufferData buf ;
2019-04-27 21:49:22 +08:00
2019-11-25 22:05:05 +08:00
if ( ! CreateBuffer ( & buf , VK_BUFFER_USAGE_VERTEX_BUFFER_BIT , size , data , sharing_mode ) )
2019-04-27 21:49:22 +08:00
return ( nullptr ) ;
2020-07-14 14:03:26 +08:00
return ( new VertexAttribBuffer ( attr - > device , buf , format , stride , count ) ) ;
2019-04-27 21:49:22 +08:00
}
2020-10-21 12:09:15 +08:00
IndexBuffer * RenderDevice : : CreateIBO ( IndexType index_type , uint32_t count , const void * data , SharingMode sharing_mode )
2019-04-27 21:49:22 +08:00
{
uint32_t stride ;
2020-09-02 19:06:12 +08:00
if ( index_type = = IndexType : : U16 ) stride = 2 ; else
if ( index_type = = IndexType : : U32 ) stride = 4 ; else
2019-05-06 12:00:03 +08:00
return ( nullptr ) ;
2019-04-27 21:49:22 +08:00
const VkDeviceSize size = stride * count ;
2019-11-25 22:05:05 +08:00
BufferData buf ;
2019-04-27 21:49:22 +08:00
2019-11-25 22:05:05 +08:00
if ( ! CreateBuffer ( & buf , VK_BUFFER_USAGE_INDEX_BUFFER_BIT , size , data , sharing_mode ) )
2019-04-27 21:49:22 +08:00
return ( nullptr ) ;
2019-11-25 22:05:05 +08:00
return ( new IndexBuffer ( attr - > device , buf , index_type , count ) ) ;
2019-04-27 21:49:22 +08:00
}
2020-10-21 12:09:15 +08:00
GPUBuffer * RenderDevice : : CreateBuffer ( VkBufferUsageFlags buf_usage , VkDeviceSize size , const void * data , SharingMode sharing_mode )
2019-04-27 21:49:22 +08:00
{
2019-11-25 22:05:05 +08:00
BufferData buf ;
2019-04-27 21:49:22 +08:00
2019-11-25 22:05:05 +08:00
if ( ! CreateBuffer ( & buf , buf_usage , size , data , sharing_mode ) )
2019-04-27 21:49:22 +08:00
return ( nullptr ) ;
2020-10-21 12:09:15 +08:00
return ( new GPUBuffer ( attr - > device , buf ) ) ;
2019-04-27 21:49:22 +08:00
}
VK_NAMESPACE_END