From 59b3ec6bf3433da2f83c90f82c209bf4943c4c62 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 19 Oct 2024 13:08:05 +0800 Subject: [PATCH] preparing RenderFramework --- CMCore | 2 +- inc/hgl/graph/LightingModel.h | 73 +++++++++++ inc/hgl/graph/RenderFramework.h | 120 ++++++++++++++++++ inc/hgl/graph/RenderModule.h | 66 ++++++++++ inc/hgl/graph/component/Component.h | 39 ++++++ inc/hgl/graph/component/SceneComponent.h | 39 ++++++ inc/hgl/graph/deferred/GBufferFormat.h | 63 +++++++++ inc/hgl/graph/{ => mesh}/StaticMesh.h | 2 +- .../graph/{ => mesh}/StaticMeshLODPolicy.h | 0 inc/hgl/graph/mesh/StaticMeshNode.h | 17 +++ inc/hgl/graph/mtl/Material3DCreateConfig.h | 50 -------- src/SceneGraph/CMakeLists.txt | 40 +++++- src/SceneGraph/MaterialRenderList.cpp | 3 +- src/SceneGraph/RenderFramework.cpp | 31 +++++ src/SceneGraph/RenderModule/RenderModule.cpp | 39 ++++++ src/SceneGraph/deferred/GBufferFormat.cpp | 0 src/SceneGraph/{ => mesh}/StaticMesh.cpp | 2 +- 17 files changed, 527 insertions(+), 59 deletions(-) create mode 100644 inc/hgl/graph/LightingModel.h create mode 100644 inc/hgl/graph/RenderFramework.h create mode 100644 inc/hgl/graph/RenderModule.h create mode 100644 inc/hgl/graph/component/Component.h create mode 100644 inc/hgl/graph/component/SceneComponent.h create mode 100644 inc/hgl/graph/deferred/GBufferFormat.h rename inc/hgl/graph/{ => mesh}/StaticMesh.h (96%) rename inc/hgl/graph/{ => mesh}/StaticMeshLODPolicy.h (100%) create mode 100644 inc/hgl/graph/mesh/StaticMeshNode.h create mode 100644 src/SceneGraph/RenderFramework.cpp create mode 100644 src/SceneGraph/RenderModule/RenderModule.cpp create mode 100644 src/SceneGraph/deferred/GBufferFormat.cpp rename src/SceneGraph/{ => mesh}/StaticMesh.cpp (91%) diff --git a/CMCore b/CMCore index 09d77726..ca8517f1 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 09d777261f4249ccdba5bef44d794742f4e3237a +Subproject commit ca8517f146b08bb4df1669218579223ac7bfbcae diff --git a/inc/hgl/graph/LightingModel.h b/inc/hgl/graph/LightingModel.h new file mode 100644 index 00000000..7607525e --- /dev/null +++ b/inc/hgl/graph/LightingModel.h @@ -0,0 +1,73 @@ +#pragma once + +#include + +VK_NAMESPACE_BEGIN + +enum class LightingModel:uint8 +{ + Unlit, + + Gizmo, /// +#include +#include + +VK_NAMESPACE_BEGIN + +class RenderModule; + +/** +* 光照剔除模式 +*/ +enum class LightingCullingMode +{ + None, ///<不剔除 + + /* + * 基于Tile的剔除模式 + * 按屏幕XY坐标划分成多个Tile,再配合znear/zfar形成一个Volume。所有光源和Volume计算相交性 + */ + Tile, ///<瓦片剔除 + + /** + * 基于Tile的剔除模式的改进型 + * 同Tile方法,得出Tile后,再通过Compute Shader遍历Tile内所有象素,得出当前Tile的最远z值和最近z值。 + * 根据XY与zNear/zFar得出一个Volume,计算所有光源与Volume相交性。 + */ + TileVolume, ///<瓦片体积剔除 + + /** + * 基于Tile的剔除模式的改进型 + * 同TileVolume方法得出Volume后,再将Volume按深度划分成多个Volume。 + * 剔除掉没有象素的Volume,再将剩下的Volume与光源计算相交性。 + */ + Cluster, ///<集簇剔除 + + ENUM_CLASS_RANGE(None,Cluster) +};//enum class LightingCullingMode + +enum class RenderingMode +{ + Forward, ///<前向渲染 + + Deferred, ///<延迟渲染 + + ENUM_CLASS_RANGE(Forward,Deferred) +};//enum class RenderingMode + + +enum class RenderPhase +{ + PreRender, ///<渲染前 + + Physics, ///<物理 + Anim, ///<动画前 + + Depth, ///<渲染深度(一般用于Early-Z pass) + + GBuffer, ///<生成GBuffer + + Transparent, ///<渲染透明物件 + + PostProcess, ///<后处理 + + Submit, ///<提交指令队列 + Present, ///<呈现 + + ENUM_CLASS_RANGE(PreRender,Present) +};//enum class RenderPhase + +/** +* 渲染框架 +*/ +class RenderFramework +{ + ObjectList module_list; + +protected: + + ViewportInfo viewport_info; + + int swap_chain_count =0; + +protected: + + GPUDevice * device =nullptr; + RenderPass * device_render_pass =nullptr; + RTSwapchain * default_rt =nullptr; + + RenderCmdBuffer ** render_cmd_buffers =nullptr; + +private: + + uint64 frame_count =0; + +public: + + const uint64 GetFrameCount ()const noexcept{return frame_count;} ///<取得当前帧数 + void RestartFrameCount ()noexcept{frame_count=0;} ///<重新开始统计帧数 + +public: + + NO_COPY_NO_MOVE(RenderFramework) + + RenderFramework(){} + virtual ~RenderFramework()=default; + + /** + * @pragma delta_time 本帧时间间隔 + */ + virtual void Update(const double delta_time){} + + virtual void BeginFrame(){}; ///<开始当前帧 + virtual void EndFrame(){}; ///<当前帧结束 + + virtual void MainLoop(); ///<主循环 +};//class RenderFramework + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/RenderModule.h b/inc/hgl/graph/RenderModule.h new file mode 100644 index 00000000..3a3216d0 --- /dev/null +++ b/inc/hgl/graph/RenderModule.h @@ -0,0 +1,66 @@ +#pragma once + +#include +#include + +VK_NAMESPACE_BEGIN + +class RenderCmdBuffer; + +/** +* 渲染模块基类 +*/ +class RenderModule +{ + OSString module_name; + + VkExtent2D current_extent; + + bool module_enable; + bool module_ready; + +protected: + + virtual void SetModuleEnable(bool e){module_enable=e;} + virtual void SetModuleReady(bool r){module_ready=r;} + +public: + + const OSString &GetModuleName()const{return module_name;} + + const bool IsEnable ()const noexcept{return module_enable;} + const bool IsReady ()const noexcept{return module_ready;} + +public: + + NO_COPY_NO_MOVE(RenderModule) + + RenderModule(const OSString &name) + { + module_name=name; + } + + virtual ~RenderModule()=default; + + virtual bool Init(){return true;} + + virtual void Execute(const double,RenderCmdBuffer *){} + + virtual void OnResize(const VkExtent2D &ext){current_extent=ext;} + virtual void OnFocusLost(){} + + virtual void OnPreFrame(){} + virtual void OnPostFrame(){} +};//class RenderModule + +template RenderModule *Create() +{ + return new T(); +} + +typedef RenderModule *(*RenderModuleCreateFunc)(); + +bool RegistryRenderModule(const OSString &,RenderModuleCreateFunc); +RenderModule *GetRenderModule(const OSString &); + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/component/Component.h b/inc/hgl/graph/component/Component.h new file mode 100644 index 00000000..49dc3dcc --- /dev/null +++ b/inc/hgl/graph/component/Component.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +VK_NAMESPACE_BEGIN + +class SceneNode; + +class SceneComponentData +{ +};//class SceneComponentData + +class SceneComponentManager; + +class SceneComponent +{ + SceneComponentManager *manager; + + SceneNode *owner; + +public: + + SceneComponentManager *GetManager()const{return manager;} + + SceneNode *GetOwner()const{return owner;} + + virtual const size_t GetTypeHash()=0; + +public: + + +};//class SceneComponent + +class SceneComponentManager +{ +};//class SceneComponentManager + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/component/SceneComponent.h b/inc/hgl/graph/component/SceneComponent.h new file mode 100644 index 00000000..589abdbf --- /dev/null +++ b/inc/hgl/graph/component/SceneComponent.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include + +VK_NAMESPACE_BEGIN + +class SceneNode; + +class ComponentData +{ +};//class ComponentData + +class ComponentManager; + +class Component +{ + ComponentManager *manager; + + SceneNode *owner; + +public: + + ComponentManager *GetManager()const{return manager;} + + SceneNode *GetOwner()const{return owner;} + + virtual const size_t GetTypeHash()=0; + +public: + + +};//class Component + +class ComponentManager +{ +};//class ComponentManager + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/deferred/GBufferFormat.h b/inc/hgl/graph/deferred/GBufferFormat.h new file mode 100644 index 00000000..8cb4f97f --- /dev/null +++ b/inc/hgl/graph/deferred/GBufferFormat.h @@ -0,0 +1,63 @@ +#pragma once + +#include + +VK_NAMESPACE_BEGIN + +/** +/ + + +//Material models from Google Filament +//@see https://google.github.io/filament/Materials.html + +enum class MaterialModels:uint8 +{ + Unlit=0, + Lit, + Subsurface, + Cloth, +}; + +struct GBufferFormat +{ + VkFormat ShadingModel; ///<着色模式 + + VkFormat BaseColor; ///<颜色缓冲区格式 + VkFormat Depth; ///<深度缓冲区格式 + VkFormat Stencil; ///<模板缓冲区格式 + + VkFormat Normal; ///<法线缓冲区格式 + + VkFormat Metallic; ///<金属度 + VkFormat Roughness; ///<粗糙度(glossiness) + VkFormat Reflectance; ///<反射率(Specular for non-metals) + + VkFormat ClearCoat; + VkFormat ClearCoatRoughness; + VkFormat Anisotropy; + + VkFormat Emissive; ///<自发光 + VkFormat AmbientOcclusion; ///<环境光遮蔽 + + VkFormat MotionVector; ///<运动向量 +};//struct GBufferFormat + +void InitGBufferFormat(GBufferFormat &bf) +{ + bf.BaseColor =PF_A2BGR10UN; + + bf.Depth =PF_D24UN_S8U; + bf.Stencil =PF_D24UN_S8U; + + bf.Normal =PF_RG8UN; + + bf.MetallicRoughness=PF_RG8UN; + + bf.Emissive =PF_A2BGR10UN; + bf.AmbientOcclusion =PF_R8UN; + + bf.MotionVector =PF_RG16SN; +} + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/StaticMesh.h b/inc/hgl/graph/mesh/StaticMesh.h similarity index 96% rename from inc/hgl/graph/StaticMesh.h rename to inc/hgl/graph/mesh/StaticMesh.h index 17d4f8f6..57364314 100644 --- a/inc/hgl/graph/StaticMesh.h +++ b/inc/hgl/graph/mesh/StaticMesh.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include VK_NAMESPACE_BEGIN diff --git a/inc/hgl/graph/StaticMeshLODPolicy.h b/inc/hgl/graph/mesh/StaticMeshLODPolicy.h similarity index 100% rename from inc/hgl/graph/StaticMeshLODPolicy.h rename to inc/hgl/graph/mesh/StaticMeshLODPolicy.h diff --git a/inc/hgl/graph/mesh/StaticMeshNode.h b/inc/hgl/graph/mesh/StaticMeshNode.h new file mode 100644 index 00000000..2ee4607e --- /dev/null +++ b/inc/hgl/graph/mesh/StaticMeshNode.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +VK_NAMESPACE_BEGIN + +class StaticMeshNode:public SceneNode +{ + StaticMesh *sm; + +public: + + + +};//class StaticMeshNode + +VK_NAMESPACE_END diff --git a/inc/hgl/graph/mtl/Material3DCreateConfig.h b/inc/hgl/graph/mtl/Material3DCreateConfig.h index 0df9f25f..fcdfc32e 100644 --- a/inc/hgl/graph/mtl/Material3DCreateConfig.h +++ b/inc/hgl/graph/mtl/Material3DCreateConfig.h @@ -5,56 +5,6 @@ #include STD_MTL_NAMESPACE_BEGIN -enum class LightingModel:uint8 -{ - Unlit, - - Gizmo, ///firstVertex =ri->prd->vertex_offset; dicp->firstInstance =ri->first_instance; } + void MaterialRenderList::WriteICB(VkDrawIndexedIndirectCommand *diicp,RenderItem *ri) { diicp->indexCount =ri->prd->index_count; @@ -432,7 +433,7 @@ void MaterialRenderList::Render(RenderItem *ri) BindVAB(ri->pdb,ri->first_instance); if(ri->pdb->ibo) - cmd_buf->BindIBO(ri->pdb->ibo); + cmd_buf->BindIBO(ri->pdb->ibo); } if(ri->pdb->vdm) diff --git a/src/SceneGraph/RenderFramework.cpp b/src/SceneGraph/RenderFramework.cpp new file mode 100644 index 00000000..46e949e6 --- /dev/null +++ b/src/SceneGraph/RenderFramework.cpp @@ -0,0 +1,31 @@ +#include +#include + +VK_NAMESPACE_BEGIN + +void RenderFramework::MainLoop() +{ + for(auto rm:module_list) + { + if(rm->IsEnable()) + rm->OnPreFrame(); + } + + BeginFrame(); + + for(auto rm:module_list) + { + if(rm->IsEnable()) + rm->Execute(0,nullptr); + } + + for(auto rm:module_list) + { + if(rm->IsEnable()) + rm->OnPostFrame(); + } + + EndFrame(); +} + +VK_NAMESPACE_END diff --git a/src/SceneGraph/RenderModule/RenderModule.cpp b/src/SceneGraph/RenderModule/RenderModule.cpp new file mode 100644 index 00000000..da87b0fc --- /dev/null +++ b/src/SceneGraph/RenderModule/RenderModule.cpp @@ -0,0 +1,39 @@ +#include +#include + +VK_NAMESPACE_BEGIN + +namespace +{ + using RenderModuleMap=Map; + + RenderModuleMap render_module_map; +}//namespace + +bool RegistryRenderModule(const OSString &name,RenderModuleCreateFunc func) +{ + if(name.IsEmpty()||!func) + return(false); + + if(render_module_map.ContainsKey(name)) + return(false); + + render_module_map.Add(name,func); + + return(true); +} + +RenderModule *GetRenderModule(const OSString &name) +{ + if(name.IsEmpty()) + return(nullptr); + + RenderModuleCreateFunc func; + + if(!render_module_map.Get(name,func)) + return(nullptr); + + return func(); +} + +VK_NAMESPACE_END diff --git a/src/SceneGraph/deferred/GBufferFormat.cpp b/src/SceneGraph/deferred/GBufferFormat.cpp new file mode 100644 index 00000000..e69de29b diff --git a/src/SceneGraph/StaticMesh.cpp b/src/SceneGraph/mesh/StaticMesh.cpp similarity index 91% rename from src/SceneGraph/StaticMesh.cpp rename to src/SceneGraph/mesh/StaticMesh.cpp index 52086f25..8cbe2849 100644 --- a/src/SceneGraph/StaticMesh.cpp +++ b/src/SceneGraph/mesh/StaticMesh.cpp @@ -1,4 +1,4 @@ -#include +#include #include VK_NAMESPACE_BEGIN