From e96bfe9c4b3a5c5b00d176db93674e3dedcd02c3 Mon Sep 17 00:00:00 2001 From: HuYingzhuo Date: Sun, 28 Apr 2019 21:25:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E5=92=8C=E4=BF=AE=E6=AD=A3=E7=A8=8B=E5=BA=8F=E7=BB=93=E6=9E=84?= =?UTF-8?q?=EF=BC=8CVulkanTest=E5=8F=AF=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/VKCommandBuffer.cpp | 16 ++- example/Vulkan/VKCommandBuffer.h | 4 +- example/Vulkan/VKDescriptorSets.cpp | 87 ++++++-------- example/Vulkan/VKDescriptorSets.h | 41 +++---- example/Vulkan/VKInstance.cpp | 24 +++- example/Vulkan/VKMaterial.cpp | 135 ++++++++++++---------- example/Vulkan/VKMaterial.h | 47 ++------ example/Vulkan/VKPipeline.cpp | 12 +- example/Vulkan/VKPipeline.h | 21 +--- example/Vulkan/VKRenderable.cpp | 2 +- example/Vulkan/VKShaderModuleManage.cpp | 4 +- example/Vulkan/VKVertexAttributeBinding.h | 2 +- example/Vulkan/main.cpp | 13 +-- 13 files changed, 194 insertions(+), 214 deletions(-) diff --git a/example/Vulkan/VKCommandBuffer.cpp b/example/Vulkan/VKCommandBuffer.cpp index 1743330a..493773ba 100644 --- a/example/Vulkan/VKCommandBuffer.cpp +++ b/example/Vulkan/VKCommandBuffer.cpp @@ -3,6 +3,7 @@ #include"VKFramebuffer.h" #include"VKPipeline.h" #include"VKBuffer.h" +#include"VKMaterial.h" #include"VKRenderable.h" #include"VKDescriptorSets.h" @@ -71,10 +72,19 @@ bool CommandBuffer::Bind(Pipeline *p) { if(!p)return(false); - vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,*p); + 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(Material *mat) +{ + if(!mat) + return(false); + + const uint32_t count=mat->GetDescriptorSetCount(); + + if(count>0) + vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,mat->GetPipelineLayout(),0,count,mat->GetDescriptorSets(),0,nullptr); return(true); } diff --git a/example/Vulkan/VKCommandBuffer.h b/example/Vulkan/VKCommandBuffer.h index e19dc038..423bed92 100644 --- a/example/Vulkan/VKCommandBuffer.h +++ b/example/Vulkan/VKCommandBuffer.h @@ -6,7 +6,7 @@ VK_NAMESPACE_BEGIN class RenderPass; class Framebuffer; class Pipeline; -class PipelineLayout; +class Material; class Renderable; class CommandBuffer @@ -45,7 +45,7 @@ public: bool Begin(RenderPass *rp,Framebuffer *fb); bool Bind(Pipeline *p); - bool Bind(PipelineLayout *pl); + bool Bind(Material *); bool Bind(Renderable *); bool End(); diff --git a/example/Vulkan/VKDescriptorSets.cpp b/example/Vulkan/VKDescriptorSets.cpp index 18bad06d..ee0c3d3b 100644 --- a/example/Vulkan/VKDescriptorSets.cpp +++ b/example/Vulkan/VKDescriptorSets.cpp @@ -4,60 +4,37 @@ VK_NAMESPACE_BEGIN namespace { - void DestroyDescriptorSetLayout(VkDevice device,List &dsl_list) + void DestroyDescriptorSetLayout(VkDevice device,const int count,VkDescriptorSetLayout *dsl_list) { - const int count=dsl_list.GetCount(); - - if(count>0) + if(count<=0) + return; + + for(int i=0;i0) - // vkFreeDescriptorSets(device->GetDevice(),device->GetDescriptorPool(),count,desc_sets.GetData()); - //} - - DestroyDescriptorSetLayout(*device,desc_set_layout_list); -} - -bool DescriptorSetLayout::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info) +VkDescriptorSet DescriptorSetLayout::GetDescriptorSet(const uint32_t binding) { int index; - - if(!binding_index.Get(binding,index)) - return(false); - VkDescriptorSet set; - if(!desc_sets.Get(index,set)) - return(false); + if(!index_by_binding.Get(binding,index)) + return(nullptr); - VkWriteDescriptorSet writeDescriptorSet = {}; + return desc_set_list[index]; +} - writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - writeDescriptorSet.dstSet = set; - writeDescriptorSet.descriptorCount = 1; - writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - writeDescriptorSet.pBufferInfo = buf_info; - writeDescriptorSet.dstBinding = binding; +DescriptorSetLayout::~DescriptorSetLayout() +{ + //if(count>0) + // vkFreeDescriptorSets(device->GetDevice(),device->GetDescriptorPool(),count,desc_set_list); - vkUpdateDescriptorSets(device->GetDevice(), 1, &writeDescriptorSet, 0, nullptr); - return(true); + delete[] desc_set_list; + DestroyDescriptorSetLayout(*device,count,desc_set_layout_list); + delete[] desc_set_layout_list; } void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags) @@ -71,7 +48,7 @@ void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType de const int index=layout_binding_list.Add(layout_binding); - binding_index.Add(binding,index); + index_by_binding.Add(binding,index); } void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags) @@ -90,7 +67,7 @@ void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t cou p->stageFlags = stageFlags; p->pImmutableSamplers = nullptr; - binding_index.Add(*binding,i); + index_by_binding.Add(*binding,i); ++binding; ++p; @@ -110,29 +87,31 @@ DescriptorSetLayout *DescriptorSetLayoutCreater::Create() descriptor_layout.bindingCount = count; descriptor_layout.pBindings = layout_binding_list.GetData(); - List dsl_list; + VkDescriptorSetLayout *dsl_list=new VkDescriptorSetLayout[count]; - dsl_list.SetCount(count); - if(vkCreateDescriptorSetLayout(device->GetDevice(),&descriptor_layout, nullptr, dsl_list.GetData())!=VK_SUCCESS) + if(vkCreateDescriptorSetLayout(device->GetDevice(),&descriptor_layout, nullptr, dsl_list)!=VK_SUCCESS) + { + delete[] dsl_list; return(nullptr); + } VkDescriptorSetAllocateInfo alloc_info; alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; alloc_info.pNext = nullptr; alloc_info.descriptorPool = device->GetDescriptorPool(); alloc_info.descriptorSetCount = count; - alloc_info.pSetLayouts = dsl_list.GetData(); + alloc_info.pSetLayouts = dsl_list; - List desc_set; + VkDescriptorSet *desc_set=new VkDescriptorSet[count]; - desc_set.SetCount(count); - - if(vkAllocateDescriptorSets(device->GetDevice(), &alloc_info, desc_set.GetData())!=VK_SUCCESS) + if(vkAllocateDescriptorSets(device->GetDevice(),&alloc_info,desc_set)!=VK_SUCCESS) { - DestroyDescriptorSetLayout(*device,dsl_list); + delete[] desc_set; + DestroyDescriptorSetLayout(*device,count,dsl_list); + delete[] dsl_list; return(nullptr); } - return(new DescriptorSetLayout(device,dsl_list,desc_set,binding_index)); + return(new DescriptorSetLayout(device,count,dsl_list,desc_set,index_by_binding)); } VK_NAMESPACE_END diff --git a/example/Vulkan/VKDescriptorSets.h b/example/Vulkan/VKDescriptorSets.h index d5467b64..6a7fa4ad 100644 --- a/example/Vulkan/VKDescriptorSets.h +++ b/example/Vulkan/VKDescriptorSets.h @@ -9,30 +9,32 @@ class Device; class DescriptorSetLayout { Device *device; - List desc_set_layout_list; - List desc_sets; + int count; + VkDescriptorSetLayout *desc_set_layout_list; + VkDescriptorSet *desc_set_list; + Map index_by_binding; - Map binding_index; +private: + + friend class DescriptorSetLayoutCreater; + + DescriptorSetLayout(Device *dev,const int c,VkDescriptorSetLayout *dsl_list,VkDescriptorSet *desc_set,Map &bi) + { + device=dev; + count=c; + desc_set_layout_list=dsl_list; + desc_set_list=desc_set; + index_by_binding=bi; + } public: - DescriptorSetLayout(Device *dev,const List &dsl_list,List &ds_list, - Map &bi) - { - device=dev; - desc_set_layout_list=dsl_list; - desc_sets=ds_list; - binding_index=bi; - } - ~DescriptorSetLayout(); - const uint32_t GetCount ()const{return desc_set_layout_list.GetCount();} - const VkDescriptorSetLayout * GetLayouts ()const{return desc_set_layout_list.GetData();} - - const List & GetSets ()const{return desc_sets;} - - bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info); + const uint32_t GetCount ()const{return count;} + const VkDescriptorSetLayout * GetLayouts ()const{return desc_set_layout_list;} + const VkDescriptorSet * GetDescriptorSets ()const{return desc_set_list;} + VkDescriptorSet GetDescriptorSet (const uint32_t binding); };//class DescriptorSetLayout /** @@ -41,11 +43,10 @@ public: class DescriptorSetLayoutCreater { Device *device; - VkDescriptorSet desc_set; List layout_binding_list; - Map binding_index; + Map index_by_binding; public: diff --git a/example/Vulkan/VKInstance.cpp b/example/Vulkan/VKInstance.cpp index 03e61bf8..cda915a6 100644 --- a/example/Vulkan/VKInstance.cpp +++ b/example/Vulkan/VKInstance.cpp @@ -32,7 +32,27 @@ namespace VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,VkDebugUtilsMessageTypeFlagsEXT messageType,const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,void *pUserData) { - std::cerr<<"validation layer: "<pMessage<pMessage<pMessage<pMessage<pMessage<pMessage<GetCount(); - shader_stage_list.SetCount(shader_count); - VkPipelineShaderStageCreateInfo *p=shader_stage_list.GetData(); + List *shader_stage_list=new List; + + shader_stage_list->SetCount(shader_count); + VkPipelineShaderStageCreateInfo *p=shader_stage_list->GetData(); + + VertexShaderModule *vertex_sm; const ShaderModule *sm; auto **itp=shader_maps->GetDataList(); for(int i=0;iBindUBO(ubo_list.GetData(),ubo_list.GetCount(),sm->GetStage()); } } + + VertexAttributeBinding *vab=vertex_sm->CreateVertexAttributeBinding(); + + DescriptorSetLayout *dsl=dsl_creater->Create(); + + if(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(dev->GetDevice(), &pPipelineLayoutCreateInfo, nullptr, &pipeline_layout)==VK_SUCCESS) + { + return(new Material(dev,shader_maps,vertex_sm,shader_stage_list,dsl_creater,dsl,pipeline_layout,vab)); + } + } + + return(nullptr); +} + +Material::Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List *psci_list,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VkPipelineLayout pl,VertexAttributeBinding *v) +{ + device=*dev; + shader_maps=smm; + vertex_sm=vsm; + shader_stage_list=psci_list; + dsl_creater=dslc; + desc_set_layout=dsl; + pipeline_layout=pl; + vab=v; } Material::~Material() { + delete vab; + + if(pipeline_layout) + vkDestroyPipelineLayout(device,pipeline_layout,nullptr); + + delete desc_set_layout; delete dsl_creater; - const int count=shader_stage_list.GetCount(); + const int count=shader_stage_list->GetCount(); if(count>0) { - VkPipelineShaderStageCreateInfo *ss=shader_stage_list.GetData(); + VkPipelineShaderStageCreateInfo *ss=shader_stage_list->GetData(); for(int i=0;imodule,nullptr); @@ -54,6 +101,7 @@ Material::~Material() } } + delete shader_stage_list; delete shader_maps; } @@ -84,72 +132,39 @@ const int Material::GetVBOBinding(const UTF8String &name)const return vertex_sm->GetBinding(name); } -MaterialInstance *Material::CreateInstance()const +const uint32_t Material::GetDescriptorSetCount()const { - if(!vertex_sm) - return(nullptr); - - VertexAttributeBinding *vab=vertex_sm->CreateVertexAttributeBinding(); - - if(!vab) - return(nullptr); - - DescriptorSetLayout *dsl=dsl_creater->Create(); - - 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)); + return desc_set_layout->GetCount(); } -MaterialInstance::MaterialInstance(VkDevice dev,const Material *m,const VertexShaderModule *vsm,VertexAttributeBinding *v,DescriptorSetLayout *d,VkPipelineLayout pl) +const VkDescriptorSet *Material::GetDescriptorSets()const { - device=dev; - mat=m; - vertex_sm=vsm; - vab=v; - desc_set_layout=d; - pipeline_layout=pl; + return desc_set_layout->GetDescriptorSets(); } -MaterialInstance::~MaterialInstance() +bool Material::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info) { - if(pipeline_layout) - vkDestroyPipelineLayout(device,pipeline_layout,nullptr); + VkDescriptorSet set=desc_set_layout->GetDescriptorSet(binding); - delete desc_set_layout; - delete vab; + VkWriteDescriptorSet writeDescriptorSet = {}; + + writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + writeDescriptorSet.dstSet = set; + writeDescriptorSet.descriptorCount = 1; + writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + writeDescriptorSet.pBufferInfo = buf_info; + writeDescriptorSet.dstBinding = binding; + + vkUpdateDescriptorSets(device,1,&writeDescriptorSet,0,nullptr); + return(true); } -bool MaterialInstance::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info) -{ - return desc_set_layout->UpdateUBO(binding,buf_info); -} - -void MaterialInstance::Write(VkPipelineVertexInputStateCreateInfo &vis)const +void Material::Write(VkPipelineVertexInputStateCreateInfo &vis)const { return vab->Write(vis); } -const List *MaterialInstance::GetDescriptorSets()const -{ - return &(desc_set_layout->GetSets()); -} - -Renderable *MaterialInstance::CreateRenderable() +Renderable *Material::CreateRenderable() { return(new Renderable(vertex_sm)); } diff --git a/example/Vulkan/VKMaterial.h b/example/Vulkan/VKMaterial.h index 7d99b5bd..96716b0a 100644 --- a/example/Vulkan/VKMaterial.h +++ b/example/Vulkan/VKMaterial.h @@ -10,7 +10,6 @@ class ShaderModule; class VertexShaderModule; class DescriptorSetLayoutCreater; class DescriptorSetLayout; -class MaterialInstance; class VertexAttributeBinding; class VertexBuffer; class Renderable; @@ -27,42 +26,26 @@ class Material VkDevice device; ShaderModuleMap *shader_maps; VertexShaderModule *vertex_sm; - List shader_stage_list; - + List *shader_stage_list; DescriptorSetLayoutCreater *dsl_creater; + DescriptorSetLayout *desc_set_layout; + VkPipelineLayout pipeline_layout; + VertexAttributeBinding *vab; public: - Material(Device *dev,ShaderModuleMap *smm); + Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List *,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VkPipelineLayout pl,VertexAttributeBinding *v); ~Material(); const int GetUBOBinding(const UTF8String &)const; const int GetVBOBinding(const UTF8String &)const; - MaterialInstance *CreateInstance()const; + const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;} + const uint32_t GetStageCount ()const{return shader_stage_list->GetCount();} + const VkPipelineShaderStageCreateInfo * GetStages ()const{return shader_stage_list->GetData();} - const uint32_t GetStageCount ()const{return shader_stage_list.GetCount();} - const VkPipelineShaderStageCreateInfo * GetStages ()const{return shader_stage_list.GetData();} -};//class Material - -/** - * 材质实例
- * 用于在指定Material的情况下,具体绑定UBO/TEXTURE等,提供pipeline - */ -class MaterialInstance -{ - VkDevice device; - const Material *mat; ///<这里的是对material的完全引用,不做任何修改 - const VertexShaderModule *vertex_sm; - VertexAttributeBinding *vab; - DescriptorSetLayout *desc_set_layout; - - VkPipelineLayout pipeline_layout; - -public: - - MaterialInstance(VkDevice dev,const Material *m,const VertexShaderModule *,VertexAttributeBinding *v,DescriptorSetLayout *d,VkPipelineLayout pl); - ~MaterialInstance(); + const uint32_t GetDescriptorSetCount ()const; + const VkDescriptorSet * GetDescriptorSets ()const; bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info); bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info) @@ -70,18 +53,12 @@ public: if(name.IsEmpty()||!buf_info) return(false); - return UpdateUBO(mat->GetUBOBinding(name),buf_info); + return UpdateUBO(GetUBOBinding(name),buf_info); } - - const uint32_t GetStageCount ()const{return mat->GetStageCount();} - const VkPipelineShaderStageCreateInfo * GetStages ()const{return mat->GetStages();} void Write(VkPipelineVertexInputStateCreateInfo &vis)const; - const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;} - const List * GetDescriptorSets ()const; - Renderable *CreateRenderable(); -};//class MaterialInstance +};//class Material VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_MATERIAL_INCLUDE diff --git a/example/Vulkan/VKPipeline.cpp b/example/Vulkan/VKPipeline.cpp index 21601d81..cbe67bd2 100644 --- a/example/Vulkan/VKPipeline.cpp +++ b/example/Vulkan/VKPipeline.cpp @@ -9,21 +9,19 @@ Pipeline::~Pipeline() vkDestroyPipeline(device,pipeline,nullptr); } -PipelineCreater::PipelineCreater(Device *dev,const MaterialInstance *mi,RenderPass *rp) +PipelineCreater::PipelineCreater(Device *dev,const Material *material,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(); + pipelineInfo.stageCount = material->GetStageCount(); + pipelineInfo.pStages = material->GetStages(); { if(!rp) @@ -172,8 +170,6 @@ Pipeline *PipelineCreater::Create() if (vkCreateGraphicsPipelines(device, cache, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) return(nullptr); - return(new Pipeline(device,graphicsPipeline, - material->GetPipelineLayout(), - material->GetDescriptorSets())); + return(new Pipeline(device,graphicsPipeline)); } VK_NAMESPACE_END diff --git a/example/Vulkan/VKPipeline.h b/example/Vulkan/VKPipeline.h index 2d15304a..19819e8c 100644 --- a/example/Vulkan/VKPipeline.h +++ b/example/Vulkan/VKPipeline.h @@ -7,32 +7,21 @@ VK_NAMESPACE_BEGIN class Device; class RenderPass; class VertexAttributeBinding; -class MaterialInstance; +class Material; class Pipeline { VkDevice device; VkPipeline pipeline; - VkPipelineLayout pipeline_layout; - - const List *desc_sets; public: - Pipeline(VkDevice dev,VkPipeline p,VkPipelineLayout pl,const List *ds){device=dev;pipeline=p;pipeline_layout=pl;desc_sets=ds;} + Pipeline(VkDevice dev,VkPipeline p){device=dev;pipeline=p;} 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; -class VertexInput; - class PipelineCreater { VkDevice device; @@ -58,13 +47,9 @@ class PipelineCreater VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE]; VkPipelineDynamicStateCreateInfo dynamicState; -private: - - const MaterialInstance *material=nullptr; - public: - PipelineCreater(Device *dev,const MaterialInstance *,RenderPass *rp=nullptr); + PipelineCreater(Device *dev,const Material *,RenderPass *rp=nullptr); ~PipelineCreater()=default; bool Set(const VkPrimitiveTopology,bool=false); diff --git a/example/Vulkan/VKRenderable.cpp b/example/Vulkan/VKRenderable.cpp index 94331947..eaf41542 100644 --- a/example/Vulkan/VKRenderable.cpp +++ b/example/Vulkan/VKRenderable.cpp @@ -29,7 +29,7 @@ bool Renderable::Set(const int binding,VertexBuffer *vbo,VkDeviceSize offset) if(vbo->GetFormat()!=attr->format)return(false); if(vbo->GetStride()!=desc->stride)return(false); - //format信息来自于shader->MaterialInstance,实际中可以不一样。但那样需要为每一个格式产生一个同样shader的material instance,不同的格式又需要不同的pipeline,我们不支持这种行为 + //format信息来自于shader,实际中可以不一样。但那样需要为每一个格式产生一个同样shader的material instance,不同的格式又需要不同的pipeline,我们不支持这种行为 buf_list[binding]=*vbo; buf_offset[binding]=offset; diff --git a/example/Vulkan/VKShaderModuleManage.cpp b/example/Vulkan/VKShaderModuleManage.cpp index a706e257..7a370308 100644 --- a/example/Vulkan/VKShaderModuleManage.cpp +++ b/example/Vulkan/VKShaderModuleManage.cpp @@ -6,6 +6,8 @@ #include"VKShaderParse.h" VK_NAMESPACE_BEGIN +Material *CreateMaterial(Device *dev,ShaderModuleMap *shader_maps); + ShaderModuleManage::~ShaderModuleManage() { const int count=shader_list.GetCount(); @@ -115,6 +117,6 @@ Material *ShaderModuleManage::CreateMaterial(const VertexShaderModule *vertex_sh smm->Add(VK_SHADER_STAGE_VERTEX_BIT,vertex_shader_module); smm->Add(VK_SHADER_STAGE_FRAGMENT_BIT,fragment_shader_module); - return(new Material(device,smm)); + return(VK_NAMESPACE::CreateMaterial(device,smm)); } VK_NAMESPACE_END diff --git a/example/Vulkan/VKVertexAttributeBinding.h b/example/Vulkan/VKVertexAttributeBinding.h index 50ee2724..50270ca4 100644 --- a/example/Vulkan/VKVertexAttributeBinding.h +++ b/example/Vulkan/VKVertexAttributeBinding.h @@ -10,7 +10,7 @@ class VertexShaderModule; /** * 顶点输入状态实例
-* 本对象用于传递给MaterialInstance,用于已经确定好顶点格式的情况下,依然可修改部分设定(如instance)。 +* 本对象用于传递给Material,用于已经确定好顶点格式的情况下,依然可修改部分设定(如instance)。 */ class VertexAttributeBinding { diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 1a547444..ce98218e 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -120,7 +120,6 @@ class TestApp:public VulkanApplicationFramework private: //需释放数据 vulkan::Material * material =nullptr; - vulkan::MaterialInstance * material_instance =nullptr; vulkan::Renderable * render_obj =nullptr; vulkan::Buffer * ubo_mvp =nullptr; @@ -142,7 +141,6 @@ public: SAFE_CLEAR(pipeline_creater); SAFE_CLEAR(ubo_mvp); SAFE_CLEAR(render_obj); - SAFE_CLEAR(material_instance); SAFE_CLEAR(material); } @@ -154,11 +152,7 @@ private: if(!material) return(false); - material_instance=material->CreateInstance(); - if(!material_instance) - return(false); - - render_obj=material_instance->CreateRenderable(); + render_obj=material->CreateRenderable(); return(true); } @@ -173,7 +167,7 @@ private: if(!ubo_mvp) return(false); - return material_instance->UpdateUBO("world",*ubo_mvp); + return material->UpdateUBO("world",*ubo_mvp); } void InitVBO() @@ -187,7 +181,7 @@ private: bool InitPipeline() { - pipeline_creater=new vulkan::PipelineCreater(device,material_instance); + pipeline_creater=new vulkan::PipelineCreater(device,material); pipeline_creater->SetDepthTest(false); pipeline_creater->SetDepthWrite(false); pipeline_creater->CloseCullFace(); @@ -207,6 +201,7 @@ private: cmd_buf->Begin(device->GetRenderPass(),device->GetFramebuffer(0)); cmd_buf->Bind(pipeline); + cmd_buf->Bind(material); cmd_buf->Bind(render_obj); cmd_buf->Draw(VERTEX_COUNT); cmd_buf->End();