新的Vulkan Framework

This commit is contained in:
hyzboy 2019-06-18 00:48:05 +08:00
parent 84bce16954
commit ece292965c
9 changed files with 115 additions and 74 deletions

View File

@ -1,5 +1,5 @@
macro(CreateProject name) macro(CreateProject name)
add_executable(${name} ${ARGN}) add_executable(${name} ${ARGN} VulkanAppFramework.h)
target_link_libraries(${name} ${ULRE}) target_link_libraries(${name} ${ULRE})
endmacro() endmacro()
@ -10,7 +10,7 @@ CreateProject(3.Geometry2D Geometry2D.cpp)
CreateProject(4.Geometry3D Geometry3D.cpp) CreateProject(4.Geometry3D Geometry3D.cpp)
CreateProject(5.SceneTree SceneTree.cpp) CreateProject(5.SceneTree SceneTree.cpp)
CreateProject(6.LoadModel LoadModel.cpp TGATexture.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp) CreateProject(6.LoadModel LoadModel.cpp TGATexture.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp ViewModelFramework.h)
target_link_libraries(6.LoadModel assimp) target_link_libraries(6.LoadModel assimp)
CreateProject(7.InlineGeometryScene InlineGeometryScene.cpp) CreateProject(7.InlineGeometryScene InlineGeometryScene.cpp)

View File

@ -66,7 +66,7 @@ private:
rci.scope.Set(10,10,20,20); rci.scope.Set(10,10,20,20);
ro_rectangle=CreateRectangle(db,material,&rci); ro_rectangle=CreateRenderableRectangle(db,material,&rci);
} }
{ {
@ -76,7 +76,7 @@ private:
rrci.radius=5; rrci.radius=5;
rrci.round_per=5; rrci.round_per=5;
ro_round_rectangle=CreateRoundRectangle(db,material,&rrci); ro_round_rectangle=CreateRenderableRoundRectangle(db,material,&rrci);
} }
{ {
@ -90,7 +90,7 @@ private:
cci.field_count=8; cci.field_count=8;
ro_circle=CreateCircle(db,material,&cci); ro_circle=CreateRenderableCircle(db,material,&cci);
} }
} }
@ -138,7 +138,6 @@ private:
render_root.Add(db->CreateRenderableInstance(pipeline,descriptor_sets,ro_circle)); render_root.Add(db->CreateRenderableInstance(pipeline,descriptor_sets,ro_circle));
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list); BuildCommandBuffer(&render_list);
return(true); return(true);
} }
@ -167,10 +166,16 @@ public:
return(true); return(true);
} }
void Resize(int,int)override void Resize(int w,int h)
{ {
BuildCommandBuffer(&render_list); BuildCommandBuffer(&render_list);
} }
void Draw()
{
AcquireNextImage();
VulkanApplicationFramework::Draw();
}
};//class TestApp:public VulkanApplicationFramework };//class TestApp:public VulkanApplicationFramework
int main(int,char **) int main(int,char **)

View File

@ -61,16 +61,16 @@ private:
pgci.color.Set(0.75,0,0,1); pgci.color.Set(0.75,0,0,1);
pgci.side_color.Set(1,0,0,1); pgci.side_color.Set(1,0,0,1);
ro_plane_grid[0]=CreatePlaneGrid(db,material,&pgci); ro_plane_grid[0]=CreateRenderablePlaneGrid(db,material,&pgci);
pgci.color.Set(0,0.75,0,1); pgci.color.Set(0,0.75,0,1);
pgci.side_color.Set(0,1,0,1); pgci.side_color.Set(0,1,0,1);
ro_plane_grid[1]=CreatePlaneGrid(db,material,&pgci); ro_plane_grid[1]=CreateRenderablePlaneGrid(db,material,&pgci);
pgci.color.Set(0,0,0.75,1); pgci.color.Set(0,0,0.75,1);
pgci.side_color.Set(0,0,1,1); pgci.side_color.Set(0,0,1,1);
ro_plane_grid[2]=CreatePlaneGrid(db,material,&pgci); ro_plane_grid[2]=CreateRenderablePlaneGrid(db,material,&pgci);
} }
bool InitUBO() bool InitUBO()
@ -114,7 +114,6 @@ private:
render_root.RefreshMatrix(); render_root.RefreshMatrix();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
return(true); return(true);
} }
@ -142,9 +141,9 @@ public:
return(true); return(true);
} }
void Resize(int,int)override void BuildCommandBuffer(uint32 index)
{ {
BuildCommandBuffer(&render_list); VulkanApplicationFramework::BuildCommandBuffer(index,&render_list);
} }
};//class TestApp:public CameraAppFramework };//class TestApp:public CameraAppFramework

