2023-03-06 14:06:20 +08:00
|
|
|
#include<hgl/shadergen/ShaderCreater.h>
|
|
|
|
|
|
|
|
SHADERGEN_NAMESPACE_BEGIN
|
|
|
|
int ShaderCreater::AddOutput(const VAT &type,const AnsiString &name)
|
|
|
|
{
|
|
|
|
ShaderStage *ss=new ShaderStage;
|
|
|
|
|
|
|
|
hgl::strcpy(ss->name,sizeof(ss->name),name.c_str());
|
|
|
|
|
|
|
|
ss->basetype=(uint8) type.basetype;
|
|
|
|
ss->vec_size= type.vec_size;
|
|
|
|
|
|
|
|
return sdm.AddOutput(ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ShaderCreater::AddOutput(const AnsiString &type,const AnsiString &name)
|
|
|
|
{
|
|
|
|
VAT vat;
|
|
|
|
|
2023-03-08 21:41:57 +08:00
|
|
|
if(name.IsEmpty())
|
|
|
|
return -1;
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
if(!ParseVertexAttribType(&vat,type))
|
2023-03-08 21:41:57 +08:00
|
|
|
return -2;
|
2023-03-06 14:06:20 +08:00
|
|
|
|
|
|
|
return AddOutput(vat,name);
|
|
|
|
}
|
|
|
|
|
2023-03-09 23:21:52 +08:00
|
|
|
bool ShaderCreater::ProcSubpassInput()
|
2023-03-08 21:41:57 +08:00
|
|
|
{
|
2023-03-10 02:15:26 +08:00
|
|
|
auto sil=sdm.GetSubpassInputList();
|
|
|
|
|
|
|
|
if(sil.IsEmpty())
|
|
|
|
return(true);
|
|
|
|
|
|
|
|
const auto si=sil.GetData();
|
|
|
|
const int si_count=sil.GetCount();
|
|
|
|
|
|
|
|
for(int i=0;i<si_count;i++)
|
|
|
|
{
|
|
|
|
final_shader+="layout(input_attachment_index=";
|
|
|
|
final_shader+=AnsiString::numberOf((*si)->input_attachment_index);
|
|
|
|
final_shader+=", binding=";
|
|
|
|
final_shader+=AnsiString::numberOf((*si)->binding);
|
|
|
|
final_shader+=") uniform subpassInput ";
|
|
|
|
final_shader+=(*si)->name;
|
|
|
|
final_shader+=";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
final_shader+="\n";
|
|
|
|
|
|
|
|
return(true);
|
2023-03-09 23:21:52 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCreater::ProcInput(ShaderCreater *last_sc)
|
|
|
|
{
|
|
|
|
if(!last_sc)
|
2023-03-10 02:15:26 +08:00
|
|
|
return(false);
|
2023-03-09 23:21:52 +08:00
|
|
|
|
|
|
|
AnsiString last_output=last_sc->GetOutputStruct();
|
|
|
|
|
|
|
|
if(last_output.IsEmpty())
|
2023-03-10 02:15:26 +08:00
|
|
|
return(true);
|
2023-03-09 23:21:52 +08:00
|
|
|
|
|
|
|
final_shader+="layout(location=0) in ";
|
|
|
|
final_shader+=last_output;
|
|
|
|
final_shader+="Input;\n\n";
|
2023-03-10 02:15:26 +08:00
|
|
|
|
|
|
|
return(true);
|
2023-03-08 21:41:57 +08:00
|
|
|
}
|
|
|
|
|
2023-03-09 23:21:52 +08:00
|
|
|
bool ShaderCreater::ProcOutput()
|
2023-03-08 21:41:57 +08:00
|
|
|
{
|
|
|
|
final_shader+="layout(location=0) out ";
|
|
|
|
|
|
|
|
output_struct.Clear();
|
|
|
|
|
2023-03-09 23:21:52 +08:00
|
|
|
output_struct=GetShaderStageName(shader_stage);
|
|
|
|
output_struct+="_Output\n{\n";
|
|
|
|
|
2023-03-08 21:41:57 +08:00
|
|
|
for(auto *ss:sdm.GetShaderStageIO().output)
|
|
|
|
{
|
|
|
|
output_struct+="\t";
|
|
|
|
output_struct+=GetShaderStageTypeName(ss);
|
|
|
|
output_struct+=" ";
|
|
|
|
output_struct+=ss->name;
|
|
|
|
output_struct+=";\n";
|
|
|
|
}
|
|
|
|
|
2023-03-09 23:21:52 +08:00
|
|
|
output_struct+="}";
|
|
|
|
|
2023-03-08 21:41:57 +08:00
|
|
|
final_shader+=output_struct;
|
2023-03-09 23:21:52 +08:00
|
|
|
final_shader+="Output;\n\n";
|
2023-03-10 02:15:26 +08:00
|
|
|
|
|
|
|
return(true);
|
2023-03-09 23:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCreater::ProcStruct()
|
|
|
|
{
|
|
|
|
const AnsiStringList struct_list=sdm.GetStructList();
|
|
|
|
|
|
|
|
AnsiString codes;
|
|
|
|
|
2023-03-10 02:15:26 +08:00
|
|
|
for(auto &str:struct_list)
|
2023-03-09 23:21:52 +08:00
|
|
|
{
|
2023-03-10 02:15:26 +08:00
|
|
|
if(!mdm->GetStruct(*str,codes))
|
2023-03-09 23:21:52 +08:00
|
|
|
return(false);
|
2023-03-10 02:15:26 +08:00
|
|
|
|
|
|
|
final_shader+="struct ";
|
|
|
|
final_shader+=*str;
|
|
|
|
final_shader+="\n{\n";
|
|
|
|
final_shader+=codes;
|
|
|
|
final_shader+="};\n\n";
|
2023-03-09 23:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCreater::ProcUBO()
|
|
|
|
{
|
2023-03-10 02:15:26 +08:00
|
|
|
auto ubo_list=sdm.GetUBOList();
|
|
|
|
|
|
|
|
const int count=ubo_list.GetCount();
|
|
|
|
|
|
|
|
if(count<=0)return(true);
|
|
|
|
|
|
|
|
auto ubo=ubo_list.GetData();
|
|
|
|
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
{
|
|
|
|
final_shader+="layout(set=";
|
|
|
|
final_shader+=AnsiString::numberOf((*ubo)->set);
|
|
|
|
final_shader+=",binding=";
|
|
|
|
final_shader+=AnsiString::numberOf((*ubo)->binding);
|
|
|
|
final_shader+=") uniform ";
|
|
|
|
final_shader+=(*ubo)->type;
|
|
|
|
final_shader+=" ";
|
|
|
|
final_shader+=(*ubo)->name;
|
|
|
|
final_shader+="\n";
|
|
|
|
|
|
|
|
++ubo;
|
|
|
|
}
|
|
|
|
|
|
|
|
final_shader+="\n";
|
|
|
|
return(true);
|
2023-03-09 23:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCreater::ProcSSBO()
|
|
|
|
{
|
2023-03-10 02:15:26 +08:00
|
|
|
return(false);
|
2023-03-09 23:21:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCreater::ProcConst()
|
2023-03-10 02:15:26 +08:00
|
|
|
{auto const_list=sdm.GetConstList();
|
|
|
|
|
|
|
|
const int count=const_list.GetCount();
|
|
|
|
|
|
|
|
if(count<=0)return(true);
|
|
|
|
|
|
|
|
auto const_data=const_list.GetData();
|
|
|
|
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
{
|
|
|
|
final_shader+="layout(constant_id=";
|
|
|
|
final_shader+=AnsiString::numberOf((*const_data)->constant_id);
|
|
|
|
final_shader+=") const ";
|
|
|
|
final_shader+=(*const_data)->type;
|
|
|
|
final_shader+=" ";
|
|
|
|
final_shader+=(*const_data)->name;
|
|
|
|
final_shader+="=";
|
|
|
|
final_shader+=(*const_data)->value;
|
|
|
|
final_shader+=";\n";
|
|
|
|
|
|
|
|
++const_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
final_shader+="\n";
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCreater::ProcSampler()
|
2023-03-09 23:21:52 +08:00
|
|
|
{
|
2023-03-10 02:15:26 +08:00
|
|
|
auto sampler_list=sdm.GetSamplerList();
|
|
|
|
|
|
|
|
const int count=sampler_list.GetCount();
|
|
|
|
|
|
|
|
if(count<=0)return(true);
|
|
|
|
|
|
|
|
auto sampler=sampler_list.GetData();
|
|
|
|
|
|
|
|
for(int i=0;i<count;i++)
|
|
|
|
{
|
|
|
|
final_shader+="layout(set=";
|
|
|
|
final_shader+=AnsiString::numberOf((*sampler)->set);
|
|
|
|
final_shader+=",binding=";
|
|
|
|
final_shader+=AnsiString::numberOf((*sampler)->binding);
|
|
|
|
final_shader+=") uniform ";
|
|
|
|
final_shader+=(*sampler)->type;
|
|
|
|
final_shader+=" ";
|
|
|
|
final_shader+=(*sampler)->name;
|
|
|
|
final_shader+="\n";
|
|
|
|
|
|
|
|
++sampler;
|
|
|
|
}
|
|
|
|
|
|
|
|
final_shader+="\n";
|
|
|
|
return(true);
|
2023-03-08 21:41:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShaderCreater::CreateShader(ShaderCreater *last_sc)
|
|
|
|
{
|
|
|
|
final_shader="#version 460 core\n";
|
|
|
|
|
2023-03-10 02:15:26 +08:00
|
|
|
if(!ProcSubpassInput())
|
|
|
|
return(false);
|
|
|
|
if(!ProcInput(last_sc))
|
|
|
|
return(false);
|
|
|
|
if(!ProcStruct())
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
if(!ProcUBO())
|
|
|
|
return(false);
|
|
|
|
//if(!ProcSSBO())
|
|
|
|
//return(false);
|
|
|
|
if(!ProcConst())
|
|
|
|
return(false);
|
|
|
|
if(!ProcSampler())
|
|
|
|
return(false);
|
2023-03-09 23:21:52 +08:00
|
|
|
|
|
|
|
ProcOutput();
|
2023-03-10 02:15:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return(true);
|
2023-03-08 21:41:57 +08:00
|
|
|
}
|
|
|
|
|
2023-03-06 21:30:32 +08:00
|
|
|
bool ShaderCreater::CompileToSPV()
|
2023-03-06 14:06:20 +08:00
|
|
|
{
|
2023-03-08 14:02:51 +08:00
|
|
|
if(shader_codes.IsEmpty())
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
}
|
2023-03-06 21:30:32 +08:00
|
|
|
SHADERGEN_NAMESPACE_END
|