supported MaterialInstance in ShaderGen module.

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-05-22 14:30:38 +08:00
parent d5b719152f
commit fa7ecb91ac
11 changed files with 98 additions and 56 deletions

View File

@ -42,7 +42,9 @@ vec3 world_up;
float znear,zfar;)" float znear,zfar;)"
}; };
constexpr const ShaderBufferSource SBS_MaterialInstance= constexpr const char MaterialInstanceStruct[]="MaterialInstance";
constexpr const ShaderBufferSource SBS_MaterialInstanceData=
{ {
"MaterialInstanceData", "MaterialInstanceData",
"mtl", "mtl",

View File

@ -52,23 +52,7 @@ public:
MaterialCreateInfo(const MaterialConfig *); MaterialCreateInfo(const MaterialConfig *);
~MaterialCreateInfo()=default; ~MaterialCreateInfo()=default;
/** bool SetMaterialInstance(const AnsiString &mi_glsl_codes,const uint32_t mi_struct_bytes,const uint32_t shader_stage);
*
* @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 AddStruct(const AnsiString &ubo_typename,const AnsiString &codes); bool AddStruct(const AnsiString &ubo_typename,const AnsiString &codes);
bool AddStruct(const ShaderBufferSource &ss) bool AddStruct(const ShaderBufferSource &ss)

View File

@ -28,7 +28,7 @@ public:
return(true); return(true);
} }
bool GetStruct(const AnsiString &name,AnsiString &code) const bool GetStruct(const AnsiString &name,AnsiString &code)
{ {
return(struct_map.Get(name,code)); return(struct_map.Get(name,code));
} }
@ -38,8 +38,8 @@ public:
return(struct_map.KeyExist(name)); return(struct_map.KeyExist(name));
} }
const UBODescriptor *AddUBO(VkShaderStageFlagBits ssb,DescriptorSetType set_type,UBODescriptor *sd); const UBODescriptor *AddUBO(uint32_t shader_stage_flag_bits,DescriptorSetType set_type,UBODescriptor *sd);
const SamplerDescriptor *AddSampler(VkShaderStageFlagBits ssb,DescriptorSetType set_type,SamplerDescriptor *sd); const SamplerDescriptor *AddSampler(uint32_t shader_stage_flag_bits,DescriptorSetType set_type,SamplerDescriptor *sd);
UBODescriptor *GetUBO(const AnsiString &name); UBODescriptor *GetUBO(const AnsiString &name);
SamplerDescriptor *GetSampler(const AnsiString &name); SamplerDescriptor *GetSampler(const AnsiString &name);

View File

@ -13,6 +13,8 @@ struct SPVData;
class MaterialDescriptorInfo; class MaterialDescriptorInfo;
class ShaderDescriptorInfo; class ShaderDescriptorInfo;
struct UBODescriptor;
class ShaderCreateInfo class ShaderCreateInfo
{ {
protected: protected:
@ -63,6 +65,8 @@ public:
void AddFunction(const AnsiString &str){function_list.Add(str);} void AddFunction(const AnsiString &str){function_list.Add(str);}
void SetMaterialInstance(UBODescriptor *);
const AnsiString &GetOutputStruct()const{return output_struct;} const AnsiString &GetOutputStruct()const{return output_struct;}
const AnsiString &GetShaderSource()const{return final_shader;} const AnsiString &GetShaderSource()const{return final_shader;}

View File

@ -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 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); int AddInput(const AnsiString &type,const AnsiString &name,const VkVertexInputRate input_rate=VK_VERTEX_INPUT_RATE_VERTEX,const VertexInputGroup &group=VertexInputGroup::Basic);
void AddMaterialID() void AddMaterialInstanceID();
{ void AddJoint();
AddInput(VAT_UINT,VAN::MaterialInstanceID,VK_VERTEX_INPUT_RATE_INSTANCE,VertexInputGroup::MaterialInstanceID); void AddLocalToWorld();
}
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);
}
}
};//class ShaderCreateInfoVertex:public ShaderCreateInfo };//class ShaderCreateInfoVertex:public ShaderCreateInfo
}//namespace graph }//namespace graph
}//namespace hgl::graph }//namespace hgl::graph

View File

@ -3,7 +3,6 @@
#include<hgl/graph/mtl/2d/Material2DConfig.h> #include<hgl/graph/mtl/2d/Material2DConfig.h>
#include<hgl/graph/mtl/UBOCommon.h> #include<hgl/graph/mtl/UBOCommon.h>
#include"common/MFGetPosition.h" #include"common/MFGetPosition.h"
#include"common/MFCommon.h"
STD_MTL_NAMESPACE_BEGIN STD_MTL_NAMESPACE_BEGIN
Std2DMaterial::Std2DMaterial(const Material2DConfig *c) Std2DMaterial::Std2DMaterial(const Material2DConfig *c)
@ -23,7 +22,6 @@ bool Std2DMaterial::CreateVertexShader(ShaderCreateInfoVertex *vsc)
if(cfg->local_to_world) if(cfg->local_to_world)
{ {
vsc->AddLocalToWorld(); vsc->AddLocalToWorld();
vsc->AddFunction(func::GetLocalToWorld);
vsc->AddFunction(func::GetPosition2DL2W[size_t(cfg->coordinate_system)]); vsc->AddFunction(func::GetPosition2DL2W[size_t(cfg->coordinate_system)]);
} }

View File

@ -1,6 +1,7 @@
#include<hgl/shadergen/MaterialCreateInfo.h> #include<hgl/shadergen/MaterialCreateInfo.h>
#include<hgl/shadergen/ShaderDescriptorInfo.h> #include<hgl/shadergen/ShaderDescriptorInfo.h>
#include<hgl/graph/mtl/UBOCommon.h> #include<hgl/graph/mtl/UBOCommon.h>
#include"common/MFCommon.h"
using namespace hgl; using namespace hgl;
using namespace hgl::graph; using namespace hgl::graph;
@ -118,18 +119,54 @@ bool MaterialCreateInfo::AddUBO(const uint32_t flag_bits,const DescriptorSetType
return(result==shader_map.GetCount()); 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;i<shader_map.GetCount();i++)
{
if((*it)->key&shader_stage)
(*it)->value->SetMaterialInstance(ubo);
}
vert->AddMaterialInstanceID(); //增加一个材质实例ID
return(true);
}
bool MaterialCreateInfo::CreateShader() bool MaterialCreateInfo::CreateShader()
{ {
if(shader_map.IsEmpty()) if(shader_map.IsEmpty())
return(false); return(false);
if(mi_length>0)
{
AddUBO( config->shader_stage,
DescriptorSetType::Global,
mtl::SBS_MaterialInstance);
}
mdi.Resort(); mdi.Resort();
ShaderCreateInfo *sc,*last=nullptr; ShaderCreateInfo *sc,*last=nullptr;

View File

@ -27,7 +27,7 @@ const DescriptorSetType MaterialDescriptorInfo::GetSetType(const AnsiString &nam
return DescriptorSetType::Global; 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); RANGE_CHECK_RETURN_NULLPTR(set_type);
if(!sd)return(nullptr); if(!sd)return(nullptr);
@ -40,7 +40,7 @@ const UBODescriptor *MaterialDescriptorInfo::AddUBO(VkShaderStageFlagBits ssb,De
return((UBODescriptor *)obj); 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); RANGE_CHECK_RETURN_NULLPTR(set_type);
if(!sd)return(nullptr); if(!sd)return(nullptr);

View File

@ -1,6 +1,8 @@
#include<hgl/shadergen/ShaderCreateInfo.h> #include<hgl/shadergen/ShaderCreateInfo.h>
#include<hgl/shadergen/ShaderDescriptorInfo.h> #include<hgl/shadergen/ShaderDescriptorInfo.h>
#include"GLSLCompiler.h" #include"GLSLCompiler.h"
#include<hgl/graph/mtl/UBOCommon.h>
#include"common/MFCommon.h"
namespace hgl{namespace graph{ namespace hgl{namespace graph{
ShaderCreateInfo::ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorInfo *m) ShaderCreateInfo::ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorInfo *m)
@ -74,6 +76,13 @@ bool ShaderCreateInfo::ProcSubpassInput()
return(true); 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) bool ShaderCreateInfo::ProcInput(ShaderCreateInfo *last_sc)
{ {
if(!last_sc) if(!last_sc)

View File

@ -3,6 +3,7 @@
#include<hgl/graph/VertexAttrib.h> #include<hgl/graph/VertexAttrib.h>
#include<hgl/graph/VKShaderStage.h> #include<hgl/graph/VKShaderStage.h>
#include"GLSLCompiler.h" #include"GLSLCompiler.h"
#include"common/MFCommon.h"
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
int ShaderCreateInfoVertex::AddInput(const VAT &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group) 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); 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 *) bool ShaderCreateInfoVertex::ProcInput(ShaderCreateInfo *)
{ {
const auto &input=sdm->GetShaderStageIO().input; const auto &input=sdm->GetShaderStageIO().input;

View File

@ -35,7 +35,6 @@ void HandoverMI()
MaterialInstance GetMI() MaterialInstance GetMI()
{ {
return mtl.mi[Input.MaterialInstanceID]; return mtl.mi[Input.MaterialInstanceID];
} })";
)";
}//namespace func }//namespace func
STD_MTL_NAMESPACE_END STD_MTL_NAMESPACE_END