Преглед изворни кода

Make the include path absolute in all script examples so that ScriptCompiler can compile them properly.
Renamed Octree::Resize() to Octree::SetSize()
When destroying a scene, destroy root-level components first so that child node components are detached from their internal structures and can be destroyed faster in turn.
Use raw pointers instead of weak pointers in Octree update/reinsertion lists for some performance gain.

Lasse Öörni пре 12 година
родитељ
комит
be7636f823

+ 1 - 1
Bin/Data/Scripts/01_HelloWorld.as

@@ -4,7 +4,7 @@
 //     - Adding a Text element to the graphical user interface;
 //     - Subscribing to and handling of update events;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 void Start()
 {

+ 1 - 1
Bin/Data/Scripts/02_HelloGUI.as

@@ -4,7 +4,7 @@
 //     - Loading UI style from XML and applying it to controls;
 //     - Handling of global and per-control events;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Window@ window;
 

+ 1 - 1
Bin/Data/Scripts/03_Sprites.as

@@ -4,7 +4,7 @@
 //     - Storing custom data (sprite velocity) inside UI elements;
 //     - Handling frame update events in which the sprites are moved;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 // Number of sprites to draw
 const uint NUM_SPRITES = 100;

+ 1 - 1
Bin/Data/Scripts/04_StaticScene.as

@@ -4,7 +4,7 @@
 //     - Displaying the scene using the Renderer subsystem;
 //     - Handling keyboard and mouse input to move a freelook camera;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/05_AnimatingScene.as

@@ -4,7 +4,7 @@
 //     - Controlling scene ambience with the Zone component;
 //     - Attaching a light to an object (the camera);
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/06_SkeletalAnimation.as

@@ -6,7 +6,7 @@
 //       over a large area (typically used in outdoor scenes for shadows cast by sunlight);
 //     - Displaying renderer debug geometry;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/07_Billboards.as

@@ -4,7 +4,7 @@
 //     - Parenting scene nodes to allow more intuitive creation of groups of objects;
 //     - Examining rendering performance with a somewhat large object and light count;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/08_Decals.as

@@ -5,7 +5,7 @@
 //     - Marking suitable (large) objects as occluders for occlusion culling;
 //     - Displaying renderer debug geometry to see the effect of occlusion;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/09_MultipleViewports.as

@@ -3,7 +3,7 @@
 //     - Setting up two viewports with two separate cameras;
 //     - Adding post processing effects to a viewport's render path and toggling them;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/10_RenderToTexture.as

@@ -3,7 +3,7 @@
 //     - Creating two 3D scenes and rendering the other into a texture;
 //     - Creating rendertarget textures and materials programmatically;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Scene@ rttScene_;

+ 1 - 1
Bin/Data/Scripts/11_Physics.as

@@ -4,7 +4,7 @@
 //     - Displaying physics debug geometry;
 //     - Saving a scene to a file and loading it to restore a previous state;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/12_PhysicsStressTest.as

@@ -4,7 +4,7 @@
 //     - Using triangle meshes for collision;
 //     - Optimizing physics simulation by leaving out collision event signaling;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/13_Ragdolls.as

@@ -4,7 +4,7 @@
 //     - Moving an AnimatedModel's bones with physics and connecting them with constraints;
 //     - Using rolling friction to stop rolling objects from moving infinitely;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/14_SoundEffects.as

@@ -3,7 +3,7 @@
 //     - Playing sound effects and music;
 //     - Controlling sound and music master volume;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 

+ 1 - 1
Bin/Data/Scripts/15_Navigation.as

@@ -5,7 +5,7 @@
 //     - Rebuilding the navigation mesh partially when adding or removing objects;
 //     - Visualizing custom debug geometry;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 1 - 1
Bin/Data/Scripts/16_Chat.as

@@ -3,7 +3,7 @@
 //     - Starting up a network server or connecting to it;
 //     - Implementing simple chat functionality with network messages;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 // Identifier for the chat network messages
 const int MSG_CHAT = 32;

+ 1 - 1
Bin/Data/Scripts/17_SceneReplication.as

@@ -5,7 +5,7 @@
 //       where the authoritative simulation happens;
 //     - Controlling a physics object's movement by applying forces;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 // UDP port we will use
 const uint SERVER_PORT = 2345;

+ 1 - 1
Bin/Data/Scripts/18_CharacterDemo.as

@@ -5,7 +5,7 @@
 //     - Implementing 1st and 3rd person cameras, using raycasts to avoid the 3rd person camera
 //       clipping into scenery
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 const int CTRL_FORWARD = 1;
 const int CTRL_BACK = 2;

+ 1 - 1
Bin/Data/Scripts/19_VehicleDemo.as

@@ -3,7 +3,7 @@
 //     - Creating a heightmap terrain with collision;
 //     - Constructing a physical vehicle with rigid bodies for the hull and the wheels, joined with constraints;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 const int CTRL_FORWARD = 1;
 const int CTRL_BACK = 2;

+ 1 - 1
Bin/Data/Scripts/20_HugeObjectCount.as

@@ -4,7 +4,7 @@
 //     - Competing with http://yosoygames.com.ar/wp/2013/07/ogre-2-0-is-up-to-3x-faster/ :)
 //     - Allowing examination of performance hotspots in the rendering code;
 
-#include "Utilities/Sample.as"
+#include "Scripts/Utilities/Sample.as"
 
 Scene@ scene_;
 Node@ cameraNode;

+ 2 - 3
Bin/Data/Scripts/Editor/EditorScene.as

@@ -68,9 +68,8 @@ bool ResetScene()
 
     // Create a scene with default values, these will be overridden when loading scenes
     editorScene.Clear();
-    Octree@ octree = editorScene.CreateComponent("Octree");
-    PhysicsWorld@ physicsWorld = editorScene.CreateComponent("PhysicsWorld");
-    octree.Resize(BoundingBox(-1000.0, 1000.0), 8);
+    editorScene.CreateComponent("Octree");
+    editorScene.CreateComponent("PhysicsWorld");
     editorScene.CreateComponent("DebugRenderer");
 
     sceneModified = false;

+ 8 - 0
Source/Engine/Graphics/Drawable.cpp

@@ -394,7 +394,15 @@ void Drawable::AddToOctree()
 void Drawable::RemoveFromOctree()
 {
     if (octant_)
+    {
+        Octree* octree = octant_->GetRoot();
+        if (updateQueued_)
+            octree->CancelUpdate(this);
+        if (reinsertionQueued_)
+            octree->CancelReinsertion(this);
+        
         octant_->RemoveDrawable(this);
+    }
 }
 
 }

