2019-04-25 10:09:56 +08:00
|
|
|
|
#include"VKMaterial.h"
|
2019-04-28 16:06:53 +08:00
|
|
|
|
#include"VKDevice.h"
|
2019-04-25 21:57:37 +08:00
|
|
|
|
#include"VKDescriptorSets.h"
|
2019-04-28 16:06:53 +08:00
|
|
|
|
#include"VKShaderModule.h"
|
2019-04-27 01:09:27 +08:00
|
|
|
|
#include"VKVertexAttributeBinding.h"
|
2019-04-28 17:02:38 +08:00
|
|
|
|
#include"VKRenderable.h"
|
|
|
|
|
#include"VKBuffer.h"
|
2019-04-25 10:09:56 +08:00
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-04-28 16:06:53 +08:00
|
|
|
|
Material::Material(Device *dev,ShaderModuleMap *smm)
|
2019-04-27 02:07:06 +08:00
|
|
|
|
{
|
2019-04-28 16:06:53 +08:00
|
|
|
|
device=*dev;
|
|
|
|
|
shader_maps=smm;
|
2019-04-27 02:07:06 +08:00
|
|
|
|
dsl_creater=new DescriptorSetLayoutCreater(dev);
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
|
|
|
|
const int shader_count=shader_maps->GetCount();
|
|
|
|
|
|
|
|
|
|
shader_stage_list.SetCount(shader_count);
|
|
|
|
|
VkPipelineShaderStageCreateInfo *p=shader_stage_list.GetData();
|
|
|
|
|
|
|
|
|
|
const ShaderModule *sm;
|
|
|
|
|
auto **itp=shader_maps->GetDataList();
|
|
|
|
|
for(int i=0;i<shader_count;i++)
|
|
|
|
|
{
|
|
|
|
|
sm=(*itp)->right;
|
|
|
|
|
|
|
|
|
|
if(sm->GetStage()==VK_SHADER_STAGE_VERTEX_BIT)
|
|
|
|
|
vertex_sm=(VertexShaderModule *)sm;
|
|
|
|
|
|
|
|
|
|
memcpy(p,sm->GetCreateInfo(),sizeof(VkPipelineShaderStageCreateInfo));
|
|
|
|
|
++p;
|
|
|
|
|
++itp;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
const ShaderBindingList &ubo_list=sm->GetUBOBindingList();
|
|
|
|
|
|
|
|
|
|
dsl_creater->BindUBO(ubo_list.GetData(),ubo_list.GetCount(),sm->GetStage());
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-27 02:07:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 10:09:56 +08:00
|
|
|
|
Material::~Material()
|
|
|
|
|
{
|
2019-04-27 02:07:06 +08:00
|
|
|
|
delete dsl_creater;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
|
|
|
|
const int count=shader_stage_list.GetCount();
|
|
|
|
|
|
|
|
|
|
if(count>0)
|
|
|
|
|
{
|
|
|
|
|
VkPipelineShaderStageCreateInfo *ss=shader_stage_list.GetData();
|
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyShaderModule(device,ss->module,nullptr);
|
|
|
|
|
++ss;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete shader_maps;
|
2019-04-25 10:09:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 16:06:53 +08:00
|
|
|
|
const int Material::GetUBOBinding(const UTF8String &name)const
|
2019-04-25 10:09:56 +08:00
|
|
|
|
{
|
2019-04-28 16:06:53 +08:00
|
|
|
|
int result;
|
|
|
|
|
const int shader_count=shader_maps->GetCount();
|
|
|
|
|
|
|
|
|
|
const ShaderModule *sm;
|
|
|
|
|
auto **itp=shader_maps->GetDataList();
|
|
|
|
|
for(int i=0;i<shader_count;i++)
|
|
|
|
|
{
|
|
|
|
|
sm=(*itp)->right;
|
|
|
|
|
result=sm->GetUBO(name);
|
|
|
|
|
if(result!=-1)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
++itp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(-1);
|
2019-04-27 01:36:58 +08:00
|
|
|
|
}
|
2019-04-26 21:43:22 +08:00
|
|
|
|
|
2019-04-28 17:02:38 +08:00
|
|
|
|
const int Material::GetVBOBinding(const UTF8String &name)const
|
|
|
|
|
{
|
|
|
|
|
if(!vertex_sm)return(-1);
|
|
|
|
|
|
|
|
|
|
return vertex_sm->GetBinding(name);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 16:06:53 +08:00
|
|
|
|
MaterialInstance *Material::CreateInstance()const
|
|
|
|
|
{
|
|
|
|
|
if(!vertex_sm)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
VertexAttributeBinding *vab=vertex_sm->CreateVertexAttributeBinding();
|
|
|
|
|
|
|
|
|
|
if(!vab)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
|
|
|
|
DescriptorSetLayout *dsl=dsl_creater->Create();
|
|
|
|
|
|
2019-04-28 17:47:58 +08:00
|
|
|
|
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));
|
2019-04-28 16:06:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 17:47:58 +08:00
|
|
|
|
MaterialInstance::MaterialInstance(VkDevice dev,const Material *m,const VertexShaderModule *vsm,VertexAttributeBinding *v,DescriptorSetLayout *d,VkPipelineLayout pl)
|
2019-04-27 01:36:58 +08:00
|
|
|
|
{
|
2019-04-28 17:47:58 +08:00
|
|
|
|
device=dev;
|
2019-04-27 01:36:58 +08:00
|
|
|
|
mat=m;
|
2019-04-28 17:02:38 +08:00
|
|
|
|
vertex_sm=vsm;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
vab=v;
|
|
|
|
|
desc_set_layout=d;
|
2019-04-28 17:47:58 +08:00
|
|
|
|
pipeline_layout=pl;
|
2019-04-26 21:43:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MaterialInstance::~MaterialInstance()
|
|
|
|
|
{
|
2019-04-28 17:47:58 +08:00
|
|
|
|
if(pipeline_layout)
|
|
|
|
|
vkDestroyPipelineLayout(device,pipeline_layout,nullptr);
|
|
|
|
|
|
2019-04-28 16:06:53 +08:00
|
|
|
|
delete desc_set_layout;
|
2019-04-27 01:09:27 +08:00
|
|
|
|
delete vab;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
return vab->Write(vis);
|
2019-04-25 10:09:56 +08:00
|
|
|
|
}
|
2019-04-28 17:02:38 +08:00
|
|
|
|
|
2019-04-28 17:47:58 +08:00
|
|
|
|
const List<VkDescriptorSet> *MaterialInstance::GetDescriptorSets()const
|
|
|
|
|
{
|
|
|
|
|
return &(desc_set_layout->GetSets());
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 17:02:38 +08:00
|
|
|
|
Renderable *MaterialInstance::CreateRenderable()
|
|
|
|
|
{
|
|
|
|
|
return(new Renderable(vertex_sm));
|
|
|
|
|
}
|
2019-04-25 10:09:56 +08:00
|
|
|
|
VK_NAMESPACE_END
|