From 62f2e4f819df647c41b31ae37cc8714a6b79186a Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 23 Oct 2024 01:11:57 +0800 Subject: [PATCH] first edition of GraphModule/GraphModuleManager --- inc/hgl/graph/VK.h | 4 ++ inc/hgl/graph/module/GraphModule.h | 49 +++++++++---- src/SceneGraph/module/GraphModule.cpp | 100 ++++++++++++++++++++++---- 3 files changed, 128 insertions(+), 25 deletions(-) diff --git a/inc/hgl/graph/VK.h b/inc/hgl/graph/VK.h index bba99d20..1d45671a 100644 --- a/inc/hgl/graph/VK.h +++ b/inc/hgl/graph/VK.h @@ -24,6 +24,10 @@ constexpr size_t VK_DESCRIPTOR_TYPE_END_RANGE=VK_DESCRIPTOR_TYPE_INPUT_ATTACHMEN constexpr size_t VK_DESCRIPTOR_TYPE_RANGE_SIZE=VK_DESCRIPTOR_TYPE_END_RANGE-VK_DESCRIPTOR_TYPE_BEGIN_RANGE+1; #endif//VK_DESCRIPTOR_TYPE_RANGE_SIZE +class GraphModule; +class RenderModule; +class GraphModuleManager; + using BindingMap =Map; using BindingMapArray =BindingMap[VK_DESCRIPTOR_TYPE_RANGE_SIZE]; diff --git a/inc/hgl/graph/module/GraphModule.h b/inc/hgl/graph/module/GraphModule.h index 6a282804..2c9fe95a 100644 --- a/inc/hgl/graph/module/GraphModule.h +++ b/inc/hgl/graph/module/GraphModule.h @@ -5,9 +5,12 @@ VK_NAMESPACE_BEGIN class RenderCmdBuffer; +class GraphModuleManager; class GraphModule { + GraphModuleManager *module_manager; + AnsiString module_name; bool module_enable; @@ -20,10 +23,14 @@ protected: public: - const AnsiString &GetModuleName()const{return module_name;} + virtual const bool IsRender()=0; ///<是否为渲染模块 - const bool IsEnable ()const noexcept{return module_enable;} - const bool IsReady ()const noexcept{return module_ready;} + GraphModuleManager *GetManager(){return module_manager;} ///<取得模块管理器 + + const AnsiString &GetModuleName()const{return module_name;} ///<取得模块名称 + + const bool IsEnable ()const noexcept{return module_enable;} ///<当前模块是否启用 + const bool IsReady ()const noexcept{return module_ready;} ///<当前模块是否准备好 public: @@ -32,19 +39,37 @@ public: GraphModule(const AnsiString &name){module_name=name;} virtual ~GraphModule()=default; - virtual bool Init(){return true;} - virtual void Execute(const double,RenderCmdBuffer *){} + virtual bool Init(){return true;} ///<初始化当前模块 +public: //回调事件 + + virtual void OnRenderTarget(RenderTarget *){} ///<设置渲染目标 + virtual void OnResize(const VkExtent2D &){} ///<窗口大小改变 + + virtual void OnPreFrame(){} ///<帧绘制前回调 + virtual void OnExecute(const double,RenderCmdBuffer *){} + virtual void OnPostFrame(){} ///<帧绘制后回调 };//class GraphModule -template GraphModule *Create() +class GraphModuleManager { - return new T(); -} + GPUDevice *device; -typedef GraphModule *(*GraphModuleCreateFunc)(); + Map graph_module_map; -bool RegistryGraphModule(const AnsiString &,GraphModuleCreateFunc); -GraphModule *GetGraphModule(const AnsiString &); +public: -VK_NAMESPACE_END \ No newline at end of file + GraphModuleManager(GPUDevice *dev){device=dev;} + ~GraphModuleManager(); + + bool Registry(const AnsiString &name,GraphModule *gm); + GraphModule *GetModule(const AnsiString &name); + +public: //事件 + + void OnResize(const VkExtent2D &); +};//class GraphModuleManager + +GraphModuleManager *GetGraphModuleManager(GPUDevice *); + +VK_NAMESPACE_END diff --git a/src/SceneGraph/module/GraphModule.cpp b/src/SceneGraph/module/GraphModule.cpp index 2e948e95..c202e211 100644 --- a/src/SceneGraph/module/GraphModule.cpp +++ b/src/SceneGraph/module/GraphModule.cpp @@ -5,35 +5,109 @@ VK_NAMESPACE_BEGIN namespace { - using GraphModuleMap=Map; + using GraphModuleManagerMap=Map; - GraphModuleMap render_module_map; -}//namespace + static GraphModuleManagerMap *graph_module_manager_map=nullptr; +} -bool RegistryGraphModule(const AnsiString &name,GraphModuleCreateFunc func) +GraphModuleManager *InitGraphModuleManager(GPUDevice *dev) { - if(name.IsEmpty()||!func) + if(!dev) + return(nullptr); + + if(graph_module_manager_map) + { + if(graph_module_manager_map->ContainsKey(dev)) + return(nullptr); + } + else + { + graph_module_manager_map=new GraphModuleManagerMap; + } + + GraphModuleManager *gmm=new GraphModuleManager(dev); + + graph_module_manager_map->Add(dev,gmm); + + return gmm; +} + +bool ClearGraphModuleManager(GPUDevice *dev) +{ + if(!dev) return(false); - if(render_module_map.ContainsKey(name)) + if(!graph_module_manager_map) return(false); - render_module_map.Add(name,func); + GraphModuleManager *gmm; + + if(!graph_module_manager_map->Get(dev,gmm)) + return(false); + graph_module_manager_map->DeleteByKey(dev); + + delete gmm; return(true); } -GraphModule *GetGraphModule(const AnsiString &name) +GraphModuleManager *GetGraphModuleManager(GPUDevice *dev) { + if(!dev) + return(nullptr); + + if(!graph_module_manager_map) + return(nullptr); + + GraphModuleManager *gmm; + + if(graph_module_manager_map->Get(dev,gmm)) + return gmm; + + return(nullptr); +} + +bool GraphModuleManager::Registry(const AnsiString &name,GraphModule *gm) +{ + if(!gm) + return(false); if(name.IsEmpty()) - return(nullptr); + return(false); - GraphModuleCreateFunc func; + if(graph_module_map.ContainsKey(name)) + return(false); - if(!render_module_map.Get(name,func)) - return(nullptr); + graph_module_map.Add(name,gm); + return(true); +} - return func(); +GraphModule *GraphModuleManager::GetModule(const AnsiString &name) +{ + GraphModule *gm; + + if(graph_module_map.Get(name,gm)) + return gm; + + return nullptr; +} + +GraphModuleManager::~GraphModuleManager() +{ + for(auto *gm:graph_module_map) + { + delete gm->value; + } +} + +void GraphModuleManager::OnResize(const VkExtent2D &extent) +{ + if(graph_module_map.IsEmpty()) + return; + + for(auto *gm:graph_module_map) + { + gm->value->OnResize(extent); + } } VK_NAMESPACE_END