2023-03-17 10:08:41 +08:00
|
|
|
|
#include<hgl/shadergen/MaterialCreateInfo.h>
|
2023-03-06 14:06:20 +08:00
|
|
|
|
|
|
|
|
|
using namespace hgl;
|
|
|
|
|
using namespace hgl::graph;
|
|
|
|
|
using namespace hgl::shadergen;
|
|
|
|
|
|
2023-03-06 21:30:32 +08:00
|
|
|
|
bool PureColor2DMaterial()
|
2023-03-06 14:06:20 +08:00
|
|
|
|
{
|
2023-03-17 21:06:05 +08:00
|
|
|
|
MaterialCreateInfo mc("PureColor2D",1,false); //一个新材质,1个RT输出,默认使用Vertex/Fragment shader
|
2023-03-06 14:06:20 +08:00
|
|
|
|
|
|
|
|
|
//vertex部分
|
|
|
|
|
{
|
2023-03-17 10:08:41 +08:00
|
|
|
|
ShaderCreateInfoVertex *vsc=mc.GetVS(); //获取vertex shader creater
|
2023-03-06 14:06:20 +08:00
|
|
|
|
|
|
|
|
|
//以下代码会被展开为
|
|
|
|
|
/*
|
2023-03-14 22:22:35 +08:00
|
|
|
|
layout(location=?) in vec3 Position; //位置属性
|
2023-03-06 14:06:20 +08:00
|
|
|
|
*/
|
2023-03-06 21:30:32 +08:00
|
|
|
|
vsc->AddInput("vec2","Position"); //添加一个vec3类型的position属性输入
|
2023-03-06 14:06:20 +08:00
|
|
|
|
|
2023-03-06 21:30:32 +08:00
|
|
|
|
vsc->SetShaderCodes(R"(
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
gl_Position=vec4(Position,0,1);
|
|
|
|
|
})");
|
2023-03-06 14:06:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//添加一个名称为ColorMaterial的UBO定义,其内部有一个vec4 color的属性
|
|
|
|
|
//该代码会被展开为
|
|
|
|
|
/*
|
|
|
|
|
struct ColorMaterial
|
|
|
|
|
{
|
|
|
|
|
vec4 color;
|
|
|
|
|
};
|
|
|
|
|
*/
|
2023-03-09 23:21:52 +08:00
|
|
|
|
mc.AddStruct("ColorMaterial","vec4 color;");
|
2023-03-06 14:06:20 +08:00
|
|
|
|
|
|
|
|
|
//添加一个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部分
|
|
|
|
|
{
|
2023-03-17 10:08:41 +08:00
|
|
|
|
ShaderCreateInfoFragment *fsc=mc.GetFS(); //获取fragment shader creater
|
2023-03-06 14:06:20 +08:00
|
|
|
|
|
|
|
|
|
//以下代码会被展开为
|
|
|
|
|
/*
|
|
|
|
|
layout(location=?) out vec4 Color; //颜色输出
|
|
|
|
|
*/
|
|
|
|
|
fsc->AddOutput("vec4","Color"); //添加一个vec4类型的color属性输出
|
|
|
|
|
|
2023-03-06 21:30:32 +08:00
|
|
|
|
fsc->SetShaderCodes(R"(
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
Color=mtl.color;
|
|
|
|
|
})");
|
2023-03-06 14:06:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-14 22:22:35 +08:00
|
|
|
|
mc.CreateShader();
|
2023-03-08 14:02:51 +08:00
|
|
|
|
|
2023-03-15 17:50:33 +08:00
|
|
|
|
return(true);
|
2023-03-06 14:06:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-15 17:50:33 +08:00
|
|
|
|
bool VertexColor2DMaterial()
|
|
|
|
|
{
|
2023-03-17 21:06:05 +08:00
|
|
|
|
MaterialCreateInfo mc("VertexColor2D",1,false);
|
2023-03-15 17:50:33 +08:00
|
|
|
|
|
|
|
|
|
//vertex部分
|
|
|
|
|
{
|
2023-03-17 10:08:41 +08:00
|
|
|
|
ShaderCreateInfoVertex *vsc=mc.GetVS();
|
2023-03-15 17:50:33 +08:00
|
|
|
|
|
|
|
|
|
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部分
|
|
|
|
|
{
|
2023-03-17 10:08:41 +08:00
|
|
|
|
ShaderCreateInfoFragment *fsc=mc.GetFS();
|
2023-03-15 17:50:33 +08:00
|
|
|
|
|
|
|
|
|
fsc->AddOutput("vec4","Color");
|
|
|
|
|
|
|
|
|
|
fsc->SetShaderCodes(R"(
|
|
|
|
|
void main()
|
|
|
|
|
{
|
|
|
|
|
Color=Input.Color;
|
2023-03-16 21:54:03 +08:00
|
|
|
|
})");
|
2023-03-15 17:50:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mc.CreateShader();
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace glsl_compiler
|
|
|
|
|
{
|
|
|
|
|
bool Init();
|
|
|
|
|
void Close();
|
|
|
|
|
}//namespace glsl_compiler
|
|
|
|
|
|
2023-03-14 22:22:35 +08:00
|
|
|
|
int main()
|
2023-03-06 14:06:20 +08:00
|
|
|
|
{
|
2023-03-15 17:50:33 +08:00
|
|
|
|
if(!glsl_compiler::Init())
|
|
|
|
|
return -1;
|
|
|
|
|
|
2023-03-14 22:22:35 +08:00
|
|
|
|
PureColor2DMaterial();
|
2023-03-15 17:50:33 +08:00
|
|
|
|
VertexColor2DMaterial();
|
|
|
|
|
|
|
|
|
|
glsl_compiler::Close();
|
2023-03-14 22:22:35 +08:00
|
|
|
|
|
2023-03-06 14:06:20 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|