Removed need for sampler uniform names 86/60886/5
authorDavid Steele <david.steele@samsung.com>
Wed, 2 Mar 2016 20:19:30 +0000 (20:19 +0000)
committerDavid Steele <david.steele@samsung.com>
Fri, 1 Apr 2016 17:53:37 +0000 (18:53 +0100)
Used glGetActiveUniform to introspect the shader and get the sampler
uniforms. No longer need to store the sampler names with the textures.

Change-Id: I8f2f78ca503a3ff6b615113cfc758350c0be3665
Signed-off-by: David Steele <david.steele@samsung.com>
dali/internal/render/renderers/render-renderer.cpp
dali/internal/render/shaders/program.cpp
dali/internal/render/shaders/program.h

index 2a93574..8d666b7 100644 (file)
@@ -367,39 +367,34 @@ bool Renderer::BindTextures( SceneGraph::TextureCache& textureCache, Program& pr
 
       if( result )
       {
-        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 )
+        GLint uniformLocation;
+
+        bool result = program.GetSamplerUniformLocation( i, uniformLocation );
+        if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
         {
           program.SetUniform1i( uniformLocation, 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())
-                                                       );
+          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;
         }
-        else
-        {
-          samplerBitfield = ImageSampler::DEFAULT_BITFIELD;
-        }
-
-        texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
-
-        ++textureUnit;
       }
     }
   }
index a2367f6..c707aa9 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 ) /// Is there a native sampler type?
+      {
+        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;
index 66db3db..0ed9ea2 100644 (file)
@@ -157,11 +157,17 @@ 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] uniqueIndex of the sampler uniform in local cache
-   * @return the index of the uniform in the GL program
+   * @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
    */
-  GLint GetSamplerUniformLocation( int32_t uniqueIndex, const std::string& samplerName );
+  bool GetSamplerUniformLocation( unsigned int index, GLint& location );
 
   /**
    * Sets the uniform value
@@ -361,12 +367,15 @@ 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
-  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
+  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
 
   // uniform value caching
   GLint mUniformCacheInt[ MAX_UNIFORM_CACHE_SIZE ];         ///< Value cache for uniforms of single int