diff --git a/example/Vulkan/VulkanAppFramework.h b/example/Vulkan/VulkanAppFramework.h index 829e612c..1006e209 100644 --- a/example/Vulkan/VulkanAppFramework.h +++ b/example/Vulkan/VulkanAppFramework.h @@ -51,6 +51,11 @@ protected: protected: vulkan::Device * device =nullptr; + vulkan::Swapchain * swapchain =nullptr; + + vulkan::Semaphore * present_complete_semaphore =nullptr, + * render_complete_semaphore =nullptr; + vulkan::ShaderModuleManage *shader_manage =nullptr; private: @@ -69,6 +74,9 @@ public: virtual ~VulkanApplicationFramework() { + SAFE_CLEAR(present_complete_semaphore); + SAFE_CLEAR(render_complete_semaphore); + SAFE_CLEAR(db); SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count); @@ -105,6 +113,10 @@ public: if(!device) return(false); + swapchain=device->GetSwapchain(); + present_complete_semaphore =device->CreateSem(); + render_complete_semaphore =device->CreateSem(); + shader_manage=device->CreateShaderModuleManage(); db=new SceneDB(device); @@ -135,14 +147,15 @@ public: if(cmd_buf) SAFE_CLEAR_OBJECT_ARRAY(cmd_buf,swap_chain_count); - swap_chain_count=device->GetSwapChainImageCount(); + + swap_chain_count=swapchain->GetImageCount(); { - const VkExtent2D extent=device->GetExtent(); + const VkExtent2D extent=swapchain->GetExtent(); cmd_buf=hgl_zero_new(swap_chain_count); for(uint i=0;iCreateCommandBuffer(&extent,2); + cmd_buf[i]=device->CreateCommandBuffer(extent,2); } } @@ -156,7 +169,7 @@ public: vulkan::CommandBuffer *cb=cmd_buf[index]; cb->Begin(); - cb->BeginRenderPass(device->GetMainRenderPass(),device->GetFramebuffer(index)); + cb->BeginRenderPass(swapchain->GetMainRenderPass(),swapchain->GetFramebuffer(index)); cb->Bind(p); cb->Bind(ds); cb->Bind(r); @@ -178,7 +191,7 @@ public: void BuildCurrentCommandBuffer(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r) { - BuildCommandBuffer(device->GetCurrentFrameIndices(),p,ds,r); + BuildCommandBuffer(swapchain->GetCurrentFrameIndices(),p,ds,r); } void BuildCommandBuffer(uint32_t index,RenderList *rl) @@ -188,7 +201,7 @@ public: vulkan::CommandBuffer *cb=cmd_buf[index]; cb->Begin(); - cb->BeginRenderPass(device->GetMainRenderPass(),device->GetFramebuffer(index)); + cb->BeginRenderPass(swapchain->GetMainRenderPass(),swapchain->GetFramebuffer(index)); rl->Render(cb); cb->EndRenderPass(); cb->End(); @@ -202,16 +215,19 @@ public: void BuildCurrentCommandBuffer(RenderList *rl) { - BuildCommandBuffer(device->GetCurrentFrameIndices(),rl); + BuildCommandBuffer(swapchain->GetCurrentFrameIndices(),rl); } public: int AcquireNextImage() { - if(device->Wait()) - if(device->AcquireNextImage()) - return device->GetCurrentFrameIndices(); + if(swapchain->Wait()) + { + int cur=swapchain->AcquireNextImage(present_complete_semaphore); + + return cur; + } return -1; } @@ -220,8 +236,8 @@ public: { VkCommandBuffer cb=*cmd_buf[index]; - device->SubmitDraw(&cb); - device->PresentBackbuffer(); + swapchain->SubmitDraw(cb,present_complete_semaphore,render_complete_semaphore); + swapchain->PresentBackbuffer(render_complete_semaphore); } virtual void Draw() diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 831dbd3e..b61165e9 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -77,7 +77,7 @@ private: bool InitUBO() { - const VkExtent2D extent=device->GetExtent(); + const VkExtent2D extent=swapchain->GetExtent(); world.mvp=ortho(extent.width,extent.height); @@ -108,7 +108,7 @@ private: { AutoDelete - pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetMainRenderPass(),device->GetExtent()); + pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent()); pipeline_creater->CloseCullFace(); pipeline_creater->Set(PRIM_TRIANGLES); @@ -119,7 +119,7 @@ private: void *data; uint size=filesystem::LoadFileToMemory(PIPELINE_FILENAME,(void **)&data); - AutoDelete pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetMainRenderPass(),device->GetExtent(),(uchar *)data,size); + AutoDelete pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent(),(uchar *)data,size); pipeline=pipeline_creater->Create(); } diff --git a/inc/hgl/graph/VertexBuffer.h b/inc/hgl/graph/VertexBuffer.h index 4aed8518..7a29f24c 100644 --- a/inc/hgl/graph/VertexBuffer.h +++ b/inc/hgl/graph/VertexBuffer.h @@ -1111,11 +1111,11 @@ namespace hgl typedef VertexBuffer2 VB2f; template<> inline VkFormat VertexBuffer2::GetDataType()const{return FMT_RG32F; } typedef VertexBuffer2 VB2d; template<> inline VkFormat VertexBuffer2::GetDataType()const{return FMT_RG64F; } - typedef VertexBuffer3 VB3i8 ,VB3b; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB8I; } - typedef VertexBuffer3 VB3i16 ,VB3s; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB16I; } +// typedef VertexBuffer3 VB3i8 ,VB3b; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB8I; } +// typedef VertexBuffer3 VB3i16 ,VB3s; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB16I; } typedef VertexBuffer3 VB3i32 ,VB3i; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB32I; } - typedef VertexBuffer3 VB3u8 ,VB3ub; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB8U; } - typedef VertexBuffer3 VB3u16 ,VB3us; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB16U; } +// typedef VertexBuffer3 VB3u8 ,VB3ub; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB8U; } +// typedef VertexBuffer3 VB3u16 ,VB3us; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB16U; } typedef VertexBuffer3 VB3u32 ,VB3ui; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB32U; } typedef VertexBuffer3 VB3f; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB32F; } typedef VertexBuffer3 VB3d; template<> inline VkFormat VertexBuffer3::GetDataType()const{return FMT_RGB64F; } diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index ef47a754..8b9cd1fb 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -50,6 +50,12 @@ public: public: bool Resize (const VkExtent2D &); + bool Resize (const uint32_t &w,const uint32_t &h) + { + VkExtent2D extent={w,h}; + + return Resize(extent); + } public: //内存相关 diff --git a/inc/hgl/graph/vulkan/VKSwapchain.h b/inc/hgl/graph/vulkan/VKSwapchain.h index cb566799..9779d0ce 100644 --- a/inc/hgl/graph/vulkan/VKSwapchain.h +++ b/inc/hgl/graph/vulkan/VKSwapchain.h @@ -1,26 +1,11 @@ #ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE #define HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE -#include +#include #include #include #include VK_NAMESPACE_BEGIN -struct SwapchainAttribute -{ - VkDevice device; - - VkExtent2D extent; - - VkQueue graphics_queue =VK_NULL_HANDLE; - VkSwapchainKHR swap_chain =VK_NULL_HANDLE; - - uint32_t swap_chain_count; - - ObjectList sc_color; - Texture2D * sc_depth =nullptr; -};//struct SwapchainAttribute - class Swapchain { protected: @@ -39,10 +24,7 @@ protected: uint32_t current_fence; ObjectList fence_list; - RenderPass *main_rp =nullptr; - - Semaphore *present_complete_semaphore =nullptr, - *render_complete_semaphore =nullptr; + RenderPass *main_rp =nullptr; VkSubmitInfo submit_info; VkPresentInfoKHR present_info; @@ -63,8 +45,6 @@ public: virtual ~Swapchain(); - void Recreate (); - bool Wait (bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1); ///<等待队列完成 /** @@ -73,7 +53,7 @@ public: * @return 下一帧的索引 * @return <0 错误 */ - int AcquireNextImage (VkSemaphore complete_semaphore); ///<请求获得下一帧的索引 + int AcquireNextImage (vulkan::Semaphore *complete_semaphore); ///<请求获得下一帧的索引 /** * 提交一个绘制指令 @@ -81,7 +61,7 @@ public: * @param wait_sem 指令开始前要等待的确认的信号 * @param complete_semaphore 绘制完成后发送的信号 */ - bool SubmitDraw (VkCommandBuffer &cmd_list,VkSemaphore &wait_sem,VkSemaphore &complete_semaphore); ///<提交绘制指令 + bool SubmitDraw (VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_semaphore); ///<提交绘制指令 /** * 提交一批绘制指令 @@ -91,8 +71,10 @@ public: */ bool SubmitDraw (List &cmd_list,List &wait_sems,List &complete_semaphores); ///<提交绘制指令 - - bool PresentBackbuffer (); ///<等待绘制队列完成,并将后台缓冲区呈现到前台 + /** + * @param render_complete_semaphore 渲染完成信号 + */ + bool PresentBackbuffer (vulkan::Semaphore *render_complete_semaphore); ///<等待绘制队列完成,并将后台缓冲区呈现到前台 };//class Swapchain VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKSwapchainAttribute.h b/inc/hgl/graph/vulkan/VKSwapchainAttribute.h index c5cfab02..b26fe4f2 100644 --- a/inc/hgl/graph/vulkan/VKSwapchainAttribute.h +++ b/inc/hgl/graph/vulkan/VKSwapchainAttribute.h @@ -1,7 +1,8 @@ #ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE #define HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE -#include +#include +#include VK_NAMESPACE_BEGIN struct SwapchainAttribute { diff --git a/src/Platform/Window.cpp b/src/Platform/Window.cpp index 13734bdc..6774ae1b 100644 --- a/src/Platform/Window.cpp +++ b/src/Platform/Window.cpp @@ -4,7 +4,7 @@ #include VK_NAMESPACE_BEGIN -Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,uint width,uint height); +Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,const VkExtent2D &); VK_NAMESPACE_END namespace hgl @@ -34,7 +34,9 @@ namespace hgl if(!surface) return(nullptr); - device=VK_NAMESPACE::CreateRenderDevice(*vk_inst,pd,surface,width,height); + VkExtent2D extent={width,height}; + + device=VK_NAMESPACE::CreateRenderDevice(*vk_inst,pd,surface,extent); if(!device) { diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index c6fba34c..3bb9ac1d 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -23,11 +23,14 @@ Device::Device(DeviceAttribute *da) texture_cmd_buf=nullptr; + swapchain=nullptr; Resize(attr->surface_caps.currentExtent); } Device::~Device() { + SAFE_CLEAR(swapchain); + delete texture_cmd_buf; delete texture_fence; diff --git a/src/RenderDevice/Vulkan/VKSwapchain.cpp b/src/RenderDevice/Vulkan/VKSwapchain.cpp index e99b3cc5..6c16678d 100644 --- a/src/RenderDevice/Vulkan/VKSwapchain.cpp +++ b/src/RenderDevice/Vulkan/VKSwapchain.cpp @@ -9,49 +9,22 @@ Swapchain::Swapchain(Device *dev,SwapchainAttribute *sa) sc_attr=sa; pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - - current_frame=0; - main_rp=nullptr; - - present_complete_semaphore =device->CreateSem(); - render_complete_semaphore =device->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.waitSemaphoreCount = 1; + //submit_info.pWaitSemaphores = *present_complete_semaphore; submit_info.pWaitDstStageMask = &pipe_stage_flags; - submit_info.signalSemaphoreCount = 1; - submit_info.pSignalSemaphores = *render_complete_semaphore; + //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.waitSemaphoreCount = 1; - present_info.pWaitSemaphores = *render_complete_semaphore; + //present_info.pWaitSemaphores = *render_complete_semaphore; present_info.swapchainCount = 1; present_info.pResults = nullptr; -} - -Swapchain::~Swapchain() -{ - fence_list.Clear(); - render_frame.Clear(); - - delete present_complete_semaphore; - delete render_complete_semaphore; - - delete main_rp; - - SAFE_CLEAR(sc_attr); -} - -void Swapchain::Recreate() -{ - fence_list.Clear(); - render_frame.Clear(); - - if(main_rp)delete main_rp; - + present_info.pSwapchains=&(sc_attr->swap_chain); main_rp=device->CreateRenderPass(sc_attr->sc_color[0]->GetFormat(),sc_attr->sc_depth->GetFormat()); @@ -66,6 +39,16 @@ void Swapchain::Recreate() current_fence=0; } +Swapchain::~Swapchain() +{ + fence_list.Clear(); + render_frame.Clear(); + + delete main_rp; + + SAFE_CLEAR(sc_attr); +} + bool Swapchain::Wait(bool wait_all,uint64_t time_out) { VkFence fence=*fence_list[current_fence]; @@ -78,22 +61,27 @@ bool Swapchain::Wait(bool wait_all,uint64_t time_out) return(true); } -int Swapchain::AcquireNextImage(VkSemaphore present_complete_semaphore) +int Swapchain::AcquireNextImage(vulkan::Semaphore *present_complete_semaphore) { - if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS) + VkSemaphore sem=*present_complete_semaphore; + + if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,sem,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS) return current_frame; return -1; } -bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,VkSemaphore &wait_sem,VkSemaphore &complete_sem) +bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem) { + VkSemaphore ws=*wait_sem; + VkSemaphore cs=*complete_sem; + submit_info.waitSemaphoreCount =1; - submit_info.pWaitSemaphores =&wait_sem; + submit_info.pWaitSemaphores =&ws; submit_info.commandBufferCount =1; submit_info.pCommandBuffers =&cmd_list; submit_info.signalSemaphoreCount=1; - submit_info.pSignalSemaphores =&complete_sem; + submit_info.pSignalSemaphores =&cs; VkFence fence=*fence_list[current_fence]; @@ -131,8 +119,11 @@ bool Swapchain::SubmitDraw(List &cmd_lists,List &w return(result==VK_SUCCESS); } -bool Swapchain::PresentBackbuffer() +bool Swapchain::PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore) { + VkSemaphore sem=*render_complete_semaphore; + + present_info.pWaitSemaphores=&sem; present_info.pImageIndices=¤t_frame; VkResult result=vkQueuePresentKHR(sc_attr->graphics_queue,&present_info);