+ 24 - 13
Source/Engine/Graphics/Octree.cpp

@@ -66,8 +66,8 @@ void RaycastDrawablesWork(const WorkItem* item, unsigned threadIndex)
 void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex)
 {
     const FrameInfo& frame = *(reinterpret_cast<FrameInfo*>(item->aux_));
-    WeakPtr<Drawable>* start = reinterpret_cast<WeakPtr<Drawable>*>(item->start_);
-    WeakPtr<Drawable>* end = reinterpret_cast<WeakPtr<Drawable>*>(item->end_);
+    Drawable** start = reinterpret_cast<Drawable**>(item->start_);
+    Drawable** end = reinterpret_cast<Drawable**>(item->end_);
 
     while (start != end)
     {
@@ -343,6 +343,8 @@ Octree::Octree(Context* context) :
 Octree::~Octree()
 {
     // Reset root pointer from all child octants now so that they do not move their drawables to root
+    drawableUpdates_.Clear();
+    drawableReinsertions_.Clear();
     ResetRoot();
 }
 
@@ -362,7 +364,7 @@ void Octree::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
 {
     // If any of the (size) attributes change, resize the octree
     Component::OnSetAttribute(attr, src);
-    Resize(worldBoundingBox_, numLevels_);
+    SetSize(worldBoundingBox_, numLevels_);
 }
 
 void Octree::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
@@ -375,7 +377,7 @@ void Octree::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
     }
 }
 
-void Octree::Resize(const BoundingBox& box, unsigned numLevels)
+void Octree::SetSize(const BoundingBox& box, unsigned numLevels)
 {
     PROFILE(ResizeOctree);
 
@@ -530,7 +532,7 @@ void Octree::RaycastSingle(RayOctreeQuery& query) const
 
 void Octree::QueueUpdate(Drawable* drawable)
 {
-    drawableUpdates_.Push(WeakPtr<Drawable>(drawable));
+    drawableUpdates_.Push(drawable);
     drawable->updateQueued_ = true;
 }
 
@@ -540,14 +542,26 @@ void Octree::QueueReinsertion(Drawable* drawable)
     if (scene && scene->IsThreadedUpdate())
     {
         MutexLock lock(octreeMutex_);
-        drawableReinsertions_.Push(WeakPtr<Drawable>(drawable));
+        drawableReinsertions_.Push(drawable);
     }
     else
-        drawableReinsertions_.Push(WeakPtr<Drawable>(drawable));
+        drawableReinsertions_.Push(drawable);
 
     drawable->reinsertionQueued_ = true;
 }
 
