From 05e96c58ba79e6377076f2a11b63ed8fd0e68cc9 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Tue, 28 Mar 2023 21:52:08 +0800 Subject: [PATCH] added VKUBODynamic.h and support from VKArrayBuffer.h --- CMSceneGraph | 2 +- example/Vulkan/second_triangle.cpp | 2 +- inc/hgl/graph/RenderList2D.h | 4 ++ inc/hgl/graph/VKArrayBuffer.h | 29 +++++++++- inc/hgl/graph/VKMaterialParameters.h | 5 -- inc/hgl/graph/VKUBODynamic.h | 80 ++++++++++++++++++++++++++++ src/SceneGraph/CMakeLists.txt | 1 + src/SceneGraph/RenderList2D.cpp | 79 +++++++++++++++++++++------ 8 files changed, 178 insertions(+), 24 deletions(-) create mode 100644 inc/hgl/graph/VKUBODynamic.h diff --git a/CMSceneGraph b/CMSceneGraph index eaa42e6c..fd78d7e1 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit eaa42e6c967b605c78e959cd5db6d10e3f443205 +Subproject commit fd78d7e14b95e01e37eb5e631cbd6967e284fb95 diff --git a/example/Vulkan/second_triangle.cpp b/example/Vulkan/second_triangle.cpp index 5ecb8c35..71389fc6 100644 --- a/example/Vulkan/second_triangle.cpp +++ b/example/Vulkan/second_triangle.cpp @@ -30,7 +30,7 @@ constexpr float color_data[VERTEX_COUNT][4]= {0,0,1,1} }; -#define USE_ZERO2ONE_COORD //使用左上角0,0右下角1,1的坐标系 +//#define USE_ZERO2ONE_COORD //使用左上角0,0右下角1,1的坐标系 class TestApp:public VulkanApplicationFramework { diff --git a/inc/hgl/graph/RenderList2D.h b/inc/hgl/graph/RenderList2D.h index ea1aa903..7c7d5e8d 100644 --- a/inc/hgl/graph/RenderList2D.h +++ b/inc/hgl/graph/RenderList2D.h @@ -46,8 +46,12 @@ namespace hgl virtual bool ExpendNode(SceneNode *); virtual void End(); + bool BindPerFrameDescriptor(); + bool BindPerMaterialDescriptor(); + private: + Material * last_mtl; Pipeline * last_pipeline; MaterialParameters *last_mp[DESCRIPTOR_SET_TYPE_COUNT]; uint32_t last_vbo; diff --git a/inc/hgl/graph/VKArrayBuffer.h b/inc/hgl/graph/VKArrayBuffer.h index e40bdd74..19333039 100644 --- a/inc/hgl/graph/VKArrayBuffer.h +++ b/inc/hgl/graph/VKArrayBuffer.h @@ -2,6 +2,7 @@ #define HGL_GRAPH_VULKAN_ARRAY_BUFFER_INCLUDE #include +#include namespace hgl { class Collection; @@ -29,6 +30,11 @@ namespace hgl Collection *coll; + protected: + + void * Map(const uint32 start,const uint32 count); + void Flush(const uint32 count); + public: GPUArrayBuffer(GPUDevice *dev,VkBufferUsageFlags flags,const uint il,VkDescriptorType dt); @@ -41,8 +47,27 @@ namespace hgl uint32 Alloc(const uint32 max_count); ///<预分配空间 void Clear(); - void * Map(const uint32 start,const uint32 count); - void Flush(const uint32 count); + template + bool Start(UBODynamicAccess *ubo_access,const uint32 start,const uint32 count) + { + if(!ubo_access)return(false); + + void *ptr=Map(start,count); + + if(!ptr)return(false); + + ubo_access->Start((uchar *)ptr,offset_alignment,count); + return(true); + } + + void End(UBODynamicAccess *ubo_access) + { + if(!ubo_access)return; + + Flush(ubo_access->GetCount()); + + ubo_access->Restart(); + } };//class GPUArrayBuffer }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/VKMaterialParameters.h b/inc/hgl/graph/VKMaterialParameters.h index 3b111ba4..b7f4e841 100644 --- a/inc/hgl/graph/VKMaterialParameters.h +++ b/inc/hgl/graph/VKMaterialParameters.h @@ -33,11 +33,6 @@ public: const uint32_t GetDescriptorCount ()const{return desc_manager->GetBindCount(set_type);} ///<获取总共需要绑定的描述符数量 const BindingMapArray & GetBindingMap ()const{return desc_manager->GetBindingMap(set_type);} - const uint32_t GetDynamicCount ()const //返回动态ubo/ssbo总量 - { - - } - const uint32_t GetBoundCount ()const{return descriptor_set->GetCount();} ///<获取已经绑好的数量 const bool IsReady ()const{return descriptor_set->IsReady();} ///<是否全部绑好了 diff --git a/inc/hgl/graph/VKUBODynamic.h b/inc/hgl/graph/VKUBODynamic.h new file mode 100644 index 00000000..74240e67 --- /dev/null +++ b/inc/hgl/graph/VKUBODynamic.h @@ -0,0 +1,80 @@ +#ifndef HGL_GRAPH_UBO_DYNAMIC_INCLUDE +#define HGL_GRAPH_UBO_DYNAMIC_INCLUDE + +#include +VK_NAMESPACE_BEGIN + +template class UBODynamicAccess +{ + uchar *pointer; + uchar *current; + + uint unit_size; + + uint count; + uint index; + +private: + + UBODynamicAccess() + { + Restart(); + } + + void Restart() + { + pointer=nullptr; + current=nullptr; + unit_size=0; + count=0; + index=0; + } + + void Start(uchar *buf,const uint us,const uint c) + { + current=pointer=buf; + unit_size=us; + count=c; + index=0; + } + + friend class GPUArrayBuffer; + +public: + + const uint GetCount()const{return count;} + const uint GetCurrentIndex()const{return index;} + const uint GetOffsetBytes()const{return index*unit_size;} + + bool Write(uchar *src) + { + if(!src)return(false); + if(index>=count)return(false); + + memcpy(current,src,sizeof(T)); + current+=unit_size; + ++index; + + return(true); + } + + bool Write(uchar *src,const uint c) + { + if(!src)return(false); + if(c<=0)return(false); + if(index+c>count)return(false); + + for(uint i=0;i class UBODynamicAccess +VK_NAMESPACE_END +#endif//HGL_GRAPH_UBO_DYNAMIC_INCLUDE diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index e8e1ad60..4616713f 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -185,6 +185,7 @@ SET(VK_MATERIAL_SOURCE ${SG_INCLUDE_PATH}/VKMaterial.h ${SG_INCLUDE_PATH}/VKMaterialParameters.h ${SG_INCLUDE_PATH}/VKMaterialInstance.h ${SG_INCLUDE_PATH}/VKDescriptorBindingManage.h + ${SG_INCLUDE_PATH}/VKUBODynamic.h Vulkan/VKDescriptorBindingManage.cpp Vulkan/VKMaterial.cpp Vulkan/VKMaterialParameters.cpp diff --git a/src/SceneGraph/RenderList2D.cpp b/src/SceneGraph/RenderList2D.cpp index 63458df0..677ed63b 100644 --- a/src/SceneGraph/RenderList2D.cpp +++ b/src/SceneGraph/RenderList2D.cpp @@ -11,9 +11,10 @@ /** * 理论上讲,我们需要按以下顺序排序 * -* for(pipeline) -* for(material_instance) -* for(vbo) +* for(material) +* for(pipeline) +* for(material_instance) +* for(vbo) */ template<> @@ -24,6 +25,15 @@ int Comparator::compare(const RenderNode2DPointer &obj_one, hgl::graph::Renderable *ri_one=obj_one->ri; hgl::graph::Renderable *ri_two=obj_two->ri; + //比较材质 + { + off=ri_one->GetMaterial() + -ri_two->GetMaterial(); + + if(off) + return off; + } + //比较管线 { off=ri_one->GetPipeline() @@ -34,18 +44,18 @@ int Comparator::compare(const RenderNode2DPointer &obj_one, } //比较材质实例 - { - for(int i =(int)hgl::graph::DescriptorSetType::BEGIN_RANGE; - i<=(int)hgl::graph::DescriptorSetType::END_RANGE; - i++) - { - off=ri_one->GetMP((hgl::graph::DescriptorSetType)i) - -ri_two->GetMP((hgl::graph::DescriptorSetType)i); + //{ + // for(int i =(int)hgl::graph::DescriptorSetType::BEGIN_RANGE; + // i<=(int)hgl::graph::DescriptorSetType::END_RANGE; + // i++) + // { + // off=ri_one->GetMP((hgl::graph::DescriptorSetType)i) + // -ri_two->GetMP((hgl::graph::DescriptorSetType)i); - if(off) - return off; - } - } + // if(off) + // return off; + // } + //} //比较vbo+ebo { @@ -71,6 +81,7 @@ namespace hgl ubo_offset =0; ubo_align =0; + last_mtl =nullptr; last_pipeline =nullptr; hgl_zero(last_mp); last_vbo =0; @@ -180,6 +191,41 @@ namespace hgl return(true); } + bool RenderList2D::BindPerFrameDescriptor() + { + if(!cmd_buf)return(false); + + for(Material *mtl:material_sets) + { + MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerFrame); + + if(!mp)continue; + + //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) + // mp->Update(); + } + + return(true); + } + + bool RenderList2D::BindPerMaterialDescriptor() + { + //为每个材质实例,绑定它们的描述符 + if(!cmd_buf)return(false); + + for(Material *mtl:material_sets) + { + MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerMaterial); + + if(!mp)continue; + + //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) + // mp->Update(); + } + + return(true); + } + void RenderList2D::Render(Renderable *ri) { if(last_pipeline!=ri->GetPipeline()) @@ -265,11 +311,14 @@ namespace hgl cmd_buf=cb; - last_pipeline=nullptr; + last_mtl=nullptr; + last_pipeline=nullptr; hgl_zero(last_mp); last_vbo=0; ubo_offset=0; + BindPerFrameDescriptor(); + for(Renderable *ri:ri_list) Render(ri);