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;
}
}
}
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 )
}
}
+ GetActiveSamplerUniforms();
+
// No longer needed
FreeShaders();
}
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;
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
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