进一步简化和修正程序结构,VulkanTest可运行

This commit is contained in:
HuYingzhuo 2019-04-28 21:25:52 +08:00
parent e53a57fe00
commit e96bfe9c4b
13 changed files with 194 additions and 214 deletions

View File

@ -3,6 +3,7 @@
#include"VKFramebuffer.h" #include"VKFramebuffer.h"
#include"VKPipeline.h" #include"VKPipeline.h"
#include"VKBuffer.h" #include"VKBuffer.h"
#include"VKMaterial.h"
#include"VKRenderable.h" #include"VKRenderable.h"
#include"VKDescriptorSets.h" #include"VKDescriptorSets.h"
@ -71,10 +72,19 @@ bool CommandBuffer::Bind(Pipeline *p)
{ {
if(!p)return(false); 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) bool CommandBuffer::Bind(Material *mat)
vkCmdBindDescriptorSets(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, p->GetLayout(), 0, p->GetDescriptorSetCount(),p->GetDescriptorSets(), 0, nullptr); {
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); return(true);
} }

View File

@ -6,7 +6,7 @@ VK_NAMESPACE_BEGIN
class RenderPass; class RenderPass;
class Framebuffer; class Framebuffer;
class Pipeline; class Pipeline;
class PipelineLayout; class Material;
class Renderable; class Renderable;
class CommandBuffer class CommandBuffer
@ -45,7 +45,7 @@ public:
bool Begin(RenderPass *rp,Framebuffer *fb); bool Begin(RenderPass *rp,Framebuffer *fb);
bool Bind(Pipeline *p); bool Bind(Pipeline *p);
bool Bind(PipelineLayout *pl); bool Bind(Material *);
bool Bind(Renderable *); bool Bind(Renderable *);
bool End(); bool End();

View File

