Uniform data cached locally in the uniform map
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.cpp
index 4a2a786..c6083a7 100644 (file)
@@ -432,41 +432,45 @@ bool Renderer::Render(Graphics::CommandBuffer&                             comma
 
   // 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(
-    fragShader,
-    Graphics::PipelineStage::FRAGMENT_SHADER,
-    shaderData->GetSourceMode());
-
-  std::vector<Graphics::ShaderState> shaderStates{
-    Graphics::ShaderState()
-      .SetShader(vertexShader)
-      .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
-    Graphics::ShaderState()
-      .SetShader(fragmentShader)
-      .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
-
-  auto createInfo = Graphics::ProgramCreateInfo();
-  createInfo.SetShaderState(shaderStates);
-
-  auto     graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
+
   Program* program         = Program::New(*mProgramCache,
                                   shaderData,
-                                  *mGraphicsController,
-                                  std::move(graphicsProgram));
-
+                                  *mGraphicsController);
   if(!program)
   {
     DALI_LOG_ERROR("Failed to get program for shader at address %p.\n", reinterpret_cast<void*>(&mRenderDataProvider->GetShader()));
     return false;
   }
 
+  // If program doesn't have Gfx program object assigned yet, prepare it.
+  if(!program->GetGraphicsProgramPtr())
+  {
+    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(
+      fragShader,
+      Graphics::PipelineStage::FRAGMENT_SHADER,
+      shaderData->GetSourceMode());
+
+    std::vector<Graphics::ShaderState> shaderStates{
+      Graphics::ShaderState()
+        .SetShader(vertexShader)
+        .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER),
+      Graphics::ShaderState()
+        .SetShader(fragmentShader)
+        .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)};
+
+    auto createInfo = Graphics::ProgramCreateInfo();
+    createInfo.SetShaderState(shaderStates);
+    auto graphicsProgram = mGraphicsController->CreateProgram(createInfo, nullptr);
+    program->SetGraphicsProgram(std::move(graphicsProgram));
+  }
+
   // Prepare the graphics pipeline. This may either re-use an existing pipeline or create a new one.
   auto& pipeline = PrepareGraphicsPipeline(*program, instruction, node, blend);
 
@@ -698,21 +702,43 @@ void Renderer::FillUniformBuffer(Program&                                      p
         iter != end;
         ++iter)
     {
-      // @todo This means parsing the uniform string every frame. Instead, store the array index if present.
-      int arrayIndex = (*iter).arrayIndex;
+      auto& uniform = *iter;
+      int arrayIndex = uniform.arrayIndex;
+
+      if(!uniform.uniformFunc)
+      {
+        auto uniformInfo  = Graphics::UniformInfo{};
+        auto uniformFound = program.GetUniform(uniform.uniformName.GetCString(),
+                                               uniform.uniformNameHashNoArray ? uniform.uniformNameHashNoArray
+                                                                              : uniform.uniformNameHash,
+                                               uniformInfo);
 
-      auto uniformInfo  = Graphics::UniformInfo{};
-      auto uniformFound = program.GetUniform((*iter).uniformName.GetCString(),
-                                             (*iter).uniformNameHashNoArray ? (*iter).uniformNameHashNoArray
-                                                                            : (*iter).uniformNameHash,
-                                             uniformInfo);
+        uniform.uniformOffset   = uniformInfo.offset;
+        uniform.uniformLocation = uniformInfo.location;
 
-      if(uniformFound)
+        if (uniformFound)
+        {
+          auto       dst      = ubo.GetOffset() + uniformInfo.offset;
+          const auto typeSize = GetPropertyValueSizeForUniform((*iter).propertyValue->GetType());
+          const auto dest     = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
+          const auto func     = GetPropertyValueGetter((*iter).propertyValue->GetType());
+
+          ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
+                    typeSize,
+                    dest);
+
+          uniform.uniformSize = typeSize;
+          uniform.uniformFunc = func;
+        }
+      }
+      else
       {
-        auto dst = ubo.GetOffset() + uniformInfo.offset;
-        const auto typeSize = GetPropertyValueSizeForUniform( (*iter).propertyValue->GetType() );
-        const auto dest = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
-        const auto func = GetPropertyValueGetter((*iter).propertyValue->GetType());
+        auto       dst      = ubo.GetOffset() + uniform.uniformOffset;
+        const auto typeSize = uniform.uniformSize;
+        const auto dest     = dst + static_cast<uint32_t>(typeSize) * arrayIndex;
+        const auto func     = uniform.uniformFunc;
+
+
         ubo.Write(&((*iter).propertyValue->*func)(updateBufferIndex),
                   typeSize,
                   dest);