From 9d8da06a3eaae01b029ef280adfe7a03e3f2c188 Mon Sep 17 00:00:00 2001 From: HuYingzhuo Date: Sun, 28 Apr 2019 17:47:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4PipelineLayout=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E6=95=B0=E6=8D=AE=E5=B9=B6=E5=85=A5Pipeline=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/CMakeLists.txt | 2 - example/Vulkan/VKCommandBuffer.cpp | 12 ++---- example/Vulkan/VKMaterial.cpp | 30 ++++++++++++++- example/Vulkan/VKMaterial.h | 13 ++++--- example/Vulkan/VKPipeline.cpp | 41 ++++++--------------- example/Vulkan/VKPipeline.h | 17 ++++++--- example/Vulkan/VKPipelineLayout.cpp | 31 ---------------- example/Vulkan/VKPipelineLayout.h | 32 ---------------- example/Vulkan/VKVertexAttributeBinding.cpp | 2 - example/Vulkan/main.cpp | 17 ++------- 10 files changed, 65 insertions(+), 132 deletions(-) delete mode 100644 example/Vulkan/VKPipelineLayout.cpp delete mode 100644 example/Vulkan/VKPipelineLayout.h diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index 9800acfd..0bafc249 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -19,7 +19,6 @@ SET(VULKAN_TEST_SOURCE_FILES main.cpp VKDeviceBuffer.cpp VKBuffer.cpp VKDescriptorSets.cpp - VKPipelineLayout.cpp VKRenderPass.cpp VKShaderModule.cpp VKShaderModuleManage.cpp @@ -45,7 +44,6 @@ SET(VULKAN_TEST_HEADER_FILES AssetsManage.h VKDevice.h VKBuffer.h VKDescriptorSets.h - VKPipelineLayout.h VKRenderPass.h VKShaderModule.h VKShaderModuleManage.h diff --git a/example/Vulkan/VKCommandBuffer.cpp b/example/Vulkan/VKCommandBuffer.cpp index 774a40af..15c112b9 100644 --- a/example/Vulkan/VKCommandBuffer.cpp +++ b/example/Vulkan/VKCommandBuffer.cpp @@ -2,7 +2,6 @@ #include"VKRenderPass.h" #include"VKFramebuffer.h" #include"VKPipeline.h" -#include"VKPipelineLayout.h" #include"VKBuffer.h" #include"VKRenderable.h" #include"VKDescriptorSets.h" @@ -72,15 +71,10 @@ bool CommandBuffer::Bind(Pipeline *p) { if(!p)return(false); - vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, *p); - return(true); -} + if(p->GetDescriptorSetCount()>0) + vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, p->GetLayout(), 0, p->GetDescriptorSetCount(),p->GetDescriptorSets(), 0, nullptr); -bool CommandBuffer::Bind(PipelineLayout *pl) -{ - if(!pl)return(false); - if(pl->GetDescriptorSetCount()>0) - vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, *pl, 0, pl->GetDescriptorSetCount(),pl->GetDescriptorSets(), 0, nullptr); + vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,*p); return(true); } diff --git a/example/Vulkan/VKMaterial.cpp b/example/Vulkan/VKMaterial.cpp index 098df925..0a2a1062 100644 --- a/example/Vulkan/VKMaterial.cpp +++ b/example/Vulkan/VKMaterial.cpp @@ -96,19 +96,40 @@ MaterialInstance *Material::CreateInstance()const DescriptorSetLayout *dsl=dsl_creater->Create(); - return(new MaterialInstance(this,vertex_sm,vab,dsl)); + const uint32_t layout_count=dsl->GetCount(); + const VkDescriptorSetLayout *layouts=(layout_count>0?dsl->GetLayouts():nullptr); + + VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {}; + pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + pPipelineLayoutCreateInfo.pNext = nullptr; + pPipelineLayoutCreateInfo.pushConstantRangeCount = 0; + pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr; + pPipelineLayoutCreateInfo.setLayoutCount = layout_count; + pPipelineLayoutCreateInfo.pSetLayouts = layouts; + + VkPipelineLayout pipeline_layout; + + if(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipeline_layout)!=VK_SUCCESS) + return(nullptr); + + return(new MaterialInstance(device,this,vertex_sm,vab,dsl,pipeline_layout)); } -MaterialInstance::MaterialInstance(const Material *m,const VertexShaderModule *vsm,VertexAttributeBinding *v,DescriptorSetLayout *d) +MaterialInstance::MaterialInstance(VkDevice dev,const Material *m,const VertexShaderModule *vsm,VertexAttributeBinding *v,DescriptorSetLayout *d,VkPipelineLayout pl) { + device=dev; mat=m; vertex_sm=vsm; vab=v; desc_set_layout=d; + pipeline_layout=pl; } MaterialInstance::~MaterialInstance() { + if(pipeline_layout) + vkDestroyPipelineLayout(device,pipeline_layout,nullptr); + delete desc_set_layout; delete vab; } @@ -123,6 +144,11 @@ void MaterialInstance::Write(VkPipelineVertexInputStateCreateInfo &vis)const return vab->Write(vis); } +const List *MaterialInstance::GetDescriptorSets()const +{ + return &(desc_set_layout->GetSets()); +} + Renderable *MaterialInstance::CreateRenderable() { return(new Renderable(vertex_sm)); diff --git a/example/Vulkan/VKMaterial.h b/example/Vulkan/VKMaterial.h index 7b019d04..7d99b5bd 100644 --- a/example/Vulkan/VKMaterial.h +++ b/example/Vulkan/VKMaterial.h @@ -14,6 +14,7 @@ class MaterialInstance; class VertexAttributeBinding; class VertexBuffer; class Renderable; +class PipelineLayout; using ShaderModuleMap=hgl::Map; @@ -50,14 +51,17 @@ public: */ class MaterialInstance { + VkDevice device; const Material *mat; ///<这里的是对material的完全引用,不做任何修改 const VertexShaderModule *vertex_sm; VertexAttributeBinding *vab; DescriptorSetLayout *desc_set_layout; + VkPipelineLayout pipeline_layout; + public: - MaterialInstance(const Material *m,const VertexShaderModule *,VertexAttributeBinding *v,DescriptorSetLayout *d); + MaterialInstance(VkDevice dev,const Material *m,const VertexShaderModule *,VertexAttributeBinding *v,DescriptorSetLayout *d,VkPipelineLayout pl); ~MaterialInstance(); bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info); @@ -68,15 +72,14 @@ public: return UpdateUBO(mat->GetUBOBinding(name),buf_info); } - - const VertexShaderModule * GetVertexShader ()const{return vertex_sm;} - + const uint32_t GetStageCount ()const{return mat->GetStageCount();} const VkPipelineShaderStageCreateInfo * GetStages ()const{return mat->GetStages();} void Write(VkPipelineVertexInputStateCreateInfo &vis)const; - DescriptorSetLayout * GetDSL(){return desc_set_layout;} + const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;} + const List * GetDescriptorSets ()const; Renderable *CreateRenderable(); };//class MaterialInstance diff --git a/example/Vulkan/VKPipeline.cpp b/example/Vulkan/VKPipeline.cpp index ebd4d501..21601d81 100644 --- a/example/Vulkan/VKPipeline.cpp +++ b/example/Vulkan/VKPipeline.cpp @@ -9,14 +9,21 @@ Pipeline::~Pipeline() vkDestroyPipeline(device,pipeline,nullptr); } -PipelineCreater::PipelineCreater(Device *dev,RenderPass *rp) +PipelineCreater::PipelineCreater(Device *dev,const MaterialInstance *mi,RenderPass *rp) { device=dev->GetDevice(); extent=dev->GetExtent(); cache=dev->GetPipelineCache(); + material=mi; + + //未来这里需要增加是否有vs/fs的检测 + hgl_zero(pipelineInfo); pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + pipelineInfo.layout = material->GetPipelineLayout(); + pipelineInfo.stageCount=material->GetStageCount(); + pipelineInfo.pStages=material->GetStages(); { if(!rp) @@ -31,11 +38,7 @@ PipelineCreater::PipelineCreater(Device *dev,RenderPass *rp) vis_create_info.pNext = nullptr; vis_create_info.flags = 0; - vis_create_info.vertexBindingDescriptionCount = 0; - vis_create_info.pVertexBindingDescriptions = nullptr; - - vis_create_info.vertexAttributeDescriptionCount = 0; - vis_create_info.pVertexAttributeDescriptions = nullptr; + material->Write(vis_create_info); pipelineInfo.pVertexInputState=&vis_create_info; } @@ -147,20 +150,6 @@ PipelineCreater::PipelineCreater(Device *dev,RenderPass *rp) pipelineInfo.basePipelineIndex = 0; } -bool PipelineCreater::Set(const MaterialInstance *mi) -{ - if(!mi)return(false); - - //未来这里需要增加是否有vs/fs的检测 - - pipelineInfo.stageCount=mi->GetStageCount(); - pipelineInfo.pStages=mi->GetStages(); - - mi->Write(vis_create_info); - - return(true); -} - bool PipelineCreater::Set(const VkPrimitiveTopology topology,bool restart) { if(topologyVK_PRIMITIVE_TOPOLOGY_END_RANGE) @@ -176,14 +165,6 @@ bool PipelineCreater::Set(const VkPrimitiveTopology topology,bool restart) return(true); } -bool PipelineCreater::Set(VkPipelineLayout pl) -{ - if(!pl)return(false); - - pipelineInfo.layout = pl; - return(true); -} - Pipeline *PipelineCreater::Create() { VkPipeline graphicsPipeline; @@ -191,6 +172,8 @@ Pipeline *PipelineCreater::Create() if (vkCreateGraphicsPipelines(device, cache, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) return(nullptr); - return(new Pipeline(device,graphicsPipeline)); + return(new Pipeline(device,graphicsPipeline, + material->GetPipelineLayout(), + material->GetDescriptorSets())); } VK_NAMESPACE_END diff --git a/example/Vulkan/VKPipeline.h b/example/Vulkan/VKPipeline.h index 20d41d09..9e851e4d 100644 --- a/example/Vulkan/VKPipeline.h +++ b/example/Vulkan/VKPipeline.h @@ -13,13 +13,21 @@ class Pipeline { VkDevice device; VkPipeline pipeline; + VkPipelineLayout pipeline_layout; + + const List *desc_sets; public: - Pipeline(VkDevice dev,VkPipeline p){device=dev;pipeline=p;} + Pipeline(VkDevice dev,VkPipeline p,VkPipelineLayout pl,const List *ds){device=dev;pipeline=p;pipeline_layout=pl;desc_sets=ds;} virtual ~Pipeline(); operator VkPipeline(){return pipeline;} + + VkPipelineLayout GetLayout(){return pipeline_layout;} + + const uint32_t GetDescriptorSetCount()const{return desc_sets->GetCount();} + const VkDescriptorSet * GetDescriptorSets()const{return desc_sets->GetData();} };//class GraphicsPipeline class Shader; @@ -52,17 +60,14 @@ class PipelineCreater private: - const Shader * shader =nullptr; - const VertexInput * vertex_input=nullptr; + const MaterialInstance *material=nullptr; public: - PipelineCreater(Device *dev,RenderPass *rp=nullptr); + PipelineCreater(Device *dev,const MaterialInstance *,RenderPass *rp=nullptr); ~PipelineCreater()=default; - bool Set(const MaterialInstance *); bool Set(const VkPrimitiveTopology,bool=false); - bool Set(VkPipelineLayout pl); void SetViewport( float x,float y,float w,float h){viewport.x=x;viewport.y=y;viewport.width=w;viewport.height=h;} void SetDepthRange( float min_depth,float max_depth){viewport.minDepth=min_depth;viewport.maxDepth=max_depth;} diff --git a/example/Vulkan/VKPipelineLayout.cpp b/example/Vulkan/VKPipelineLayout.cpp deleted file mode 100644 index 2bd1d0cd..00000000 --- a/example/Vulkan/VKPipelineLayout.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include"VKPipelineLayout.h" -#include"VKDescriptorSets.h" - -VK_NAMESPACE_BEGIN -PipelineLayout::~PipelineLayout() -{ - if(layout) - vkDestroyPipelineLayout(device,layout,nullptr); -} - -PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl) -{ - const uint32_t layout_count=(dsl?dsl->GetCount():0); - const VkDescriptorSetLayout *layouts=(layout_count>0?dsl->GetLayouts():nullptr); - - VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {}; - pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - pPipelineLayoutCreateInfo.pNext = nullptr; - pPipelineLayoutCreateInfo.pushConstantRangeCount = 0; - pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr; - pPipelineLayoutCreateInfo.setLayoutCount = layout_count; - pPipelineLayoutCreateInfo.pSetLayouts = layouts; - - VkPipelineLayout pipeline_layout; - - if(vkCreatePipelineLayout(dev, &pPipelineLayoutCreateInfo, nullptr, &pipeline_layout)!=VK_SUCCESS) - return(nullptr); - - return(new PipelineLayout(dev,pipeline_layout,dsl->GetSets())); -} -VK_NAMESPACE_END diff --git a/example/Vulkan/VKPipelineLayout.h b/example/Vulkan/VKPipelineLayout.h deleted file mode 100644 index 5057cde2..00000000 --- a/example/Vulkan/VKPipelineLayout.h +++ /dev/null @@ -1,32 +0,0 @@ -#ifndef HGL_GRAPH_VULKAN_PIPELINE_LAYOUT_INCLUDE -#define HGL_GRAPH_VULKAN_PIPELINE_LAYOUT_INCLUDE - -#include"VK.h" -#include"VKDescriptorSets.h" -VK_NAMESPACE_BEGIN -class PipelineLayout -{ - VkDevice device; - VkPipelineLayout layout; - - List desc_sets; - -private: - - friend PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl); - - PipelineLayout(VkDevice dev,VkPipelineLayout pl,const List &ds){device=dev;layout=pl;desc_sets=ds;} - -public: - - ~PipelineLayout(); - - operator VkPipelineLayout (){return layout;} - - const uint32_t GetDescriptorSetCount ()const{return desc_sets.GetCount();} - const VkDescriptorSet * GetDescriptorSets ()const{return desc_sets.GetData();} -};//class PipelineLayout - -PipelineLayout *CreatePipelineLayout(VkDevice dev,const DescriptorSetLayout *dsl); -VK_NAMESPACE_END -#endif//HGL_GRAPH_VULKAN_PIPELINE_LAYOUT_INCLUDE diff --git a/example/Vulkan/VKVertexAttributeBinding.cpp b/example/Vulkan/VKVertexAttributeBinding.cpp index 39d5f70f..add0154c 100644 --- a/example/Vulkan/VKVertexAttributeBinding.cpp +++ b/example/Vulkan/VKVertexAttributeBinding.cpp @@ -70,8 +70,6 @@ bool VertexAttributeBinding::SetOffset(const uint index,const uint32_t offset) void VertexAttributeBinding::Write(VkPipelineVertexInputStateCreateInfo &vis_create_info) const { - vis_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - const uint32_t count=vsm->GetAttrCount(); vis_create_info.vertexBindingDescriptionCount = count; diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 8aa83c64..50e4047c 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -9,7 +9,6 @@ #include"VKRenderable.h" #include"VKDescriptorSets.h" #include"VKRenderPass.h" -#include"VKPipelineLayout.h" #include"VKPipeline.h" #include"VKCommandBuffer.h" #include"VKFormat.h" @@ -131,24 +130,16 @@ int main(int,char **) vulkan::MaterialInstance *mi=material->CreateInstance(); vulkan::Renderable *render_obj=mi->CreateRenderable(); - - vulkan::Buffer *ubo=CreateUBO(device); - - vulkan::PipelineCreater pc(device); - - mi->UpdateUBO("world",*ubo); - CreateVertexBuffer(device,render_obj); - vulkan::PipelineLayout *pl=CreatePipelineLayout(*device,mi->GetDSL()); + vulkan::Buffer *ubo=CreateUBO(device); + mi->UpdateUBO("world",*ubo); + vulkan::PipelineCreater pc(device,mi); pc.SetDepthTest(false); pc.SetDepthWrite(false); pc.CloseCullFace(); - - pc.Set(mi); pc.Set(PRIM_TRIANGLES); - pc.Set(*pl); vulkan::Pipeline *pipeline=pc.Create(); @@ -161,7 +152,6 @@ int main(int,char **) cmd_buf->Begin(device->GetRenderPass(),device->GetFramebuffer(0)); cmd_buf->Bind(pipeline); - cmd_buf->Bind(pl); cmd_buf->Bind(render_obj); cmd_buf->Draw(VERTEX_COUNT); cmd_buf->End(); @@ -177,7 +167,6 @@ int main(int,char **) delete pipeline; - delete pl; delete ubo; delete mi;