From 99bfd4d257404da8d9de6c09be4e2ba24da19121 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 14 Sep 2021 20:31:15 +0800 Subject: [PATCH] splited DescriptorSetLayoutCreater. --- example/Vulkan/FragCoordTest.cpp | 1 - inc/hgl/graph/VK.h | 3 +- inc/hgl/graph/VKDescriptorSets.h | 25 ++-- inc/hgl/graph/VKDevice.h | 13 ++- inc/hgl/graph/VKMaterial.h | 69 ++++++----- inc/hgl/graph/VKMaterialDescriptorSets.h | 34 +----- inc/hgl/graph/VKMaterialInstance.h | 2 +- inc/hgl/graph/VKMaterialParameters.h | 2 +- src/SceneGraph/CMakeLists.txt | 4 +- .../Vulkan/VKDescriptorSetLayoutCreater.cpp | 108 ------------------ .../Vulkan/VKDescriptorSetLayoutCreater.h | 59 ---------- src/SceneGraph/Vulkan/VKDevice.cpp | 2 +- src/SceneGraph/Vulkan/VKDeviceImage.cpp | 2 +- src/SceneGraph/Vulkan/VKDeviceMaterial.cpp | 95 +++++++++++++-- src/SceneGraph/Vulkan/VKDeviceTexture.cpp | 2 +- src/SceneGraph/Vulkan/VKMaterial.cpp | 72 ++---------- .../Vulkan/VKMaterialDescriptorSets.cpp | 4 +- src/SceneGraph/Vulkan/VKMaterialInstance.cpp | 9 +- .../Vulkan/VKPipelineLayoutData.cpp | 77 +++++++++++++ src/SceneGraph/Vulkan/VKPipelineLayoutData.h | 24 ++++ src/SceneGraph/Vulkan/VKRenderResource.cpp | 2 +- .../Vulkan/VKRenderResourceMaterial.cpp | 3 +- src/SceneGraph/Vulkan/VKRenderTarget.cpp | 4 +- .../Vulkan/VKSwapchainRenderTarget.cpp | 2 +- 24 files changed, 294 insertions(+), 324 deletions(-) delete mode 100644 src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.cpp delete mode 100644 src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.h create mode 100644 src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp create mode 100644 src/SceneGraph/Vulkan/VKPipelineLayoutData.h diff --git a/example/Vulkan/FragCoordTest.cpp b/example/Vulkan/FragCoordTest.cpp index 5be62c5e..7d284038 100644 --- a/example/Vulkan/FragCoordTest.cpp +++ b/example/Vulkan/FragCoordTest.cpp @@ -70,7 +70,6 @@ private: return(false); if(!mp_global->BindUBO("g_camera",ubo_camera_info))return(false); - if(!mp_global->BindUBO("g_frag_camera",ubo_camera_info))return(false); mp_global->Update(); } diff --git a/inc/hgl/graph/VK.h b/inc/hgl/graph/VK.h index 37010c33..6ab2e453 100644 --- a/inc/hgl/graph/VK.h +++ b/inc/hgl/graph/VK.h @@ -81,7 +81,7 @@ enum class DescriptorSetType const DescriptorSetType CheckDescriptorSetType(const char *str); -class DescriptorSetLayoutCreater; +struct PipelineLayoutData; class DescriptorSets; struct ShaderStage; @@ -95,7 +95,6 @@ class MaterialDescriptorSets; class Material; class MaterialParameters; class MaterialInstance; -class PipelineLayout; struct PipelineData; enum class InlinePipeline; class Pipeline; diff --git a/inc/hgl/graph/VKDescriptorSets.h b/inc/hgl/graph/VKDescriptorSets.h index b1c43e85..277851b7 100644 --- a/inc/hgl/graph/VKDescriptorSets.h +++ b/inc/hgl/graph/VKDescriptorSets.h @@ -9,9 +9,8 @@ class GPUBuffer; class DescriptorSets { VkDevice device; - int layout_binding_count; + int binding_count; VkDescriptorSet desc_set; -// const BindingMapping *index_by_binding; VkPipelineLayout pipeline_layout; @@ -21,30 +20,30 @@ class DescriptorSets private: - friend class DescriptorSetLayoutCreater; + friend class GPUDevice; - DescriptorSets(VkDevice dev,const int lbc,VkPipelineLayout pl,VkDescriptorSet ds)//,const BindingMapping *bi):index_by_binding(bi) + DescriptorSets(VkDevice dev,const int bc,VkPipelineLayout pl,VkDescriptorSet ds) { - device=dev; - layout_binding_count=lbc; - desc_set=ds; - pipeline_layout=pl; + device =dev; + binding_count =bc; + desc_set =ds; + pipeline_layout =pl; } public: ~DescriptorSets()=default; - const uint32_t GetCount ()const{return layout_binding_count;} + const uint32_t GetCount ()const{return binding_count;} const VkDescriptorSet GetDescriptorSet ()const{return desc_set;} const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;} void Clear(); - bool BindUBO(const int binding,const GPUBuffer *buf,bool dynamic=false); - bool BindUBO(const int binding,const GPUBuffer *buf,const VkDeviceSize offset,const VkDeviceSize range,bool dynamic=false); - bool BindSSBO(const int binding,const GPUBuffer *buf,bool dynamic=false); - bool BindSSBO(const int binding,const GPUBuffer *buf,const VkDeviceSize offset,const VkDeviceSize range,bool dynamic=false); + bool BindUBO (const int binding,const GPUBuffer *buf,bool dynamic=false); + bool BindUBO (const int binding,const GPUBuffer *buf,const VkDeviceSize offset,const VkDeviceSize range,bool dynamic=false); + bool BindSSBO (const int binding,const GPUBuffer *buf,bool dynamic=false); + bool BindSSBO (const int binding,const GPUBuffer *buf,const VkDeviceSize offset,const VkDeviceSize range,bool dynamic=false); bool BindSampler(const int binding,Texture *,Sampler *); bool BindInputAttachment(const int binding,Texture *); diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index c1ce6897..03469b20 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -139,7 +139,7 @@ public: //Buffer相关 public: //Image VkImage CreateImage (VkImageCreateInfo *); - void DestoryImage (VkImage); + void DestroyImage (VkImage); private: //texture @@ -191,7 +191,12 @@ public: // public: //shader & material - DescriptorSetLayoutCreater *CreateDescriptorSetLayoutCreater(const MaterialDescriptorSets *); + PipelineLayoutData *CreatePipelineLayoutData(const MaterialDescriptorSets *); + void Destroy(PipelineLayoutData *); + + DescriptorSets * CreateDescriptorSets(const PipelineLayoutData *,const DescriptorSetType &type)const; + MaterialParameters *CreateMP(const MaterialDescriptorSets *,const PipelineLayoutData *,const DescriptorSetType &); + MaterialParameters *CreateMP(Material *,const DescriptorSetType &); ShaderModule *CreateShaderModule(ShaderResource *); @@ -199,6 +204,8 @@ public: //shader & material Material *CreateMaterial(const UTF8String &mtl_name,const VertexShaderModule *vertex_shader_module,const ShaderModule *fragment_shader_module,MaterialDescriptorSets *); Material *CreateMaterial(const UTF8String &mtl_name,const VertexShaderModule *vertex_shader_module,const ShaderModule *geometry_shader_module,const ShaderModule *fragment_shader_module,MaterialDescriptorSets *); + MaterialInstance *CreateMI(Material *); + public: //Command Buffer 相关 RenderCmdBuffer * CreateRenderCommandBuffer(); @@ -212,7 +219,7 @@ public: //Command Buffer 相关 RenderPass * CreateRenderPass( const RenderbufferInfo *); GPUFence * CreateFence(bool); - GPUSemaphore * CreateSemaphore(); + GPUSemaphore * CreateGPUSemaphore(); public: //FrameBuffer相关 diff --git a/inc/hgl/graph/VKMaterial.h b/inc/hgl/graph/VKMaterial.h index 227b9e1f..f257fe61 100644 --- a/inc/hgl/graph/VKMaterial.h +++ b/inc/hgl/graph/VKMaterial.h @@ -7,7 +7,31 @@ #include #include VK_NAMESPACE_BEGIN -class DescriptorSetLayoutCreater; +struct MaterialData +{ + UTF8String name; + + ShaderModuleMap *shader_maps; + MaterialDescriptorSets *mds; + + VertexShaderModule *vertex_sm; + VertexAttributeBinding *vab; + + List shader_stage_list; + + PipelineLayoutData *pipeline_layout_data; + + struct + { + MaterialParameters *m,*g,*r; + }mp; + +private: + + friend class Material; + + ~MaterialData(); +};//struct MaterialData /** * 材质类
@@ -15,51 +39,44 @@ class DescriptorSetLayoutCreater; */ class Material { - UTF8String mtl_name; + MaterialData *data; - ShaderModuleMap *shader_maps; - MaterialDescriptorSets *mds; +private: - VertexShaderModule *vertex_sm; - List shader_stage_list; + friend GPUDevice; - DescriptorSetLayoutCreater *dsl_creater; - - MaterialParameters *mp_m,*mp_g,*mp_r; - - VertexAttributeBinding *vab; + MaterialData *GetMaterialData(){return data;} public: - Material(const UTF8String &name,ShaderModuleMap *smm,MaterialDescriptorSets *_mds,DescriptorSetLayoutCreater *); + Material(MaterialData *md):data(md){} ~Material(); - const UTF8String & GetName ()const{return mtl_name;} + const UTF8String & GetName ()const{return data->name;} - const VertexShaderModule * GetVertexShaderModule ()const{return vertex_sm;} + const VertexShaderModule * GetVertexShaderModule ()const{return data->vertex_sm;} - const uint32_t GetStageCount ()const{return shader_stage_list.GetCount();} - const VkPipelineShaderStageCreateInfo * GetStages ()const{return shader_stage_list.GetData();} + const uint32_t GetStageCount ()const{return data->shader_stage_list.GetCount();} + const VkPipelineShaderStageCreateInfo * GetStages ()const{return data->shader_stage_list.GetData();} + const MaterialDescriptorSets * GetDescriptorSets ()const{return data->mds;} const VkPipelineLayout GetPipelineLayout ()const; + const PipelineLayoutData * GetPipelineLayoutData ()const{return data->pipeline_layout_data;} - const VertexAttributeBinding * GetVAB ()const{return vab;} - const uint32_t GetVertexAttrCount ()const{return vab->GetVertexAttrCount();} - const VkVertexInputBindingDescription * GetVertexBindingList ()const{return vab->GetVertexBindingList();} - const VkVertexInputAttributeDescription * GetVertexAttributeList ()const{return vab->GetVertexAttributeList();} + const VertexAttributeBinding * GetVAB ()const{return data->vab;} + const uint32_t GetVertexAttrCount ()const{return data->vab->GetVertexAttrCount();} + const VkVertexInputBindingDescription * GetVertexBindingList ()const{return data->vab->GetVertexBindingList();} + const VkVertexInputAttributeDescription * GetVertexAttributeList ()const{return data->vab->GetVertexAttributeList();} public: - MaterialParameters * CreateMP (const DescriptorSetType &type)const; MaterialParameters * GetMP (const DescriptorSetType &type) { - if(type==DescriptorSetType::Material )return mp_m;else - if(type==DescriptorSetType::Renderable )return mp_r;else - if(type==DescriptorSetType::Global )return mp_g;else + if(type==DescriptorSetType::Material )return data->mp.m;else + if(type==DescriptorSetType::Renderable )return data->mp.r;else + if(type==DescriptorSetType::Global )return data->mp.g;else return(nullptr); } - - MaterialInstance * CreateInstance(); };//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 9d22d415..79284092 100644 --- a/inc/hgl/graph/VKMaterialDescriptorSets.h +++ b/inc/hgl/graph/VKMaterialDescriptorSets.h @@ -45,38 +45,14 @@ public: MaterialDescriptorSets(ShaderDescriptor *,const uint); ~MaterialDescriptorSets(); - const ShaderDescriptorList * GetDescriptorList ()const {return descriptor_list;} - ShaderDescriptorList * GetDescriptorList (VkDescriptorType desc_type) - { - if(desc_typeVK_DESCRIPTOR_TYPE_END_RANGE)return nullptr; - - return descriptor_list+desc_type; - } - - //ShaderDescriptorList &GetUBO (){return descriptor_list[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER];} - //ShaderDescriptorList &GetSSBO (){return descriptor_list[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER];} - //ShaderDescriptorList &GetUBODynamic (){return descriptor_list[VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC];} - //ShaderDescriptorList &GetSSBODynamic(){return descriptor_list[VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC];} - //ShaderDescriptorList &GetSampler (){return descriptor_list[VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER];} - - const int GetBinding(const VkDescriptorType &desc_type,const AnsiString &name)const; - const int GetUBO(const AnsiString &name)const{return GetBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,name);} - const int GetSSBO(const AnsiString &name)const{return GetBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,name);} - const int GetSampler(const AnsiString &name)const{return GetBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,name);} + const int GetUBO (const AnsiString &name)const{return GetBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, name);} + const int GetSSBO (const AnsiString &name)const{return GetBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, name);} + const int GetSampler(const AnsiString &name)const{return GetBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, name);} - const int *GetBindingList(const VkDescriptorType &desc_type)const - { - if(desc_typeVK_DESCRIPTOR_TYPE_END_RANGE)return nullptr; - - return binding_list[(size_t)desc_type]; - } - - const int GetSetBindingCount(const DescriptorSetType &type)const{return sds[(size_t)type].count;} - const VkDescriptorSetLayoutBinding *GetSetBindingList(const DescriptorSetType &type)const{return sds[(size_t)type].binding_list;} + const int GetBindingCount (const DescriptorSetType &type)const{return sds[(size_t)type].count;} + const VkDescriptorSetLayoutBinding *GetBindingList (const DescriptorSetType &type)const{return sds[(size_t)type].binding_list;} };//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 8827414f..19716882 100644 --- a/inc/hgl/graph/VKMaterialInstance.h +++ b/inc/hgl/graph/VKMaterialInstance.h @@ -12,7 +12,7 @@ class MaterialInstance private: - friend class Material; + friend class GPUDevice; MaterialInstance(Material *,MaterialParameters *); diff --git a/inc/hgl/graph/VKMaterialParameters.h b/inc/hgl/graph/VKMaterialParameters.h index cca2e6c5..96c6aac9 100644 --- a/inc/hgl/graph/VKMaterialParameters.h +++ b/inc/hgl/graph/VKMaterialParameters.h @@ -15,7 +15,7 @@ class MaterialParameters private: - friend class Material; + friend class GPUDevice; MaterialParameters(const MaterialDescriptorSets *,const DescriptorSetType &type,DescriptorSets *); diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index a2d6219c..7d8bfd8d 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -136,8 +136,8 @@ SOURCE_GROUP("Vulkan\\Device\\Memory" FILES ${VK_MEMORY_SOURCE}) SET(VK_DESCRIPTOR_SETS_SOURCE ${SG_INCLUDE_PATH}/VKDescriptorSets.h Vulkan/VKDescriptorSets.cpp - Vulkan/VKDescriptorSetLayoutCreater.cpp - Vulkan/VKDescriptorSetLayoutCreater.h) + Vulkan/VKPipelineLayoutData.h + Vulkan/VKPipelineLayoutData.cpp) SOURCE_GROUP("Vulkan\\Descriptor Sets" FILES ${VK_DESCRIPTOR_SETS_SOURCE}) diff --git a/src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.cpp b/src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.cpp deleted file mode 100644 index 59b70277..00000000 --- a/src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.cpp +++ /dev/null @@ -1,108 +0,0 @@ -#include"VKDescriptorSetLayoutCreater.h" -#include -#include -#include - -VK_NAMESPACE_BEGIN -DescriptorSetLayoutCreater *GPUDevice::CreateDescriptorSetLayoutCreater(const MaterialDescriptorSets *mds) -{ - return(new DescriptorSetLayoutCreater(attr->device,attr->desc_pool,mds)); -} - -DescriptorSetLayoutCreater::DescriptorSetLayoutCreater(VkDevice dev,VkDescriptorPool dp,const MaterialDescriptorSets *_mds) -{ - hgl_zero(fin_dsl); - fin_dsl_count=0; - device=dev; - pool=dp; - mds=_mds; - - ENUM_CLASS_FOR(DescriptorSetType,int,i) - layouts[i]=nullptr; -} - -DescriptorSetLayoutCreater::~DescriptorSetLayoutCreater() -{ - if(pipeline_layout) - vkDestroyPipelineLayout(device,pipeline_layout,nullptr); - - ENUM_CLASS_FOR(DescriptorSetType,int,i) - if(layouts[i]) - vkDestroyDescriptorSetLayout(device,layouts[i],nullptr); -} - -bool DescriptorSetLayoutCreater::CreatePipelineLayout() -{ - fin_dsl_count=0; - - ENUM_CLASS_FOR(DescriptorSetType,int,i) - { - const int count=mds->GetSetBindingCount((DescriptorSetType)i); - - if(count<=0) - continue; - - DescriptorSetLayoutCreateInfo descriptor_layout; - - descriptor_layout.bindingCount = count; - descriptor_layout.pBindings = mds->GetSetBindingList((DescriptorSetType)i); - - if(layouts[i]) - vkDestroyDescriptorSetLayout(device,layouts[i],nullptr); - - if(vkCreateDescriptorSetLayout(device,&descriptor_layout,nullptr,layouts+i)!=VK_SUCCESS) - return(false); - - fin_dsl[fin_dsl_count]=layouts[i]; - ++fin_dsl_count; - } - - if(fin_dsl_count<=0) - return(false); - - //VkPushConstantRange push_constant_range; - - //push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; - //push_constant_range.size = MAX_PUSH_CONSTANT_BYTES; - //push_constant_range.offset = 0; - - PipelineLayoutCreateInfo pPipelineLayoutCreateInfo; - - pPipelineLayoutCreateInfo.setLayoutCount = fin_dsl_count; - pPipelineLayoutCreateInfo.pSetLayouts = fin_dsl; - pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;//1; - pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;//&push_constant_range; - - if(vkCreatePipelineLayout(device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)!=VK_SUCCESS) - return(false); - - return(true); -} - -DescriptorSets *DescriptorSetLayoutCreater::Create(const DescriptorSetType &type)const -{ - if(!pipeline_layout) - return(nullptr); - - ENUM_CLASS_RANGE_ERROR_RETURN_NULLPTR(DescriptorSetType,type); - - const uint32_t count=mds->GetSetBindingCount(type); - - if(!count) - return(nullptr); - - DescriptorSetAllocateInfo alloc_info; - - alloc_info.descriptorPool = pool; - alloc_info.descriptorSetCount = 1; - alloc_info.pSetLayouts = layouts+size_t(type); - - VkDescriptorSet desc_set; - - if(vkAllocateDescriptorSets(device,&alloc_info,&desc_set)!=VK_SUCCESS) - return(nullptr); - - return(new DescriptorSets(device,count,pipeline_layout,desc_set));//,bm)); -} -VK_NAMESPACE_END - diff --git a/src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.h b/src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.h deleted file mode 100644 index 9fb35a29..00000000 --- a/src/SceneGraph/Vulkan/VKDescriptorSetLayoutCreater.h +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once - -#include -#include -#include -#include -VK_NAMESPACE_BEGIN -class DescriptorSets; - -/** -* 描述符合集创造器 -*/ -class DescriptorSetLayoutCreater -{ - VkDevice device; - VkDescriptorPool pool; - - const MaterialDescriptorSets *mds; - - VkDescriptorSetLayout layouts[size_t(DescriptorSetType::RANGE_SIZE)]; - VkDescriptorSetLayout fin_dsl[size_t(DescriptorSetType::RANGE_SIZE)]; - uint32_t fin_dsl_count; - - VkPipelineLayout pipeline_layout=VK_NULL_HANDLE; - -public: - - DescriptorSetLayoutCreater(VkDevice,VkDescriptorPool,const MaterialDescriptorSets *); - ~DescriptorSetLayoutCreater(); - -//以下代码不再需要,使用一个void Bind(const ShaderResource &sr,VkShaderStageFlagBits stage)即可全部替代,而且更方便,但以此为提示 -// -//#define DESC_SET_BIND_FUNC(name,vkname) void Bind##name(const uint32_t binding,VkShaderStageFlagBits stage_flag){Bind(binding,VK_DESCRIPTOR_TYPE_##vkname,stage_flag);} \ -// void Bind##name(const uint32_t *binding,const uint32_t count,VkShaderStageFlagBits stage_flag){Bind(binding,count,VK_DESCRIPTOR_TYPE_##vkname,stage_flag);} -// -// DESC_SET_BIND_FUNC(Sampler, SAMPLER); -// -// DESC_SET_BIND_FUNC(CombinedImageSampler, COMBINED_IMAGE_SAMPLER); -// DESC_SET_BIND_FUNC(SampledImage, SAMPLED_IMAGE); -// DESC_SET_BIND_FUNC(StorageImage, STORAGE_IMAGE); -// -// DESC_SET_BIND_FUNC(UTBO, UNIFORM_TEXEL_BUFFER); -// DESC_SET_BIND_FUNC(SSTBO, STORAGE_TEXEL_BUFFER); -// DESC_SET_BIND_FUNC(UBO, UNIFORM_BUFFER); -// DESC_SET_BIND_FUNC(SSBO, STORAGE_BUFFER); -// DESC_SET_BIND_FUNC(UBODynamic, UNIFORM_BUFFER_DYNAMIC); -// DESC_SET_BIND_FUNC(SSBODynamic, STORAGE_BUFFER_DYNAMIC); -// -// DESC_SET_BIND_FUNC(InputAttachment, INPUT_ATTACHMENT); -// -//#undef DESC_SET_BIND_FUNC - - bool CreatePipelineLayout(); - - const VkPipelineLayout GetPipelineLayout()const{return pipeline_layout;} - - DescriptorSets *Create(const DescriptorSetType &type)const; -};//class DescriptorSetLayoutCreater -VK_NAMESPACE_END \ No newline at end of file diff --git a/src/SceneGraph/Vulkan/VKDevice.cpp b/src/SceneGraph/Vulkan/VKDevice.cpp index d69d0d2a..3662353a 100644 --- a/src/SceneGraph/Vulkan/VKDevice.cpp +++ b/src/SceneGraph/Vulkan/VKDevice.cpp @@ -110,7 +110,7 @@ GPUFence *GPUDevice::CreateFence(bool create_signaled) return(new GPUFence(attr->device,fence)); } -GPUSemaphore *GPUDevice::CreateSemaphore() +GPUSemaphore *GPUDevice::CreateGPUSemaphore() { SemaphoreCreateInfo SemaphoreCreateInfo; diff --git a/src/SceneGraph/Vulkan/VKDeviceImage.cpp b/src/SceneGraph/Vulkan/VKDeviceImage.cpp index 9b38a298..93cf92b7 100644 --- a/src/SceneGraph/Vulkan/VKDeviceImage.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceImage.cpp @@ -16,7 +16,7 @@ VkImage GPUDevice::CreateImage(VkImageCreateInfo *ici) return image; } -void GPUDevice::DestoryImage(VkImage img) +void GPUDevice::DestroyImage(VkImage img) { if(img==VK_NULL_HANDLE)return; diff --git a/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp b/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp index 7c8011e5..2e17d2ee 100644 --- a/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceMaterial.cpp @@ -1,9 +1,76 @@ #include #include #include -#include"VKDescriptorSetLayoutCreater.h" +#include +#include +#include +#include +#include"VKPipelineLayoutData.h" VK_NAMESPACE_BEGIN +DescriptorSets *GPUDevice::CreateDescriptorSets(const PipelineLayoutData *pld,const DescriptorSetType &type)const +{ + ENUM_CLASS_RANGE_ERROR_RETURN_NULLPTR(DescriptorSetType,type); + + const uint32_t binding_count=pld->binding_count[size_t(type)]; + + if(!binding_count) + return(nullptr); + + DescriptorSetAllocateInfo alloc_info; + + alloc_info.descriptorPool = attr->desc_pool; + alloc_info.descriptorSetCount = 1; + alloc_info.pSetLayouts = pld->layouts+size_t(type); + + VkDescriptorSet desc_set; + + if(vkAllocateDescriptorSets(attr->device,&alloc_info,&desc_set)!=VK_SUCCESS) + return(nullptr); + + return(new DescriptorSets(attr->device,binding_count,pld->pipeline_layout,desc_set)); +} + +MaterialParameters *GPUDevice::CreateMP(const MaterialDescriptorSets *mds,const PipelineLayoutData *pld,const DescriptorSetType &desc_set_type) +{ + if(!mds||!pld)return(nullptr); + if(!RangeCheck(desc_set_type)) + return(nullptr); + + DescriptorSets *ds=CreateDescriptorSets(pld,desc_set_type); + + if(!ds)return(nullptr); + + return(new MaterialParameters(mds,desc_set_type,ds)); +} + +MaterialParameters *GPUDevice::CreateMP(Material *mtl,const DescriptorSetType &desc_set_type) +{ + if(!mtl)return(nullptr); + + return CreateMP(mtl->GetDescriptorSets(),mtl->GetPipelineLayoutData(),desc_set_type); +} + +void CreateShaderStageList(List &shader_stage_list,ShaderModuleMap *shader_maps) +{ + const ShaderModule *sm; + + const int shader_count=shader_maps->GetCount(); + shader_stage_list.SetCount(shader_count); + + VkPipelineShaderStageCreateInfo *p=shader_stage_list.GetData(); + + auto **itp=shader_maps->GetDataList(); + for(int i=0;iright; + hgl_cpy(p,sm->GetCreateInfo()); + + ++p; + ++itp; + } +} + Material *GPUDevice::CreateMaterial(const UTF8String &mtl_name,ShaderModuleMap *shader_maps,MaterialDescriptorSets *mds) { const int shader_count=shader_maps->GetCount(); @@ -11,21 +78,35 @@ Material *GPUDevice::CreateMaterial(const UTF8String &mtl_name,ShaderModuleMap * if(shader_count<2) return(nullptr); - const ShaderModule *sm; + const ShaderModule *vsm; - if(!shader_maps->Get(VK_SHADER_STAGE_VERTEX_BIT,sm)) + if(!shader_maps->Get(VK_SHADER_STAGE_VERTEX_BIT,vsm)) return(nullptr); - DescriptorSetLayoutCreater *dsl_creater=CreateDescriptorSetLayoutCreater(mds); + PipelineLayoutData *pld=CreatePipelineLayoutData(mds); - if(!dsl_creater->CreatePipelineLayout()) + if(!pld) { delete shader_maps; SAFE_CLEAR(mds); return(nullptr); } - return(new Material(mtl_name,shader_maps,mds,dsl_creater)); + MaterialData *data=new MaterialData; + + data->name=mtl_name; + data->shader_maps=shader_maps; + data->mds=mds; + data->vertex_sm=(VertexShaderModule *)vsm; + data->vab=data->vertex_sm->CreateVertexAttributeBinding(); + + CreateShaderStageList(data->shader_stage_list,shader_maps); + data->pipeline_layout_data=pld; + data->mp.m=CreateMP(mds,pld,DescriptorSetType::Material ); + data->mp.r=CreateMP(mds,pld,DescriptorSetType::Renderable ); + data->mp.g=CreateMP(mds,pld,DescriptorSetType::Global ); + + return(new Material(data)); } Material *GPUDevice::CreateMaterial(const UTF8String &mtl_name,const VertexShaderModule *vertex_shader_module,const ShaderModule *fragment_shader_module,MaterialDescriptorSets *mds) @@ -63,4 +144,4 @@ Material *GPUDevice::CreateMaterial(const UTF8String &mtl_name,const VertexShade return CreateMaterial(mtl_name,smm,mds); } -VK_NAMESPACE_END \ No newline at end of file +VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKDeviceTexture.cpp b/src/SceneGraph/Vulkan/VKDeviceTexture.cpp index d9fd3f4b..8c99b5f4 100644 --- a/src/SceneGraph/Vulkan/VKDeviceTexture.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceTexture.cpp @@ -159,7 +159,7 @@ void GPUDevice::Clear(TextureCreateInfo *tci) { if(!tci)return; - if(tci->image)DestoryImage(tci->image); + if(tci->image)DestroyImage(tci->image); if(tci->image_view)delete tci->image_view; if(tci->memory)delete tci->memory; diff --git a/src/SceneGraph/Vulkan/VKMaterial.cpp b/src/SceneGraph/Vulkan/VKMaterial.cpp index 6c86de73..db6ba3d3 100644 --- a/src/SceneGraph/Vulkan/VKMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKMaterial.cpp @@ -1,58 +1,13 @@ #include #include #include -#include"VKDescriptorSetLayoutCreater.h" +#include"VKPipelineLayoutData.h" VK_NAMESPACE_BEGIN -Material::Material(const UTF8String &name,ShaderModuleMap *smm,MaterialDescriptorSets *_mds,DescriptorSetLayoutCreater *dslc) +MaterialData::~MaterialData() { - mtl_name=name; - shader_maps=smm; - mds=_mds; - dsl_creater=dslc; - - const ShaderModule *sm; - - { - const int shader_count=shader_maps->GetCount(); - shader_stage_list.SetCount(shader_count); - - VkPipelineShaderStageCreateInfo *p=shader_stage_list.GetData(); - - auto **itp=shader_maps->GetDataList(); - for(int i=0;iright; - hgl_cpy(p,sm->GetCreateInfo()); - - ++p; - ++itp; - } - } - - if(smm->Get(VK_SHADER_STAGE_VERTEX_BIT,sm)) - { - vertex_sm=(VertexShaderModule *)sm; - vab=vertex_sm->CreateVertexAttributeBinding(); - } - else - { - //理论上不可能到达这里,前面CreateMaterial已经检测过了 - vertex_sm=nullptr; - vab=nullptr; - } - - mp_m=CreateMP(DescriptorSetType::Material); - mp_r=CreateMP(DescriptorSetType::Renderable); - mp_g=CreateMP(DescriptorSetType::Global); -} - -Material::~Material() -{ - SAFE_CLEAR(mp_m); - SAFE_CLEAR(mp_r); - SAFE_CLEAR(mp_g); - - delete dsl_creater; + SAFE_CLEAR(mp.m); + SAFE_CLEAR(mp.r); + SAFE_CLEAR(mp.g); if(vab) { @@ -64,17 +19,14 @@ Material::~Material() SAFE_CLEAR(mds); } +Material::~Material() +{ + delete data->pipeline_layout_data; + delete data; +} + const VkPipelineLayout Material::GetPipelineLayout()const { - return dsl_creater->GetPipelineLayout(); -} - -MaterialParameters *Material::CreateMP(const DescriptorSetType &type)const -{ - DescriptorSets *ds=dsl_creater->Create(type); - - if(!ds)return(nullptr); - - return(new MaterialParameters(mds,type,ds)); + return data->pipeline_layout_data->pipeline_layout; } VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp b/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp index 58b2be1c..200ddb23 100644 --- a/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp +++ b/src/SceneGraph/Vulkan/VKMaterialDescriptorSets.cpp @@ -19,7 +19,7 @@ MaterialDescriptorSets::MaterialDescriptorSets(ShaderDescriptor *sd,const uint c if(sd_count<=0)return; { - hgl_zero(sds); + hgl_zero(sds,size_t(DescriptorSetType::RANGE_SIZE)); { ShaderDescriptor *sp=sd_list; @@ -78,6 +78,8 @@ MaterialDescriptorSets::MaterialDescriptorSets(ShaderDescriptor *sd,const uint c { binding_list[i]=nullptr; } + + ++sdl; } } } diff --git a/src/SceneGraph/Vulkan/VKMaterialInstance.cpp b/src/SceneGraph/Vulkan/VKMaterialInstance.cpp index ca99df7d..f3b25915 100644 --- a/src/SceneGraph/Vulkan/VKMaterialInstance.cpp +++ b/src/SceneGraph/Vulkan/VKMaterialInstance.cpp @@ -1,13 +1,16 @@ +#include #include #include #include VK_NAMESPACE_BEGIN -MaterialInstance *Material::CreateInstance() +MaterialInstance *GPUDevice::CreateMI(Material *mtl) { - MaterialParameters *mp=CreateMP(DescriptorSetType::Value); + if(!mtl)return(nullptr); - return(new MaterialInstance(this,mp)); + MaterialParameters *mp=CreateMP(mtl,DescriptorSetType::Value); + + return(new MaterialInstance(mtl,mp)); } MaterialInstance::MaterialInstance(Material *mtl,MaterialParameters *p) diff --git a/src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp b/src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp new file mode 100644 index 00000000..a96d7bd0 --- /dev/null +++ b/src/SceneGraph/Vulkan/VKPipelineLayoutData.cpp @@ -0,0 +1,77 @@ +#include"VKPipelineLayoutData.h" +#include +#include +#include + +VK_NAMESPACE_BEGIN +PipelineLayoutData *GPUDevice::CreatePipelineLayoutData(const MaterialDescriptorSets *mds) +{ + PipelineLayoutData *pld=hgl_zero_new(); + + ENUM_CLASS_FOR(DescriptorSetType,int,i) + { + const int binding_count=mds->GetBindingCount((DescriptorSetType)i); + + if(binding_count<=0) + continue; + + DescriptorSetLayoutCreateInfo descriptor_layout; + + descriptor_layout.bindingCount = binding_count; + descriptor_layout.pBindings = mds->GetBindingList((DescriptorSetType)i); + + if(pld->layouts[i]) + vkDestroyDescriptorSetLayout(attr->device,pld->layouts[i],nullptr); + + if(vkCreateDescriptorSetLayout(attr->device,&descriptor_layout,nullptr,pld->layouts+i)!=VK_SUCCESS) + { + delete pld; + return(nullptr); + } + + pld->binding_count[i]=binding_count; + + pld->fin_dsl[pld->fin_dsl_count]=pld->layouts[i]; + ++pld->fin_dsl_count; + } + + if(pld->fin_dsl_count<=0) + { + delete pld; + return(nullptr); + } + + //VkPushConstantRange push_constant_range; + + //push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; + //push_constant_range.size = MAX_PUSH_CONSTANT_BYTES; + //push_constant_range.offset = 0; + + PipelineLayoutCreateInfo pPipelineLayoutCreateInfo; + + pPipelineLayoutCreateInfo.setLayoutCount = pld->fin_dsl_count; + pPipelineLayoutCreateInfo.pSetLayouts = pld->fin_dsl; + pPipelineLayoutCreateInfo.pushConstantRangeCount = 0;//1; + pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr;//&push_constant_range; + + pld->device=attr->device; + + if(vkCreatePipelineLayout(attr->device,&pPipelineLayoutCreateInfo,nullptr,&(pld->pipeline_layout))!=VK_SUCCESS) + { + delete pld; + return(nullptr); + } + + return(pld); +} + +PipelineLayoutData::~PipelineLayoutData() +{ + vkDestroyPipelineLayout(device,pipeline_layout,nullptr); + + ENUM_CLASS_FOR(DescriptorSetType,int,i) + if(layouts[i]) + vkDestroyDescriptorSetLayout(device,layouts[i],nullptr); +} +VK_NAMESPACE_END + diff --git a/src/SceneGraph/Vulkan/VKPipelineLayoutData.h b/src/SceneGraph/Vulkan/VKPipelineLayoutData.h new file mode 100644 index 00000000..5e7d21d7 --- /dev/null +++ b/src/SceneGraph/Vulkan/VKPipelineLayoutData.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include +VK_NAMESPACE_BEGIN +struct PipelineLayoutData +{ + VkDevice device; + + int binding_count[size_t(DescriptorSetType::RANGE_SIZE)]; + VkDescriptorSetLayout layouts[size_t(DescriptorSetType::RANGE_SIZE)]; + + VkDescriptorSetLayout fin_dsl[size_t(DescriptorSetType::RANGE_SIZE)]; + uint32_t fin_dsl_count; + + VkPipelineLayout pipeline_layout; + +public: + + ~PipelineLayoutData(); +};//class PipelineLayoutData +VK_NAMESPACE_END \ No newline at end of file diff --git a/src/SceneGraph/Vulkan/VKRenderResource.cpp b/src/SceneGraph/Vulkan/VKRenderResource.cpp index 64b2189d..9059fc86 100644 --- a/src/SceneGraph/Vulkan/VKRenderResource.cpp +++ b/src/SceneGraph/Vulkan/VKRenderResource.cpp @@ -54,7 +54,7 @@ MaterialInstance *RenderResource::CreateMaterialInstance(Material *mtl) { if(!mtl)return(nullptr); - MaterialInstance *mi=mtl->CreateInstance(); + MaterialInstance *mi=device->CreateMI(mtl); if(mi) Add(mi); diff --git a/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp b/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp index 2057f1bd..33945809 100644 --- a/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp +++ b/src/SceneGraph/Vulkan/VKRenderResourceMaterial.cpp @@ -145,7 +145,7 @@ Material *RenderResource::CreateMaterial(const OSString &filename) { ShaderDescriptor *sd_list=hgl_zero_new(count); - LoadShaderDescriptor(filedata,sd_list,count); + LoadShaderDescriptor(sp,sd_list,count); mds=new MaterialDescriptorSets(sd_list,count); } @@ -158,6 +158,7 @@ Material *RenderResource::CreateMaterial(const OSString &filename) } else { + SAFE_CLEAR(mds); delete smm; mtl=nullptr; } diff --git a/src/SceneGraph/Vulkan/VKRenderTarget.cpp b/src/SceneGraph/Vulkan/VKRenderTarget.cpp index b2eea712..91065763 100644 --- a/src/SceneGraph/Vulkan/VKRenderTarget.cpp +++ b/src/SceneGraph/Vulkan/VKRenderTarget.cpp @@ -18,7 +18,7 @@ RenderTarget::RenderTarget(GPUDevice *dev,Framebuffer *_fb,const uint32_t fence_ color_textures=nullptr; depth_texture=nullptr; - render_complete_semaphore=dev->CreateSemaphore(); + render_complete_semaphore=dev->CreateGPUSemaphore(); } RenderTarget::RenderTarget(GPUDevice *dev,RenderPass *_rp,Framebuffer *_fb,Texture2D **ctl,const uint32_t cc,Texture2D *dt,const uint32_t fence_count):GPUQueue(dev,dev->GetGraphicsQueue(),fence_count) @@ -48,7 +48,7 @@ RenderTarget::RenderTarget(GPUDevice *dev,RenderPass *_rp,Framebuffer *_fb,Textu } } - render_complete_semaphore=dev->CreateSemaphore(); + render_complete_semaphore=dev->CreateGPUSemaphore(); } RenderTarget::~RenderTarget() diff --git a/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp b/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp index aa759928..ea4061a9 100644 --- a/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp +++ b/src/SceneGraph/Vulkan/VKSwapchainRenderTarget.cpp @@ -35,7 +35,7 @@ SwapchainRenderTarget::SwapchainRenderTarget(GPUDevice *dev,Swapchain *sc):Rende current_frame=0; - present_complete_semaphore=dev->CreateSemaphore(); + present_complete_semaphore=dev->CreateGPUSemaphore(); } SwapchainRenderTarget::~SwapchainRenderTarget()