[3.0] Fix support for NativeImage
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.cpp
index c100895..38370dc 100644 (file)
@@ -79,11 +79,7 @@ namespace
 const char* gStdAttribs[ Program::ATTRIB_TYPE_LAST ] =
 {
   "aPosition",    // ATTRIB_POSITION
-  "aNormal",      // ATTRIB_NORMAL
   "aTexCoord",    // ATTRIB_TEXCOORD
-  "aColor",       // ATTRIB_COLOR
-  "aBoneWeights", // ATTRIB_BONE_WEIGHTS
-  "aBoneIndices"  // ATTRIB_BONE_INDICES
 };
 
 const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
@@ -116,7 +112,6 @@ Program* Program::New( ProgramCache& cache, Internal::ShaderDataPtr shaderData,
     program = new Program( cache, shaderData, modifiesGeometry );
 
     // we want to lazy load programs so dont do a Load yet, it gets done in Use()
-
     cache.AddProgram( shaderHash, program );
   }
 
@@ -227,6 +222,91 @@ GLint Program::GetUniformLocation( unsigned int uniformIndex )
   return location;
 }
 
+namespace
+{
+/**
+ * This struct is used to record the position of a uniform declaration
+ * within the fragment shader source code.
+ */
+struct LocationPosition
+{
+  GLint uniformLocation; ///< The location of the uniform (used as an identifier)
+  int characterPosition; ///< the position of the uniform declaration
+  LocationPosition( GLint uniformLocation, int characterPosition )
+  : uniformLocation(uniformLocation), characterPosition(characterPosition)
+  {
+  }
+};
+
+bool sortByPosition( LocationPosition a, LocationPosition b )
+{
+  return a.characterPosition < b.characterPosition;
+}
+}
+
+void Program::GetActiveSamplerUniforms()
+{
+  GLint numberOfActiveUniforms = -1;
+  GLint uniformMaxNameLength=-1;
+
+  mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms );
+  mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength );
+
+  std::vector<std::string> samplerNames;
+  char name[uniformMaxNameLength+1]; // Allow for null terminator
+  std::vector< LocationPosition >  samplerUniformLocations;
+
+  {
+    int nameLength = -1;
+    int number = -1;
+    GLenum type = GL_ZERO;
+
+    for( int i=0; i<numberOfActiveUniforms; ++i )
+    {
+      mGlAbstraction.GetActiveUniform( mProgramId, (GLuint)i, uniformMaxNameLength,
+                                       &nameLength, &number, &type, name );
+
+      if( type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES )
+      {
+        GLuint location = mGlAbstraction.GetUniformLocation( mProgramId, name );
+        samplerNames.push_back(name);
+        samplerUniformLocations.push_back(LocationPosition(location, 0u));
+      }
+    }
+  }
+
+  if( samplerUniformLocations.size() > 1 )
+  {
+    // Now, re-order according to declaration order in the fragment source.
+    std::string fragShader( mProgramData->GetFragmentShader() );
+    for( unsigned int i=0; i<samplerUniformLocations.size(); ++i )
+    {
+      // Better to write own search algorithm that searches for all of
+      // the sampler names simultaneously, ensuring only one iteration
+      // over fragShader.
+      size_t characterPosition = fragShader.find( samplerNames[i] );
+      samplerUniformLocations[i].characterPosition = characterPosition;
+    }
+    std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
+  }
+
+  for( unsigned int i=0; i<samplerUniformLocations.size(); ++i )
+  {
+    mSamplerUniformLocations.push_back( samplerUniformLocations[i].uniformLocation );
+  }
+}
+
+bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location  )
+{
+  bool result = false;
+  if( index < mSamplerUniformLocations.size() )
+  {
+    location = mSamplerUniformLocations[index];
+    result = true;
+  }
+  return result;
+}
+
 void Program::SetUniform1i( GLint location, GLint value0 )
 {
   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
@@ -323,9 +403,28 @@ void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
     return;
   }
 
-  // Not caching these as based on current analysis this is not called that often by our shaders
-  LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
-  CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
+  // check if uniform location fits the cache
+  if( location >= MAX_UNIFORM_CACHE_SIZE )
+  {
+    // not cached, make the gl call
+    LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
+    CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
+  }
+  else
+  {
+    // check if the same value has already been set, reset if it is different
+    if( ( fabsf(value0 - mUniformCacheFloat2[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
+        ( fabsf(value1 - mUniformCacheFloat2[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 ) )
+    {
+      // make the gl call
+      LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
+      CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
+
+      // update cache
+      mUniformCacheFloat2[ location ][ 0 ] = value0;
+      mUniformCacheFloat2[ location ][ 1 ] = value1;
+    }
+  }
 }
 
 void Program::SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
@@ -482,6 +581,7 @@ Program::Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool
   {
     RegisterUniform( gStdUniforms[ i ] );
   }
+
   // reset values
   ResetAttribsUniformCache();
 }
@@ -572,6 +672,8 @@ void Program::Load()
     }
   }
 
+  GetActiveSamplerUniforms();
+
   // No longer needed
   FreeShaders();
 }
@@ -702,6 +804,8 @@ void Program::ResetAttribsUniformCache()
     mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
   }
 
+  mSamplerUniformLocations.clear();
+
   // reset uniform caches
   mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
 
@@ -710,6 +814,8 @@ void Program::ResetAttribsUniformCache()
     // GL initializes uniforms to 0
     mUniformCacheInt[ i ] = 0;
     mUniformCacheFloat[ i ] = 0.0f;
+    mUniformCacheFloat2[ i ][ 0 ] = 0.0f;
+    mUniformCacheFloat2[ i ][ 1 ] = 0.0f;
     mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
     mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
     mUniformCacheFloat4[ i ][ 2 ] = 0.0f;