改进framework结构
This commit is contained in:
parent
6c8cf14abe
commit
ddf0f3682d
@ -188,23 +188,18 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
void AcquireNextFrame()
|
|
||||||
{
|
|
||||||
device->AcquireNextImage();
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual void Draw()
|
virtual void Draw()
|
||||||
{
|
{
|
||||||
|
device->AcquireNextImage();
|
||||||
|
device->Wait();
|
||||||
|
|
||||||
uint32_t index=device->GetCurrentFrameIndices();
|
uint32_t index=device->GetCurrentFrameIndices();
|
||||||
|
|
||||||
VkCommandBuffer cb=*cmd_buf[index];
|
VkCommandBuffer cb=*cmd_buf[index];
|
||||||
|
|
||||||
device->SubmitDraw(&cb);
|
device->SubmitDraw(&cb);
|
||||||
device->Wait(&index);
|
|
||||||
device->QueuePresent();
|
device->QueuePresent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,10 +208,7 @@ public:
|
|||||||
if(!win->Update())return(false);
|
if(!win->Update())return(false);
|
||||||
|
|
||||||
if(win->IsVisible())
|
if(win->IsVisible())
|
||||||
{
|
|
||||||
AcquireNextFrame();
|
|
||||||
Draw();
|
Draw();
|
||||||
}
|
|
||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,16 @@ class Device
|
|||||||
{
|
{
|
||||||
DeviceAttribute *attr;
|
DeviceAttribute *attr;
|
||||||
|
|
||||||
|
Semaphore *present_complete_semaphore=nullptr,
|
||||||
|
*render_complete_semaphore=nullptr;
|
||||||
|
|
||||||
|
VkPipelineStageFlags pipe_stage_flags;
|
||||||
|
VkSubmitInfo submit_info;
|
||||||
|
|
||||||
struct RenderFrame
|
struct RenderFrame
|
||||||
{
|
{
|
||||||
Framebuffer *frame_buffer=nullptr;
|
Framebuffer *frame_buffer=nullptr;
|
||||||
|
|
||||||
Semaphore *present_complete_semaphore=nullptr,
|
|
||||||
*render_complete_semaphore=nullptr;
|
|
||||||
|
|
||||||
Fence *draw_fence=nullptr;
|
Fence *draw_fence=nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -31,9 +34,6 @@ class Device
|
|||||||
~RenderFrame()
|
~RenderFrame()
|
||||||
{
|
{
|
||||||
SAFE_CLEAR(frame_buffer);
|
SAFE_CLEAR(frame_buffer);
|
||||||
|
|
||||||
SAFE_CLEAR(present_complete_semaphore);
|
|
||||||
SAFE_CLEAR(render_complete_semaphore);
|
|
||||||
SAFE_CLEAR(draw_fence);
|
SAFE_CLEAR(draw_fence);
|
||||||
}
|
}
|
||||||
};//struct RenderFrame
|
};//struct RenderFrame
|
||||||
@ -42,7 +42,7 @@ class Device
|
|||||||
|
|
||||||
Fence *texture_fence;
|
Fence *texture_fence;
|
||||||
|
|
||||||
VkSubmitInfo texture_submitInfo;
|
VkSubmitInfo texture_submit_info;
|
||||||
CommandBuffer *texture_cmd_buf;
|
CommandBuffer *texture_cmd_buf;
|
||||||
|
|
||||||
RenderPass *main_rp;
|
RenderPass *main_rp;
|
||||||
@ -50,7 +50,7 @@ class Device
|
|||||||
uint32_t current_frame;
|
uint32_t current_frame;
|
||||||
ObjectList<RenderFrame> render_frame;
|
ObjectList<RenderFrame> render_frame;
|
||||||
|
|
||||||
VkPresentInfoKHR present;
|
VkPresentInfoKHR present_info;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -23,6 +23,8 @@ public:
|
|||||||
~Semaphore();
|
~Semaphore();
|
||||||
|
|
||||||
operator VkSemaphore(){return sem;}
|
operator VkSemaphore(){return sem;}
|
||||||
|
|
||||||
|
operator const VkSemaphore *()const{return &sem;}
|
||||||
};//class Semaphore
|
};//class Semaphore
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
#endif//HGL_GRAPH_VULKAN_SEMAPHORE_INCLUDE
|
#endif//HGL_GRAPH_VULKAN_SEMAPHORE_INCLUDE
|
||||||
|
@ -19,11 +19,32 @@ Device::Device(DeviceAttribute *da)
|
|||||||
|
|
||||||
texture_fence=this->CreateFence();
|
texture_fence=this->CreateFence();
|
||||||
|
|
||||||
hgl_zero(texture_submitInfo);
|
hgl_zero(texture_submit_info);
|
||||||
texture_submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
texture_submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||||
|
|
||||||
main_rp=nullptr;
|
main_rp=nullptr;
|
||||||
texture_cmd_buf=nullptr;
|
texture_cmd_buf=nullptr;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
RecreateDevice();
|
RecreateDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,6 +52,9 @@ Device::~Device()
|
|||||||
{
|
{
|
||||||
render_frame.Clear();
|
render_frame.Clear();
|
||||||
|
|
||||||
|
delete present_complete_semaphore;
|
||||||
|
delete render_complete_semaphore;
|
||||||
|
|
||||||
delete main_rp;
|
delete main_rp;
|
||||||
|
|
||||||
delete texture_cmd_buf;
|
delete texture_cmd_buf;
|
||||||
@ -45,11 +69,7 @@ void Device::RecreateDevice()
|
|||||||
if(main_rp)delete main_rp;
|
if(main_rp)delete main_rp;
|
||||||
if(texture_cmd_buf)delete texture_cmd_buf;
|
if(texture_cmd_buf)delete texture_cmd_buf;
|
||||||
|
|
||||||
present.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
present_info.pSwapchains=&attr->swap_chain;
|
||||||
present.pNext = nullptr;
|
|
||||||
present.swapchainCount = 1;
|
|
||||||
present.pSwapchains = &attr->swap_chain;
|
|
||||||
present.pResults = nullptr;
|
|
||||||
|
|
||||||
main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat());
|
main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat());
|
||||||
|
|
||||||
@ -60,8 +80,6 @@ void Device::RecreateDevice()
|
|||||||
RenderFrame *rf=new RenderFrame;
|
RenderFrame *rf=new RenderFrame;
|
||||||
|
|
||||||
rf->frame_buffer =vulkan::CreateFramebuffer(this,main_rp,attr->sc_image_views[i],attr->depth.view);
|
rf->frame_buffer =vulkan::CreateFramebuffer(this,main_rp,attr->sc_image_views[i],attr->depth.view);
|
||||||
rf->present_complete_semaphore =this->CreateSem();
|
|
||||||
rf->render_complete_semaphore =this->CreateSem();
|
|
||||||
rf->draw_fence =this->CreateFence();
|
rf->draw_fence =this->CreateFence();
|
||||||
|
|
||||||
render_frame.Add(rf);
|
render_frame.Add(rf);
|
||||||
@ -227,9 +245,7 @@ ShaderModuleManage *Device::CreateShaderModuleManage()
|
|||||||
|
|
||||||
bool Device::AcquireNextImage()
|
bool Device::AcquireNextImage()
|
||||||
{
|
{
|
||||||
VkSemaphore present_complete_semaphore=*(render_frame[current_frame]->present_complete_semaphore);
|
return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,*present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS);
|
||||||
|
|
||||||
return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Device::SubmitDraw(const VkCommandBuffer *cmd_bufs,const uint32_t count)
|
bool Device::SubmitDraw(const VkCommandBuffer *cmd_bufs,const uint32_t count)
|
||||||
@ -237,47 +253,37 @@ bool Device::SubmitDraw(const VkCommandBuffer *cmd_bufs,const uint32_t count)
|
|||||||
if(!cmd_bufs||count<=0)
|
if(!cmd_bufs||count<=0)
|
||||||
return(false);
|
return(false);
|
||||||
|
|
||||||
VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
||||||
VkSubmitInfo submit_info = {};
|
|
||||||
|
|
||||||
RenderFrame *rf=render_frame[current_frame];
|
|
||||||
|
|
||||||
VkSemaphore present_sem=*(rf->present_complete_semaphore);
|
|
||||||
VkSemaphore complete_sem=*(rf->render_complete_semaphore);
|
|
||||||
|
|
||||||
submit_info.pNext = nullptr;
|
|
||||||
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
||||||
submit_info.waitSemaphoreCount = 1;
|
|
||||||
submit_info.pWaitSemaphores = &present_sem;
|
|
||||||
submit_info.pWaitDstStageMask = &pipe_stage_flags;
|
|
||||||
submit_info.commandBufferCount = count;
|
submit_info.commandBufferCount = count;
|
||||||
submit_info.pCommandBuffers = cmd_bufs;
|
submit_info.pCommandBuffers = cmd_bufs;
|
||||||
submit_info.signalSemaphoreCount = 1;
|
|
||||||
submit_info.pSignalSemaphores = &complete_sem;
|
|
||||||
|
|
||||||
VkFence fence=*(rf->draw_fence);
|
VkFence fence=*(render_frame[current_frame]->draw_fence);
|
||||||
|
|
||||||
return(vkQueueSubmit(attr->graphics_queue,1,&submit_info,fence)==VK_SUCCESS);
|
VkResult result=vkQueueSubmit(attr->graphics_queue,1,&submit_info,fence);
|
||||||
|
|
||||||
|
return(result==VK_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Device::Wait(bool wait_all,uint64_t time_out)
|
bool Device::Wait(bool wait_all,uint64_t time_out)
|
||||||
{
|
{
|
||||||
VkFence fence=*(render_frame[current_frame]->draw_fence);
|
VkFence fence=*(render_frame[current_frame]->draw_fence);
|
||||||
|
|
||||||
while(vkWaitForFences(attr->device,1,&fence,wait_all,time_out)==VK_TIMEOUT){}
|
VkResult result=vkWaitForFences(attr->device,1,&fence,wait_all,time_out);
|
||||||
vkResetFences(attr->device,1,&fence);
|
|
||||||
|
result=vkResetFences(attr->device,1,&fence);
|
||||||
|
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Device::QueuePresent()
|
bool Device::QueuePresent()
|
||||||
{
|
{
|
||||||
VkSemaphore render_complete_semaphore=*(render_frame[current_frame]->render_complete_semaphore);
|
present_info.pImageIndices=¤t_frame;
|
||||||
|
|
||||||
present.pImageIndices = ¤t_frame;
|
if(vkQueuePresentKHR(attr->graphics_queue,&present_info)!=VK_SUCCESS)
|
||||||
present.waitSemaphoreCount = 1;
|
return(false);
|
||||||
present.pWaitSemaphores = &render_complete_semaphore;
|
|
||||||
|
|
||||||
return(vkQueuePresentKHR(attr->present_queue, &present)==VK_SUCCESS);
|
if(vkQueueWaitIdle(attr->graphics_queue)!=VK_SUCCESS)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
return(true);
|
||||||
}
|
}
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
|
@ -259,12 +259,12 @@ bool Device::SubmitTexture(const VkCommandBuffer *cmd_bufs,const uint32_t count)
|
|||||||
if(!cmd_bufs||count<=0)
|
if(!cmd_bufs||count<=0)
|
||||||
return(false);
|
return(false);
|
||||||
|
|
||||||
texture_submitInfo.commandBufferCount = count;
|
texture_submit_info.commandBufferCount = count;
|
||||||
texture_submitInfo.pCommandBuffers = cmd_bufs;
|
texture_submit_info.pCommandBuffers = cmd_bufs;
|
||||||
|
|
||||||
VkFence fence=*texture_fence;
|
VkFence fence=*texture_fence;
|
||||||
|
|
||||||
if(vkQueueSubmit(attr->graphics_queue, 1, &texture_submitInfo, fence))return(false);
|
if(vkQueueSubmit(attr->graphics_queue, 1, &texture_submit_info, fence))return(false);
|
||||||
if(vkWaitForFences(attr->device, 1, &fence, VK_TRUE, HGL_NANO_SEC_PER_SEC*0.1)!=VK_SUCCESS)return(false);
|
if(vkWaitForFences(attr->device, 1, &fence, VK_TRUE, HGL_NANO_SEC_PER_SEC*0.1)!=VK_SUCCESS)return(false);
|
||||||
vkResetFences(attr->device,1,&fence);
|
vkResetFences(attr->device,1,&fence);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user