独立Swapchain后,triangle范例可以正常绘制且不报错了

This commit is contained in:
hyzboy 2019-07-16 10:26:24 +08:00
parent 4a46a6e014
commit 7da7e12020
9 changed files with 88 additions and 87 deletions

View File

@ -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<vulkan::CommandBuffer *>(swap_chain_count);
for(uint i=0;i<swap_chain_count;i++)
cmd_buf[i]=device->CreateCommandBuffer(&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()

View File

@ -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<vulkan::PipelineCreater>
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<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,material,device->GetMainRenderPass(),device->GetExtent(),(uchar *)data,size);
AutoDelete<vulkan::PipelineCreater> pipeline_creater=new vulkan::PipelineCreater(device,material,swapchain->GetMainRenderPass(),swapchain->GetExtent(),(uchar *)data,size);
pipeline=pipeline_creater->Create();
}

View File

@ -1111,11 +1111,11 @@ namespace hgl
typedef VertexBuffer2<float > VB2f; template<> inline VkFormat VertexBuffer2<float >::GetDataType()const{return FMT_RG32F; }
typedef VertexBuffer2<double> VB2d; template<> inline VkFormat VertexBuffer2<double >::GetDataType()const{return FMT_RG64F; }
typedef VertexBuffer3<int8 > VB3i8 ,VB3b; template<> inline VkFormat VertexBuffer3<int8 >::GetDataType()const{return FMT_RGB8I; }
typedef VertexBuffer3<int16 > VB3i16 ,VB3s; template<> inline VkFormat VertexBuffer3<int16 >::GetDataType()const{return FMT_RGB16I; }
// typedef VertexBuffer3<int8 > VB3i8 ,VB3b; template<> inline VkFormat VertexBuffer3<int8 >::GetDataType()const{return FMT_RGB8I; }
// typedef VertexBuffer3<int16 > VB3i16 ,VB3s; template<> inline VkFormat VertexBuffer3<int16 >::GetDataType()const{return FMT_RGB16I; }
typedef VertexBuffer3<int32 > VB3i32 ,VB3i; template<> inline VkFormat VertexBuffer3<int32 >::GetDataType()const{return FMT_RGB32I; }
typedef VertexBuffer3<uint8 > VB3u8 ,VB3ub; template<> inline VkFormat VertexBuffer3<uint8 >::GetDataType()const{return FMT_RGB8U; }
typedef VertexBuffer3<uint16> VB3u16 ,VB3us; template<> inline VkFormat VertexBuffer3<uint16 >::GetDataType()const{return FMT_RGB16U; }
// typedef VertexBuffer3<uint8 > VB3u8 ,VB3ub; template<> inline VkFormat VertexBuffer3<uint8 >::GetDataType()const{return FMT_RGB8U; }
// typedef VertexBuffer3<uint16> VB3u16 ,VB3us; template<> inline VkFormat VertexBuffer3<uint16 >::GetDataType()const{return FMT_RGB16U; }
typedef VertexBuffer3<uint32> VB3u32 ,VB3ui; template<> inline VkFormat VertexBuffer3<uint32 >::GetDataType()const{return FMT_RGB32U; }
typedef VertexBuffer3<float > VB3f; template<> inline VkFormat VertexBuffer3<float >::GetDataType()const{return FMT_RGB32F; }
typedef VertexBuffer3<double> VB3d; template<> inline VkFormat VertexBuffer3<double >::GetDataType()const{return FMT_RGB64F; }

View File

@ -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: //内存相关

View File

@ -1,26 +1,11 @@
#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE
#define HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE
#include<hgl/graph/Vulkan/VK.h>
#include<hgl/graph/vulkan/VKSwapchainAttribute.h>
#include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKFence.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
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<Texture2D> sc_color;
Texture2D * sc_depth =nullptr;
};//struct SwapchainAttribute
class Swapchain
{
protected:
@ -41,9 +26,6 @@ protected:
RenderPass *main_rp =nullptr;
Semaphore *present_complete_semaphore =nullptr,
*render_complete_semaphore =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<VkCommandBuffer> &cmd_list,List<VkSemaphore> &wait_sems,List<VkSemaphore> &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

View File

@ -1,7 +1,8 @@
#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE
#define HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE
#include<hgl/graph/Vulkan/VK.h>
#include<hgl/graph/vulkan/VK.h>
#include<hgl/type/List.h>
VK_NAMESPACE_BEGIN
struct SwapchainAttribute
{

View File

@ -4,7 +4,7 @@
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
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)
{

View File

@ -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;

View File

@ -10,47 +10,20 @@ Swapchain::Swapchain(Device *dev,SwapchainAttribute *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);
@ -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,&current_frame)==VK_SUCCESS)
VkSemaphore sem=*present_complete_semaphore;
if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,sem,VK_NULL_HANDLE,&current_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<VkCommandBuffer> &cmd_lists,List<VkSemaphore> &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=&current_frame;
VkResult result=vkQueuePresentKHR(sc_attr->graphics_queue,&present_info);