From 220a6798d24b702bf887901382218abd8bb50300 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 21 May 2019 12:02:57 +0800 Subject: [PATCH] =?UTF-8?q?DescriptorSets=E5=A4=9A=E6=AC=A1update=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E4=B8=BA=E4=B8=80=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/indices_rect.cpp | 6 +++- example/Vulkan/main.cpp | 6 +++- example/Vulkan/texture_rect.cpp | 21 ++++++-------- inc/hgl/graph/vulkan/VK.h | 1 + inc/hgl/graph/vulkan/VKDescriptorSets.h | 12 ++++---- src/RenderDevice/Vulkan/VKDescriptorSets.cpp | 29 +++++++++++++++++--- 6 files changed, 51 insertions(+), 24 deletions(-) diff --git a/example/Vulkan/indices_rect.cpp b/example/Vulkan/indices_rect.cpp index 5fffd090..ca271a18 100644 --- a/example/Vulkan/indices_rect.cpp +++ b/example/Vulkan/indices_rect.cpp @@ -89,7 +89,11 @@ private: if(!ubo_mvp) return(false); - return desciptor_sets->UpdateUBO(material->GetUBO("world"),*ubo_mvp); + if(!desciptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) + return(false); + + desciptor_sets->Update(); + return(true); } void InitVBO() diff --git a/example/Vulkan/main.cpp b/example/Vulkan/main.cpp index 40f3b21a..83686abd 100644 --- a/example/Vulkan/main.cpp +++ b/example/Vulkan/main.cpp @@ -90,7 +90,11 @@ private: if(!ubo_mvp) return(false); - return desciptor_sets->UpdateUBO(material->GetUBO("world"),*ubo_mvp); //material中这里可以改成不区分类型,返回的值包含类型和ID,这样descriptor_sets->Update也不再需要类型 + if(!desciptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp)) + return(false); + + desciptor_sets->Update(); + return(true); } void InitVBO() diff --git a/example/Vulkan/texture_rect.cpp b/example/Vulkan/texture_rect.cpp index 39fbe826..94a08319 100644 --- a/example/Vulkan/texture_rect.cpp +++ b/example/Vulkan/texture_rect.cpp @@ -13,8 +13,8 @@ VK_NAMESPACE_BEGIN Texture2D *LoadTGATexture(const OSString &filename,Device *device); VK_NAMESPACE_END -constexpr uint32_t SCREEN_WIDTH=512; -constexpr uint32_t SCREEN_HEIGHT=512; +constexpr uint32_t SCREEN_WIDTH=128; +constexpr uint32_t SCREEN_HEIGHT=128; struct WorldConfig { @@ -113,12 +113,10 @@ private: sampler=device->CreateSampler(&sampler_create_info); - VkDescriptorImageInfo image_info; - image_info.imageView =*texture; - image_info.imageLayout =*texture; - image_info.sampler =*sampler; + desciptor_sets->BindSampler(material->GetSampler("texture_lena"),texture,sampler); + desciptor_sets->BindUBO(material->GetUBO("world"),*ubo_mvp); + desciptor_sets->Update(); - desciptor_sets->UpdateSampler(material->GetSampler("texture_lena"),&image_info); return(true); } @@ -130,10 +128,7 @@ private: ubo_mvp=device->CreateUBO(sizeof(WorldConfig),&world); - if(!ubo_mvp) - return(false); - - return desciptor_sets->UpdateUBO(material->GetUBO("world"),*ubo_mvp); + return ubo_mvp; } void InitVBO() @@ -197,10 +192,10 @@ public: swap_chain_count=device->GetSwapChainImageCount(); - if(!InitMaterial()) + if(!InitUBO()) return(false); - if(!InitUBO()) + if(!InitMaterial()) return(false); InitVBO(); diff --git a/inc/hgl/graph/vulkan/VK.h b/inc/hgl/graph/vulkan/VK.h index 8833750b..646567b1 100644 --- a/inc/hgl/graph/vulkan/VK.h +++ b/inc/hgl/graph/vulkan/VK.h @@ -19,6 +19,7 @@ class Device; class ImageView; class Framebuffer; +class Texture; class Texture1D; class Texture1DArray; class Texture2D; diff --git a/inc/hgl/graph/vulkan/VKDescriptorSets.h b/inc/hgl/graph/vulkan/VKDescriptorSets.h index 33afa5f2..6912e2e1 100644 --- a/inc/hgl/graph/vulkan/VKDescriptorSets.h +++ b/inc/hgl/graph/vulkan/VKDescriptorSets.h @@ -15,6 +15,9 @@ class DescriptorSets VkPipelineLayout pipeline_layout; + List desc_image_info; + List write_desc_sets; + private: friend class DescriptorSetLayoutCreater; @@ -35,11 +38,10 @@ public: const VkDescriptorSet * GetDescriptorSets ()const{return &desc_set;} const VkPipelineLayout GetPipelineLayout ()const{return pipeline_layout;} - //未来统合所有的write descriptor sets,这里的update改为只是添加记录 - //最终bind到cmd时一次性写入。 - - bool UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *); - bool UpdateSampler(const uint32_t binding,const VkDescriptorImageInfo *); + void Clear(); + bool BindUBO(const uint32_t binding,const VkDescriptorBufferInfo *); + bool BindSampler(const uint32_t binding,Texture *,Sampler *); + void Update(); };//class DescriptorSets VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_DESCRIPTOR_SETS_LAYOUT_INCLUDE diff --git a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp index ddd330b2..2754f297 100644 --- a/src/RenderDevice/Vulkan/VKDescriptorSets.cpp +++ b/src/RenderDevice/Vulkan/VKDescriptorSets.cpp @@ -1,8 +1,16 @@ #include #include +#include +#include VK_NAMESPACE_BEGIN -bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info) +void DescriptorSets::Clear() +{ + write_desc_sets.ClearData(); + desc_image_info.ClearData(); +} + +bool DescriptorSets::BindUBO(const uint32_t binding,const VkDescriptorBufferInfo *buf_info) { VkWriteDescriptorSet writeDescriptorSet = {}; @@ -13,12 +21,20 @@ bool DescriptorSets::UpdateUBO(const uint32_t binding,const VkDescriptorBufferIn writeDescriptorSet.pBufferInfo = buf_info; writeDescriptorSet.dstBinding = binding; - vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr); + write_desc_sets.Add(writeDescriptorSet); return(true); } -bool DescriptorSets::UpdateSampler(const uint32_t binding,const VkDescriptorImageInfo *image_info) +bool DescriptorSets::BindSampler(const uint32_t binding,Texture *tex,Sampler *sampler) { + if(!tex||!sampler) + return(false); + + VkDescriptorImageInfo *image_info=desc_image_info.Add(); + image_info->imageView =*tex; + image_info->imageLayout =*tex; + image_info->sampler =*sampler; + VkWriteDescriptorSet writeDescriptorSet = {}; writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; @@ -28,7 +44,12 @@ bool DescriptorSets::UpdateSampler(const uint32_t binding,const VkDescriptorImag writeDescriptorSet.pImageInfo = image_info; writeDescriptorSet.dstBinding = binding; - vkUpdateDescriptorSets(*device,1,&writeDescriptorSet,0,nullptr); + write_desc_sets.Add(writeDescriptorSet); return(true); } + +void DescriptorSets::Update() +{ + vkUpdateDescriptorSets(*device,write_desc_sets.GetCount(),write_desc_sets.GetData(),0,nullptr); +} VK_NAMESPACE_END