[3.0] Fix support for NativeImage
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.cpp
index a2367f6..38370dc 100644 (file)
@@ -222,29 +222,89 @@ GLint Program::GetUniformLocation( unsigned int uniformIndex )
   return location;
 }
 
-GLint Program::GetSamplerUniformLocation( int32_t uniqueIndex, const std::string& samplerName  )
+namespace
+{
+/**
+ * This struct is used to record the position of a uniform declaration
+ * within the fragment shader source code.
+ */
+struct LocationPosition
 {
-  // don't accept negative values (should never happen)
-  DALI_ASSERT_DEBUG( 0 <= uniqueIndex );
-  const uint32_t index( uniqueIndex ); // avoid compiler warning of signed vs unsigned comparisons
+  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 );
 
-  GLint location = UNIFORM_NOT_QUERIED;
+      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( index < mSamplerUniformLocations.Size() )
+  if( samplerUniformLocations.size() > 1 )
   {
-    location = mSamplerUniformLocations[ index ];
+    // 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);
   }
-  else
+
+  for( unsigned int i=0; i<samplerUniformLocations.size(); ++i )
   {
-    // not in cache yet, make space and initialize value to not queried
-    mSamplerUniformLocations.Resize( index + 1, UNIFORM_NOT_QUERIED );
+    mSamplerUniformLocations.push_back( samplerUniformLocations[i].uniformLocation );
   }
-  if( location == UNIFORM_NOT_QUERIED )
+}
+
+bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location  )
+{
+  bool result = false;
+  if( index < mSamplerUniformLocations.size() )
   {
-    location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, samplerName.c_str() ) );
-    mSamplerUniformLocations[ index ] = location;
+    location = mSamplerUniformLocations[index];
+    result = true;
   }
-  return location;
+  return result;
 }
 
 void Program::SetUniform1i( GLint location, GLint value0 )
@@ -612,6 +672,8 @@ void Program::Load()
     }
   }
 
+  GetActiveSamplerUniforms();
+
   // No longer needed
   FreeShaders();
 }
@@ -742,10 +804,7 @@ void Program::ResetAttribsUniformCache()
     mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
   }
 
-  for( unsigned int i = 0; i < mSamplerUniformLocations.Size(); ++i )
-  {
-    mSamplerUniformLocations[ i ] = UNIFORM_NOT_QUERIED;
-  }
+  mSamplerUniformLocations.clear();
 
   // reset uniform caches
   mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;