update ShaderMaker

This commit is contained in:
hyzboy 2019-12-16 20:35:51 +08:00
parent 557a6c3f56
commit f9531064b6
15 changed files with 412 additions and 108 deletions

@ -1 +1 @@
Subproject commit f912c32898dbf558635c9d5a2d50ff887c1402ae
Subproject commit fd5aa3ad51ece55a1b51fe6bfb271db6844ae291

@ -1 +1 @@
Subproject commit 487ce954dbf669d724b36ee00efff972bc37a5fe
Subproject commit 16397c1849c2b180bc7fe1e5ef103ba03a8990d5

@ -1 +1 @@
Subproject commit d6c4a8fb2defc505d401d39097c00fdd8be9ae33
Subproject commit 2cf939e8c37494922ca2991902a0fe50baa2eaea

2
CMCore

@ -1 +1 @@
Subproject commit e3a58db799b9588d0e10e3eb804ba3403f18dff4
Subproject commit 51be93f2d7fe2ad0a48257c27dce03313e3e1831

View File

@ -3,22 +3,46 @@
#include<hgl/graph/shader/node/finished.h>
#include<hgl/graph/shader/node/vertex_input.h>
#include<hgl/type/StringList.h>
BEGIN_SHADER_NAMESPACE
using NodeList=List<node::Node *>;
enum class API
{
OpenGLCore,
OpenGLES,
Vulkan
};
class ShaderMaker
{
protected:
API api;
uint api_version;
UTF8StringList shader_source;
protected:
node::VertexInput *vi_node;
node::Finished *fin_node;
List<node::Node *> node_stack;
protected:
NodeList node_list[node::NodeType::NODE_TYPE_RANGE_SIZE];
uint in_location=0;
uint out_location=0;
uint binding=0;
protected:
virtual void MakeHeader();
virtual void MakeVertexInput(const NodeList &);
virtual bool MakeHeader();
virtual bool MakeVertexInput();
virtual void MakeConstValue(const NodeList &);
virtual void MakeTextureInput(const NodeList &);
virtual void MakeUBOInput(const NodeList &);
@ -27,11 +51,20 @@ protected:
public:
ShaderMaker(node::Finished *fn){fin_node=fn;}
ShaderMaker(const API &a,const uint av,node::Finished *fn,node::VertexInput *vi=nullptr)
{
api=a;
api_version=av;
fin_node=fn;
vi_node=vi;
}
virtual ~ShaderMaker()=default;
virtual bool Check();
virtual bool Make();
virtual bool SaveToFile(const OSString &);
};//class ShaderMaker
END_SHADER_NAMESPACE
#endif//HGL_GRAPH_SHADER_MAKER_INCLUDE

View File

@ -26,6 +26,9 @@ public:
VertexFinished():Finished("Vertex Output")
{
SHADER_INPUT_PARAM(true,Position, Float3)
SHADER_OUTPUT_PARAM(FragmentPosition,Float3)
SHADER_OUTPUT_PARAM(FragmentTexCoord,Float2)
}
~VertexFinished()=default;

View File

