Explorar el Código

Fixed redundant copying of vertex buffer data when rendering the UI.

Lasse Öörni hace 12 años
padre
commit
7cd99fe257
Se han modificado 2 ficheros con 35 adiciones y 20 borrados
  1. 28 17
      Engine/UI/UI.cpp
  2. 7 3
      Engine/UI/UI.h

+ 28 - 17
Engine/UI/UI.cpp

@@ -374,16 +374,20 @@ void UI::RenderUpdate()
 void UI::Render()
 void UI::Render()
 {
 {
     PROFILE(RenderUI);
     PROFILE(RenderUI);
+    
+    SetVertexData(vertexBuffer_, vertexData_);
+    SetVertexData(debugVertexBuffer_, debugVertexData_);
+    
     // Render non-modal batches
     // Render non-modal batches
-    Render(batches_, vertexData_, 0, nonModalBatchSize_);
+    Render(vertexBuffer_, batches_, 0, nonModalBatchSize_);
     // Render debug draw
     // Render debug draw
-    Render(debugDrawBatches_, debugDrawVertexData_, 0, debugDrawBatches_.Size());
+    Render(debugVertexBuffer_, debugDrawBatches_, 0, debugDrawBatches_.Size());
     // Render modal batches
     // Render modal batches
-    Render(batches_, vertexData_, nonModalBatchSize_, batches_.Size());
-
+    Render(vertexBuffer_, batches_, nonModalBatchSize_, batches_.Size());
+    
     // Clear the debug draw batches and data
     // Clear the debug draw batches and data
     debugDrawBatches_.Clear();
     debugDrawBatches_.Clear();
-    debugDrawVertexData_.Clear();
+    debugVertexData_.Clear();
 }
 }
 
 
 void UI::DebugDraw(UIElement* element)
 void UI::DebugDraw(UIElement* element)
@@ -391,7 +395,7 @@ void UI::DebugDraw(UIElement* element)
     if (element)
     if (element)
     {
     {
         const IntVector2& rootSize = rootElement_->GetSize();
         const IntVector2& rootSize = rootElement_->GetSize();
-        element->GetDebugDrawBatches(debugDrawBatches_, debugDrawVertexData_, IntRect(0, 0, rootSize.x_, rootSize.y_));
+        element->GetDebugDrawBatches(debugDrawBatches_, debugVertexData_, IntRect(0, 0, rootSize.x_, rootSize.y_));
     }
     }
 }
 }
 
 
@@ -532,6 +536,7 @@ void UI::Initialize()
     alphaTexturePS_ = renderer->GetPixelShader("Basic_AlphaVCol");
     alphaTexturePS_ = renderer->GetPixelShader("Basic_AlphaVCol");
 
 
     vertexBuffer_ = new VertexBuffer(context_);
     vertexBuffer_ = new VertexBuffer(context_);
+    debugVertexBuffer_ = new VertexBuffer(context_);
 
 
     initialized_ = true;
     initialized_ = true;
 
 
@@ -550,21 +555,27 @@ void UI::Update(float timeStep, UIElement* element)
         Update(timeStep, *i);
         Update(timeStep, *i);
 }
 }
 
 
