From 3768507169d921a044b028c43d3da38b60c77db7 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 2 Aug 2024 23:17:07 +0800 Subject: [PATCH] save full SceneNode in RenderNode,added length sort. --- CMCore | 2 +- example/Gizmo/GizmoTest.cpp | 1 + inc/hgl/graph/MaterialRenderList.h | 8 ++++ inc/hgl/graph/MaterialRenderMap.h | 5 ++- inc/hgl/graph/RenderList.h | 9 ++-- inc/hgl/graph/RenderNode.h | 6 ++- src/SceneGraph/MaterialRenderList.cpp | 62 ++++++++++++++++++--------- src/SceneGraph/RenderAssignBuffer.cpp | 7 +-- src/SceneGraph/RenderList.cpp | 4 +- 9 files changed, 73 insertions(+), 31 deletions(-) diff --git a/CMCore b/CMCore index 7589d627..0dd89d78 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 7589d62732b68b99de1bec561b1703dd2b043c03 +Subproject commit 0dd89d78abd3cb24c6ce36d57fd9d8ccac3ec983 diff --git a/example/Gizmo/GizmoTest.cpp b/example/Gizmo/GizmoTest.cpp index 6635bd3c..1f319360 100644 --- a/example/Gizmo/GizmoTest.cpp +++ b/example/Gizmo/GizmoTest.cpp @@ -92,6 +92,7 @@ public: } root.RefreshMatrix(); + render_list->SetCamera(&(camera_control->GetCameraInfo())); render_list->Expend(&root); SceneAppFramework::BuildCommandBuffer(index); diff --git a/inc/hgl/graph/MaterialRenderList.h b/inc/hgl/graph/MaterialRenderList.h index 24ce8b98..240ae21c 100644 --- a/inc/hgl/graph/MaterialRenderList.h +++ b/inc/hgl/graph/MaterialRenderList.h @@ -7,6 +7,7 @@ VK_NAMESPACE_BEGIN class RenderAssignBuffer; class SceneNode; +struct CameraInfo; /** * 同一材质的对象渲染列表 @@ -18,6 +19,8 @@ class MaterialRenderList Material *material; + CameraInfo *camera_info; + RenderNodeList rn_list; private: @@ -76,6 +79,11 @@ public: void Add(SceneNode *); + void SetCameraInfo(CameraInfo *ci) + { + camera_info=ci; + } + void Clear() { rn_list.Clear(); diff --git a/inc/hgl/graph/MaterialRenderMap.h b/inc/hgl/graph/MaterialRenderMap.h index 5687720f..21400632 100644 --- a/inc/hgl/graph/MaterialRenderMap.h +++ b/inc/hgl/graph/MaterialRenderMap.h @@ -9,10 +9,13 @@ public: MaterialRenderMap()=default; virtual ~MaterialRenderMap()=default; - void Begin() + void Begin(CameraInfo *ci) { for(auto *it:data_list) + { + it->value->SetCameraInfo(ci); it->value->Clear(); + } } void End() diff --git a/inc/hgl/graph/RenderList.h b/inc/hgl/graph/RenderList.h index 8782a422..8f7f3eb9 100644 --- a/inc/hgl/graph/RenderList.h +++ b/inc/hgl/graph/RenderList.h @@ -20,6 +20,8 @@ namespace hgl GPUDevice * device; + CameraInfo * camera_info; ///<相机信息 + uint renderable_count; ///<可渲染对象数量 MaterialRenderMap mrl_map; ///<按材质分类的渲染列表 @@ -32,11 +34,12 @@ namespace hgl RenderList(GPUDevice *); virtual ~RenderList()=default; - virtual bool Expend(SceneNode *); ///<展开场景树到渲染列表 + virtual void SetCamera(CameraInfo *ci){camera_info=ci;} ///<设置相机信息 + virtual bool Expend(SceneNode *); ///<展开场景树到渲染列表 - virtual bool Render(RenderCmdBuffer *); ///<渲染所有对象 + virtual bool Render(RenderCmdBuffer *); ///<渲染所有对象 - virtual void Clear(); ///<彻底清理 + virtual void Clear(); ///<彻底清理 };//class RenderList }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/RenderNode.h b/inc/hgl/graph/RenderNode.h index ce779065..6d09ad13 100644 --- a/inc/hgl/graph/RenderNode.h +++ b/inc/hgl/graph/RenderNode.h @@ -9,12 +9,14 @@ namespace hgl { class Renderable; class MaterialInstance; + class SceneNode; struct RenderNode { - Matrix4f local_to_world; + SceneNode *scene_node; - Renderable *ri; + Vector3f world_position; + float to_camera_distance; }; using RenderNodeList=List; diff --git a/src/SceneGraph/MaterialRenderList.cpp b/src/SceneGraph/MaterialRenderList.cpp index 133d5866..853f5af4 100644 --- a/src/SceneGraph/MaterialRenderList.cpp +++ b/src/SceneGraph/MaterialRenderList.cpp @@ -8,6 +8,7 @@ #include"RenderAssignBuffer.h" #include #include +#include /** * @@ -30,8 +31,8 @@ int Comparator::compare(const hgl::graph::RenderNode &ob { hgl::int64 off; - hgl::graph::Renderable *ri_one=obj_one.ri; - hgl::graph::Renderable *ri_two=obj_two.ri; + hgl::graph::Renderable *ri_one=obj_one.scene_node->GetRenderable(); + hgl::graph::Renderable *ri_two=obj_two.scene_node->GetRenderable(); //比较管线 { @@ -45,29 +46,39 @@ int Comparator::compare(const hgl::graph::RenderNode &ob auto *prim_one=ri_one->GetPrimitive(); auto *prim_two=ri_two->GetPrimitive(); - //比如VDM + //比较VDM + + if(prim_one->GetVDM()) //有VDM { off=prim_one->GetVDM() -prim_two->GetVDM(); if(off) return off; - } - //比较模型 - { - off=prim_one - -prim_two; - - if(off) + //比较模型 { - off=prim_one->GetVertexOffset()-prim_two->GetVertexOffset(); //保证vertex offset小的在前面 + off=prim_one + -prim_two; - return off; + if(off) + { + off=prim_one->GetVertexOffset()-prim_two->GetVertexOffset(); //保证vertex offset小的在前面 + + return off; + } } } - return 0; + //比较距离。。。。。。。。。。。。。。。。。。。。。还不知道这个是正了还是反了,等测出来确认后修改下面的返回值和这里的注释 + + float foff=obj_one.to_camera_distance + -obj_two.to_camera_distance; + + if(foff>0) + return 1; + else + return -1; } VK_NAMESPACE_BEGIN @@ -77,6 +88,8 @@ MaterialRenderList::MaterialRenderList(GPUDevice *d,bool l2w,Material *m) cmd_buf=nullptr; material=m; + camera_info=nullptr; + assign_buffer=new RenderAssignBuffer(device,material); vab_list=new VABList(material->GetVertexInput()->GetCount()); @@ -98,8 +111,14 @@ void MaterialRenderList::Add(SceneNode *sn) { RenderNode rn; - rn.local_to_world =sn->GetLocalToWorldMatrix(); - rn.ri =sn->GetRenderable(); + rn.scene_node =sn; + + rn.world_position =sn->GetWorldPosition(); + + if(camera_info) + rn.to_camera_distance=length(camera_info->pos,rn.world_position); + else + rn.to_camera_distance=0; rn_list.Add(rn); } @@ -177,12 +196,13 @@ void MaterialRenderList::Stat() ri_array.Alloc(count); RenderItem *ri=ri_array.GetData(); + Renderable *ro=rn->scene_node->GetRenderable(); ri_count=1; ri->first_instance=0; ri->instance_count=1; - ri->Set(rn->ri); + ri->Set(ro); last_pipeline =ri->pipeline; last_data_buffer=ri->pdb; @@ -193,9 +213,11 @@ void MaterialRenderList::Stat() for(uint i=1;iri->GetPipeline()) - if(last_data_buffer->Comp(rn->ri->GetDataBuffer())) - if(last_render_data->_Comp(rn->ri->GetRenderData())==0) + ro=rn->scene_node->GetRenderable(); + + if(last_pipeline==ro->GetPipeline()) + if(last_data_buffer->Comp(ro->GetDataBuffer())) + if(last_render_data->_Comp(ro->GetRenderData())==0) { ++ri->instance_count; ++rn; @@ -218,7 +240,7 @@ void MaterialRenderList::Stat() ri->first_instance=i; ri->instance_count=1; - ri->Set(rn->ri); + ri->Set(ro); last_pipeline =ri->pipeline; last_data_buffer=ri->pdb; diff --git a/src/SceneGraph/RenderAssignBuffer.cpp b/src/SceneGraph/RenderAssignBuffer.cpp index e24a0da0..4770f803 100644 --- a/src/SceneGraph/RenderAssignBuffer.cpp +++ b/src/SceneGraph/RenderAssignBuffer.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -72,7 +73,7 @@ void RenderAssignBuffer::StatL2W(const RenderNodeList &rn_list) for(uint i=0;ilocal_to_world; + *l2wp=rn->scene_node->GetLocalToWorldMatrix(); ++l2wp; ++rn; } @@ -115,7 +116,7 @@ void RenderAssignBuffer::StatMI(const RenderNodeList &rn_list) mi_set.PreAlloc(rn_list.GetCount()); for(RenderNode &rn:rn_list) - mi_set.Add(rn.ri->GetMaterialInstance()); + mi_set.Add(rn.scene_node->GetRenderable()->GetMaterialInstance()); if(mi_set.GetCount()>material->GetMIMaxCount()) { @@ -181,7 +182,7 @@ void RenderAssignBuffer::WriteNode(const RenderNodeList &rn_list) for(uint i=0;il2w=i; - adp->mi=mi_set.Find(rn->ri->GetMaterialInstance()); + adp->mi=mi_set.Find(rn->scene_node->GetRenderable()->GetMaterialInstance()); ++adp; ++rn; diff --git a/src/SceneGraph/RenderList.cpp b/src/SceneGraph/RenderList.cpp index 9777ace9..72009c61 100644 --- a/src/SceneGraph/RenderList.cpp +++ b/src/SceneGraph/RenderList.cpp @@ -16,6 +16,8 @@ namespace hgl { device =dev; renderable_count=0; + + camera_info=nullptr; } bool RenderList::ExpendNode(SceneNode *sn) @@ -51,7 +53,7 @@ namespace hgl { if(!device|!sn)return(false); - mrl_map.Begin(); + mrl_map.Begin(camera_info); ExpendNode(sn); mrl_map.End();