@ -11,7 +11,7 @@
BEGIN_SHADER_NODE_NAMESPACE
#define SHADER_INPUT_PARAM(mj,name,type) AddInput(mj,#name,SHADER_PARAM_NAMESPACE::ParamType::type);
#define SHADER_OUTPUT_PARAM(name,type) AddOutput(#name,SHADER_PARAM_NAMESPACE::ParamType::type));
#define SHADER_OUTPUT_PARAM(name,type) AddOutput(#name,SHADER_PARAM_NAMESPACE::ParamType::type);
using InputParamList=ObjectList<param::InputParam>;
using OutputParamList=ObjectList<param::OutputParam>;
@ -23,11 +23,11 @@ using OutputParamMapByName=Map<UTF8String,param::OutputParam *>;
*/
class Node
{
NodeType type;
NodeType node_type;
protected:
UTF8String user_name; ///<节点用户自定义名称
UTF8String node_name; ///<节点用户自定义名称
InputParamList input_params;
OutputParamList output_params;
@ -37,8 +37,8 @@ protected:
protected:
void AddInput (bool mj,const UTF8String &n,const param::ParamType &pt);
void AddOutput (const UTF8String &n,const param::ParamType &pt);
param::InputParam * AddInput (bool mj,const UTF8String &n,const param::ParamType &pt);
param::OutputParam *AddOutput (const UTF8String &n,const param::ParamType &pt);
public:
@ -50,14 +50,14 @@ public:
public:
Node(const NodeType &nt){type=nt;}
Node(const NodeType &nt,const UTF8String &n){type=nt;user_name=n;}
Node(const NodeType &nt){node_type=nt;}
Node(const NodeType &nt,const UTF8String &n){node_type=nt;node_name=n;}
virtual ~Node()=default;
const NodeType GetNodeType ()const{return type;}
const NodeType GetNodeType ()const{return node_type;}
const UTF8String & GetUsername ()const{return user_name;}
void SetUsername (const UTF8String &n){user_name=n;}
const UTF8String & GetNodeName ()const{return node_name;}
void SetNodeName (const UTF8String &n){node_name=n;}
public: //参数相关
@ -71,6 +71,8 @@ public: //参数相关
virtual bool IsOutput(param::OutputParam *);
virtual bool Check(); ///<检测当前节点是否可用
virtual bool GenCode(UTF8StringList &){return false;}
};//class Node
END_SHADER_NODE_NAMESPACE
#endif//HGL_GRAPH_SHADER_NODE_INCLUDE

View File

@ -2,46 +2,70 @@
#define HGL_GRAPH_SHADER_NODE_TEXTURE_INCLUDE
#include<hgl/graph/shader/node/vector.h>
#include<hgl/graph/shader/node/inout.h>
#include<hgl/graph/shader/node/node.h>
BEGIN_SHADER_NODE_NAMESPACE
class texture1D:public InputOutput
class texture:public Node
{
param::ParamType texture_type;
public:
texture(const param::ParamType &pt):Node(NodeType::Texture)
{
texture_type=pt;
}
const param::ParamType &GetTextureType()const{return texture_type;}
};//class texture:public Node
class texture1D:public texture
{
public:
texture1D():InputOutput("texture1D")
texture1D():texture(param::ParamType::Texture1D)
{
SHADER_INPUT_PARAM(U,FLOAT1)
SHADER_INPUT_PARAM(false,U,Float1)
}
};//class texture1D:public InputOutput
};//class texture1D:public texture
class texture2D:public InputOutput
class texture2D:public texture
{
public:
texture2D():InputOutput("texture2D")
texture2D():texture(param::ParamType::Texture2D)
{
SHADER_INPUT_PARAM(UV,FLOAT2)
SHADER_INPUT_PARAM(false,UV,Float2)
}
};//class texture2D:public InputOutput
};//class texture2D:public texture
class texture3D:public InputOutput
class textureRect:public texture
{
public:
texture3D():InputOutput("texture3D")
textureRect():texture(param::ParamType::TextureRect)
{
SHADER_INPUT_PARAM(UVD,FLOAT3)
SHADER_INPUT_PARAM(false,UV,UInteger2)
}
};//class texture3D:public InputOutput
};//class textureRect:public texture
class textureCube:public InputOutput
class texture3D:public texture
{
public:
textureCube():InputOutput("textureCube")
texture3D():texture(param::ParamType::Texture3D)
{
SHADER_INPUT_PARAM(XYZ,FLOAT3)
SHADER_INPUT_PARAM(false,UVD,Float3)
}
};//class TextureCube:public InputOutput
};//class texture3D:public texture
class textureCube:public texture
{
public:
textureCube():texture(param::ParamType::TextureCube)
{
SHADER_INPUT_PARAM(false,XYZ,Float3)
}
};//class TextureCube:public texture
END_SHADER_NODE_NAMESPACE
#endif//HGL_GRAPH_SHADER_NODE_TEXTURE_INCLUDE

View File

