From 983202966e7bef5a13e7c87c52a99c7f1451975a Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 15 Jun 2021 15:36:30 +0800 Subject: [PATCH] refactor RenderList and SceneTreeToRenderList --- inc/hgl/graph/RenderList.h | 26 +++--- src/SceneGraph/RenderList.cpp | 111 +++++++---------------- src/SceneGraph/SceneTreeToRenderList.cpp | 4 +- 3 files changed, 50 insertions(+), 91 deletions(-) diff --git a/inc/hgl/graph/RenderList.h b/inc/hgl/graph/RenderList.h index 4448b04d..17259d27 100644 --- a/inc/hgl/graph/RenderList.h +++ b/inc/hgl/graph/RenderList.h @@ -13,10 +13,6 @@ namespace hgl { namespace graph { -// using SceneNodeList=List; - using MVPArrayBuffer=GPUArrayBuffer; - using MVPOffsetBuffer=List; - /** * 渲染对象列表
* 已经展开的渲染对象列表,产生mvp用UBO/SSBO等数据,最终创建RenderCommandBuffer @@ -30,15 +26,19 @@ namespace hgl Camera *camera; - RenderNodeList render_node_list; + GPUBuffer *mvp_buffer; + List *ri_list; - MVPArrayBuffer *mvp_array; - MVPOffsetBuffer mvp_offset; + DescriptorSets *renderable_desc_sets; + + uint32_t ubo_offset; + uint32_t ubo_align; private: Pipeline * last_pipeline; - RenderableInstance *last_ri; + MaterialInstance * last_mi; + uint32_t last_vbo; void Render(RenderableInstance *); @@ -48,14 +48,14 @@ namespace hgl RenderList(GPUDevice *); + friend class SceneTreeToRenderList; + + void Set(List *,GPUBuffer *,const uint32_t); + public: - virtual ~RenderList(); + virtual ~RenderList()=default; - void Begin (); - void Add (SceneNode *); - void End (CameraInfo *); - bool Render (RenderCmdBuffer *); };//class RenderList }//namespace graph diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 58c8f8d2..044b641a 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -13,85 +13,27 @@ namespace hgl { namespace graph { - constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix); - - //bool FrustumClipFilter(const SceneNode *node,void *fc) - //{ - // if(!node||!fc)return(false); - - // return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE); - //} - RenderList::RenderList(GPUDevice *gd) { device =gd; cmd_buf =nullptr; - last_pipeline =nullptr; - last_ri =nullptr; + mvp_buffer =nullptr; + ri_list =nullptr; - mvp_array =new MVPArrayBuffer(gd,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); - } - - RenderList::~RenderList() - { - delete mvp_array; - } - - void RenderList::Begin() - { - scene_node_list.ClearData(); - - mvp_array->Clear(); - mvp_offset.ClearData(); + ubo_offset =0; + ubo_align =0; last_pipeline =nullptr; - last_ri =nullptr; + last_mi =nullptr; + last_vbo =0; } - void RenderList::Add(SceneNode *node) + void RenderList::Set(List *ril,GPUBuffer *buf,const uint32_t align) { - if(!node)return; - - scene_node_list.Add(node); - } - - void RenderList::End(CameraInfo *camera_info) - { - //清0计数器 - uint32_t mvp_count=0; //local to world矩阵总数量 - - //统计Render - const uint32_t count=scene_node_list.GetCount(); - - mvp_array->Alloc(count); - mvp_array->Clear(); - - mvp_offset.PreMalloc(count); - mvp_offset.ClearData(); - - MVPMatrix *mp=mvp_array->Map(0,count); - uint32_t *op=mvp_offset.GetData(); - - for(SceneNode *node:scene_node_list) - { - const Matrix4f &l2w=node->GetLocalToWorldMatrix(); - - if(!l2w.IsIdentity()) - { - mp->Set(l2w,camera_info->vp); - ++mp; - - *op=mvp_count*MVPMatrixBytes; - ++mvp_count; - } - else - { - *op=mvp_count*MVPMatrixBytes; - } - - ++op; - } + ri_list=ril; + mvp_buffer=buf; + ubo_align=align; } void RenderList::Render(RenderableInstance *ri) @@ -106,16 +48,22 @@ namespace hgl { MaterialInstance *mi=ri->GetMaterialInstance(); - cmd_buf->BindDescriptorSets(mi->GetDescriptorSets()); + if(mi!=last_mi) + { + last_mi=mi; + cmd_buf->BindDescriptorSets(mi->GetDescriptorSets()); + } } - //更新fin_mvp - - if(ri!=last_ri) { - cmd_buf->BindVAB(ri); + + } - last_ri=ri; + if(last_vbo!=ri->GetBufferHash()) + { + last_vbo=ri->GetBufferHash(); + + cmd_buf->BindVAB(ri); } const IndexBuffer *ib=ri->GetIndexBuffer(); @@ -135,13 +83,22 @@ namespace hgl if(!cb) return(false); + if(!mvp_buffer + ||!ri_list) + return(false); + + if(ri_list->GetCount()<=0) + return(true); + cmd_buf=cb; last_pipeline=nullptr; - last_ri=nullptr; + last_mi=nullptr; + last_vbo=0; + ubo_offset=0; - for(SceneNode *sn:scene_node_list) - Render(sn->render_obj); + for(RenderableInstance *ri:*ri_list) + Render(ri); return(true); } diff --git a/src/SceneGraph/SceneTreeToRenderList.cpp b/src/SceneGraph/SceneTreeToRenderList.cpp index 14cf961c..5c31b884 100644 --- a/src/SceneGraph/SceneTreeToRenderList.cpp +++ b/src/SceneGraph/SceneTreeToRenderList.cpp @@ -130,7 +130,9 @@ namespace hgl } //写入RenderList - render_list->Set(mvp_array->GetBuffer(),&ri_list); + render_list->Set( &ri_list, + mvp_array->GetBuffer(), + mvp_array->GetUnitSize()); } bool SceneTreeToRenderList::Expend(SceneNode *sn)