+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)
+ int32_t position; ///< the position of the uniform declaration
+ LocationPosition( GLint uniformLocation, int32_t position )
+ : uniformLocation(uniformLocation), position(position)
+ {
+ }
+};
+
+bool sortByPosition( LocationPosition a, LocationPosition b )
+{
+ return a.position < b.position;
+}
+
+const char* const DELIMITERS = " \t\n";
+
+struct StringSize
+{
+ const char* const mString;
+ const uint32_t mLength;
+
+ template <uint32_t kLength>
+ constexpr StringSize(const char(&string)[kLength])
+ : mString(string),
+ mLength(kLength - 1) // remove terminating null; N.B. there should be no other null.
+ {}
+
+ operator const char*() const
+ {
+ return mString;
+ }
+};
+
+bool operator==(const StringSize& lhs, const char* rhs)
+{
+ return strncmp(lhs.mString, rhs, lhs.mLength) == 0;
+}
+
+constexpr StringSize UNIFORM{ "uniform" };
+constexpr StringSize SAMPLER_PREFIX{ "sampler" };
+constexpr StringSize SAMPLER_TYPES[] = {
+ "2D",
+ "Cube",
+ "ExternalOES"
+};
+
+constexpr auto END_SAMPLER_TYPES = SAMPLER_TYPES + std::extent<decltype(SAMPLER_TYPES)>::value;
+
+}
+
+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;
+ std::vector< 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, static_cast< GLuint >( i ), uniformMaxNameLength,
+ &nameLength, &number, &type, name.data() );
+
+ if( type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES )
+ {
+ GLuint location = mGlAbstraction.GetUniformLocation( mProgramId, name.data() );
+ samplerNames.push_back( name.data() );
+ samplerUniformLocations.push_back(LocationPosition(location, -1));
+ }
+ }
+ }
+
+ //Determine declaration order of each sampler
+ char* fragShader = strdup( mProgramData->GetFragmentShader() );
+ char* uniform = strstr( fragShader, UNIFORM );
+ int samplerPosition = 0;
+ while ( uniform )
+ {
+ char* outerToken = strtok_r( uniform + UNIFORM.mLength, ";", &uniform );
+
+ char* nextPtr = NULL;
+ char* token = strtok_r( outerToken, DELIMITERS, &nextPtr );
+ while ( token )
+ {
+ if ( SAMPLER_PREFIX == token )
+ {
+ token += SAMPLER_PREFIX.mLength;
+ if ( std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES )
+ {
+ bool found( false );
+ token = strtok_r( NULL, DELIMITERS, &nextPtr );
+ for (uint32_t i=0; i < static_cast<uint32_t>( samplerUniformLocations.size() ); ++i)
+ {
+ if ( samplerUniformLocations[i].position == -1 &&
+ strncmp(token, samplerNames[i].c_str(), samplerNames[i].size()) == 0 )
+ {
+ samplerUniformLocations[i].position = samplerPosition++;
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ {
+ DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token );
+ }
+ break;
+ }
+ }
+
+ token = strtok_r( NULL, DELIMITERS, &nextPtr );
+ }
+
+ uniform = strstr( uniform, UNIFORM );
+ }
+
+ free( fragShader );
+
+ // Re-order according to declaration order in the fragment source.
+ uint32_t samplerUniformCount = static_cast<uint32_t>( samplerUniformLocations.size() );
+ if( samplerUniformCount > 1 )
+ {
+ std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
+ }
+
+ mSamplerUniformLocations.resize( samplerUniformCount );
+ for( uint32_t i=0; i<samplerUniformCount; ++i )
+ {
+ mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
+ }
+}
+
+bool Program::GetSamplerUniformLocation( uint32_t index, GLint& location )
+{
+ bool result = false;
+ if( index < mSamplerUniformLocations.size() )
+ {
+ location = mSamplerUniformLocations[index];
+ result = true;
+ }
+ return result;
+}
+
+uint32_t Program::GetActiveSamplerCount() const
+{
+ return static_cast<uint32_t>( mSamplerUniformLocations.size() );
+}
+