@ -2,55 +2,64 @@
#define HGL_GRAPH_SHADER_NODE_VECTOR_INCLUDE
#include<hgl/graph/shader/param/out.h>
#include<hgl/graph/shader/node/out.h>
#include<hgl/graph/shader/node/node.h>
BEGIN_SHADER_NODE_NAMESPACE
class float1:public Output
class Parameter:public Node
{
param::ParamType param_type;
public:
Parameter(const param::ParamType &pt):Node(NodeType::Param)
{
param_type=pt;
}
const param::ParamType &GetParamType()const{return param_type;}
};//class Parameter:public Node
#define SHADER_PARAMETER_CONSTRUCT_FUNC(name,value) name():Parameter(param::ParamType::name) \
{ \
SHADER_OUTPUT_PARAM(value,name) \
}
class Float1:public Parameter
{
float x;
public:
float1():Output("float1")
{
SHADER_OUTPUT_PARAM(X,FLOAT_1)
}
};//class float1:public Output
SHADER_PARAMETER_CONSTRUCT_FUNC(Float1,X)
};//class float1:public Parameter
class float2:public Output
class Float2:public Parameter
{
float x,y;
public:
float2():Output("float2")
{
SHADER_OUTPUT_PARAM(XY,FLOAT_2)
}
};//class float2:public Output
SHADER_PARAMETER_CONSTRUCT_FUNC(Float2,XY)
};//class float2:public Parameter
class float3:public Output
class Float3:public Parameter
{
float x,y,z;
public:
float3():Output("float3")
{
SHADER_OUTPUT_PARAM(XYZ,FLOAT_3)
}
};//class float3:public Output
SHADER_PARAMETER_CONSTRUCT_FUNC(Float3,XYZ)
};//class Float3:public Parameter
class float4:public Output
class Float4:public Parameter
{
float x,y,z,w;
public:
float4():Output("float4")
{
SHADER_OUTPUT_PARAM(XYZW,FLOAT_4)
}
};//class float4:public Output
SHADER_PARAMETER_CONSTRUCT_FUNC(Float4,XYZW)
};//class Float4:public Parameter
#undef SHADER_PARAMETER_CONSTRUCT_FUNC
END_SHADER_NODE_NAMESPACE
#endif//HGL_GRAPH_SHADER_NODE_VECTOR_INCLUDE

View File

@ -10,9 +10,19 @@ class VertexInput:public Node
{
public:
VertexInput(const UTF8String &n,const param::ParamType &pt):Node(NodeType::VertexInput,n)
VertexInput():Node(NodeType::VertexInput,"VertexInput")
{
AddOutput(n,pt);
}
param::OutputParam *Add(const UTF8String &n,const param::ParamType &pt)
{
//未来还要做个数判断之类的
if(pt<param::ParamType::Float1
||pt>param::ParamType::UInteger4)
return(nullptr);
return AddOutput(n,pt);
}
};//class VertexInput:public Node
END_SHADER_NODE_NAMESPACE

View File

