/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// EXTERNAL INCLUDES
#include <iomanip>
+#include <cstring>
// INTERNAL INCLUDES
#include <dali/public-api/common/dali-common.h>
{
// program not found so create it
program = new Program( cache, shaderData, modifiesGeometry );
-
- // we want to lazy load programs so dont do a Load yet, it gets done in Use()
+ program->Load();
cache.AddProgram( shaderHash, program );
}
void Program::Use()
{
- if ( !mLinked )
- {
- Load();
- }
-
if ( mLinked )
{
if ( this != mCache.GetCurrentProgram() )
struct LocationPosition
{
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)
+ int position; ///< the position of the uniform declaration
+ LocationPosition( GLint uniformLocation, int position )
+ : uniformLocation(uniformLocation), position(position)
{
}
};
bool sortByPosition( LocationPosition a, LocationPosition b )
{
- return a.characterPosition < b.characterPosition;
+ return a.position < b.position;
}
}
for( int i=0; i<numberOfActiveUniforms; ++i )
{
- mGlAbstraction.GetActiveUniform( mProgramId, (GLuint)i, uniformMaxNameLength,
+ mGlAbstraction.GetActiveUniform( mProgramId, static_cast< GLuint >( i ), uniformMaxNameLength,
&nameLength, &number, &type, name );
- if( type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE ) /// Is there a native sampler type?
+ 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));
+ samplerUniformLocations.push_back(LocationPosition(location, -1));
}
}
}
- if( samplerUniformLocations.size() > 1 )
+ //Determine declaration order of each sampler
+ char* fragShader = strdup( mProgramData->GetFragmentShader() );
+ char* nextPtr = NULL;
+ const char* token = strtok_r( fragShader, " ;\n", &nextPtr );
+ int samplerPosition = 0;
+ while( token )
{
- // 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 )
+ if( ( strncmp( token, "sampler2D", 9u ) == 0 ) ||
+ ( strncmp( token, "samplerCube", 11u ) == 0 ) ||
+ ( strncmp( token, "samplerExternalOES", 18u ) == 0 ) )
+ {
+ bool found( false );
+ token = strtok_r( NULL, " ;\n", &nextPtr );
+ for( size_t i=0; i<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 );
+ }
+ }
+ else
{
- // 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;
+ token = strtok_r( NULL, " ;\n", &nextPtr );
}
+ }
+
+ free( fragShader );
+
+ // Re-order according to declaration order in the fragment source.
+ size_t samplerUniformCount = samplerUniformLocations.size();
+ if( samplerUniformCount > 1 )
+ {
std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
}
- for( unsigned int i=0; i<samplerUniformLocations.size(); ++i )
+ mSamplerUniformLocations.resize( samplerUniformCount );
+ for( size_t i=0; i<samplerUniformCount; ++i )
{
- mSamplerUniformLocations.push_back( samplerUniformLocations[i].uniformLocation );
+ mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
}
}
return result;
}
+size_t Program::GetActiveSamplerCount() const
+{
+ return mSamplerUniformLocations.size();
+}
+
void Program::SetUniform1i( GLint location, GLint value0 )
{
DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used