ULRE/src/ShaderGen/MaterialCreater.cpp

152 lines
3.3 KiB
C++
Raw Normal View History

2023-03-06 14:06:20 +08:00
#include<hgl/shadergen/MaterialCreater.h>
2023-03-03 23:02:40 +08:00
using namespace hgl;
using namespace hgl::graph;
SHADERGEN_NAMESPACE_BEGIN
2023-03-06 14:06:20 +08:00
MaterialCreater::MaterialCreater(const uint rc,const uint32 ss)
2023-03-03 23:02:40 +08:00
{
2023-03-06 14:06:20 +08:00
rt_count=rc;
shader_stage=ss;
2023-03-03 23:02:40 +08:00
2023-03-06 14:06:20 +08:00
if(hasVertex ())shader_map.Add(vert=new ShaderCreaterVertex );else vert=nullptr;
if(hasGeometry ())shader_map.Add(geom=new ShaderCreaterGeometry);else geom=nullptr;
if(hasFragment ())shader_map.Add(frag=new ShaderCreaterFragment);else frag=nullptr;
}
2023-03-03 23:02:40 +08:00
2023-03-06 14:06:20 +08:00
bool MaterialCreater::AddUBOStruct(const AnsiString &ubo_typename,const AnsiString &codes)
2023-03-03 23:02:40 +08:00
{
2023-03-06 14:06:20 +08:00
if(ubo_typename.IsEmpty()||codes.IsEmpty())
return(false);
2023-03-03 23:02:40 +08:00
2023-03-08 14:02:51 +08:00
return mdm.AddUBOStruct(ubo_typename,codes);
2023-03-06 14:06:20 +08:00
}
2023-03-03 23:02:40 +08:00
2023-03-08 14:02:51 +08:00
bool MaterialCreater::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-08 14:02:51 +08:00
if(!mdm.hasUBOStruct(type_name))
2023-03-06 14:06:20 +08:00
return(false);
2023-03-03 23:02:40 +08:00
2023-03-08 14:02:51 +08:00
ShaderCreater *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-08 14:02:51 +08:00
UBODescriptor *ubo=mdm.GetUBO(name);
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-06 14:06:20 +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-06 14:06:20 +08:00
ubo->type=type_name;
ubo->name=name;
2023-03-03 23:02:40 +08:00
2023-03-08 14:02:51 +08:00
return sc->sdm.AddUBO(set_type,mdm.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-08 14:02:51 +08:00
bool MaterialCreater::AddSampler(const VkShaderStageFlagBits flag_bit,const DescriptorSetType set_type,const SamplerType &st,const AnsiString &name)
{
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-06 14:06:20 +08:00
RANGE_CHECK_RETURN_FALSE(st);
2023-03-08 14:02:51 +08:00
ShaderCreater *sc=shader_map[flag_bit];
2023-03-06 14:06:20 +08:00
if(!sc)
return(false);
2023-03-08 14:02:51 +08:00
SamplerDescriptor *sampler=mdm.GetSampler(name);
2023-03-06 14:06:20 +08:00
AnsiString st_name=GetSamplerTypeName(st);
2023-03-06 14:06:20 +08:00
if(sampler)
{
2023-03-06 14:06:20 +08:00
if(sampler->type!=st_name)
return(false);
2023-03-08 14:02:51 +08:00
sampler->stage_flag|=flag_bit;
2023-03-06 14:06:20 +08:00
return sc->sdm.AddSampler(set_type,sampler);
}
2023-03-06 14:06:20 +08:00
else
{
sampler=new SamplerDescriptor();
2023-03-06 14:06:20 +08:00
sampler->type=st_name;
sampler->name=name;
2023-03-03 23:02:40 +08:00
2023-03-08 14:02:51 +08:00
return sc->sdm.AddSampler(set_type,mdm.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
void MaterialCreater::SetContext()
{
//ShaderMap使用ObjectMap保存,ObjectMap本质附带排序功能所以这里无需再排序直接设定prev,next即可
LOG_INFO("Resort Shader.");
ShaderCreater *prev,*cur,*next;
auto *it=shader_map.GetDataList();
const int count=shader_map.GetCount();
for(int i=0; i<count; i++)
{
cur=(*it)->right;
++it;
if(i>0)
cur->sdm.SetPrevShader(prev->GetShaderStage());
if(i<count-1)
{
next=(*it)->right;
cur->sdm.SetNextShader(next->GetShaderStage());
}
#ifdef _DEBUG
cur->sdm.DebugOutput(i);
#endif//_DEBUG
prev=cur;
}
}
bool MaterialCreater::CompileShader()
{
if(shader_map.IsEmpty())
return(false);
SetContext(); //设定上下文
ShaderCreater *sc;
for(int i=0;i<shader_map.GetCount();i++)
{
if(!shader_map.GetValue(i,sc))
return(false);
if(!sc->CompileToSPV())
return(false);
}
return(true);
}
2023-03-03 23:02:40 +08:00
SHADERGEN_NAMESPACE_END