ULRE/inc/hgl/graph/module/GraphModule.h

204 lines
7.9 KiB
C
Raw Normal View History

2024-12-05 13:49:44 +08:00
#pragma once
2024-10-22 01:51:25 +08:00
#include<hgl/graph/VK.h>
#include<hgl/graph/VKDevice.h>
#include<hgl/type/IDName.h>
2024-10-22 01:51:25 +08:00
VK_NAMESPACE_BEGIN
class RenderCmdBuffer;
class GraphModule;
class RenderFramework;
struct GraphModulesMap
{
2024-12-18 08:26:01 +08:00
SortedSet<GraphModule *> gm_set;
Map<AnsiIDName,GraphModule *> gm_map_by_name;
Map<size_t,GraphModule *> gm_map_by_hash;
public:
bool Add(GraphModule *gm);
2024-12-18 08:26:01 +08:00
const bool IsEmpty()const
{
return gm_set.IsEmpty();
}
GraphModule *Get(const AnsiIDName &name)const
{
GraphModule *gm;
if(gm_map_by_name.Get(name,gm))
return gm;
return nullptr;
}
template<typename T>
const bool Get(T *&gm)const
{
return gm_map_by_hash.Get(GetTypeHash<T>(),gm);
}
2024-12-18 08:26:01 +08:00
const bool IsLoaded(const AnsiIDName &name)const{return gm_map_by_name.ContainsKey(name);}
template<typename T>
const bool IsLoaded()const{return gm_map_by_hash.ContainsKey(T::GetTypeHash());}
};
class GraphModuleManager
{
GPUDevice *device;
RenderFramework *framework;
GraphModulesMap graph_module_map; ///<模块映射表
public:
GraphModuleManager(RenderFramework *);
2024-12-18 08:26:01 +08:00
virtual ~GraphModuleManager();
2024-11-19 00:20:35 +08:00
GPUDevice * GetDevice ()noexcept {return device;} ///<取得GPU设备
VkDevice GetVkDevice ()const {return device->GetDevice();}
const GPUPhysicalDevice * GetPhysicalDevice ()const {return device->GetPhysicalDevice();} ///<取得物理设备
GPUDeviceAttribute *GetDeviceAttribute () {return device->GetDeviceAttribute();} ///<取得设备属性
RenderFramework * GetFramework () {return framework;} ///<取得渲染框架
/**
*
* @param create
*/
GraphModule *GetModule(const AnsiIDName &name,bool create); ///<获取指定名称的模块
/**
*
* @param create
*/
template<typename T>
T *GetModule(bool create=false){return (T *)GetModule(T::GetModuleName(),create);} ///<获取指定类型的模块
void ReleaseModule(GraphModule *); ///<释放指定模块
public: //事件
void OnResize(const VkExtent2D &);
};//class GraphModuleManager
GraphModuleManager *GetGraphModuleManager(RenderFramework *);
2024-10-22 01:51:25 +08:00
class GraphModule:public Comparator<GraphModule>
2024-10-22 01:51:25 +08:00
{
GraphModuleManager *module_manager;
AnsiIDName module_name;
2024-12-18 08:26:01 +08:00
AnsiIDName module_fullname;
2024-10-22 01:51:25 +08:00
bool module_inited;
bool module_enabled;
2024-10-22 01:51:25 +08:00
bool module_ready;
GraphModulesMap dependent_modules;
protected:
//GraphModule *GetModule(const AnsiIDName &name){return module_manager->GetModule(name,true);} ///<获取指定名称的模块
//template<typename T>
//T *GetModule(){return (T *)GetModule(T::GetModuleName());} ///<获取指定类型的模块
2024-10-22 01:51:25 +08:00
protected:
virtual void SetModuleEnabled(bool e){module_enabled=e;}
2024-10-22 01:51:25 +08:00
virtual void SetModuleReady(bool r){module_ready=r;}
public:
2024-11-15 00:11:06 +08:00
GraphModuleManager *GetManager () {return module_manager;} ///<取得模块管理器
GPUDevice * GetDevice () {return module_manager->GetDevice();} ///<取得GPU设备
VkDevice GetVkDevice ()const {return module_manager->GetVkDevice();} ///<取得VkDevice
const GPUPhysicalDevice * GetPhysicalDevice ()const {return module_manager->GetPhysicalDevice();} ///<取得物理设备
GPUDeviceAttribute *GetDeviceAttribute () {return module_manager->GetDeviceAttribute();} ///<取得设备属性
RenderFramework * GetFramework () {return module_manager->GetFramework();} ///<取得渲染框架
2024-12-18 08:26:01 +08:00
const AnsiIDName & GetName ()const {return module_name;} ///<取得模块名称(标准通用的名称比如Upscale供通用模块使用)
const AnsiIDName & GetFullName ()const {return module_fullname;} ///<取得名称(完整的私有名称比如FSR3Upscale,DLSS3Upscale)
2024-10-22 01:51:25 +08:00
virtual const bool IsPerFrame () {return false;} ///<是否每帧运行
virtual const bool IsRender () {return false;} ///<是否为渲染模块
const bool IsInited ()const {return module_inited;} ///<是否已经初始化
const bool IsEnabled ()const noexcept{return module_enabled;} ///<当前模块是否启用
const bool IsReady ()const noexcept{return module_ready;} ///<当前模块是否准备好
2024-10-22 01:51:25 +08:00
public:
NO_COPY_NO_MOVE(GraphModule)
2024-12-18 08:26:01 +08:00
GraphModule(GraphModuleManager *gmm,const AnsiIDName &name,const AnsiIDName &fullname);
virtual ~GraphModule();
2024-10-22 01:51:25 +08:00
2024-12-18 08:26:01 +08:00
virtual const size_t GetTypeHash()const=0;
virtual bool Init(GraphModulesMap *); ///<初始化当前模块
static const AnsiIDNameSet &GetDependentModules() ///<取得依赖的模块列表
{
static const AnsiIDNameSet empty;
return empty;
}
const int compare(const GraphModule &gm)const override
2024-12-05 13:49:44 +08:00
{
auto &dependent_modules_name=GetDependentModules();
return(dependent_modules_name.Contains(gm.module_name)?1:-1); //如果我依赖于他,那么我比他大
2024-12-05 13:49:44 +08:00
}
public:
2024-12-18 08:26:01 +08:00
GraphModule * GetDependentModule(const AnsiIDName &name){return dependent_modules.Get(name);} ///<获取指定名称的模块
template<typename T>
2024-12-18 08:26:01 +08:00
T * GetDependentModule(){return dependent_modules.Get<T>();} ///<获取指定类型的模块
2024-10-22 01:51:25 +08:00
public: //回调事件
virtual void OnRenderTarget(RenderTarget *){} ///<设置渲染目标
virtual void OnResize(const VkExtent2D &){} ///<窗口大小改变
virtual void OnPreFrame(){} ///<帧绘制前回调
virtual void OnPostFrame(){} ///<帧绘制后回调
2024-10-22 01:51:25 +08:00
};//class GraphModule
#define GRAPH_MODULE_CONSTRUCT(name) public:\
NO_COPY_NO_MOVE(name) \
2024-12-18 08:26:01 +08:00
static const size_t StaticHash(){return typeid(name).hash_code();} \
const size_t GetTypeHash()const override{return name::StaticHash();} \
static const AnsiIDName &GetModuleName() \
{ \
static const AnsiIDName id_name(#name); \
return id_name; \
} \
\
name(GraphModuleManager *gmm):GraphModule(gmm,GetModuleName()){}
2024-10-22 01:51:25 +08:00
#define RENDER_MODULE_CONSTRUCT(name) public:\
2024-12-18 08:26:01 +08:00
static const size_t StaticHash(){return typeid(name).hash_code();} \
const size_t GetTypeHash()const override{return name::StaticHash();} \
NO_COPY_NO_MOVE(name) \
static const AnsiIDName &GetModuleName() \
{ \
static const AnsiIDName id_name(#name); \
return id_name; \
} \
\
name(GraphModuleManager *gmm):RenderModule(gmm,GetModuleName()){}
VK_NAMESPACE_END