-void UI::Render(const PODVector<UIBatch>& batches, const PODVector<float>& vertexData, unsigned batchStart, unsigned batchSize)
+void UI::SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData)
 {
 {
-    // Engine does not render when window is closed or device is lost
-    assert(graphics_ && graphics_->IsInitialized() && !graphics_->IsDeviceLost());
-
     if (vertexData.Empty())
     if (vertexData.Empty())
         return;
         return;
-
+    
     // Update quad geometry into the vertex buffer
     // Update quad geometry into the vertex buffer
+    // Resize the vertex buffer first if too small or much too large
     unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
     unsigned numVertices = vertexData.Size() / UI_VERTEX_SIZE;
-    // Resize the vertex buffer if too small or much too large
-    if (vertexBuffer_->GetVertexCount() < numVertices || vertexBuffer_->GetVertexCount() > numVertices * 2)
-        vertexBuffer_->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
+    if (dest->GetVertexCount() < numVertices || dest->GetVertexCount() > numVertices * 2)
+        dest->SetSize(numVertices, MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1, true);
+    
+    dest->SetData(&vertexData[0]);
+}
 
 
-    vertexBuffer_->SetData(&vertexData[0]);
+void UI::Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd)
+{
+    // Engine does not render when window is closed or device is lost
+    assert(graphics_ && graphics_->IsInitialized() && !graphics_->IsDeviceLost());
+
+    if (batches.Empty())
+        return;
 
 
     Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
     Vector2 invScreenSize(1.0f / (float)graphics_->GetWidth(), 1.0f / (float)graphics_->GetHeight());
     Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
     Vector2 scale(2.0f * invScreenSize.x_, -2.0f * invScreenSize.y_);
@@ -585,13 +596,13 @@ void UI::Render(const PODVector<UIBatch>& batches, const PODVector<float>& verte
     graphics_->SetDepthWrite(false);
     graphics_->SetDepthWrite(false);
     graphics_->SetStencilTest(false);
     graphics_->SetStencilTest(false);
     graphics_->ResetRenderTargets();
     graphics_->ResetRenderTargets();
-
+    
     ShaderVariation* ps = 0;
     ShaderVariation* ps = 0;
     ShaderVariation* vs = 0;
     ShaderVariation* vs = 0;
 
 
     unsigned alphaFormat = Graphics::GetAlphaFormat();
     unsigned alphaFormat = Graphics::GetAlphaFormat();
 
 
-    for (unsigned i = batchStart; i < batchSize; ++i)
+    for (unsigned i = batchStart; i < batchEnd; ++i)
     {
     {
         const UIBatch& batch = batches[i];
         const UIBatch& batch = batches[i];
         if (batch.vertexStart_ == batch.vertexEnd_)
         if (batch.vertexStart_ == batch.vertexEnd_)

+ 7 - 3
Engine/UI/UI.h

@@ -107,8 +107,10 @@ private:
     void Initialize();
     void Initialize();
     /// Update UI element logic recursively.
     /// Update UI element logic recursively.
     void Update(float timeStep, UIElement* element);
     void Update(float timeStep, UIElement* element);
-    /// Render the batches.
-    void Render(const PODVector<UIBatch>& batches, const PODVector<float>& vertexData, unsigned batchStart, unsigned batchSize);
+    /// Upload UI geometry into a vertex buffer.
+    void SetVertexData(VertexBuffer* dest, const PODVector<float>& vertexData);
+    /// Render UI batches. Geometry must have been uploaded first.
+    void Render(VertexBuffer* buffer, const PODVector<UIBatch>& batches, unsigned batchStart, unsigned batchEnd);
     /// Generate batches from an UI element recursively.
     /// Generate batches from an UI element recursively.
     void GetBatches(UIElement* element, IntRect currentScissor);
     void GetBatches(UIElement* element, IntRect currentScissor);
     /// Return UI element at screen position recursively.
     /// Return UI element at screen position recursively.
@@ -175,9 +177,11 @@ private:
     /// UI rendering batches for debug draw.
     /// UI rendering batches for debug draw.
     PODVector<UIBatch> debugDrawBatches_;
     PODVector<UIBatch> debugDrawBatches_;
     /// UI rendering vertex data for debug draw.
     /// UI rendering vertex data for debug draw.
-    PODVector<float> debugDrawVertexData_;
+    PODVector<float> debugVertexData_;
     /// UI vertex buffer.
     /// UI vertex buffer.
     SharedPtr<VertexBuffer> vertexBuffer_;
     SharedPtr<VertexBuffer> vertexBuffer_;
+    /// UI debug geometry vertex buffer.
+    SharedPtr<VertexBuffer> debugVertexBuffer_;
     /// UI element query vector.
     /// UI element query vector.
     PODVector<UIElement*> tempElements_;
     PODVector<UIElement*> tempElements_;
     /// Clipboard text.
     /// Clipboard text.