/*
- * 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.
// EXTERNAL INCLUDES
#include <iomanip>
+#include <cstring>
// INTERNAL INCLUDES
#include <dali/public-api/common/dali-common.h>
#include <dali/public-api/common/dali-vector.h>
#include <dali/public-api/common/constants.h>
#include <dali/integration-api/debug.h>
-#include <dali/integration-api/shader-data.h>
+#include <dali/internal/common/shader-data.h>
#include <dali/integration-api/gl-defines.h>
#include <dali/internal/render/common/performance-monitor.h>
#include <dali/internal/render/shaders/program-cache.h>
{
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
- "aNormal", // ATTRIB_NORMAL
"aTexCoord", // ATTRIB_TEXCOORD
- "aColor", // ATTRIB_COLOR
- "aBoneWeights", // ATTRIB_BONE_WEIGHTS
- "aBoneIndices" // ATTRIB_BONE_INDICES
};
-const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
+const char* const gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
{
"uMvpMatrix", // UNIFORM_MVP_MATRIX
"uModelView", // UNIFORM_MODELVIEW_MATRIX
"uViewMatrix", // UNIFORM_VIEW_MATRIX,
"uNormalMatrix", // UNIFORM_NORMAL_MATRIX
"uColor", // UNIFORM_COLOR
- "uCustomTextureCoords", // UNIFORM_CUSTOM_TEXTURE_COORDS
"sTexture", // UNIFORM_SAMPLER
"sTextureRect", // UNIFORM_SAMPLER_RECT
"sEffect", // UNIFORM_EFFECT_SAMPLER
- "sEffectRect", // UNIFORM_EFFECT_SAMPLER_RECT
- "uTimeDelta", // UNIFORM_TIME_DELTA
- "sOpacityTexture", // UNIFORM_SAMPLER_OPACITY
- "sNormalMapTexture" // UNIFORM_SAMPLER_NORMAL_MAP
+ "uSize" // UNIFORM_SIZE
};
} // <unnamed> namespace
// IMPLEMENTATION
-Program* Program::New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
+Program* Program::New( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
{
size_t shaderHash = shaderData->GetHashValue();
Program* program = cache.GetProgram( shaderHash );
{
// 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() )
{
LOG_GL( "UseProgram(%d)\n", mProgramId );
CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(mProgramId) );
- INCREASE_COUNTER(PerformanceMonitor::SHADER_STATE_CHANGES);
mCache.SetCurrentProgram( this );
}
{
DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
- if( mAttribLocations[ type ] == ATTRIB_UNKNOWN )
+ return GetCustomAttributeLocation( type );
+}
+
+uint32_t Program::RegisterCustomAttribute( const std::string& name )
+{
+ uint32_t index = 0;
+ // find the value from cache
+ for( ;index < static_cast<uint32_t>( mAttributeLocations.size() ); ++index )
+ {
+ if( mAttributeLocations[ index ].first == name )
+ {
+ // name found so return index
+ return index;
+ }
+ }
+ // if we get here, index is one past end so push back the new name
+ mAttributeLocations.push_back( std::make_pair( name, ATTRIB_UNKNOWN ) );
+ return index;
+}
+
+GLint Program::GetCustomAttributeLocation( uint32_t attributeIndex )
+{
+ // debug check that index is within name cache
+ DALI_ASSERT_DEBUG( mAttributeLocations.size() > attributeIndex );
+
+ // check if we have already queried the location of the attribute
+ GLint location = mAttributeLocations[ attributeIndex ].second;
+
+ if( location == ATTRIB_UNKNOWN )
{
- GLint loc = CHECK_GL( mGlAbstraction, mGlAbstraction.GetAttribLocation( mProgramId, gStdAttribs[type] ) );
- mAttribLocations[ type ] = loc;
- LOG_GL( "GetAttribLocation(program=%d,%s) = %d\n", mProgramId, gStdAttribs[type], mAttribLocations[type] );
+ location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetAttribLocation( mProgramId, mAttributeLocations[ attributeIndex ].first.c_str() ) );
+
+ mAttributeLocations[ attributeIndex ].second = location;
+ LOG_GL( "GetAttributeLocation(program=%d,%s) = %d\n", mProgramId, mAttributeLocations[ attributeIndex ].first.c_str(), mAttributeLocations[ attributeIndex ].second );
}
- return mAttribLocations[type];
+ return location;
}
-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 );
return location;
}
+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() );
+}
+
void Program::SetUniform1i( GLint location, GLint value0 )
{
DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
return;
}
- // Not caching these as based on current analysis this is not called that often by our shaders
- LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
- CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
+ // check if uniform location fits the cache
+ if( location >= MAX_UNIFORM_CACHE_SIZE )
+ {
+ // not cached, make the gl call
+ LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
+ CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
+ }
+ else
+ {
+ // check if the same value has already been set, reset if it is different
+ if( ( fabsf(value0 - mUniformCacheFloat2[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
+ ( fabsf(value1 - mUniformCacheFloat2[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 ) )
+ {
+ // make the gl call
+ LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
+ CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
+
+ // update cache
+ mUniformCacheFloat2[ location ][ 0 ] = value0;
+ mUniformCacheFloat2[ location ][ 1 ] = value1;
+ }
+ }
+}
+
+void Program::SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
+{
+ if( ( fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1 )||
+ ( fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1 )||
+ ( fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1 ) )
+ {
+ mSizeUniformCache.x = value0;
+ mSizeUniformCache.y = value1;
+ mSizeUniformCache.z = value2;
+ SetUniform3f( location, value0, value1, value2 );
+ }
}
void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
return mModifiesGeometry;
}
-Program::Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
+Program::Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
: mCache( cache ),
mGlAbstraction( mCache.GetGlAbstraction() ),
mProjectionMatrix( NULL ),
mProgramData(shaderData),
mModifiesGeometry( modifiesGeometry )
{
+ // reserve space for standard attributes
+ mAttributeLocations.reserve( ATTRIB_TYPE_LAST );
+ 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 ] );
}
+
// reset values
ResetAttribsUniformCache();
}
void Program::Load()
{
DALI_ASSERT_ALWAYS( NULL != 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" );
mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
{
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) );
{
DALI_LOG_ERROR("Failed to load program binary \n");
- char* szLog = NULL;
GLint nLength;
CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
if(nLength > 0)
{
- szLog = new char[ nLength ];
- CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog ) );
- DALI_LOG_ERROR( "Program Link Error: %s\n", szLog );
-
- delete [] szLog;
+ Dali::Vector< char > szLog;
+ szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
+ CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() ) );
+ DALI_LOG_ERROR( "Program Link Error: %s\n", szLog.Begin() );
}
}
else
{
mLinked = true;
+ DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Reused binary.\n" );
}
}
{
GLint binaryLength = 0;
GLenum binaryFormat;
+ DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Compiled and linked.\n\nVS:\n%s\nFS:\n%s\n", mProgramData->GetVertexShader(), mProgramData->GetFragmentShader() );
CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
// Copy the bytecode to ShaderData
CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
mCache.StoreBinary( mProgramData );
+ DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Saved binary.\n" );
}
}
}
}
}
+ GetActiveSamplerUniforms();
+
// No longer needed
FreeShaders();
}
if(nLength > 0)
{
Dali::Vector< char > szLog;
- szLog.Resize( nLength );
+ szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
}
if(nLength > 0)
{
Dali::Vector< char > szLog;
- szLog.Resize( nLength );
+ szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() );
DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog.Begin() );
}
void Program::ResetAttribsUniformCache()
{
// reset attribute locations
- for( unsigned i = 0; i < ATTRIB_TYPE_LAST; ++i )
+ for( uint32_t i = 0; i < mAttributeLocations.size() ; ++i )
{
- mAttribLocations[ i ] = ATTRIB_UNKNOWN;
+ 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 cache
- for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
+ mSamplerUniformLocations.clear();
+
+ // reset uniform caches
+ mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
+
+ for( uint32_t i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
{
// GL initializes uniforms to 0
mUniformCacheInt[ i ] = 0;
mUniformCacheFloat[ i ] = 0.0f;
+ mUniformCacheFloat2[ i ][ 0 ] = 0.0f;
+ mUniformCacheFloat2[ i ][ 1 ] = 0.0f;
mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
mUniformCacheFloat4[ i ][ 2 ] = 0.0f;