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-10-07 17:02:00 +08:00
|
|
|
|
#include"ShaderLibrary.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-09-05 20:19:53 +08:00
|
|
|
|
ubo_range=config->dev_attr->physical_device->GetUBORange(); //Mali-T系/G71为16k,nVidia和Mali-G系列除G71外为64k,Intel/PowerVR为128M,AMD无限制。
|
|
|
|
|
ssbo_range=config->dev_attr->physical_device->GetSSBORange();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
mi_data_bytes=0;
|
|
|
|
|
mi_shader_stage=0;
|
|
|
|
|
mi_max_count=0;
|
|
|
|
|
mi_ubo=nullptr;
|
|
|
|
|
}
|
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
|
|
|
|
|
2024-03-09 22:25:03 +08:00
|
|
|
|
bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const DescriptorSetType set_type,const AnsiString &struct_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
|
|
|
|
|
2024-03-09 22:25:03 +08:00
|
|
|
|
if(!mdi.hasStruct(struct_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
|
|
|
|
{
|
2024-03-09 22:25:03 +08:00
|
|
|
|
if(ubo->type!=struct_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
|
|
|
|
|
2024-03-09 22:25:03 +08:00
|
|
|
|
ubo->type=struct_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
|
|
|
|
|
2024-03-09 22:25:03 +08:00
|
|
|
|
bool MaterialCreateInfo::AddUBO(const uint32_t flag_bits,const DescriptorSetType &set_type,const AnsiString &struct_name,const AnsiString &name)
|
|
|
|
|
{
|
|
|
|
|
if(flag_bits==0)return(false); //没有任何SHADER用?
|
|
|
|
|
|
|
|
|
|
if(!mdi.hasStruct(struct_name))
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
uint result=0;
|
|
|
|
|
VkShaderStageFlagBits bit;
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<shader_map.GetCount();i++)
|
|
|
|
|
{
|
|
|
|
|
shader_map.GetKey(i,bit);
|
|
|
|
|
|
|
|
|
|
if(flag_bits&bit)
|
|
|
|
|
if(AddUBO(bit,set_type,struct_name,name))
|
|
|
|
|
++result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(result==shader_map.GetCount());
|
|
|
|
|
}
|
|
|
|
|
|
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-09-28 17:42:14 +08:00
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
UBODescriptor *CreateUBODescriptor(const ShaderBufferSource &sbs,const uint32_t flag_bits)
|
|
|
|
|
{
|
|
|
|
|
UBODescriptor *ubo=new UBODescriptor;
|
|
|
|
|
|
|
|
|
|
ubo->type=sbs.struct_name;
|
|
|
|
|
|
|
|
|
|
hgl::strcpy(ubo->name,DESCRIPTOR_NAME_MAX_LENGTH,sbs.name);
|
|
|
|
|
|
|
|
|
|
ubo->stage_flag=flag_bits;
|
|
|
|
|
|
|
|
|
|
return ubo;
|
|
|
|
|
}
|
|
|
|
|
}//namespace
|
|
|
|
|
|
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-09-05 23:36:59 +08:00
|
|
|
|
mi_max_count=hgl_min<uint32_t>(ubo_range/data_bytes,HGL_U16_MAX);
|
2023-06-02 20:45:19 +08:00
|
|
|
|
|
2023-05-22 14:30:38 +08:00
|
|
|
|
mdi.AddStruct(MaterialInstanceStruct,mi_codes);
|
2023-09-20 21:53:30 +08:00
|
|
|
|
mdi.AddStruct(SBS_MaterialInstance);
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-09-28 17:42:14 +08:00
|
|
|
|
mi_ubo=CreateUBODescriptor(SBS_MaterialInstance,shader_stage_flag_bits);
|
2023-05-22 14:30:38 +08:00
|
|
|
|
|
2023-09-26 14:50:38 +08:00
|
|
|
|
mdi.AddUBO(shader_stage_flag_bits,DescriptorSetType::PerMaterial,mi_ubo);
|
2023-09-05 20:19:53 +08:00
|
|
|
|
|
|
|
|
|
const AnsiString MI_MAX_COUNT=AnsiString::numberOf(mi_max_count);
|
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-09-05 20:19:53 +08:00
|
|
|
|
(*it)->value->SetMaterialInstance(mi_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-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-09-05 20:19:53 +08:00
|
|
|
|
bool MaterialCreateInfo::SetLocalToWorld(const uint32_t shader_stage_flag_bits)
|
|
|
|
|
{
|
|
|
|
|
if(shader_stage_flag_bits==0)return(false);
|
|
|
|
|
|
|
|
|
|
l2w_shader_stage=shader_stage_flag_bits;
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MaterialCreateInfo::CreateShader()
|
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)
|
2023-10-07 23:35:22 +08:00
|
|
|
|
sc->AddMaterialInstanceOutput();
|
2023-09-05 20:19:53 +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
|