Revert "Removed need for sampler uniform names" 75/64675/2
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Mon, 4 Apr 2016 13:59:08 +0000 (06:59 -0700)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Mon, 4 Apr 2016 13:59:22 +0000 (06:59 -0700)
This reverts commit 9f94da7375ab73f889c82d60639434e72543c826.

Change-Id: I444e4aba29a14bc14daac0127f287a63c5e30776

dali/internal/render/renderers/render-renderer.cpp
dali/internal/render/shaders/program.cpp
dali/internal/render/shaders/program.h

index 8d666b7..2a93574 100644 (file)
@@ -367,34 +367,39 @@ bool Renderer::BindTextures( SceneGraph::TextureCache& textureCache, Program& pr
 
       if( result )
       {
-        GLint uniformLocation;
-
-        bool result = program.GetSamplerUniformLocation( i, uniformLocation );
-        if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
+        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 )
         {
           program.SetUniform1i( uniformLocation, textureUnit );
+        }
 
-          unsigned int samplerBitfield(0);
-          Render::Texture& textureMapping = textures[i];
-          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;
-          }
-
-          texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
-
-          ++textureUnit;
+        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;
+        }
+
+        texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
+
+        ++textureUnit;
       }
     }
   }
index c707aa9..a2367f6 100644 (file)
@@ -222,89 +222,29 @@ 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 Program::GetSamplerUniformLocation( int32_t uniqueIndex, const std::string& samplerName  )
 {
-  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 );
+  // 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
 
-      if( type == GL_SAMPLER_2D ) /// Is there a native sampler type?
-      {
-        GLuint location = mGlAbstraction.GetUniformLocation( mProgramId, name );
-        samplerNames.push_back(name);
-        samplerUniformLocations.push_back(LocationPosition(location, 0u));
-      }
-    }
-  }
+  GLint location = UNIFORM_NOT_QUERIED;
 
-  if( samplerUniformLocations.size() > 1 )
+  if( index < mSamplerUniformLocations.Size() )
   {
-    // 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);
+    location = mSamplerUniformLocations[ index ];
   }
-
-  for( unsigned int i=0; i<samplerUniformLocations.size(); ++i )
+  else
   {
-    mSamplerUniformLocations.push_back( samplerUniformLocations[i].uniformLocation );
+    // not in cache yet, make space and initialize value to not queried
+    mSamplerUniformLocations.Resize( index + 1, UNIFORM_NOT_QUERIED );
   }
-}
-
-bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location  )
-{
-  bool result = false;
-  if( index < mSamplerUniformLocations.size() )
+  if( location == UNIFORM_NOT_QUERIED )
   {
-    location = mSamplerUniformLocations[index];
-    result = true;
+    location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, samplerName.c_str() ) );
+    mSamplerUniformLocations[ index ] = location;
   }
-  return result;
+  return location;
 }
 
 void Program::SetUniform1i( GLint location, GLint value0 )
@@ -672,8 +612,6 @@ void Program::Load()
     }
   }
 
-  GetActiveSamplerUniforms();
-
   // No longer needed
   FreeShaders();
 }
@@ -804,7 +742,10 @@ void Program::ResetAttribsUniformCache()
     mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
   }
 
-  mSamplerUniformLocations.clear();
+  for( unsigned int i = 0; i < mSamplerUniformLocations.Size(); ++i )
+  {
+    mSamplerUniformLocations[ i ] = UNIFORM_NOT_QUERIED;
+  }
 
   // reset uniform caches
   mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
index 0ed9ea2..66db3db 100644 (file)
@@ -157,17 +157,11 @@ public:
   GLint GetUniformLocation( unsigned int uniformIndex );
 
   /**
-   * Introspect the newly loaded shader to get the active sampler locations
-   */
-  void GetActiveSamplerUniforms();
-
-  /**
    * Gets the uniform location for a sampler
-   * @param [in] index The index of the active sampler
-   * @param [out] location The location of the requested sampler
-   * @return true if the active sampler was found
+   * @param [in] uniqueIndex of the sampler uniform in local cache
+   * @return the index of the uniform in the GL program
    */
-  bool GetSamplerUniformLocation( unsigned int index, GLint& location );
+  GLint GetSamplerUniformLocation( int32_t uniqueIndex, const std::string& samplerName );
 
   /**
    * Sets the uniform value
@@ -367,15 +361,12 @@ private:  // Data
   GLuint mVertexShaderId;                     ///< GL identifier for vertex shader
   GLuint mFragmentShaderId;                   ///< GL identifier for fragment shader
   GLuint mProgramId;                          ///< GL identifier for program
-  Internal::ShaderDataPtr mProgramData;       ///< Shader program source and binary (when compiled & linked or loaded)
+  Internal::ShaderDataPtr mProgramData;    ///< Shader program source and binary (when compiled & linked or loaded)
 
   // location caches
-  typedef std::pair< std::string, GLint > NameLocationPair;
-  typedef std::vector< NameLocationPair > Locations;
-
-  Locations mAttributeLocations;      ///< attribute location cache
-  Locations mUniformLocations;        ///< uniform location cache
-  std::vector<GLint> mSamplerUniformLocations; ///< sampler uniform location cache
+  std::vector< std::pair< std::string, GLint > > mAttributeLocations; ///< attribute location cache
+  std::vector< std::pair< std::string, GLint > > mUniformLocations; ///< uniform location cache
+  Dali::Vector< GLint > mSamplerUniformLocations; ///< sampler uniform location cache
 
   // uniform value caching
   GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ];         ///< Value cache for uniforms of single int