Rendering API Stencil Implementation
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.cpp
index cdd8752..01c03e3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 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.
 // CLASS HEADER
 #include <dali/internal/render/renderers/render-renderer.h>
 
-
 // INTERNAL INCLUDES
 #include <dali/internal/common/image-sampler.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>
 #include <dali/internal/render/data-providers/uniform-name-cache.h>
-#include <dali/internal/render/gl-resources/texture.h>
 #include <dali/internal/render/gl-resources/texture-cache.h>
 #include <dali/public-api/actors/blending.h>
+#include <dali/internal/render/gl-resources/gl-texture.h>
 
 namespace Dali
 {
@@ -110,31 +110,49 @@ namespace Render
 {
 
 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
-                         SceneGraph::RenderGeometry* renderGeometry,
+                         Render::Geometry* geometry,
                          unsigned int blendingBitmask,
                          const Vector4* blendColor,
-                         Dali::Renderer::FaceCullingMode faceCullingMode,
-                         bool preMultipliedAlphaEnabled )
+                         FaceCullingMode::Type faceCullingMode,
+                         bool preMultipliedAlphaEnabled,
+                         DepthWriteMode::Type depthWriteMode,
+                         DepthTestMode::Type depthTestMode,
+                         DepthFunction::Type depthFunction,
+                         StencilParameters& stencilParameters,
+                         bool writeToColorBuffer )
 {
-  return new Renderer( dataProvider, renderGeometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled );
+  return new Renderer( dataProvider, geometry, blendingBitmask, blendColor,
+                       faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode,
+                       depthFunction, stencilParameters, writeToColorBuffer );
 }
 
 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
-                    SceneGraph::RenderGeometry* renderGeometry,
+                    Render::Geometry* geometry,
                     unsigned int blendingBitmask,
                     const Vector4* blendColor,
-                    Dali::Renderer::FaceCullingMode faceCullingMode,
-                    bool preMultipliedAlphaEnabled)
+                    FaceCullingMode::Type faceCullingMode,
+                    bool preMultipliedAlphaEnabled,
+                    DepthWriteMode::Type depthWriteMode,
+                    DepthTestMode::Type depthTestMode,
+                    DepthFunction::Type depthFunction,
+                    StencilParameters& stencilParameters,
+                    bool writeToColorBuffer )
 : mRenderDataProvider( dataProvider ),
-  mContext(NULL),
+  mContext( NULL),
   mTextureCache( NULL ),
   mUniformNameCache( NULL ),
-  mRenderGeometry( renderGeometry ),
+  mGeometry( geometry ),
   mUniformIndexMap(),
   mAttributesLocation(),
+  mStencilParameters( stencilParameters ),
   mBlendingOptions(),
-  mFaceCullingMode( faceCullingMode  ),
-  mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
+  mIndexedDrawFirstElement( 0 ),
+  mIndexedDrawElementsCount( 0 ),
+  mDepthFunction( depthFunction ),
+  mFaceCullingMode( faceCullingMode ),
+  mDepthWriteMode( depthWriteMode ),
+  mDepthTestMode( depthTestMode ),
+  mWriteToColorBuffer( writeToColorBuffer ),
   mUpdateAttributesLocation( true ),
   mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
 {
@@ -166,20 +184,12 @@ void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvid
   mUpdateAttributesLocation = true;
 }
 
-void Renderer::SetGeometry( SceneGraph::RenderGeometry* renderGeometry )
+void Renderer::SetGeometry( Render::Geometry* geometry )
 {
-  mRenderGeometry = renderGeometry;
+  mGeometry = geometry;
   mUpdateAttributesLocation = true;
 }
 
