added a parse in MaterialFileLoader.cpp that it's UBOParse

This commit is contained in:
hyzboy 2023-12-05 02:08:18 +08:00
parent 775e031422
commit 4aeb28c571
No known key found for this signature in database
GPG Key ID: 067EE4525D4FB6D3
4 changed files with 145 additions and 26 deletions

View File

@ -3,7 +3,16 @@ Name BlinnPhong+HalfLambert shading model only color
Reference https://zhuanlan.zhihu.com/p/442023993
Base Std3D
Require LocalToWorld,Camera,Sun
//某些Require并不真的存在.ubo文件写成一行一个是为了方便未来改成带路径的
Require LocalToWorld
Require Camera
UBO
{
File Sun.ubo
Name sun
Stage Vertex,Fragment
}
#MaterialInstance

View File

@ -0,0 +1,2 @@
vec4 direction;
vec4 color;

View File

@ -34,16 +34,27 @@ namespace material_file
SamplerType type;
};
struct UBOData
{
char name[SHADER_RESOURCE_NAME_MAX_LENGTH];
char filename[HGL_MAX_PATH];
uint32_t shader_stage_flag_bits;
};
using UBODataList=List<UBOData>;
struct ShaderData
{
VkShaderStageFlagBits shader_stage;
VkShaderStageFlagBits shader_stage;
const char *code;
uint code_length;
const char * code;
uint code_length;
List<UniformAttrib> output;
List<UniformAttrib> output;
List<SamplerData> sampler;
List<SamplerData> sampler;
public:
@ -91,6 +102,8 @@ namespace material_file
List<UniformAttrib> vi; ///<Vertex Input
UBODataList ubo_list;
ShaderDataMap shader;
uint32_t shader_stage_flag_bit;

View File

@ -63,19 +63,113 @@ namespace
return MaterialFileBlock::None;
}
const uint32_t ShaderStageParse(char *text,const char *ep)
{
uint32_t shader_stage_flag_bits=0;
char *sp;
while(*text==' '||*text=='\t')++text;
while(text<ep)
{
sp=text;
while(hgl::isalpha(*text))
++text;
shader_stage_flag_bits|=GetShaderStageFlagBits(sp,text-sp);
++text;
}
return shader_stage_flag_bits;
}
int ClipCodeString(char *str,const int max_len,const char *text)
{
while(*text==' '||*text=='\t')++text;
const char *sp=text;
while(hgl::iscodechar(*text))++text;
hgl::strcpy(str,max_len,sp,text-sp);
return text-sp;
}
struct UBOParse:public TextParse
{
UBOData ubo_data;
public:
void Clear()
{
hgl_zero(ubo_data);
}
bool OnLine(char *text,const int len) override
{
if(!text||!*text||len<=0)
return(false);
if(*text=='{')
{
++text;
while(*text=='\r'||*text=='\n')++text;
return(false);
}
if(*text=='}')
{
*text=0;
return(true);
}
while(*text==' '||*text=='\t')++text;
if(text[0]=='/'&&text[1]=='/')
return(false);
if(hgl::stricmp(text,"File ",5)==0)
{
ClipCodeString(ubo_data.filename,sizeof(ubo_data.filename),text+5);
}
else
if(hgl::stricmp(text,"Name ",5)==0)
{
ClipCodeString(ubo_data.name,sizeof(ubo_data.name),text+5);
}
else
if(hgl::stricmp(text,"Stage ",6)==0)
{
ubo_data.shader_stage_flag_bits=ShaderStageParse(text+6,text+len);
}
return(false);
}
};//struct UBOParse
struct MaterialBlockParse:public TextParse
{
MaterialFileBlock state;
AnsiStringList *require_list=nullptr;
UBODataList *ubo_list=nullptr;
bool ubo=false;
UBOParse ubo_parse;
public:
MaterialBlockParse(AnsiStringList *asl)
MaterialBlockParse(AnsiStringList *asl,UBODataList *udl)
{
state=MaterialFileBlock::None;
require_list=asl;
ubo_list=udl;
}
bool OnLine(char *text,const int len) override
@ -85,6 +179,19 @@ namespace
char *ep=text+len;
if(ubo)
{
if(ubo_parse.OnLine(text,len))
{
ubo_list->Add(ubo_parse.ubo_data);
ubo_parse.Clear();
ubo=false;
}
return(true);
}
if(hgl::stricmp(text,"Require ",8)==0)
{
text+=8;
@ -102,6 +209,11 @@ namespace
sp=text;
}
}
else
if(hgl::stricmp(text,"UBO",3)==0)
{
ubo=true;
}
return(true);
}
@ -195,24 +307,7 @@ namespace
else
if(hgl::stricmp(text,"Stage ",6)==0)
{
const char *ep=text+len;
const char *sp;
text+=6;
while(*text==' '||*text=='\t')++text;
while(text<ep)
{
sp=text;
while(hgl::isalpha(*text))
++text;
shader_stage_flag_bits|=GetShaderStageFlagBits(sp,text-sp);
++text;
}
shader_stage_flag_bits=ShaderStageParse(text+6,text+len);
}
return(true);
@ -461,7 +556,7 @@ namespace
state=GetMaterialFileState(text+1,len-1);
if(state==MaterialFileBlock::Material)
parse=new MaterialBlockParse(&(mfd->require_list));
parse=new MaterialBlockParse(&(mfd->require_list),&(mfd->ubo_list));
else
if(state==MaterialFileBlock::MaterialInstance)
parse=new MaterialInstanceBlockParse(&(mfd->mi));