@ -2,6 +2,7 @@
#define HGL_GRAPH_SHADER_PARAM_TYPE_INCLUDE
#include<hgl/graph/shader/common.h>
#include<hgl/type/BaseString.h>
BEGIN_SHADER_PARAM_NAMESPACE
/**
@ -9,9 +10,13 @@ BEGIN_SHADER_PARAM_NAMESPACE
*/
enum class ParamType
{
BOOL=1,
BOOL=0,
FLOAT,DOUBLE,INT,UINT,MATRIX, //不区分1/2/3/4通道数量的类型
FLOAT,INT,UINT,MATRIX, //不区分1/2/3/4通道数量的类型
Bool1,
Bool2,
Bool3,
Bool4,
Float1,
Float2,
@ -28,9 +33,20 @@ enum class ParamType
UInteger3,
UInteger4,
Matrix3x3,
Double1,
Double2,
Double3,
Double4,
Matrix2,
Matrix3,
Matrix4,
Matrix3x2,
Matrix2x3,
Matrix4x2,
Matrix2x4,
Matrix4x3,
Matrix3x4,
Matrix4x4,
Texture1D,
Texture2D,
@ -42,20 +58,19 @@ enum class ParamType
Texture2DArray,
TextureCubeArray,
FLOAT_1_STREAM,
FLOAT_2_STREAM,
FLOAT_3_STREAM,
FLOAT_4_STREAM,
Texture1DShadow,
Texture2DShadow,
TextureCubeShadow,
TextureRectShadow,
INT_1_STREAM,
INT_2_STREAM,
INT_3_STREAM,
INT_4_STREAM,
Texture1DArrayShadow,
Texture2DArrayShadow,
TextureCubeArrayShadow,
UINT_1_STREAM,
UINT_2_STREAM,
UINT_3_STREAM,
UINT_4_STREAM,
Texture2DMultiSample,
Texture2DMultiSampleArray,
TBO,
ARRAY_1D, //阵列
ARRAY_2D, //2D阵列
@ -63,9 +78,11 @@ enum class ParamType
UBO, //UBO name
NODE, //另一个节点,只可做为输入参数
BEGIN_RANGE =Float1,
BEGIN_RANGE =BOOL,
END_RANGE =NODE,
RANGE_SIZE =(END_RANGE-BEGIN_RANGE+1)
};//enum class ParamType
const char *GetTypename(const ParamType);
END_SHADER_PARAM_NAMESPACE
#endif//HGL_GRAPH_SHADER_PARAM_TYPE_INCLUDE

View File

