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-16 15:21:32 +08:00
|
|
|
|
#include<hgl/graph/mtl/UBOCommon.h>
|
2023-06-02 20:45:19 +08:00
|
|
|
|
#include<hgl/graph/VKDeviceAttribute.h>
|
2023-05-22 14:30:38 +08:00
|
|
|
|
#include"common/MFCommon.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
|
2023-06-02 20:45:19 +08:00
|
|
|
|
MaterialCreateInfo::MaterialCreateInfo(const MaterialCreateConfig *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
|
|
|
|
|
2023-06-02 14:22:07 +08:00
|
|
|
|
mi_data_bytes=0;
|
2023-05-31 20:14:45 +08:00
|
|
|
|
mi_shader_stage=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-05-22 14:30:38 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置材质实例代码与数据长度
|
2023-06-02 14:23:33 +08:00
|
|
|
|
* @param glsl_codes 材质实例GLSL代码
|
|
|
|
|
* @param data_bytes 单个材质实例数据长度
|
2023-06-13 18:02:00 +08:00
|
|
|
|
* @param shader_stage_flag_bits 具体使用材质实例的shader
|
2023-05-22 14:30:38 +08:00
|
|
|
|
* @return 是否设置成功
|
|
|
|
|
*/
|
2023-06-13 18:02:00 +08:00
|
|
|
|
bool MaterialCreateInfo::SetMaterialInstance(const AnsiString &glsl_codes,const uint32_t data_bytes,const uint32_t shader_stage_flag_bits)
|
2023-03-08 14:02:51 +08:00
|
|
|
|
{
|
2023-06-02 14:22:07 +08:00
|
|
|
|
if(mi_data_bytes>0)return(false); //已经有数据了
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-06-13 18:02:00 +08:00
|
|
|
|
if(shader_stage_flag_bits==0)return(false);
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-06-02 14:23:33 +08:00
|
|
|
|
if(data_bytes>0&&glsl_codes.Length()<4)return(false);
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-06-02 14:23:33 +08:00
|
|
|
|
mi_data_bytes=data_bytes;
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-06-02 14:23:33 +08:00
|
|
|
|
if(data_bytes>0)
|
|
|
|
|
mi_codes=glsl_codes;
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-06-13 18:02:00 +08:00
|
|
|
|
const uint32_t ubo_range=config->dev_attr->physical_device->GetUBORange(); //Mali-T系/G71为16k,Intel/PowerVR为128M,AMD无限制。nVidia和Mali-G除G71外为64k
|
2023-06-05 21:39:12 +08:00
|
|
|
|
const uint32_t mi_max_count=ubo_range/data_bytes;
|
|
|
|
|
|
|
|
|
|
const AnsiString MI_MAX_COUNT=AnsiString::numberOf(mi_max_count>256?256:mi_max_count); //我们使用uint8传递材质实例ID,所以最大数量为256。未来如考虑使用更多,需综合考虑
|
2023-06-02 20:45:19 +08:00
|
|
|
|
|
2023-05-22 14:30:38 +08:00
|
|
|
|
mdi.AddStruct(MaterialInstanceStruct,mi_codes);
|
2023-06-02 20:45:19 +08:00
|
|
|
|
mdi.AddStruct(SBS_MaterialInstanceData);
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
|
|
|
|
UBODescriptor *ubo=new UBODescriptor();
|
|
|
|
|
|
|
|
|
|
ubo->type=SBS_MaterialInstanceData.struct_name;
|
|
|
|
|
hgl::strcpy(ubo->name,DESCRIPTOR_NAME_MAX_LENGTH,SBS_MaterialInstanceData.name);
|
2023-06-13 18:02:00 +08:00
|
|
|
|
ubo->stage_flag=shader_stage_flag_bits;
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-06-13 18:02:00 +08:00
|
|
|
|
mdi.AddUBO(shader_stage_flag_bits,DescriptorSetType::PerMaterial,ubo);
|
2023-03-08 14:02:51 +08:00
|
|
|
|
|
2023-05-22 14:30:38 +08:00
|
|
|
|
auto *it=shader_map.GetDataList();
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<shader_map.GetCount();i++)
|
2023-05-12 20:30:29 +08:00
|
|
|
|
{
|
2023-06-13 18:02:00 +08:00
|
|
|
|
if((*it)->key&shader_stage_flag_bits)
|
2023-06-02 20:45:19 +08:00
|
|
|
|
{
|
|
|
|
|
(*it)->value->AddDefine("MI_MAX_COUNT",MI_MAX_COUNT);
|
2023-06-01 14:47:05 +08:00
|
|
|
|
(*it)->value->SetMaterialInstance(ubo,mi_codes);
|
2023-06-02 20:45:19 +08:00
|
|
|
|
}
|
2023-06-01 14:47:05 +08:00
|
|
|
|
|
|
|
|
|
++it;
|
2023-05-12 20:30:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-22 14:30:38 +08:00
|
|
|
|
vert->AddMaterialInstanceID(); //增加一个材质实例ID
|
|
|
|
|
|
2023-06-13 18:02:00 +08:00
|
|
|
|
mi_shader_stage=shader_stage_flag_bits;
|
2023-05-31 20:14:45 +08:00
|
|
|
|
|
2023-05-22 14:30:38 +08:00
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 20:45:19 +08:00
|
|
|
|
bool MaterialCreateInfo::CreateShader(const GPUDeviceAttribute *dev_attr)
|
2023-05-22 14:30:38 +08:00
|
|
|
|
{
|
|
|
|
|
if(shader_map.IsEmpty())
|
|
|
|
|
return(false);
|
|
|
|
|
|
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-06-02 20:45:19 +08:00
|
|
|
|
if(sc->GetShaderStage()<mi_shader_stage)
|
|
|
|
|
{
|
|
|
|
|
sc->AddOutput(VAT_UINT,VAN::MaterialInstanceID,Interpolation::Flat);
|
|
|
|
|
sc->AddFunction(mtl::func::HandoverMI);
|
|
|
|
|
}
|
2023-05-31 20:14:45 +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
|