2019-04-10 23:31:24 +08:00
# include " RenderSurface.h "
2019-04-16 02:21:35 +08:00
# include<hgl / type / Pair.h>
2019-04-10 21:54:39 +08:00
VK_NAMESPACE_BEGIN
2019-04-16 02:21:35 +08:00
namespace
2019-04-11 22:40:13 +08:00
{
2019-04-16 02:21:35 +08:00
bool CreateVulkanBuffer ( VulkanBuffer & vb , const RenderSurfaceAttribute * rsa , VkBufferUsageFlags buf_usage , VkDeviceSize size , VkSharingMode sharing_mode )
2019-04-11 22:40:13 +08:00
{
2019-04-16 02:21:35 +08:00
VkBufferCreateInfo buf_info = { } ;
buf_info . sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO ;
buf_info . pNext = nullptr ;
buf_info . usage = buf_usage ;
buf_info . size = size ;
buf_info . queueFamilyIndexCount = 0 ;
buf_info . pQueueFamilyIndices = nullptr ;
buf_info . sharingMode = sharing_mode ;
buf_info . flags = 0 ;
if ( vkCreateBuffer ( rsa - > device , & buf_info , nullptr , & vb . buffer ) ! = VK_SUCCESS )
return ( false ) ;
VkMemoryRequirements mem_reqs ;
vkGetBufferMemoryRequirements ( rsa - > device , vb . buffer , & mem_reqs ) ;
VkMemoryAllocateInfo alloc_info = { } ;
alloc_info . sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO ;
alloc_info . pNext = nullptr ;
alloc_info . memoryTypeIndex = 0 ;
alloc_info . allocationSize = mem_reqs . size ;
if ( rsa - > CheckMemoryType ( mem_reqs . memoryTypeBits , VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT , & alloc_info . memoryTypeIndex ) )
2019-04-11 22:40:13 +08:00
{
2019-04-17 02:27:33 +08:00
if ( vkAllocateMemory ( rsa - > device , & alloc_info , nullptr , & vb . memory ) = = VK_SUCCESS )
2019-04-11 22:40:13 +08:00
{
2019-04-17 02:27:33 +08:00
if ( vkBindBufferMemory ( rsa - > device , vb . buffer , vb . memory , 0 ) = = VK_SUCCESS )
2019-04-16 02:21:35 +08:00
{
vb . info . buffer = vb . buffer ;
vb . info . offset = 0 ;
vb . info . range = size ;
2019-04-11 22:40:13 +08:00
2019-04-16 02:21:35 +08:00
return ( true ) ;
}
2019-04-11 22:40:13 +08:00
2019-04-17 02:27:33 +08:00
vkFreeMemory ( rsa - > device , vb . memory , nullptr ) ;
2019-04-11 22:40:13 +08:00
}
}
2019-04-16 02:21:35 +08:00
vkDestroyBuffer ( rsa - > device , vb . buffer , nullptr ) ;
return ( false ) ;
2019-04-11 22:40:13 +08:00
}
2019-04-16 02:21:35 +08:00
} //namespace
VertexBuffer * RenderSurface : : CreateBuffer ( VkBufferUsageFlags buf_usage , VkFormat format , uint32_t count , VkSharingMode sharing_mode )
{
2019-04-16 14:21:06 +08:00
const uint32_t stride = GetStrideByFormat ( format ) ;
2019-04-16 14:58:10 +08:00
if ( stride = = 0 )
{
std : : cerr < < " format[ " < < format < < " ] stride length is 0,please use \" RenderSurface::CreateBuffer(VkBufferUsageFlags,VkDeviceSize,VkSharingMode) \" function. " ;
return ( nullptr ) ;
}
2019-04-16 02:21:35 +08:00
const VkDeviceSize size = stride * count ;
2019-04-16 14:58:10 +08:00
VulkanBuffer vb ;
2019-04-16 02:21:35 +08:00
if ( ! CreateVulkanBuffer ( vb , rsa , buf_usage , size , sharing_mode ) )
return ( nullptr ) ;
return ( new VertexBuffer ( rsa - > device , vb , format , stride , count ) ) ;
}
Buffer * RenderSurface : : CreateBuffer ( VkBufferUsageFlags buf_usage , VkDeviceSize size , VkSharingMode sharing_mode )
{
VulkanBuffer vb ;
if ( ! CreateVulkanBuffer ( vb , rsa , buf_usage , size , sharing_mode ) )
return ( nullptr ) ;
2019-04-11 22:40:13 +08:00
2019-04-16 02:21:35 +08:00
return ( new Buffer ( rsa - > device , vb ) ) ;
2019-04-11 22:40:13 +08:00
}
2019-04-10 21:54:39 +08:00
CommandBuffer * RenderSurface : : CreateCommandBuffer ( )
{
2019-04-11 02:29:21 +08:00
if ( ! rsa - > cmd_pool )
2019-04-10 21:54:39 +08:00
return ( nullptr ) ;
VkCommandBufferAllocateInfo cmd = { } ;
cmd . sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO ;
cmd . pNext = nullptr ;
2019-04-11 02:29:21 +08:00
cmd . commandPool = rsa - > cmd_pool ;
2019-04-10 21:54:39 +08:00
cmd . level = VK_COMMAND_BUFFER_LEVEL_PRIMARY ;
cmd . commandBufferCount = 1 ;
VkCommandBuffer cmd_buf ;
2019-04-11 02:29:21 +08:00
VkResult res = vkAllocateCommandBuffers ( rsa - > device , & cmd , & cmd_buf ) ;
2019-04-10 21:54:39 +08:00
if ( res ! = VK_SUCCESS )
return ( nullptr ) ;
2019-04-11 02:29:21 +08:00
return ( new CommandBuffer ( rsa - > device , rsa - > cmd_pool , cmd_buf ) ) ;
2019-04-10 21:54:39 +08:00
}
2019-04-12 16:39:22 +08:00
//DescriptorSet *RenderSurface::CreateDescSet(int count)
//{
// VkDescriptorSetAllocateInfo alloc_info[1];
// alloc_info[0].sType=VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
// alloc_info[0].pNext=nullptr;
// alloc_info[0].descriptorPool=rsa->desc_pool;
// alloc_info[0].descriptorSetCount=count;
// alloc_info[0].pSetLayouts=desc_layout.data();
//
// VkDescriptorSet desc_set;
//
// desc_set.resize(count);
// res=vkAllocateDescriptorSets(info.device,alloc_info,info.desc_set.data());
//}
RenderPass * RenderSurface : : CreateRenderPass ( )
{
VkAttachmentDescription attachments [ 2 ] ;
attachments [ 0 ] . format = rsa - > format ;
attachments [ 0 ] . samples = VK_SAMPLE_COUNT_1_BIT ;
attachments [ 0 ] . loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR ;
attachments [ 0 ] . storeOp = VK_ATTACHMENT_STORE_OP_STORE ;
attachments [ 0 ] . stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE ;
attachments [ 0 ] . stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE ;
attachments [ 0 ] . initialLayout = VK_IMAGE_LAYOUT_UNDEFINED ;
attachments [ 0 ] . finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR ;
attachments [ 0 ] . flags = 0 ;
attachments [ 1 ] . format = rsa - > depth . format ;
attachments [ 1 ] . samples = VK_SAMPLE_COUNT_1_BIT ;
attachments [ 1 ] . loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR ;
attachments [ 1 ] . storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE ;
attachments [ 1 ] . stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE ;
attachments [ 1 ] . stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE ;
attachments [ 1 ] . initialLayout = VK_IMAGE_LAYOUT_UNDEFINED ;
attachments [ 1 ] . finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ;
attachments [ 1 ] . flags = 0 ;
VkAttachmentReference color_reference = { } ;
color_reference . attachment = 0 ;
color_reference . layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL ;
VkAttachmentReference depth_reference = { } ;
depth_reference . attachment = 1 ;
depth_reference . layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ;
VkSubpassDescription subpass = { } ;
subpass . pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS ;
subpass . flags = 0 ;
subpass . inputAttachmentCount = 0 ;
subpass . pInputAttachments = nullptr ;
subpass . colorAttachmentCount = 1 ;
subpass . pColorAttachments = & color_reference ;
subpass . pResolveAttachments = nullptr ;
subpass . pDepthStencilAttachment = & depth_reference ;
subpass . preserveAttachmentCount = 0 ;
subpass . pPreserveAttachments = nullptr ;
VkRenderPassCreateInfo rp_info = { } ;
rp_info . sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO ;
rp_info . pNext = nullptr ;
rp_info . attachmentCount = 2 ;
rp_info . pAttachments = attachments ;
rp_info . subpassCount = 1 ;
rp_info . pSubpasses = & subpass ;
rp_info . dependencyCount = 0 ;
rp_info . pDependencies = nullptr ;
VkRenderPass render_pass ;
if ( vkCreateRenderPass ( rsa - > device , & rp_info , nullptr , & render_pass ) ! = VK_SUCCESS )
return ( nullptr ) ;
return ( new RenderPass ( rsa - > device , render_pass ) ) ;
}
2019-04-10 21:54:39 +08:00
VK_NAMESPACE_END