[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20048] trunk/blender: BGE performance: second round of scenegraph improvement.

Benoit Bolsee benoit.bolsee at online.be
Mon May 4 00:29:00 CEST 2009


Revision: 20048
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20048
Author:   ben2610
Date:     2009-05-04 00:29:00 +0200 (Mon, 04 May 2009)

Log Message:
-----------
BGE performance: second round of scenegraph improvement.

Use dynamic linked list to handle scenegraph rather than dumb scan
of the whole tree. The performance improvement depends on the fraction
of moving objects. If most objects are static, the speed up is 
considerable. The following table compares the time spent on 
scenegraph before and after this commit on a scene with 10000 objects
in various configuratons:

Scenegraph time (ms)              Before         After
(includes culling)

All objects static,               8.8            1.7  
all visible but small fraction          
in the view frustrum

All objects static,               7,5            0.01
all invisible.

All objects moving,               14.1           8.4
all visible but small fraction
in the view frustrum

This tables shows that static and invisible objects take no CPU at all
for scenegraph and culling. In the general case, this commit will 
speed up the scenegraph between 2x and 5x. Compared to 2.48a, it should
be between 4x and 10x faster. Further speed up is possible by making
the scenegraph cache-friendly.

Next round of performance improvement will be on the rasterizer: use
the same dynamic linked list technique for the mesh slots.

Modified Paths:
--------------
    trunk/blender/projectfiles_vc9/gameengine/scenegraph/SG_SceneGraph.vcproj
    trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp
    trunk/blender/source/gameengine/Ketsji/KX_SG_BoneParentNodeRelationship.cpp
    trunk/blender/source/gameengine/Ketsji/KX_SG_NodeRelationships.cpp
    trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp
    trunk/blender/source/gameengine/Ketsji/KX_Scene.h
    trunk/blender/source/gameengine/SceneGraph/SG_IObject.cpp
    trunk/blender/source/gameengine/SceneGraph/SG_IObject.h
    trunk/blender/source/gameengine/SceneGraph/SG_Node.cpp
    trunk/blender/source/gameengine/SceneGraph/SG_Node.h
    trunk/blender/source/gameengine/SceneGraph/SG_Spatial.cpp
    trunk/blender/source/gameengine/SceneGraph/SG_Spatial.h

Added Paths:
-----------
    trunk/blender/source/gameengine/SceneGraph/SG_DList.h
    trunk/blender/source/gameengine/SceneGraph/SG_QList.h

Modified: trunk/blender/projectfiles_vc9/gameengine/scenegraph/SG_SceneGraph.vcproj
===================================================================
--- trunk/blender/projectfiles_vc9/gameengine/scenegraph/SG_SceneGraph.vcproj	2009-05-03 21:51:57 UTC (rev 20047)
+++ trunk/blender/projectfiles_vc9/gameengine/scenegraph/SG_SceneGraph.vcproj	2009-05-03 22:29:00 UTC (rev 20048)
@@ -4,6 +4,7 @@
 	Version="9,00"
 	Name="SG_SceneGraph"
 	ProjectGUID="{09222F5E-1625-4FF3-A89A-384D16875EE5}"
+	RootNamespace="SG_SceneGraph"
 	TargetFrameworkVersion="131072"
 	>
 	<Platforms>
@@ -510,6 +511,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\..\source\gameengine\SceneGraph\SG_DList.h"
+				>
+			</File>
+			<File
 				RelativePath="..\..\..\source\gameengine\SceneGraph\SG_IObject.h"
 				>
 			</File>
@@ -522,6 +527,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\..\source\gameengine\SceneGraph\SG_QList.h"
+				>
+			</File>
+			<File
 				RelativePath="..\..\..\source\gameengine\SceneGraph\SG_Spatial.h"
 				>
 			</File>

