Merge "Adding packed small float pixel format." into devel/graphics
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.cpp
index 4570797..4a06a0b 100644 (file)
 // INTERNAL INCLUDES
 #include <dali/graphics-api/graphics-program.h>
 #include <dali/graphics-api/graphics-types.h>
+#include <dali/integration-api/debug.h>
 #include <dali/internal/common/image-sampler.h>
 #include <dali/internal/render/common/render-instruction.h>
 #include <dali/internal/render/data-providers/node-data-provider.h>
 #include <dali/internal/render/data-providers/uniform-map-data-provider.h>
-#include <dali/internal/render/gl-resources/context.h>
 #include <dali/internal/render/renderers/render-sampler.h>
 #include <dali/internal/render/renderers/render-texture.h>
 #include <dali/internal/render/renderers/render-vertex-buffer.h>
 #include <dali/internal/render/renderers/shader-cache.h>
 #include <dali/internal/render/shaders/program.h>
-#include <dali/internal/render/shaders/scene-graph-shader.h>
+#include <dali/internal/render/shaders/render-shader.h>
+#include <dali/internal/update/common/uniform-map.h>
 
 namespace Dali
 {
@@ -39,69 +40,11 @@ namespace Internal
 {
 namespace
 {
-/**
- * Helper to set view and projection matrices once per program
- * @param program to set the matrices to
- * @param modelMatrix to set
- * @param viewMatrix to set
- * @param projectionMatrix to set
- * @param modelViewMatrix to set
- * @param modelViewProjectionMatrix to set
- */
-inline void SetMatrices(Program&      program,
-                        const Matrix& modelMatrix,
-                        const Matrix& viewMatrix,
-                        const Matrix& projectionMatrix,
-                        const Matrix& modelViewMatrix)
-{
-  GLint loc = program.GetUniformLocation(Program::UNIFORM_MODEL_MATRIX);
-  if(Program::UNIFORM_UNKNOWN != loc)
-  {
-    program.SetUniformMatrix4fv(loc, 1, modelMatrix.AsFloat());
-  }
-  loc = program.GetUniformLocation(Program::UNIFORM_VIEW_MATRIX);
-  if(Program::UNIFORM_UNKNOWN != loc)
-  {
-    if(program.GetViewMatrix() != &viewMatrix)
-    {
-      program.SetViewMatrix(&viewMatrix);
-      program.SetUniformMatrix4fv(loc, 1, viewMatrix.AsFloat());
-    }
-  }
-  // set projection matrix if program has not yet received it this frame or if it is dirty
-  loc = program.GetUniformLocation(Program::UNIFORM_PROJECTION_MATRIX);
-  if(Program::UNIFORM_UNKNOWN != loc)
-  {
-    if(program.GetProjectionMatrix() != &projectionMatrix)
-    {
-      program.SetProjectionMatrix(&projectionMatrix);
-      program.SetUniformMatrix4fv(loc, 1, projectionMatrix.AsFloat());
-    }
-  }
-  loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
-  if(Program::UNIFORM_UNKNOWN != loc)
-  {
-    program.SetUniformMatrix4fv(loc, 1, modelViewMatrix.AsFloat());
-  }
-
-  loc = program.GetUniformLocation(Program::UNIFORM_MVP_MATRIX);
-  if(Program::UNIFORM_UNKNOWN != loc)
-  {
-    Matrix modelViewProjectionMatrix(false);
-    Matrix::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
-    program.SetUniformMatrix4fv(loc, 1, modelViewProjectionMatrix.AsFloat());
-  }
+// Size of uniform buffer page used when resizing
+constexpr uint32_t UBO_PAGE_SIZE = 64;
 
-  loc = program.GetUniformLocation(Program::UNIFORM_NORMAL_MATRIX);
-  if(Program::UNIFORM_UNKNOWN != loc)
-  {
-    Matrix3 normalMatrix;
-    normalMatrix = modelViewMatrix;
-    normalMatrix.Invert();
-    normalMatrix.Transpose();
-    program.SetUniformMatrix3fv(loc, 1, normalMatrix.AsFloat());
-  }
-}
+// UBO allocation threshold below which the UBO will shrink
+constexpr auto UBO_SHRINK_THRESHOLD = 0.75f;
 
 // Helper to get the vertex input format
 Dali::Graphics::VertexInputFormat GetPropertyVertexFormat(Property::Type propertyType)
@@ -152,10 +95,6 @@ Dali::Graphics::VertexInputFormat GetPropertyVertexFormat(Property::Type propert
       break;
     }
     case Property::MATRIX3:
-    {
-      type = Dali::Graphics::VertexInputFormat::FLOAT;
-      break;
-    }
     case Property::MATRIX:
     {
       type = Dali::Graphics::VertexInputFormat::FLOAT;
@@ -260,6 +199,16 @@ constexpr Graphics::BlendOp ConvertBlendEquation(DevelBlendEquation::Type blendE
   return Graphics::BlendOp{};
 }
 
+/**
+ * Helper function to calculate the correct alignment of data for uniform buffers
+ * @param dataSize size of uniform buffer
+ * @return aligned offset of data
+ */
+inline uint32_t GetUniformBufferDataAlignment(uint32_t dataSize)
+{
+  return ((dataSize / 256u) + ((dataSize % 256u) ? 1u : 0u)) * 256u;
+}
+
 } // namespace
 
 namespace Render
@@ -288,8 +237,8 @@ Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider,
                    DepthTestMode::Type             depthTestMode,
                    DepthFunction::Type             depthFunction,
                    StencilParameters&              stencilParameters)
-: mRenderDataProvider(dataProvider),
-  mContext(nullptr),
+: mGraphicsController(nullptr),
+  mRenderDataProvider(dataProvider),
   mGeometry(geometry),
   mProgramCache(nullptr),
   mUniformIndexMap(),
@@ -304,7 +253,7 @@ Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider,
   mDepthWriteMode(depthWriteMode),
   mDepthTestMode(depthTestMode),
   mUpdateAttributeLocations(true),
-  mPremultipledAlphaEnabled(preMultipliedAlphaEnabled),
+  mPremultipliedAlphaEnabled(preMultipliedAlphaEnabled),
   mShaderChanged(false),
   mUpdated(true)
 {
@@ -316,12 +265,12 @@ Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider,
   mBlendingOptions.SetBlendColor(blendColor);
 }
 
-void Renderer::Initialize(Context& context, Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache)
+void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager)
 {
-  mContext            = &context;
-  mGraphicsController = &graphicsController;
-  mProgramCache       = &programCache;
-  mShaderCache        = &shaderCache;
+  mGraphicsController   = &graphicsController;
+  mProgramCache         = &programCache;
+  mShaderCache          = &shaderCache;
+  mUniformBufferManager = &uniformBufferManager;
 }
 
 Renderer::~Renderer() = default;
@@ -337,159 +286,10 @@ void Renderer::SetDrawCommands(Dali::DevelRenderer::DrawCommand* pDrawCommands,
   mDrawCommands.insert(mDrawCommands.end(), pDrawCommands, pDrawCommands + size);
 }
 
-void Renderer::GlContextDestroyed()
-{
-  mGeometry->GlContextDestroyed();
-}
-
-void Renderer::GlCleanup()
-{
-}
-
-void Renderer::SetUniforms(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
-{
-  // Check if the map has changed
-  DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
-
-  const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
-
-  if(uniformMapDataProvider.GetUniformMapChanged(bufferIndex) ||
-     node.GetUniformMapChanged(bufferIndex) ||
-     mUniformIndexMap.Count() == 0 ||
-     mShaderChanged)
-  {
-    // Reset shader pointer
-    mShaderChanged = false;
-
-    const SceneGraph::CollectedUniformMap& uniformMap     = uniformMapDataProvider.GetUniformMap(bufferIndex);
-    const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap(bufferIndex);
-
-    uint32_t maxMaps = static_cast<uint32_t>(uniformMap.Count() + uniformMapNode.Count()); // 4,294,967,295 maps should be enough
-    mUniformIndexMap.Clear();                                                              // Clear contents, but keep memory if we don't change size
-    mUniformIndexMap.Resize(maxMaps);
-
-    uint32_t mapIndex = 0;
-    for(; mapIndex < uniformMap.Count(); ++mapIndex)
-    {
-      mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex].propertyPtr;
-      mUniformIndexMap[mapIndex].uniformIndex  = program.RegisterUniform(uniformMap[mapIndex].uniformName);
-    }
-
-    for(uint32_t nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count(); ++nodeMapIndex)
-    {
-      uint32_t uniformIndex = program.RegisterUniform(uniformMapNode[nodeMapIndex].uniformName);
-      bool     found(false);
-      for(uint32_t i = 0; i < uniformMap.Count(); ++i)
-      {
-        if(mUniformIndexMap[i].uniformIndex == uniformIndex)
-        {
-          mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
-          found                             = true;
-          break;
-        }
-      }
-
-      if(!found)
-      {
-        mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
-        mUniformIndexMap[mapIndex].uniformIndex  = uniformIndex;
-        ++mapIndex;
-      }
-    }
-
-    mUniformIndexMap.Resize(mapIndex);
-  }
-
-  // Set uniforms in local map
-  for(UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
-                                     end  = mUniformIndexMap.End();
-      iter != end;
-      ++iter)
-  {
-    SetUniformFromProperty(bufferIndex, program, *iter);
-  }
-
-  GLint sizeLoc = program.GetUniformLocation(Program::UNIFORM_SIZE);
-  if(-1 != sizeLoc)
-  {
-    program.SetSizeUniform3f(sizeLoc, size.x, size.y, size.z);
-  }
-}
-
-void Renderer::SetUniformFromProperty(BufferIndex bufferIndex, Program& program, UniformIndexMap& map)
-{
-  GLint location = program.GetUniformLocation(map.uniformIndex);
-  if(Program::UNIFORM_UNKNOWN != location)
-  {
-    // switch based on property type to use correct GL uniform setter
-    switch(map.propertyValue->GetType())
-    {
-      case Property::INTEGER:
-      {
-        program.SetUniform1i(location, map.propertyValue->GetInteger(bufferIndex));
-        break;
-      }
-      case Property::FLOAT:
-      {
-        program.SetUniform1f(location, map.propertyValue->GetFloat(bufferIndex));
-        break;
-      }
-      case Property::VECTOR2:
-      {
-        Vector2 value(map.propertyValue->GetVector2(bufferIndex));
-        program.SetUniform2f(location, value.x, value.y);
-        break;
-      }
-
-      case Property::VECTOR3:
-      {
-        Vector3 value(map.propertyValue->GetVector3(bufferIndex));
-        program.SetUniform3f(location, value.x, value.y, value.z);
-        break;
-      }
-
-      case Property::VECTOR4:
-      {
-        Vector4 value(map.propertyValue->GetVector4(bufferIndex));
-        program.SetUniform4f(location, value.x, value.y, value.z, value.w);
-        break;
-      }
-
-      case Property::ROTATION:
-      {
-        Quaternion value(map.propertyValue->GetQuaternion(bufferIndex));
-        program.SetUniform4f(location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w);
-        break;
-      }
-
-      case Property::MATRIX:
-      {
-        const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
-        program.SetUniformMatrix4fv(location, 1, value.AsFloat());
-        break;
-      }
-
-      case Property::MATRIX3:
-      {
-        const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
-        program.SetUniformMatrix3fv(location, 1, value.AsFloat());
-        break;
-      }
-
-      default:
-      {
-        // Other property types are ignored
-        break;
-      }
-    }
-  }
-}
-
-bool Renderer::BindTextures(Program& program, Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures)
+void Renderer::BindTextures(Graphics::CommandBuffer& commandBuffer, Vector<Graphics::Texture*>& boundTextures)
 {
   uint32_t textureUnit = 0;
 
-  GLint                          uniformLocation(-1);
   std::vector<Render::Sampler*>& samplers(mRenderDataProvider->GetSamplers());
   std::vector<Render::Texture*>& textures(mRenderDataProvider->GetTextures());
 
@@ -498,32 +298,25 @@ bool Renderer::BindTextures(Program& program, Graphics::CommandBuffer& commandBu
   {
     if(textures[i] && textures[i]->GetGraphicsObject())
     {
-      if(program.GetSamplerUniformLocation(i, uniformLocation))
-      {
-        // if the sampler exists,
-        //   if it's default, delete the graphics object
-        //   otherwise re-initialize it if dirty
+      // if the sampler exists,
+      //   if it's default, delete the graphics object
+      //   otherwise re-initialize it if dirty
 
-        const Graphics::Sampler* graphicsSampler = (samplers[i] ? samplers[i]->GetGraphicsObject()
-                                                                : nullptr);
+      const Graphics::Sampler* graphicsSampler = (samplers[i] ? samplers[i]->GetGraphicsObject()
+                                                              : nullptr);
 
-        boundTextures.PushBack(textures[i]->GetGraphicsObject());
-        const Graphics::TextureBinding textureBinding{textures[i]->GetGraphicsObject(), graphicsSampler, textureUnit};
-        textureBindings.push_back(textureBinding);
+      boundTextures.PushBack(textures[i]->GetGraphicsObject());
+      const Graphics::TextureBinding textureBinding{textures[i]->GetGraphicsObject(), graphicsSampler, textureUnit};
+      textureBindings.push_back(textureBinding);
 
-        program.SetUniform1i(uniformLocation, textureUnit); // Get through shader reflection
-        ++textureUnit;
-      }
+      ++textureUnit;
     }
   }
 
-  if(textureBindings.size() > 0)
+  if(!textureBindings.empty())
   {
     commandBuffer.BindTextures(textureBindings);
   }
-
-  // @todo remove check from caller.
-  return true;
 }
 
 void Renderer::SetFaceCullingMode(FaceCullingMode::Type mode)
@@ -558,8 +351,8 @@ void Renderer::SetIndexedDrawElementsCount(uint32_t elementsCount)
 
 void Renderer::EnablePreMultipliedAlpha(bool enable)
 {
-  mPremultipledAlphaEnabled = enable;
-  mUpdated                  = true;
+  mPremultipliedAlphaEnabled = enable;
+  mUpdated                   = true;
 }
 
 void Renderer::SetDepthWriteMode(DepthWriteMode::Type depthWriteMode)
@@ -688,7 +481,7 @@ void Renderer::Upload()
   mGeometry->Upload(*mGraphicsController);
 }
 
-void Renderer::Render(Context&                                             context,
+bool Renderer::Render(Graphics::CommandBuffer&                             commandBuffer,
                       BufferIndex                                          bufferIndex,
                       const SceneGraph::NodeDataProvider&                  node,
                       const Matrix&                                        modelMatrix,
@@ -704,7 +497,7 @@ void Renderer::Render(Context&                                             conte
   // Before doing anything test if the call happens in the right queue
   if(mDrawCommands.empty() && queueIndex > 0)
   {
-    return;
+    return false;
   }
 
   // Prepare commands
@@ -720,33 +513,26 @@ void Renderer::Render(Context&                                             conte
   // Have commands but nothing to be drawn - abort
   if(!mDrawCommands.empty() && commands.empty())
   {
-    return;
+    return false;
   }
 
-  Graphics::UniquePtr<Graphics::CommandBuffer> commandBuffer = mGraphicsController->CreateCommandBuffer(
-    Graphics::CommandBufferCreateInfo()
-      .SetLevel(Graphics::CommandBufferLevel::SECONDARY),
-    nullptr);
-
-  //Set blending mode
+  // Set blending mode
   if(!mDrawCommands.empty())
   {
-    blend = (commands[0]->queue == DevelRenderer::RENDER_QUEUE_OPAQUE ? false : blend);
+    blend = (commands[0]->queue != DevelRenderer::RENDER_QUEUE_OPAQUE) && blend;
   }
 
-  // Create Shader.
-  // Really, need to have a pipeline cache in implementation.
-  // Get the program to use
-  // The program cache owns the Program object so we don't need to worry about this raw allocation here.
-  ShaderDataPtr shaderData = mRenderDataProvider->GetShader().GetShaderData();
-
-  Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader(
-    shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
+  // Create Program
+  ShaderDataPtr            shaderData   = mRenderDataProvider->GetShader().GetShaderData();
+  const std::vector<char>& vertShader   = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
+  const std::vector<char>& fragShader   = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
+  Dali::Graphics::Shader&  vertexShader = mShaderCache->GetShader(
+    vertShader,
     Graphics::PipelineStage::VERTEX_SHADER,
     shaderData->GetSourceMode());
 
   Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader(
-    shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER),
+    fragShader,
     Graphics::PipelineStage::FRAGMENT_SHADER,
     shaderData->GetSourceMode());
 
@@ -765,81 +551,358 @@ void Renderer::Render(Context&                                             conte
   Program* program         = Program::New(*mProgramCache,
                                   shaderData,
                                   *mGraphicsController,
-                                  std::move(graphicsProgram),
-                                  (shaderData->GetHints() & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0);
+                                  std::move(graphicsProgram));
 
   if(!program)
   {
     DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<void*>(&mRenderDataProvider->GetShader()));
-    return;
+    return false;
+  }
+
+  // Prepare the graphics pipeline. This may either re-use an existing pipeline or create a new one.
+  auto& pipeline = PrepareGraphicsPipeline(*program, instruction, node, blend);
+
+  commandBuffer.BindPipeline(pipeline);
+
+  BindTextures(commandBuffer, boundTextures);
+
+  BuildUniformIndexMap(bufferIndex, node, size, *program);
+
+  WriteUniformBuffer(bufferIndex, commandBuffer, program, instruction, node, modelMatrix, modelViewMatrix, viewMatrix, projectionMatrix, size);
+
+  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())
+  {
+    drawn = mGeometry->Draw(*mGraphicsController, commandBuffer, mIndexedDrawFirstElement, mIndexedDrawElementsCount);
+  }
+  else
+  {
+    for(auto& cmd : commands)
+    {
+      mGeometry->Draw(*mGraphicsController, commandBuffer, cmd->firstIndex, cmd->elementCount);
+    }
   }
 
-  // Temporarily create a pipeline here - this will be used for transporting
-  // topology, vertex format, attrs, rasterization state
-  mGraphicsPipeline = PrepareGraphicsPipeline(*program, instruction, blend, std::move(mGraphicsPipeline));
+  mUpdated = false;
+  return drawn;
+}
+
+void Renderer::BuildUniformIndexMap(BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program)
+{
+  // Check if the map has changed
+  DALI_ASSERT_DEBUG(mRenderDataProvider && "No Uniform map data provider available");
 
-  commandBuffer->BindPipeline(*mGraphicsPipeline.get());
+  const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
 
-  if(DALI_LIKELY(BindTextures(*program, *commandBuffer.get(), boundTextures)))
+  if(uniformMapDataProvider.GetUniformMapChanged(bufferIndex) ||
+     node.GetUniformMapChanged(bufferIndex) ||
+     mUniformIndexMap.Count() == 0 ||
+     mShaderChanged)
   {
-    // Only set up and draw if we have textures and they are all valid
+    // Reset shader pointer
+    mShaderChanged = false;
 
-    // set projection and view matrix if program has not yet received them yet this frame
-    SetMatrices(*program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix);
+    const SceneGraph::CollectedUniformMap& uniformMap     = uniformMapDataProvider.GetUniformMap(bufferIndex);
+    const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap(bufferIndex);
+
+    auto maxMaps = static_cast<uint32_t>(uniformMap.Count() + uniformMapNode.Count()); // 4,294,967,295 maps should be enough
+    mUniformIndexMap.Clear();                                                          // Clear contents, but keep memory if we don't change size
+    mUniformIndexMap.Resize(maxMaps);
+
+    // Copy uniform map into mUniformIndexMap
+    uint32_t mapIndex = 0;
+    for(; mapIndex < uniformMap.Count(); ++mapIndex)
+    {
+      mUniformIndexMap[mapIndex].propertyValue          = uniformMap[mapIndex].propertyPtr;
+      mUniformIndexMap[mapIndex].uniformName            = uniformMap[mapIndex].uniformName;
+      mUniformIndexMap[mapIndex].uniformNameHash        = uniformMap[mapIndex].uniformNameHash;
+      mUniformIndexMap[mapIndex].uniformNameHashNoArray = uniformMap[mapIndex].uniformNameHashNoArray;
+      mUniformIndexMap[mapIndex].arrayIndex             = uniformMap[mapIndex].arrayIndex;
+    }
 
-    // set color uniform
-    GLint loc = program->GetUniformLocation(Program::UNIFORM_COLOR);
-    if(Program::UNIFORM_UNKNOWN != loc)
+    for(uint32_t nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count(); ++nodeMapIndex)
     {
-      const Vector4& color = node.GetRenderColor(bufferIndex);
-      if(mPremultipledAlphaEnabled)
+      auto  hash = uniformMapNode[nodeMapIndex].uniformNameHash;
+      auto& name = uniformMapNode[nodeMapIndex].uniformName;
+      bool  found(false);
+      for(uint32_t i = 0; i < uniformMap.Count(); ++i)
       {
-        float alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
-        program->SetUniform4f(loc, color.r * alpha, color.g * alpha, color.b * alpha, alpha);
+        if(mUniformIndexMap[i].uniformNameHash == hash &&
+           mUniformIndexMap[i].uniformName == name)
+        {
+          mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
+          found                             = true;
+          break;
+        }
       }
-      else
+
+      if(!found)
       {
-        program->SetUniform4f(loc, color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
+        mUniformIndexMap[mapIndex].propertyValue          = uniformMapNode[nodeMapIndex].propertyPtr;
+        mUniformIndexMap[mapIndex].uniformName            = uniformMapNode[nodeMapIndex].uniformName;
+        mUniformIndexMap[mapIndex].uniformNameHash        = uniformMapNode[nodeMapIndex].uniformNameHash;
+        mUniformIndexMap[mapIndex].uniformNameHashNoArray = uniformMapNode[nodeMapIndex].uniformNameHashNoArray;
+        mUniformIndexMap[mapIndex].arrayIndex             = uniformMapNode[nodeMapIndex].arrayIndex;
+        ++mapIndex;
       }
     }
 
-    SetUniforms(bufferIndex, node, size, *program);
+    mUniformIndexMap.Resize(mapIndex);
+  }
+}
 
-    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
+void Renderer::WriteUniformBuffer(
+  BufferIndex                          bufferIndex,
+  Graphics::CommandBuffer&             commandBuffer,
+  Program*                             program,
+  const SceneGraph::RenderInstruction& instruction,
+  const SceneGraph::NodeDataProvider&  node,
+  const Matrix&                        modelMatrix,
+  const Matrix&                        modelViewMatrix,
+  const Matrix&                        viewMatrix,
+  const Matrix&                        projectionMatrix,
+  const Vector3&                       size)
+{
+  // Create the UBO
+  uint32_t uniformBlockAllocationBytes{0u};
+  uint32_t uniformBlockMaxSize{0u};
+  uint32_t uboOffset{0u};
 
-    //@todo manage mDrawCommands in the same way as above command buffer?!
-    if(mDrawCommands.empty())
+  auto& reflection = mGraphicsController->GetProgramReflection(program->GetGraphicsProgram());
+  for(auto i = 0u; i < reflection.GetUniformBlockCount(); ++i)
+  {
+    auto blockSize = GetUniformBufferDataAlignment(reflection.GetUniformBlockSize(i));
+    if(uniformBlockMaxSize < blockSize)
     {
-      drawn = mGeometry->Draw(*mGraphicsController, *commandBuffer.get(), mIndexedDrawFirstElement, mIndexedDrawElementsCount);
+      uniformBlockMaxSize = blockSize;
     }
-    else
+    uniformBlockAllocationBytes += blockSize;
+  }
+
+  auto pagedAllocation = ((uniformBlockAllocationBytes / UBO_PAGE_SIZE + 1u)) * UBO_PAGE_SIZE;
+
+  // Allocate twice memory as required by the uniform buffers
+  // todo: memory usage backlog to use optimal allocation
+  if(uniformBlockAllocationBytes && !mUniformBuffer[bufferIndex])
+  {
+    mUniformBuffer[bufferIndex] = mUniformBufferManager->AllocateUniformBuffer(pagedAllocation);
+  }
+  else if(uniformBlockAllocationBytes && (mUniformBuffer[bufferIndex]->GetSize() < pagedAllocation ||
+                                          (pagedAllocation < uint32_t(float(mUniformBuffer[bufferIndex]->GetSize()) * UBO_SHRINK_THRESHOLD))))
+  {
+    mUniformBuffer[bufferIndex]->Reserve(pagedAllocation);
+  }
+
+  // Clear UBO
+  if(mUniformBuffer[bufferIndex])
+  {
+    mUniformBuffer[bufferIndex]->Fill(0, 0u, 0u);
+  }
+
+  // update the uniform buffer
+  // pass shared UBO and offset, return new offset for next item to be used
+  // don't process bindings if there are no uniform buffers allocated
+  auto ubo = mUniformBuffer[bufferIndex].get();
+  if(ubo)
+  {
+    auto uboCount = reflection.GetUniformBlockCount();
+    mUniformBufferBindings.resize(uboCount);
+
+    std::vector<Graphics::UniformBufferBinding>* bindings{&mUniformBufferBindings};
+
+    // Write default uniforms
+    WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_MATRIX), *ubo, *bindings, modelMatrix);
+    WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::VIEW_MATRIX), *ubo, *bindings, viewMatrix);
+    WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::PROJECTION_MATRIX), *ubo, *bindings, projectionMatrix);
+    WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::MODEL_VIEW_MATRIX), *ubo, *bindings, modelViewMatrix);
+
+    auto mvpUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::MVP_MATRIX);
+    if(mvpUniformInfo && !mvpUniformInfo->name.empty())
     {
-      for(auto& cmd : commands)
-      {
-        // @todo This should generate a command buffer per cmd
-        // Tests WILL fail. (Temporarily commented out)
-        mGeometry->Draw(*mGraphicsController, *commandBuffer.get(), cmd->firstIndex, cmd->elementCount);
-      }
+      Matrix modelViewProjectionMatrix(false);
+      Matrix::Multiply(modelViewProjectionMatrix, modelViewMatrix, projectionMatrix);
+      WriteDefaultUniform(mvpUniformInfo, *ubo, *bindings, modelViewProjectionMatrix);
     }
 
