瀏覽代碼

Reorder functions in DrawableProxy2D.

aster2013 11 年之前
父節點
當前提交
077df45dc0
共有 3 個文件被更改,包括 104 次插入108 次删除
  1. 4 6
      Source/Engine/Urho2D/Drawable2D.h
  2. 92 92
      Source/Engine/Urho2D/DrawableProxy2D.cpp
  3. 8 10
      Source/Engine/Urho2D/DrawableProxy2D.h

+ 4 - 6
Source/Engine/Urho2D/Drawable2D.h

@@ -130,15 +130,13 @@ protected:
 
 
 inline bool CompareDrawable2Ds(Drawable2D* lhs, Drawable2D* rhs)
 inline bool CompareDrawable2Ds(Drawable2D* lhs, Drawable2D* rhs)
 {
 {
-    if (lhs->GetLayer() == rhs->GetLayer())
-    {
-        if (lhs->GetOrderInLayer() == rhs->GetOrderInLayer())
-            return lhs->GetID() < rhs->GetID();
+    if (lhs->GetLayer() != rhs->GetLayer())
+        return lhs->GetLayer() < rhs->GetLayer();
 
 
+    if (lhs->GetOrderInLayer() != rhs->GetOrderInLayer())
         return lhs->GetOrderInLayer() < rhs->GetOrderInLayer();
         return lhs->GetOrderInLayer() < rhs->GetOrderInLayer();
-    }
 
 
-    return lhs->GetLayer() < rhs->GetLayer();
+    return lhs->GetID() < rhs->GetID();
 }
 }
 
 
 }
 }

+ 92 - 92
Source/Engine/Urho2D/DrawableProxy2D.cpp

@@ -45,9 +45,9 @@ DrawableProxy2D::DrawableProxy2D(Context* context) :
     Drawable(context, DRAWABLE_GEOMETRY),
     Drawable(context, DRAWABLE_GEOMETRY),
     indexBuffer_(new IndexBuffer(context_)),
     indexBuffer_(new IndexBuffer(context_)),
     vertexBuffer_(new VertexBuffer(context_)),
     vertexBuffer_(new VertexBuffer(context_)),
-    vertexCount_(0),
+    orderDirty_(true),
     indexCount_(0),
     indexCount_(0),
-    orderDirty_(true)
+    vertexCount_(0)
 {
 {
     SubscribeToEvent(E_BEGINVIEWUPDATE, HANDLER(DrawableProxy2D, HandleBeginViewUpdate));
     SubscribeToEvent(E_BEGINVIEWUPDATE, HANDLER(DrawableProxy2D, HandleBeginViewUpdate));
 }
 }
@@ -61,94 +61,6 @@ void DrawableProxy2D::RegisterObject(Context* context)
     context->RegisterFactory<DrawableProxy2D>();
     context->RegisterFactory<DrawableProxy2D>();
 }
 }
 
 
