From 686372b60fcb31923b0651d9f2e47349d948266a Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 18 Jun 2024 01:28:53 +0800 Subject: [PATCH] added ShaderVariableType.h --- CMCore | 2 +- CMSceneGraph | 2 +- inc/hgl/graph/mtl/Material2DCreateConfig.h | 2 +- inc/hgl/graph/mtl/Material3DCreateConfig.h | 2 +- inc/hgl/graph/mtl/ShaderVariableType.h | 365 ++++++++++++++++++++ inc/hgl/shadergen/ShaderCreateInfo.h | 2 +- inc/hgl/shadergen/ShaderCreateInfoVertex.h | 2 +- src/ShaderGen/3d/M_BillboardDynamicSize.cpp | 2 +- src/ShaderGen/CMakeLists.txt | 1 + src/ShaderGen/MaterialFileData.h | 2 +- src/ShaderGen/ShaderCreateInfo.cpp | 4 +- src/ShaderGen/ShaderCreateInfoVertex.cpp | 4 +- 12 files changed, 378 insertions(+), 12 deletions(-) create mode 100644 inc/hgl/graph/mtl/ShaderVariableType.h diff --git a/CMCore b/CMCore index ee2bf2b9..a82810d5 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit ee2bf2b9b854871b7af3c5e46007fe979119a338 +Subproject commit a82810d58aa2dab60360a278038aa31ab7bcb116 diff --git a/CMSceneGraph b/CMSceneGraph index deb54cde..66518a8a 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit deb54cdeaf5889d95e37524c00317c6274f3e2f7 +Subproject commit 66518a8af330139c482755c5b249a6044f9104ff diff --git a/inc/hgl/graph/mtl/Material2DCreateConfig.h b/inc/hgl/graph/mtl/Material2DCreateConfig.h index f9ef2447..40a73b32 100644 --- a/inc/hgl/graph/mtl/Material2DCreateConfig.h +++ b/inc/hgl/graph/mtl/Material2DCreateConfig.h @@ -12,7 +12,7 @@ struct Material2DCreateConfig:public MaterialCreateConfig bool local_to_world; ///<包含LocalToWorld矩阵 - VAT position_format; /// +#include +#include +#include + +namespace hgl +{ + namespace graph + { + enum class ShaderVariableBaseType:uint8 + { + Scalar=0, + Vector, + Materix, + Sampler, + Image, + AtomicCounter, + ImplicitConversion, + + ENUM_CLASS_RANGE(Scalar,Materix) + };//enum class ShaderVariableBaseType + + using SVBaseType=ShaderVariableBaseType; + + #pragma pack(push,1) + + struct ShaderVariableType + { + union + { + struct + { + union + { + struct + { + SVBaseType base_type; + + union + { + struct Scalar + { + VABaseType type; + }scalar; + + struct Vector + { + VABaseType type; + uint8 vec_size; + }vector; + + struct Matrix + { + VABaseType type; + uint8 n; + uint8 m; + }matrix; + + struct Sampler + { + SamplerType type; + }sampler; + + struct Image + { + ShaderImageType type; + }image; + }; + }; + + uint32 type_code; + }; + + uint32 array_size; + }; + + uint64 svt_code; + }; + + public: + + ShaderVariableType() + { + svt_code=0; + } + + ShaderVariableType(const ShaderVariableType &svt) + { + svt_code=svt.svt_code; + } + + ShaderVariableType(const VABaseType &vabt,const uint32 count) + { + svt_code=0; + + base_type=SVBaseType::Scalar; + + scalar.type=vabt; + + array_size=count; + } + + ShaderVariableType(const VABaseType &vabt,const uint8 size,const uint32 count) + { + svt_code=0; + + base_type=SVBaseType::Vector; + + vector.type=vabt; + vector.vec_size=size; + + array_size=count; + } + + ShaderVariableType(const VABaseType &vabt,const uint8 row,const uint8 col,const uint32 count) + { + svt_code=0; + + base_type=SVBaseType::Materix; + + matrix.type=vabt; + matrix.n=col; + matrix.m=row; + + array_size=count; + } + + ShaderVariableType(const SamplerType &st,const uint32 count) + { + svt_code=0; + + base_type=SVBaseType::Sampler; + + sampler.type=st; + + array_size=count; + } + + ShaderVariableType(const ShaderImageType &sit,const uint32 count) + { + svt_code=0; + + base_type=SVBaseType::Image; + + image.type=sit; + + array_size=count; + } + + const bool Check()const + { + if(!RangeCheck(base_type))return(false); + + RANGE_CHECK_RETURN_FALSE(base_type) + + if(base_type==SVBaseType::Scalar) + return(true); + + if(base_type==SVBaseType::Vector) + { + if(vector.vec_size<2||vector.vec_size>4)return(false); + + return(true); + } + + if(base_type==SVBaseType::Materix) + { + if(matrix.m==0) + { + if(matrix.n<2||matrix.n>4)return(false); + } + else + { + if(matrix.n<2||matrix.n>4)return(false); + if(matrix.m<2||matrix.m>4)return(false); + } + + return(true); + } + + if(base_type==SVBaseType::Sampler) + return RangeCheck(sampler.type); + + if(base_type==SVBaseType::Image) + return RangeCheck(image.type); + + return(true); + } + + int Comp(const ShaderVariableType &svt)const + { + int off=(int)base_type-(int)svt.base_type; + + if(off)return(off); + + if(base_type==SVBaseType::Scalar) + return int(scalar.type)-int(svt.scalar.type); + + if(base_type==SVBaseType::Vector) + { + off=int(vector.type)-int(svt.vector.type); + + if(off)return(off); + + off=vector.vec_size-svt.vector.vec_size; + + if(off)return(off); + + return int(vector.type)-int(svt.vector.type); + } + + if(base_type==SVBaseType::Materix) + { + off=int(matrix.type)-int(svt.matrix.type); + + if(off)return(off); + + off=matrix.n-svt.matrix.n; + + if(off)return(off); + + return matrix.m-svt.matrix.m; + } + + if(base_type==SVBaseType::Sampler) + return int(sampler.type)-int(svt.sampler.type); + + if(base_type==SVBaseType::Image) + return int(image.type)-int(svt.image.type); + + return 0; + } + + CompOperator(const ShaderVariableType &,Comp) + + const uint64 ToCode()const{return svt_code;} + const bool FromCode(const uint64 code) + { + svt_code=code; + + return Check(); + } + + const bool From(const VAType &vat,const uint16 count=1) + { + array_size=count; + + if(vat.vec_size==1) + { + base_type=SVBaseType::Scalar; + scalar.type=vat.basetype; + + return(true); + } + else if(vat.vec_size<=4) + { + base_type=SVBaseType::Vector; + + vector.type=vat.basetype; + vector.vec_size=vat.vec_size; + + return(true); + } + + return(false); + } + + static const ShaderVariableType Scalar(const VABaseType &vabt,const uint count=1) + { + ShaderVariableType svt; + + svt.base_type =SVBaseType::Scalar; + svt.scalar.type =vabt; + svt.array_size =count; + + return svt; + } + };//struct ShaderVariableType + #pragma pack(pop) + + using SVType=ShaderVariableType; + + const SVType SVT_BOOL (VABaseType::Bool, 1); + const SVType SVT_INT (VABaseType::Int, 1); + const SVType SVT_UINT (VABaseType::UInt, 1); + const SVType SVT_FLOAT (VABaseType::Float, 1); + const SVType SVT_DOUBLE(VABaseType::Double, 1); + + const SVType SVT_BVEC2 (VABaseType::Bool, 2,1); + const SVType SVT_BVEC3 (VABaseType::Bool, 3,1); + const SVType SVT_BVEC4 (VABaseType::Bool, 4,1); + + const SVType SVT_IVEC2 (VABaseType::Int, 2,1); + const SVType SVT_IVEC3 (VABaseType::Int, 3,1); + const SVType SVT_IVEC4 (VABaseType::Int, 4,1); + + const SVType SVT_UVEC2 (VABaseType::UInt, 2,1); + const SVType SVT_UVEC3 (VABaseType::UInt, 3,1); + const SVType SVT_UVEC4 (VABaseType::UInt, 4,1); + + const SVType SVT_VEC2 (VABaseType::Float, 2,1); + const SVType SVT_VEC3 (VABaseType::Float, 3,1); + const SVType SVT_VEC4 (VABaseType::Float, 4,1); + + const SVType SVT_DVEC2 (VABaseType::Double, 2,1); + const SVType SVT_DVEC3 (VABaseType::Double, 3,1); + const SVType SVT_DVEC4 (VABaseType::Double, 4,1); + + const SVType SVT_MAT2 (VABaseType::Float, 2,2,1); + const SVType SVT_MAT3 (VABaseType::Float, 3,3,1); + const SVType SVT_MAT4 (VABaseType::Float, 4,4,1); + const SVType SVT_MAT2x3(VABaseType::Float, 2,3,1); + const SVType SVT_MAT2x4(VABaseType::Float, 2,4,1); + const SVType SVT_MAT3x2(VABaseType::Float, 3,2,1); + const SVType SVT_MAT3x4(VABaseType::Float, 3,4,1); + const SVType SVT_MAT4x2(VABaseType::Float, 4,2,1); + const SVType SVT_MAT4x3(VABaseType::Float, 4,3,1); + + const SVType SVT_Sampler1D(SamplerType::Sampler1D, 1); + const SVType SVT_Sampler2D(SamplerType::Sampler2D, 1); + const SVType SVT_Sampler3D(SamplerType::Sampler3D, 1); + + const SVType SVT_SamplerCube(SamplerType::SamplerCube, 1); + const SVType SVT_Sampler2DRect(SamplerType::Sampler2DRect, 1); + + const SVType SVT_Sampler1DArray(SamplerType::Sampler1DArray,1); + const SVType SVT_Sampler2DArray(SamplerType::Sampler2DArray,1); + + const SVType SVT_SamplerCubeArray(SamplerType::SamplerCubeArray,1); + + const SVType SVT_SamplerBuffer(SamplerType::SamplerBuffer,1); + + const SVType SVT_Sampler2DMS(SamplerType::Sampler2DMS,1); + const SVType SVT_Sampler2DMSArray(SamplerType::Sampler2DMSArray,1); + + const SVType SVT_Sampler1DShadow(SamplerType::Sampler1DShadow,1); + const SVType SVT_Sampler2DShadow(SamplerType::Sampler2DShadow,1); + + const SVType SVT_SamplerCubeShadow(SamplerType::SamplerCubeShadow,1); + const SVType SVT_Sampler2DRectShadow(SamplerType::Sampler2DRectShadow,1); + + const SVType SVT_Sampler1DArrayShadow(SamplerType::Sampler1DArrayShadow,1); + const SVType SVT_Sampler2DArrayShadow(SamplerType::Sampler2DArrayShadow,1); + const SVType SVT_SamplerCubeArrayShadow(SamplerType::SamplerCubeArrayShadow,1); + + const SVType SVT_Image1D(ShaderImageType::Image1D,1); + const SVType SVT_Image2D(ShaderImageType::Image2D,1); + const SVType SVT_Image3D(ShaderImageType::Image3D,1); + + const SVType SVT_ImageCube(ShaderImageType::ImageCube,1); + const SVType SVT_Image2DRect(ShaderImageType::Image2DRect,1); + + const SVType SVT_Image1DArray(ShaderImageType::Image1DArray,1); + const SVType SVT_Image2DArray(ShaderImageType::Image2DArray,1); + + const SVType SVT_ImageCubeArray(ShaderImageType::ImageCubeArray,1); + + const SVType SVT_ImageBuffer(ShaderImageType::ImageBuffer,1); + + const SVType SVT_Image2DMS(ShaderImageType::Image2DMS,1); + const SVType SVT_Image2DMSArray(ShaderImageType::Image2DMSArray,1); + }//namespace graph +}//namespace hgl diff --git a/inc/hgl/shadergen/ShaderCreateInfo.h b/inc/hgl/shadergen/ShaderCreateInfo.h index dc5308e0..e6515fc0 100644 --- a/inc/hgl/shadergen/ShaderCreateInfo.h +++ b/inc/hgl/shadergen/ShaderCreateInfo.h @@ -76,7 +76,7 @@ public: bool AddDefine(const AnsiString &m,const AnsiString &v); - int AddOutput(const graph::VAT &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth); + int AddOutput(const graph::VAType &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth); int AddOutput(const AnsiString &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth); void AddFunction(const char *str){function_list.Add(str);} diff --git a/inc/hgl/shadergen/ShaderCreateInfoVertex.h b/inc/hgl/shadergen/ShaderCreateInfoVertex.h index f73b47a7..975c7db3 100644 --- a/inc/hgl/shadergen/ShaderCreateInfoVertex.h +++ b/inc/hgl/shadergen/ShaderCreateInfoVertex.h @@ -16,7 +16,7 @@ namespace hgl ShaderCreateInfoVertex(MaterialDescriptorInfo *); ~ShaderCreateInfoVertex()=default; - int AddInput(const graph::VAT &type,const AnsiString &name,const VkVertexInputRate input_rate=VK_VERTEX_INPUT_RATE_VERTEX,const VertexInputGroup &group=VertexInputGroup::Basic); + int AddInput(const graph::VAType &type,const AnsiString &name,const VkVertexInputRate input_rate=VK_VERTEX_INPUT_RATE_VERTEX,const VertexInputGroup &group=VertexInputGroup::Basic); int AddInput(const AnsiString &type,const AnsiString &name,const VkVertexInputRate input_rate=VK_VERTEX_INPUT_RATE_VERTEX,const VertexInputGroup &group=VertexInputGroup::Basic); int hasInput(const char *); diff --git a/src/ShaderGen/3d/M_BillboardDynamicSize.cpp b/src/ShaderGen/3d/M_BillboardDynamicSize.cpp index 28ef8b0b..037e9581 100644 --- a/src/ShaderGen/3d/M_BillboardDynamicSize.cpp +++ b/src/ShaderGen/3d/M_BillboardDynamicSize.cpp @@ -32,7 +32,7 @@ void main() 1 ); - Output.TexCoord=vec2(BillboardVertex[i].x+0.5,BillboardVertex[i].y*-1+0.5); + Output.TexCoord=vec2(BillboardVertex[i].x+0.5,BillboardVertex[i].y*-1.0+0.5); EmitVertex(); } diff --git a/src/ShaderGen/CMakeLists.txt b/src/ShaderGen/CMakeLists.txt index 906a02f7..e971504a 100644 --- a/src/ShaderGen/CMakeLists.txt +++ b/src/ShaderGen/CMakeLists.txt @@ -39,6 +39,7 @@ set(STD_MTL_HEADER_PATH ${ROOT_INCLUDE_PATH}/hgl/graph/mtl) SET(SHADERGEN_COMMON_FILES ${STD_MTL_HEADER_PATH}/UniformBuffer.h ${STD_MTL_HEADER_PATH}/UBOCommon.h ${STD_MTL_HEADER_PATH}/SamplerName.h + ${STD_MTL_HEADER_PATH}/ShaderVariableType.h common/MFCommon.h common/MFGetPosition.h common/MFGetNormal.h diff --git a/src/ShaderGen/MaterialFileData.h b/src/ShaderGen/MaterialFileData.h index a5f92d2e..3a7bc473 100644 --- a/src/ShaderGen/MaterialFileData.h +++ b/src/ShaderGen/MaterialFileData.h @@ -16,7 +16,7 @@ namespace material_file struct ShaderIOAttrib { - VAT vat; + VAType vat; char name[VERTEX_ATTRIB_NAME_MAX_LENGTH]; }; diff --git a/src/ShaderGen/ShaderCreateInfo.cpp b/src/ShaderGen/ShaderCreateInfo.cpp index ba9f1bab..a04e2fff 100644 --- a/src/ShaderGen/ShaderCreateInfo.cpp +++ b/src/ShaderGen/ShaderCreateInfo.cpp @@ -100,7 +100,7 @@ bool ShaderCreateInfo::ProcDefine() return(true); } -int ShaderCreateInfo::AddOutput(const VAT &type,const AnsiString &name,Interpolation inter) +int ShaderCreateInfo::AddOutput(const VAType &type,const AnsiString &name,Interpolation inter) { ShaderAttribute *ss=new ShaderAttribute; @@ -115,7 +115,7 @@ int ShaderCreateInfo::AddOutput(const VAT &type,const AnsiString &name,Interpola int ShaderCreateInfo::AddOutput(const AnsiString &type,const AnsiString &name,Interpolation inter) { - VAT vat; + VAType vat; if(name.IsEmpty()) return -1; diff --git a/src/ShaderGen/ShaderCreateInfoVertex.cpp b/src/ShaderGen/ShaderCreateInfoVertex.cpp index c0cb614d..7baf0f0b 100644 --- a/src/ShaderGen/ShaderCreateInfoVertex.cpp +++ b/src/ShaderGen/ShaderCreateInfoVertex.cpp @@ -12,7 +12,7 @@ ShaderCreateInfoVertex::ShaderCreateInfoVertex(MaterialDescriptorInfo *m):Shader { } -int ShaderCreateInfoVertex::AddInput(const VAT &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group) +int ShaderCreateInfoVertex::AddInput(const VAType &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group) { ShaderAttribute *ss=new ShaderAttribute; @@ -29,7 +29,7 @@ int ShaderCreateInfoVertex::AddInput(const VAT &type,const AnsiString &name,cons int ShaderCreateInfoVertex::AddInput(const AnsiString &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group) { - VAT vat; + VAType vat; if(!ParseVertexAttribType(&vat,type)) return(-2);