-// Note - this is currently called from UpdateThread, PrepareRenderInstructions,
-// as an optimisation.
-// @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
-bool Renderer::RequiresDepthTest() const
-{
-  return mRenderGeometry->RequiresDepthTest();
-}
-
 void Renderer::SetBlending( Context& context, bool blend )
 {
   context.SetBlend( blend );
@@ -210,14 +220,14 @@ void Renderer::SetBlending( Context& context, bool blend )
 
 void Renderer::GlContextDestroyed()
 {
-  mRenderGeometry->GlContextDestroyed();
+  mGeometry->GlContextDestroyed();
 }
 
 void Renderer::GlCleanup()
 {
 }
 
-void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, 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" );
@@ -278,7 +288,6 @@ void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataP
   GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
   if( -1 != sizeLoc )
   {
-    Vector3 size = node.GetRenderSize( bufferIndex );
     program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
   }
 }
@@ -352,57 +361,68 @@ void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program
   }
 }
 
-void Renderer::BindTextures( SceneGraph::TextureCache& textureCache, Program& program )
+bool Renderer::BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program )
 {
-  int textureUnit = 0;
+  unsigned int textureUnit = 0;
+  bool result = true;
+
+  std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
 
   std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
-  for( size_t i(0); i<textures.size(); ++i )
+  GLint uniformLocation(-1);
+  for( size_t i(0); result && i<textures.size(); ++i )
   {
     ResourceId textureId = textures[i].GetTextureId();
     Internal::Texture* texture = textureCache.GetTexture( textureId );
     if( texture )
     {
-      textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
+      result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
 
-      Render::Texture& textureMapping = textures[i];
-      // Set sampler uniform location for the texture
-      int32_t uniqueIndex = textureMapping.GetUniformUniqueIndex();
-      if( Render::Texture::NOT_INITIALIZED == uniqueIndex )
-      {
-        uniqueIndex = mUniformNameCache->GetSamplerUniformUniqueIndex( textureMapping.GetUniformName() );
-        textureMapping.SetUniformUniqueIndex( uniqueIndex );
-      }
-      GLint uniformLocation = program.GetSamplerUniformLocation( uniqueIndex, textureMapping.GetUniformName() );
-      if( Program::UNIFORM_UNKNOWN != uniformLocation )
+      if( result )
       {
-        program.SetUniform1i( uniformLocation, textureUnit );
-      }
+        GLint uniformLocation;
 
-      unsigned int samplerBitfield(0);
-      const Render::Sampler* sampler( textureMapping.GetSampler() );
-      if( sampler )
-      {
-        samplerBitfield = ImageSampler::PackBitfield(
-          static_cast< FilterMode::Type >(sampler->GetMinifyFilterMode()),
-          static_cast< FilterMode::Type >(sampler->GetMagnifyFilterMode()),
-          static_cast< WrapMode::Type >(sampler->GetUWrapMode()),
-          static_cast< WrapMode::Type >(sampler->GetVWrapMode())
-          );
-      }
-      else
-      {
-        samplerBitfield = ImageSampler::DEFAULT_BITFIELD;
+        //TODO : This is a bug, result variable is being shadowed. Fix it!
+        bool result = program.GetSamplerUniformLocation( i, uniformLocation );
+        if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
+        {
+          program.SetUniform1i( uniformLocation, textureUnit );
+
+          unsigned int samplerBitfield(ImageSampler::DEFAULT_BITFIELD);
+          const Render::Sampler* sampler(  samplers[i] );
+          if( sampler )
+          {
+            samplerBitfield = sampler->mBitfield;
+          }
+
+          texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
+
+          ++textureUnit;
+        }
       }
+    }
+  }
 
-      texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
+  std::vector<Render::NewTexture*>& newTextures( mRenderDataProvider->GetNewTextures() );
+  for( size_t i(0); result && i<newTextures.size(); ++i )
+  {
+    if( newTextures[i] )
+    {
+      result = program.GetSamplerUniformLocation( i, uniformLocation ) &&
+               newTextures[i]->Bind(context, textureUnit, samplers[i] );
 
-      ++textureUnit;
+      if( result )
+      {
+        program.SetUniform1i( uniformLocation, textureUnit );
+        ++textureUnit;
+      }
     }
   }
+
+  return result;
 }
 
-void Renderer::SetFaceCullingMode( Dali::Renderer::FaceCullingMode mode )
+void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
 {
   mFaceCullingMode =  mode;
 }
