From b35ef27610ddd5393c984b7c2f6366b05a355944 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Wed, 22 Feb 2023 21:50:18 +0800 Subject: [PATCH] upgraded to newly DescriptorSetType --- inc/hgl/graph/VK.h | 16 +++--- inc/hgl/graph/VKMaterial.h | 16 +++--- inc/hgl/graph/VKMaterialDescriptorSets.h | 9 +++- inc/hgl/graph/VKMaterialInstance.h | 16 +++--- inc/hgl/graph/VKMaterialParameters.h | 11 +++-- inc/hgl/graph/VKShaderResource.h | 1 - res | 2 +- src/SceneGraph/RenderList.cpp | 8 +-- .../Vulkan/VKCommandBufferRender.cpp | 2 +- src/SceneGraph/Vulkan/VKDeviceMaterial.cpp | 16 ++++-- src/SceneGraph/Vulkan/VKMaterial.cpp | 11 +++-- .../Vulkan/VKMaterialDescriptorSets.cpp | 18 ++++--- src/SceneGraph/Vulkan/VKMaterialInstance.cpp | 49 ++++++------------- .../Vulkan/VKPipelineLayoutData.cpp | 2 +- .../Vulkan/VKRenderResourceMaterial.cpp | 21 +++++--- src/SceneGraph/Vulkan/VKShaderResource.cpp | 12 ----- src/SceneGraph/font/TextRender.cpp | 2 +- 17 files changed, 106 insertions(+), 106 deletions(-) diff --git a/inc/hgl/graph/VK.h b/inc/hgl/graph/VK.h index fbbc64b2..67c9e35d 100644 --- a/inc/hgl/graph/VK.h +++ b/inc/hgl/graph/VK.h @@ -75,19 +75,19 @@ enum class DescriptorSetsType //设计使其对应shader中的set Global=0, ///<全局参数(如太阳光等) - Material, ///<材质中永远不变的参数 -// Texture, ///<材质中的纹理参数 - Value, ///<材质中的变量参数 - Primitive, ///<渲染实例参数(如Local2World matrix) + PerFrame, ///<帧参数(如摄像机等) + PerMaterial,///<材质参数(如纹理等) + PerObject, ///<对象参数(如模型矩阵等) - ENUM_CLASS_RANGE(Global,Primitive) + Instance, + Skeleton, + + ENUM_CLASS_RANGE(Global,Skeleton) };// -const DescriptorSetsType CheckDescriptorSetsType(const char *str); - constexpr char *DescriptSetsTypeName[]= { - "Global","Material","Value","Renderable" + "Global","PerFrame","PerMaterial","PerObject","Instance","Skeleton" }; inline const char *GetDescriptorSetsTypeName(const enum class DescriptorSetsType &type) diff --git a/inc/hgl/graph/VKMaterial.h b/inc/hgl/graph/VKMaterial.h index 532e9c6e..8aaf6769 100644 --- a/inc/hgl/graph/VKMaterial.h +++ b/inc/hgl/graph/VKMaterial.h @@ -8,6 +8,8 @@ VK_NAMESPACE_BEGIN using ShaderStageCreateInfoList=List; +using MaterialParameterArray=MaterialParameters *[size_t(DescriptorSetsType::RANGE_SIZE)]; + struct MaterialData { UTF8String name; @@ -21,10 +23,7 @@ struct MaterialData PipelineLayoutData *pipeline_layout_data; - struct - { - MaterialParameters *m,*g,*r; - }mp; + MaterialParameterArray mp_array; private: @@ -66,11 +65,12 @@ public: MaterialParameters * GetMP (const DescriptorSetsType &type) { - if(type==DescriptorSetsType::Material )return data->mp.m;else - if(type==DescriptorSetsType::Primitive )return data->mp.r;else - if(type==DescriptorSetsType::Global )return data->mp.g;else - return(nullptr); + RANGE_CHECK_RETURN_NULLPTR(type) + + return data->mp_array[size_t(type)]; } + + const bool hasSet (const DescriptorSetsType &type)const; };//class Material VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_MATERIAL_INCLUDE diff --git a/inc/hgl/graph/VKMaterialDescriptorSets.h b/inc/hgl/graph/VKMaterialDescriptorSets.h index b4e25bc6..17d55996 100644 --- a/inc/hgl/graph/VKMaterialDescriptorSets.h +++ b/inc/hgl/graph/VKMaterialDescriptorSets.h @@ -26,6 +26,7 @@ class MaterialDescriptorSets uint sd_count; ShaderDescriptorList descriptor_list[VK_DESCRIPTOR_TYPE_RANGE_SIZE]; + ShaderDescriptorList descriptor_list_by_set_type[size_t(DescriptorSetsType::RANGE_SIZE)]; Map sd_by_name; Map binding_map[VK_DESCRIPTOR_TYPE_RANGE_SIZE]; @@ -34,7 +35,7 @@ class MaterialDescriptorSets private: - DescriptorSetLayoutCreateInfo sds[size_t(DescriptorSetsType::RANGE_SIZE)]; + DescriptorSetLayoutCreateInfo dsl_ci[size_t(DescriptorSetsType::RANGE_SIZE)]; public: @@ -50,7 +51,11 @@ public: const int GetSampler (const AnsiString &name )const{return GetBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,name);} const int GetAttachment (const AnsiString &name )const{return GetBinding(VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,name);} - const DescriptorSetLayoutCreateInfo *GetBinding(const DescriptorSetsType &type)const{return sds+size_t(type);} + const DescriptorSetLayoutCreateInfo *GetDSLCI(const DescriptorSetsType &type)const{return dsl_ci+size_t(type);} + + const ShaderDescriptorList &GetDescriptorList(const DescriptorSetsType &type)const{return descriptor_list_by_set_type[size_t(type)];} + + const bool hasSet(const DescriptorSetsType &type)const{return !descriptor_list_by_set_type[size_t(type)].IsEmpty();} };//class MaterialDescriptorSets VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_MATERIAL_DESCRIPTOR_SETS_INCLUDE diff --git a/inc/hgl/graph/VKMaterialInstance.h b/inc/hgl/graph/VKMaterialInstance.h index cb431d12..f4d0d863 100644 --- a/inc/hgl/graph/VKMaterialInstance.h +++ b/inc/hgl/graph/VKMaterialInstance.h @@ -1,7 +1,7 @@ -#ifndef HGL_GRAPH_VULKAN_MATERIAL_INSTANCE_INCLUDE +#ifndef HGL_GRAPH_VULKAN_MATERIAL_INSTANCE_INCLUDE #define HGL_GRAPH_VULKAN_MATERIAL_INSTANCE_INCLUDE -#include +#include VK_NAMESPACE_BEGIN class MaterialInstance @@ -10,23 +10,23 @@ class MaterialInstance VIL *vil; - MaterialParameters *mp_value; + MaterialParameters *mp_per_mi; ///<材质实例独有参数,对应PerMaterial合集 private: friend class GPUDevice; - MaterialInstance(Material *,VIL *,MaterialParameters *); + MaterialInstance(Material *,VIL *); public: - virtual ~MaterialInstance(); + virtual ~MaterialInstance()=default; Material *GetMaterial(){return material;} const VIL *GetVIL()const{return vil;} - MaterialParameters *GetMP(){return mp_value;} - MaterialParameters *GetMP(const DescriptorSetsType &type); + MaterialParameters *GetMP(){return mp_per_mi;} + MaterialParameters *GetMP(const DescriptorSetsType &type){return material->GetMP(type);} bool BindUBO(const DescriptorSetsType &type,const AnsiString &name,DeviceBuffer *ubo,bool dynamic=false); bool BindSSBO(const DescriptorSetsType &type,const AnsiString &name,DeviceBuffer *ubo,bool dynamic=false); @@ -34,4 +34,4 @@ public: };//class MaterialInstance VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_MATERIAL_INSTANCE_INCLUDE - \ No newline at end of file + diff --git a/inc/hgl/graph/VKMaterialParameters.h b/inc/hgl/graph/VKMaterialParameters.h index 8131fcf8..1309ae8e 100644 --- a/inc/hgl/graph/VKMaterialParameters.h +++ b/inc/hgl/graph/VKMaterialParameters.h @@ -22,7 +22,7 @@ private: public: const DescriptorSetsType GetType (){return ds_type;} - DescriptorSet * GetDescriptorSet (){return descriptor_sets;} + DescriptorSet * GetDescriptorSet (){return descriptor_sets;} const VkDescriptorSet GetVkDescriptorSet ()const{return descriptor_sets->GetDescriptorSet();} const uint32_t GetCount ()const{return descriptor_sets->GetCount();} @@ -31,10 +31,11 @@ public: public: #define MP_TYPE_IS(name) const bool is##name()const{return ds_type==DescriptorSetsType::name;} - MP_TYPE_IS(Material) -// MP_TYPE_IS(Texture) - MP_TYPE_IS(Value) - MP_TYPE_IS(Primitive) + MP_TYPE_IS(Skeleton) + MP_TYPE_IS(Instance) + MP_TYPE_IS(PerObject) + MP_TYPE_IS(PerMaterial) + MP_TYPE_IS(PerFrame) MP_TYPE_IS(Global) #undef MP_TYPE_IS diff --git a/inc/hgl/graph/VKShaderResource.h b/inc/hgl/graph/VKShaderResource.h index 1ae27a6e..446bca0c 100644 --- a/inc/hgl/graph/VKShaderResource.h +++ b/inc/hgl/graph/VKShaderResource.h @@ -15,7 +15,6 @@ struct ShaderStage VertexAttribType type; ///<成份数量(如vec4中的4) - bool global; ///<是否全局数据 bool dynamic; ///<是否动态数据 };//struct ShaderStage diff --git a/res b/res index 2f972741..008dbd53 160000 --- a/res +++ b/res @@ -1 +1 @@ -Subproject commit 2f9727410cbe53f9867c544d8b6bd9f8ecdb97bf +Subproject commit 008dbd5353e6afe7add287333cbda1b5c5fffdec diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 19e7f167..82a964b4 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -151,7 +151,7 @@ namespace hgl //为所有的材质绑定 for(Material *mtl:material_sets) { - MaterialParameters *mp=mtl->GetMP(DescriptorSetsType::Primitive); + MaterialParameters *mp=mtl->GetMP(DescriptorSetsType::PerObject); if(mp) { @@ -219,7 +219,7 @@ namespace hgl MaterialParameters *mp; for(int i=(int)DescriptorSetsType::BEGIN_RANGE; - i<(int)DescriptorSetsType::Primitive; + i<(int)DescriptorSetsType::PerObject; i++) { mp=ri->GetMP((DescriptorSetsType)i); @@ -242,13 +242,13 @@ namespace hgl } { - mp=ri->GetMP(DescriptorSetsType::Primitive); + mp=ri->GetMP(DescriptorSetsType::PerObject); if(mp) { ds_list[ds_count]=mp->GetVkDescriptorSet(); ++ds_count; - + cmd_buf->BindDescriptorSets(ri->GetPipelineLayout(),first_set,ds_list,ds_count,&ubo_offset,1); } else diff --git a/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp b/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp index 89ed05bd..0310a9ba 100644 --- a/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp +++ b/src/SceneGraph/Vulkan/VKCommandBufferRender.cpp @@ -123,7 +123,7 @@ bool RenderCmdBuffer::BindDescriptorSets(Renderable *ri) ds[count]=mp->GetVkDescriptorSet(); ++count; - if((DescriptorSetsType)i==DescriptorSetsType::Primitive) + if((DescriptorSetsType)i==DescriptorSetsType::PerObject) { dynamic_count=mp->GetCount(); diff --git a/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp b/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp index ea2342a7..3d904bbe 100644 --- a/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp @@ -108,9 +108,19 @@ Material *GPUDevice::CreateMaterial(const UTF8String &mtl_name,ShaderModuleMap * CreateShaderStageList(data->shader_stage_list,shader_maps); data->pipeline_layout_data=pld; - data->mp.m=CreateMP(mds,pld,DescriptorSetsType::Material ); - data->mp.r=CreateMP(mds,pld,DescriptorSetsType::Primitive ); - data->mp.g=CreateMP(mds,pld,DescriptorSetsType::Global ); + + if(mds) + { + ENUM_CLASS_FOR(DescriptorSetsType,int,dst) + { + if(mds->hasSet((DescriptorSetsType)dst)) + data->mp_array[dst]=CreateMP(mds,pld,(DescriptorSetsType)dst); + else + data->mp_array[dst]=nullptr; + } + } + else + hgl_zero(data->mp_array); return(new Material(data)); } diff --git a/src/SceneGraph/Vulkan/VKMaterial.cpp b/src/SceneGraph/Vulkan/VKMaterial.cpp index 033cb0a5..9fdd4ee2 100644 --- a/src/SceneGraph/Vulkan/VKMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKMaterial.cpp @@ -5,9 +5,9 @@ VK_NAMESPACE_BEGIN MaterialData::~MaterialData() { - SAFE_CLEAR(mp.m); - SAFE_CLEAR(mp.r); - SAFE_CLEAR(mp.g); + for(int i=0;ipipeline_layout_data->pipeline_layout; } + +const bool Material::hasSet(const DescriptorSetsType &dst)const +{ + return data->mds->hasSet(dst); +} VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp b/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp index 91bb74e9..9daceca3 100644 --- a/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp +++ b/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp @@ -23,8 +23,8 @@ MaterialDescriptorSets::MaterialDescriptorSets(const UTF8String &name,ShaderDesc { ENUM_CLASS_FOR(DescriptorSetsType,int,i) { - sds[i].bindingCount=0; - sds[i].pBindings=nullptr; + dsl_ci[i].bindingCount=0; + dsl_ci[i].pBindings=nullptr; } { @@ -39,7 +39,9 @@ MaterialDescriptorSets::MaterialDescriptorSets(const UTF8String &name,ShaderDesc sd_by_name.Add(sp->name,sp); binding_map[size_t(sp->desc_type)].Add(sp->name,sp->binding); - ++sds[size_t(sp->set_type)].bindingCount; + ++dsl_ci[size_t(sp->set_type)].bindingCount; + + descriptor_list_by_set_type[size_t(sp->set_type)].Add(sp); ++sp; } @@ -49,10 +51,10 @@ MaterialDescriptorSets::MaterialDescriptorSets(const UTF8String &name,ShaderDesc { ENUM_CLASS_FOR(DescriptorSetsType,int,i) - if(sds[i].bindingCount>0) + if(dsl_ci[i].bindingCount>0) { - sds[i].pBindings=new VkDescriptorSetLayoutBinding[sds[i].bindingCount]; - sds_ptr[i]=(VkDescriptorSetLayoutBinding *)sds[i].pBindings; + dsl_ci[i].pBindings=new VkDescriptorSetLayoutBinding[dsl_ci[i].bindingCount]; + sds_ptr[i]=(VkDescriptorSetLayoutBinding *)dsl_ci[i].pBindings; } } @@ -94,8 +96,8 @@ MaterialDescriptorSets::MaterialDescriptorSets(const UTF8String &name,ShaderDesc MaterialDescriptorSets::~MaterialDescriptorSets() { ENUM_CLASS_FOR(DescriptorSetsType,int,i) - if(sds[i].bindingCount) - delete[] sds[i].pBindings; + if(dsl_ci[i].bindingCount) + delete[] dsl_ci[i].pBindings; delete[] sd_list; //"delete[] nullptr" isn't bug. } diff --git a/src/SceneGraph/Vulkan/VKMaterialInstance.cpp b/src/SceneGraph/Vulkan/VKMaterialInstance.cpp index 269fcb27..dd9cb9ca 100644 --- a/src/SceneGraph/Vulkan/VKMaterialInstance.cpp +++ b/src/SceneGraph/Vulkan/VKMaterialInstance.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include @@ -15,72 +14,54 @@ MaterialInstance *GPUDevice::CreateMI(Material *mtl,const VILConfig *vil_cfg) if(!vil)return(nullptr); - MaterialParameters *mp=CreateMP(mtl,DescriptorSetsType::Value); - - return(new MaterialInstance(mtl,vil,mp)); + return(new MaterialInstance(mtl,vil)); } -MaterialInstance::MaterialInstance(Material *mtl,VIL *v,MaterialParameters *p) +MaterialInstance::MaterialInstance(Material *mtl,VIL *v) { material=mtl; vil=v; - mp_value=p; -} - -MaterialInstance::~MaterialInstance() -{ - SAFE_CLEAR(mp_value); -} - -MaterialParameters *MaterialInstance::GetMP(const DescriptorSetsType &type) -{ - //if(type==DescriptorSetsType::Texture - // return mp_texture; - - if(type==DescriptorSetsType::Value) - return mp_value; - - return material->GetMP(type); + mp_per_mi=mtl->GetMP(DescriptorSetsType::PerMaterial); } bool MaterialInstance::BindUBO(const DescriptorSetsType &type,const AnsiString &name,DeviceBuffer *ubo,bool dynamic) { - MaterialParameters *mp_global=GetMP(type); + MaterialParameters *mp=GetMP(type); - if(!mp_global) + if(!mp) return(false); - if(!mp_global->BindUBO(name,ubo,dynamic))return(false); + if(!mp->BindUBO(name,ubo,dynamic))return(false); - mp_global->Update(); + mp->Update(); return(true); } bool MaterialInstance::BindSSBO(const DescriptorSetsType &type,const AnsiString &name,DeviceBuffer *ubo,bool dynamic) { - MaterialParameters *mp_global=GetMP(type); + MaterialParameters *mp=GetMP(type); - if(!mp_global) + if(!mp) return(false); - if(!mp_global->BindSSBO(name,ubo,dynamic))return(false); + if(!mp->BindSSBO(name,ubo,dynamic))return(false); - mp_global->Update(); + mp->Update(); return(true); } bool MaterialInstance::BindSampler(const DescriptorSetsType &type,const AnsiString &name,Texture *tex,Sampler *sampler) { - MaterialParameters *mp_global=GetMP(type); + MaterialParameters *mp=GetMP(type); - if(!mp_global) + if(!mp) return(false); - if(!mp_global->BindSampler(name,tex,sampler))return(false); + if(!mp->BindSampler(name,tex,sampler))return(false); - mp_global->Update(); + mp->Update(); return(true); } VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp b/src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp index 13c55d0b..d269efc9 100644 --- a/src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp +++ b/src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp @@ -12,7 +12,7 @@ PipelineLayoutData *GPUDevice::CreatePipelineLayoutData(const MaterialDescriptor { ENUM_CLASS_FOR(DescriptorSetsType,int,i) { - const DescriptorSetLayoutCreateInfo *dslci=mds->GetBinding((DescriptorSetsType)i); + const DescriptorSetLayoutCreateInfo *dslci=mds->GetDSLCI((DescriptorSetsType)i); if(!dslci||dslci->bindingCount<=0) continue; diff --git a/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp b/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp index 46d73bc7..6e1460b6 100644 --- a/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp @@ -40,7 +40,7 @@ const ShaderModule *RenderResource::CreateShaderModule(const OSString &filename, return sm; } -void LoadShaderDescriptor(const uint8 *data,ShaderDescriptor *sd_list,const uint count) +void LoadShaderDescriptor(const uint8 *data,ShaderDescriptor *sd_list,const uint count,const uint8 ver) { ShaderDescriptor *sd=sd_list; uint str_len; @@ -55,14 +55,23 @@ void LoadShaderDescriptor(const uint8 *data,ShaderDescriptor *sd_list,const uint data+=str_len; } + if(ver==3) + sd->set_type =(DescriptorSetsType)*data++; + else + if(ver==2) //以下是旧的,未来不用了,现仅保证能运行 + { + if(sd->name[0]=='g')sd->set_type=DescriptorSetsType::Global;else + if(sd->name[0]=='m')sd->set_type=DescriptorSetsType::PerMaterial;else + if(sd->name[0]=='r')sd->set_type=DescriptorSetsType::PerObject;else + sd->set_type=DescriptorSetsType::PerFrame; + } + sd->set =*data++; sd->binding =*data++; sd->stage_flag =*(uint32 *)data; data+=sizeof(uint32); - sd->set_type=CheckDescriptorSetsType(sd->name); - - if(sd->set_type==DescriptorSetsType::Primitive) + if(sd->set_type==DescriptorSetsType::PerObject) { if(sd->desc_type==VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;else if(sd->desc_type==VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)sd->desc_type=VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC; @@ -107,7 +116,7 @@ Material *RenderResource::CreateMaterial(const OSString &filename) const uint8 ver=*sp; ++sp; - if(ver!=2) + if(ver<2||ver>3) return(nullptr); const uint32_t shader_bits=*(uint32_t *)sp; @@ -160,7 +169,7 @@ Material *RenderResource::CreateMaterial(const OSString &filename) { ShaderDescriptor *sd_list=hgl_zero_new(count); - LoadShaderDescriptor(sp,sd_list,count); + LoadShaderDescriptor(sp,sd_list,count,ver); mds=new MaterialDescriptorSets(mtl_name,sd_list,count); } diff --git a/src/SceneGraph/Vulkan/VKShaderResource.cpp b/src/SceneGraph/Vulkan/VKShaderResource.cpp index b1f25e64..d2db232b 100644 --- a/src/SceneGraph/Vulkan/VKShaderResource.cpp +++ b/src/SceneGraph/Vulkan/VKShaderResource.cpp @@ -5,18 +5,6 @@ VK_NAMESPACE_BEGIN - const DescriptorSetsType CheckDescriptorSetsType(const char *str) - { - if(str[1]=='_') - { - if(str[0]=='m')return DescriptorSetsType::Material; - if(str[0]=='g')return DescriptorSetsType::Global; - if(str[0]=='r')return DescriptorSetsType::Primitive; - } - - return DescriptorSetsType::Value; - } - #define AccessByPointer(data,type) *(type *)data;data+=sizeof(type); namespace diff --git a/src/SceneGraph/font/TextRender.cpp b/src/SceneGraph/font/TextRender.cpp index 70c01a1c..38b8541f 100644 --- a/src/SceneGraph/font/TextRender.cpp +++ b/src/SceneGraph/font/TextRender.cpp @@ -109,7 +109,7 @@ namespace hgl } { - MaterialParameters *mp=material_instance->GetMP(DescriptorSetsType::Value); + MaterialParameters *mp=material_instance->GetMP(DescriptorSetsType::PerMaterial); if(!mp) return(false);