ULRE/src/RenderDevice/Vulkan/VKDevice.cpp

278 lines
8.1 KiB
C++
Raw Normal View History

#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/type/Pair.h>
#include<hgl/graph/vulkan/VKImageView.h>
#include<hgl/graph/vulkan/VKCommandBuffer.h>
//#include<hgl/graph/vulkan/VKDescriptorSet.h>
#include<hgl/graph/vulkan/VKRenderPass.h>
#include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKFence.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
#include<hgl/graph/vulkan/VKShaderModuleManage.h>
#include<hgl/graph/vulkan/VKDescriptorSets.h>
VK_NAMESPACE_BEGIN
bool ResizeRenderDevice(DeviceAttribute *attr,uint width,uint height);
2019-04-19 20:10:59 +08:00
Device::Device(DeviceAttribute *da)
{
attr=da;
2019-04-20 16:12:22 +08:00
current_frame=0;
2019-04-19 20:10:59 +08:00
image_acquired_semaphore=this->CreateSem();
2019-04-20 16:12:22 +08:00
draw_fence=this->CreateFence();
texture_fence=this->CreateFence();
hgl_zero(texture_submitInfo);
texture_submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
texture_cmd_buf=CreateCommandBuffer();
present.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
present.pNext = nullptr;
present.swapchainCount = 1;
present.pSwapchains = &attr->swap_chain;
present.pWaitSemaphores = nullptr;
present.waitSemaphoreCount = 0;
present.pResults = nullptr;
2019-05-08 00:25:40 +08:00
main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat());
CreateMainFramebuffer();
2019-04-19 20:10:59 +08:00
}
2019-04-18 22:24:39 +08:00
Device::~Device()
{
main_fb.Clear();
delete main_rp;
2019-04-19 20:10:59 +08:00
delete image_acquired_semaphore;
delete texture_cmd_buf;
delete texture_fence;
2019-04-20 16:12:22 +08:00
delete draw_fence;
2019-04-18 22:24:39 +08:00
delete attr;
}
2019-05-08 00:25:40 +08:00
void Device::CreateMainFramebuffer()
{
const int sc_count=attr->sc_image_views.GetCount();
for(int i=0;i<sc_count;i++)
main_fb.Add(vulkan::CreateFramebuffer(this,main_rp,attr->sc_image_views[i],attr->depth.view));
}
bool Device::Resize(uint width,uint height)
{
main_fb.Clear();
delete main_rp;
delete texture_cmd_buf;
if(!ResizeRenderDevice(attr,width,height))
return(false);
main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat());
2019-05-08 00:25:40 +08:00
CreateMainFramebuffer();
texture_cmd_buf=CreateCommandBuffer();
return(true);
}
2019-04-18 16:06:44 +08:00
CommandBuffer *Device::CreateCommandBuffer()
{
2019-04-18 22:24:39 +08:00
if(!attr->cmd_pool)
return(nullptr);
VkCommandBufferAllocateInfo cmd={};
cmd.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
cmd.pNext=nullptr;
2019-04-18 22:24:39 +08:00
cmd.commandPool=attr->cmd_pool;
cmd.level=VK_COMMAND_BUFFER_LEVEL_PRIMARY;
cmd.commandBufferCount=1;
VkCommandBuffer cmd_buf;
2019-04-18 22:24:39 +08:00
VkResult res=vkAllocateCommandBuffers(attr->device,&cmd,&cmd_buf);
if(res!=VK_SUCCESS)
return(nullptr);
2019-04-20 02:28:57 +08:00
return(new CommandBuffer(attr->device,attr->swapchain_extent,attr->cmd_pool,cmd_buf));
}
RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format)
2019-04-12 16:39:22 +08:00
{
VkAttachmentDescription attachments[2];
2019-04-18 21:28:25 +08:00
VkAttachmentReference color_reference={0,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkAttachmentReference depth_reference={1,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL};
2019-04-12 16:39:22 +08:00
VkSubpassDescription subpass={};
subpass.pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.flags=0;
subpass.inputAttachmentCount=0;
subpass.pInputAttachments=nullptr;
2019-04-12 16:39:22 +08:00
subpass.pResolveAttachments=nullptr;
subpass.preserveAttachmentCount=0;
subpass.pPreserveAttachments=nullptr;
int att_count=0;
if(color_format!=VK_FORMAT_UNDEFINED)
{
attachments[0].format=color_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;
++att_count;
subpass.colorAttachmentCount=1;
subpass.pColorAttachments=&color_reference;
}
else
{
subpass.colorAttachmentCount=0;
subpass.pColorAttachments=nullptr;
}
if(depth_format!=VK_FORMAT_UNDEFINED)
{
attachments[att_count].format=depth_format;
attachments[att_count].samples=VK_SAMPLE_COUNT_1_BIT;
attachments[att_count].loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[att_count].storeOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[att_count].stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[att_count].stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[att_count].initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
attachments[att_count].finalLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachments[att_count].flags=0;
depth_reference.attachment=att_count;
++att_count;
subpass.pDepthStencilAttachment=&depth_reference;
}
else
{
subpass.pDepthStencilAttachment=nullptr;
}
2019-04-12 16:39:22 +08:00
2019-04-20 16:12:22 +08:00
VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
2019-04-12 16:39:22 +08:00
VkRenderPassCreateInfo rp_info={};
rp_info.sType=VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
rp_info.pNext=nullptr;
rp_info.attachmentCount=att_count;
2019-04-12 16:39:22 +08:00
rp_info.pAttachments=attachments;
rp_info.subpassCount=1;
rp_info.pSubpasses=&subpass;
2019-04-20 16:12:22 +08:00
rp_info.dependencyCount=1;
rp_info.pDependencies=&dependency;
2019-04-12 16:39:22 +08:00
VkRenderPass render_pass;
2019-04-18 22:24:39 +08:00
if(vkCreateRenderPass(attr->device,&rp_info,nullptr,&render_pass)!=VK_SUCCESS)
2019-04-12 16:39:22 +08:00
return(nullptr);
return(new RenderPass(attr->device,render_pass,color_format,depth_format));
2019-04-12 16:39:22 +08:00
}
2019-04-19 19:58:01 +08:00
Fence *Device::CreateFence()
{
VkFenceCreateInfo fenceInfo;
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceInfo.pNext = nullptr;
fenceInfo.flags = 0;
2019-04-20 16:12:22 +08:00
VkFence fence;
2019-04-19 19:58:01 +08:00
2019-04-20 16:12:22 +08:00
if(vkCreateFence(attr->device, &fenceInfo, nullptr, &fence)!=VK_SUCCESS)
2019-04-19 19:58:01 +08:00
return(nullptr);
2019-04-20 16:12:22 +08:00
return(new Fence(attr->device,fence));
2019-04-19 19:58:01 +08:00
}
2019-04-19 20:04:08 +08:00
Semaphore *Device::CreateSem()
{
VkSemaphoreCreateInfo SemaphoreCreateInfo;
SemaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
SemaphoreCreateInfo.pNext = nullptr;
SemaphoreCreateInfo.flags = 0;
VkSemaphore sem;
if(vkCreateSemaphore(attr->device, &SemaphoreCreateInfo, nullptr, &sem)!=VK_SUCCESS)
return(nullptr);
return(new Semaphore(attr->device,sem));
}
2019-04-19 20:10:59 +08:00
ShaderModuleManage *Device::CreateShaderModuleManage()
{
return(new ShaderModuleManage(this));
}
2019-04-19 20:10:59 +08:00
bool Device::AcquireNextImage()
{
2019-04-20 16:12:22 +08:00
return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,*image_acquired_semaphore,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS);
2019-04-19 20:10:59 +08:00
}
bool Device::SubmitDraw(const VkCommandBuffer *cmd_bufs,const uint32_t count)
{
if(!cmd_bufs||count<=0)
return(false);
VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
2019-04-20 16:12:22 +08:00
VkSubmitInfo submit_info = {};
2019-04-20 16:12:22 +08:00
VkSemaphore wait_sem=*image_acquired_semaphore;
2019-04-20 16:12:22 +08:00
submit_info.pNext = nullptr;
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info.waitSemaphoreCount = 1;
submit_info.pWaitSemaphores = &wait_sem;
submit_info.pWaitDstStageMask = &pipe_stage_flags;
submit_info.commandBufferCount = count;
submit_info.pCommandBuffers = cmd_bufs;
2019-04-20 16:12:22 +08:00
submit_info.signalSemaphoreCount = 0;
submit_info.pSignalSemaphores = nullptr;
return(vkQueueSubmit(attr->graphics_queue, 1, &submit_info, *draw_fence)==VK_SUCCESS);
}
2019-04-20 16:12:22 +08:00
bool Device::Wait(bool wait_all,uint64_t time_out)
{
2019-04-20 16:12:22 +08:00
VkFence fence=*draw_fence;
2019-04-20 16:12:22 +08:00
vkWaitForFences(attr->device, 1, &fence, wait_all, time_out);
vkResetFences(attr->device,1,&fence);
return(true);
}
bool Device::QueuePresent()
{
2019-04-20 16:12:22 +08:00
present.pImageIndices = &current_frame;
present.waitSemaphoreCount = 0;
present.pWaitSemaphores = nullptr;
return(vkQueuePresentKHR(attr->present_queue, &present)==VK_SUCCESS);
}
VK_NAMESPACE_END