View File

@ -178,7 +178,6 @@ private:
render_root.RefreshMatrix(); render_root.RefreshMatrix();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
return(true); return(true);
} }
@ -206,9 +205,13 @@ public:
return(true); return(true);
} }
void Resize(int,int)override void BuildCommandBuffer(uint32_t index) override
{ {
BuildCommandBuffer(&render_list); render_root.RefreshMatrix();
render_list.Clear();
render_root.ExpendToList(&render_list);
VulkanApplicationFramework::BuildCommandBuffer(index,&render_list);
} }
};//class TestApp:public CameraAppFramework };//class TestApp:public CameraAppFramework

View File

@ -58,10 +58,9 @@ private:
void CreateRenderObject() void CreateRenderObject()
{ {
//struct CubeCreateInfo cci; struct CubeCreateInfo cci;
//renderable_object=CreateCube(db,material,&cci); renderable_object=CreateRenderableCube(db,material,&cci);
renderable_object=CreateRenderableSphere(db,material,16);
} }
bool InitUBO() bool InitUBO()
@ -121,8 +120,6 @@ private:
render_root.RefreshMatrix(); render_root.RefreshMatrix();
render_list.Clear(); render_list.Clear();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
return(true); return(true);
} }
@ -159,15 +156,11 @@ public:
render_root.RefreshMatrix(&rot); render_root.RefreshMatrix(&rot);
render_list.Clear(); render_list.Clear();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list);
} }
void Resize(int w,int h)override void BuildCommandBuffer(uint32 index)
{ {
CameraAppFramework::Resize(w,h); VulkanApplicationFramework::BuildCommandBuffer(index,&render_list);
BuildCommandBuffer(&render_list);
} }
};//class TestApp:public CameraAppFramework };//class TestApp:public CameraAppFramework

View File

