2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/render/shaders/program.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/dali-vector.h>
28 #include <dali/public-api/common/constants.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/internal/common/shader-data.h>
31 #include <dali/integration-api/gl-defines.h>
32 #include <dali/internal/render/common/performance-monitor.h>
33 #include <dali/internal/render/shaders/program-cache.h>
34 #include <dali/internal/render/gl-resources/gl-call-debug.h>
38 void LogWithLineNumbers( const char * source )
40 unsigned int lineNumber = 0u;
41 const char *prev = source;
42 const char *ptr = prev;
50 // seek the next end of line or end of text
51 while( *ptr!='\n' && *ptr != '\0' )
56 std::string line( prev, ptr-prev );
57 Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%4d %s\n", lineNumber, line.c_str());
80 const char* gStdAttribs[ Program::ATTRIB_TYPE_LAST ] =
82 "aPosition", // ATTRIB_POSITION
83 "aTexCoord", // ATTRIB_TEXCOORD
86 const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
88 "uMvpMatrix", // UNIFORM_MVP_MATRIX
89 "uModelView", // UNIFORM_MODELVIEW_MATRIX
90 "uProjection", // UNIFORM_PROJECTION_MATRIX
91 "uModelMatrix", // UNIFORM_MODEL_MATRIX,
92 "uViewMatrix", // UNIFORM_VIEW_MATRIX,
93 "uNormalMatrix", // UNIFORM_NORMAL_MATRIX
94 "uColor", // UNIFORM_COLOR
95 "sTexture", // UNIFORM_SAMPLER
96 "sTextureRect", // UNIFORM_SAMPLER_RECT
97 "sEffect", // UNIFORM_EFFECT_SAMPLER
98 "uSize" // UNIFORM_SIZE
101 } // <unnamed> namespace
105 Program* Program::New( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
107 size_t shaderHash = shaderData->GetHashValue();
108 Program* program = cache.GetProgram( shaderHash );
110 if( NULL == program )
112 // program not found so create it
113 program = new Program( cache, shaderData, modifiesGeometry );
115 cache.AddProgram( shaderHash, program );
125 if ( this != mCache.GetCurrentProgram() )
127 LOG_GL( "UseProgram(%d)\n", mProgramId );
128 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(mProgramId) );
130 mCache.SetCurrentProgram( this );
135 bool Program::IsUsed()
137 return ( this == mCache.GetCurrentProgram() );
140 GLint Program::GetAttribLocation( AttribType type )
142 DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
144 return GetCustomAttributeLocation( type );
147 unsigned int Program::RegisterCustomAttribute( const std::string& name )
149 unsigned int index = 0;
150 // find the value from cache
151 for( ;index < mAttributeLocations.size(); ++index )
153 if( mAttributeLocations[ index ].first == name )
155 // name found so return index
159 // if we get here, index is one past end so push back the new name
160 mAttributeLocations.push_back( std::make_pair( name, ATTRIB_UNKNOWN ) );
164 GLint Program::GetCustomAttributeLocation( unsigned int attributeIndex )
166 // debug check that index is within name cache
167 DALI_ASSERT_DEBUG( mAttributeLocations.size() > attributeIndex );
169 // check if we have already queried the location of the attribute
170 GLint location = mAttributeLocations[ attributeIndex ].second;
172 if( location == ATTRIB_UNKNOWN )
174 location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetAttribLocation( mProgramId, mAttributeLocations[ attributeIndex ].first.c_str() ) );
176 mAttributeLocations[ attributeIndex ].second = location;
177 LOG_GL( "GetAttributeLocation(program=%d,%s) = %d\n", mProgramId, mAttributeLocations[ attributeIndex ].first.c_str(), mAttributeLocations[ attributeIndex ].second );
184 unsigned int Program::RegisterUniform( const std::string& name )
186 unsigned int index = 0;
187 // find the value from cache
188 for( ;index < mUniformLocations.size(); ++index )
190 if( mUniformLocations[ index ].first == name )
192 // name found so return index
196 // if we get here, index is one past end so push back the new name
197 mUniformLocations.push_back( std::make_pair( name, UNIFORM_NOT_QUERIED ) );
201 GLint Program::GetUniformLocation( unsigned int uniformIndex )
203 // debug check that index is within name cache
204 DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
206 // check if we have already queried the location of the uniform
207 GLint location = mUniformLocations[ uniformIndex ].second;
209 if( location == UNIFORM_NOT_QUERIED )
211 location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, mUniformLocations[ uniformIndex ].first.c_str() ) );
213 mUniformLocations[ uniformIndex ].second = location;
214 LOG_GL( "GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[ uniformIndex ].first.c_str(), mUniformLocations[ uniformIndex ].second );
223 * This struct is used to record the position of a uniform declaration
224 * within the fragment shader source code.
226 struct LocationPosition
228 GLint uniformLocation; ///< The location of the uniform (used as an identifier)
229 int position; ///< the position of the uniform declaration
230 LocationPosition( GLint uniformLocation, int position )
231 : uniformLocation(uniformLocation), position(position)
236 bool sortByPosition( LocationPosition a, LocationPosition b )
238 return a.position < b.position;
242 void Program::GetActiveSamplerUniforms()
244 GLint numberOfActiveUniforms = -1;
245 GLint uniformMaxNameLength=-1;
247 mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms );
248 mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength );
250 std::vector<std::string> samplerNames;
251 char name[uniformMaxNameLength+1]; // Allow for null terminator
252 std::vector< LocationPosition > samplerUniformLocations;
257 GLenum type = GL_ZERO;
259 for( int i=0; i<numberOfActiveUniforms; ++i )
261 mGlAbstraction.GetActiveUniform( mProgramId, static_cast< GLuint >( i ), uniformMaxNameLength,
262 &nameLength, &number, &type, name );
264 if( type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES )
266 GLuint location = mGlAbstraction.GetUniformLocation( mProgramId, name );
267 samplerNames.push_back(name);
268 samplerUniformLocations.push_back(LocationPosition(location, -1));
273 //Determine declaration order of each sampler
274 char* fragShader = strdup( mProgramData->GetFragmentShader() );
275 char* nextPtr = NULL;
276 const char* token = strtok_r( fragShader, " ;\n", &nextPtr );
277 int samplerPosition = 0;
280 if( ( strncmp( token, "sampler2D", 9u ) == 0 ) ||
281 ( strncmp( token, "samplerCube", 11u ) == 0 ) ||
282 ( strncmp( token, "samplerExternalOES", 18u ) == 0 ) )
285 token = strtok_r( NULL, " ;\n", &nextPtr );
286 for( size_t i=0; i<samplerUniformLocations.size(); ++i )
288 if( samplerUniformLocations[i].position == -1 &&
289 strncmp( token, samplerNames[i].c_str(), samplerNames[i].size() ) == 0 )
291 samplerUniformLocations[i].position = samplerPosition++;
298 DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token );
303 token = strtok_r( NULL, " ;\n", &nextPtr );
309 // Re-order according to declaration order in the fragment source.
310 size_t samplerUniformCount = samplerUniformLocations.size();
311 if( samplerUniformCount > 1 )
313 std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
316 mSamplerUniformLocations.resize( samplerUniformCount );
317 for( size_t i=0; i<samplerUniformCount; ++i )
319 mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
323 bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location )
326 if( index < mSamplerUniformLocations.size() )
328 location = mSamplerUniformLocations[index];
334 size_t Program::GetActiveSamplerCount() const
336 return mSamplerUniformLocations.size();
339 void Program::SetUniform1i( GLint location, GLint value0 )
341 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
343 if( UNIFORM_UNKNOWN == location )
345 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
346 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
347 // specified uniform variable will not be changed.following opengl silently do nothing
351 // check if uniform location fits the cache
352 if( location >= MAX_UNIFORM_CACHE_SIZE )
354 // not cached, make the gl call
355 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
356 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
360 // check if the value is different from what's already been set
361 if( value0 != mUniformCacheInt[ location ] )
364 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
365 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
367 mUniformCacheInt[ location ] = value0;
372 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
374 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
376 if( UNIFORM_UNKNOWN == location )
378 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
379 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
380 // specified uniform variable will not be changed.following opengl silently do nothing
384 // Not caching these as based on current analysis this is not called that often by our shaders
385 LOG_GL( "Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3 );
386 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4i( location, value0, value1, value2, value3 ) );
389 void Program::SetUniform1f( GLint location, GLfloat value0 )
391 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
393 if( UNIFORM_UNKNOWN == location )
395 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
396 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
397 // specified uniform variable will not be changed.following opengl silently do nothing
401 // check if uniform location fits the cache
402 if( location >= MAX_UNIFORM_CACHE_SIZE )
404 // not cached, make the gl call
405 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
406 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
410 // check if the same value has already been set, reset if it is different
411 if( ( fabsf(value0 - mUniformCacheFloat[ location ]) >= Math::MACHINE_EPSILON_1 ) )
414 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
415 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
418 mUniformCacheFloat[ location ] = value0;
423 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
425 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
427 if( UNIFORM_UNKNOWN == location )
429 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
430 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
431 // specified uniform variable will not be changed.following opengl silently do nothing
435 // check if uniform location fits the cache
436 if( location >= MAX_UNIFORM_CACHE_SIZE )
438 // not cached, make the gl call
439 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
440 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
444 // check if the same value has already been set, reset if it is different
445 if( ( fabsf(value0 - mUniformCacheFloat2[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
446 ( fabsf(value1 - mUniformCacheFloat2[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 ) )
449 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
450 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
453 mUniformCacheFloat2[ location ][ 0 ] = value0;
454 mUniformCacheFloat2[ location ][ 1 ] = value1;
459 void Program::SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
461 if( ( fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1 )||
462 ( fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1 )||
463 ( fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1 ) )
465 mSizeUniformCache.x = value0;
466 mSizeUniformCache.y = value1;
467 mSizeUniformCache.z = value2;
468 SetUniform3f( location, value0, value1, value2 );
472 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
474 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
476 if( UNIFORM_UNKNOWN == location )
478 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
479 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
480 // specified uniform variable will not be changed.following opengl silently do nothing
484 // Not caching these as based on current analysis this is not called that often by our shaders
485 LOG_GL( "Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2 );
486 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform3f( location, value0, value1, value2 ) );
489 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
491 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
493 if( UNIFORM_UNKNOWN == location )
495 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
496 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
497 // specified uniform variable will not be changed.following opengl silently do nothing
501 // check if uniform location fits the cache
502 if( location >= MAX_UNIFORM_CACHE_SIZE )
504 // not cached, make the gl call
505 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
506 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
510 // check if the same value has already been set, reset if any component is different
511 // checking index 3 first because we're often animating alpha (rgba)
512 if( ( fabsf(value3 - mUniformCacheFloat4[ location ][ 3 ]) >= Math::MACHINE_EPSILON_1 )||
513 ( fabsf(value0 - mUniformCacheFloat4[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
514 ( fabsf(value1 - mUniformCacheFloat4[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 )||
515 ( fabsf(value2 - mUniformCacheFloat4[ location ][ 2 ]) >= Math::MACHINE_EPSILON_1 ) )
518 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
519 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
521 mUniformCacheFloat4[ location ][ 0 ] = value0;
522 mUniformCacheFloat4[ location ][ 1 ] = value1;
523 mUniformCacheFloat4[ location ][ 2 ] = value2;
524 mUniformCacheFloat4[ location ][ 3 ] = value3;
529 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
531 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
533 if( UNIFORM_UNKNOWN == location )
535 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
536 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
537 // specified uniform variable will not be changed.following opengl silently do nothing
541 // Not caching these calls. Based on current analysis this is called very often
542 // but with different values (we're using this for MVP matrices)
543 // NOTE! we never want driver or GPU to transpose
544 LOG_GL( "UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
545 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix4fv( location, count, GL_FALSE, value ) );
548 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
550 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
552 if( UNIFORM_UNKNOWN == location )
554 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
555 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
556 // specified uniform variable will not be changed.following opengl silently do nothing
561 // Not caching these calls. Based on current analysis this is called very often
562 // but with different values (we're using this for MVP matrices)
563 // NOTE! we never want driver or GPU to transpose
564 LOG_GL( "UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
565 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix3fv( location, count, GL_FALSE, value ) );
568 void Program::GlContextCreated()
572 void Program::GlContextDestroyed()
576 mFragmentShaderId = 0;
579 ResetAttribsUniformCache();
582 bool Program::ModifiesGeometry()
584 return mModifiesGeometry;
587 Program::Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
589 mGlAbstraction( mCache.GetGlAbstraction() ),
590 mProjectionMatrix( NULL ),
593 mVertexShaderId( 0 ),
594 mFragmentShaderId( 0 ),
596 mProgramData(shaderData),
597 mModifiesGeometry( modifiesGeometry )
599 // reserve space for standard attributes
600 mAttributeLocations.reserve( ATTRIB_TYPE_LAST );
601 for( int i=0; i<ATTRIB_TYPE_LAST; ++i )
603 RegisterCustomAttribute( gStdAttribs[i] );
606 // reserve space for standard uniforms
607 mUniformLocations.reserve( UNIFORM_TYPE_LAST );
608 // reset built in uniform names in cache
609 for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
611 RegisterUniform( gStdUniforms[ i ] );
615 ResetAttribsUniformCache();
625 DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
626 DALI_ASSERT_DEBUG( mProgramId == 0 && "mProgramId != 0, so about to leak a GL resource by overwriting it." );
628 LOG_GL( "CreateProgram()\n" );
629 mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
631 GLint linked = GL_FALSE;
633 const bool binariesSupported = mCache.IsBinarySupported();
635 // if shader binaries are supported and ShaderData contains compiled bytecode?
636 if( binariesSupported && mProgramData->HasBinary() )
638 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
640 CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
642 CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
645 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
647 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
649 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
651 if( GL_FALSE == linked )
653 DALI_LOG_ERROR("Failed to load program binary \n");
656 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
659 Dali::Vector< char > szLog;
660 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
661 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() ) );
662 DALI_LOG_ERROR( "Program Link Error: %s\n", szLog.Begin() );
668 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Reused binary.\n" );
672 // Fall back to compiling and linking the vertex and fragment sources
673 if( GL_FALSE == linked )
675 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
676 if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader() ) )
678 if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader() ) )
682 if( binariesSupported && mLinked )
684 GLint binaryLength = 0;
686 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Compiled and linked.\n\nVS:\n%s\nFS:\n%s\n", mProgramData->GetVertexShader(), mProgramData->GetFragmentShader() );
688 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
689 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
690 if( binaryLength > 0 )
692 // Allocate space for the bytecode in ShaderData
693 mProgramData->AllocateBuffer(binaryLength);
694 // Copy the bytecode to ShaderData
695 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
696 mCache.StoreBinary( mProgramData );
697 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Saved binary.\n" );
704 GetActiveSamplerUniforms();
710 void Program::Unload()
714 if( this == mCache.GetCurrentProgram() )
716 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
718 mCache.SetCurrentProgram( NULL );
723 LOG_GL( "DeleteProgram(%d)\n", mProgramId );
724 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteProgram( mProgramId ) );
732 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
736 LOG_GL( "CreateShader(%d)\n", shaderType );
737 shaderId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateShader( shaderType ) );
738 LOG_GL( "AttachShader(%d,%d)\n", mProgramId, shaderId );
739 CHECK_GL( mGlAbstraction, mGlAbstraction.AttachShader( mProgramId, shaderId ) );
742 LOG_GL( "ShaderSource(%d)\n", shaderId );
743 CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
745 LOG_GL( "CompileShader(%d)\n", shaderId );
746 CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
749 LOG_GL( "GetShaderiv(%d)\n", shaderId );
750 CHECK_GL( mGlAbstraction, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
752 if (compiled == GL_FALSE)
754 DALI_LOG_ERROR("Failed to compile shader\n");
755 LogWithLineNumbers(src);
758 mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
761 Dali::Vector< char > szLog;
762 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
763 mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
764 DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
767 DALI_ASSERT_ALWAYS( 0 && "Shader compilation failure" );
770 return compiled != 0;
775 LOG_GL( "LinkProgram(%d)\n", mProgramId );
776 CHECK_GL( mGlAbstraction, mGlAbstraction.LinkProgram( mProgramId ) );
779 LOG_GL( "GetProgramiv(%d)\n", mProgramId );
780 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
782 if (linked == GL_FALSE)
784 DALI_LOG_ERROR("Shader failed to link \n");
787 mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength);
790 Dali::Vector< char > szLog;
791 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
792 mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() );
793 DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog.Begin() );
796 DALI_ASSERT_ALWAYS( 0 && "Shader linking failure" );
799 mLinked = linked != GL_FALSE;
802 void Program::FreeShaders()
806 LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
807 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
808 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mVertexShaderId ) );
812 if (mFragmentShaderId)
814 LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
815 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
816 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
817 mFragmentShaderId = 0;
821 void Program::ResetAttribsUniformCache()
823 // reset attribute locations
824 for( unsigned i = 0; i < mAttributeLocations.size() ; ++i )
826 mAttributeLocations[ i ].second = ATTRIB_UNKNOWN;
829 // reset all gl uniform locations
830 for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
832 // reset gl program locations and names
833 mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
836 mSamplerUniformLocations.clear();
838 // reset uniform caches
839 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
841 for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
843 // GL initializes uniforms to 0
844 mUniformCacheInt[ i ] = 0;
845 mUniformCacheFloat[ i ] = 0.0f;
846 mUniformCacheFloat2[ i ][ 0 ] = 0.0f;
847 mUniformCacheFloat2[ i ][ 1 ] = 0.0f;
848 mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
849 mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
850 mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
851 mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
855 } // namespace Internal