From 07c761eab9a7c2c5c42391dbdfa73c70cb42de88 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 29 Jan 2021 20:50:47 +0800 Subject: [PATCH] add SceneGraph/SceneTreeToRenderList.cpp, GPUDataArray.h, SceneRoot.h --- CMCore | 2 +- CMSceneGraph | 2 +- inc/hgl/graph/GPUDataArray.h | 77 ++++++++++++++++++++ inc/hgl/graph/RenderList.h | 36 ++++++++- inc/hgl/graph/SceneNode.h | 12 --- inc/hgl/graph/SceneRoot.h | 23 ++++++ src/SceneGraph/CMakeLists.txt | 1 + src/SceneGraph/RenderList.cpp | 93 ++++++++++-------------- src/SceneGraph/SceneTreeToRenderList.cpp | 29 ++++++++ 9 files changed, 205 insertions(+), 70 deletions(-) create mode 100644 inc/hgl/graph/GPUDataArray.h create mode 100644 inc/hgl/graph/SceneRoot.h create mode 100644 src/SceneGraph/SceneTreeToRenderList.cpp diff --git a/CMCore b/CMCore index d065f247..74f7f146 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit d065f24770fae3132012f11312495b3dc5ff3892 +Subproject commit 74f7f146ba6b79fd904f6fd47f4dbee0750831f6 diff --git a/CMSceneGraph b/CMSceneGraph index 275dce7c..fd6fefe9 160000 --- a/CMSceneGraph +++ b/CMSceneGraph @@ -1 +1 @@ -Subproject commit 275dce7c49fd0d1b5ab61a51d18d356ddd2526d1 +Subproject commit fd6fefe93ebf844945c6366d6927447549727bf1 diff --git a/inc/hgl/graph/GPUDataArray.h b/inc/hgl/graph/GPUDataArray.h new file mode 100644 index 00000000..25d1de60 --- /dev/null +++ b/inc/hgl/graph/GPUDataArray.h @@ -0,0 +1,77 @@ +#ifndef HGL_GRAPH_GPU_DATA_ARRAY_INCLUDE +#define HGL_GRAPH_GPU_DATA_ARRAY_INCLUDE + +#include +namespace hgl +{ + namespace graph + { + /** + * GPU数据阵列 + **/ + class GPUDataArray + { + uint32_t alloc_count; + uint32_t count; + uint32_t item_size; + + GPUBuffer * buf_gpu; + uint8 * buf_cpu; + + uint32_t * offset; + + public: + + GPUDataArray(const uint32_t s=0) + { + alloc_count=0; + count=0; + item_size=s; + buf_gpu=nullptr; + buf_cpu=nullptr; + offset=nullptr; + } + + virtual ~GPUDataArray() + { + Clear(); + } + + virtual void Clear() + { + alloc_count=0; + count=0; + + if(buf_gpu) + { + buf_gpu->Unmap(); + delete buf_gpu; + buf_gpu=nullptr; + } + + SAFE_CLEAR_ARRAY(buf_cpu); + SAFE_CLEAR_ARRAY(offset); + } + + bool Init(const uint32_t c,const uint32_t s=0) + { + if(s + &&item_size<=0) + { + if(s>HGL_SIZE_1KB*64) + return(false); + + item_size=s; + } + + count=c; + + if(count*item_size<=0) + return(false); + + buf_cpu=new + } + };//class GPUDataArray + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_GPU_DATA_ARRAY_INCLUDE diff --git a/inc/hgl/graph/RenderList.h b/inc/hgl/graph/RenderList.h index 8d202516..9010e5f0 100644 --- a/inc/hgl/graph/RenderList.h +++ b/inc/hgl/graph/RenderList.h @@ -9,7 +9,7 @@ namespace hgl { namespace graph { - struct L2WArrays; + struct MVPArray; class RenderList { @@ -18,7 +18,9 @@ namespace hgl private: - L2WArrays *LocalToWorld; + Camera *camera; + + MVPArray *mvp_array; List scene_node_list; @@ -40,10 +42,38 @@ namespace hgl void Begin (); void Add (SceneNode *); - void End (); + void End (CameraMatrix *); bool Render (RenderCmdBuffer *); };//class RenderList + + class SceneTreeToRenderList + { + GPUDevice *device; + + public: + + Camera * camera; + CameraMatrix * camera_matrix; + Frustum frustum; + + public: + + virtual uint32 CameraLength(SceneNode *,SceneNode *); ///<摄像机距离比较函数 + + virtual bool InFrustum(const SceneNode *,void *); ///<平截头截剪函数 + + public: + + SceneTreeToRenderList(GPUDevice *d) + { + device=d; + camera=nullptr; + camera_matrix=nullptr; + } + + virtual bool Expend(RenderList *,Camera *,SceneNode *); + };//class SceneTreeToRenderList }//namespace graph }//namespace hgl #endif//HGL_GRAPH_RENDER_LIST_INCLUDE diff --git a/inc/hgl/graph/SceneNode.h b/inc/hgl/graph/SceneNode.h index 4ef33a23..0039786b 100644 --- a/inc/hgl/graph/SceneNode.h +++ b/inc/hgl/graph/SceneNode.h @@ -8,18 +8,6 @@ namespace hgl { namespace graph { - class SceneNode; - struct Camera; - class RenderList; - - using RenderListCompFunc=float (*)(Camera *,SceneNode *,SceneNode *); ///<渲染列表排序比较函数 - - float CameraLengthComp(Camera *,SceneNode *,SceneNode *); ///<摄像机距离比较函数 - - using FilterSceneNodeFunc=bool (*)(const SceneNode *,void *); ///<场景节点过滤函数重定义 - - bool FrustumClipFilter(const SceneNode *,void *); ///<平截头截剪函数 - /** * 场景节点数据类
* 从场景坐标变换(SceneOrient)类继承 diff --git a/inc/hgl/graph/SceneRoot.h b/inc/hgl/graph/SceneRoot.h new file mode 100644 index 00000000..4f978353 --- /dev/null +++ b/inc/hgl/graph/SceneRoot.h @@ -0,0 +1,23 @@ +#ifndef HGL_GRAPH_SCENE_ROOT_INCLUDE +#define HGL_GRAPH_SCENE_ROOT_INCLUDE + +#include + +namespace hgl +{ + namespace graph + { + /** + * 场景根节点 + */ + class SceneRoot + { + Camera *camera; ///<根相机 + + public: + + + };//class SceneRoot + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_SCENE_ROOT_INCLUDE diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index 1dc7ef30..cdf4f111 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -42,6 +42,7 @@ SET(SCENE_GRAPH_SOURCE RenderList.cpp SceneNode.cpp SceneOrient.cpp InlineGeometry.cpp + SceneTreeToRenderList.cpp #InlinePipeline.cpp #Material.cpp #Mesh.cpp diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 478f2602..69d411b0 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -12,44 +12,31 @@ namespace hgl { namespace graph { - constexpr uint32_t L2WItemBytes=sizeof(Matrix4f); ///<单个L2W数据字节数 - - struct L2WArrays + /** + * MVP矩阵 + */ + struct MVPMatrix { - uint alloc_count; - uint count; + Matrix4f l2w; ///< Local to World + Matrix4f inverse_l2w; - List items; - GPUBuffer *buffer; - Matrix4f *buffer_address; - List offset; + Matrix4f mvp; ///< projection * view * local_to_world + Matrix4f inverse_mvp; public: - L2WArrays() + void Set(const Matrix4f &w,const Matrix4f &vp) { - alloc_count=0; - count=0; - buffer=nullptr; - buffer_address=nullptr; - } + l2w=w; + inverse_l2w=l2w.Inverted(); - ~L2WArrays() - { - if(buffer) - { - buffer->Unmap(); - delete buffer; - } + mvp=vp*l2w; + inverse_mvp=mvp.Inverted(); } + };//struct MVPMatrix + + constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix); - void Init(const uint32_t c) - { - count=c; - items.SetCount(count); - offset.SetCount(count); - } - };// float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two) { @@ -75,12 +62,12 @@ namespace hgl last_pipeline =nullptr; last_ri =nullptr; - LocalToWorld=new L2WArrays; + mvp_array=new MVPArray; } RenderList::~RenderList() { - delete LocalToWorld; + delete mvp_array; } void RenderList::Begin() @@ -98,58 +85,58 @@ namespace hgl scene_node_list.Add(node); } - void RenderList::End() + void RenderList::End(CameraMatrix *camera_matrix) { //清0计数器 - uint32_t l2w_count=0; //local to world矩阵总数量 + uint32_t mvp_count=0; //local to world矩阵总数量 //统计Render const uint32_t count=scene_node_list.GetCount(); - LocalToWorld->Init(count); + mvp_array->Init(count); - Matrix4f *mp=LocalToWorld->items.GetData(); - uint32_t *op=LocalToWorld->offset.GetData(); + MVPMatrix *mp=mvp_array->items.GetData(); + uint32_t *op=mvp_array->offset.GetData(); for(SceneNode *node:scene_node_list) { - const Matrix4f &mat=node->GetLocalToWorldMatrix(); + const Matrix4f &l2w=node->GetLocalToWorldMatrix(); - if(!mat.IsIdentity()) + if(!l2w.IsIdentity()) { - hgl_cpy(mp,&mat); + mp->Set(l2w,camera_matrix->vp); ++mp; - *op=(l2w_count)*L2WItemBytes; - ++l2w_count; + *op=(mvp_count)*MVPMatrixBytes; + ++mvp_count; } else { - *op=l2w_count; + *op=mvp_count; } ++op; } - if(LocalToWorld->buffer) + if(mvp_array->buffer) { - if(LocalToWorld->alloc_countalloc_countbuffer->Unmap(); - delete LocalToWorld->buffer; - LocalToWorld->buffer=nullptr; -// LocalToWorld->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作 + mvp_array->buffer->Unmap(); + delete mvp_array->buffer; + mvp_array->buffer=nullptr; +// mvp_array->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作 } } - if(!LocalToWorld->buffer) + if(!mvp_array->buffer) { - LocalToWorld->alloc_count=power_to_2(l2w_count); - LocalToWorld->buffer=device->CreateUBO(LocalToWorld->alloc_count*L2WItemBytes); - LocalToWorld->buffer_address=(Matrix4f *)(LocalToWorld->buffer->Map()); + mvp_array->alloc_count=power_to_2(mvp_count); + mvp_array->buffer=device->CreateUBO(mvp_array->alloc_count*MVPMatrixBytes); + mvp_array->buffer_address=(MVPMatrix *)(mvp_array->buffer->Map()); } - hgl_cpy(LocalToWorld->buffer_address,LocalToWorld->items.GetData(),l2w_count); + hgl_cpy(mvp_array->buffer_address,mvp_array->items.GetData(),mvp_count); } void RenderList::Render(SceneNode *node,RenderableInstance *ri) diff --git a/src/SceneGraph/SceneTreeToRenderList.cpp b/src/SceneGraph/SceneTreeToRenderList.cpp new file mode 100644 index 00000000..918cd9da --- /dev/null +++ b/src/SceneGraph/SceneTreeToRenderList.cpp @@ -0,0 +1,29 @@ +#include + +namespace hgl +{ + namespace graph + { + uint32 SceneTreeToRenderList::CameraLength(SceneNode *,SceneNode *) + { + } + + bool SceneTreeToRenderList::InFrustum(const SceneNode *,void *) + { + } + + bool SceneTreeToRenderList::Expend(RenderList *rl,Camera *c,SceneNode *sn) + { + if(!device)return(false); + if(!rl||!c||sn)return(false); + + camera=c; + camera->Refresh(); + camera_matrix=&(camera->matrix); + + //Frustum f; + + + } + }//namespace graph +}//namespace hgl