ULRE/src/RenderDevice/Vulkan/VKDevice.cpp

290 lines
8.4 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/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;
texture_fence=this->CreateFence();
2019-06-15 23:52:10 +08:00
hgl_zero(texture_submit_info);
texture_submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
2019-06-14 10:32:43 +08:00
main_rp=nullptr;
texture_cmd_buf=nullptr;
2019-06-15 23:52:10 +08:00
pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
present_complete_semaphore =this->CreateSem();
render_complete_semaphore =this->CreateSem();
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submit_info.pNext = nullptr;
submit_info.waitSemaphoreCount = 1;
submit_info.pWaitSemaphores = *present_complete_semaphore;
submit_info.pWaitDstStageMask = &pipe_stage_flags;
submit_info.signalSemaphoreCount = 1;
submit_info.pSignalSemaphores = *render_complete_semaphore;
present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
present_info.pNext = nullptr;
present_info.swapchainCount = 1;
present_info.pResults = nullptr;
present_info.waitSemaphoreCount = 1;
present_info.pWaitSemaphores = *render_complete_semaphore;
2019-06-14 10:32:43 +08:00
RecreateDevice();
2019-04-19 20:10:59 +08:00
}
2019-04-18 22:24:39 +08:00
Device::~Device()
{
render_frame.Clear();
2019-06-15 23:52:10 +08:00
delete present_complete_semaphore;
delete render_complete_semaphore;
delete main_rp;
delete texture_cmd_buf;
delete texture_fence;
2019-04-20 16:12:22 +08:00
2019-04-18 22:24:39 +08:00
delete attr;
}
2019-06-14 10:32:43 +08:00
void Device::RecreateDevice()
{
render_frame.Clear();
2019-06-14 10:32:43 +08:00
if(main_rp)delete main_rp;
if(texture_cmd_buf)delete texture_cmd_buf;
2019-06-15 23:52:10 +08:00
present_info.pSwapchains=&attr->swap_chain;
2019-06-14 10:32:43 +08:00
main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat());
const int sc_count=attr->sc_image_views.GetCount();
for(int i=0;i<sc_count;i++)
{
RenderFrame *rf=new RenderFrame;
rf->frame_buffer =vulkan::CreateFramebuffer(this,main_rp,attr->sc_image_views[i],attr->depth.view);
rf->draw_fence =this->CreateFence();
render_frame.Add(rf);
}
2019-06-14 10:32:43 +08:00
texture_cmd_buf=CreateCommandBuffer();
}
bool Device::Resize(uint width,uint height)
{
if(!ResizeRenderDevice(attr,width,height))
return(false);
2019-06-14 10:32:43 +08:00
RecreateDevice();
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-06-15 23:52:10 +08:00
return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,*present_complete_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);
submit_info.commandBufferCount = count;
submit_info.pCommandBuffers = cmd_bufs;
2019-06-15 23:52:10 +08:00
VkFence fence=*(render_frame[current_frame]->draw_fence);
2019-04-20 16:12:22 +08:00
2019-06-15 23:52:10 +08:00
VkResult result=vkQueueSubmit(attr->graphics_queue,1,&submit_info,fence);
return(result==VK_SUCCESS);
}
2019-04-20 16:12:22 +08:00
bool Device::Wait(bool wait_all,uint64_t time_out)
{
VkFence fence=*(render_frame[current_frame]->draw_fence);
2019-06-15 23:52:10 +08:00
VkResult result=vkWaitForFences(attr->device,1,&fence,wait_all,time_out);
result=vkResetFences(attr->device,1,&fence);
return(true);
}
bool Device::QueuePresent()
{
2019-06-15 23:52:10 +08:00
present_info.pImageIndices=&current_frame;
2019-06-15 23:52:10 +08:00
if(vkQueuePresentKHR(attr->graphics_queue,&present_info)!=VK_SUCCESS)
return(false);
if(vkQueueWaitIdle(attr->graphics_queue)!=VK_SUCCESS)
return(false);
2019-06-15 23:52:10 +08:00
return(true);
}
VK_NAMESPACE_END