Remove old pipeline caches
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.cpp
index 3bea0e5..486eb2f 100644 (file)
@@ -146,13 +146,42 @@ inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
   return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
 }
 
+/**
+ * @brief Store latest bound RenderGeometry, and help that we can skip duplicated vertex attributes bind.
+ *
+ * @param[in] geometry Current geometry to be used, or nullptr if render finished
+ * @return True if we can reuse latest bound vertex attributes. False otherwise.
+ */
+inline bool ReuseLatestBoundVertexAttributes(const Render::Geometry* geometry)
+{
+  static const Render::Geometry* gLatestVertexBoundGeometry = nullptr;
+  if(gLatestVertexBoundGeometry == geometry)
+  {
+    return true;
+  }
+  gLatestVertexBoundGeometry = geometry;
+  return false;
+}
+
 } // namespace
 
 namespace Render
 {
 namespace
 {
-MemoryPoolObjectAllocator<Renderer> gRenderRendererMemoryPool;
+MemoryPoolObjectAllocator<Renderer>& GetRenderRendererMemoryPool()
+{
+  static MemoryPoolObjectAllocator<Renderer> gRenderRendererMemoryPool;
+  return gRenderRendererMemoryPool;
+}
+} // namespace
+
+void Renderer::PrepareCommandBuffer()
+{
+  // Reset latest geometry informations, So we can bind the first of geometry.
+  ReuseLatestBoundVertexAttributes(nullptr);
+
+  // todo : Fill here as many caches as we can store for reduce the number of command buffers
 }
 
 RendererKey Renderer::NewKey(SceneGraph::RenderDataProvider* dataProvider,
@@ -166,8 +195,8 @@ RendererKey Renderer::NewKey(SceneGraph::RenderDataProvider* dataProvider,
                              DepthFunction::Type             depthFunction,
                              StencilParameters&              stencilParameters)
 {
-  void* ptr = gRenderRendererMemoryPool.AllocateRawThreadSafe();
-  auto  key = gRenderRendererMemoryPool.GetKeyFromPtr(static_cast<Renderer*>(ptr));
+  void* ptr = GetRenderRendererMemoryPool().AllocateRawThreadSafe();
+  auto  key = GetRenderRendererMemoryPool().GetKeyFromPtr(static_cast<Renderer*>(ptr));
 
   // Use placement new to construct renderer.
   new(ptr) Renderer(dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode, depthFunction, stencilParameters);
@@ -216,16 +245,20 @@ void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache
   mPipelineCache        = &pipelineCache;
 }
 
-Renderer::~Renderer() = default;
+Renderer::~Renderer()
+{
+  // Reset old pipeline
+  mPipelineCache->ResetPipeline(mPipeline);
+}
 
 void Renderer::operator delete(void* ptr)
 {
-  gRenderRendererMemoryPool.FreeThreadSafe(static_cast<Renderer*>(ptr));
+  GetRenderRendererMemoryPool().FreeThreadSafe(static_cast<Renderer*>(ptr));
 }
 
 Renderer* Renderer::Get(RendererKey::KeyType rendererKey)
 {
-  return gRenderRendererMemoryPool.GetPtrFromKey(rendererKey);
+  return GetRenderRendererMemoryPool().GetPtrFromKey(rendererKey);
 }
 
 void Renderer::SetGeometry(Render::Geometry* geometry)
@@ -487,7 +520,7 @@ bool Renderer::Render(Graphics::CommandBuffer&                             comma
     mRenderCallbackInput->size       = size;
     mRenderCallbackInput->projection = projectionMatrix;
 
-    MatrixUtils::Multiply(mRenderCallbackInput->mvp, modelViewMatrix, projectionMatrix);
+    MatrixUtils::MultiplyProjectionMatrix(mRenderCallbackInput->mvp, modelViewMatrix, projectionMatrix);
 
     // submit draw
     commandBuffer.DrawNative(&info);
@@ -571,16 +604,25 @@ bool Renderer::Render(Graphics::CommandBuffer&                             comma
   bool drawn = false; // Draw can fail if there are no vertex buffers or they haven't been uploaded yet
                       // @todo We should detect this case much earlier to prevent unnecessary work
 
-  if(mDrawCommands.empty())
+  // Reuse latest bound vertex attributes location, or Bind buffers to attribute locations.
+  if(ReuseLatestBoundVertexAttributes(mGeometry) || mGeometry->BindVertexAttributes(commandBuffer))
   {
-    drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
+    if(mDrawCommands.empty())
+    {
+      drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
+    }
+    else
+    {
+      for(auto& cmd : commands)
+      {
+        drawn |= mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
+      }
+    }
   }
   else
   {
-    for(auto& cmd : commands)
-    {
-      mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
-    }
+    // BindVertexAttributes failed. Reset cached geometry.
+    ReuseLatestBoundVertexAttributes(nullptr);
   }
 
   return drawn;
@@ -739,7 +781,7 @@ void Renderer::WriteUniformBuffer(
     if(mvpUniformInfo && !mvpUniformInfo->name.empty())
     {
       Matrix modelViewProjectionMatrix(false);
-      MatrixUtils::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
+      MatrixUtils::MultiplyProjectionMatrix(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
       WriteDefaultUniform(mvpUniformInfo, *uboView, modelViewProjectionMatrix);
     }
 
@@ -927,8 +969,16 @@ Graphics::Pipeline& Renderer::PrepareGraphicsPipeline(
   queryInfo.alphaPremultiplied    = mPremultipliedAlphaEnabled;
   queryInfo.cameraUsingReflection = instruction.GetCamera()->GetReflectionUsed();
 
+  queryInfo.GenerateHash();
+
+  // Reset old pipeline
+  mPipelineCache->ResetPipeline(mPipeline);
+
+  // Find or generate new pipeline.
   auto pipelineResult = mPipelineCache->GetPipeline(queryInfo, true);
 
+  mPipeline = pipelineResult.level2;
+
   // should be never null?
   return *pipelineResult.pipeline;
 }