-    // Command buffer contains Texture bindings, vertex bindings, index buffer binding, pipeline(vertex format)
-    // @todo We should return the command buffer(s) and let the calling method submit
-    // If not drawn, then don't add command buffer to submit info, and if empty, don't
-    // submit.
-    if(drawn)
+    auto normalUniformInfo = program->GetDefaultUniform(Program::DefaultUniformIndex::NORMAL_MATRIX);
+    if(normalUniformInfo && !normalUniformInfo->name.empty())
     {
-      Graphics::SubmitInfo submitInfo{{}, 0 | Graphics::SubmitFlagBits::FLUSH};
-      submitInfo.cmdBuffer.push_back(commandBuffer.get());
-      mGraphicsController->SubmitCommandBuffers(submitInfo);
+      Matrix3 normalMatrix(modelViewMatrix);
+      normalMatrix.Invert();
+      normalMatrix.Transpose();
+      WriteDefaultUniform(normalUniformInfo, *ubo, *bindings, normalMatrix);
     }
 
-    mUpdated = false;
+    Vector4        finalColor;
+    const Vector4& color = node.GetRenderColor(bufferIndex);
+    if(mPremultipliedAlphaEnabled)
+    {
+      float alpha = color.a * mRenderDataProvider->GetOpacity(bufferIndex);
+      finalColor  = Vector4(color.r * alpha, color.g * alpha, color.b * alpha, alpha);
+    }
+    else
+    {
+      finalColor = Vector4(color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity(bufferIndex));
+    }
+    WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::COLOR), *ubo, *bindings, finalColor);
+
+    // Write uniforms from the uniform map
+    FillUniformBuffer(*program, instruction, *ubo, bindings, uboOffset, bufferIndex);
+
+    // Write uSize in the end, as it shouldn't be overridable by dynamic properties.
+    WriteDefaultUniform(program->GetDefaultUniform(Program::DefaultUniformIndex::SIZE), *ubo, *bindings, size);
+
+    commandBuffer.BindUniformBuffers(*bindings);
+  }
+}
+
+template<class T>
+bool Renderer::WriteDefaultUniform(const Graphics::UniformInfo* uniformInfo, Render::UniformBuffer& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const T& data)
+{
+  if(uniformInfo && !uniformInfo->name.empty())
+  {
+    WriteUniform(ubo, bindings, *uniformInfo, data);
+    return true;
   }
+  return false;
+}
+
+template<class T>
+void Renderer::WriteUniform(Render::UniformBuffer& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const Graphics::UniformInfo& uniformInfo, const T& data)
+{
+  WriteUniform(ubo, bindings, uniformInfo, &data, sizeof(T));
+}
+
+void Renderer::WriteUniform(Render::UniformBuffer& ubo, const std::vector<Graphics::UniformBufferBinding>& bindings, const Graphics::UniformInfo& uniformInfo, const void* data, uint32_t size)
+{
+  ubo.Write(data, size, bindings[uniformInfo.bufferIndex].offset + uniformInfo.offset);
 }
 