-void DrawableProxy2D::HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData)
-{
-    using namespace BeginViewUpdate;
-    
-    // Check that we are updating the correct scene
-    if (GetScene() != eventData[P_SCENE].GetPtr())
-        return;
-    
-    PROFILE(UpdateDrawableProxy2D);
-    
-    if (orderDirty_)
-    {
-        Sort(drawables_.Begin(), drawables_.End(), CompareDrawable2Ds);
-        orderDirty_ = false;
-    }
-    
-    vertexCount_ = 0;
-    
-    if (drawablesVisible_.Size() != drawables_.Size())
-        drawablesVisible_.Resize(drawables_.Size());
-    
-    /// \todo We could add frustum culling, but that would be problematic if we have several viewports. Right now all Drawable2D's
-    /// are always submitted for rendering
-    for (unsigned i = 0; i < drawables_.Size(); ++i)
-    {
-        Material* usedMaterial = drawables_[i]->GetUsedMaterial();
-        const Vector<Vertex2D>& vertices = drawables_[i]->GetVertices();
-        if (drawables_[i]->GetUsedMaterial() && vertices.Size())
-        {
-            drawablesVisible_[i] = true;
-            vertexCount_ += vertices.Size();
-        }
-        else
-            drawablesVisible_[i] = false;
-    }
-    
-    indexCount_ = vertexCount_ / 4 * 6;
-    
-    // Go through the drawables to form geometries & batches, but upload the actual vertex data later
-    materials_.Clear();
-    
-    Material* material = 0;
-    unsigned iStart = 0;
-    unsigned iCount = 0;
-    unsigned vStart = 0;
-    unsigned vCount = 0;
-
-    for (unsigned d = 0; d < drawables_.Size(); ++d)
-    {
-        if (!drawablesVisible_[d])
-            continue;
-        
-        Material* usedMaterial = drawables_[d]->GetUsedMaterial();
-        const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
-
-        if (material != usedMaterial)
-        {
-            if (material)
-            {
-                AddBatch(material, iStart, iCount, vStart, vCount);
-                iStart += iCount;
-                iCount = 0;
-                vStart += vCount;
-                vCount = 0;
-            }
-
-            material = usedMaterial;
-        }
-
-        iCount += vertices.Size() / 4 * 6;
-        vCount += vertices.Size();
-    }
-
-    if (material)
-        AddBatch(material, iStart, iCount, vStart, vCount);
-
-    // Now the amount of batches is known. Build the part of source batches that are sensitive to threading issues
-    // (material & geometry pointers)
-    unsigned count = materials_.Size();
-    batches_.Resize(count);
-
-    for (unsigned i = 0; i < count; ++i)
-    {
-        batches_[i].material_ = materials_[i];
-        batches_[i].geometry_ = geometries_[i];
-    }
-}
-
 void DrawableProxy2D::UpdateBatches(const FrameInfo& frame)
 void DrawableProxy2D::UpdateBatches(const FrameInfo& frame)
 {
 {
     unsigned count = batches_.Size();
     unsigned count = batches_.Size();
@@ -230,13 +142,13 @@ void DrawableProxy2D::UpdateGeometry(const FrameInfo& frame)
             {
             {
                 if (!drawablesVisible_[d])
                 if (!drawablesVisible_[d])
                     continue;
                     continue;
-                
+
                 const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
                 const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
                 for (unsigned i = 0; i < vertices.Size(); ++i)
                 for (unsigned i = 0; i < vertices.Size(); ++i)
                     dest[i] = vertices[i];
                     dest[i] = vertices[i];
                 dest += vertices.Size();
                 dest += vertices.Size();
             }
             }
-            
+
             vertexBuffer_->Unlock();
             vertexBuffer_->Unlock();
         }
         }
         else
         else
@@ -277,6 +189,94 @@ void DrawableProxy2D::OnWorldBoundingBoxUpdate()
     worldBoundingBox_ = boundingBox_;
     worldBoundingBox_ = boundingBox_;
 }
 }
 
 