Modified: trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2009-05-03 21:51:57 UTC (rev 20047)
+++ trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2009-05-03 22:29:00 UTC (rev 20048)
@@ -2055,7 +2055,7 @@
 			if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
 			{
 				// blender has an additional 'parentinverse' offset in each object
-				SG_Node* parentinversenode = new SG_Node(NULL,NULL,SG_Callbacks());
+				SG_Node* parentinversenode = new SG_Node(NULL,kxscene,SG_Callbacks(NULL,NULL,NULL,KX_Scene::KX_ScenegraphUpdateFunc,KX_Scene::KX_ScenegraphRescheduleFunc));
 			
 				// define a normal parent relationship for this node.
 				KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();
@@ -2249,7 +2249,7 @@
 							if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
 							{
 								// blender has an additional 'parentinverse' offset in each object
-								SG_Node* parentinversenode = new SG_Node(NULL,NULL,SG_Callbacks());
+								SG_Node* parentinversenode = new SG_Node(NULL,kxscene,SG_Callbacks(NULL,NULL,NULL,KX_Scene::KX_ScenegraphUpdateFunc,KX_Scene::KX_ScenegraphRescheduleFunc));
 							
 								// define a normal parent relationship for this node.
 								KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();

Modified: trunk/blender/source/gameengine/Ketsji/KX_SG_BoneParentNodeRelationship.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_SG_BoneParentNodeRelationship.cpp	2009-05-03 21:51:57 UTC (rev 20047)
+++ trunk/blender/source/gameengine/Ketsji/KX_SG_BoneParentNodeRelationship.cpp	2009-05-03 22:29:00 UTC (rev 20048)
@@ -125,7 +125,9 @@
 	else {
 		child->SetWorldFromLocalTransform();
 	}
-	child->SetModified(false);
+	child->ClearModified();
+	// this node must always be updated, so reschedule it for next time
+	child->ActivateRecheduleUpdateCallback();
 	return valid_parent_transform;
 }
 

Modified: trunk/blender/source/gameengine/Ketsji/KX_SG_NodeRelationships.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_SG_NodeRelationships.cpp	2009-05-03 21:51:57 UTC (rev 20047)
+++ trunk/blender/source/gameengine/Ketsji/KX_SG_NodeRelationships.cpp	2009-05-03 22:29:00 UTC (rev 20048)
@@ -63,7 +63,7 @@
 
 	if (parent==NULL) { /* Simple case */
 		child->SetWorldFromLocalTransform();
-		child->SetModified(false);
+		child->ClearModified();
 		return true; //false;
 	}
 	else {
@@ -75,7 +75,7 @@
 		child->SetWorldScale(p_world_scale * child->GetLocalScale());
 		child->SetWorldOrientation(p_world_rotation * child->GetLocalOrientation());
 		child->SetWorldPosition(p_world_pos + p_world_scale * (p_world_rotation * child->GetLocalPosition()));
-		child->SetModified(false);
+		child->ClearModified();
 		return true;
 	}
 }
@@ -137,7 +137,7 @@
 		child->SetWorldPosition(child->GetLocalPosition());
 	
 	child->SetWorldOrientation(child->GetLocalOrientation());
-	child->SetModified(false);
+	child->ClearModified();
 	return true; //parent != NULL;
 }
 
@@ -259,7 +259,9 @@
 	child->SetWorldScale(child_w_scale);
 	child->SetWorldPosition(child_w_pos);
 	child->SetWorldOrientation(child_w_rotation);
-	child->SetModified(false);
+	child->ClearModified();
+	// this node must always be updated, so reschedule it for next time
+	child->ActivateRecheduleUpdateCallback();
 	
 	return true; //parent != NULL;
 }

Modified: trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp	2009-05-03 21:51:57 UTC (rev 20047)
+++ trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp	2009-05-03 22:29:00 UTC (rev 20048)
@@ -111,8 +111,23 @@
 	return NULL;
 };
 
-SG_Callbacks KX_Scene::m_callbacks = SG_Callbacks(KX_SceneReplicationFunc,KX_SceneDestructionFunc,KX_GameObject::UpdateTransformFunc);
+bool KX_Scene::KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene)
+{
+	return ((SG_Node*)node)->Schedule(((KX_Scene*)scene)->m_sghead);
+}
 
+bool KX_Scene::KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene)
+{
+	return ((SG_Node*)node)->Reschedule(((KX_Scene*)scene)->m_sghead);
+}
+
+SG_Callbacks KX_Scene::m_callbacks = SG_Callbacks(
+	KX_SceneReplicationFunc,
+	KX_SceneDestructionFunc,
+	KX_GameObject::UpdateTransformFunc,
+	KX_Scene::KX_ScenegraphUpdateFunc,
+	KX_Scene::KX_ScenegraphRescheduleFunc);
+
 // temporarily var until there is a button in the userinterface
 // (defined in KX_PythonInit.cpp)
 extern bool gUseVisibilityTemp;
