diff --git a/CMCore b/CMCore index ec23a526..0a7b1ff3 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit ec23a526afb1543236817a930c2f8a57b0b37570 +Subproject commit 0a7b1ff3afd95e2c41a4476c1c00dec6511ad447 diff --git a/inc/hgl/graph/MVPMatrix.h b/inc/hgl/graph/MVPMatrix.h new file mode 100644 index 00000000..db1e7405 --- /dev/null +++ b/inc/hgl/graph/MVPMatrix.h @@ -0,0 +1,36 @@ +#ifndef HGL_GRAPH_MVP_MATRIX_INCLUDE +#define HGL_GRAPH_MVP_MATRIX_INCLUDE + +#include +#include +namespace hgl +{ + namespace graph + { + /** + * MVP + */ + struct MVPMatrix + { + Matrix4f l2w; ///< Local to World + Matrix4f inverse_l2w; + + Matrix4f mvp; ///< projection * view * local_to_world + Matrix4f inverse_mvp; + + public: + + void Set(const Matrix4f &w,const Matrix4f &vp) + { + l2w=w; + inverse_l2w=l2w.Inverted(); + + mvp=vp*l2w; + inverse_mvp=mvp.Inverted(); + } + + CompOperatorMemcmp(const MVPMatrix &); + };//struct MVPMatrix + }//namespace graph +//namespace hgl +#endif//HGL_GRAPH_MVP_MATRIX_INCLUDE diff --git a/inc/hgl/graph/RenderList.h b/inc/hgl/graph/RenderList.h index 5ad1c5db..67d2d463 100644 --- a/inc/hgl/graph/RenderList.h +++ b/inc/hgl/graph/RenderList.h @@ -4,15 +4,17 @@ #include #include #include +#include +#include #include #include namespace hgl { namespace graph { - struct MVPArray; - - using SceneNodeList=List; + using SceneNodeList=List; + using MVPArrayBuffer=GPUArrayBuffer; + using MVPOffsetBuffer=List; class RenderList { @@ -23,9 +25,12 @@ namespace hgl Camera *camera; - MVPArray *mvp_array; + SceneNodeList scene_node_list; - SceneNodeList *scene_node_list; + MVPArrayBuffer *mvp_array; + MVPOffsetBuffer mvp_offset; + + private: Pipeline * last_pipeline; RenderableInstance *last_ri; @@ -48,60 +53,7 @@ namespace hgl void End (CameraMatrix *); bool Render (RenderCmdBuffer *); - };//class RenderList - - class SceneTreeToRenderList - { - using PipelineSets=Sets; - using MaterialSets=Sets; - using MatInstanceSets=Sets; - - protected: - - GPUDevice *device; - - protected: - - Camera * camera; - CameraMatrix * camera_matrix; - Frustum frustum; - - protected: - - SceneNodeList * scene_node_list; ///<场景节点列表 - - PipelineSets pipeline_sets; ///<管线合集 - MaterialSets material_sets; ///<材质合集 - MatInstanceSets mat_instance_sets; ///<材质实例合集 - - RenderList * render_list; - - protected: - - virtual uint32 CameraLength(SceneNode *,SceneNode *); ///<摄像机距离比较函数 - - virtual bool InFrustum(const SceneNode *,void *); ///<平截头截剪函数 - - virtual bool Begin(); - virtual bool Expend(SceneNode *); - virtual bool End(); - - public: - - SceneTreeToRenderList(GPUDevice *d) - { - device=d; - camera=nullptr; - camera_matrix=nullptr; - - scene_node_list=nullptr; - render_list=nullptr; - } - - virtual ~SceneTreeToRenderList(); - - virtual bool Expend(RenderList *,Camera *,SceneNode *); - };//class SceneTreeToRenderList + };//class RenderList }//namespace graph }//namespace hgl #endif//HGL_GRAPH_RENDER_LIST_INCLUDE diff --git a/inc/hgl/graph/SceneTreeToRenderList.h b/inc/hgl/graph/SceneTreeToRenderList.h new file mode 100644 index 00000000..6ed85e45 --- /dev/null +++ b/inc/hgl/graph/SceneTreeToRenderList.h @@ -0,0 +1,63 @@ +#ifndef HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE +#define HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE + +#include +namespace hgl +{ + namespace graph + { + class SceneTreeToRenderList + { + using PipelineSets=Sets; + using MaterialSets=Sets; + using MatInstanceSets=Sets; + + protected: + + GPUDevice *device; + + protected: + + Camera * camera; + CameraMatrix * camera_matrix; + Frustum frustum; + + protected: + + SceneNodeList * scene_node_list; ///<ڵб + + PipelineSets pipeline_sets; ///<ߺϼ + MaterialSets material_sets; ///<ʺϼ + MatInstanceSets mat_instance_sets; ///<ʵϼ + + RenderList * render_list; + + protected: + + virtual uint32 CameraLength(SceneNode *,SceneNode *); ///<ȽϺ + + virtual bool InFrustum(const SceneNode *,void *); ///<ƽͷؼ + + virtual bool Begin(); + virtual bool Expend(SceneNode *); + virtual bool End(); + + public: + + SceneTreeToRenderList(GPUDevice *d) + { + device=d; + camera=nullptr; + camera_matrix=nullptr; + + scene_node_list=nullptr; + render_list=nullptr; + } + + virtual ~SceneTreeToRenderList(); + + virtual bool Expend(RenderList *,Camera *,SceneNode *); + };//class SceneTreeToRenderList + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_SCENE_TREE_TO_RENDER_LIST_INCLUDE diff --git a/inc/hgl/graph/VKArrayBuffer.h b/inc/hgl/graph/VKArrayBuffer.h index 8d0cdca5..4bb87752 100644 --- a/inc/hgl/graph/VKArrayBuffer.h +++ b/inc/hgl/graph/VKArrayBuffer.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace hgl { @@ -16,26 +17,43 @@ namespace hgl { protected: - GPUDevice * device; + GPUDevice *device; + VkBufferUsageFlags buffer_usage_flags; Collection *coll; - private: + public: - GPUArrayBuffer(GPUDevice *device,const uint32_t s,const uint32_t c) + GPUArrayBuffer(GPUDevice *dev,VkBufferUsageFlags flags) { - + device=dev; + buffer_usage_flags=flags; + + coll=new Collection(new MemoryBlock(new VKMemoryAllocator(device,buffer_usage_flags))); } - friend class GPUDevice; + virtual ~GPUArrayBuffer() + { + delete coll; + } - public: + uint32 Alloc(const uint32 max_count) ///<预分配空间 + { + if(!coll->Alloc(max_count)) + return(0); - virtual ~GPUArrayBuffer(); + return coll->GetAllocCount(); + } - void Clear(); ///<清空缓冲区 + void Clear() + { + coll->Clear(); + } - bool Init(const uint32_t); ///<初始化并分配空间 + T *Map(const uint32 start,const uint32 count) + { + return coll->Map(start,count); + } };//class GPUArrayBuffer }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index 5ea7adca..dedac110 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -14,6 +14,7 @@ #include #include #include +#include namespace hgl { diff --git a/inc/hgl/graph/VKMemoryAllocator.h b/inc/hgl/graph/VKMemoryAllocator.h index 4b46a0db..014b87c6 100644 --- a/inc/hgl/graph/VKMemoryAllocator.h +++ b/inc/hgl/graph/VKMemoryAllocator.h @@ -25,7 +25,7 @@ public: public: - VKMemoryAllocator(GPUDevice *,const uint32_t); + VKMemoryAllocator(GPUDevice *,const uint32_t flags); ~VKMemoryAllocator(); void Free() override {/* DON'T RUN ANY OPERATION.*/} diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index f4fb59a4..91ac1bbd 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -29,9 +29,11 @@ SET(TILE_SOURCE ${SG_INCLUDE_PATH}/TileData.h SOURCE_GROUP("Tile" FILES ${TILE_SOURCE}) SET(SCENE_GRAPH_HEADER ${SG_INCLUDE_PATH}/Light.h + ${SG_INCLUDE_PATH}/MVPMatrix.h ${SG_INCLUDE_PATH}/SceneNode.h ${SG_INCLUDE_PATH}/SceneOrient.h ${SG_INCLUDE_PATH}/RenderList.h + ${SG_INCLUDE_PATH}/SceneTreeToRenderList.h ${SG_INCLUDE_PATH}/InlineGeometry.h #${SG_INCLUDE_PATH}/Mesh.h #${SG_INCLUDE_PATH}/Material.h diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 69d411b0..3182b9a8 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -12,32 +13,8 @@ namespace hgl { namespace graph { - /** - * MVP矩阵 - */ - struct MVPMatrix - { - Matrix4f l2w; ///< Local to World - Matrix4f inverse_l2w; - - Matrix4f mvp; ///< projection * view * local_to_world - Matrix4f inverse_mvp; - - public: - - void Set(const Matrix4f &w,const Matrix4f &vp) - { - l2w=w; - inverse_l2w=l2w.Inverted(); - - mvp=vp*l2w; - inverse_mvp=mvp.Inverted(); - } - };//struct MVPMatrix - constexpr size_t MVPMatrixBytes=sizeof(MVPMatrix); - float CameraLengthComp(Camera *cam,SceneNode *obj_one,SceneNode *obj_two) { if(!cam||!obj_one||!obj_two) @@ -62,7 +39,7 @@ namespace hgl last_pipeline =nullptr; last_ri =nullptr; - mvp_array=new MVPArray; + mvp_array =new MVPArrayBuffer(gd,VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT); } RenderList::~RenderList() @@ -74,6 +51,9 @@ namespace hgl { scene_node_list.ClearData(); + mvp_array->Clear(); + mvp_offset.ClearData(); + last_pipeline =nullptr; last_ri =nullptr; } @@ -93,10 +73,14 @@ namespace hgl //统计Render const uint32_t count=scene_node_list.GetCount(); - mvp_array->Init(count); + mvp_array->Alloc(count); + mvp_array->Clear(); - MVPMatrix *mp=mvp_array->items.GetData(); - uint32_t *op=mvp_array->offset.GetData(); + 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) { @@ -107,36 +91,16 @@ namespace hgl mp->Set(l2w,camera_matrix->vp); ++mp; - *op=(mvp_count)*MVPMatrixBytes; + *op=mvp_count*MVPMatrixBytes; ++mvp_count; } else { - *op=mvp_count; + *op=mvp_count*MVPMatrixBytes; } ++op; } - - if(mvp_array->buffer) - { - if(mvp_array->alloc_countbuffer->Unmap(); - delete mvp_array->buffer; - mvp_array->buffer=nullptr; -// mvp_array->buffer_address=nullptr; //下面一定会重写,所以这一行没必要操作 - } - } - - if(!mvp_array->buffer) - { - 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(mvp_array->buffer_address,mvp_array->items.GetData(),mvp_count); } void RenderList::Render(SceneNode *node,RenderableInstance *ri)