+void DrawableProxy2D::HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData)
+{
+    using namespace BeginViewUpdate;
+
+    // Check that we are updating the correct scene
+    if (GetScene() != eventData[P_SCENE].GetPtr())
+        return;
+
+    PROFILE(UpdateDrawableProxy2D);
+
+    if (orderDirty_)
+    {
+        Sort(drawables_.Begin(), drawables_.End(), CompareDrawable2Ds);
+        orderDirty_ = false;
+    }
+
+    vertexCount_ = 0;
+
+    if (drawablesVisible_.Size() != drawables_.Size())
+        drawablesVisible_.Resize(drawables_.Size());
+
+    /// \todo We could add frustum culling, but that would be problematic if we have several viewports. Right now all Drawable2D's
+    /// are always submitted for rendering
+    for (unsigned i = 0; i < drawables_.Size(); ++i)
+    {
+        Material* usedMaterial = drawables_[i]->GetUsedMaterial();
+        const Vector<Vertex2D>& vertices = drawables_[i]->GetVertices();
+        if (drawables_[i]->GetUsedMaterial() && vertices.Size())
+        {
+            drawablesVisible_[i] = true;
+            vertexCount_ += vertices.Size();
+        }
+        else
+            drawablesVisible_[i] = false;
+    }
+
+    indexCount_ = vertexCount_ / 4 * 6;
+
+    // Go through the drawables to form geometries & batches, but upload the actual vertex data later
+    materials_.Clear();
+
+    Material* material = 0;
+    unsigned iStart = 0;
+    unsigned iCount = 0;
+    unsigned vStart = 0;
+    unsigned vCount = 0;
+
+    for (unsigned d = 0; d < drawables_.Size(); ++d)
+    {
+        if (!drawablesVisible_[d])
+            continue;
+
+        Material* usedMaterial = drawables_[d]->GetUsedMaterial();
+        const Vector<Vertex2D>& vertices = drawables_[d]->GetVertices();
+
+        if (material != usedMaterial)
+        {
+            if (material)
+            {
+                AddBatch(material, iStart, iCount, vStart, vCount);
+                iStart += iCount;
+                iCount = 0;
+                vStart += vCount;
+                vCount = 0;
+            }
+
+            material = usedMaterial;
+        }
+
+        iCount += vertices.Size() / 4 * 6;
+        vCount += vertices.Size();
+    }
+
+    if (material)
+        AddBatch(material, iStart, iCount, vStart, vCount);
+
+    // Now the amount of batches is known. Build the part of source batches that are sensitive to threading issues
+    // (material & geometry pointers)
+    unsigned count = materials_.Size();
+    batches_.Resize(count);
+
+    for (unsigned i = 0; i < count; ++i)
+    {
+        batches_[i].material_ = materials_[i];
+        batches_[i].geometry_ = geometries_[i];
+    }
+}
+
 void DrawableProxy2D::AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount)
 void DrawableProxy2D::AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount)
 {
 {
     if (!material || indexCount == 0 || vertexCount == 0)
     if (!material || indexCount == 0 || vertexCount == 0)

+ 8 - 10
Source/Engine/Urho2D/DrawableProxy2D.h

@@ -58,16 +58,14 @@ public:
     /// Mark order dirty.
     /// Mark order dirty.
     void MarkOrderDirty() { orderDirty_ = true; }
     void MarkOrderDirty() { orderDirty_ = true; }
 
 
-protected:
+private:
     /// Recalculate the world-space bounding box.
     /// Recalculate the world-space bounding box.
     virtual void OnWorldBoundingBoxUpdate();
     virtual void OnWorldBoundingBoxUpdate();
+    /// Handle view update begin event. Determine Drawable2D's and their batches here.
+    void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
     /// Add batch.
     /// Add batch.
     void AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount);
     void AddBatch(Material* material, unsigned indexStart, unsigned indexCount, unsigned vertexStart, unsigned vertexCount);
 
 
-private:
-    /// Handle view update begin event. Determine Drawable2D's and their batches here.
-    void HandleBeginViewUpdate(StringHash eventType, VariantMap& eventData);
-    
     /// Index buffer.
     /// Index buffer.
     SharedPtr<IndexBuffer> indexBuffer_;
     SharedPtr<IndexBuffer> indexBuffer_;
     /// Vertex buffer.
     /// Vertex buffer.
@@ -77,15 +75,15 @@ private:
     /// Geometries.
     /// Geometries.
     Vector<SharedPtr<Geometry> > geometries_;
     Vector<SharedPtr<Geometry> > geometries_;
     /// Drawables.
     /// Drawables.
-    PODVector<Drawable2D* > drawables_;
+    PODVector<Drawable2D*> drawables_;
+    /// Order dirty.
+    bool orderDirty_;
     /// Drawable visibility results.
     /// Drawable visibility results.
     PODVector<bool> drawablesVisible_;
     PODVector<bool> drawablesVisible_;
-    /// Total vertex count for the current frame.
-    unsigned vertexCount_;
     /// Total index count for the current frame.
     /// Total index count for the current frame.
     unsigned indexCount_;
     unsigned indexCount_;
-    /// Order dirty.
-    bool orderDirty_;
+    /// Total vertex count for the current frame.
+    unsigned vertexCount_;
 };
 };
 
 
 }
 }