From fa7ecb91aca489bd438976e79fce07aabe923986 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Mon, 22 May 2023 14:30:38 +0800 Subject: [PATCH] supported MaterialInstance in ShaderGen module. --- inc/hgl/graph/mtl/UBOCommon.h | 4 +- inc/hgl/shadergen/MaterialCreateInfo.h | 18 +------- inc/hgl/shadergen/MaterialDescriptorInfo.h | 6 +-- inc/hgl/shadergen/ShaderCreateInfo.h | 4 ++ inc/hgl/shadergen/ShaderCreateInfoVertex.h | 25 ++--------- src/ShaderGen/2d/Std2DMaterial.cpp | 2 - src/ShaderGen/MaterialCreateInfo.cpp | 51 +++++++++++++++++++--- src/ShaderGen/MaterialDescriptorInfo.cpp | 4 +- src/ShaderGen/ShaderCreateInfo.cpp | 9 ++++ src/ShaderGen/ShaderCreateInfoVertex.cpp | 28 ++++++++++++ src/ShaderGen/common/MFCommon.h | 3 +- 11 files changed, 98 insertions(+), 56 deletions(-) diff --git a/inc/hgl/graph/mtl/UBOCommon.h b/inc/hgl/graph/mtl/UBOCommon.h index 46af4100..3ba10b56 100644 --- a/inc/hgl/graph/mtl/UBOCommon.h +++ b/inc/hgl/graph/mtl/UBOCommon.h @@ -42,7 +42,9 @@ vec3 world_up; float znear,zfar;)" }; -constexpr const ShaderBufferSource SBS_MaterialInstance= +constexpr const char MaterialInstanceStruct[]="MaterialInstance"; + +constexpr const ShaderBufferSource SBS_MaterialInstanceData= { "MaterialInstanceData", "mtl", diff --git a/inc/hgl/shadergen/MaterialCreateInfo.h b/inc/hgl/shadergen/MaterialCreateInfo.h index 08d024c3..cf5700f8 100644 --- a/inc/hgl/shadergen/MaterialCreateInfo.h +++ b/inc/hgl/shadergen/MaterialCreateInfo.h @@ -52,23 +52,7 @@ public: MaterialCreateInfo(const MaterialConfig *); ~MaterialCreateInfo()=default; - /** - * 设置材质实例代码与数据长度 - * @param mi_glsl_codes 材质实例GLSL代码 - * @param mi_struct_bytes 单个材质实例数据长度 - * @return 是否设置成功 - */ - bool SetMaterialInstance(const AnsiString &mi_glsl_codes,const uint32_t mi_struct_bytes) - { - if(mi_struct_bytes>0&&mi_glsl_codes.Length()<4)return(false); - - mi_length=mi_struct_bytes; - - if(mi_struct_bytes>0) - mi_codes=mi_glsl_codes; - - return(true); - } + bool SetMaterialInstance(const AnsiString &mi_glsl_codes,const uint32_t mi_struct_bytes,const uint32_t shader_stage); bool AddStruct(const AnsiString &ubo_typename,const AnsiString &codes); bool AddStruct(const ShaderBufferSource &ss) diff --git a/inc/hgl/shadergen/MaterialDescriptorInfo.h b/inc/hgl/shadergen/MaterialDescriptorInfo.h index c3123c73..3464aa5d 100644 --- a/inc/hgl/shadergen/MaterialDescriptorInfo.h +++ b/inc/hgl/shadergen/MaterialDescriptorInfo.h @@ -28,7 +28,7 @@ public: return(true); } - bool GetStruct(const AnsiString &name,AnsiString &code) const + bool GetStruct(const AnsiString &name,AnsiString &code) { return(struct_map.Get(name,code)); } @@ -38,8 +38,8 @@ public: return(struct_map.KeyExist(name)); } - const UBODescriptor *AddUBO(VkShaderStageFlagBits ssb,DescriptorSetType set_type,UBODescriptor *sd); - const SamplerDescriptor *AddSampler(VkShaderStageFlagBits ssb,DescriptorSetType set_type,SamplerDescriptor *sd); + const UBODescriptor *AddUBO(uint32_t shader_stage_flag_bits,DescriptorSetType set_type,UBODescriptor *sd); + const SamplerDescriptor *AddSampler(uint32_t shader_stage_flag_bits,DescriptorSetType set_type,SamplerDescriptor *sd); UBODescriptor *GetUBO(const AnsiString &name); SamplerDescriptor *GetSampler(const AnsiString &name); diff --git a/inc/hgl/shadergen/ShaderCreateInfo.h b/inc/hgl/shadergen/ShaderCreateInfo.h index 24483b8c..b57c59c1 100644 --- a/inc/hgl/shadergen/ShaderCreateInfo.h +++ b/inc/hgl/shadergen/ShaderCreateInfo.h @@ -13,6 +13,8 @@ struct SPVData; class MaterialDescriptorInfo; class ShaderDescriptorInfo; +struct UBODescriptor; + class ShaderCreateInfo { protected: @@ -63,6 +65,8 @@ public: void AddFunction(const AnsiString &str){function_list.Add(str);} + void SetMaterialInstance(UBODescriptor *); + const AnsiString &GetOutputStruct()const{return output_struct;} const AnsiString &GetShaderSource()const{return final_shader;} diff --git a/inc/hgl/shadergen/ShaderCreateInfoVertex.h b/inc/hgl/shadergen/ShaderCreateInfoVertex.h index 9db65b1e..231f4bce 100644 --- a/inc/hgl/shadergen/ShaderCreateInfoVertex.h +++ b/inc/hgl/shadergen/ShaderCreateInfoVertex.h @@ -19,28 +19,9 @@ namespace hgl 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 AnsiString &type,const AnsiString &name,const VkVertexInputRate input_rate=VK_VERTEX_INPUT_RATE_VERTEX,const VertexInputGroup &group=VertexInputGroup::Basic); - void AddMaterialID() - { - AddInput(VAT_UINT,VAN::MaterialInstanceID,VK_VERTEX_INPUT_RATE_INSTANCE,VertexInputGroup::MaterialInstanceID); - } - - void AddJoint() - { - AddInput(VAT_UVEC4, VAN::JointID, VK_VERTEX_INPUT_RATE_VERTEX,VertexInputGroup::JointID); - AddInput(VAT_VEC4, VAN::JointWeight,VK_VERTEX_INPUT_RATE_VERTEX,VertexInputGroup::JointWeight); - } - - void AddLocalToWorld() - { - char name[]= "LocalToWorld_?"; - - for(uint i=0;i<4;i++) - { - name[sizeof(name)-2]='0'+i; - - AddInput(VAT_VEC4,name,VK_VERTEX_INPUT_RATE_INSTANCE,VertexInputGroup::LocalToWorld); - } - } + void AddMaterialInstanceID(); + void AddJoint(); + void AddLocalToWorld(); };//class ShaderCreateInfoVertex:public ShaderCreateInfo }//namespace graph }//namespace hgl::graph diff --git a/src/ShaderGen/2d/Std2DMaterial.cpp b/src/ShaderGen/2d/Std2DMaterial.cpp index 78151b2c..a1505ec8 100644 --- a/src/ShaderGen/2d/Std2DMaterial.cpp +++ b/src/ShaderGen/2d/Std2DMaterial.cpp @@ -3,7 +3,6 @@ #include #include #include"common/MFGetPosition.h" -#include"common/MFCommon.h" STD_MTL_NAMESPACE_BEGIN Std2DMaterial::Std2DMaterial(const Material2DConfig *c) @@ -23,7 +22,6 @@ bool Std2DMaterial::CreateVertexShader(ShaderCreateInfoVertex *vsc) if(cfg->local_to_world) { vsc->AddLocalToWorld(); - vsc->AddFunction(func::GetLocalToWorld); vsc->AddFunction(func::GetPosition2DL2W[size_t(cfg->coordinate_system)]); } diff --git a/src/ShaderGen/MaterialCreateInfo.cpp b/src/ShaderGen/MaterialCreateInfo.cpp index 498dbc98..9b8f2904 100644 --- a/src/ShaderGen/MaterialCreateInfo.cpp +++ b/src/ShaderGen/MaterialCreateInfo.cpp @@ -1,6 +1,7 @@ #include #include #include +#include"common/MFCommon.h" using namespace hgl; using namespace hgl::graph; @@ -118,18 +119,54 @@ bool MaterialCreateInfo::AddUBO(const uint32_t flag_bits,const DescriptorSetType return(result==shader_map.GetCount()); } +/** +* 设置材质实例代码与数据长度 +* @param mi_glsl_codes 材质实例GLSL代码 +* @param mi_struct_bytes 单个材质实例数据长度 +* @param shader_stage 具体使用材质实例的shader +* @return 是否设置成功 +*/ +bool MaterialCreateInfo::SetMaterialInstance(const AnsiString &mi_glsl_codes,const uint32_t mi_struct_bytes,const uint32_t shader_stage) +{ + if(mi_length>0)return(false); //已经有数据了 + + if(shader_stage==0)return(false); + + if(mi_struct_bytes>0&&mi_glsl_codes.Length()<4)return(false); + + mi_length=mi_struct_bytes; + + if(mi_struct_bytes>0) + mi_codes=mi_glsl_codes; + + mdi.AddStruct(MaterialInstanceStruct,mi_codes); + + UBODescriptor *ubo=new UBODescriptor(); + + ubo->type=SBS_MaterialInstanceData.struct_name; + hgl::strcpy(ubo->name,DESCRIPTOR_NAME_MAX_LENGTH,SBS_MaterialInstanceData.name); + ubo->stage_flag=shader_stage; + + mdi.AddUBO(shader_stage,DescriptorSetType::PerMaterial,ubo); + + auto *it=shader_map.GetDataList(); + + for(int i=0;ikey&shader_stage) + (*it)->value->SetMaterialInstance(ubo); + } + + vert->AddMaterialInstanceID(); //增加一个材质实例ID + + return(true); +} + bool MaterialCreateInfo::CreateShader() { if(shader_map.IsEmpty()) return(false); - if(mi_length>0) - { - AddUBO( config->shader_stage, - DescriptorSetType::Global, - mtl::SBS_MaterialInstance); - } - mdi.Resort(); ShaderCreateInfo *sc,*last=nullptr; diff --git a/src/ShaderGen/MaterialDescriptorInfo.cpp b/src/ShaderGen/MaterialDescriptorInfo.cpp index 7dd72bc2..7f9ad209 100644 --- a/src/ShaderGen/MaterialDescriptorInfo.cpp +++ b/src/ShaderGen/MaterialDescriptorInfo.cpp @@ -27,7 +27,7 @@ const DescriptorSetType MaterialDescriptorInfo::GetSetType(const AnsiString &nam return DescriptorSetType::Global; } -const UBODescriptor *MaterialDescriptorInfo::AddUBO(VkShaderStageFlagBits ssb,DescriptorSetType set_type,UBODescriptor *sd) +const UBODescriptor *MaterialDescriptorInfo::AddUBO(uint32_t ssb,DescriptorSetType set_type,UBODescriptor *sd) { RANGE_CHECK_RETURN_NULLPTR(set_type); if(!sd)return(nullptr); @@ -40,7 +40,7 @@ const UBODescriptor *MaterialDescriptorInfo::AddUBO(VkShaderStageFlagBits ssb,De return((UBODescriptor *)obj); } -const SamplerDescriptor *MaterialDescriptorInfo::AddSampler(VkShaderStageFlagBits ssb,DescriptorSetType set_type,SamplerDescriptor *sd) +const SamplerDescriptor *MaterialDescriptorInfo::AddSampler(uint32_t ssb,DescriptorSetType set_type,SamplerDescriptor *sd) { RANGE_CHECK_RETURN_NULLPTR(set_type); if(!sd)return(nullptr); diff --git a/src/ShaderGen/ShaderCreateInfo.cpp b/src/ShaderGen/ShaderCreateInfo.cpp index 947398bc..724afa17 100644 --- a/src/ShaderGen/ShaderCreateInfo.cpp +++ b/src/ShaderGen/ShaderCreateInfo.cpp @@ -1,6 +1,8 @@ #include #include #include"GLSLCompiler.h" +#include +#include"common/MFCommon.h" namespace hgl{namespace graph{ ShaderCreateInfo::ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorInfo *m) @@ -74,6 +76,13 @@ bool ShaderCreateInfo::ProcSubpassInput() return(true); } +void ShaderCreateInfo::SetMaterialInstance(UBODescriptor *ubo) +{ + sdm->AddUBO(DescriptorSetType::PerMaterial,ubo); + sdm->AddStruct(mtl::MaterialInstanceStruct); + AddFunction(mtl::func::GetMI); +} + bool ShaderCreateInfo::ProcInput(ShaderCreateInfo *last_sc) { if(!last_sc) diff --git a/src/ShaderGen/ShaderCreateInfoVertex.cpp b/src/ShaderGen/ShaderCreateInfoVertex.cpp index 9d655423..a14d8208 100644 --- a/src/ShaderGen/ShaderCreateInfoVertex.cpp +++ b/src/ShaderGen/ShaderCreateInfoVertex.cpp @@ -3,6 +3,7 @@ #include #include #include"GLSLCompiler.h" +#include"common/MFCommon.h" VK_NAMESPACE_BEGIN int ShaderCreateInfoVertex::AddInput(const VAT &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group) @@ -30,6 +31,33 @@ int ShaderCreateInfoVertex::AddInput(const AnsiString &type,const AnsiString &na return AddInput(vat,name,input_rate,group); } +void ShaderCreateInfoVertex::AddMaterialInstanceID() +{ + AddInput(VAT_UINT, VAN::MaterialInstanceID,VK_VERTEX_INPUT_RATE_INSTANCE,VertexInputGroup::MaterialInstanceID); + + AddFunction(mtl::func::HandoverMI); +} + +void ShaderCreateInfoVertex::AddJoint() +{ + AddInput(VAT_UVEC4, VAN::JointID, VK_VERTEX_INPUT_RATE_VERTEX,VertexInputGroup::JointID); + AddInput(VAT_VEC4, VAN::JointWeight,VK_VERTEX_INPUT_RATE_VERTEX,VertexInputGroup::JointWeight); +} + +void ShaderCreateInfoVertex::AddLocalToWorld() +{ + char name[]= "LocalToWorld_?"; + + for(uint i=0;i<4;i++) + { + name[sizeof(name)-2]='0'+i; + + AddInput(VAT_VEC4,name,VK_VERTEX_INPUT_RATE_INSTANCE,VertexInputGroup::LocalToWorld); + } + + AddFunction(mtl::func::GetLocalToWorld); +} + bool ShaderCreateInfoVertex::ProcInput(ShaderCreateInfo *) { const auto &input=sdm->GetShaderStageIO().input; diff --git a/src/ShaderGen/common/MFCommon.h b/src/ShaderGen/common/MFCommon.h index 519d13e5..15ba58f6 100644 --- a/src/ShaderGen/common/MFCommon.h +++ b/src/ShaderGen/common/MFCommon.h @@ -35,7 +35,6 @@ void HandoverMI() MaterialInstance GetMI() { return mtl.mi[Input.MaterialInstanceID]; -} -)"; +})"; }//namespace func STD_MTL_NAMESPACE_END