/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
{
void LogWithLineNumbers( const char * source )
{
- unsigned int lineNumber = 0u;
- const char *prev = source;
- const char *ptr = prev;
+ uint32_t lineNumber = 0u;
+ const char* prev = source;
+ const char* ptr = prev;
while( true )
{
namespace
{
-const char* gStdAttribs[ Program::ATTRIB_TYPE_LAST ] =
+const char* const gStdAttribs[ Program::ATTRIB_TYPE_LAST ] =
{
"aPosition", // ATTRIB_POSITION
"aTexCoord", // ATTRIB_TEXCOORD
};
-const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
+const char* const gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
{
"uMvpMatrix", // UNIFORM_MVP_MATRIX
"uModelView", // UNIFORM_MODELVIEW_MATRIX
size_t shaderHash = shaderData->GetHashValue();
Program* program = cache.GetProgram( shaderHash );
- if( NULL == program )
+ if( nullptr == program )
{
// program not found so create it
program = new Program( cache, shaderData, modifiesGeometry );
return GetCustomAttributeLocation( type );
}
-unsigned int Program::RegisterCustomAttribute( const std::string& name )
+uint32_t Program::RegisterCustomAttribute( const std::string& name )
{
- unsigned int index = 0;
+ uint32_t index = 0;
// find the value from cache
- for( ;index < mAttributeLocations.size(); ++index )
+ for( ;index < static_cast<uint32_t>( mAttributeLocations.size() ); ++index )
{
if( mAttributeLocations[ index ].first == name )
{
return index;
}
-GLint Program::GetCustomAttributeLocation( unsigned int attributeIndex )
+GLint Program::GetCustomAttributeLocation( uint32_t attributeIndex )
{
// debug check that index is within name cache
DALI_ASSERT_DEBUG( mAttributeLocations.size() > attributeIndex );
}
-unsigned int Program::RegisterUniform( const std::string& name )
+uint32_t Program::RegisterUniform( const std::string& name )
{
- unsigned int index = 0;
+ uint32_t index = 0;
// find the value from cache
- for( ;index < mUniformLocations.size(); ++index )
+ for( ;index < static_cast<uint32_t>( mUniformLocations.size() ); ++index )
{
if( mUniformLocations[ index ].first == name )
{
return index;
}
-GLint Program::GetUniformLocation( unsigned int uniformIndex )
+GLint Program::GetUniformLocation( uint32_t uniformIndex )
{
// debug check that index is within name cache
DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
struct LocationPosition
{
GLint uniformLocation; ///< The location of the uniform (used as an identifier)
- int position; ///< the position of the uniform declaration
- LocationPosition( GLint uniformLocation, int position )
+ int32_t position; ///< the position of the uniform declaration
+ LocationPosition( GLint uniformLocation, int32_t position )
: uniformLocation(uniformLocation), position(position)
{
}
{
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()
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< std::string > samplerNames;
+ std::vector< char > name(uniformMaxNameLength + 1); // Allow for null terminator
std::vector< LocationPosition > samplerUniformLocations;
{
for( int i=0; i<numberOfActiveUniforms; ++i )
{
- mGlAbstraction.GetActiveUniform( mProgramId, (GLuint)i, uniformMaxNameLength,
- &nameLength, &number, &type, name );
+ mGlAbstraction.GetActiveUniform( mProgramId, static_cast< GLuint >( i ), uniformMaxNameLength,
+ &nameLength, &number, &type, name.data() );
- 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);
+ 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* nextPtr = NULL;
- const char* token = strtok_r( fragShader, " ;\n", &nextPtr );
+ char* uniform = strstr( fragShader, UNIFORM );
int samplerPosition = 0;
- while( token )
+ while ( uniform )
{
- if( ( strncmp( token, "sampler2D", 9u ) == 0 ) || ( strncmp( token, "samplerCube", 11u ) == 0 ) )
+ char* outerToken = strtok_r( uniform + UNIFORM.mLength, ";", &uniform );
+
+ char* nextPtr = nullptr;
+ char* token = strtok_r( outerToken, DELIMITERS, &nextPtr );
+ while ( token )
{
- bool found( false );
- token = strtok_r( NULL, " ;\n", &nextPtr );
- for( size_t i=0; i<samplerUniformLocations.size(); ++i )
+ if ( SAMPLER_PREFIX == token )
{
- if( samplerUniformLocations[i].position == -1 &&
- strncmp( token, samplerNames[i].c_str(), samplerNames[i].size() ) == 0 )
+ token += SAMPLER_PREFIX.mLength;
+ if ( std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES )
{
- samplerUniformLocations[i].position = samplerPosition++;
- found = true;
+ bool found( false );
+ token = strtok_r( nullptr, 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;
}
}
- if( !found )
- {
- DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token );
- }
- }
- else
- {
- token = strtok_r( NULL, " ;\n", &nextPtr );
+
+ token = strtok_r( nullptr, DELIMITERS, &nextPtr );
}
+
+ uniform = strstr( uniform, UNIFORM );
}
free( fragShader );
// Re-order according to declaration order in the fragment source.
- size_t samplerUniformCount = samplerUniformLocations.size();
+ uint32_t samplerUniformCount = static_cast<uint32_t>( samplerUniformLocations.size() );
if( samplerUniformCount > 1 )
{
std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
}
mSamplerUniformLocations.resize( samplerUniformCount );
- for( size_t i=0; i<samplerUniformCount; ++i )
+ for( uint32_t i=0; i<samplerUniformCount; ++i )
{
mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
}
}
-bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location )
+bool Program::GetSamplerUniformLocation( uint32_t index, GLint& location )
{
bool result = false;
if( index < mSamplerUniformLocations.size() )
return result;
}
-size_t Program::GetActiveSamplerCount() const
+uint32_t Program::GetActiveSamplerCount() const
{
- return mSamplerUniformLocations.size();
+ return static_cast<uint32_t>( mSamplerUniformLocations.size() );
}
void Program::SetUniform1i( GLint location, GLint value0 )
Program::Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
: mCache( cache ),
mGlAbstraction( mCache.GetGlAbstraction() ),
- mProjectionMatrix( NULL ),
- mViewMatrix( NULL ),
+ mProjectionMatrix( nullptr ),
+ mViewMatrix( nullptr ),
mLinked( false ),
mVertexShaderId( 0 ),
mFragmentShaderId( 0 ),
{
// reserve space for standard attributes
mAttributeLocations.reserve( ATTRIB_TYPE_LAST );
- for( int i=0; i<ATTRIB_TYPE_LAST; ++i )
+ for( uint32_t i = 0; i < ATTRIB_TYPE_LAST; ++i )
{
RegisterCustomAttribute( gStdAttribs[i] );
}
// reserve space for standard uniforms
mUniformLocations.reserve( UNIFORM_TYPE_LAST );
// reset built in uniform names in cache
- for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
+ for( uint32_t i = 0; i < UNIFORM_TYPE_LAST; ++i )
{
RegisterUniform( gStdUniforms[ i ] );
}
void Program::Load()
{
- DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
+ DALI_ASSERT_ALWAYS( nullptr != mProgramData.Get() && "Program data is not initialized" );
DALI_ASSERT_DEBUG( mProgramId == 0 && "mProgramId != 0, so about to leak a GL resource by overwriting it." );
LOG_GL( "CreateProgram()\n" );
{
DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
- CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
+ CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), static_cast<GLsizei>( mProgramData->GetBufferSize() ) ) ); // truncated
CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
// Allocate space for the bytecode in ShaderData
mProgramData->AllocateBuffer(binaryLength);
// Copy the bytecode to ShaderData
- CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
+ CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, nullptr, &binaryFormat, mProgramData->GetBufferData()) );
mCache.StoreBinary( mProgramData );
DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Saved binary.\n" );
}
{
CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
- mCache.SetCurrentProgram( NULL );
+ mCache.SetCurrentProgram( nullptr );
}
if (mProgramId)
}
LOG_GL( "ShaderSource(%d)\n", shaderId );
- CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
+ CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, nullptr ) );
LOG_GL( "CompileShader(%d)\n", shaderId );
CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
void Program::ResetAttribsUniformCache()
{
// reset attribute locations
- for( unsigned i = 0; i < mAttributeLocations.size() ; ++i )
+ for( uint32_t i = 0; i < mAttributeLocations.size() ; ++i )
{
mAttributeLocations[ i ].second = ATTRIB_UNKNOWN;
}
// reset all gl uniform locations
- for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
+ for( uint32_t i = 0; i < mUniformLocations.size(); ++i )
{
// reset gl program locations and names
mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
// reset uniform caches
mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
- for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
+ for( uint32_t i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
{
// GL initializes uniforms to 0
mUniformCacheInt[ i ] = 0;