Parse MaterialInstance filed in MaterialFileLoader.cpp
This commit is contained in:
parent
b6e391762b
commit
3a3f9d10b7
@ -21,4 +21,3 @@ macro(CreateProject name)
|
||||
set_property(TARGET ${name} PROPERTY FOLDER "ULRE/Example")
|
||||
endmacro()
|
||||
|
||||
CreateProject(MaterialCreaterTest MaterialCreaterTest.cpp)
|
||||
|
@ -1,126 +0,0 @@
|
||||
#include<hgl/shadergen/MaterialCreateInfo.h>
|
||||
|
||||
using namespace hgl;
|
||||
using namespace hgl::graph;
|
||||
using namespace hgl::shadergen;
|
||||
|
||||
bool PureColor2DMaterial()
|
||||
{
|
||||
MaterialCreateInfo mc("PureColor2D",1,false); //一个新材质,1个RT输出,默认使用Vertex/Fragment shader
|
||||
|
||||
//vertex部分
|
||||
{
|
||||
ShaderCreateInfoVertex *vsc=mc.GetVS(); //获取vertex shader creater
|
||||
|
||||
//以下代码会被展开为
|
||||
/*
|
||||
layout(location=?) in vec3 Position; //位置属性
|
||||
*/
|
||||
vsc->AddInput("vec2","Position"); //添加一个vec3类型的position属性输入
|
||||
|
||||
vsc->SetShaderCodes(R"(
|
||||
void main()
|
||||
{
|
||||
gl_Position=vec4(Position,0,1);
|
||||
})");
|
||||
}
|
||||
|
||||
//添加一个名称为ColorMaterial的UBO定义,其内部有一个vec4 color的属性
|
||||
//该代码会被展开为
|
||||
/*
|
||||
struct ColorMaterial
|
||||
{
|
||||
vec4 color;
|
||||
};
|
||||
*/
|
||||
mc.AddStruct("ColorMaterial","vec4 color;");
|
||||
|
||||
//添加一个UBO,该代码会被展开为
|
||||
/*
|
||||
layout(set=?,binding=?) uniform ColorMaterial mtl;
|
||||
*/
|
||||
mc.AddUBO( VK_SHADER_STAGE_FRAGMENT_BIT, //这个UBO出现在fragment shader
|
||||
DescriptorSetType::PerMaterial, //它属于材质合集
|
||||
"ColorMaterial", //UBO名称为ColorMaterial
|
||||
"mtl"); //UBO变量名称为mtl
|
||||
|
||||
//fragment部分
|
||||
{
|
||||
ShaderCreateInfoFragment *fsc=mc.GetFS(); //获取fragment shader creater
|
||||
|
||||
//以下代码会被展开为
|
||||
/*
|
||||
layout(location=?) out vec4 Color; //颜色输出
|
||||
*/
|
||||
fsc->AddOutput("vec4","Color"); //添加一个vec4类型的color属性输出
|
||||
|
||||
fsc->SetShaderCodes(R"(
|
||||
void main()
|
||||
{
|
||||
Color=mtl.color;
|
||||
})");
|
||||
}
|
||||
|
||||
mc.CreateShader();
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool VertexColor2DMaterial()
|
||||
{
|
||||
MaterialCreateInfo mc("VertexColor2D",1,false);
|
||||
|
||||
//vertex部分
|
||||
{
|
||||
ShaderCreateInfoVertex *vsc=mc.GetVS();
|
||||
|
||||
vsc->AddInput("vec2","Position");
|
||||
vsc->AddInput("vec4","Color");
|
||||
|
||||
vsc->AddOutput("vec4","Color");
|
||||
|
||||
vsc->SetShaderCodes(R"(
|
||||
void main()
|
||||
{
|
||||
Output.Color=Color;
|
||||
|
||||
gl_Position=vec4(Position,0,1);
|
||||
})");
|
||||
}
|
||||
|
||||
//fragment部分
|
||||
{
|
||||
ShaderCreateInfoFragment *fsc=mc.GetFS();
|
||||
|
||||
fsc->AddOutput("vec4","Color");
|
||||
|
||||
fsc->SetShaderCodes(R"(
|
||||
void main()
|
||||
{
|
||||
Color=Input.Color;
|
||||
})");
|
||||
}
|
||||
|
||||
mc.CreateShader();
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
namespace glsl_compiler
|
||||
{
|
||||
bool Init();
|
||||
void Close();
|
||||
}//namespace glsl_compiler
|
||||
|
||||
int main()
|
||||
{
|
||||
if(!glsl_compiler::Init())
|
||||
return -1;
|
||||
|
||||
PureColor2DMaterial();
|
||||
VertexColor2DMaterial();
|
||||
|
||||
glsl_compiler::Close();
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/String.h>
|
||||
|
||||
#define STD_MTL_NAMESPACE_BEGIN namespace hgl{namespace graph{namespace mtl{
|
||||
#define STD_MTL_NAMESPACE_END }}}
|
||||
|
||||
@ -45,6 +47,9 @@ namespace hgl
|
||||
|
||||
virtual MaterialCreateInfo *Create()=0;
|
||||
};//class StdMaterial
|
||||
|
||||
|
||||
bool LoadMaterialFromFile(const AnsiString &mtl_filename);
|
||||
}//namespace mtl
|
||||
}//namespace graph
|
||||
}//namespace hgl
|
||||
|
@ -35,23 +35,34 @@ namespace
|
||||
FragmentOutput,
|
||||
FragmentCode,
|
||||
|
||||
|
||||
ENUM_CLASS_RANGE(None,FragmentCode)
|
||||
};//enum class State
|
||||
|
||||
constexpr const char *StateNameList[]=
|
||||
struct MaterialFileStateInfo
|
||||
{
|
||||
"Material",
|
||||
"MaterialInstance",
|
||||
"Vertex",
|
||||
"Geometry",
|
||||
"Fragment",
|
||||
const char *name;
|
||||
const int len;
|
||||
MaterialFileState state;
|
||||
};
|
||||
|
||||
const MaterialFileState GetMaterialFileState(const char *str)
|
||||
#define MFS(name) {#name,sizeof(#name)-1,MaterialFileState::name},
|
||||
|
||||
constexpr const MaterialFileStateInfo state_list[]=
|
||||
{
|
||||
for(int i=0;i<int(MaterialFileState::END_RANGE);i++)
|
||||
if(hgl::stricmp(str,StateNameList[i])==0)
|
||||
return MaterialFileState(i);
|
||||
MFS(Material)
|
||||
MFS(MaterialInstance)
|
||||
MFS(Vertex)
|
||||
MFS(Geometry)
|
||||
MFS(Fragment)
|
||||
};
|
||||
|
||||
const MaterialFileState GetMaterialFileState(const char *str,const int len)
|
||||
{
|
||||
for(const MaterialFileStateInfo &info:state_list)
|
||||
if(len==info.len)
|
||||
if(hgl::stricmp(str,info.name,len)==0)
|
||||
return info.state;
|
||||
|
||||
return MaterialFileState::None;
|
||||
}
|
||||
@ -94,24 +105,27 @@ namespace
|
||||
{
|
||||
if(*text=='{')
|
||||
{
|
||||
code_sp=text+1;
|
||||
++text;
|
||||
while(*text=='\r'||*text=='\n')++text;
|
||||
|
||||
code_sp=text;
|
||||
return(true);
|
||||
}
|
||||
|
||||
if(*text=='}')
|
||||
{
|
||||
code_ep=text-1;
|
||||
code_ep=text;
|
||||
code=false;
|
||||
return(true);
|
||||
}
|
||||
}
|
||||
|
||||
if(hgl::stricmp(text,"Code")==0)
|
||||
if(hgl::stricmp(text,"Code",4)==0)
|
||||
{
|
||||
code=true;
|
||||
}
|
||||
else
|
||||
if(hgl::stricmp(text,"Length")==0)
|
||||
if(hgl::stricmp(text,"Length",6)==0)
|
||||
{
|
||||
text+=7;
|
||||
while(*text==' '||*text=='\t')++text;
|
||||
@ -119,7 +133,7 @@ namespace
|
||||
hgl::stou(text,mi_bytes);
|
||||
}
|
||||
else
|
||||
if(hgl::stricmp(text,"Stage")==0)
|
||||
if(hgl::stricmp(text,"Stage",5)==0)
|
||||
{
|
||||
const char *ep=text+len;
|
||||
const char *sp;
|
||||
@ -173,15 +187,15 @@ namespace
|
||||
{
|
||||
SAFE_CLEAR(parse)
|
||||
|
||||
state=GetMaterialFileState(text+1);
|
||||
state=GetMaterialFileState(text+1,len-1);
|
||||
|
||||
switch(state)
|
||||
{
|
||||
case MaterialFileState::Material: parse=new MaterialStateParse;break;
|
||||
case MaterialFileState::MaterialInstance: parse=new MaterialInstanceStateParse;break;
|
||||
case MaterialFileState::Vertex: parse=new VertexStateParse;break;
|
||||
case MaterialFileState::Geometry: parse=new GeometryStateParse;break;
|
||||
case MaterialFileState::Fragment: parse=new FragmentStateParse;break;
|
||||
// case MaterialFileState::Vertex: parse=new VertexStateParse;break;
|
||||
// case MaterialFileState::Geometry: parse=new GeometryStateParse;break;
|
||||
// case MaterialFileState::Fragment: parse=new FragmentStateParse;break;
|
||||
|
||||
default: state=MaterialFileState::None;return(false);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user