@ -7,7 +7,7 @@
BEGIN_SHADER_NAMESPACE
namespace
{
#define SHADER_VERTEX_INPUT_STREAM_PARAM_NAME "Position" //<顶点数据输入流,输出参数名称
#define SHADER_VERTEX_INPUT_STREAM_POSITION_NAME "Position" //<顶点数据输入流,输出参数名称
#define SHADER_VERTEX_POSITION_INPUT_PARAM_NAME "Position" //<顶点shader最终节点输入参数名称
//**********************************************************************注:它们两个名字一样只是碰巧****************
@ -16,21 +16,22 @@ namespace
{
node::VertexFinished vs_fin_node; //创建一个vs最终节点
//创建一个顶点输入节点
node::VertexInput ni( SHADER_VERTEX_INPUT_STREAM_PARAM_NAME, //该节点shader名称
param::ParamType::Float3); //该节点数据类型
node::VertexInput vi; //创建一个顶点输入节点
param::OutputParam *op=ni.GetOutput(SHADER_VERTEX_INPUT_STREAM_PARAM_NAME); //获取节点中的输出参数
param::OutputParam *op=vi.Add( SHADER_VERTEX_INPUT_STREAM_POSITION_NAME, //该节点输出参数名称
param::ParamType::Float3); //该节点数据类型
//对接顶点输入节点到VS最终节点
vs_fin_node.JoinInput( SHADER_VERTEX_POSITION_INPUT_PARAM_NAME, //最终节点中这个输入参数的名称
&ni,op); //要接入的节点以及它的输出参数
&vi,op); //要接入的节点以及它的输出参数
ShaderMaker vs_maker(&vs_fin_node);
ShaderMaker vs_maker(API::Vulkan,450,&vs_fin_node,&vi);
if(!vs_maker.Make())
return(false);
vs_maker.SaveToFile(OS_TEXT("default.vert"));
return(true);
}
@ -38,7 +39,7 @@ namespace
{
node::FragmentFinished fs_fin_node;
ShaderMaker fs_maker(&fs_fin_node);
ShaderMaker fs_maker(API::Vulkan,450,&fs_fin_node);
if(!fs_maker.Make())
return(false);

View File

@ -1,9 +1,19 @@
#include<hgl/graph/shader/ShaderMaker.h>
#include<hgl/graph/shader/param/in.h>
#include<hgl/graph/shader/node/texture.h>
#include<hgl/type/Set.h>
#include<hgl/log/LogInfo.h>
#include<hgl/io/FileOutputStream.h>
#include<hgl/io/DataOutputStream.h>
#include<hgl/io/TextOutputStream.h>
BEGIN_SHADER_NAMESPACE
namespace
{
}//namespace
bool ShaderMaker::Check()
{
if(!fin_node)
@ -22,21 +32,21 @@ bool ShaderMaker::Check()
while(prev.GetEnd(cur))
{
type=cur->GetNodeType();
if(type<node::NodeType::BEGIN_NODE_TYPE_RANGE
||type>node::NodeType::END_NODE_TYPE_RANGE)
{
LOG_ERROR(U8_TEXT("node type error!"));
return(false);
}
ipl =&(cur->GetInputParamList());
count =ipl->GetCount();
ip =ipl->GetData();
for(i=0;i<count;i++)
{
type=cur->GetNodeType();
if(type<node::NodeType::BEGIN_NODE_TYPE_RANGE
||type>node::NodeType::END_NODE_TYPE_RANGE)
{
LOG_ERROR(U8_TEXT("node type error!"));
return(false);
}
if(!(*ip)->Check())
return(false);
@ -58,17 +68,56 @@ bool ShaderMaker::Check()
node_list[int(type)-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)].Add(cur);
prev.Delete(cur);
post.Add(cur);
node_stack.Add(cur);
}
return(true);
}
void ShaderMaker::MakeHeader()
bool ShaderMaker::MakeHeader()
{
if(api==API::Vulkan||api==API::OpenGLCore)
shader_source.Add("#version "+UTF8String(api_version)+" core"); else
if(api==API::OpenGLES)
shader_source.Add("#version "+UTF8String(api_version)+" es"); else
{
return(false);
}
shader_source.Add("layout(push_constant) uniform PushConstant");
shader_source.Add("{");
shader_source.Add(" mat4 local_to_world;");
shader_source.Add("}pc;");
shader_source.Add("");
return(true);
}
void ShaderMaker::MakeVertexInput(const NodeList &nl)
bool ShaderMaker::MakeVertexInput()
{
if(!vi_node)return(true);
const node::OutputParamList &op_list=vi_node->GetOutputParamList();
const uint count=op_list.GetCount();
if(count<=0)return(false);
param::OutputParam **op=op_list.GetData();
param::ParamType pt;
for(uint i=0;i<count;i++)
{
pt=(*op)->GetType();
shader_source.Add("layout(location="+UTF8String(in_location)+") in "+GetTypename(pt)+" "+(*op)->GetName()+";");
++op;
++in_location;
}
shader_source.Add("");
return(true);
}
void ShaderMaker::MakeConstValue(const NodeList &nl)
@ -77,6 +126,24 @@ void ShaderMaker::MakeConstValue(const NodeList &nl)
void ShaderMaker::MakeTextureInput(const NodeList &nl)
{
const uint count=nl.GetCount();
if(count<=0)return;
node::texture **tex_node=(node::texture **)nl.GetData();
param::ParamType tt;
for(uint i=0;i<count;i++)
{
tt=(*tex_node)->GetTextureType();
shader_source.Add("layout(binding="+UTF8String(binding)+") uniform "+GetTypename(tt)+" "+(*tex_node)->GetNodeName()+";");
++tex_node;
++binding;
}
shader_source.Add("");
}
void ShaderMaker::MakeUBOInput(const NodeList &nl)
@ -85,25 +152,73 @@ void ShaderMaker::MakeUBOInput(const NodeList &nl)
void ShaderMaker::MakeOutput()
{
node::OutputParamList &opl=fin_node->GetOutputParamList();
const uint count=opl.GetCount();
if(count<=0)return;
param::OutputParam **op=opl.GetData();
param::ParamType pt;
for(uint i=0;i<count;i++)
{
pt=(*op)->GetType();
shader_source.Add("layout(location="+UTF8String(out_location)+") out "+GetTypename(pt)+" "+(*op)->GetName()+";");
++op;
++out_location;
}
shader_source.Add("");
}
void ShaderMaker::MakeFinished()
{
shader_source.Add("void main()");
shader_source.Add("{");
shader_source.Add("}");
}
bool ShaderMaker::Make()
{
shader_source.Clear();
node_stack.ClearData();
in_location=0;
out_location=0;
binding=0;
if(!Check())
return(false);
MakeHeader();
MakeVertexInput( node_list[int(node::NodeType::VertexInput )-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)]);
MakeConstValue( node_list[int(node::NodeType::Const )-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)]);
MakeTextureInput( node_list[int(node::NodeType::Texture )-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)]);
MakeUBOInput( node_list[int(node::NodeType::UBO )-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)]);
MakeOutput();
MakeFinished();
if(!MakeHeader())return(false);
if(!MakeVertexInput())return(false);
MakeConstValue (node_list[int(node::NodeType::Const )-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)]);
MakeTextureInput(node_list[int(node::NodeType::Texture )-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)]);
MakeUBOInput (node_list[int(node::NodeType::UBO )-int(node::NodeType::BEGIN_NODE_TYPE_RANGE)]);
MakeOutput ();
MakeFinished ();
return(true);
}
bool ShaderMaker::SaveToFile(const OSString &filename)
{
io::FileOutputStream fos;
if(!fos.CreateTrunc(filename))
return(false);
io::TextOutputStream *tos=io::CreateTextOutputStream<char>(&fos);
tos->WriteText(shader_source);
delete tos;
return(true);
}
END_SHADER_NAMESPACE

