5 Commits

Author SHA1 Message Date
1b5d4aabb0 Optimized shader 2024-06-20 00:23:45 +08:00
af5988d96e optimized shader of billboard dynamic size 2024-06-20 00:21:53 +08:00
56b8798122 maybe finished, all examples test OK! 2024-06-20 00:16:29 +08:00
2f49e80122 [WIP] 2024-06-19 14:03:46 +08:00
806bf5dfdd [WIP] next step is ShaderCreateInfo::ProcOutput 2024-06-19 09:35:54 +08:00
29 changed files with 958 additions and 299 deletions

View File

@@ -1,6 +1,9 @@
#pragma once #pragma once
#include<hgl/type/String.h>
#include<hgl/type/List.h>
#include<hgl/graph/VertexAttrib.h> #include<hgl/graph/VertexAttrib.h>
#include<hgl/graph/VKInterpolation.h>
#include<hgl/graph/VKSamplerType.h> #include<hgl/graph/VKSamplerType.h>
#include<hgl/graph/VKImageType.h> #include<hgl/graph/VKImageType.h>
#include<hgl/CompOperator.h> #include<hgl/CompOperator.h>
@@ -13,13 +16,12 @@ namespace hgl
{ {
Scalar=0, Scalar=0,
Vector, Vector,
Materix, Matrix,
Sampler, Sampler,
Image, Image,
AtomicCounter, AtomicCounter,
ImplicitConversion,
ENUM_CLASS_RANGE(Scalar,Materix) ENUM_CLASS_RANGE(Scalar,AtomicCounter)
};//enum class ShaderVariableBaseType };//enum class ShaderVariableBaseType
using SVBaseType=ShaderVariableBaseType; using SVBaseType=ShaderVariableBaseType;
@@ -123,7 +125,7 @@ namespace hgl
{ {
svt_code=0; svt_code=0;
base_type=SVBaseType::Materix; base_type=SVBaseType::Matrix;
matrix.type=vabt; matrix.type=vabt;
matrix.n=col; matrix.n=col;
@@ -170,7 +172,7 @@ namespace hgl
return(true); return(true);
} }
if(base_type==SVBaseType::Materix) if(base_type==SVBaseType::Matrix)
{ {
if(matrix.m==0) if(matrix.m==0)
{ {
@@ -216,7 +218,7 @@ namespace hgl
return int(vector.type)-int(svt.vector.type); return int(vector.type)-int(svt.vector.type);
} }
if(base_type==SVBaseType::Materix) if(base_type==SVBaseType::Matrix)
{ {
off=int(matrix.type)-int(svt.matrix.type); off=int(matrix.type)-int(svt.matrix.type);
@@ -240,6 +242,10 @@ namespace hgl
CompOperator(const ShaderVariableType &,Comp) CompOperator(const ShaderVariableType &,Comp)
const char *GetTypename()const;
bool ParseTypeString(const char *str);
const uint64 ToCode()const{return svt_code;} const uint64 ToCode()const{return svt_code;}
const bool FromCode(const uint64 code) const bool FromCode(const uint64 code)
{ {
@@ -366,5 +372,185 @@ namespace hgl
const SVType SVT_Image2DMS(ShaderImageType::Image2DMS,1); const SVType SVT_Image2DMS(ShaderImageType::Image2DMS,1);
const SVType SVT_Image2DMSArray(ShaderImageType::Image2DMSArray,1); const SVType SVT_Image2DMSArray(ShaderImageType::Image2DMSArray,1);
struct ShaderVariable
{
char name[VERTEX_ATTRIB_NAME_MAX_LENGTH];
uint location;
SVType type;
Interpolation interpolation; //插值方式
};
using SVList=List<ShaderVariable>;
struct ShaderVariableArray
{
uint count;
ShaderVariable *items;
public:
const bool IsEmpty()const{return !items||count<=0;}
public:
ShaderVariableArray()
{
count=0;
items=nullptr;
}
~ShaderVariableArray()
{
Clear();
}
int Comp(const ShaderVariableArray *sva)const
{
if(!sva)
return 1;
int off=count-sva->count;
if(off)return off;
for(uint i=0;i<count;i++)
{
off=items[i].location-sva->items[i].location;
if(off)
return off;
if(items[i].type.ToCode()>sva->items[i].type.ToCode())
return 1;
//ToCode返回的是uint64可能差值超大所以不能直接用-的结果
if(items[i].type.ToCode()<sva->items[i].type.ToCode())
return -1;
off=int(items[i].interpolation)-int(sva->items[i].interpolation);
if(off)
return off;
off=hgl::strcmp(items[i].name,sva->items[i].name);
if(off)
return off;
}
return 0;
}
int Comp(const ShaderVariableArray &sva)const{return Comp(&sva);}
CompOperator(const ShaderVariableArray *,Comp)
CompOperator(const ShaderVariableArray &,Comp)
bool Init(const uint c=0)
{
if(items)
return(false);
if(c>0)
{
count=c;
items=array_alloc<ShaderVariable>(count);
}
else
{
count=0;
items=nullptr;
}
return(true);
}
bool IsMember(const char *name)const
{
if(count<=0)
return(false);
for(uint i=0;i<count;i++)
if(hgl::strcmp(items[i].name,name)==0)
return(true);
return(false);
}
bool Add(ShaderVariable &sv)
{
if(IsMember(sv.name))
return(false);
sv.location=count;
if(!items)
{
items=array_alloc<ShaderVariable>(1);
count=1;
}
else
{
++count;
items=array_realloc(items,count);
}
hgl_cpy(items[count-1],sv);
return(true);
}
void Clear()
{
if(items)
{
array_free(items);
items=nullptr;
}
count=0;
}
bool Clone(const ShaderVariableArray *src)
{
if(!src)
return(false);
if(!Init(src->count))
return(false);
hgl_cpy(items,src->items,src->count);
return(true);
}
void ToString(AnsiString &output_string)
{
if(IsEmpty())
return;
const ShaderVariable *sv=items;
for(uint i=0;i<count;i++)
{
output_string+=" ";
if(sv->interpolation!=Interpolation::Smooth)
{
if(RangeCheck(sv->interpolation))
{
output_string+=InterpolationName[size_t(sv->interpolation)];
output_string+=" ";
}
}
output_string+=sv->type.GetTypename();
output_string+=" ";
output_string+=sv->name;
output_string+=";\n";
++sv;
}
}
};//struct ShaderVariableArray
using SVArray=ShaderVariableArray;
}//namespace graph }//namespace graph
}//namespace hgl }//namespace hgl

View File

@@ -4,6 +4,7 @@
#include<hgl/graph/VertexAttrib.h> #include<hgl/graph/VertexAttrib.h>
#include<hgl/graph/VK.h> #include<hgl/graph/VK.h>
#include<hgl/graph/VKInterpolation.h> #include<hgl/graph/VKInterpolation.h>
#include<hgl/graph/VKDescriptorSetType.h>
#include<hgl/graph/mtl/ShaderVariableType.h> #include<hgl/graph/mtl/ShaderVariableType.h>
#include<hgl/type/StringList.h> #include<hgl/type/StringList.h>
@@ -15,6 +16,7 @@ class MaterialDescriptorInfo;
class ShaderDescriptorInfo; class ShaderDescriptorInfo;
struct UBODescriptor; struct UBODescriptor;
struct SamplerDescriptor;
class ShaderCreateInfo class ShaderCreateInfo
{ {
@@ -28,8 +30,8 @@ protected:
AnsiStringList define_macro_list; AnsiStringList define_macro_list;
AnsiStringList define_value_list; AnsiStringList define_value_list;
uint32_t define_macro_max_length; int define_macro_max_length;
uint32_t define_value_max_length; int define_value_max_length;
AnsiString output_struct; AnsiString output_struct;
@@ -49,8 +51,10 @@ protected:
virtual bool ProcDefine(); virtual bool ProcDefine();
virtual bool ProcLayout(){return(true);} virtual bool ProcLayout(){return(true);}
virtual bool ProcSubpassInput();
virtual bool ProcInput(ShaderCreateInfo *); virtual bool ProcInput(ShaderCreateInfo *);
virtual bool IsEmptyOutput()const=0;
virtual void GetOutputStrcutString(AnsiString &){}
virtual bool ProcOutput(); virtual bool ProcOutput();
virtual bool ProcStruct(); virtual bool ProcStruct();
@@ -66,25 +70,28 @@ protected:
public: public:
ShaderDescriptorInfo *sdi; virtual ShaderDescriptorInfo *GetSDI()=0;
const VkShaderStageFlagBits GetShaderStage()const{return shader_stage;} const VkShaderStageFlagBits GetShaderStage()const{return shader_stage;}
protected:
void Init(ShaderDescriptorInfo *sdi,MaterialDescriptorInfo *m);
public: public:
ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorInfo *m); ShaderCreateInfo();
virtual ~ShaderCreateInfo(); virtual ~ShaderCreateInfo();
bool AddDefine(const AnsiString &m,const AnsiString &v); bool AddDefine(const AnsiString &m,const AnsiString &v);
int AddOutput(const graph::VAType &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth); void AddStruct(const AnsiString &);
int AddOutput(const AnsiString &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth); bool AddUBO(DescriptorSetType type,const UBODescriptor *sd);
//int AddOutput(const ShaderVariableType &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth); bool AddSampler(DescriptorSetType type,const SamplerDescriptor *sd);
void AddFunction(const char *str){function_list.Add(str);} void AddFunction(const char *str){function_list.Add(str);}
void SetMaterialInstance(UBODescriptor *,const AnsiString &); void SetMaterialInstance(UBODescriptor *,const AnsiString &);
void AddMaterialInstanceOutput(); virtual void AddMaterialInstanceOutput()=0;
void SetMain(const AnsiString &str){main_function=str;} void SetMain(const AnsiString &str){main_function=str;}
void SetMain(const char *str,const int len) void SetMain(const char *str,const int len)

View File

@@ -1,16 +1,32 @@
#pragma once #pragma once
#include<hgl/shadergen/ShaderCreateInfo.h> #include<hgl/shadergen/ShaderCreateInfo.h>
#include<hgl/shadergen/ShaderDescriptorInfo.h>
namespace hgl{namespace graph{ namespace hgl{namespace graph{
class ShaderCreateInfoFragment:public ShaderCreateInfo class ShaderCreateInfoFragment:public ShaderCreateInfo
{ {
FragmentShaderDescriptorInfo fsdi;
protected:
bool IsEmptyOutput()const override{return false;/*这个返回啥无所谓因为Fragment Shader走自己的ProcOutput*/ }
bool ProcOutput() override; bool ProcOutput() override;
void AddMaterialInstanceOutput() override{};
public: public:
ShaderCreateInfoFragment(MaterialDescriptorInfo *m):ShaderCreateInfo(VK_SHADER_STAGE_FRAGMENT_BIT,m){} ShaderDescriptorInfo *GetSDI()override{return &fsdi;}
public:
ShaderCreateInfoFragment(MaterialDescriptorInfo *m):ShaderCreateInfo(){ShaderCreateInfo::Init(&fsdi,m);}
~ShaderCreateInfoFragment()=default; ~ShaderCreateInfoFragment()=default;
int AddOutput(VIAList &);
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);
}; };
}}//namespace hgl::graph }}//namespace hgl::graph

View File

@@ -1,21 +1,35 @@
#pragma once #pragma once
#include<hgl/shadergen/ShaderCreateInfo.h> #include<hgl/shadergen/ShaderCreateInfo.h>
#include<hgl/shadergen/ShaderDescriptorInfo.h>
namespace hgl{namespace graph{ namespace hgl{namespace graph{
class ShaderCreateInfoGeometry:public ShaderCreateInfo class ShaderCreateInfoGeometry:public ShaderCreateInfo
{ {
GeometryShaderDescriptorInfo gsdi;
AnsiString input_prim; AnsiString input_prim;
AnsiString output_prim; AnsiString output_prim;
uint32_t max_vertices; uint32_t max_vertices;
public: public:
ShaderCreateInfoGeometry(MaterialDescriptorInfo *m):ShaderCreateInfo(VK_SHADER_STAGE_GEOMETRY_BIT,m){} bool IsEmptyOutput()const override{return gsdi.IsEmptyOutput();}
~ShaderCreateInfoGeometry()=default; void GetOutputStrcutString(AnsiString &str) override;
ShaderDescriptorInfo *GetSDI()override{return &gsdi;}
public:
ShaderCreateInfoGeometry(MaterialDescriptorInfo *m):ShaderCreateInfo(){ShaderCreateInfo::Init(&gsdi,m);}
~ShaderCreateInfoGeometry()override=default;
bool SetGeom(const Prim &ip,const Prim &op,const uint32_t mv); bool SetGeom(const Prim &ip,const Prim &op,const uint32_t mv);
int AddOutput(SVList &);
int AddOutput(const ShaderVariableType &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth);
void AddMaterialInstanceOutput() override;
bool ProcLayout() override; bool ProcLayout() override;
}; };
}}//namespace hgl::graph }}//namespace hgl::graph

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include<hgl/shadergen/ShaderCreateInfo.h> #include<hgl/shadergen/ShaderCreateInfo.h>
#include<hgl/graph/VKShaderStage.h> #include<hgl/shadergen/ShaderDescriptorInfo.h>
namespace hgl namespace hgl
{ {
@@ -9,18 +9,35 @@ namespace hgl
{ {
class ShaderCreateInfoVertex:public ShaderCreateInfo class ShaderCreateInfoVertex:public ShaderCreateInfo
{ {
VertexShaderDescriptorInfo vsdi;
bool ProcSubpassInput();
bool ProcInput(ShaderCreateInfo *) override; bool ProcInput(ShaderCreateInfo *) override;
bool IsEmptyOutput()const override{return vsdi.IsEmptyOutput();}
void GetOutputStrcutString(AnsiString &str) override;
public: public:
ShaderCreateInfoVertex(MaterialDescriptorInfo *); VIAArray &GetInput(){return vsdi.GetInput();}
~ShaderCreateInfoVertex()=default;
ShaderDescriptorInfo *GetSDI()override{return &vsdi;}
public:
ShaderCreateInfoVertex(MaterialDescriptorInfo *m):ShaderCreateInfo(){ShaderCreateInfo::Init(&vsdi,m);}
~ShaderCreateInfoVertex()override=default;
int AddInput(VIAList &);
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 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 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 *); int hasInput(const char *);
int AddOutput(SVList &);
int AddOutput(const SVType &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth);
void AddMaterialInstanceOutput() override;
void AddAssign(); void AddAssign();
void AddJoint(); void AddJoint();

View File

@@ -4,6 +4,7 @@
#include<hgl/type/StringList.h> #include<hgl/type/StringList.h>
#include<hgl/graph/VKShaderStage.h> #include<hgl/graph/VKShaderStage.h>
#include<hgl/graph/VKDescriptorSetType.h> #include<hgl/graph/VKDescriptorSetType.h>
#include<hgl/graph/mtl/ShaderVariableType.h>
#include<hgl/shadergen/MaterialDescriptorInfo.h> #include<hgl/shadergen/MaterialDescriptorInfo.h>
namespace hgl{namespace graph namespace hgl{namespace graph
@@ -18,9 +19,9 @@ using SubpassInputDescriptorList=ObjectList<SubpassInputDescriptor>;
*/ */
class ShaderDescriptorInfo class ShaderDescriptorInfo
{ {
VkShaderStageFlagBits stage_flag; protected:
ShaderStageIO stage_io; VkShaderStageFlagBits stage_flag;
AnsiStringList struct_list; //用到的结构列表 AnsiStringList struct_list; //用到的结构列表
@@ -29,22 +30,19 @@ class ShaderDescriptorInfo
SamplerDescriptorList sampler_list; SamplerDescriptorList sampler_list;
ConstValueDescriptorList const_value_list; ConstValueDescriptorList const_value_list;
SubpassInputDescriptorList subpass_input;
ShaderPushConstant push_constant; ShaderPushConstant push_constant;
public: public:
ShaderDescriptorInfo(VkShaderStageFlagBits); ShaderDescriptorInfo(VkShaderStageFlagBits);
~ShaderDescriptorInfo()=default; virtual ~ShaderDescriptorInfo()=default;
const VkShaderStageFlagBits GetStageFlag()const { return stage_flag; } const VkShaderStageFlagBits GetShaderStage()const { return stage_flag; }
const AnsiString GetStageName()const { return AnsiString(GetShaderStageName(stage_flag)); } const AnsiString GetStageName()const { return AnsiString(GetShaderStageName(stage_flag)); }
public: public:
const ShaderStageIO & GetShaderStageIO()const{return stage_io;}
const AnsiStringList & GetStructList()const{return struct_list;} const AnsiStringList & GetStructList()const{return struct_list;}
const UBODescriptorList & GetUBOList()const{return ubo_list;} const UBODescriptorList & GetUBOList()const{return ubo_list;}
@@ -52,22 +50,60 @@ public:
const ConstValueDescriptorList & GetConstList()const{return const_value_list;} const ConstValueDescriptorList & GetConstList()const{return const_value_list;}
const SubpassInputDescriptorList & GetSubpassInputList()const{return subpass_input;}
public: public:
bool AddInput(VertexInputAttribute *);
bool AddOutput(VertexInputAttribute *);
bool hasInput(const char *)const; ///<是否有指定输入
void AddStruct(const AnsiString &); void AddStruct(const AnsiString &);
bool AddUBO(DescriptorSetType type,const UBODescriptor *sd); bool AddUBO(DescriptorSetType type,const UBODescriptor *sd);
bool AddSampler(DescriptorSetType type,const SamplerDescriptor *sd); bool AddSampler(DescriptorSetType type,const SamplerDescriptor *sd);
bool AddConstValue(ConstValueDescriptor *sd); bool AddConstValue(ConstValueDescriptor *sd);
bool AddSubpassInput(const AnsiString name,uint8_t index);
void SetPushConstant(const AnsiString name,uint8_t offset,uint8_t size); void SetPushConstant(const AnsiString name,uint8_t offset,uint8_t size);
};//class ShaderDescriptorInfo };//class ShaderDescriptorInfo
template<VkShaderStageFlagBits SS,typename IArray,typename I,typename OArray,typename O> class CustomShaderDescriptorInfo:public ShaderDescriptorInfo
{
IArray input;
OArray output;
public:
CustomShaderDescriptorInfo():ShaderDescriptorInfo(SS){}
virtual ~CustomShaderDescriptorInfo()override=default;
bool AddInput(I &item){return input.Add(item);}
bool AddOutput(O &item){return output.Add(item);}
bool hasInput(const char *name)const{return input.IsMember(name);} ///<是否有指定输入
public:
IArray &GetInput(){return input;}
OArray &GetOutput(){return output;}
const bool IsEmptyInput()const{return input.IsEmpty();}
const bool IsEmptyOutput()const{return output.IsEmpty();}
};//class CustomShaderDescriptorInfo
class VertexShaderDescriptorInfo:public CustomShaderDescriptorInfo<VK_SHADER_STAGE_VERTEX_BIT,VIAArray,VIA,SVArray,ShaderVariable >
{
SubpassInputDescriptorList subpass_input;
public:
const SubpassInputDescriptorList & GetSubpassInputList()const{return subpass_input;}
public:
using CustomShaderDescriptorInfo<VK_SHADER_STAGE_VERTEX_BIT,VIAArray,VIA,SVArray,ShaderVariable>::CustomShaderDescriptorInfo;
~VertexShaderDescriptorInfo()override=default;
bool AddSubpassInput(const AnsiString name,uint8_t index);
};//class VertexShaderDescriptorInfo
using TessCtrlShaderDescriptorInfo=CustomShaderDescriptorInfo<VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, SVArray, ShaderVariable, SVArray, ShaderVariable >;
using TessEvalShaderDescriptorInfo=CustomShaderDescriptorInfo<VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, SVArray, ShaderVariable, SVArray, ShaderVariable >;
using GeometryShaderDescriptorInfo=CustomShaderDescriptorInfo<VK_SHADER_STAGE_GEOMETRY_BIT, SVArray, ShaderVariable, SVArray, ShaderVariable >;
using FragmentShaderDescriptorInfo=CustomShaderDescriptorInfo<VK_SHADER_STAGE_FRAGMENT_BIT, SVArray, ShaderVariable, VIAArray, VIA >;
}}//namespace hgl::graph }}//namespace hgl::graph

View File

@@ -123,7 +123,7 @@ Material *RenderResource::CreateMaterial(const mtl::MaterialCreateInfo *mci)
ShaderCreateInfoVertex *vert=mci->GetVS(); ShaderCreateInfoVertex *vert=mci->GetVS();
if(vert) if(vert)
mtl->vertex_input=GetVertexInput(vert->sdi->GetShaderStageIO().input); mtl->vertex_input=GetVertexInput(vert->GetInput());
} }
{ {

View File

@@ -37,7 +37,7 @@ void main()
vsc->AddInput(VAT_VEC2,VAN::TexCoord); vsc->AddInput(VAT_VEC2,VAN::TexCoord);
vsc->AddOutput(VAT_VEC2,"TexCoord"); vsc->AddOutput(SVT_VEC2,"TexCoord");
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);

View File

@@ -54,7 +54,7 @@ void main()
vsc->AddInput(VAT_VEC4,VAN::TexCoord); vsc->AddInput(VAT_VEC4,VAN::TexCoord);
vsc->AddOutput(VAT_VEC4,"TexCoord"); vsc->AddOutput(SVT_VEC4,"TexCoord");
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);
@@ -64,7 +64,7 @@ void main()
{ {
gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4); gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4);
gsc->AddOutput(VAT_VEC2,"TexCoord"); gsc->AddOutput(SVT_VEC2,"TexCoord");
gsc->SetMain(gs_main); gsc->SetMain(gs_main);
return(true); return(true);

View File

@@ -61,7 +61,7 @@ void main()
vsc->AddInput(VAT_VEC4,VAN::TexCoord); vsc->AddInput(VAT_VEC4,VAN::TexCoord);
vsc->AddOutput(VAT_VEC4,"TexCoord"); vsc->AddOutput(SVT_VEC4,"TexCoord");
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);
@@ -71,7 +71,7 @@ void main()
{ {
gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4); gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4);
gsc->AddOutput(VAT_VEC2,"TexCoord"); gsc->AddOutput(SVT_VEC2,"TexCoord");
gsc->SetMain(gs_main); gsc->SetMain(gs_main);
return(true); return(true);

View File

@@ -38,7 +38,7 @@ void main()
vsc->AddInput(VAT_VEC4,VAN::Color); vsc->AddInput(VAT_VEC4,VAN::Color);
vsc->AddOutput(VAT_VEC4,"Color"); vsc->AddOutput(SVT_VEC4,"Color");
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);

View File

@@ -53,15 +53,15 @@ namespace
return true; return true;
} }
material_file::ShaderData *CommonProc(VkShaderStageFlagBits ss,ShaderCreateInfo *sc) template<VkShaderStageFlagBits ss,typename SD,typename SCI>
SD *CommonProc(SCI *sc)
{ {
material_file::ShaderData *sd=mfd->shader[ss]; SD *sd=(SD *)(mfd->shader[ss]);
if(!sd) if(!sd)
return(nullptr); return(nullptr);
for(auto &ua:sd->output) sc->AddOutput(sd->output);
sc->AddOutput(ua.vat,ua.name);
for(auto &s:sd->sampler) for(auto &s:sd->sampler)
mci->AddSampler(ss,DescriptorSetType::PerMaterial,s.type,s.name); mci->AddSampler(ss,DescriptorSetType::PerMaterial,s.type,s.name);
@@ -73,18 +73,17 @@ namespace
bool CustomVertexShader(ShaderCreateInfoVertex *vsc) override bool CustomVertexShader(ShaderCreateInfoVertex *vsc) override
{ {
for(auto &ua:mfd->vi) vsc->AddInput(mfd->via);
vsc->AddInput(ua.vat,ua.name);
if(!Std2DMaterial::CustomVertexShader(vsc)) if(!Std2DMaterial::CustomVertexShader(vsc))
return(false); return(false);
return CommonProc(VK_SHADER_STAGE_VERTEX_BIT,vsc); return CommonProc<VK_SHADER_STAGE_VERTEX_BIT,material_file::VertexShaderData,ShaderCreateInfoVertex>(vsc);
} }
bool CustomGeometryShader(ShaderCreateInfoGeometry *gsc) override bool CustomGeometryShader(ShaderCreateInfoGeometry *gsc) override
{ {
material_file::GeometryShaderData *sd=(material_file::GeometryShaderData *)CommonProc(VK_SHADER_STAGE_GEOMETRY_BIT,gsc); material_file::GeometryShaderData *sd=CommonProc<VK_SHADER_STAGE_GEOMETRY_BIT,material_file::GeometryShaderData,ShaderCreateInfoGeometry>(gsc);
if(!sd) if(!sd)
return(false); return(false);
@@ -96,7 +95,7 @@ namespace
bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override
{ {
return CommonProc(VK_SHADER_STAGE_FRAGMENT_BIT,fsc); return CommonProc<VK_SHADER_STAGE_FRAGMENT_BIT,material_file::FragmentShaderData,ShaderCreateInfoFragment>(fsc);
} }
};//class Std2DMaterialLoader:public Std2DMaterial };//class Std2DMaterialLoader:public Std2DMaterial
}//namespace }//namespace

View File

@@ -22,10 +22,11 @@ void main()
vec2( 0.5, 0.5) vec2( 0.5, 0.5)
); );
mat4 MVPMatrix=camera.vp*l2w.mats[Input[0].l2w_id];
for(int i=0;i<4;i++) for(int i=0;i<4;i++)
{ {
gl_Position=camera.vp gl_Position=MVPMatrix
*l2w.mats[Input[0].l2w_id]
*vec4( gl_in[0].gl_Position.xyz+ *vec4( gl_in[0].gl_Position.xyz+
BillboardVertex[i].x*camera.billboard_right+ BillboardVertex[i].x*camera.billboard_right+
BillboardVertex[i].y*camera.billboard_up, BillboardVertex[i].y*camera.billboard_up,
@@ -57,7 +58,7 @@ void main()
if(!Std3DMaterial::CustomVertexShader(vsc)) if(!Std3DMaterial::CustomVertexShader(vsc))
return(false); return(false);
vsc->AddOutput(VAT_UINT,"l2w_id",Interpolation::Flat); vsc->AddOutput(SVT_UINT,"l2w_id",Interpolation::Flat);
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);
@@ -67,7 +68,7 @@ void main()
{ {
gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4); gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4);
gsc->AddOutput(VAT_VEC2,"TexCoord"); gsc->AddOutput(SVT_VEC2,"TexCoord");
gsc->SetMain(gs_main); gsc->SetMain(gs_main);
return(true); return(true);

View File

@@ -59,7 +59,7 @@ void main()
if(!Std3DMaterial::CustomVertexShader(vsc)) if(!Std3DMaterial::CustomVertexShader(vsc))
return(false); return(false);
vsc->AddOutput(VAT_VEC2,"BillboardSize"); vsc->AddOutput(SVT_VEC2,"BillboardSize");
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);
@@ -69,7 +69,7 @@ void main()
{ {
gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4); gsc->SetGeom(Prim::Points,Prim::TriangleStrip,4);
gsc->AddOutput(VAT_VEC2,"TexCoord"); gsc->AddOutput(SVT_VEC2,"TexCoord");
gsc->SetMain(gs_main); gsc->SetMain(gs_main);
return(true); return(true);

View File

@@ -38,7 +38,7 @@ void main()
vsc->AddInput(VAT_VEC4,VAN::Color); vsc->AddInput(VAT_VEC4,VAN::Color);
vsc->AddOutput(VAT_VEC4,"Color"); vsc->AddOutput(SVT_VEC4,"Color");
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);

View File

@@ -43,7 +43,7 @@ void main()
vsc->AddInput(VAT_FLOAT,VAN::Luminance); vsc->AddInput(VAT_FLOAT,VAN::Luminance);
vsc->AddOutput(VAT_VEC4,"Color"); vsc->AddOutput(SVT_VEC4,"Color");
vsc->SetMain(vs_main); vsc->SetMain(vs_main);
return(true); return(true);

View File

@@ -54,15 +54,15 @@ namespace
return true; return true;
} }
material_file::ShaderData *CommonProc(VkShaderStageFlagBits ss,ShaderCreateInfo *sc) template<VkShaderStageFlagBits ss,typename SD,typename SCI>
SD *CommonProc(SCI *sc)
{ {
material_file::ShaderData *sd=mfd->shader[ss]; SD *sd=(SD *)(mfd->shader[ss]);
if(!sd) if(!sd)
return (nullptr); return (nullptr);
for(auto &ua:sd->output) sc->AddOutput(sd->output);
sc->AddOutput(ua.vat,ua.name);
for(auto &s:sd->sampler) for(auto &s:sd->sampler)
mci->AddSampler(ss,DescriptorSetType::PerMaterial,s.type,s.name); mci->AddSampler(ss,DescriptorSetType::PerMaterial,s.type,s.name);
@@ -74,18 +74,17 @@ namespace
bool CustomVertexShader(ShaderCreateInfoVertex *vsc) override bool CustomVertexShader(ShaderCreateInfoVertex *vsc) override
{ {
for(auto &ua:mfd->vi) vsc->AddInput(mfd->via);
vsc->AddInput(ua.vat,ua.name);
if(!Std3DMaterial::CustomVertexShader(vsc)) if(!Std3DMaterial::CustomVertexShader(vsc))
return (false); return (false);
return CommonProc(VK_SHADER_STAGE_VERTEX_BIT,vsc); return CommonProc<VK_SHADER_STAGE_VERTEX_BIT,material_file::VertexShaderData,ShaderCreateInfoVertex>(vsc);
} }
bool CustomGeometryShader(ShaderCreateInfoGeometry *gsc) override bool CustomGeometryShader(ShaderCreateInfoGeometry *gsc) override
{ {
material_file::GeometryShaderData *sd=(material_file::GeometryShaderData *)CommonProc(VK_SHADER_STAGE_GEOMETRY_BIT,gsc); material_file::GeometryShaderData *sd=CommonProc<VK_SHADER_STAGE_GEOMETRY_BIT,material_file::GeometryShaderData,ShaderCreateInfoGeometry>(gsc);
if(!sd) if(!sd)
return (false); return (false);
@@ -97,7 +96,7 @@ namespace
bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override bool CustomFragmentShader(ShaderCreateInfoFragment *fsc) override
{ {
return CommonProc(VK_SHADER_STAGE_FRAGMENT_BIT,fsc); return CommonProc<VK_SHADER_STAGE_FRAGMENT_BIT,material_file::FragmentShaderData,ShaderCreateInfoFragment>(fsc);
} }
}; // class Std3DMaterialLoader:public Std3DMaterial }; // class Std3DMaterialLoader:public Std3DMaterial
} // namespace } // namespace

View File

@@ -40,6 +40,7 @@ SET(SHADERGEN_COMMON_FILES ${STD_MTL_HEADER_PATH}/UniformBuffer.h
${STD_MTL_HEADER_PATH}/UBOCommon.h ${STD_MTL_HEADER_PATH}/UBOCommon.h
${STD_MTL_HEADER_PATH}/SamplerName.h ${STD_MTL_HEADER_PATH}/SamplerName.h
${STD_MTL_HEADER_PATH}/ShaderVariableType.h ${STD_MTL_HEADER_PATH}/ShaderVariableType.h
ShaderVariableType.cpp
common/MFCommon.h common/MFCommon.h
common/MFGetPosition.h common/MFGetPosition.h
common/MFGetNormal.h common/MFGetNormal.h

View File

@@ -58,7 +58,7 @@ bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const Descr
ubo->stage_flag|=flag_bit; ubo->stage_flag|=flag_bit;
return sc->sdi->AddUBO(set_type,ubo); return sc->AddUBO(set_type,ubo);
} }
else else
{ {
@@ -67,7 +67,7 @@ bool MaterialCreateInfo::AddUBO(const VkShaderStageFlagBits flag_bit,const Descr
ubo->type=struct_name; ubo->type=struct_name;
hgl::strcpy(ubo->name,DESCRIPTOR_NAME_MAX_LENGTH,name); hgl::strcpy(ubo->name,DESCRIPTOR_NAME_MAX_LENGTH,name);
return sc->sdi->AddUBO(set_type,mdi.AddUBO(flag_bit,set_type,ubo)); return sc->AddUBO(set_type,mdi.AddUBO(flag_bit,set_type,ubo));
} }
} }
@@ -116,7 +116,7 @@ bool MaterialCreateInfo::AddSampler(const VkShaderStageFlagBits flag_bit,const D
sampler->stage_flag|=flag_bit; sampler->stage_flag|=flag_bit;
return sc->sdi->AddSampler(set_type,sampler); return sc->AddSampler(set_type,sampler);
} }
else else
{ {
@@ -125,7 +125,7 @@ bool MaterialCreateInfo::AddSampler(const VkShaderStageFlagBits flag_bit,const D
sampler->type=st_name; sampler->type=st_name;
hgl::strcpy(sampler->name,DESCRIPTOR_NAME_MAX_LENGTH,name); hgl::strcpy(sampler->name,DESCRIPTOR_NAME_MAX_LENGTH,name);
return sc->sdi->AddSampler(set_type,mdi.AddSampler(flag_bit,set_type,sampler)); return sc->AddSampler(set_type,mdi.AddSampler(flag_bit,set_type,sampler));
} }
} }

View File

@@ -6,6 +6,7 @@
#include<hgl/graph/VKSamplerType.h> #include<hgl/graph/VKSamplerType.h>
#include<hgl/graph/VKPrimitiveType.h> #include<hgl/graph/VKPrimitiveType.h>
#include<hgl/graph/VKDescriptorSetType.h> #include<hgl/graph/VKDescriptorSetType.h>
#include<hgl/graph/mtl/ShaderVariableType.h>
namespace material_file namespace material_file
{ {
@@ -14,13 +15,6 @@ namespace material_file
constexpr size_t SHADER_RESOURCE_NAME_MAX_LENGTH=VERTEX_ATTRIB_NAME_MAX_LENGTH; constexpr size_t SHADER_RESOURCE_NAME_MAX_LENGTH=VERTEX_ATTRIB_NAME_MAX_LENGTH;
struct ShaderIOAttrib
{
VAType vat;
char name[VERTEX_ATTRIB_NAME_MAX_LENGTH];
};
struct MaterialInstanceData struct MaterialInstanceData
{ {
const char *code; const char *code;
@@ -61,8 +55,6 @@ namespace material_file
const char * code; const char * code;
uint code_length; uint code_length;
List<ShaderIOAttrib> output;
List<SamplerData> sampler; List<SamplerData> sampler;
public: public:
@@ -78,15 +70,41 @@ namespace material_file
const VkShaderStageFlagBits GetShaderStage()const{return shader_stage;} const VkShaderStageFlagBits GetShaderStage()const{return shader_stage;}
}; };
struct VertexShaderData:public ShaderData
{
SVList output;
VertexShaderData():ShaderData(VK_SHADER_STAGE_VERTEX_BIT){}
public:
void AddOutput(const ShaderVariable &sv){output.Add(sv);}
};
struct GeometryShaderData:public ShaderData struct GeometryShaderData:public ShaderData
{ {
Prim input_prim; Prim input_prim;
Prim output_prim; Prim output_prim;
uint max_vertices=0; uint max_vertices=0;
SVList output;
public: public:
using ShaderData::ShaderData; GeometryShaderData():ShaderData(VK_SHADER_STAGE_GEOMETRY_BIT){}
public:
void AddOutput(const ShaderVariable &sv){output.Add(sv);}
};
struct FragmentShaderData:public ShaderData
{
VIAList output;
FragmentShaderData():ShaderData(VK_SHADER_STAGE_FRAGMENT_BIT){}
void AddOutput(const VIA &via){output.Add(via);}
}; };
using ShaderDataMap=ObjectMap<VkShaderStageFlagBits,ShaderData>; using ShaderDataMap=ObjectMap<VkShaderStageFlagBits,ShaderData>;
@@ -109,7 +127,7 @@ namespace material_file
MaterialInstanceData mi{}; MaterialInstanceData mi{};
List<ShaderIOAttrib> vi; ///<Vertex Input VIAList via; ///<Vertex Input
UBODataList ubo_list; UBODataList ubo_list;

View File

@@ -346,13 +346,74 @@ namespace
} }
};//struct MaterialInstanceBlockParse };//struct MaterialInstanceBlockParse
bool ParseUniformAttrib(ShaderIOAttrib *ua,const char *str) bool ParseInterpolation(Interpolation *inter,const char *name)
{
if(!inter)return(false);
if(!name||!*name)return(false);
for(int i=(int)Interpolation::Smooth;i<(int)Interpolation::Flat;i++)
if(!strcmp(name,InterpolationName[i],sizeof(InterpolationName[i])))
{
*inter=Interpolation(i);
return(true);
}
return(false);
}
bool ParseOutputItem(VIA *via,const char *str)
{ {
const char *sp; const char *sp;
while(*str==' '||*str=='\t')++str; while(*str==' '||*str=='\t')++str;
if(!ParseVertexAttribType(&(ua->vat),str)) if(ParseInterpolation(&(via->interpolation),str))
{
while(*str!=' '&&*str!='\t')++str;
while(*str==' '||*str=='\t')++str;
}
else
{
via->interpolation=Interpolation::Smooth;
}
VAType vat;
if(!ParseVertexAttribType(&(vat),str))
return(false);
via->basetype=uint8(vat.basetype);
via->vec_size=vat.vec_size;
while(*str!=' '&&*str!='\t')++str;
while(*str==' '||*str=='\t')++str;
sp=str;
while(hgl::iscodechar(*str))++str;
hgl::strcpy(via->name,SHADER_RESOURCE_NAME_MAX_LENGTH,sp,str-sp);
return(true);
}
bool ParseOutputItem(ShaderVariable *sv,const char *str)
{
const char *sp;
while(*str==' '||*str=='\t')++str;
if(ParseInterpolation(&(sv->interpolation),str))
{
while(*str!=' '&&*str!='\t')++str;
while(*str==' '||*str=='\t')++str;
}
else
{
sv->interpolation=Interpolation::Smooth;
}
if(!sv->type.ParseTypeString(str))
return(false); return(false);
while(*str!=' '&&*str!='\t')++str; while(*str!=' '&&*str!='\t')++str;
@@ -362,20 +423,20 @@ namespace
while(hgl::iscodechar(*str))++str; while(hgl::iscodechar(*str))++str;
hgl::strcpy(ua->name,SHADER_RESOURCE_NAME_MAX_LENGTH,sp,str-sp); hgl::strcpy(sv->name,SHADER_RESOURCE_NAME_MAX_LENGTH,sp,str-sp);
return(true); return(true);
} }
struct VertexInputBlockParse:public TextParse struct VertexInputBlockParse:public TextParse
{ {
List<ShaderIOAttrib> *vi_list=nullptr; List<VIA> *via_list=nullptr;
public: public:
VertexInputBlockParse(List<ShaderIOAttrib> *ual) VertexInputBlockParse(List<VIA> *ual)
{ {
vi_list=ual; via_list=ual;
} }
bool OnLine(char *text,const int len) override bool OnLine(char *text,const int len) override
@@ -383,18 +444,19 @@ namespace
if(!text||!*text||len<=0) if(!text||!*text||len<=0)
return(false); return(false);
ShaderIOAttrib ua; VIA via;
if(ParseUniformAttrib(&ua,text)) if(ParseOutputItem(&via,text))
vi_list->Add(ua); via_list->Add(via);
return(true); return(true);
} }
};//struct VertexInputBlockParse };//struct VertexInputBlockParse
template<typename SD,typename OIT>
struct ShaderBlockParse:public TextParse struct ShaderBlockParse:public TextParse
{ {
ShaderData * shader_data=nullptr; SD * shader_data=nullptr;
bool output=false; bool output=false;
@@ -403,7 +465,7 @@ namespace
public: public:
ShaderBlockParse(ShaderData *sd) ShaderBlockParse(SD *sd)
{ {
shader_data=sd; shader_data=sd;
} }
@@ -434,10 +496,10 @@ namespace
return(true); return(true);
} }
ShaderIOAttrib ua; OIT output_item;
if(ParseUniformAttrib(&ua,text)) if(ParseOutputItem(&output_item,text))
shader_data->output.Add(ua); shader_data->AddOutput(output_item);
} }
if(hgl::stricmp(text,"Code",4)==0) if(hgl::stricmp(text,"Code",4)==0)
@@ -481,7 +543,10 @@ namespace
} }
};//struct ShaderBlockParse };//struct ShaderBlockParse
struct GeometryShaderBlockParse:public ShaderBlockParse using VertexShaderBlockParse=ShaderBlockParse<VertexShaderData,ShaderVariable>;
using FragmentShaderBlockParse=ShaderBlockParse<FragmentShaderData,VIA>;
struct GeometryShaderBlockParse:public ShaderBlockParse<GeometryShaderData,ShaderVariable>
{ {
GeometryShaderData *gsd=nullptr; GeometryShaderData *gsd=nullptr;
@@ -550,7 +615,6 @@ namespace
return(true); return(true);
} }
};//struct GeometryShaderBlockParse };//struct GeometryShaderBlockParse
struct MaterialTextParse:public TextParse struct MaterialTextParse:public TextParse
@@ -594,7 +658,7 @@ namespace
parse=new MaterialInstanceBlockParse(&(mfd->mi)); parse=new MaterialInstanceBlockParse(&(mfd->mi));
else else
if(state==MaterialFileBlock::VertexInput) if(state==MaterialFileBlock::VertexInput)
parse=new VertexInputBlockParse(&(mfd->vi)); parse=new VertexInputBlockParse(&(mfd->via));
else else
if(state>=MaterialFileBlock::Vertex if(state>=MaterialFileBlock::Vertex
&&state<=MaterialFileBlock::Fragment) &&state<=MaterialFileBlock::Fragment)
@@ -603,23 +667,23 @@ namespace
if(state==MaterialFileBlock::Vertex) if(state==MaterialFileBlock::Vertex)
{ {
sd=new ShaderData(VK_SHADER_STAGE_VERTEX_BIT); sd=new VertexShaderData();
parse=new ShaderBlockParse(sd); parse=new VertexShaderBlockParse((VertexShaderData *)sd);
} }
else else
if(state==MaterialFileBlock::Geometry) if(state==MaterialFileBlock::Geometry)
{ {
sd=new GeometryShaderData(VK_SHADER_STAGE_GEOMETRY_BIT); sd=new GeometryShaderData();
parse=new GeometryShaderBlockParse((GeometryShaderData *)sd); parse=new GeometryShaderBlockParse((GeometryShaderData *)sd);
} }
else else
if(state==MaterialFileBlock::Fragment) if(state==MaterialFileBlock::Fragment)
{ {
sd=new ShaderData(VK_SHADER_STAGE_FRAGMENT_BIT); sd=new FragmentShaderData();
parse=new ShaderBlockParse(sd); parse=new FragmentShaderBlockParse((FragmentShaderData *)sd);
} }
if(!sd) if(!sd)