@@ -417,14 +437,139 @@ void Renderer::SetBlendColor( const Vector4* color )
   mBlendingOptions.SetBlendColor( *color );
 }
 
+void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
+{
+  mIndexedDrawFirstElement = firstElement;
+}
+
+void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
+{
+  mIndexedDrawElementsCount = elementsCount;
+}
+
 void Renderer::EnablePreMultipliedAlpha( bool enable )
 {
   mPremultipledAlphaEnabled = enable;
 }
 
-void Renderer::SetSampler( unsigned int samplerBitfield )
+void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
+{
+  mDepthWriteMode = depthWriteMode;
+}
+
+void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
+{
+  mDepthTestMode = depthTestMode;
+}
+
+DepthWriteMode::Type Renderer::GetDepthWriteMode() const
+{
+  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::SetStencilMode( StencilMode::Type stencilMode )
+{
+  mStencilParameters.stencilMode = stencilMode;
+}
+
+StencilMode::Type Renderer::GetStencilMode() const
+{
+  return mStencilParameters.stencilMode;
+}
+
+void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
+{
+  mStencilParameters.stencilFunction = stencilFunction;
+}
+
+StencilFunction::Type Renderer::GetStencilFunction() const
 {
-  mSamplerBitfield = samplerBitfield;
+  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;
+}
+
+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;
+}
+
+void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
+{
+  mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
+}
+
+StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
+{
+  return mStencilParameters.stencilOperationOnZPass;
+}
+
+void Renderer::SetWriteToColorBuffer( bool writeToColorBuffer )
+{
+  mWriteToColorBuffer = writeToColorBuffer;
+}
+
+bool Renderer::GetWriteToColorBuffer() const
+{
+  return mWriteToColorBuffer;
 }
 
 void Renderer::Render( Context& context,
@@ -432,9 +577,11 @@ void Renderer::Render( Context& context,
                        BufferIndex bufferIndex,
                        const SceneGraph::NodeDataProvider& node,
                        SceneGraph::Shader& defaultShader,
+                       const Matrix& modelMatrix,
                        const Matrix& modelViewMatrix,
                        const Matrix& viewMatrix,
                        const Matrix& projectionMatrix,
+                       const Vector3& size,
                        bool blend )
 {
   // Get the program to use:
@@ -460,37 +607,38 @@ void Renderer::Render( Context& context,
   // Take the program into use so we can send uniforms to it
   program->Use();
 
-  // set projection and view matrix if program has not yet received them yet this frame
-  SetMatrices( *program, node.GetModelMatrix( bufferIndex ), viewMatrix, projectionMatrix, modelViewMatrix );
-
-  // set color uniform
-  GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
-  if( Program::UNIFORM_UNKNOWN != loc )
+  if( DALI_LIKELY( BindTextures( context, textureCache, *program ) ) )
   {
-    const Vector4& color = node.GetRenderColor( bufferIndex );
-    if( mPremultipledAlphaEnabled )
-    {
-      program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
-    }
-    else
+    // 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 )
     {
-      program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
+      const Vector4& color = node.GetRenderColor( bufferIndex );
+      if( mPremultipledAlphaEnabled )
+      {
+        program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
+      }
+      else
+      {
+        program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
+      }
     }
-  }
 
-  //Bind textures
-  BindTextures( textureCache, *program );
+    SetUniforms( bufferIndex, node, size, *program );
 
-  //Set uniforms
-  SetUniforms( bufferIndex, node, *program );
+    if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
+    {
+      mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
+      mUpdateAttributesLocation = false;
+    }
 
-  if( mUpdateAttributesLocation || mRenderGeometry->AttributesChanged() )
-  {
-    mRenderGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
-    mUpdateAttributesLocation = false;
+    mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
   }
-
-  mRenderGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation );
 }
 
 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
@@ -506,7 +654,7 @@ void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererW
     sortAttributes.textureResourceId = Integration::InvalidResourceId;
   }
 
-  sortAttributes.geometry = mRenderGeometry;
+  sortAttributes.geometry = mGeometry;
 }
 
 } // namespace SceneGraph