View File

@ -1,27 +1,31 @@
#include<hgl/graph/shader/node/node.h>
BEGIN_SHADER_NODE_NAMESPACE
void Node::AddInput(bool mj,const UTF8String &n,const param::ParamType &pt)
param::InputParam *Node::AddInput(bool mj,const UTF8String &n,const param::ParamType &pt)
{
param::InputParam *ip=new param::InputParam(mj,n,pt);
input_params.Add(ip);
input_params_by_name.Add(n,ip);
return ip;
}
void Node::AddOutput(const UTF8String &n,const param::ParamType &pt)
param::OutputParam *Node::AddOutput(const UTF8String &n,const param::ParamType &pt)
{
param::OutputParam *op=new param::OutputParam(n,pt);
output_params.Add(op);
output_params_by_name.Add(n,op);
return op;
}
bool Node::JoinInput(const UTF8String &param_name,node::Node *n,param::OutputParam *op)
{
if(param_name.IsEmpty()||!n||op)
if(param_name.IsEmpty()||!n||!op)
return(false);
if(!n->IsOutput(op))

View File

@ -0,0 +1,86 @@
#include<hgl/graph/shader/param/type.h>
BEGIN_SHADER_PARAM_NAMESPACE
const char *GetTypename(const ParamType pt)
{
constexpr char *name[]=
{
"?boolean?",
"?float?",
"?double?",
"?int?",
"?uint?",
"?matrix?",
"boolean",
"bvec2",
"bvec3",
"bvec4",
"float",
"vec2",
"vec3",
"vec4",
"int",
"ivec2",
"ivec3",
"ivec4",
"uint",
"uvec2",
"uvec3",
"uvec4",
"double",
"dvec2",
"dvec3",
"dvec4",
"mat2",
"mat3",
"mat4",
"mat3x2",
"mat2x3",
"mat4x2",
"mat2x4",
"mat4x3",
"mat3x4",
"sampler1D",
"sampler2D",
"sampler3D",
"samplerCube",
"sampler2DRect",
"sampler1DArray",
"sampler2DArray",
"samplerCubeArray",
"sampler1DShadow",
"sampler2DShadow",
"samplerCubeShadow",
"sampler2DRectShadow",
"sampler1DArrayShadow",
"sampler2DArrayShadow",
"samplerCubeArrayShadow",
"sampler2DMS",
"sampler2DMSArray",
"samplerBuffer",
"?ARRAY_1D?",
"?ARRAY_2D?",
"?UBO?",
"?NODE?"
};
if(pt<ParamType::BEGIN_RANGE
||pt>ParamType::END_RANGE)return(nullptr);
return name[size_t(pt)-size_t(ParamType::BEGIN_RANGE)];
}
END_SHADER_PARAM_NAMESPACE