View File

@@ -9,11 +9,10 @@
namespace hgl{namespace graph{ namespace hgl{namespace graph{
ShaderCreateInfo::ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorInfo *m) ShaderCreateInfo::ShaderCreateInfo()
{ {
shader_stage=ss; hgl_zero(shader_stage);
mdi=m; mdi=nullptr;
sdi=new ShaderDescriptorInfo(ss);
spv_data=nullptr; spv_data=nullptr;
@@ -21,12 +20,16 @@ ShaderCreateInfo::ShaderCreateInfo(VkShaderStageFlagBits ss,MaterialDescriptorIn
define_value_max_length=0; define_value_max_length=0;
} }
void ShaderCreateInfo::Init(ShaderDescriptorInfo *sdi,MaterialDescriptorInfo *m)
{
shader_stage=sdi->GetShaderStage();
mdi=m;
}
ShaderCreateInfo::~ShaderCreateInfo() ShaderCreateInfo::~ShaderCreateInfo()
{ {
if(spv_data) if(spv_data)
FreeSPVData(spv_data); FreeSPVData(spv_data);
delete sdi;
} }
bool ShaderCreateInfo::AddDefine(const AnsiString &m,const AnsiString &v) bool ShaderCreateInfo::AddDefine(const AnsiString &m,const AnsiString &v)
@@ -100,104 +103,31 @@ bool ShaderCreateInfo::ProcDefine()
return(true); return(true);
} }
int ShaderCreateInfo::AddOutput(const VAType &type,const AnsiString &name,Interpolation inter) void ShaderCreateInfo::AddStruct(const AnsiString &name)
{ {
VertexInputAttribute *ss=new VertexInputAttribute; return GetSDI()->AddStruct(name);
hgl::strcpy(ss->name,sizeof(ss->name),name.c_str());
ss->basetype =(uint8)type.basetype;
ss->vec_size = type.vec_size;
ss->interpolation = inter;
return sdi->AddOutput(ss);
} }
int ShaderCreateInfo::AddOutput(const AnsiString &type,const AnsiString &name,Interpolation inter) bool ShaderCreateInfo::AddUBO(DescriptorSetType type,const UBODescriptor *sd)
{ {
VAType vat; return GetSDI()->AddUBO(type,sd);
if(name.IsEmpty())
return -1;
if(!ParseVertexAttribType(&vat,type))
return -2;
return AddOutput(vat,name,inter);
} }
//int ShaderCreateInfo::AddOutput(const ShaderVariableType &type,const AnsiString &name,Interpolation inter=Interpolation::Smooth) bool ShaderCreateInfo::AddSampler(DescriptorSetType type,const SamplerDescriptor *sd)
//{
// VertexInputAttribute *ss=new VertexInputAttribute;
//
// hgl::strcpy(ss->name,sizeof(ss->name),name.c_str());
//
// ss->basetype =(uint8)type.base_type;
// ss->vec_size = type.vec_size;
// ss->interpolation = inter;
//
// return sdi->AddOutput(ss);
//
//}
bool ShaderCreateInfo::ProcSubpassInput()
{ {
auto sil=sdi->GetSubpassInputList(); return GetSDI()->AddSampler(type,sd);
if(sil.IsEmpty())
return(true);
final_shader+="\n";
auto si=sil.GetData();
int si_count=sil.GetCount();
for(int i=0;i<si_count;i++)
{
final_shader+="layout(input_attachment_index=";
final_shader+=AnsiString::numberOf((*si)->input_attachment_index);
final_shader+=", binding=";
final_shader+=AnsiString::numberOf((*si)->binding);
final_shader+=") uniform subpassInput ";
final_shader+=(*si)->name;
final_shader+=";\n";
++si;
}
return(true);
} }
namespace
{
constexpr const char MI_ID_OUTPUT[]="MaterialInstanceID";
constexpr const char MF_GetMI_VS []="\nMaterialInstance GetMI(){return mtl.mi[Assign.y];}\n";
constexpr const char MF_GetMI_Other []="\nMaterialInstance GetMI(){return mtl.mi[Input.MaterialInstanceID];}\n";
constexpr const char MF_HandoverMI_VS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Assign.y;}\n";
constexpr const char MF_HandoverMI_GS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input[0].MaterialInstanceID;}\n";
constexpr const char MF_HandoverMI_OTHER[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input.MaterialInstanceID;}\n";
}//namespace
void ShaderCreateInfo::SetMaterialInstance(UBODescriptor *ubo,const AnsiString &mi) void ShaderCreateInfo::SetMaterialInstance(UBODescriptor *ubo,const AnsiString &mi)
{ {
sdi->AddUBO(DescriptorSetType::PerMaterial,ubo); AddUBO(DescriptorSetType::PerMaterial,ubo);
sdi->AddStruct(mtl::MaterialInstanceStruct); AddStruct(mtl::MaterialInstanceStruct);
AddFunction(shader_stage==VK_SHADER_STAGE_VERTEX_BIT?MF_GetMI_VS:MF_GetMI_Other); AddFunction(shader_stage==VK_SHADER_STAGE_VERTEX_BIT?mtl::func::MF_GetMI_VS:mtl::func::MF_GetMI_Other);
mi_codes=mi; mi_codes=mi;
} }
void ShaderCreateInfo::AddMaterialInstanceOutput()
{
AddOutput(VAT_UINT,MI_ID_OUTPUT,Interpolation::Flat);
if(shader_stage==VK_SHADER_STAGE_VERTEX_BIT) AddFunction(MF_HandoverMI_VS);else
if(shader_stage==VK_SHADER_STAGE_GEOMETRY_BIT) AddFunction(MF_HandoverMI_GS);else
AddFunction(MF_HandoverMI_OTHER);
}
bool ShaderCreateInfo::ProcInput(ShaderCreateInfo *last_sc) bool ShaderCreateInfo::ProcInput(ShaderCreateInfo *last_sc)
{ {
if(!last_sc) if(!last_sc)
@@ -226,33 +156,13 @@ bool ShaderCreateInfo::ProcOutput()
{ {
output_struct.Clear(); output_struct.Clear();
const VertexInputAttributeArray &ssd=sdi->GetShaderStageIO().output; if(IsEmptyOutput())
return(true);
if(ssd.count<=0)return(true);
output_struct=GetShaderStageName(shader_stage); output_struct=GetShaderStageName(shader_stage);
output_struct+="_Output\n{\n"; output_struct+="_Output\n{\n";
const VertexInputAttribute *ss=ssd.items; GetOutputStrcutString(output_struct);
for(uint i=0;i<ssd.count;i++)
{
output_struct+=" ";
if(ss->interpolation!=Interpolation::Smooth)
{
output_struct+=InterpolationName[size_t(ss->interpolation)];
output_struct+=" ";
}
output_struct+=GetShaderAttributeTypename(ss);
output_struct+=" ";
output_struct+=ss->name;
output_struct+=";\n";
++ss;
}
output_struct+="}"; output_struct+="}";
@@ -265,7 +175,7 @@ bool ShaderCreateInfo::ProcOutput()
bool ShaderCreateInfo::ProcStruct() bool ShaderCreateInfo::ProcStruct()
{ {
const AnsiStringList &struct_list=sdi->GetStructList(); const AnsiStringList &struct_list=GetSDI()->GetStructList();
AnsiString codes; AnsiString codes;
@@ -297,7 +207,7 @@ bool ShaderCreateInfo::ProcMI()
bool ShaderCreateInfo::ProcUBO() bool ShaderCreateInfo::ProcUBO()
{ {
auto ubo_list=sdi->GetUBOList(); auto ubo_list=GetSDI()->GetUBOList();
const int count=ubo_list.GetCount(); const int count=ubo_list.GetCount();
@@ -341,7 +251,7 @@ bool ShaderCreateInfo::ProcSSBO()
bool ShaderCreateInfo::ProcConstantID() bool ShaderCreateInfo::ProcConstantID()
{ {
auto const_list=sdi->GetConstList(); auto const_list=GetSDI()->GetConstList();
const int count=const_list.GetCount(); const int count=const_list.GetCount();
@@ -371,7 +281,7 @@ bool ShaderCreateInfo::ProcConstantID()
bool ShaderCreateInfo::ProcSampler() bool ShaderCreateInfo::ProcSampler()
{ {
auto sampler_list=sdi->GetSamplerList(); auto sampler_list=GetSDI()->GetSamplerList();
const int count=sampler_list.GetCount(); const int count=sampler_list.GetCount();
@@ -430,8 +340,6 @@ bool ShaderCreateInfo::CreateShader(ShaderCreateInfo *last_sc)
if(!ProcLayout()) if(!ProcLayout())
return(false); return(false);
if(!ProcSubpassInput())
return(false);
if(!ProcInput(last_sc)) if(!ProcInput(last_sc))
return(false); return(false);
// if(!ProcStruct()) // if(!ProcStruct())

View File

@@ -3,11 +3,52 @@
namespace hgl{namespace graph{ namespace hgl{namespace graph{
using namespace hgl::graph; int ShaderCreateInfoFragment::AddOutput(VIAList &via_list)
{
int count=0;
for(VIA &via:via_list)
{
//都输出了,没这些值
//via.input_rate=VK_VERTEX_INPUT_RATE_VERTEX;
//via.group=VertexInputGroup::Basic;
if(fsdi.AddOutput(via))
++count;
}
return count;
}
int ShaderCreateInfoFragment::AddOutput(const VAType &type,const AnsiString &name,Interpolation inter)
{
VertexInputAttribute via;
hgl::strcpy(via.name,sizeof(via.name),name.c_str());
via.basetype =(uint8)type.basetype;
via.vec_size = type.vec_size;
via.interpolation = inter;
return fsdi.AddOutput(via);
}
int ShaderCreateInfoFragment::AddOutput(const AnsiString &type,const AnsiString &name,Interpolation inter)
{
VAType vat;
if(name.IsEmpty())
return -1;
if(!ParseVertexAttribType(&vat,type))
return -2;
return AddOutput(vat,name,inter);
}
bool ShaderCreateInfoFragment::ProcOutput() bool ShaderCreateInfoFragment::ProcOutput()
{ {
const auto &output_list=sdi->GetShaderStageIO().output; const auto &output_list=fsdi.GetOutput();
const VertexInputAttribute *o=output_list.items; const VertexInputAttribute *o=output_list.items;

View File

@@ -1,4 +1,5 @@
#include<hgl/shadergen/ShaderCreateInfoGeometry.h> #include<hgl/shadergen/ShaderCreateInfoGeometry.h>
#include"common/MFCommon.h"
namespace hgl namespace hgl
{ {
@@ -19,6 +20,44 @@ namespace hgl
return(true); return(true);
} }
void ShaderCreateInfoGeometry::AddMaterialInstanceOutput()
{
AddOutput(SVT_UINT,mtl::func::MI_ID_OUTPUT,Interpolation::Flat);
AddFunction(mtl::func::MF_HandoverMI_GS);
}
int ShaderCreateInfoGeometry::AddOutput(SVList &sv_list)
{
int count=0;
for(ShaderVariable &sv:sv_list)
{
sv.interpolation=Interpolation::Smooth;
if(gsdi.AddOutput(sv))
++count;
}
return count;
}
int ShaderCreateInfoGeometry::AddOutput(const SVType &type,const AnsiString &name,Interpolation inter)
{
ShaderVariable sv;
hgl::strcpy(sv.name,sizeof(sv.name),name.c_str());
sv.type=type;
sv.interpolation=inter;
return gsdi.AddOutput(sv);
}
void ShaderCreateInfoGeometry::GetOutputStrcutString(AnsiString &str)
{
gsdi.GetOutput().ToString(str);
}
bool ShaderCreateInfoGeometry::ProcLayout() bool ShaderCreateInfoGeometry::ProcLayout()
{ {
final_shader+="layout("+input_prim+") in;\n" final_shader+="layout("+input_prim+") in;\n"

View File

@@ -1,5 +1,4 @@
#include<hgl/shadergen/ShaderCreateInfoVertex.h> #include<hgl/shadergen/ShaderCreateInfoVertex.h>
#include<hgl/shadergen/ShaderDescriptorInfo.h>
#include<hgl/graph/VertexAttrib.h> #include<hgl/graph/VertexAttrib.h>
#include<hgl/graph/VKShaderStage.h> #include<hgl/graph/VKShaderStage.h>
#include<hgl/graph/VKRenderAssign.h> #include<hgl/graph/VKRenderAssign.h>
@@ -8,23 +7,44 @@
#include"ShaderLibrary.h" #include"ShaderLibrary.h"
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
ShaderCreateInfoVertex::ShaderCreateInfoVertex(MaterialDescriptorInfo *m):ShaderCreateInfo(VK_SHADER_STAGE_VERTEX_BIT,m)
void ShaderCreateInfoVertex::AddMaterialInstanceOutput()
{ {
AddOutput(SVT_UINT,mtl::func::MI_ID_OUTPUT,Interpolation::Flat);
AddFunction(mtl::func::MF_HandoverMI_VS);
}
int ShaderCreateInfoVertex::AddInput(VIAList &via_list)
{
int count=0;
for(VIA &via:via_list)
{
via.input_rate=VK_VERTEX_INPUT_RATE_VERTEX;
via.group=VertexInputGroup::Basic;
if(vsdi.AddInput(via))
++count;
}
return count;
} }
int ShaderCreateInfoVertex::AddInput(const VAType &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)
{ {
VertexInputAttribute *ss=new VertexInputAttribute; VIA via;
hgl::strcpy(ss->name,sizeof(ss->name),name.c_str()); hgl::strcpy(via.name,sizeof(via.name),name.c_str());
ss->basetype=(uint8) type.basetype; via.basetype=(uint8) type.basetype;
ss->vec_size= type.vec_size; via.vec_size= type.vec_size;
ss->input_rate =input_rate; via.input_rate =input_rate;
ss->group =group; via.group =group;
return sdi->AddInput(ss); via.interpolation =Interpolation::Smooth;
return vsdi.AddInput(via);
} }
int ShaderCreateInfoVertex::AddInput(const AnsiString &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group) int ShaderCreateInfoVertex::AddInput(const AnsiString &type,const AnsiString &name,const VkVertexInputRate input_rate,const VertexInputGroup &group)
@@ -39,7 +59,34 @@ int ShaderCreateInfoVertex::AddInput(const AnsiString &type,const AnsiString &na
int ShaderCreateInfoVertex::hasInput(const char *name) int ShaderCreateInfoVertex::hasInput(const char *name)
{ {
return sdi->hasInput(name); return vsdi.hasInput(name);
}
int ShaderCreateInfoVertex::AddOutput(SVList &sv_list)
{
int count=0;
for(ShaderVariable &sv:sv_list)
{
sv.interpolation=Interpolation::Smooth;
if(vsdi.AddOutput(sv))
++count;
}
return count;
}
int ShaderCreateInfoVertex::AddOutput(const SVType &type,const AnsiString &name,Interpolation inter)
{
ShaderVariable sv;
hgl::strcpy(sv.name,sizeof(sv.name),name.c_str());
sv.type=type;
sv.interpolation=inter;
return vsdi.AddOutput(sv);
} }
void ShaderCreateInfoVertex::AddJoint() void ShaderCreateInfoVertex::AddJoint()
@@ -58,9 +105,40 @@ void ShaderCreateInfoVertex::AddAssign()
AddFunction(STD_MTL_FUNC_NAMESPACE::MF_GetLocalToWorld_ByAssign); AddFunction(STD_MTL_FUNC_NAMESPACE::MF_GetLocalToWorld_ByAssign);
} }
bool ShaderCreateInfoVertex::ProcSubpassInput()
{
auto sil=vsdi.GetSubpassInputList();
if(sil.IsEmpty())
return(true);
final_shader+="\n";
auto si=sil.GetData();
int si_count=sil.GetCount();
for(int i=0;i<si_count;i++)
{
final_shader+="layout(input_attachment_index=";
final_shader+=AnsiString::numberOf((*si)->input_attachment_index);
final_shader+=", binding=";
final_shader+=AnsiString::numberOf((*si)->binding);
final_shader+=") uniform subpassInput ";
final_shader+=(*si)->name;
final_shader+=";\n";
++si;
}
return(true);
}
bool ShaderCreateInfoVertex::ProcInput(ShaderCreateInfo *) bool ShaderCreateInfoVertex::ProcInput(ShaderCreateInfo *)
{ {
const auto &input=sdi->GetShaderStageIO().input; if(!ProcSubpassInput())
return(false);
const auto &input=vsdi.GetInput();
if(input.count<=0) if(input.count<=0)
{ {
@@ -87,4 +165,9 @@ bool ShaderCreateInfoVertex::ProcInput(ShaderCreateInfo *)
return(true); return(true);
} }
void ShaderCreateInfoVertex::GetOutputStrcutString(AnsiString &str)
{
vsdi.GetOutput().ToString(str);
}
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@@ -5,59 +5,9 @@ ShaderDescriptorInfo::ShaderDescriptorInfo(VkShaderStageFlagBits flag_bit)
{ {
stage_flag=flag_bit; stage_flag=flag_bit;
Init(stage_io);
hgl_zero(push_constant); hgl_zero(push_constant);
} }
namespace
{
bool Find(const VertexInputAttributeArray &sad,const char *name)
{
if(sad.count<=0)
return(false);
for(uint i=0;i<sad.count;i++)
if(hgl::strcmp(sad.items[i].name,name)==0)
return(true);
return(false);
}
}//namespace
bool ShaderDescriptorInfo::AddInput(VertexInputAttribute *ss)
{
if(!ss)return(false);
if(Find(stage_io.input,ss->name))return(false);
ss->location=stage_io.input.count;
Append(&stage_io.input,ss);
return(true);
}
bool ShaderDescriptorInfo::hasInput(const char *name)const
{
if(!name||!*name)return(false);
return Find(stage_io.input,name);
}
bool ShaderDescriptorInfo::AddOutput(VertexInputAttribute *ss)
{
if(!ss)return(false);
if(Find(stage_io.output,ss->name))return(false);
ss->location=stage_io.output.count;
Append(&stage_io.output,ss);
return(true);
}
void ShaderDescriptorInfo::AddStruct(const AnsiString &name) void ShaderDescriptorInfo::AddStruct(const AnsiString &name)
{ {
struct_list.AddUnique(name); struct_list.AddUnique(name);
@@ -95,7 +45,7 @@ bool ShaderDescriptorInfo::AddConstValue(ConstValueDescriptor *sd)
return(true); return(true);
} }
bool ShaderDescriptorInfo::AddSubpassInput(const UTF8String name,uint8_t index) bool VertexShaderDescriptorInfo::AddSubpassInput(const UTF8String name,uint8_t index)
{ {
for(auto *si:subpass_input) for(auto *si:subpass_input)
{ {

View File

@@ -0,0 +1,271 @@
#include<hgl/graph/mtl/ShaderVariableType.h>
VK_NAMESPACE_BEGIN
namespace
{
constexpr const char *scalar_typename[]=
{
"bool",
"int",
"uint",
"float",
"double"
};
constexpr const char *vector_typename[][3]=
{
{"bvec2","bvec3","bvec4"},
{"ivec2","ivec3","ivec4"},
{"uvec2","uvec3","uvec4"},
{ "vec2", "vec3", "vec4"},
{"dvec2","dvec3","dvec4"}
};
constexpr const char *mat_1_typename[][3]=
{
{"mat2","mat3","mat4"},
{"dmat2","dmat3","dmat4"}
};
constexpr const char *mat_2_typename[][3][3]=
{
{
{"mat2x2","mat2x3","mat2x4"},
{"mat3x2","mat3x3","mat3x4"},
{"mat4x2","mat4x3","mat4x4"}
},
{
{"dmat2x2","dmat2x3","dmat2x4"},
{"dmat3x2","dmat3x3","dmat3x4"},
{"dmat4x2","dmat4x3","dmat4x4"}
}
};
constexpr const char AtomicCounterTypename[]="atomic_uint";
}//namespace
const char *ShaderVariableType::GetTypename()const
{
if(base_type==SVBaseType::Scalar)
{
RANGE_CHECK_RETURN_NULLPTR(scalar.type)
return scalar_typename[int(scalar.type)];
}
if(base_type==SVBaseType::Vector)
{
RANGE_CHECK_RETURN_NULLPTR(vector.type)
if(vector.vec_size<2||vector.vec_size>4)
return(nullptr);
return vector_typename[int(vector.type)][vector.vec_size-2];
}
if(base_type==SVBaseType::Matrix)
{
if(matrix.type!=VABaseType::Float&&matrix.type==VABaseType::Double)
return(nullptr);
if(matrix.n<2||matrix.n>4)
return(nullptr);
if(matrix.m==0)
return mat_1_typename[int(matrix.type)-int(VABaseType::Float)][matrix.n-2];
if(matrix.m<2||matrix.m>4)
return(nullptr);
return mat_2_typename[int(matrix.type)-int(VABaseType::Float)][matrix.n-2][matrix.m-2];
}
if(base_type==SVBaseType::Sampler)
{
return GetSamplerTypeName(sampler.type);
}
if(base_type==SVBaseType::Image)
{
return GetShaderImageTypeName(image.type);
}
if(base_type==SVBaseType::AtomicCounter)
{
return AtomicCounterTypename;
}
return nullptr;
}
bool ShaderVariableType::ParseTypeString(const char *str)
{
if(!str||!*str)
return(false);
if(str[0]=='b')
{
if(str[1]=='o'&&str[2]=='o'&&str[3]=='l')
{
base_type=SVBaseType::Scalar;
scalar.type=VABaseType::Bool;
return(true);
}
if(str[1]=='v'&&str[2]=='e'&&str[3]=='c')
{
base_type=SVBaseType::Vector;
vector.type=VABaseType::Bool;
vector.vec_size=str[4]-'0';
return(vector.vec_size>=2&&vector.vec_size<=4);
}
return(false);
}
if(str[1]=='i')
{
if(str[2]=='n'&&str[3]=='t')
{
base_type=SVBaseType::Scalar;
scalar.type=VABaseType::Int;
return(true);
}
if(str[2]=='v'&&str[3]=='e'&&str[4]=='c')
{
base_type=SVBaseType::Vector;
vector.type=VABaseType::Int;
vector.vec_size=str[5]-'0';
return(vector.vec_size>=2&&vector.vec_size<=4);
}
if(str[1]=='m'&&str[2]=='a'&&str[3]=='g'&&str[4]=='e')
{
int name_len=5;
while(iscodechar(str[name_len]))
++name_len;
base_type=SVBaseType::Image;
image.type=ParseShaderImageType(str,name_len);
return(image.type!=ShaderImageType::Error);
}
return(false);
}
if(str[1]=='u')
{
if(str[2]=='i'&&str[3]=='n'&&str[4]=='t')
{
base_type=SVBaseType::Scalar;
scalar.type=VABaseType::UInt;
return(true);
}
if(str[2]=='v'&&str[3]=='e'&&str[4]=='c')
{
base_type=SVBaseType::Vector;
vector.type=VABaseType::UInt;
vector.vec_size=str[5]-'0';
return(vector.vec_size>=2&&vector.vec_size<=4);
}
return(false);
}
if(str[1]=='f')
{
if(str[2]=='l'&&str[3]=='o'&&str[4]=='a'&&str[5]=='t')
{
base_type=SVBaseType::Scalar;
scalar.type=VABaseType::Float;
return(true);
}
return(false);
}
if(str[0]=='v')
{
if(str[1]=='e'&&str[2]=='c')
{
base_type=SVBaseType::Vector;
vector.type=VABaseType::Float;
vector.vec_size=str[3]-'0';
return(vector.vec_size>=2&&vector.vec_size<=4);
}
return(false);
}
if(str[1]=='d')
{
if(str[2]=='o'&&str[3]=='u'&&str[4]=='b'&&str[5]=='l'&&str[6]=='e')
{
base_type=SVBaseType::Scalar;
scalar.type=VABaseType::Double;
return(true);
}
if(str[2]=='m'&&str[3]=='a'&&str[4]=='t')
{
base_type=SVBaseType::Matrix;
matrix.type=VABaseType::Double;
matrix.n=str[5]-'0';
if(str[6]=='x')
{
matrix.m=str[7]-'0';
return(matrix.m>=2&&matrix.m<=4);
}
matrix.m=0;
return(matrix.n>=2&&matrix.n<=4);
}
return(false);
}
if(str[0]=='m')
{
if(str[1]=='a'&&str[2]=='t')
{
base_type=SVBaseType::Matrix;
matrix.type=VABaseType::Float;
matrix.n=str[3]-'0';
if(str[4]=='x')
{
matrix.m=str[5]-'0';
return(matrix.m>=2&&matrix.m<=4);
}
matrix.m=0;
return(matrix.n>=2&&matrix.n<=4);
}
return(false);
}
if(hgl::strcmp(str,"sampler",7)==0)
{
int name_len=7;
while(iscodechar(str[name_len]))
++name_len;
base_type=SVBaseType::Sampler;
sampler.type=ParseSamplerType(str,name_len);
return(sampler.type!=SamplerType::Error);
}
if(hgl::strcmp(str,"atomic_uint",11)==0)
{
base_type=SVBaseType::AtomicCounter;
return(true);
}
return(false);
}
VK_NAMESPACE_END

View File

@@ -9,4 +9,13 @@ STD_MTL_FUNC_NAMESPACE_BEGIN
constexpr const char MF_GetMaterialInstance_ByAssign[]= "\nMaterialInstance GetMaterialInstance(){return mi_set[Assign.y];}\n"; constexpr const char MF_GetMaterialInstance_ByAssign[]= "\nMaterialInstance GetMaterialInstance(){return mi_set[Assign.y];}\n";
constexpr const char MI_ID_OUTPUT[]="MaterialInstanceID";
constexpr const char MF_HandoverMI_VS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Assign.y;}\n";
constexpr const char MF_HandoverMI_GS[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input[0].MaterialInstanceID;}\n";
constexpr const char MF_HandoverMI_OTHER[]= "\nvoid HandoverMI(){Output.MaterialInstanceID=Input.MaterialInstanceID;}\n";
constexpr const char MF_GetMI_VS []="\nMaterialInstance GetMI(){return mtl.mi[Assign.y];}\n";
constexpr const char MF_GetMI_Other []="\nMaterialInstance GetMI(){return mtl.mi[Input.MaterialInstanceID];}\n";
STD_MTL_FUNC_NAMESPACE_END STD_MTL_FUNC_NAMESPACE_END