@@ -1473,17 +1488,30 @@
   */
 void KX_Scene::UpdateParents(double curtime)
 {
-//	int numrootobjects = GetRootParentList()->GetCount();
+	// we use the SG dynamic list
+	SG_Node* node;
 
-	for (int i=0; i<GetRootParentList()->GetCount(); i++)
+	while ((node = SG_Node::GetNextScheduled(m_sghead)) != NULL)
 	{
-		KX_GameObject* parentobj = (KX_GameObject*)GetRootParentList()->GetValue(i);
-		parentobj->NodeUpdateGS(curtime);
+		node->UpdateWorldData(curtime);
 	}
+
+	//for (int i=0; i<GetRootParentList()->GetCount(); i++)
+	//{
+	//	KX_GameObject* parentobj = (KX_GameObject*)GetRootParentList()->GetValue(i);
+	//	parentobj->NodeUpdateGS(curtime);
+	//}
+
+	// the list must be empty here
+	assert(m_sghead.Empty());
+	// some nodes may be ready for reschedule, move them to schedule list for next time
+	while ((node = SG_Node::GetNextRescheduled(m_sghead)) != NULL)
+	{
+		node->Schedule(m_sghead);
+	}
 }
 
 
-
 RAS_MaterialBucket* KX_Scene::FindBucket(class RAS_IPolyMaterial* polymat, bool &bucketCreated)
 {
 	return m_bucketmanager->FindBucket(polymat, bucketCreated);

Modified: trunk/blender/source/gameengine/Ketsji/KX_Scene.h
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_Scene.h	2009-05-03 21:51:57 UTC (rev 20047)
+++ trunk/blender/source/gameengine/Ketsji/KX_Scene.h	2009-05-03 22:29:00 UTC (rev 20048)
@@ -118,6 +118,11 @@
 	CListValue*			m_parentlist; // all 'root' parents
 	CListValue*			m_lightlist;
 	CListValue*			m_inactivelist;	// all objects that are not in the active layer
+	
+	SG_QList			m_sghead;		// list of nodes that needs scenegraph update
+										// the Dlist is not object that must be updated
+										// the Qlist is for objects that needs to be rescheduled
+										// for updates after udpate is over (slow parent, bone parent)
 
 	/**
 	 *  The tree of objects in the scene.
@@ -307,6 +312,8 @@
 	/**
 	 * Update all transforms according to the scenegraph.
 	 */
+	static bool KX_ScenegraphUpdateFunc(SG_IObject* node,void* gameobj,void* scene);
+	static bool KX_ScenegraphRescheduleFunc(SG_IObject* node,void* gameobj,void* scene);
 	void UpdateParents(double curtime);
 	void DupliGroupRecurse(CValue* gameobj, int level);
 	bool IsObjectInGroup(CValue* gameobj)

Added: trunk/blender/source/gameengine/SceneGraph/SG_DList.h
===================================================================
--- trunk/blender/source/gameengine/SceneGraph/SG_DList.h	                        (rev 0)
+++ trunk/blender/source/gameengine/SceneGraph/SG_DList.h	2009-05-03 22:29:00 UTC (rev 20048)
@@ -0,0 +1,147 @@
+/**
+ * $Id$
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+#ifndef __SG_DLIST
+#define __SG_DLIST
+
+#include <stdlib.h>
+
+/**
+ * Double circular linked list
+ */
+class SG_DList
+{
+protected :
+	SG_DList* m_flink;
+	SG_DList* m_blink;
+
+public:
+	template<typename T> class iterator
+	{
+	private:
+		SG_DList&	m_head;
+		T*			m_current;
+	public:
+		typedef iterator<T> _myT;
+		iterator(SG_DList& head) : m_head(head), m_current(NULL) {}
+		~iterator() {}
+
+		void begin()
+		{
+			m_current = (T*)m_head.Peek();
+			if (m_current == (T*)m_head.Self())
+			{
+				m_current = NULL;
+			}
+		}
+		bool end()
+		{
+			return (NULL == m_current);
+		}
+		T* operator*()
+		{
+			return m_current;
+		}
+		_myT& operator++()
+		{
+			assert(m_current);
+			m_current = (T*)m_current->Peek();
+			if (m_current == (T*)m_head.Self())
+			{
+				m_current = NULL;
+			}
+			return *this;
+		}
+	};
+
+    SG_DList() 
+    { 
+        m_flink = m_blink = this; 
+    }
+    virtual ~SG_DList() 
+    {
+		Delink();
+    }
+

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list