From cd03ee82113f10f97d5aff8ee5ba6b8188eb3c89 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Tue, 25 Apr 2023 19:18:07 +0800 Subject: [PATCH] added RenderNode2D.cpp --- src/SceneGraph/CMakeLists.txt | 5 +- src/SceneGraph/RenderList2D.cpp | 238 ++++++++++++-------------------- src/SceneGraph/RenderNode2D.cpp | 81 +++++++++++ 3 files changed, 176 insertions(+), 148 deletions(-) create mode 100644 src/SceneGraph/RenderNode2D.cpp diff --git a/src/SceneGraph/CMakeLists.txt b/src/SceneGraph/CMakeLists.txt index d67a2b66..53802284 100644 --- a/src/SceneGraph/CMakeLists.txt +++ b/src/SceneGraph/CMakeLists.txt @@ -35,14 +35,15 @@ source_group("Light" FILES ${LIGHT_FILES}) SET(SCENE_GRAPH_HEADER ${SG_INCLUDE_PATH}/SceneInfo.h ${SG_INCLUDE_PATH}/SceneNode.h - ${SG_INCLUDE_PATH}/RenderNode.h + ${SG_INCLUDE_PATH}/RenderNode2D.h ${SG_INCLUDE_PATH}/SceneOrient.h - ${SG_INCLUDE_PATH}/RenderList.h + #${SG_INCLUDE_PATH}/RenderList.h ${SG_INCLUDE_PATH}/RenderList2D.h ) SET(SCENE_GRAPH_SOURCE #RenderList.cpp + RenderNode2D.cpp RenderList2D.cpp #RenderList3D.cpp SceneNode.cpp diff --git a/src/SceneGraph/RenderList2D.cpp b/src/SceneGraph/RenderList2D.cpp index 581b2613..037c738c 100644 --- a/src/SceneGraph/RenderList2D.cpp +++ b/src/SceneGraph/RenderList2D.cpp @@ -8,68 +8,6 @@ #include #include -/** -* -* 理论上讲,我们需要按以下顺序排序 -* -* for(material) -* for(pipeline) -* for(material_instance) -* for(vbo) -*/ - -template<> -int Comparator::compare(const RenderNode2DPointer &obj_one,const RenderNode2DPointer &obj_two) const -{ - int off; - - hgl::graph::Renderable *ri_one=obj_one->ri; - hgl::graph::Renderable *ri_two=obj_two->ri; - - //比较材质 - { - off=ri_one->GetMaterial() - -ri_two->GetMaterial(); - - if(off) - return off; - } - - //比较管线 - { - off=ri_one->GetPipeline() - -ri_two->GetPipeline(); - - if(off) - return off; - } - - //比较材质实例 - //{ - // for(int i =(int)hgl::graph::DescriptorSetType::BEGIN_RANGE; - // i<=(int)hgl::graph::DescriptorSetType::END_RANGE; - // i++) - // { - // off=ri_one->GetMP((hgl::graph::DescriptorSetType)i) - // -ri_two->GetMP((hgl::graph::DescriptorSetType)i); - - // if(off) - // return off; - // } - //} - - //比较vbo+ebo - { - off=ri_one->GetBufferHash() - -ri_two->GetBufferHash(); - - if(off) - return off; - } - - return 0; -} - namespace hgl { namespace graph @@ -79,7 +17,6 @@ namespace hgl device =dev; cmd_buf =nullptr; - last_mtl =nullptr; last_pipeline =nullptr; hgl_zero(last_mp); last_vbo =0; @@ -91,61 +28,66 @@ namespace hgl bool RenderList2D::Begin() { - mrl_map.ClearData(); + renderable_count=0; + mrl_map.Begin(); return(true); } void RenderList2D::End() { - if(render_node_list.GetCount()<=0)return; - - //排序 - Sort(render_node_list,&render_node_comparator); - - //产生MVP矩阵UBO数据 - { - const uint32_t count=render_node_list.GetCount(); - - { - //按当前总节点数量分配UBO -// mvp_array->Alloc(count); -// mvp_array->Clear(); - - ri_list.ClearData(); - ri_list.SetCount(count); - } - - { -// ubo_align=mvp_array->GetAlignSize(); - -// char *mp=(char *)(mvp_array->Map(0,count)); - Renderable **ri=ri_list.GetData(); - - for(RenderNode2D *node:render_node_list) //未来可能要在Expend处考虑做去重 - { -// memcpy(mp,&(node->matrix),MVPMatrixBytes); -// mp+=ubo_align; - - (*ri)=node->ri; - ++ri; - } - -// mvp_array->Flush(count); - } - } - - //为所有的材质绑定 - //for(Material *mtl:material_sets) - //{ - // MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerObject); - - // if(mp) - // { - // if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) - // mp->Update(); - // } - //} + if(renderable_count<=0)return; + + mrl_map.End(); + + +// +// //排序 +// Sort(render_node_list,&render_node_comparator); +// +// //产生MVP矩阵UBO数据 +// { +// const uint32_t count=render_node_list.GetCount(); +// +// { +// //按当前总节点数量分配UBO +//// mvp_array->Alloc(count); +//// mvp_array->Clear(); +// +// ri_list.ClearData(); +// ri_list.SetCount(count); +// } +// +// { +//// ubo_align=mvp_array->GetAlignSize(); +// +//// char *mp=(char *)(mvp_array->Map(0,count)); +// Renderable **ri=ri_list.GetData(); +// +// for(RenderNode2D *node:render_node_list) //未来可能要在Expend处考虑做去重 +// { +//// memcpy(mp,&(node->matrix),MVPMatrixBytes); +//// mp+=ubo_align; +// +// (*ri)=node->ri; +// ++ri; +// } +// +//// mvp_array->Flush(count); +// } +// } +// +// //为所有的材质绑定 +// //for(Material *mtl:material_sets) +// //{ +// // MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerObject); +// +// // if(mp) +// // { +// // if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) +// // mp->Update(); +// // } +// //} } bool RenderList2D::ExpendNode(SceneNode *sn) @@ -156,15 +98,19 @@ namespace hgl if(ri) { - RenderNode2D *rn=new RenderNode2D; + Material *mtl=ri->GetMaterial(); + MaterialRenderList2D *mrl; - rn->local_to_world=sn->GetLocalToWorldMatrix(); + if(!mrl_map.Get(mtl,mrl)) + { + mrl=new MaterialRenderList2D(mtl); - rn->ri=ri; + mrl_map.Add(mtl,mrl); + } + + mrl->Add(ri,sn->GetLocalToWorldMatrix()); - render_node_list.Add(rn); - - material_sets.Add(ri->GetMaterial()); + ++renderable_count; } for(SceneNode *sub:sn->SubNode) @@ -179,45 +125,45 @@ namespace hgl Begin(); ExpendNode(sn); - End(); +// End(); return(true); } - bool RenderList2D::BindPerFrameDescriptor() - { - if(!cmd_buf)return(false); + //bool RenderList2D::BindPerFrameDescriptor() + //{ + // if(!cmd_buf)return(false); - for(Material *mtl:material_sets) - { - MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerFrame); + // for(Material *mtl:material_sets) + // { + // MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerFrame); - if(!mp)continue; - - //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) - // mp->Update(); - } + // if(!mp)continue; + // + // //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) + // // mp->Update(); + // } - return(true); - } + // return(true); + //} - bool RenderList2D::BindPerMaterialDescriptor() - { - //为每个材质实例,绑定它们的描述符 - if(!cmd_buf)return(false); + //bool RenderList2D::BindPerMaterialDescriptor() + //{ + // //为每个材质实例,绑定它们的描述符 + // if(!cmd_buf)return(false); - for(Material *mtl:material_sets) - { - MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerMaterial); + // for(Material *mtl:material_sets) + // { + // MaterialParameters *mp=mtl->GetMP(DescriptorSetType::PerMaterial); - if(!mp)continue; - - //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) - // mp->Update(); - } + // if(!mp)continue; + // + // //if(mp->BindUBO("r_scene_info",mvp_array->GetBuffer(),true)) + // // mp->Update(); + // } - return(true); - } + // return(true); + //} void RenderList2D::Render(Renderable *ri) { diff --git a/src/SceneGraph/RenderNode2D.cpp b/src/SceneGraph/RenderNode2D.cpp new file mode 100644 index 00000000..a8bee3cd --- /dev/null +++ b/src/SceneGraph/RenderNode2D.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +/** +* +* ϽҪ˳ +* +* for(material) +* for(pipeline) +* for(material_instance) +* for(vbo) +*/ + +template<> +int Comparator::compare(const hgl::graph::RenderNode2D &obj_one,const hgl::graph::RenderNode2D &obj_two) const +{ + int off; + + hgl::graph::Renderable *ri_one=obj_one.ri; + hgl::graph::Renderable *ri_two=obj_two.ri; + + //ȽϹ + { + off=ri_one->GetPipeline() + -ri_two->GetPipeline(); + + if(off) + return off; + } + + //Ƚϲʵ + //{ + // for(int i =(int)hgl::graph::DescriptorSetType::BEGIN_RANGE; + // i<=(int)hgl::graph::DescriptorSetType::END_RANGE; + // i++) + // { + // off=ri_one->GetMP((hgl::graph::DescriptorSetType)i) + // -ri_two->GetMP((hgl::graph::DescriptorSetType)i); + + // if(off) + // return off; + // } + //} + + //Ƚvbo+ebo + { + off=ri_one->GetBufferHash() + -ri_two->GetBufferHash(); + + if(off) + return off; + } + + return 0; +} + +namespace hgl +{ + namespace graph + { + void MaterialRenderList2D::Add(Renderable *ri,const Matrix3x4f &mat) + { + RenderNode2D rn; + + rn.local_to_world=mat; + rn.ri=ri; + + rn_list.Add(rn); + } + + void MaterialRenderList2D::End() + { + Comparator rnc; + + Sort(rn_list,&rnc); + + + } + }//namespace graph +}//namespace hgl