@ -4,60 +4,37 @@
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
namespace namespace
{ {
void DestroyDescriptorSetLayout(VkDevice device,List<VkDescriptorSetLayout> &dsl_list) void DestroyDescriptorSetLayout(VkDevice device,const int count,VkDescriptorSetLayout *dsl_list)
{ {
const int count=dsl_list.GetCount(); if(count<=0)
return;
if(count>0) for(int i=0;i<count;i++)
{ {
VkDescriptorSetLayout *dsl=dsl_list.GetData(); vkDestroyDescriptorSetLayout(device,*dsl_list,nullptr);
++dsl_list;
for(int i=0;i<count;i++)
{
vkDestroyDescriptorSetLayout(device,*dsl,nullptr);
++dsl;
}
dsl_list.Clear();
} }
} }
}//namespace }//namespace
DescriptorSetLayout::~DescriptorSetLayout() VkDescriptorSet DescriptorSetLayout::GetDescriptorSet(const uint32_t binding)
{
// 这里注释掉是因为从来不见那里的范例有FREE过但又有vkFreeDescriptorSets这个函数。如发现此注释请使用工具查是否有资源泄露
//{
//const int count=desc_sets.GetCount();
//if(count>0)
// 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)
{ {
int index; int index;
if(!binding_index.Get(binding,index)) if(!index_by_binding.Get(binding,index))
return(false); return(nullptr);
VkDescriptorSet set; return desc_set_list[index];
if(!desc_sets.Get(index,set)) }
return(false);
VkWriteDescriptorSet writeDescriptorSet = {}; DescriptorSetLayout::~DescriptorSetLayout()
{
//if(count>0)
// vkFreeDescriptorSets(device->GetDevice(),device->GetDescriptorPool(),count,desc_set_list);
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; delete[] desc_set_list;
writeDescriptorSet.dstSet = set; DestroyDescriptorSetLayout(*device,count,desc_set_layout_list);
writeDescriptorSet.descriptorCount = 1; delete[] desc_set_layout_list;
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
writeDescriptorSet.pBufferInfo = buf_info;
writeDescriptorSet.dstBinding = binding;
vkUpdateDescriptorSets(device->GetDevice(), 1, &writeDescriptorSet, 0, nullptr);
return(true);
} }
void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags) 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); 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) 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->stageFlags = stageFlags;
p->pImmutableSamplers = nullptr; p->pImmutableSamplers = nullptr;
binding_index.Add(*binding,i); index_by_binding.Add(*binding,i);
++binding; ++binding;
++p; ++p;
@ -110,29 +87,31 @@ DescriptorSetLayout *DescriptorSetLayoutCreater::Create()
descriptor_layout.bindingCount = count; descriptor_layout.bindingCount = count;
descriptor_layout.pBindings = layout_binding_list.GetData(); descriptor_layout.pBindings = layout_binding_list.GetData();
List<VkDescriptorSetLayout> dsl_list; VkDescriptorSetLayout *dsl_list=new VkDescriptorSetLayout[count];
dsl_list.SetCount(count); if(vkCreateDescriptorSetLayout(device->GetDevice(),&descriptor_layout, nullptr, dsl_list)!=VK_SUCCESS)
if(vkCreateDescriptorSetLayout(device->GetDevice(),&descriptor_layout, nullptr, dsl_list.GetData())!=VK_SUCCESS) {
delete[] dsl_list;
return(nullptr); return(nullptr);
}
VkDescriptorSetAllocateInfo alloc_info; VkDescriptorSetAllocateInfo alloc_info;
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
alloc_info.pNext = nullptr; alloc_info.pNext = nullptr;
alloc_info.descriptorPool = device->GetDescriptorPool(); alloc_info.descriptorPool = device->GetDescriptorPool();
alloc_info.descriptorSetCount = count; alloc_info.descriptorSetCount = count;
alloc_info.pSetLayouts = dsl_list.GetData(); alloc_info.pSetLayouts = dsl_list;
List<VkDescriptorSet> desc_set; VkDescriptorSet *desc_set=new VkDescriptorSet[count];
desc_set.SetCount(count); if(vkAllocateDescriptorSets(device->GetDevice(),&alloc_info,desc_set)!=VK_SUCCESS)
if(vkAllocateDescriptorSets(device->GetDevice(), &alloc_info, desc_set.GetData())!=VK_SUCCESS)
{ {
DestroyDescriptorSetLayout(*device,dsl_list); delete[] desc_set;
DestroyDescriptorSetLayout(*device,count,dsl_list);
delete[] dsl_list;
return(nullptr); 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 VK_NAMESPACE_END

View File

@ -9,30 +9,32 @@ class Device;
class DescriptorSetLayout class DescriptorSetLayout
{ {
Device *device; Device *device;
List<VkDescriptorSetLayout> desc_set_layout_list; int count;
List<VkDescriptorSet> desc_sets; VkDescriptorSetLayout *desc_set_layout_list;
VkDescriptorSet *desc_set_list;
Map<uint32_t,int> index_by_binding;
Map<uint32_t,int> binding_index; private:
friend class DescriptorSetLayoutCreater;
DescriptorSetLayout(Device *dev,const int c,VkDescriptorSetLayout *dsl_list,VkDescriptorSet *desc_set,Map<uint32_t,int> &bi)
{
device=dev;
count=c;
desc_set_layout_list=dsl_list;
desc_set_list=desc_set;
index_by_binding=bi;
}
public: public:
DescriptorSetLayout(Device *dev,const List<VkDescriptorSetLayout> &dsl_list,List<VkDescriptorSet> &ds_list,
Map<uint32_t,int> &bi)
{
device=dev;
desc_set_layout_list=dsl_list;
desc_sets=ds_list;
binding_index=bi;
}
~DescriptorSetLayout(); ~DescriptorSetLayout();
const uint32_t GetCount ()const{return desc_set_layout_list.GetCount();} const uint32_t GetCount ()const{return count;}
const VkDescriptorSetLayout * GetLayouts ()const{return desc_set_layout_list.GetData();} const VkDescriptorSetLayout * GetLayouts ()const{return desc_set_layout_list;}
const VkDescriptorSet * GetDescriptorSets ()const{return desc_set_list;}
const List<VkDescriptorSet> & GetSets ()const{return desc_sets;} VkDescriptorSet GetDescriptorSet (const uint32_t binding);
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info);
};//class DescriptorSetLayout };//class DescriptorSetLayout
/** /**
@ -41,11 +43,10 @@ public:
class DescriptorSetLayoutCreater class DescriptorSetLayoutCreater
{ {
Device *device; Device *device;
VkDescriptorSet desc_set;
List<VkDescriptorSetLayoutBinding> layout_binding_list; List<VkDescriptorSetLayoutBinding> layout_binding_list;
Map<uint32_t,int> binding_index; Map<uint32_t,int> index_by_binding;
public: public:

View File

@ -32,7 +32,27 @@ namespace
VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,VkDebugUtilsMessageTypeFlagsEXT messageType,const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,void *pUserData) VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,VkDebugUtilsMessageTypeFlagsEXT messageType,const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,void *pUserData)
{ {
std::cerr<<"validation layer: "<<pCallbackData->pMessage<<std::endl; if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
{
std::cout<<"ERROR: "<<pCallbackData->pMessage<<std::endl;
}
else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT)
{
std::cout<<"WARNING: "<<pCallbackData->pMessage<<std::endl;
}
else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT)
{
std::cout<<"INFO: "<<pCallbackData->pMessage<<std::endl;
}
else
if(messageSeverity&VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT)
{
std::cout<<"VERBOSE: "<<pCallbackData->pMessage<<std::endl;
}
else
std::cerr<<"validation layer: "<<pCallbackData->pMessage<<std::endl;
return VK_FALSE; return VK_FALSE;
} }
@ -133,7 +153,7 @@ Instance *CreateInstance(const UTF8String &app_name)
"VK_LAYER_LUNARG_standard_validation", "VK_LAYER_LUNARG_standard_validation",
"VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_parameter_validation",
// "VK_LAYER_LUNARG_vktrace", // "VK_LAYER_LUNARG_vktrace",
"VK_LAYER_RENDERDOC_Capture", // "VK_LAYER_RENDERDOC_Capture",
// "VK_LAYER_KHRONOS_validation", // "VK_LAYER_KHRONOS_validation",

View File

@ -6,17 +6,19 @@
#include"VKRenderable.h" #include"VKRenderable.h"
#include"VKBuffer.h" #include"VKBuffer.h"
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
Material::Material(Device *dev,ShaderModuleMap *smm) Material *CreateMaterial(Device *dev,ShaderModuleMap *shader_maps)
{ {
device=*dev; DescriptorSetLayoutCreater *dsl_creater=new DescriptorSetLayoutCreater(dev);
shader_maps=smm;
dsl_creater=new DescriptorSetLayoutCreater(dev);
const int shader_count=shader_maps->GetCount(); const int shader_count=shader_maps->GetCount();
shader_stage_list.SetCount(shader_count); List<VkPipelineShaderStageCreateInfo> *shader_stage_list=new List<VkPipelineShaderStageCreateInfo>;
VkPipelineShaderStageCreateInfo *p=shader_stage_list.GetData();
shader_stage_list->SetCount(shader_count);
VkPipelineShaderStageCreateInfo *p=shader_stage_list->GetData();
VertexShaderModule *vertex_sm;
const ShaderModule *sm; const ShaderModule *sm;
auto **itp=shader_maps->GetDataList(); auto **itp=shader_maps->GetDataList();
for(int i=0;i<shader_count;i++) for(int i=0;i<shader_count;i++)
@ -36,17 +38,62 @@ Material::Material(Device *dev,ShaderModuleMap *smm)
dsl_creater->BindUBO(ubo_list.GetData(),ubo_list.GetCount(),sm->GetStage()); dsl_creater->BindUBO(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<VkPipelineShaderStageCreateInfo> *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() Material::~Material()
{ {
delete vab;
if(pipeline_layout)
vkDestroyPipelineLayout(device,pipeline_layout,nullptr);
delete desc_set_layout;
delete dsl_creater; delete dsl_creater;
const int count=shader_stage_list.GetCount(); const int count=shader_stage_list->GetCount();
if(count>0) if(count>0)
{ {
VkPipelineShaderStageCreateInfo *ss=shader_stage_list.GetData(); VkPipelineShaderStageCreateInfo *ss=shader_stage_list->GetData();
for(int i=0;i<count;i++) for(int i=0;i<count;i++)
{ {
vkDestroyShaderModule(device,ss->module,nullptr); vkDestroyShaderModule(device,ss->module,nullptr);
@ -54,6 +101,7 @@ Material::~Material()
} }
} }
delete shader_stage_list;
delete shader_maps; delete shader_maps;
} }
@ -84,72 +132,39 @@ const int Material::GetVBOBinding(const UTF8String &name)const
return vertex_sm->GetBinding(name); return vertex_sm->GetBinding(name);
} }
MaterialInstance *Material::CreateInstance()const const uint32_t Material::GetDescriptorSetCount()const
{ {
if(!vertex_sm) return desc_set_layout->GetCount();
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));
} }
MaterialInstance::MaterialInstance(VkDevice dev,const Material *m,const VertexShaderModule *vsm,VertexAttributeBinding *v,DescriptorSetLayout *d,VkPipelineLayout pl) const VkDescriptorSet *Material::GetDescriptorSets()const
{ {
device=dev; return desc_set_layout->GetDescriptorSets();
mat=m;
vertex_sm=vsm;
vab=v;
desc_set_layout=d;
pipeline_layout=pl;
} }
MaterialInstance::~MaterialInstance() bool Material::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info)
{ {
if(pipeline_layout) VkDescriptorSet set=desc_set_layout->GetDescriptorSet(binding);
vkDestroyPipelineLayout(device,pipeline_layout,nullptr);
delete desc_set_layout; VkWriteDescriptorSet writeDescriptorSet = {};
delete vab;
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) void Material::Write(VkPipelineVertexInputStateCreateInfo &vis)const
{
return desc_set_layout->UpdateUBO(binding,buf_info);
}
void MaterialInstance::Write(VkPipelineVertexInputStateCreateInfo &vis)const
{ {
return vab->Write(vis); return vab->Write(vis);
} }
const List<VkDescriptorSet> *MaterialInstance::GetDescriptorSets()const Renderable *Material::CreateRenderable()
{
return &(desc_set_layout->GetSets());
}
Renderable *MaterialInstance::CreateRenderable()
{ {
return(new Renderable(vertex_sm)); return(new Renderable(vertex_sm));
} }

View File

@ -10,7 +10,6 @@ class ShaderModule;
class VertexShaderModule; class VertexShaderModule;
class DescriptorSetLayoutCreater; class DescriptorSetLayoutCreater;
class DescriptorSetLayout; class DescriptorSetLayout;
class MaterialInstance;
class VertexAttributeBinding; class VertexAttributeBinding;
class VertexBuffer; class VertexBuffer;
class Renderable; class Renderable;
@ -27,42 +26,26 @@ class Material
VkDevice device; VkDevice device;
ShaderModuleMap *shader_maps; ShaderModuleMap *shader_maps;
VertexShaderModule *vertex_sm; VertexShaderModule *vertex_sm;
List<VkPipelineShaderStageCreateInfo> shader_stage_list; List<VkPipelineShaderStageCreateInfo> *shader_stage_list;
DescriptorSetLayoutCreater *dsl_creater; DescriptorSetLayoutCreater *dsl_creater;
DescriptorSetLayout *desc_set_layout;
VkPipelineLayout pipeline_layout;
VertexAttributeBinding *vab;
public: public:
Material(Device *dev,ShaderModuleMap *smm); Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List<VkPipelineShaderStageCreateInfo> *,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VkPipelineLayout pl,VertexAttributeBinding *v);
~Material(); ~Material();
const int GetUBOBinding(const UTF8String &)const; const int GetUBOBinding(const UTF8String &)const;
const int GetVBOBinding(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 uint32_t GetDescriptorSetCount ()const;
const VkPipelineShaderStageCreateInfo * GetStages ()const{return shader_stage_list.GetData();} const VkDescriptorSet * GetDescriptorSets ()const;
};//class Material
/**
* <br>
* 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();
bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info); bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info);
bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info) bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info)
@ -70,18 +53,12 @@ public:
if(name.IsEmpty()||!buf_info) if(name.IsEmpty()||!buf_info)
return(false); 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; void Write(VkPipelineVertexInputStateCreateInfo &vis)const;
const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;}
const List<VkDescriptorSet> * GetDescriptorSets ()const;
Renderable *CreateRenderable(); Renderable *CreateRenderable();
};//class MaterialInstance };//class Material
VK_NAMESPACE_END VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_MATERIAL_INCLUDE #endif//HGL_GRAPH_VULKAN_MATERIAL_INCLUDE

View File

@ -9,21 +9,19 @@ Pipeline::~Pipeline()
vkDestroyPipeline(device,pipeline,nullptr); 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(); device=dev->GetDevice();
extent=dev->GetExtent(); extent=dev->GetExtent();
cache=dev->GetPipelineCache(); cache=dev->GetPipelineCache();
material=mi;
//未来这里需要增加是否有vs/fs的检测 //未来这里需要增加是否有vs/fs的检测
hgl_zero(pipelineInfo); hgl_zero(pipelineInfo);
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.layout = material->GetPipelineLayout(); pipelineInfo.layout = material->GetPipelineLayout();
pipelineInfo.stageCount=material->GetStageCount(); pipelineInfo.stageCount = material->GetStageCount();
pipelineInfo.pStages=material->GetStages(); pipelineInfo.pStages = material->GetStages();
{ {
if(!rp) if(!rp)
@ -172,8 +170,6 @@ Pipeline *PipelineCreater::Create()
if (vkCreateGraphicsPipelines(device, cache, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) if (vkCreateGraphicsPipelines(device, cache, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS)
return(nullptr); return(nullptr);
return(new Pipeline(device,graphicsPipeline, return(new Pipeline(device,graphicsPipeline));
material->GetPipelineLayout(),
material->GetDescriptorSets()));
} }
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -7,32 +7,21 @@ VK_NAMESPACE_BEGIN
class Device; class Device;
class RenderPass; class RenderPass;
class VertexAttributeBinding; class VertexAttributeBinding;
class MaterialInstance; class Material;
class Pipeline class Pipeline
{ {
VkDevice device; VkDevice device;
VkPipeline pipeline; VkPipeline pipeline;
VkPipelineLayout pipeline_layout;
const List<VkDescriptorSet> *desc_sets;
public: public:
Pipeline(VkDevice dev,VkPipeline p,VkPipelineLayout pl,const List<VkDescriptorSet> *ds){device=dev;pipeline=p;pipeline_layout=pl;desc_sets=ds;} Pipeline(VkDevice dev,VkPipeline p){device=dev;pipeline=p;}
virtual ~Pipeline(); virtual ~Pipeline();
operator VkPipeline(){return 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 GraphicsPipeline
class Shader;
class VertexInput;
class PipelineCreater class PipelineCreater
{ {
VkDevice device; VkDevice device;
@ -58,13 +47,9 @@ class PipelineCreater
VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE]; VkDynamicState dynamicStateEnables[VK_DYNAMIC_STATE_RANGE_SIZE];
VkPipelineDynamicStateCreateInfo dynamicState; VkPipelineDynamicStateCreateInfo dynamicState;
private:
const MaterialInstance *material=nullptr;
public: public:
PipelineCreater(Device *dev,const MaterialInstance *,RenderPass *rp=nullptr); PipelineCreater(Device *dev,const Material *,RenderPass *rp=nullptr);
~PipelineCreater()=default; ~PipelineCreater()=default;
bool Set(const VkPrimitiveTopology,bool=false); bool Set(const VkPrimitiveTopology,bool=false);

View File

@ -29,7 +29,7 @@ bool Renderable::Set(const int binding,VertexBuffer *vbo,VkDeviceSize offset)
if(vbo->GetFormat()!=attr->format)return(false); if(vbo->GetFormat()!=attr->format)return(false);
if(vbo->GetStride()!=desc->stride)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_list[binding]=*vbo;
buf_offset[binding]=offset; buf_offset[binding]=offset;

View File

@ -6,6 +6,8 @@
#include"VKShaderParse.h" #include"VKShaderParse.h"
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
Material *CreateMaterial(Device *dev,ShaderModuleMap *shader_maps);
ShaderModuleManage::~ShaderModuleManage() ShaderModuleManage::~ShaderModuleManage()
{ {
const int count=shader_list.GetCount(); 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_VERTEX_BIT,vertex_shader_module);
smm->Add(VK_SHADER_STAGE_FRAGMENT_BIT,fragment_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 VK_NAMESPACE_END

View File

@ -10,7 +10,7 @@ class VertexShaderModule;
/** /**
* <br> * <br>
* MaterialInstance,(instance) * Material,(instance)
*/ */
class VertexAttributeBinding class VertexAttributeBinding
{ {

View File

@ -120,7 +120,6 @@ class TestApp:public VulkanApplicationFramework
private: //需释放数据 private: //需释放数据
vulkan::Material * material =nullptr; vulkan::Material * material =nullptr;
vulkan::MaterialInstance * material_instance =nullptr;
vulkan::Renderable * render_obj =nullptr; vulkan::Renderable * render_obj =nullptr;
vulkan::Buffer * ubo_mvp =nullptr; vulkan::Buffer * ubo_mvp =nullptr;
@ -142,7 +141,6 @@ public:
SAFE_CLEAR(pipeline_creater); SAFE_CLEAR(pipeline_creater);
SAFE_CLEAR(ubo_mvp); SAFE_CLEAR(ubo_mvp);
SAFE_CLEAR(render_obj); SAFE_CLEAR(render_obj);
SAFE_CLEAR(material_instance);
SAFE_CLEAR(material); SAFE_CLEAR(material);
} }
@ -154,11 +152,7 @@ private:
if(!material) if(!material)
return(false); return(false);
material_instance=material->CreateInstance(); render_obj=material->CreateRenderable();
if(!material_instance)
return(false);
render_obj=material_instance->CreateRenderable();
return(true); return(true);
} }
@ -173,7 +167,7 @@ private:
if(!ubo_mvp) if(!ubo_mvp)
return(false); return(false);
return material_instance->UpdateUBO("world",*ubo_mvp); return material->UpdateUBO("world",*ubo_mvp);
} }
void InitVBO() void InitVBO()
@ -187,7 +181,7 @@ private:
bool InitPipeline() bool InitPipeline()
{ {
pipeline_creater=new vulkan::PipelineCreater(device,material_instance); pipeline_creater=new vulkan::PipelineCreater(device,material);
pipeline_creater->SetDepthTest(false); pipeline_creater->SetDepthTest(false);
pipeline_creater->SetDepthWrite(false); pipeline_creater->SetDepthWrite(false);
pipeline_creater->CloseCullFace(); pipeline_creater->CloseCullFace();
@ -207,6 +201,7 @@ private:
cmd_buf->Begin(device->GetRenderPass(),device->GetFramebuffer(0)); cmd_buf->Begin(device->GetRenderPass(),device->GetFramebuffer(0));
cmd_buf->Bind(pipeline); cmd_buf->Bind(pipeline);
cmd_buf->Bind(material);
cmd_buf->Bind(render_obj); cmd_buf->Bind(render_obj);
cmd_buf->Draw(VERTEX_COUNT); cmd_buf->Draw(VERTEX_COUNT);
cmd_buf->End(); cmd_buf->End();