From 9425a2a91e7074da0650fbc3ae8cd10ff819f5fd Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 5 May 2019 21:30:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=B0=86DescriptorSetLayoutCreater=E7=B1=BB?= =?UTF-8?q?=E7=8B=AC=E7=AB=8B=E5=88=B0src=E7=9B=AE=E5=BD=95=E5=81=9A?= =?UTF-8?q?=E4=B8=BA=E7=A7=81=E6=9C=89=E4=BB=A3=E7=A0=81=E9=9A=90=E8=97=8F?= =?UTF-8?q?=202.=E5=B0=86shader=E6=95=B0=E6=8D=AE=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E6=94=BE=E5=9C=A8DescriptorSets=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/main.cpp | 11 +- inc/hgl/graph/vulkan/VKCommandBuffer.h | 4 +- inc/hgl/graph/vulkan/VKDescriptorSets.h | 59 ++------ inc/hgl/graph/vulkan/VKMaterial.h | 23 +-- inc/hgl/graph/vulkan/VKShaderModule.h | 13 +- inc/hgl/type/Map.cpp | 2 +- inc/hgl/type/Map.h | 5 +- src/Platform/Win/WinMessage.cpp | 3 +- src/Platform/Win/WinWindow.cpp | 17 ++- src/Platform/Win/WinWindow.h | 2 - src/RenderDevice/Vulkan/CMakeLists.txt | 6 +- src/RenderDevice/Vulkan/VKCommandBuffer.cpp | 8 +- .../Vulkan/VKDescriptorSetLayoutCreater.cpp | 131 ++++++++++++++++++ .../Vulkan/VKDescriptorSetLayoutCreater.h | 57 ++++++++ src/RenderDevice/Vulkan/VKDescriptorSets.cpp | 124 ++--------------- src/RenderDevice/Vulkan/VKMaterial.cpp | 101 +++++--------- src/RenderDevice/Vulkan/VKShaderModule.cpp | 8 +- .../Vulkan/VKShaderModuleManage.cpp | 4 +- 18 files changed, 300 insertions(+), 278 deletions(-) create mode 100644 src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp create mode 100644 src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.h diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 3dc62a75..768373d7 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -4,7 +4,7 @@ using namespace hgl; using namespace hgl::graph; -void SaveToTOML(const OSString &filename,const VkGraphicsPipelineCreateInfo *info); +//void SaveToTOML(const OSString &filename,const VkGraphicsPipelineCreateInfo *info); constexpr uint32_t SCREEN_WIDTH=1280; constexpr uint32_t SCREEN_HEIGHT=720; @@ -36,6 +36,7 @@ private: uint swap_chain_count=0; vulkan::Material * material =nullptr; + vulkan::DescriptorSets * desciptor_sets =nullptr; vulkan::Renderable * render_obj =nullptr; vulkan::Buffer * ubo_mvp =nullptr; @@ -55,6 +56,7 @@ public: SAFE_CLEAR(pipeline); SAFE_CLEAR(ubo_mvp); SAFE_CLEAR(render_obj); + SAFE_CLEAR(desciptor_sets); SAFE_CLEAR(material); } @@ -68,6 +70,7 @@ private: return(false); render_obj=material->CreateRenderable(); + desciptor_sets=material->CreateDescriptorSets(); return(true); } @@ -82,7 +85,7 @@ private: if(!ubo_mvp) return(false); - return material->UpdateUBO("world",*ubo_mvp); + return desciptor_sets->UpdateUBO(material->GetUBOBinding("world"),*ubo_mvp); } void InitVBO() @@ -103,7 +106,7 @@ private: pipeline_creater->CloseCullFace(); pipeline_creater->Set(PRIM_TRIANGLES); - SaveToTOML(OS_TEXT("pipeline.toml"),pipeline_creater->GetInfo()); +// SaveToTOML(OS_TEXT("pipeline.toml"),pipeline_creater->GetInfo()); pipeline=pipeline_creater->Create(); @@ -125,7 +128,7 @@ private: cmd_buf[i]->Begin(); cmd_buf[i]->BeginRenderPass(device->GetRenderPass(),device->GetFramebuffer(i)); cmd_buf[i]->Bind(pipeline); - cmd_buf[i]->Bind(material); + cmd_buf[i]->Bind(desciptor_sets); cmd_buf[i]->Bind(render_obj); cmd_buf[i]->Draw(VERTEX_COUNT); cmd_buf[i]->EndRenderPass(); diff --git a/inc/hgl/graph/vulkan/VKCommandBuffer.h b/inc/hgl/graph/vulkan/VKCommandBuffer.h index 7f0b5a50..5908082a 100644 --- a/inc/hgl/graph/vulkan/VKCommandBuffer.h +++ b/inc/hgl/graph/vulkan/VKCommandBuffer.h @@ -6,7 +6,7 @@ VK_NAMESPACE_BEGIN class RenderPass; class Framebuffer; class Pipeline; -class Material; +class DescriptorSets; class Renderable; class CommandBuffer @@ -47,7 +47,7 @@ public: bool Begin(); bool BeginRenderPass(RenderPass *rp,Framebuffer *fb); bool Bind(Pipeline *p); - bool Bind(Material *); + bool Bind(DescriptorSets *); bool Bind(Renderable *); void EndRenderPass(); bool End(); diff --git a/inc/hgl/graph/vulkan/VKDescriptorSets.h b/inc/hgl/graph/vulkan/VKDescriptorSets.h index 67e70111..babbaf06 100644 --- a/inc/hgl/graph/vulkan/VKDescriptorSets.h +++ b/inc/hgl/graph/vulkan/VKDescriptorSets.h @@ -6,11 +6,10 @@ VK_NAMESPACE_BEGIN class Device; -class DescriptorSetLayout +class DescriptorSets { Device *device; int count; - VkDescriptorSetLayout *desc_set_layout_list; VkDescriptorSet *desc_set_list; Map index_by_binding; @@ -20,11 +19,10 @@ private: friend class DescriptorSetLayoutCreater; - DescriptorSetLayout(Device *dev,const int c,VkDescriptorSetLayout *dsl_list,VkPipelineLayout pl,VkDescriptorSet *desc_set,Map &bi) + DescriptorSets(Device *dev,const int c,VkPipelineLayout pl,VkDescriptorSet *desc_set,Map &bi) { device=dev; count=c; - desc_set_layout_list=dsl_list; desc_set_list=desc_set; index_by_binding=bi; pipeline_layout=pl; @@ -32,56 +30,21 @@ private: public: - ~DescriptorSetLayout(); + ~DescriptorSets(); const uint32_t GetCount ()const{return count;} - const VkDescriptorSetLayout * GetLayouts ()const{return desc_set_layout_list;} const VkDescriptorSet * GetDescriptorSets ()const{return desc_set_list;} VkDescriptorSet GetDescriptorSet (const uint32_t binding); const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;} -};//class DescriptorSetLayout -/** -* 描述符合集创造器 -*/ -class DescriptorSetLayoutCreater -{ - Device *device; + bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info); + //bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info) + //{ + // if(name.IsEmpty()||!buf_info) + // return(false); - List layout_binding_list; - - Map index_by_binding; - -public: - - DescriptorSetLayoutCreater(Device *dev):device(dev){} - ~DescriptorSetLayoutCreater()=default; - - void Bind(const uint32_t binding,VkDescriptorType,VkShaderStageFlagBits); - void Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType type,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(UBO, UNIFORM_BUFFER); - DESC_SET_BIND_FUNC(SSBO, STORAGE_BUFFER); - - 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(UniformTexelBuffer, UNIFORM_TEXEL_BUFFER); - DESC_SET_BIND_FUNC(StorageTexelBuffer, STORAGE_TEXEL_BUFFER); - - - DESC_SET_BIND_FUNC(UBODynamic, UNIFORM_BUFFER_DYNAMIC); - DESC_SET_BIND_FUNC(SBODynamic, STORAGE_BUFFER_DYNAMIC); - - DESC_SET_BIND_FUNC(InputAttachment, INPUT_ATTACHMENT); - -#undef DESC_SET_BIND_FUNC - - DescriptorSetLayout *Create(); -};//class DescriptorSet + // return UpdateUBO(GetUBOBinding(name),buf_info); + //} +};//class DescriptorSets VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKMaterial.h b/inc/hgl/graph/vulkan/VKMaterial.h index 7456befc..3843e0e2 100644 --- a/inc/hgl/graph/vulkan/VKMaterial.h +++ b/inc/hgl/graph/vulkan/VKMaterial.h @@ -8,8 +8,8 @@ VK_NAMESPACE_BEGIN class Device; class ShaderModule; class VertexShaderModule; +class DescriptorSets; class DescriptorSetLayoutCreater; -class DescriptorSetLayout; class VertexAttributeBinding; class VertexBuffer; class Renderable; @@ -23,17 +23,18 @@ using ShaderModuleMap=hgl::Map; */ class Material { - VkDevice device; + Device *device; ShaderModuleMap *shader_maps; VertexShaderModule *vertex_sm; List *shader_stage_list; + DescriptorSetLayoutCreater *dsl_creater; - DescriptorSetLayout *desc_set_layout; + VertexAttributeBinding *vab; public: - Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List *,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VertexAttributeBinding *v); + Material(Device *dev,ShaderModuleMap *smm,List *,DescriptorSetLayoutCreater *dslc); ~Material(); const int GetUBOBinding(const UTF8String &)const; @@ -42,18 +43,8 @@ public: const uint32_t GetStageCount ()const{return shader_stage_list->GetCount();} const VkPipelineShaderStageCreateInfo * GetStages ()const{return shader_stage_list->GetData();} - const VkPipelineLayout GetPipelineLayout ()const; - const uint32_t GetDescriptorSetCount ()const; - const VkDescriptorSet * GetDescriptorSets ()const; - - bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info); - bool UpdateUBO(const UTF8String &name,const VkDescriptorBufferInfo *buf_info) - { - if(name.IsEmpty()||!buf_info) - return(false); - - return UpdateUBO(GetUBOBinding(name),buf_info); - } + const VkPipelineLayout GetPipelineLayout ()const; + DescriptorSets * CreateDescriptorSets()const; void Write(VkPipelineVertexInputStateCreateInfo &vis)const; diff --git a/inc/hgl/graph/vulkan/VKShaderModule.h b/inc/hgl/graph/vulkan/VKShaderModule.h index 9da1aaf6..e96781e8 100644 --- a/inc/hgl/graph/vulkan/VKShaderModule.h +++ b/inc/hgl/graph/vulkan/VKShaderModule.h @@ -17,8 +17,8 @@ class ShaderParse; */ class ShaderModule { + VkDevice device; int shader_id; - int ref_count; private: @@ -34,7 +34,7 @@ protected: public: - ShaderModule(int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *); + ShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *); virtual ~ShaderModule(); const int GetID()const{return shader_id;} @@ -78,16 +78,9 @@ private: Set vab_sets; -private: - - void ParseVertexInput(const ShaderParse *); - public: - VertexShaderModule(int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *parse):ShaderModule(id,pssci,parse) - { - ParseVertexInput(parse); - } + VertexShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *parse); virtual ~VertexShaderModule(); const int GetLocation (const UTF8String &)const; diff --git a/inc/hgl/type/Map.cpp b/inc/hgl/type/Map.cpp index bc963856..a112be62 100644 --- a/inc/hgl/type/Map.cpp +++ b/inc/hgl/type/Map.cpp @@ -543,7 +543,7 @@ namespace hgl } template - void _Map::EnumValue(void (*enum_func)(T)) + void _Map::EnumAllValue(void (*enum_func)(T)) { const int count=data_list.GetCount(); diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index cff4bd87..3a3350dc 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -104,9 +104,8 @@ namespace hgl void Enum(void (*enum_func)(const F &,T)); ///<枚举所有数据项 void EnumKey(void (*enum_func)(const F &)); ///<枚举所有索引 - void EnumValue(void (*enum_func)(T)); ///<枚举所有数值 - - void EnumValue(bool (*enum_func)(T)); ///<枚举所有数值 + void EnumAllValue(void (*enum_func)(T)); ///<枚举所有数值 + void EnumValue(bool (*enum_func)(T)); ///<枚举所有数值(返回true/false表示是否继续) };//class _Map template class Map:public _Map > diff --git a/src/Platform/Win/WinMessage.cpp b/src/Platform/Win/WinMessage.cpp index 4f947f67..2956e232 100644 --- a/src/Platform/Win/WinMessage.cpp +++ b/src/Platform/Win/WinMessage.cpp @@ -186,8 +186,9 @@ namespace hgl return KeyConvert[key]; } - void WMProcDestroy(WinWindow *,uint32,uint32) + void WMProcDestroy(WinWindow *win,uint32,uint32) { + win->OnClose(); PostQuitMessage(0); } diff --git a/src/Platform/Win/WinWindow.cpp b/src/Platform/Win/WinWindow.cpp index 1fb1c2b0..d2e7e7b8 100644 --- a/src/Platform/Win/WinWindow.cpp +++ b/src/Platform/Win/WinWindow.cpp @@ -122,12 +122,21 @@ namespace hgl void WinWindow::Close() { - ReleaseDC(win_hwnd, win_dc); - DestroyWindow(win_hwnd); + if(win_hwnd) + { + if(win_dc) + { + ReleaseDC(win_hwnd,win_dc); + win_dc = nullptr; + } + + DestroyWindow(win_hwnd); + + win_hwnd = nullptr; + } + UnregisterClassW(WIN_CLASS_NAME,hInstance); - win_dc = nullptr; - win_hwnd = nullptr; is_close = true; } diff --git a/src/Platform/Win/WinWindow.h b/src/Platform/Win/WinWindow.h index 05a0560d..e379d6d5 100644 --- a/src/Platform/Win/WinWindow.h +++ b/src/Platform/Win/WinWindow.h @@ -19,8 +19,6 @@ namespace hgl bool Create(); - void OnClose() override{Close();} - public: using Window::Window; diff --git a/src/RenderDevice/Vulkan/CMakeLists.txt b/src/RenderDevice/Vulkan/CMakeLists.txt index 8e17159f..e901d35c 100644 --- a/src/RenderDevice/Vulkan/CMakeLists.txt +++ b/src/RenderDevice/Vulkan/CMakeLists.txt @@ -34,6 +34,8 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp VKDeviceBuffer.cpp VKBuffer.cpp VKDescriptorSets.cpp + VKDescriptorSetLayoutCreater.cpp + VKDescriptorSetLayoutCreater.h VKRenderPass.cpp VKShaderModule.cpp VKShaderModuleManage.cpp @@ -46,11 +48,11 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp VKRenderable.cpp) #SET(RENDER_DEVICE_VULKAN_TOML_SOURCE toml/VKPipelineCreateInfo.TOML.cpp) -SET(RENDER_DEVICE_VULKAN_JSON_SOURCE json/VKPipelineCreateInfo.JSON.cpp) +#SET(RENDER_DEVICE_VULKAN_JSON_SOURCE json/VKPipelineCreateInfo.JSON.cpp) SOURCE_GROUP("Header Files" FILES ${RENDER_DEVICE_VULKAN_HEADER}) SOURCE_GROUP("Source Files" FILES ${RENDER_DEVICE_VULKAN_SOURCE}) -SOURCE_GROUP("JSON Source Files" FILES ${RENDER_DEVICE_VULKAN_JSON_SOURCE}) +#SOURCE_GROUP("JSON Source Files" FILES ${RENDER_DEVICE_VULKAN_JSON_SOURCE}) add_library(ULRE.RenderDevice.Vulkan STATIC ${RENDER_DEVICE_VULKAN_HEADER} ${RENDER_DEVICE_VULKAN_SOURCE} diff --git a/src/RenderDevice/Vulkan/VKCommandBuffer.cpp b/src/RenderDevice/Vulkan/VKCommandBuffer.cpp index 51328317..78159838 100644 --- a/src/RenderDevice/Vulkan/VKCommandBuffer.cpp +++ b/src/RenderDevice/Vulkan/VKCommandBuffer.cpp @@ -80,15 +80,15 @@ bool CommandBuffer::Bind(Pipeline *p) return(true); } -bool CommandBuffer::Bind(Material *mat) +bool CommandBuffer::Bind(DescriptorSets *dsl) { - if(!mat) + if(!dsl) return(false); - const uint32_t count=mat->GetDescriptorSetCount(); + const uint32_t count=dsl->GetCount(); if(count>0) - vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,mat->GetPipelineLayout(),0,count,mat->GetDescriptorSets(),0,nullptr); + vkCmdBindDescriptorSets(cmd_buf,VK_PIPELINE_BIND_POINT_GRAPHICS,dsl->GetPipelineLayout(),0,count,dsl->GetDescriptorSets(),0,nullptr); return(true); } diff --git a/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp b/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp new file mode 100644 index 00000000..67229b4c --- /dev/null +++ b/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.cpp @@ -0,0 +1,131 @@ +#include"VKDescriptorSetLayoutCreater.h" +#include +#include + +VK_NAMESPACE_BEGIN +namespace +{ + void DestroyDescriptorSetLayout(VkDevice device,const int count,VkDescriptorSetLayout *dsl_list) + { + if(count<=0) + return; + + for(int i=0;ibinding = *binding; + p->descriptorType = desc_type; + p->descriptorCount = 1; + p->stageFlags = stageFlags; + p->pImmutableSamplers = nullptr; + + index_by_binding.Add(*binding,i); + + ++binding; + ++p; + } +} + +bool DescriptorSetLayoutCreater::CreatePipelineLayout() +{ + const int count=layout_binding_list.GetCount(); + + if(count<=0) + return(false); + + VkDescriptorSetLayoutCreateInfo descriptor_layout = {}; + descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; + descriptor_layout.pNext = nullptr; + descriptor_layout.bindingCount = count; + descriptor_layout.pBindings = layout_binding_list.GetData(); + + dsl_list=new VkDescriptorSetLayout[count]; + + if(vkCreateDescriptorSetLayout(*device,&descriptor_layout,nullptr,dsl_list)==VK_SUCCESS) + { + VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {}; + pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + pPipelineLayoutCreateInfo.pNext = nullptr; + pPipelineLayoutCreateInfo.pushConstantRangeCount = 0; + pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr; + pPipelineLayoutCreateInfo.setLayoutCount = count; + pPipelineLayoutCreateInfo.pSetLayouts = dsl_list; + + if(vkCreatePipelineLayout(*device,&pPipelineLayoutCreateInfo,nullptr,&pipeline_layout)==VK_SUCCESS) + return(true); + } + + delete[] dsl_list; + dsl_list=nullptr; + return(false); +} + +DescriptorSets *DescriptorSetLayoutCreater::Create() +{ + if(!pipeline_layout||!dsl_list) + return(nullptr); + + const int count=layout_binding_list.GetCount(); + + if(count<=0) + return(nullptr); + + VkDescriptorSetAllocateInfo alloc_info; + alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; + alloc_info.pNext = nullptr; + alloc_info.descriptorPool = device->GetDescriptorPool(); + alloc_info.descriptorSetCount = count; + alloc_info.pSetLayouts = dsl_list; + + VkDescriptorSet *desc_set=new VkDescriptorSet[count]; + + if(vkAllocateDescriptorSets(*device,&alloc_info,desc_set)!=VK_SUCCESS) + { + delete[] desc_set; + return(nullptr); + } + + return(new DescriptorSets(device,count,pipeline_layout,desc_set,index_by_binding)); +} +VK_NAMESPACE_END \ No newline at end of file diff --git a/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.h b/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.h new file mode 100644 index 00000000..77f5aed5 --- /dev/null +++ b/src/RenderDevice/Vulkan/VKDescriptorSetLayoutCreater.h @@ -0,0 +1,57 @@ +#pragma once + +#include +#include +VK_NAMESPACE_BEGIN +class Device; +class DescriptorSets; + +/** +* 描述符合集创造器 +*/ +class DescriptorSetLayoutCreater +{ + Device *device; + + List layout_binding_list; + VkDescriptorSetLayout *dsl_list=nullptr; + Map index_by_binding; + + VkPipelineLayout pipeline_layout=nullptr; + +public: + + DescriptorSetLayoutCreater(Device *dev):device(dev){} + ~DescriptorSetLayoutCreater(); + + void Bind(const uint32_t binding,VkDescriptorType,VkShaderStageFlagBits); + void Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType type,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(UBO, UNIFORM_BUFFER); + DESC_SET_BIND_FUNC(SSBO, STORAGE_BUFFER); + + 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(UniformTexelBuffer, UNIFORM_TEXEL_BUFFER); + DESC_SET_BIND_FUNC(StorageTexelBuffer, STORAGE_TEXEL_BUFFER); + + + DESC_SET_BIND_FUNC(UBODynamic, UNIFORM_BUFFER_DYNAMIC); + DESC_SET_BIND_FUNC(SBODynamic, 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(); +};//class DescriptorSetLayoutCreater +VK_NAMESPACE_END \ No newline at end of file diff --git a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp index 439f0c40..98533376 100644 --- a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp +++ b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp @@ -2,22 +2,7 @@ #include VK_NAMESPACE_BEGIN -namespace -{ - void DestroyDescriptorSetLayout(VkDevice device,const int count,VkDescriptorSetLayout *dsl_list) - { - if(count<=0) - return; - - for(int i=0;i0) // vkFreeDescriptorSets(device->GetDevice(),device->GetDescriptorPool(),count,desc_set_list); - if(pipeline_layout) - vkDestroyPipelineLayout(*device,pipeline_layout,nullptr); - delete[] desc_set_list; - DestroyDescriptorSetLayout(*device,count,desc_set_layout_list); - delete[] desc_set_layout_list; } -void DescriptorSetLayoutCreater::Bind(const uint32_t binding,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags) +bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info) { - VkDescriptorSetLayoutBinding layout_binding = {}; - layout_binding.binding = binding; - layout_binding.descriptorType = desc_type; - layout_binding.descriptorCount = 1; - layout_binding.stageFlags = stageFlags; - layout_binding.pImmutableSamplers = nullptr; + VkDescriptorSet set=GetDescriptorSet(binding); - const int index=layout_binding_list.Add(layout_binding); + VkWriteDescriptorSet writeDescriptorSet = {}; - index_by_binding.Add(binding,index); -} + writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + writeDescriptorSet.dstSet = set; + writeDescriptorSet.descriptorCount = 1; + writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + writeDescriptorSet.pBufferInfo = buf_info; + writeDescriptorSet.dstBinding = binding; -void DescriptorSetLayoutCreater::Bind(const uint32_t *binding,const uint32_t count,VkDescriptorType desc_type,VkShaderStageFlagBits stageFlags) -{ - const uint old_count=layout_binding_list.GetCount(); - - layout_binding_list.SetCount(old_count+count); - - VkDescriptorSetLayoutBinding *p=layout_binding_list.GetData()+old_count; - - for(uint i=old_count;ibinding = *binding; - p->descriptorType = desc_type; - p->descriptorCount = 1; - p->stageFlags = stageFlags; - p->pImmutableSamplers = nullptr; - - index_by_binding.Add(*binding,i); - - ++binding; - ++p; - } -} - -DescriptorSetLayout *DescriptorSetLayoutCreater::Create() -{ - const int count=layout_binding_list.GetCount(); - - if(count<=0) - return(nullptr); - - VkDescriptorSetLayoutCreateInfo descriptor_layout = {}; - descriptor_layout.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; - descriptor_layout.pNext = nullptr; - descriptor_layout.bindingCount = count; - descriptor_layout.pBindings = layout_binding_list.GetData(); - - VkDescriptorSetLayout *dsl_list=new VkDescriptorSetLayout[count]; - - if(vkCreateDescriptorSetLayout(*device,&descriptor_layout, nullptr, dsl_list)!=VK_SUCCESS) - { - delete[] dsl_list; - return(nullptr); - } - - VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = {}; - pPipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - pPipelineLayoutCreateInfo.pNext = nullptr; - pPipelineLayoutCreateInfo.pushConstantRangeCount = 0; - pPipelineLayoutCreateInfo.pPushConstantRanges = nullptr; - pPipelineLayoutCreateInfo.setLayoutCount = count; - pPipelineLayoutCreateInfo.pSetLayouts = dsl_list; - - VkPipelineLayout pipeline_layout; - - if(vkCreatePipelineLayout(*device, &pPipelineLayoutCreateInfo, nullptr, &pipeline_layout)!=VK_SUCCESS) - { - delete[] dsl_list; - return(nullptr); - } - - VkDescriptorSetAllocateInfo alloc_info; - alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - alloc_info.pNext = nullptr; - alloc_info.descriptorPool = device->GetDescriptorPool(); - alloc_info.descriptorSetCount = count; - alloc_info.pSetLayouts = dsl_list; - - VkDescriptorSet *desc_set=new VkDescriptorSet[count]; - - if(vkAllocateDescriptorSets(*device,&alloc_info,desc_set)!=VK_SUCCESS) - { - vkDestroyPipelineLayout(*device,pipeline_layout,nullptr); - delete[] desc_set; - DestroyDescriptorSetLayout(*device,count,dsl_list); - delete[] dsl_list; - return(nullptr); - } - - return(new DescriptorSetLayout(device,count,dsl_list,pipeline_layout,desc_set,index_by_binding)); + vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr); + return(true); } VK_NAMESPACE_END diff --git a/src/RenderDevice/Vulkan/VKMaterial.cpp b/src/RenderDevice/Vulkan/VKMaterial.cpp index 3b7b818f..f90925aa 100644 --- a/src/RenderDevice/Vulkan/VKMaterial.cpp +++ b/src/RenderDevice/Vulkan/VKMaterial.cpp @@ -5,82 +5,75 @@ #include #include #include +#include"VKDescriptorSetLayoutCreater.h" VK_NAMESPACE_BEGIN Material *CreateMaterial(Device *dev,ShaderModuleMap *shader_maps) { - DescriptorSetLayoutCreater *dsl_creater=new DescriptorSetLayoutCreater(dev); - const int shader_count=shader_maps->GetCount(); + if(shader_count<2) + return(nullptr); + + const ShaderModule *sm; + + if(!shader_maps->Get(VK_SHADER_STAGE_VERTEX_BIT,sm)) + return(nullptr); + + const VertexShaderModule *vertex_sm=(VertexShaderModule *)sm; + + DescriptorSetLayoutCreater *dsl_creater=new DescriptorSetLayoutCreater(dev); List *shader_stage_list=new List; shader_stage_list->SetCount(shader_count); VkPipelineShaderStageCreateInfo *p=shader_stage_list->GetData(); - VertexShaderModule *vertex_sm; - const ShaderModule *sm; auto **itp=shader_maps->GetDataList(); for(int i=0;iright; - - if(sm->GetStage()==VK_SHADER_STAGE_VERTEX_BIT) - vertex_sm=(VertexShaderModule *)sm; - memcpy(p,sm->GetCreateInfo(),sizeof(VkPipelineShaderStageCreateInfo)); - ++p; - ++itp; { const ShaderBindingList &ubo_list=sm->GetUBOBindingList(); dsl_creater->BindUBO(ubo_list.GetData(),ubo_list.GetCount(),sm->GetStage()); } + + ++p; + ++itp; } - VertexAttributeBinding *vab=vertex_sm->CreateVertexAttributeBinding(); + dsl_creater->CreatePipelineLayout(); - DescriptorSetLayout *dsl=dsl_creater->Create(); - - if(dsl) - { - return(new Material(dev,shader_maps,vertex_sm,shader_stage_list,dsl_creater,dsl,vab)); - } - - return(nullptr); + return(new Material(dev,shader_maps,shader_stage_list,dsl_creater)); } -Material::Material(Device *dev,ShaderModuleMap *smm,VertexShaderModule *vsm,List *psci_list,DescriptorSetLayoutCreater *dslc,DescriptorSetLayout *dsl,VertexAttributeBinding *v) +Material::Material(Device *dev,ShaderModuleMap *smm,List *psci_list,DescriptorSetLayoutCreater *dslc) { - device=*dev; + device=dev; shader_maps=smm; - vertex_sm=vsm; shader_stage_list=psci_list; dsl_creater=dslc; - desc_set_layout=dsl; - vab=v; + + const ShaderModule *sm; + if(smm->Get(VK_SHADER_STAGE_VERTEX_BIT,sm)) + { + vertex_sm=(VertexShaderModule *)sm; + vab=vertex_sm->CreateVertexAttributeBinding(); + } + else + { + //理论上不可能到达这里,前面CreateMaterial已经检测过了 + vertex_sm=nullptr; + vab=nullptr; + } } Material::~Material() { - delete vab; - - delete desc_set_layout; delete dsl_creater; - - const int count=shader_stage_list->GetCount(); - - if(count>0) - { - VkPipelineShaderStageCreateInfo *ss=shader_stage_list->GetData(); - for(int i=0;imodule,nullptr); - ++ss; - } - } - + delete vab; delete shader_stage_list; delete shader_maps; } @@ -114,34 +107,12 @@ const int Material::GetVBOBinding(const UTF8String &name)const const VkPipelineLayout Material::GetPipelineLayout()const { - return desc_set_layout->GetPipelineLayout(); + return dsl_creater->GetPipelineLayout(); } -const uint32_t Material::GetDescriptorSetCount()const +DescriptorSets *Material::CreateDescriptorSets()const { - return desc_set_layout->GetCount(); -} - -const VkDescriptorSet *Material::GetDescriptorSets()const -{ - return desc_set_layout->GetDescriptorSets(); -} - -bool Material::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info) -{ - VkDescriptorSet set=desc_set_layout->GetDescriptorSet(binding); - - VkWriteDescriptorSet writeDescriptorSet = {}; - - writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - writeDescriptorSet.dstSet = set; - writeDescriptorSet.descriptorCount = 1; - writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - writeDescriptorSet.pBufferInfo = buf_info; - writeDescriptorSet.dstBinding = binding; - - vkUpdateDescriptorSets(device,1,&writeDescriptorSet,0,nullptr); - return(true); + return dsl_creater->Create(); } void Material::Write(VkPipelineVertexInputStateCreateInfo &vis)const diff --git a/src/RenderDevice/Vulkan/VKShaderModule.cpp b/src/RenderDevice/Vulkan/VKShaderModule.cpp index 646b182e..4d0dd97a 100644 --- a/src/RenderDevice/Vulkan/VKShaderModule.cpp +++ b/src/RenderDevice/Vulkan/VKShaderModule.cpp @@ -3,8 +3,9 @@ #include VK_NAMESPACE_BEGIN -ShaderModule::ShaderModule(int id,VkPipelineShaderStageCreateInfo *sci,const ShaderParse *sp) +ShaderModule::ShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *sci,const ShaderParse *sp) { + device=dev; shader_id=id; ref_count=0; @@ -15,6 +16,7 @@ ShaderModule::ShaderModule(int id,VkPipelineShaderStageCreateInfo *sci,const Sha ShaderModule::~ShaderModule() { + vkDestroyShaderModule(device,stage_create_info->module,nullptr); delete stage_create_info; } @@ -30,11 +32,11 @@ void ShaderModule::ParseUBO(const ShaderParse *parse) } } -void VertexShaderModule::ParseVertexInput(const ShaderParse *parse) +VertexShaderModule::VertexShaderModule(VkDevice dev,int id,VkPipelineShaderStageCreateInfo *pssci,const ShaderParse *parse):ShaderModule(dev,id,pssci,parse) { const auto &stage_inputs=parse->GetStageInput(); - attr_count=stage_inputs.size(); + attr_count=(uint32_t)stage_inputs.size(); binding_list=new VkVertexInputBindingDescription[attr_count]; attribute_list=new VkVertexInputAttributeDescription[attr_count]; diff --git a/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp b/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp index 6f6e3fa5..682f5f39 100644 --- a/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp +++ b/src/RenderDevice/Vulkan/VKShaderModuleManage.cpp @@ -49,9 +49,9 @@ const ShaderModule *ShaderModuleManage::CreateShader(const VkShaderStageFlagBits ShaderParse *parse=new ShaderParse(spv_data,spv_size); if(shader_stage_bit==VK_SHADER_STAGE_VERTEX_BIT) - sm=new VertexShaderModule(shader_count,shader_stage,parse); + sm=new VertexShaderModule(*device,shader_count,shader_stage,parse); else - sm=new ShaderModule(shader_count,shader_stage,parse); + sm=new ShaderModule(*device,shader_count,shader_stage,parse); delete parse; shader_list.Add(shader_count,sm);