@ -82,6 +82,8 @@ public:
virtual void Draw()override virtual void Draw()override
{ {
const uint32_t index=AcquireNextImage();
camera.Refresh(); //更新相机矩阵 camera.Refresh(); //更新相机矩阵
ubo_world_matrix->Write(&camera.matrix); //写入缓冲区 ubo_world_matrix->Write(&camera.matrix); //写入缓冲区
@ -89,9 +91,9 @@ public:
render_list.Clear(); render_list.Clear();
render_root.ExpendToList(&render_list); render_root.ExpendToList(&render_list);
BuildCommandBuffer(&render_list); BuildCommandBuffer(index,&render_list);
VulkanApplicationFramework::Draw(); SubmitDraw(index);
if(key_status[kbW])camera.Forward (move_speed);else if(key_status[kbW])camera.Forward (move_speed);else
if(key_status[kbS])camera.Backward (move_speed);else if(key_status[kbS])camera.Backward (move_speed);else

View File

@ -149,60 +149,91 @@ public:
} }
} }
void BuildCommandBuffer(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r) void BuildCommandBuffer(uint32_t index,vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r)
{ {
if(!p||!ds||!r) if(!p||!ds||!r)
return; return;
const vulkan::IndexBuffer *ib=r->GetIndexBuffer(); const vulkan::IndexBuffer *ib=r->GetIndexBuffer();
for(uint i=0;i<swap_chain_count;i++) vulkan::CommandBuffer *cb=cmd_buf[index];
{
cmd_buf[i]->Begin(); cb->Begin();
cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i)); cb->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(index));
cmd_buf[i]->Bind(p); cb->Bind(p);
cmd_buf[i]->Bind(ds); cb->Bind(ds);
cmd_buf[i]->Bind(r); cb->Bind(r);
if (ib) if (ib)
cmd_buf[i]->DrawIndexed(ib->GetCount()); cb->DrawIndexed(ib->GetCount());
else else
cmd_buf[i]->Draw(r->GetDrawCount()); cb->Draw(r->GetDrawCount());
cmd_buf[i]->EndRenderPass(); cb->EndRenderPass();
cmd_buf[i]->End(); cb->End();
} }
void BuildCommandBuffer(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r)
{
for(uint32_t i=0;i<swap_chain_count;i++)
BuildCommandBuffer(i,p,ds,r);
}
void BuildCurrentCommandBuffer(vulkan::Pipeline *p,vulkan::DescriptorSets *ds,vulkan::Renderable *r)
{
BuildCommandBuffer(device->GetCurrentFrameIndices(),p,ds,r);
}
void BuildCommandBuffer(uint32_t index,RenderList *rl)
{
if(!rl)return;
vulkan::CommandBuffer *cb=cmd_buf[index];
cb->Begin();
cb->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(index));
rl->Render(cb);
cb->EndRenderPass();
cb->End();
} }
void BuildCommandBuffer(RenderList *rl) void BuildCommandBuffer(RenderList *rl)
{ {
if(!rl)return; for(uint32_t i=0;i<swap_chain_count;i++)
BuildCommandBuffer(i,rl);
for(uint i=0;i<swap_chain_count;i++)
{
cmd_buf[i]->Begin();
cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i));
rl->Render(cmd_buf[i]);
cmd_buf[i]->EndRenderPass();
cmd_buf[i]->End();
} }
void BuildCurrentCommandBuffer(RenderList *rl)
{
BuildCommandBuffer(device->GetCurrentFrameIndices(),rl);
} }
public: public:
virtual void Draw() int AcquireNextImage()
{ {
device->Wait(); if(device->Wait())
device->AcquireNextImage(); if(device->AcquireNextImage())
return device->GetCurrentFrameIndices();
uint32_t index=device->GetCurrentFrameIndices(); return -1;
}
void SubmitDraw(int index)
{
VkCommandBuffer cb=*cmd_buf[index]; VkCommandBuffer cb=*cmd_buf[index];
device->SubmitDraw(&cb); device->SubmitDraw(&cb);
device->QueuePresent(); device->QueuePresent();
} }
virtual void Draw()
{
int index=AcquireNextImage();
SubmitDraw(index);
}
bool Run() bool Run()
{ {
if(!win->Update())return(false); if(!win->Update())return(false);
@ -267,12 +298,18 @@ public:
return desc_set->BindUBO(world_matrix_bindpoint,*ubo_world_matrix); return desc_set->BindUBO(world_matrix_bindpoint,*ubo_world_matrix);
} }
virtual void BuildCommandBuffer(uint32_t index)=0;
virtual void Draw()override virtual void Draw()override
{ {
const uint32_t index=AcquireNextImage();
camera.Refresh(); //更新相机矩阵 camera.Refresh(); //更新相机矩阵
ubo_world_matrix->Write(&camera.matrix); //写入缓冲区 ubo_world_matrix->Write(&camera.matrix); //写入缓冲区
VulkanApplicationFramework::Draw(); BuildCommandBuffer(index);
SubmitDraw(index);
if(key_status[kbW])camera.Forward (move_speed);else if(key_status[kbW])camera.Forward (move_speed);else
if(key_status[kbS])camera.Backward (move_speed);else if(key_status[kbS])camera.Backward (move_speed);else

View File

@ -245,8 +245,22 @@ ShaderModuleManage *Device::CreateShaderModuleManage()
return(new ShaderModuleManage(this)); return(new ShaderModuleManage(this));
} }
bool Device::Wait(bool wait_all,uint64_t time_out)
{
VkFence fence=*fence_list[current_fence];
VkResult result;
result=vkWaitForFences(attr->device,1,&fence,wait_all,time_out);
result=vkResetFences(attr->device,1,&fence);
return(true);
}
bool Device::AcquireNextImage() bool Device::AcquireNextImage()
{ {
//VkFence fence=*fence_list[current_fence];
return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,*present_complete_semaphore,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS); return(vkAcquireNextImageKHR(attr->device,attr->swap_chain,UINT64_MAX,*present_complete_semaphore,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS);
} }
@ -262,20 +276,10 @@ bool Device::SubmitDraw(const VkCommandBuffer *cmd_bufs,const uint32_t count)
VkResult result=vkQueueSubmit(attr->graphics_queue,1,&submit_info,fence); VkResult result=vkQueueSubmit(attr->graphics_queue,1,&submit_info,fence);
return(result==VK_SUCCESS);
}
bool Device::Wait(bool wait_all,uint64_t time_out)
{
VkFence fence=*fence_list[current_fence];
vkWaitForFences(attr->device,1,&fence,wait_all,time_out);
vkResetFences(attr->device,1,&fence);
if(++current_fence==swap_chain_count) if(++current_fence==swap_chain_count)
current_fence=0; current_fence=0;
return(true); return(result==VK_SUCCESS);
} }
bool Device::QueuePresent() bool Device::QueuePresent()

View File

@ -157,8 +157,6 @@ namespace hgl
{ {
GeometryCreater2D gc(db,mtl); GeometryCreater2D gc(db,mtl);
if(rci->radius==0||rci->round_per<=1) //这是要画矩形
if(rci->radius==0||rci->round_per<=1) //这是要画矩形 if(rci->radius==0||rci->round_per<=1) //这是要画矩形
{ {
if(!gc.Init(4)) if(!gc.Init(4))