2023-03-17 10:08:41 +08:00
|
|
|
|
#include<hgl/shadergen/MaterialCreateInfo.h>
|
2023-03-17 10:14:07 +08:00
|
|
|
|
#include<hgl/shadergen/ShaderDescriptorInfo.h>
|
2023-05-15 21:52:57 +08:00
|
|
|
|
#include"common/UBOCommon.h"
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
|
2023-05-15 21:52:57 +08:00
|
|
|
|
STD_MTL_NAMESPACE_BEGIN
|
|
|
|
|
MaterialCreateInfo::MaterialCreateInfo(const MaterialConfig *mc)
|
2023-03-03 23:02:40 +08:00
|
|
|
|
{
|
2023-05-15 21:52:57 +08:00
|
|
|
|
config=mc;
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
if(hasVertex ())shader_map.Add(vert=new ShaderCreateInfoVertex (&mdi));else vert=nullptr;
|
|
|
|
|
if(hasGeometry ())shader_map.Add(geom=new ShaderCreateInfoGeometry(&mdi));else geom=nullptr;
|
|
|
|
|
if(hasFragment ())shader_map.Add(frag=new ShaderCreateInfoFragment(&mdi));else frag=nullptr;
|
2023-05-12 20:30:29 +08:00
|
|
|
|
|
|
|
|
|
mi_length=0;
|
2023-03-06 14:06:20 +08:00
|
|
|
|
}
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-17 10:08:41 +08:00
|
|
|
|
bool MaterialCreateInfo::AddStruct(const AnsiString &struct_name,const AnsiString &codes)
|
2023-03-03 23:02:40 +08:00
|
|
|
|
{
|
2023-03-09 23:21:52 +08:00
|
|
|
|
if(struct_name.IsEmpty()||codes.IsEmpty())
|
2023-03-06 14:06:20 +08:00
|
|
|
|
return(false);
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
return mdi.AddStruct(struct_name,codes);
|
2023-03-06 14:06:20 +08:00
|
|
|
|
}
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-17 10:08:41 +08:00
|
|
|
|
bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const DescriptorSetType set_type,const AnsiString &type_name,const AnsiString &name)
|
2023-03-03 23:02:40 +08:00
|
|
|
|
{
|
2023-03-08 14:02:51 +08:00
|
|
|
|
if(!shader_map.KeyExist(flag_bit))
|
2023-03-06 14:06:20 +08:00
|
|
|
|
return(false);
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
if(!mdi.hasStruct(type_name))
|
2023-03-06 14:06:20 +08:00
|
|
|
|
return(false);
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-17 10:08:41 +08:00
|
|
|
|
ShaderCreateInfo *sc=shader_map[flag_bit];
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
if(!sc)
|
|
|
|
|
return(false);
|
2023-03-04 05:11:51 +08:00
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
UBODescriptor *ubo=mdi.GetUBO(name);
|
2023-03-04 05:11:51 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
if(ubo)
|
2023-03-03 23:02:40 +08:00
|
|
|
|
{
|
2023-03-06 14:06:20 +08:00
|
|
|
|
if(ubo->type!=type_name)
|
2023-03-03 23:02:40 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-03-08 14:02:51 +08:00
|
|
|
|
ubo->stage_flag|=flag_bit;
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-15 11:09:51 +08:00
|
|
|
|
return sc->sdm->AddUBO(set_type,ubo);
|
2023-03-03 23:02:40 +08:00
|
|
|
|
}
|
2023-03-06 14:06:20 +08:00
|
|
|
|
else
|
2023-03-03 23:02:40 +08:00
|
|
|
|
{
|
2023-03-06 14:06:20 +08:00
|
|
|
|
ubo=new UBODescriptor();
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
ubo->type=type_name;
|
2023-03-22 15:58:59 +08:00
|
|
|
|
hgl::strcpy(ubo->name,DESCRIPTOR_NAME_MAX_LENGTH,name);
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
return sc->sdm->AddUBO(set_type,mdi.AddUBO(flag_bit,set_type,ubo));
|
2023-03-03 23:02:40 +08:00
|
|
|
|
}
|
2023-03-06 14:06:20 +08:00
|
|
|
|
}
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-17 10:08:41 +08:00
|
|
|
|
bool MaterialCreateInfo::AddSampler(const VkShaderStageFlagBits flag_bit,const DescriptorSetType set_type,const SamplerType &st,const AnsiString &name)
|
2023-03-04 16:57:50 +08:00
|
|
|
|
{
|
2023-03-08 14:02:51 +08:00
|
|
|
|
if(!shader_map.KeyExist(flag_bit))
|
2023-03-06 14:06:20 +08:00
|
|
|
|
return(false);
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
RANGE_CHECK_RETURN_FALSE(st);
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-17 10:08:41 +08:00
|
|
|
|
ShaderCreateInfo *sc=shader_map[flag_bit];
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
if(!sc)
|
|
|
|
|
return(false);
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
SamplerDescriptor *sampler=mdi.GetSampler(name);
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
AnsiString st_name=GetSamplerTypeName(st);
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
if(sampler)
|
2023-03-04 16:57:50 +08:00
|
|
|
|
{
|
2023-03-06 14:06:20 +08:00
|
|
|
|
if(sampler->type!=st_name)
|
|
|
|
|
return(false);
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-08 14:02:51 +08:00
|
|
|
|
sampler->stage_flag|=flag_bit;
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-15 11:09:51 +08:00
|
|
|
|
return sc->sdm->AddSampler(set_type,sampler);
|
2023-03-04 16:57:50 +08:00
|
|
|
|
}
|
2023-03-06 14:06:20 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sampler=new SamplerDescriptor();
|
2023-03-04 16:57:50 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
sampler->type=st_name;
|
2023-03-22 15:58:59 +08:00
|
|
|
|
hgl::strcpy(sampler->name,DESCRIPTOR_NAME_MAX_LENGTH,name);
|
2023-03-03 23:02:40 +08:00
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
return sc->sdm->AddSampler(set_type,mdi.AddSampler(flag_bit,set_type,sampler));
|
2023-03-06 14:06:20 +08:00
|
|
|
|
}
|
2023-03-03 23:02:40 +08:00
|
|
|
|
}
|
2023-03-08 14:02:51 +08:00
|
|
|
|
|
2023-05-12 20:30:29 +08:00
|
|
|
|
bool MaterialCreateInfo::AddUBO(const uint32_t flag_bits,const DescriptorSetType &set_type,const ShaderBufferSource &ss)
|
|
|
|
|
{
|
|
|
|
|
if(flag_bits==0)return(false); //没有任何SHADER用?
|
|
|
|
|
|
|
|
|
|
if(!mdi.hasStruct(ss.struct_name))
|
|
|
|
|
mdi.AddStruct(ss.struct_name,ss.codes);
|
|
|
|
|
|
|
|
|
|
uint result=0;
|
|
|
|
|
VkShaderStageFlagBits bit;
|
|
|
|
|
|
2023-05-15 21:52:57 +08:00
|
|
|
|
for(int i=0;i<shader_map.GetCount();i++)
|
2023-05-12 20:30:29 +08:00
|
|
|
|
{
|
|
|
|
|
shader_map.GetKey(i,bit);
|
|
|
|
|
|
|
|
|
|
if(flag_bits&bit)
|
|
|
|
|
if(AddUBO(bit,set_type,ss.struct_name,ss.name))
|
|
|
|
|
++result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(result==shader_map.GetCount());
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 10:08:41 +08:00
|
|
|
|
bool MaterialCreateInfo::CreateShader()
|
2023-03-08 14:02:51 +08:00
|
|
|
|
{
|
|
|
|
|
if(shader_map.IsEmpty())
|
|
|
|
|
return(false);
|
|
|
|
|
|
2023-05-12 20:30:29 +08:00
|
|
|
|
if(mi_length>0)
|
|
|
|
|
{
|
2023-05-15 21:52:57 +08:00
|
|
|
|
AddUBO( config->shader_stage,
|
2023-05-12 20:30:29 +08:00
|
|
|
|
DescriptorSetType::Global,
|
|
|
|
|
mtl::SBS_MaterialInstance);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 15:58:59 +08:00
|
|
|
|
mdi.Resort();
|
2023-03-14 22:22:35 +08:00
|
|
|
|
|
2023-03-17 10:08:41 +08:00
|
|
|
|
ShaderCreateInfo *sc,*last=nullptr;
|
2023-03-08 14:02:51 +08:00
|
|
|
|
|
2023-03-15 14:55:10 +08:00
|
|
|
|
for(int i=0;i<shader_map.GetCount();i++)
|
|
|
|
|
{
|
|
|
|
|
if(!shader_map.GetValue(i,sc))
|
|
|
|
|
return(false);
|
2023-03-08 21:41:57 +08:00
|
|
|
|
|
2023-03-15 14:55:10 +08:00
|
|
|
|
sc->CreateShader(last);
|
2023-03-08 21:41:57 +08:00
|
|
|
|
|
2023-03-15 14:55:10 +08:00
|
|
|
|
last=sc;
|
|
|
|
|
}
|
2023-03-08 14:02:51 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
2023-05-15 21:52:57 +08:00
|
|
|
|
STD_MTL_NAMESPACE_END
|