+void Octree::CancelUpdate(Drawable* drawable)
+{
+    drawableUpdates_.Remove(drawable);
+    drawable->updateQueued_ = false;
+}
+
+void Octree::CancelReinsertion(Drawable* drawable)
+{
+    drawableReinsertions_.Remove(drawable);
+    drawable->reinsertionQueued_ = false;
+}
+
 void Octree::DrawDebugGeometry(bool depthTest)
 {
     DebugRenderer* debug = GetComponent<DebugRenderer>();
@@ -572,10 +586,10 @@ void Octree::UpdateDrawables(const FrameInfo& frame)
     item.workFunction_ = UpdateDrawablesWork;
     item.aux_ = const_cast<FrameInfo*>(&frame);
 
-    Vector<WeakPtr<Drawable> >::Iterator start = drawableUpdates_.Begin();
+    PODVector<Drawable*>::Iterator start = drawableUpdates_.Begin();
     while (start != drawableUpdates_.End())
     {
-        Vector<WeakPtr<Drawable> >::Iterator end = drawableUpdates_.End();
+        PODVector<Drawable*>::Iterator end = drawableUpdates_.End();
         if (end - start > DRAWABLES_PER_WORK_ITEM)
             end = start + DRAWABLES_PER_WORK_ITEM;
 
@@ -600,12 +614,9 @@ void Octree::ReinsertDrawables(const FrameInfo& frame)
 
     PROFILE(ReinsertToOctree);
 
-    for (Vector<WeakPtr<Drawable> >::Iterator i = drawableReinsertions_.Begin(); i != drawableReinsertions_.End(); ++i)
+    for (PODVector<Drawable*>::Iterator i = drawableReinsertions_.Begin(); i != drawableReinsertions_.End(); ++i)
     {
         Drawable* drawable = *i;
-        if (!drawable)
-            continue;
-
         drawable->reinsertionQueued_ = false;
         Octant* octant = drawable->GetOctant();
         const BoundingBox& box = drawable->GetWorldBoundingBox();

+ 8 - 4
Source/Engine/Graphics/Octree.h

@@ -170,8 +170,8 @@ public:
     /// Visualize the component as debug geometry.
     virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
     
-    /// Resize octree. If octree is not empty, drawable objects will be temporarily moved to the root.
-    void Resize(const BoundingBox& box, unsigned numLevels);
+    /// Set size and maximum subdivision levels. If octree is not empty, drawable objects will be temporarily moved to the root.
+    void SetSize(const BoundingBox& box, unsigned numLevels);
     /// Update and reinsert drawable objects.
     void Update(const FrameInfo& frame);
     /// Add a drawable manually.
@@ -192,6 +192,10 @@ public:
     void QueueUpdate(Drawable* drawable);
     /// Mark drawable object as requiring a reinsertion. Is thread-safe.
     void QueueReinsertion(Drawable* drawable);
+    /// Cancel drawable object's update.
+    void CancelUpdate(Drawable* drawable);
+    /// Cancel drawable object's reinsertion.
+    void CancelReinsertion(Drawable* drawable);
     /// Visualize the component as debug geometry.
     void DrawDebugGeometry(bool depthTest);
     
@@ -202,9 +206,9 @@ private:
     void ReinsertDrawables(const FrameInfo& frame);
     
     /// Drawable objects that require update.
-    Vector<WeakPtr<Drawable> > drawableUpdates_;
+    PODVector<Drawable*> drawableUpdates_;
     /// Drawable objects that require reinsertion.
-    Vector<WeakPtr<Drawable> > drawableReinsertions_;
+    PODVector<Drawable*> drawableReinsertions_;
     /// Mutex for octree reinsertions.
     Mutex octreeMutex_;
     /// Current threaded ray query.

+ 4 - 1
Source/Engine/Scene/Scene.cpp

@@ -73,7 +73,10 @@ Scene::Scene(Context* context) :
 
 Scene::~Scene()
 {
-    Clear();
+    // Remove root-level components first, so that scene subsystems such as the octree destroy themselves. This will speed up
+    // the removal of child nodes' components
+    RemoveAllComponents();
+    RemoveAllChildren();
     
     // Remove scene reference and owner from all nodes that still exist
     for (HashMap<unsigned, Node*>::Iterator i = replicatedNodes_.Begin(); i != replicatedNodes_.End(); ++i)

+ 1 - 1
Source/Engine/Script/GraphicsAPI.cpp

@@ -1411,7 +1411,7 @@ static void RegisterOctree(asIScriptEngine* engine)
     engine->RegisterObjectProperty("RayQueryResult", "uint subObject", offsetof(RayQueryResult, subObject_));
     
     RegisterComponent<Octree>(engine, "Octree");
-    engine->RegisterObjectMethod("Octree", "void Resize(const BoundingBox&in, uint)", asMETHOD(Octree, Resize), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Octree", "void SetSize(const BoundingBox&in, uint)", asMETHOD(Octree, SetSize), asCALL_THISCALL);
     engine->RegisterObjectMethod("Octree", "void DrawDebugGeometry(bool) const", asMETHODPR(Octree, DrawDebugGeometry, (bool), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Octree", "void AddManualDrawable(Drawable@+)", asMETHOD(Octree, AddManualDrawable), asCALL_THISCALL);
     engine->RegisterObjectMethod("Octree", "void RemoveManualDrawable(Drawable@+)", asMETHOD(Octree, RemoveManualDrawable), asCALL_THISCALL);

+ 1 - 1
Source/Extras/LuaScript/pkgs/Graphics/Octree.pkg

@@ -2,7 +2,7 @@ $#include "Octree.h"
 
 class Octree : public Component
 {    
-    void Resize(const BoundingBox& box, unsigned numLevels);
+    void SetSize(const BoundingBox& box, unsigned numLevels);
     void Update(const FrameInfo& frame);
     void AddManualDrawable(Drawable* drawable);
     void RemoveManualDrawable(Drawable* drawable);