Use stack variable instead of global when computing matrix
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.cpp
index 40fb042..e3f1684 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 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.
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
+ *
  */
 
-#include "render-renderer.h"
+// CLASS HEADER
+#include <dali/internal/render/renderers/render-renderer.h>
 
+// INTERNAL INCLUDES
 #include <dali/internal/common/image-sampler.h>
-#include <dali/internal/event/common/property-input-impl.h>
-#include <dali/internal/update/common/uniform-map.h>
-#include <dali/internal/render/data-providers/render-data-provider.h>
-#include <dali/internal/render/gl-resources/texture.h>
-#include <dali/internal/render/gl-resources/texture-cache.h>
+#include <dali/internal/render/gl-resources/context.h>
+#include <dali/internal/render/renderers/render-sampler.h>
+#include <dali/internal/render/shaders/scene-graph-shader.h>
 #include <dali/internal/render/shaders/program.h>
+#include <dali/internal/render/data-providers/node-data-provider.h>
 
 namespace Dali
 {
+
 namespace Internal
 {
-namespace SceneGraph
-{
 
-NewRenderer* NewRenderer::New( NodeDataProvider& nodeDataProvider,
-                               RenderDataProvider* dataProvider )
+namespace
 {
-  return new NewRenderer(nodeDataProvider, dataProvider);
-}
 
 
-NewRenderer::NewRenderer( NodeDataProvider& nodeDataProvider,
-                          RenderDataProvider* dataProvider )
-: Renderer( nodeDataProvider ),
-  mRenderDataProvider( dataProvider )
+/**
+ * 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() );
+  }
 
-NewRenderer::~NewRenderer()
-{
-}
+  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() );
+  }
 
-void NewRenderer::SetRenderDataProvider( RenderDataProvider* dataProvider )
-{
-  mRenderDataProvider = dataProvider;
-  mRenderGeometry.GeometryUpdated();
+  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() );
+  }
 }
 
-// Note - this is currently called from UpdateThread, PrepareRenderInstructions,
-// as an optimisation.
-// @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
-bool NewRenderer::RequiresDepthTest() const
-{
-  return true;
 }
 
-bool NewRenderer::CheckResources()
+namespace Render
 {
-  // Query material to check it has texture pointers & image has size
-  // Query geometry to check it has vertex buffers
 
-  // General point though - why would we have a render item in RenderThread with no ready
-  // resources in UpdateThread?
-  return true;
+Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
+                         Render::Geometry* geometry,
+                         uint32_t blendingBitmask,
+                         const Vector4& blendColor,
+                         FaceCullingMode::Type faceCullingMode,
+                         bool preMultipliedAlphaEnabled,
+                         DepthWriteMode::Type depthWriteMode,
+                         DepthTestMode::Type depthTestMode,
+                         DepthFunction::Type depthFunction,
+                         StencilParameters& stencilParameters )
+{
+  return new Renderer( dataProvider, geometry, blendingBitmask, blendColor,
+                       faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode,
+                       depthFunction, stencilParameters );
 }
 
-void NewRenderer::ResolveGeometryTypes( BufferIndex bufferIndex, GeometryType& outType, ShaderSubTypes& outSubType )
+Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
+                    Render::Geometry* geometry,
+                    uint32_t blendingBitmask,
+                    const Vector4& blendColor,
+                    FaceCullingMode::Type faceCullingMode,
+                    bool preMultipliedAlphaEnabled,
+                    DepthWriteMode::Type depthWriteMode,
+                    DepthTestMode::Type depthTestMode,
+                    DepthFunction::Type depthFunction,
+                    StencilParameters& stencilParameters )
+: mRenderDataProvider( dataProvider ),
+  mContext( NULL),
+  mGeometry( geometry ),
+  mUniformIndexMap(),
+  mAttributesLocation(),
+  mStencilParameters( stencilParameters ),
+  mBlendingOptions(),
+  mIndexedDrawFirstElement( 0 ),
+  mIndexedDrawElementsCount( 0 ),
+  mDepthFunction( depthFunction ),
+  mFaceCullingMode( faceCullingMode ),
+  mDepthWriteMode( depthWriteMode ),
+  mDepthTestMode( depthTestMode ),
+  mUpdateAttributesLocation( true ),
+  mPremultipledAlphaEnabled( preMultipliedAlphaEnabled ),
+  mShaderChanged( false )
 {
-  // @todo MESH_REWORK Remove after merge
+  if( blendingBitmask != 0u )
+  {
+    mBlendingOptions.SetBitmask( blendingBitmask );
+  }
 
-  // Do nothing
+  mBlendingOptions.SetBlendColor( blendColor );
 }
 
-bool NewRenderer::IsOutsideClipSpace( Context& context, const Matrix& modelMatrix, const Matrix& modelViewProjectionMatrix )
+void Renderer::Initialize( Context& context )
 {
-  // @todo MESH_REWORK Add clipping
-  return false;
+  mContext = &context;
 }
 
-void NewRenderer::DoSetUniforms( Context& context, BufferIndex bufferIndex, Shader* shader, Program* program, unsigned int programIndex, ShaderSubTypes subType )
+Renderer::~Renderer()
 {
-  // Do nothing, we're going to set up the uniforms with our own code instead
 }
 
-void NewRenderer::DoSetCullFaceMode( Context& context, BufferIndex bufferIndex )
+void Renderer::SetGeometry( Render::Geometry* geometry )
 {
+  mGeometry = geometry;
+  mUpdateAttributesLocation = true;
 }
 
-void NewRenderer::DoSetBlending( Context& context, BufferIndex bufferIndex )
+void Renderer::SetBlending( Context& context, bool blend )
 {
-  context.SetBlend(mUseBlend); // @todo MESH_REWORK Should use a RendererDataProvider
-
-  if( mUseBlend )
+  context.SetBlend( blend );
+  if( blend )
   {
-    const MaterialDataProvider& material = mRenderDataProvider->GetMaterial();
-
-    context.SetCustomBlendColor( material.GetBlendColor( bufferIndex ) );
+    // Blend color is optional and rarely used
+    const Vector4* blendColor = mBlendingOptions.GetBlendColor();
+    if( blendColor )
+    {
+      context.SetCustomBlendColor( *blendColor );
+    }
+    else
+    {
+      context.SetDefaultBlendColor();
+    }
 
     // Set blend source & destination factors
-    context.BlendFuncSeparate( material.GetBlendSrcFactorRgb( bufferIndex ),
-                               material.GetBlendDestFactorRgb( bufferIndex ),
-                               material.GetBlendSrcFactorAlpha( bufferIndex ),
-                               material.GetBlendDestFactorAlpha( bufferIndex ) );
+    context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
+                               mBlendingOptions.GetBlendDestFactorRgb(),
+                               mBlendingOptions.GetBlendSrcFactorAlpha(),
+                               mBlendingOptions.GetBlendDestFactorAlpha() );
 
     // Set blend equations
-    context.BlendEquationSeparate( material.GetBlendEquationRgb( bufferIndex ),
-                                   material.GetBlendEquationAlpha( bufferIndex ) );
+    context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
+                                   mBlendingOptions.GetBlendEquationAlpha() );
   }
 }
 
-void NewRenderer::DoRender( Context& context, TextureCache& textureCache, BufferIndex bufferIndex, Program& program, const Matrix& modelViewMatrix, const Matrix& viewMatrix )
-{
-  BindTextures( textureCache, bufferIndex, program, mRenderDataProvider->GetSamplers() );
-
-  SetUniforms( bufferIndex, program );
-
-  mRenderGeometry.UploadAndDraw( context, program, bufferIndex, mRenderDataProvider.Get() );
-}
-
-void NewRenderer::GlContextDestroyed()
+void Renderer::GlContextDestroyed()
 {
-  mRenderGeometry.GlContextDestroyed();
+  mGeometry->GlContextDestroyed();
 }
 
-void NewRenderer::GlCleanup()
+void Renderer::GlCleanup()
 {
 }
 
-void NewRenderer::SetUniforms( BufferIndex bufferIndex, Program& program )
+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 UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
+  const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
 
-  if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) )
+  if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
+      node.GetUniformMapChanged(bufferIndex) ||
+      mUniformIndexMap.Count() == 0 ||
+      mShaderChanged )
   {
-    const CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
+    // Reset shader pointer
+    mShaderChanged = false;
 
-    unsigned int numberOfMaps = uniformMap.Count();
+    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( numberOfMaps );
+    mUniformIndexMap.Resize( maxMaps );
 
-    // Remap uniform indexes to property value addresses
-    for( unsigned int mapIndex = 0 ; mapIndex < numberOfMaps ; ++mapIndex )
+    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
@@ -167,17 +271,14 @@ void NewRenderer::SetUniforms( BufferIndex bufferIndex, Program& program )
     SetUniformFromProperty( bufferIndex, program, *iter );
   }
 
-  // @todo MESH_REWORK On merge, copy code from renderer to setup standard matrices and color
-
   GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
   if( -1 != sizeLoc )
   {
-    Vector3 size = mDataProvider.GetRenderSize( bufferIndex );
-    program.SetUniform3f( sizeLoc, size.x, size.y, size.z );
+    program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
   }
 }
 
-void NewRenderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
+void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
 {
   GLint location = program.GetUniformLocation(map.uniformIndex);
   if( Program::UNIFORM_UNKNOWN != location )
@@ -246,105 +347,257 @@ void NewRenderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& prog
   }
 }
 
-void NewRenderer::BindTextures(
-  TextureCache& textureCache,
-  BufferIndex bufferIndex,
-  Program& program,
-  const RenderDataProvider::Samplers& samplers )
+bool Renderer::BindTextures( Context& context, Program& program, Vector<GLuint>& boundTextures )
 {
-  // @todo MESH_REWORK Write a cache of texture units to commonly used sampler textures
-  unsigned int textureUnit = 0;
+  uint32_t textureUnit = 0;
+  bool result = true;
 
-  for( RenderDataProvider::Samplers::Iterator iter = samplers.Begin();
-       iter != samplers.End();
-       ++iter )
+  GLint uniformLocation(-1);
+  std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
+  std::vector<Render::Texture*>& textures( mRenderDataProvider->GetTextures() );
+  for( uint32_t i = 0; i < static_cast<uint32_t>( textures.size() ) && result; ++i ) // not expecting more than uint32_t of textures
   {
-    const SamplerDataProvider* sampler = *iter;
-    ResourceId textureId = sampler->GetTextureId(bufferIndex);
-    Texture* texture = textureCache.GetTexture( textureId );
-    if( texture != NULL )
+    if( textures[i] )
     {
-      unsigned int textureUnitUniformIndex = GetTextureUnitUniformIndex( program, *sampler );
-      TextureUnit theTextureUnit = static_cast<TextureUnit>(textureUnit);
-      BindTexture( textureCache, program, textureId, texture, theTextureUnit, textureUnitUniformIndex );
-      ApplySampler( bufferIndex, texture, theTextureUnit, *sampler );
+      result = textures[i]->Bind(context, textureUnit, samplers[i] );
+      boundTextures.PushBack( textures[i]->GetId() );
+      if( result && program.GetSamplerUniformLocation( i, uniformLocation ) )
+      {
+        program.SetUniform1i( uniformLocation, textureUnit );
+        ++textureUnit;
+      }
     }
-
-    ++textureUnit;
   }
+
+  return result;
 }
 
-void NewRenderer::BindTexture(
-  TextureCache& textureCache,
-  Program& program,
-  ResourceId id,
-  Texture* texture,
-  TextureUnit textureUnit,
-  unsigned int textureUnitUniformIndex )
+void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
 {
-  if( texture != NULL )
-  {
-    textureCache.BindTexture( texture, id, GL_TEXTURE_2D, textureUnit );
+  mFaceCullingMode =  mode;
+}
 
-    // Set sampler uniform location for the texture
-    GLint textureUnitLoc = program.GetUniformLocation( textureUnitUniformIndex );
-    if( Program::UNIFORM_UNKNOWN != textureUnitLoc )
-    {
-      program.SetUniform1i( textureUnitLoc, textureUnit );
-    }
-  }
+void Renderer::SetBlendingBitMask( uint32_t bitmask )
+{
+  mBlendingOptions.SetBitmask( bitmask );
+}
+
+void Renderer::SetBlendColor( const Vector4& color )
+{
+  mBlendingOptions.SetBlendColor( color );
+}
+
+void Renderer::SetIndexedDrawFirstElement( uint32_t firstElement )
+{
+  mIndexedDrawFirstElement = firstElement;
+}
+
+void Renderer::SetIndexedDrawElementsCount( uint32_t elementsCount )
+{
+  mIndexedDrawElementsCount = elementsCount;
+}
+
+void Renderer::EnablePreMultipliedAlpha( bool enable )
+{
+  mPremultipledAlphaEnabled = enable;
+}
+
+void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
+{
+  mDepthWriteMode = depthWriteMode;
+}
+
+void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
+{
+  mDepthTestMode = depthTestMode;
 }
 
-void NewRenderer::ApplySampler(
-  BufferIndex bufferIndex,
-  Texture*    texture,
-  TextureUnit textureUnit,
-  const SamplerDataProvider& sampler )
+DepthWriteMode::Type Renderer::GetDepthWriteMode() const
 {
-  unsigned int samplerBitfield = ImageSampler::PackBitfield(
-    static_cast< FilterMode::Type >(sampler.GetMinifyFilterMode(bufferIndex)),
-    static_cast< FilterMode::Type >(sampler.GetMagnifyFilterMode(bufferIndex)) );
+  return mDepthWriteMode;
+}
+
+DepthTestMode::Type Renderer::GetDepthTestMode() const
+{
+  return mDepthTestMode;
+}
+
+void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
+{
+  mDepthFunction = depthFunction;
+}
+
+DepthFunction::Type Renderer::GetDepthFunction() const
+{
+  return mDepthFunction;
+}
+
+void Renderer::SetRenderMode( RenderMode::Type renderMode )
+{
+  mStencilParameters.renderMode = renderMode;
+}
+
+RenderMode::Type Renderer::GetRenderMode() const
+{
+  return mStencilParameters.renderMode;
+}
+
+void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
+{
+  mStencilParameters.stencilFunction = stencilFunction;
+}
+
+StencilFunction::Type Renderer::GetStencilFunction() const
+{
+  return mStencilParameters.stencilFunction;
+}
+
+void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
+{
+  mStencilParameters.stencilFunctionMask = stencilFunctionMask;
+}
+
+int Renderer::GetStencilFunctionMask() const
+{
+  return mStencilParameters.stencilFunctionMask;
+}
+
+void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
+{
+  mStencilParameters.stencilFunctionReference = stencilFunctionReference;
+}
+
+int Renderer::GetStencilFunctionReference() const
+{
+  return mStencilParameters.stencilFunctionReference;
+}
+
+void Renderer::SetStencilMask( int stencilMask )
+{
+  mStencilParameters.stencilMask = stencilMask;
+}
+
+int Renderer::GetStencilMask() const
+{
+  return mStencilParameters.stencilMask;
+}
 
-  texture->ApplySampler( textureUnit, samplerBitfield );
+void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
+{
+  mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
+}
+
+StencilOperation::Type Renderer::GetStencilOperationOnFail() const
+{
+  return mStencilParameters.stencilOperationOnFail;
+}
+
+void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
+{
+  mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
+}
+
+StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
+{
+  return mStencilParameters.stencilOperationOnZFail;
+}
 
-  // @todo MESH_REWORK add support for wrap modes
+void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
+{
+  mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
 }
 
-unsigned int NewRenderer::GetTextureUnitUniformIndex(
-  Program& program,
-  const SamplerDataProvider& sampler )
+StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
 {
-  // Find sampler in mSamplerNameCache
-  // If it doesn't exist,
-  //   get the index by calling program.RegisterUniform and store it
-  // If it exists, it's index should be set.
-  // @todo Cache should be reset on scene change
+  return mStencilParameters.stencilOperationOnZPass;
+}
 
-  unsigned int uniformIndex = 0;
-  bool found = false;
+void Renderer::Upload( Context& context )
+{
+  mGeometry->Upload( context );
+}
 
-  for( unsigned int i=0; i< mTextureUnitUniforms.Count(); ++i )
+void Renderer::Render( Context& context,
+                       BufferIndex bufferIndex,
+                       const SceneGraph::NodeDataProvider& node,
+                       const Matrix& modelMatrix,
+                       const Matrix& modelViewMatrix,
+                       const Matrix& viewMatrix,
+                       const Matrix& projectionMatrix,
+                       const Vector3& size,
+                       bool blend,
+                       Vector<GLuint>& boundTextures )
+{
+  // Get the program to use:
+  Program* program = mRenderDataProvider->GetShader().GetProgram();
+  if( !program )
   {
-    if( mTextureUnitUniforms[i].sampler == &sampler )
-    {
-      uniformIndex = mTextureUnitUniforms[i].index;
-      found = true;
-    }
+    DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", reinterpret_cast< void* >( &mRenderDataProvider->GetShader() ) );
+    return;
   }
 
-  if( ! found )
+  //Set cull face  mode
+  context.CullFace( mFaceCullingMode );
+
+  //Set blending mode
+  SetBlending( context, blend );
+
+  // Take the program into use so we can send uniforms to it
+  program->Use();
+
+  if( DALI_LIKELY( BindTextures( context, *program, boundTextures ) ) )
   {
-    TextureUnitUniformIndex textureUnitUniformIndex;
-    textureUnitUniformIndex.sampler = &sampler;
-    textureUnitUniformIndex.index = program.RegisterUniform( sampler.GetTextureUnitUniformName() );
-    mTextureUnitUniforms.PushBack( textureUnitUniformIndex );
-    uniformIndex = textureUnitUniformIndex.index;
+    // Only set up and draw if we have textures and they are all valid
+
+    // set projection and view matrix if program has not yet received them yet this frame
+    SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
+
+    // set color uniform
+    GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
+    if( Program::UNIFORM_UNKNOWN != loc )
+    {
+      const Vector4& color = node.GetRenderColor( bufferIndex );
+      if( mPremultipledAlphaEnabled )
+      {
+        float alpha = color.a * mRenderDataProvider->GetOpacity( bufferIndex );
+        program->SetUniform4f( loc, color.r * alpha, color.g * alpha, color.b * alpha, alpha );
+      }
+      else
+      {
+        program->SetUniform4f( loc, color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity( bufferIndex ) );
+      }
+    }
+
+    SetUniforms( bufferIndex, node, size, *program );
+
+    if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
+    {
+      mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
+      mUpdateAttributesLocation = false;
+    }
+
+    mGeometry->Draw( context,
+                     bufferIndex,
+                     mAttributesLocation,
+                     mIndexedDrawFirstElement,
+                     mIndexedDrawElementsCount );
   }
+}
 
-  return uniformIndex;
+void Renderer::SetSortAttributes( BufferIndex bufferIndex,
+                                  SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const
+{
+  sortAttributes.shader = &( mRenderDataProvider->GetShader() );
+  sortAttributes.geometry = mGeometry;
 }
 
+void Renderer::SetShaderChanged( bool value )
+{
+  mShaderChanged = value;
+}
+
+} // namespace SceneGraph
+
+} // namespace Internal
 
-} // SceneGraph
-} // Internal
-} // Dali
+} // namespace Dali