2019-04-30 16:42:59 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKMaterial.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKDescriptorSets.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKShaderModule.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKVertexAttributeBinding.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKRenderable.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKBuffer.h>
|
2019-04-25 10:09:56 +08:00
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-04-28 21:25:52 +08:00
|
|
|
|
Material *CreateMaterial(Device *dev,ShaderModuleMap *shader_maps)
|
2019-04-27 02:07:06 +08:00
|
|
|
|
{
|
2019-04-28 21:25:52 +08:00
|
|
|
|
DescriptorSetLayoutCreater *dsl_creater=new DescriptorSetLayoutCreater(dev);
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
|
|
|
|
const int shader_count=shader_maps->GetCount();
|
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
List<VkPipelineShaderStageCreateInfo> *shader_stage_list=new List<VkPipelineShaderStageCreateInfo>;
|
2019-05-05 14:22:58 +08:00
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
shader_stage_list->SetCount(shader_count);
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
VkPipelineShaderStageCreateInfo *p=shader_stage_list->GetData();
|
|
|
|
|
|
|
|
|
|
VertexShaderModule *vertex_sm;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
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-28 21:25:52 +08:00
|
|
|
|
|
|
|
|
|
VertexAttributeBinding *vab=vertex_sm->CreateVertexAttributeBinding();
|
|
|
|
|
|
|
|
|
|
DescriptorSetLayout *dsl=dsl_creater->Create();
|
|
|
|
|
|
|
|
|
|
if(dsl)
|
|
|
|
|
{
|
2019-05-05 18:00:26 +08:00
|
|
|
|
return(new Material(dev,shader_maps,vertex_sm,shader_stage_list,dsl_creater,dsl,vab));
|
2019-04-28 21:25:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-05 18:00:26 +08:00
|
|
|
|
Material::Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List<VkPipelineShaderStageCreateInfo> *psci_list,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VertexAttributeBinding *v)
|
2019-04-28 21:25:52 +08:00
|
|
|
|
{
|
|
|
|
|
device=*dev;
|
|
|
|
|
shader_maps=smm;
|
|
|
|
|
vertex_sm=vsm;
|
|
|
|
|
shader_stage_list=psci_list;
|
|
|
|
|
dsl_creater=dslc;
|
|
|
|
|
desc_set_layout=dsl;
|
|
|
|
|
vab=v;
|
2019-04-27 02:07:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-25 10:09:56 +08:00
|
|
|
|
Material::~Material()
|
|
|
|
|
{
|
2019-04-28 21:25:52 +08:00
|
|
|
|
delete vab;
|
2019-05-05 18:00:26 +08:00
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
delete desc_set_layout;
|
2019-04-27 02:07:06 +08:00
|
|
|
|
delete dsl_creater;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
const int count=shader_stage_list->GetCount();
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
|
|
|
|
if(count>0)
|
|
|
|
|
{
|
2019-04-28 21:25:52 +08:00
|
|
|
|
VkPipelineShaderStageCreateInfo *ss=shader_stage_list->GetData();
|
2019-04-28 16:06:53 +08:00
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyShaderModule(device,ss->module,nullptr);
|
|
|
|
|
++ss;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
delete shader_stage_list;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
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-05-05 18:00:26 +08:00
|
|
|
|
const VkPipelineLayout Material::GetPipelineLayout()const
|
|
|
|
|
{
|
|
|
|
|
return desc_set_layout->GetPipelineLayout();
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
const uint32_t Material::GetDescriptorSetCount()const
|
2019-04-28 16:06:53 +08:00
|
|
|
|
{
|
2019-04-28 21:25:52 +08:00
|
|
|
|
return desc_set_layout->GetCount();
|
2019-04-28 16:06:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
const VkDescriptorSet *Material::GetDescriptorSets()const
|
2019-04-27 01:36:58 +08:00
|
|
|
|
{
|
2019-04-28 21:25:52 +08:00
|
|
|
|
return desc_set_layout->GetDescriptorSets();
|
2019-04-26 21:43:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
bool Material::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info)
|
2019-04-26 21:43:22 +08:00
|
|
|
|
{
|
2019-04-28 21:25:52 +08:00
|
|
|
|
VkDescriptorSet set=desc_set_layout->GetDescriptorSet(binding);
|
2019-04-28 17:47:58 +08:00
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
VkWriteDescriptorSet writeDescriptorSet = {};
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2019-05-05 14:22:58 +08:00
|
|
|
|
writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
2019-04-28 21:25:52 +08:00
|
|
|
|
writeDescriptorSet.dstSet = set;
|
|
|
|
|
writeDescriptorSet.descriptorCount = 1;
|
|
|
|
|
writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
|
|
|
|
writeDescriptorSet.pBufferInfo = buf_info;
|
|
|
|
|
writeDescriptorSet.dstBinding = binding;
|
2019-04-28 16:06:53 +08:00
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
vkUpdateDescriptorSets(device,1,&writeDescriptorSet,0,nullptr);
|
|
|
|
|
return(true);
|
2019-04-25 10:09:56 +08:00
|
|
|
|
}
|
2019-04-28 17:02:38 +08:00
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
void Material::Write(VkPipelineVertexInputStateCreateInfo &vis)const
|
2019-04-28 17:47:58 +08:00
|
|
|
|
{
|
2019-04-28 21:25:52 +08:00
|
|
|
|
return vab->Write(vis);
|
2019-04-28 17:47:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 21:25:52 +08:00
|
|
|
|
Renderable *Material::CreateRenderable()
|
2019-04-28 17:02:38 +08:00
|
|
|
|
{
|
|
|
|
|
return(new Renderable(vertex_sm));
|
|
|
|
|
}
|
2019-04-25 10:09:56 +08:00
|
|
|
|
VK_NAMESPACE_END
|