-void Renderer::SetSortAttributes(BufferIndex                                             bufferIndex,
-                                 SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
+void Renderer::FillUniformBuffer(Program&                                      program,
+                                 const SceneGraph::RenderInstruction&          instruction,
+                                 Render::UniformBuffer&                        ubo,
+                                 std::vector<Graphics::UniformBufferBinding>*& outBindings,
+                                 uint32_t&                                     offset,
+                                 BufferIndex                                   updateBufferIndex)
+{
+  auto& reflection = mGraphicsController->GetProgramReflection(program.GetGraphicsProgram());
+  auto  uboCount   = reflection.GetUniformBlockCount();
+
+  // Setup bindings
+  uint32_t dataOffset = offset;
+  for(auto i = 0u; i < uboCount; ++i)
+  {
+    mUniformBufferBindings[i].dataSize = reflection.GetUniformBlockSize(i);
+    mUniformBufferBindings[i].binding  = reflection.GetUniformBlockBinding(i);
+    mUniformBufferBindings[i].offset   = dataOffset;
+
+    dataOffset += GetUniformBufferDataAlignment(mUniformBufferBindings[i].dataSize);
+    mUniformBufferBindings[i].buffer = ubo.GetBuffer();
+
+    for(UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
+                                       end  = mUniformIndexMap.End();
+        iter != end;
+        ++iter)
+    {
+      // @todo This means parsing the uniform string every frame. Instead, store the array index if present.
+      int arrayIndex = (*iter).arrayIndex;
+
+      auto uniformInfo  = Graphics::UniformInfo{};
+      auto uniformFound = program.GetUniform((*iter).uniformName.GetCString(),
+                                             (*iter).uniformNameHashNoArray ? (*iter).uniformNameHashNoArray
+                                                                            : (*iter).uniformNameHash,
+                                             uniformInfo);
+
+      if(uniformFound)
+      {
+        auto dst = mUniformBufferBindings[uniformInfo.bufferIndex].offset + uniformInfo.offset;
+
+        switch((*iter).propertyValue->GetType())
+        {
+          case Property::Type::BOOLEAN:
+          {
+            ubo.Write(&(*iter).propertyValue->GetBoolean(updateBufferIndex),
+                      sizeof(bool),
+                      dst + static_cast<uint32_t>(sizeof(bool)) * arrayIndex);
+            break;
+          }
+          case Property::Type::INTEGER:
+          {
+            ubo.Write(&(*iter).propertyValue->GetInteger(updateBufferIndex),
+                      sizeof(int32_t),
+                      dst + static_cast<int32_t>(sizeof(int32_t)) * arrayIndex);
+            break;
+          }
+          case Property::Type::FLOAT:
+          {
+            ubo.Write(&(*iter).propertyValue->GetFloat(updateBufferIndex),
+                      sizeof(float),
+                      dst + static_cast<uint32_t>(sizeof(float)) * arrayIndex);
+            break;
+          }
+          case Property::Type::VECTOR2:
+          {
+            ubo.Write(&(*iter).propertyValue->GetVector2(updateBufferIndex),
+                      sizeof(Vector2),
+                      dst + static_cast<uint32_t>(sizeof(Vector2)) * arrayIndex);
+            break;
+          }
+          case Property::Type::VECTOR3:
+          {
+            ubo.Write(&(*iter).propertyValue->GetVector3(updateBufferIndex),
+                      sizeof(Vector3),
+                      dst + static_cast<uint32_t>(sizeof(Vector3)) * arrayIndex);
+            break;
+          }
+          case Property::Type::VECTOR4:
+          {
+            ubo.Write(&(*iter).propertyValue->GetVector4(updateBufferIndex),
+                      sizeof(Vector4),
+                      dst + static_cast<uint32_t>(sizeof(Vector4)) * arrayIndex);
+            break;
+          }
+          case Property::Type::MATRIX:
+          {
+            ubo.Write(&(*iter).propertyValue->GetMatrix(updateBufferIndex),
+                      sizeof(Matrix),
+                      dst + static_cast<uint32_t>(sizeof(Matrix)) * arrayIndex);
+            break;
+          }
+          case Property::Type::MATRIX3:
+          {
+            // todo: handle data padding properly
+            // Vulkan:
+            //
+            //const auto& matrix = &(*iter).propertyValue->GetMatrix3(updateBufferIndex);
+            //for(int i = 0; i < 3; ++i)
+            //{
+            //ubo.Write(&matrix->AsFloat()[i * 3],
+            //          sizeof(float) * 3,
+            //          dst + (i * static_cast<uint32_t>(sizeof(Vector4))));
+            //}
+            // GL:
+            ubo.Write(&(*iter).propertyValue->GetMatrix3(updateBufferIndex),
+                      sizeof(Matrix3),
+                      dst + static_cast<uint32_t>(sizeof(Matrix3)) * arrayIndex);
+            break;
+          }
+          default:
+          {
+          }
+        }
+      }
+    }
+  }
+  // write output bindings
+  outBindings = &mUniformBufferBindings;
+
+  // Update offset
+  offset = dataOffset;
+}
+
+void Renderer::SetSortAttributes(SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes) const
 {
   sortAttributes.shader   = &(mRenderDataProvider->GetShader());
   sortAttributes.geometry = mGeometry;
@@ -894,11 +957,11 @@ bool Renderer::Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvid
   return false;
 }
 
-Graphics::UniquePtr<Graphics::Pipeline> Renderer::PrepareGraphicsPipeline(
+Graphics::Pipeline& Renderer::PrepareGraphicsPipeline(
   Program&                                             program,
   const Dali::Internal::SceneGraph::RenderInstruction& instruction,
-  bool                                                 blend,
-  Graphics::UniquePtr<Graphics::Pipeline>&&            oldPipeline)
+  const SceneGraph::NodeDataProvider&                  node,
+  bool                                                 blend)
 {
   Graphics::InputAssemblyState inputAssemblyState{};
   Graphics::VertexInputState   vertexInputState{};
@@ -938,7 +1001,7 @@ Graphics::UniquePtr<Graphics::Pipeline> Renderer::PrepareGraphicsPipeline(
         mAttributeLocations.PushBack(pLocation);
       }
 
-      uint32_t location = static_cast<uint32_t>(mAttributeLocations[base + i]);
+      auto location = static_cast<uint32_t>(mAttributeLocations[base + i]);
 
       vertexInputState.attributes.emplace_back(location,
                                                bindingIndex,
@@ -1009,11 +1072,8 @@ Graphics::UniquePtr<Graphics::Pipeline> Renderer::PrepareGraphicsPipeline(
       break;
   }
 
-  // @todo How to signal a blend barrier is needed?
-  //if(mBlendingOptions.IsAdvancedBlendEquationApplied() && mPremultipledAlphaEnabled)
-  //{
-  //  context.BlendBarrier();
-  //}
+  // @todo Add blend barrier to the Graphics API if we are using advanced
+  // blending options. Command?
 
   Graphics::ColorBlendState colorBlendState{};
   colorBlendState.SetBlendEnable(false);
@@ -1024,7 +1084,7 @@ Graphics::UniquePtr<Graphics::Pipeline> Renderer::PrepareGraphicsPipeline(
 
     Graphics::BlendOp rgbOp   = ConvertBlendEquation(mBlendingOptions.GetBlendEquationRgb());
     Graphics::BlendOp alphaOp = ConvertBlendEquation(mBlendingOptions.GetBlendEquationRgb());
-    if(mBlendingOptions.IsAdvancedBlendEquationApplied() && mPremultipledAlphaEnabled)
+    if(mBlendingOptions.IsAdvancedBlendEquationApplied() && mPremultipliedAlphaEnabled)
     {
       if(rgbOp != alphaOp)
       {
@@ -1042,29 +1102,54 @@ Graphics::UniquePtr<Graphics::Pipeline> Renderer::PrepareGraphicsPipeline(
       .SetAlphaBlendOp(alphaOp);
 
     // Blend color is optional and rarely used
-    Vector4* blendColor = const_cast<Vector4*>(mBlendingOptions.GetBlendColor());
+    auto* blendColor = const_cast<Vector4*>(mBlendingOptions.GetBlendColor());
     if(blendColor)
     {
       colorBlendState.SetBlendConstants(blendColor->AsFloat());
     }
   }
 
-  // Take the program into use so we can send uniforms to it
-  // @todo Remove this call entirely!
-  program.Use();
-
   mUpdated = true;
 
-  // Create a new pipeline
-  return mGraphicsController->CreatePipeline(
-    Graphics::PipelineCreateInfo()
-      .SetInputAssemblyState(&inputAssemblyState) // Passed as pointers - shallow copy will break. TOO C LIKE
-      .SetVertexInputState(&vertexInputState)
-      .SetRasterizationState(&rasterizationState)
-      .SetColorBlendState(&colorBlendState)
-      .SetProgramState(&programState)
-      .SetNextExtension(&mLegacyProgram),
-    std::move(oldPipeline));
+  // Create the pipeline
+  Graphics::PipelineCreateInfo createInfo;
+  createInfo
+    .SetInputAssemblyState(&inputAssemblyState)
+    .SetVertexInputState(&vertexInputState)
+    .SetRasterizationState(&rasterizationState)
+    .SetColorBlendState(&colorBlendState)
+    .SetProgramState(&programState);
+
+  // Store a pipeline per renderer per render (renderer can be owned by multiple nodes,
+  // and re-drawn in multiple instructions).
+  // @todo This is only needed because ColorBlend state can change. Fixme!
+  // This is ameliorated by the fact that implementation caches pipelines, and we're only storing
+  // handles.
+  auto            hash           = HashedPipeline::GetHash(&node, &instruction, blend);
+  HashedPipeline* hashedPipeline = nullptr;
+  for(auto& element : mGraphicsPipelines)
+  {
+    if(element.mHash == hash)
+    {
+      hashedPipeline = &element;
+      break;
+    }
+  }
+
+  if(hashedPipeline != nullptr)
+  {
+    hashedPipeline->mGraphicsPipeline = mGraphicsController->CreatePipeline(
+      createInfo,
+      std::move(hashedPipeline->mGraphicsPipeline));
+  }
+  else
+  {
+    mGraphicsPipelines.emplace_back();
+    mGraphicsPipelines.back().mHash             = hash;
+    mGraphicsPipelines.back().mGraphicsPipeline = mGraphicsController->CreatePipeline(createInfo, nullptr);
+    hashedPipeline                              = &mGraphicsPipelines.back();
+  }
+  return *hashedPipeline->mGraphicsPipeline.get();
 }
 
 } // namespace Render