From 5806a88964640f563fe9e6e6806ffd3dd76188ba Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 18 Dec 2020 16:52:45 +0800 Subject: [PATCH] moved GPUArrays to RenderList.cpp --- inc/hgl/graph/RenderList.h | 41 ++++--------- src/SceneGraph/RenderList.cpp | 110 +++++++++++++++++++++++++++++----- 2 files changed, 106 insertions(+), 45 deletions(-) diff --git a/inc/hgl/graph/RenderList.h b/inc/hgl/graph/RenderList.h index 6b20bea8..8d202516 100644 --- a/inc/hgl/graph/RenderList.h +++ b/inc/hgl/graph/RenderList.h @@ -9,55 +9,34 @@ namespace hgl { namespace graph { - template struct GPUArray - { - List list; - GPUBuffer *buffer; - - public: - - GPUArray() - { - buffer=nullptr; - } - - ~GPUArray() - { - SAFE_CLEAR(buffer); - } - - void Clear() - { - list.ClearData(); - } - - void Add(const T &data) - { - list.Add(data); - } - };// + struct L2WArrays; class RenderList { + GPUDevice *device; RenderCmdBuffer *cmd_buf; private: - GPUArray LocalToWorld; + L2WArrays *LocalToWorld; List scene_node_list; Pipeline * last_pipeline; - MaterialInstance * last_mat_inst; RenderableInstance *last_ri; void Render(SceneNode *,RenderableInstance *); void Render(SceneNode *,List &); + private: + + friend class GPUDevice; + + RenderList(GPUDevice *); + public: - RenderList(); - virtual ~RenderList()=default; + virtual ~RenderList(); void Begin (); void Add (SceneNode *); diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 67587e31..478f2602 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -11,6 +12,45 @@ namespace hgl { namespace graph { + constexpr uint32_t L2WItemBytes=sizeof(Matrix4f); ///<单个L2W数据字节数 + + struct L2WArrays + { + uint alloc_count; + uint count; + + List items; + GPUBuffer *buffer; + Matrix4f *buffer_address; + List offset; + + public: + + L2WArrays() + { + alloc_count=0; + count=0; + buffer=nullptr; + buffer_address=nullptr; + } + + ~L2WArrays() + { + if(buffer) + { + buffer->Unmap(); + delete buffer; + } + } + + void Init(const uint32_t c) + { + count=c; + items.SetCount(count); + offset.SetCount(count); + } + };// + float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two) { if(!cam||!obj_one||!obj_two) @@ -27,22 +67,27 @@ namespace hgl // return (((Frustum *)fc)->BoxIn(node->GetWorldBoundingBox())!=Frustum::OUTSIDE); //} - RenderList::RenderList() + RenderList::RenderList(GPUDevice *gd) { + device =gd; cmd_buf =nullptr; last_pipeline =nullptr; - last_mat_inst =nullptr; last_ri =nullptr; + + LocalToWorld=new L2WArrays; + } + + RenderList::~RenderList() + { + delete LocalToWorld; } void RenderList::Begin() { - LocalToWorld.Clear(); scene_node_list.ClearData(); last_pipeline =nullptr; - last_mat_inst =nullptr; last_ri =nullptr; } @@ -50,18 +95,61 @@ namespace hgl { if(!node)return; - LocalToWorld.Add(node->GetLocalToWorldMatrix()); - scene_node_list.Add(node); } void RenderList::End() { + //清0计数器 + uint32_t l2w_count=0; //local to world矩阵总数量 + + //统计Render const uint32_t count=scene_node_list.GetCount(); + LocalToWorld->Init(count); + + Matrix4f *mp=LocalToWorld->items.GetData(); + uint32_t *op=LocalToWorld->offset.GetData(); + for(SceneNode *node:scene_node_list) { + const Matrix4f &mat=node->GetLocalToWorldMatrix(); + + if(!mat.IsIdentity()) + { + hgl_cpy(mp,&mat); + ++mp; + + *op=(l2w_count)*L2WItemBytes; + ++l2w_count; + } + else + { + *op=l2w_count; + } + + ++op; } + + if(LocalToWorld->buffer) + { + if(LocalToWorld->alloc_countbuffer->Unmap(); + delete LocalToWorld->buffer; + LocalToWorld->buffer=nullptr; +// LocalToWorld->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作 + } + } + + if(!LocalToWorld->buffer) + { + LocalToWorld->alloc_count=power_to_2(l2w_count); + LocalToWorld->buffer=device->CreateUBO(LocalToWorld->alloc_count*L2WItemBytes); + LocalToWorld->buffer_address=(Matrix4f *)(LocalToWorld->buffer->Map()); + } + + hgl_cpy(LocalToWorld->buffer_address,LocalToWorld->items.GetData(),l2w_count); } void RenderList::Render(SceneNode *node,RenderableInstance *ri) @@ -71,15 +159,12 @@ namespace hgl last_pipeline=ri->GetPipeline(); cmd_buf->BindPipeline(last_pipeline); - - last_mat_inst=nullptr; } - if(last_mat_inst!=ri->GetMaterialInstance()) { - last_mat_inst=ri->GetMaterialInstance(); + MaterialInstance *mi=ri->GetMaterialInstance(); - cmd_buf->BindDescriptorSets(last_mat_inst->GetDescriptorSets()); + cmd_buf->BindDescriptorSets(mi->GetDescriptorSets()); } //更新fin_mvp @@ -123,7 +208,6 @@ namespace hgl cmd_buf=cb; last_pipeline=nullptr; - last_mat_inst=nullptr; last_ri=nullptr; const int count=scene_node_list.GetCount(); @@ -131,8 +215,6 @@ namespace hgl for(int i=0;i....offset L2W Matrix - Render(*node,(*node)->renderable_instances); ++node; }