diff --git a/inc/hgl/graph/RenderFramework.h b/inc/hgl/graph/RenderFramework.h new file mode 100644 index 00000000..3c32580d --- /dev/null +++ b/inc/hgl/graph/RenderFramework.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include + +VK_NAMESPACE_BEGIN + +class RenderPassManager; + +class RenderFramework:public io::WindowEvent +{ + OSString app_name; + + Window * win =nullptr; + VulkanInstance * inst =nullptr; + + GPUDevice * device =nullptr; + +protected: + + GraphModuleManager *module_manager =nullptr; + + RenderPassManager * render_pass_manager =nullptr; + RenderPass * device_render_pass =nullptr; + +public: + + Window * GetWindow (){return win;} + GPUDevice * GetDevice (){return device;} + +public: + + GraphModuleManager *GetModuleManager(){return module_manager;} + RenderPassManager * GetRenderPassManager(){return render_pass_manager;} + +public: + + RenderFramework(const OSString &); + virtual ~RenderFramework(); + + virtual bool Init(uint w,uint h); + +};//class RenderFramework + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index 65ce7ef4..99024271 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -37,7 +37,6 @@ class GPUDevice private: - DeviceRenderPassManage *render_pass_manage; RenderPass *device_render_pass; RTSwapchain *sc_rt; @@ -231,8 +230,6 @@ public: //Command Buffer 相关 public: - RenderPass * AcquireRenderPass( const RenderbufferInfo *,const uint subpass_count=2); - Fence * CreateFence(bool); Semaphore * CreateGPUSemaphore(); diff --git a/inc/hgl/graph/VKRenderPass.h b/inc/hgl/graph/VKRenderPass.h index b83a7f8b..46ae3d06 100644 --- a/inc/hgl/graph/VKRenderPass.h +++ b/inc/hgl/graph/VKRenderPass.h @@ -29,7 +29,7 @@ protected: private: - friend class DeviceRenderPassManage; + friend class RenderPassManager; RenderPass(VkDevice d,VkPipelineCache pc,VkRenderPass rp,const List &cf,VkFormat df); diff --git a/inc/hgl/graph/module/GraphModule.h b/inc/hgl/graph/module/GraphModule.h new file mode 100644 index 00000000..229965eb --- /dev/null +++ b/inc/hgl/graph/module/GraphModule.h @@ -0,0 +1,58 @@ +#pragma once + +#include +#include + +VK_NAMESPACE_BEGIN + +class GraphModule +{ + GPUDevice *device; + +public: + + GPUDevice * GetDevice () {return device;} ///<取得GPU设备 + VkDevice GetVkDevice ()const {return device->GetDevice();} ///<取得VkDevice + const GPUPhysicalDevice * GetPhysicalDevice ()const {return device->GetPhysicalDevice();} ///<取得物理设备 + GPUDeviceAttribute *GetDeviceAttribute () {return device->GetDeviceAttribute();} ///<取得设备属性 + +public: + + GraphModule(GPUDevice *dev){device=dev;} + virtual ~GraphModule()=default; + + virtual const size_t GetTypeHash()const noexcept=0; + virtual const AnsiString &GetName()const=0; +};//class GraphModule + +template class GraphModuleInherit:public GraphModule +{ + AnsiString manager_name; + +public: + + const size_t GetTypeHash()const noexcept override + { + return typeid(T).hash_code(); + } + + const AnsiString &GetName()const override + { + return manager_name; + } + +public: + + GraphModuleInherit(GPUDevice *dev,const AnsiString &name):GraphModule(dev) + { + manager_name=name; + } + + virtual ~GraphModuleInherit()=default; +};//class GraphModuleInherit + +#define GRAPH_MODULE_CLASS(class_name) class class_name:public GraphModuleInherit + +#define GRAPH_MODULE_CONSTRUCT(class_name) class_name::class_name(GPUDevice *dev):GraphModuleInherit(dev,#class_name) + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/module/GraphModuleManager.h b/inc/hgl/graph/module/GraphModuleManager.h new file mode 100644 index 00000000..b06f0e71 --- /dev/null +++ b/inc/hgl/graph/module/GraphModuleManager.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include + +VK_NAMESPACE_BEGIN + +class GraphModule; + +class GraphModuleManager +{ + GPUDevice *device; + +protected: + + List module_list; + Map module_map; + +public: + + GraphModuleManager(GPUDevice *dev){device=dev;} + + virtual ~GraphModuleManager(); + +public: + + GPUDevice * GetDevice() {return device;} ///<取得GPU设备 + +public: + + GraphModule * GetModule(const size_t type_hash) {return GetObjectFromList(module_map,type_hash);} ///<取得指定类型的模块 + template + T * GetModule() {return(GetModule(typeid(T).hash_code()));} ///<取得指定类型的模块 + + bool ConatainsModule(const size_t &type_hash)const {return module_map.ContainsKey(type_hash);} ///<确认是否包含指定类型的模块 + + bool Registry(GraphModule *); ///<注册一个模块 + bool Unregistry(GraphModule *); ///<注销一个模块 +};//class GraphModuleManager + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/module/RenderModule.h b/inc/hgl/graph/module/RenderModule.h new file mode 100644 index 00000000..9af9365d --- /dev/null +++ b/inc/hgl/graph/module/RenderModule.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include + +VK_NAMESPACE_BEGIN + +/** +* 渲染模块基类 +*/ +class RenderModule:public GraphModule +{ + VkExtent2D current_extent; + +public: + + NO_COPY_NO_MOVE(RenderModule) + + using GraphModule::GraphModule; + virtual ~RenderModule()=default; + + virtual void OnResize(const VkExtent2D &ext){current_extent=ext;} ///<窗口大小改变 + + virtual void OnFrameRender(const double,RenderCmdBuffer *)=0; ///<帧绘制回调 +};//class RenderModule + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/VKDeviceRenderPassManage.h b/inc/hgl/graph/module/RenderPassManager.h similarity index 78% rename from inc/hgl/graph/VKDeviceRenderPassManage.h rename to inc/hgl/graph/module/RenderPassManager.h index d6249582..608a159a 100644 --- a/inc/hgl/graph/VKDeviceRenderPassManage.h +++ b/inc/hgl/graph/module/RenderPassManager.h @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -12,9 +12,8 @@ inline util::Hash *CreateRenderPassHash() return util::CreateHash(util::HASH::xxH3_128); } -class DeviceRenderPassManage +class RenderPassManager:public GraphModuleInherit { - VkDevice device; VkPipelineCache pipeline_cache; util::Hash *hash; @@ -23,10 +22,8 @@ class DeviceRenderPassManage private: - friend class GPUDevice; - - DeviceRenderPassManage(VkDevice,VkPipelineCache); - ~DeviceRenderPassManage(); + RenderPassManager(GPUDevice *); + ~RenderPassManager(); private: @@ -35,6 +32,9 @@ private: const List &dependency, const RenderbufferInfo *); +public: + RenderPass * AcquireRenderPass( const RenderbufferInfo *,const uint subpass_count=2); -};//class DeviceRenderPassManage +};//class RenderPassManager + VK_NAMESPACE_END diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 8c4412c7..36c9e073 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -41,6 +41,26 @@ SET(LIGHT_FILES ${SG_INCLUDE_PATH}/Light.h source_group("Light" FILES ${LIGHT_FILES}) +SET(SGM_HEADER_PATH ${SG_INCLUDE_PATH}/module) +SET(SGM_SOURCE_PATH module) + +SET(GRAPH_MODULE_FILES ${SGM_HEADER_PATH}/GraphModule.h + ${SGM_HEADER_PATH}/GraphModuleManager.h + ${SGM_HEADER_PATH}/RenderPassManager.h + + ${SGM_HEADER_PATH}/RenderModule.h + + ${SGM_SOURCE_PATH}/GraphModuleManager.cpp + ${SGM_SOURCE_PATH}/RenderPassManager.cpp +) + +source_group("Framework\\Module" FILES ${GRAPH_MODULE_FILES}) + +SET(GRAPH_FRAMEWORK_FILES ${SG_INCLUDE_PATH}/RenderFramework.h + RenderFramework.cpp) + +source_group("Framework" FILES ${GRAPH_FRAMEWORK_FILES}) + SET(SCENE_GRAPH_HEADER ${SG_INCLUDE_PATH}/SceneManager.h ${SG_INCLUDE_PATH}/SceneNodeAttributes.h ${SG_INCLUDE_PATH}/SceneNode.h @@ -164,7 +184,6 @@ SET(VK_DEVICE_SOURCE Vulkan/VKDeviceMemory.cpp Vulkan/VKDeviceFramebuffer.cpp Vulkan/VKDeviceSwapchain.cpp Vulkan/VKDeviceRenderPass.cpp - Vulkan/VKDeviceRenderPassManage.cpp Vulkan/VKDeviceRenderTarget.cpp) SET(VK_PHYSICAL_DEVICE_SOURCE ${SG_INCLUDE_PATH}/VKPhysicalDevice.h @@ -315,6 +334,9 @@ add_cm_library(ULRE.SceneGraph "ULRE" ${SCENE_GRAPH_HEADER} ${GEOMETRY_FILES} ${LIGHT_FILES} + ${GRAPH_MODULE_FILES} + ${GRAPH_FRAMEWORK_FILES} + ${SG_TEXTURE_SOURCE} ${TILE_SOURCE} ${SG_VDM_SOURCE} diff --git a/src/SceneGraph/RenderFramework.cpp b/src/SceneGraph/RenderFramework.cpp new file mode 100644 index 00000000..df1b6b66 --- /dev/null +++ b/src/SceneGraph/RenderFramework.cpp @@ -0,0 +1,45 @@ +#include + +VK_NAMESPACE_BEGIN + +bool InitShaderCompiler(); +void CloseShaderCompiler(); + +namespace +{ + static int RENDER_FRAMEWORK_COUNT=0; + +}//namespace + +RenderFramework::RenderFramework(const OSString &an) +{ + app_name=an; + + +} + +RenderFramework::~RenderFramework() +{ + --RENDER_FRAMEWORK_COUNT; + + if(RENDER_FRAMEWORK_COUNT==0) + { + CloseShaderCompiler(); + } +} + +bool RenderFramework::Init(uint w,uint h) +{ + if(RENDER_FRAMEWORK_COUNT==0) + { + if(!InitShaderCompiler()) + return(false); + + logger::InitLogger(app_name); + } + + ++RENDER_FRAMEWORK_COUNT; + + +} +VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKDeviceRenderPass.cpp b/src/SceneGraph/Vulkan/VKDeviceRenderPass.cpp index 6d7f930f..89693d84 100644 --- a/src/SceneGraph/Vulkan/VKDeviceRenderPass.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceRenderPass.cpp @@ -1,12 +1,12 @@ #include #include #include -#include +#include VK_NAMESPACE_BEGIN void GPUDevice::InitRenderPassManage() { - render_pass_manage=new DeviceRenderPassManage(attr->device,attr->pipeline_cache); + render_pass_manage=new RenderPassManager(this); SwapchainRenderbufferInfo rbi(attr->surface_format.format,attr->physical_device->GetDepthFormat()); @@ -22,17 +22,4 @@ void GPUDevice::ClearRenderPassManage() { SAFE_CLEAR(render_pass_manage); } - -RenderPass *GPUDevice::AcquireRenderPass(const RenderbufferInfo *rbi,const uint subpass_count) -{ - for(const VkFormat &fmt:rbi->GetColorFormatList()) - if(!attr->physical_device->IsColorAttachmentOptimal(fmt)) - return(nullptr); - - if(rbi->HasDepthOrStencil()) - if(!attr->physical_device->IsDepthAttachmentOptimal(rbi->GetDepthFormat())) - return(nullptr); - - return render_pass_manage->AcquireRenderPass(rbi,subpass_count); -} VK_NAMESPACE_END diff --git a/src/SceneGraph/module/GraphModuleManager.cpp b/src/SceneGraph/module/GraphModuleManager.cpp new file mode 100644 index 00000000..53e15e94 --- /dev/null +++ b/src/SceneGraph/module/GraphModuleManager.cpp @@ -0,0 +1,53 @@ +#include +#include + +VK_NAMESPACE_BEGIN + +bool GraphModuleManager::Registry(GraphModule *gm) +{ + if(!gm) + return(false); + + const size_t type_hash=gm->GetTypeHash(); + + if(module_map.ContainsKey(type_hash)) + return(false); + + module_list.Add(gm); + module_map.Add(type_hash,gm); + + return(true); +} + +bool GraphModuleManager::Unregistry(GraphModule *gm) +{ + if(!gm) + return(false); + + const size_t type_hash=gm->GetTypeHash(); + + if(!module_map.ContainsKey(type_hash)) + return(false); + + if(module_list.DeleteByValue(gm)<0) + return(false); + + delete gm; + return(true); +} + +GraphModuleManager::~GraphModuleManager() +{ + GraphModule **gm=module_list.last(); + GraphModule **begin=module_list.begin(); + + while(gm>=begin) + { + delete *gm; + --gm; + } + + module_list.Clear(); +} + +VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKDeviceRenderPassManage.cpp b/src/SceneGraph/module/RenderPassManager.cpp similarity index 88% rename from src/SceneGraph/Vulkan/VKDeviceRenderPassManage.cpp rename to src/SceneGraph/module/RenderPassManager.cpp index 4980daa5..f88664dd 100644 --- a/src/SceneGraph/Vulkan/VKDeviceRenderPassManage.cpp +++ b/src/SceneGraph/module/RenderPassManager.cpp @@ -1,4 +1,4 @@ -#include +#include #include VK_NAMESPACE_BEGIN @@ -74,9 +74,9 @@ inline void CreateInputAttachmentReference(VkAttachmentReference *ref_list, uint bool CreateAttachmentDescription(List &desc_list,const RenderbufferInfo *rbi) { - const uint color_count=rbi->GetColorCount(); + const uint image_count=rbi->GetColorCount(); - const uint count=(rbi->HasDepthOrStencil())?color_count+1:color_count; + const uint count=(rbi->HasDepthOrStencil())?image_count+1:image_count; desc_list.SetCount(count); @@ -97,7 +97,7 @@ bool CreateAttachmentDescription(List &desc_list,const desc=desc_list.GetData(); const VkFormat *cf=rbi->GetColorFormat(); - for(uint i=0;ifinalLayout = rbi->GetColorLayout(); desc->format = *cf; @@ -184,15 +184,14 @@ bool CreateDepthAttachment( List &ref_list,Listpipeline_cache; + hash=CreateRenderPassHash(); } -DeviceRenderPassManage::~DeviceRenderPassManage() +RenderPassManager::~RenderPassManager() { SAFE_CLEAR(hash); @@ -250,7 +249,7 @@ namespace // } void HashRenderPass(RenderPassHASHCode *code,const RenderbufferInfo *rbi,const uint8 subpass_count) - { + { util::Hash *hash=CreateRenderPassHash(); hash->Init(); @@ -265,10 +264,10 @@ namespace } } -RenderPass *DeviceRenderPassManage::CreateRenderPass( const List &desc_list, - const List &subpass, - const List &dependency, - const RenderbufferInfo *rbi) +RenderPass *RenderPassManager::CreateRenderPass(const List &desc_list, + const List &subpass, + const List &dependency, + const RenderbufferInfo *rbi) { const VkFormat depth_format=rbi->GetDepthFormat(); @@ -285,14 +284,26 @@ RenderPass *DeviceRenderPassManage::CreateRenderPass( const ListGetColorFormatList(),depth_format)); + return(new RenderPass(GetVkDevice(),pipeline_cache,render_pass,rbi->GetColorFormatList(),depth_format)); } -RenderPass *DeviceRenderPassManage::AcquireRenderPass(const RenderbufferInfo *rbi,const uint subpass_count) +RenderPass *RenderPassManager::AcquireRenderPass(const RenderbufferInfo *rbi,const uint subpass_count) { + { + const auto *phy_dev=GetPhysicalDevice(); + + for(const VkFormat &fmt:rbi->GetColorFormatList()) + if(!phy_dev->IsColorAttachmentOptimal(fmt)) + return(nullptr); + + if(rbi->HasDepthOrStencil()) + if(!phy_dev->IsDepthAttachmentOptimal(rbi->GetDepthFormat())) + return(nullptr); + } + RenderPassHASHCode hash; RenderPass *rp=nullptr;