Use stack variable instead of global when computing matrix
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.cpp
index 4beee2e..e3f1684 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -35,8 +35,6 @@ namespace Internal
 namespace
 {
 
-static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
-static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
 
 /**
  * Helper to set view and projection matrices once per program
@@ -86,17 +84,19 @@ inline void SetMatrices( Program& program,
   loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
   if( Program::UNIFORM_UNKNOWN != loc )
   {
-    Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
-    program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.AsFloat() );
+    Matrix modelViewProjectionMatrix(false);
+    Matrix::Multiply( modelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
+    program.SetUniformMatrix4fv( loc, 1, modelViewProjectionMatrix.AsFloat() );
   }
 
   loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
   if( Program::UNIFORM_UNKNOWN != loc )
   {
-    gNormalMatrix = modelViewMatrix;
-    gNormalMatrix.Invert();
-    gNormalMatrix.Transpose();
-    program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
+    Matrix3 normalMatrix;
+    normalMatrix = modelViewMatrix;
+    normalMatrix.Invert();
+    normalMatrix.Transpose();
+    program.SetUniformMatrix3fv( loc, 1, normalMatrix.AsFloat() );
   }
 }
 
@@ -145,7 +145,8 @@ Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
   mDepthWriteMode( depthWriteMode ),
   mDepthTestMode( depthTestMode ),
   mUpdateAttributesLocation( true ),
-  mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
+  mPremultipledAlphaEnabled( preMultipliedAlphaEnabled ),
+  mShaderChanged( false )
 {
   if( blendingBitmask != 0u )
   {
@@ -216,8 +217,12 @@ void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataP
 
   if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
       node.GetUniformMapChanged(bufferIndex) ||
-      mUniformIndexMap.Count() == 0)
+      mUniformIndexMap.Count() == 0 ||
+      mShaderChanged )
   {
+    // Reset shader pointer
+    mShaderChanged = false;
+
     const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
     const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
 
@@ -342,7 +347,7 @@ void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program
   }
 }
 
-bool Renderer::BindTextures( Context& context, Program& program )
+bool Renderer::BindTextures( Context& context, Program& program, Vector<GLuint>& boundTextures )
 {
   uint32_t textureUnit = 0;
   bool result = true;
@@ -355,6 +360,7 @@ bool Renderer::BindTextures( Context& context, Program& program )
     if( textures[i] )
     {
       result = textures[i]->Bind(context, textureUnit, samplers[i] );
+      boundTextures.PushBack( textures[i]->GetId() );
       if( result && program.GetSamplerUniformLocation( i, uniformLocation ) )
       {
         program.SetUniform1i( uniformLocation, textureUnit );
@@ -506,6 +512,11 @@ StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
   return mStencilParameters.stencilOperationOnZPass;
 }
 
+void Renderer::Upload( Context& context )
+{
+  mGeometry->Upload( context );
+}
+
 void Renderer::Render( Context& context,
                        BufferIndex bufferIndex,
                        const SceneGraph::NodeDataProvider& node,
@@ -514,7 +525,8 @@ void Renderer::Render( Context& context,
                        const Matrix& viewMatrix,
                        const Matrix& projectionMatrix,
                        const Vector3& size,
-                       bool blend )
+                       bool blend,
+                       Vector<GLuint>& boundTextures )
 {
   // Get the program to use:
   Program* program = mRenderDataProvider->GetShader().GetProgram();
@@ -533,7 +545,7 @@ void Renderer::Render( Context& context,
   // Take the program into use so we can send uniforms to it
   program->Use();
 
-  if( DALI_LIKELY( BindTextures( context, *program ) ) )
+  if( DALI_LIKELY( BindTextures( context, *program, boundTextures ) ) )
   {
     // Only set up and draw if we have textures and they are all valid
 
@@ -564,11 +576,11 @@ void Renderer::Render( Context& context,
       mUpdateAttributesLocation = false;
     }
 
-    mGeometry->UploadAndDraw( context,
-                              bufferIndex,
-                              mAttributesLocation,
-                              mIndexedDrawFirstElement,
-                              mIndexedDrawElementsCount );
+    mGeometry->Draw( context,
+                     bufferIndex,
+                     mAttributesLocation,
+                     mIndexedDrawFirstElement,
+                     mIndexedDrawElementsCount );
   }
 }
 
@@ -579,6 +591,11 @@ void Renderer::SetSortAttributes( BufferIndex bufferIndex,
   sortAttributes.geometry = mGeometry;
 }
 
+void Renderer::SetShaderChanged( bool value )
+{
+  mShaderChanged = value;
+}
+
 } // namespace SceneGraph
 
 } // namespace Internal