2 * Copyright (c) 2014 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 // we want to lazy load programs so dont do a Load yet, it gets done in Use()
116 cache.AddProgram( shaderHash, program );
131 if ( this != mCache.GetCurrentProgram() )
133 LOG_GL( "UseProgram(%d)\n", mProgramId );
134 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(mProgramId) );
136 mCache.SetCurrentProgram( this );
141 bool Program::IsUsed()
143 return ( this == mCache.GetCurrentProgram() );
146 GLint Program::GetAttribLocation( AttribType type )
148 DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
150 return GetCustomAttributeLocation( type );
153 unsigned int Program::RegisterCustomAttribute( const std::string& name )
155 unsigned int index = 0;
156 // find the value from cache
157 for( ;index < mAttributeLocations.size(); ++index )
159 if( mAttributeLocations[ index ].first == name )
161 // name found so return index
165 // if we get here, index is one past end so push back the new name
166 mAttributeLocations.push_back( std::make_pair( name, ATTRIB_UNKNOWN ) );
170 GLint Program::GetCustomAttributeLocation( unsigned int attributeIndex )
172 // debug check that index is within name cache
173 DALI_ASSERT_DEBUG( mAttributeLocations.size() > attributeIndex );
175 // check if we have already queried the location of the attribute
176 GLint location = mAttributeLocations[ attributeIndex ].second;
178 if( location == ATTRIB_UNKNOWN )
180 location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetAttribLocation( mProgramId, mAttributeLocations[ attributeIndex ].first.c_str() ) );
182 mAttributeLocations[ attributeIndex ].second = location;
183 LOG_GL( "GetAttributeLocation(program=%d,%s) = %d\n", mProgramId, mAttributeLocations[ attributeIndex ].first.c_str(), mAttributeLocations[ attributeIndex ].second );
190 unsigned int Program::RegisterUniform( const std::string& name )
192 unsigned int index = 0;
193 // find the value from cache
194 for( ;index < mUniformLocations.size(); ++index )
196 if( mUniformLocations[ index ].first == name )
198 // name found so return index
202 // if we get here, index is one past end so push back the new name
203 mUniformLocations.push_back( std::make_pair( name, UNIFORM_NOT_QUERIED ) );
207 GLint Program::GetUniformLocation( unsigned int uniformIndex )
209 // debug check that index is within name cache
210 DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
212 // check if we have already queried the location of the uniform
213 GLint location = mUniformLocations[ uniformIndex ].second;
215 if( location == UNIFORM_NOT_QUERIED )
217 location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, mUniformLocations[ uniformIndex ].first.c_str() ) );
219 mUniformLocations[ uniformIndex ].second = location;
220 LOG_GL( "GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[ uniformIndex ].first.c_str(), mUniformLocations[ uniformIndex ].second );
229 * This struct is used to record the position of a uniform declaration
230 * within the fragment shader source code.
232 struct LocationPosition
234 GLint uniformLocation; ///< The location of the uniform (used as an identifier)
235 int position; ///< the position of the uniform declaration
236 LocationPosition( GLint uniformLocation, int position )
237 : uniformLocation(uniformLocation), position(position)
242 bool sortByPosition( LocationPosition a, LocationPosition b )
244 return a.position < b.position;
248 void Program::GetActiveSamplerUniforms()
250 GLint numberOfActiveUniforms = -1;
251 GLint uniformMaxNameLength=-1;
253 mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms );
254 mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength );
256 std::vector<std::string> samplerNames;
257 char name[uniformMaxNameLength+1]; // Allow for null terminator
258 std::vector< LocationPosition > samplerUniformLocations;
263 GLenum type = GL_ZERO;
265 for( int i=0; i<numberOfActiveUniforms; ++i )
267 mGlAbstraction.GetActiveUniform( mProgramId, (GLuint)i, uniformMaxNameLength,
268 &nameLength, &number, &type, name );
270 if( type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE ) /// Is there a native sampler type?
272 GLuint location = mGlAbstraction.GetUniformLocation( mProgramId, name );
273 samplerNames.push_back(name);
274 samplerUniformLocations.push_back(LocationPosition(location, -1));
279 //Determine declaration order of each sampler
280 char* fragShader = strdup( mProgramData->GetFragmentShader() );
281 const char* token = strtok( fragShader, " ;\n");
282 int samplerPosition = 0;
285 if( ( strncmp( token, "sampler2D", 9u ) == 0 ) || ( strncmp( token, "samplerCube", 11u ) == 0 ) )
288 token = strtok( NULL, " ;\n");
289 for( size_t i=0; i<samplerUniformLocations.size(); ++i )
291 if( samplerUniformLocations[i].position == -1 &&
292 strncmp( token, samplerNames[i].c_str(), samplerNames[i].size() ) == 0 )
294 samplerUniformLocations[i].position = samplerPosition++;
301 DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token );
306 token = strtok( NULL, " ;\n");
312 // Re-order according to declaration order in the fragment source.
313 size_t samplerUniformCount = samplerUniformLocations.size();
314 if( samplerUniformCount > 1 )
316 std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
319 mSamplerUniformLocations.resize( samplerUniformCount );
320 for( size_t i=0; i<samplerUniformCount; ++i )
322 mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
326 bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location )
329 if( index < mSamplerUniformLocations.size() )
331 location = mSamplerUniformLocations[index];
337 size_t Program::GetActiveSamplerCount() const
339 return mSamplerUniformLocations.size();
342 void Program::SetUniform1i( GLint location, GLint value0 )
344 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
346 if( UNIFORM_UNKNOWN == location )
348 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
349 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
350 // specified uniform variable will not be changed.following opengl silently do nothing
354 // check if uniform location fits the cache
355 if( location >= MAX_UNIFORM_CACHE_SIZE )
357 // not cached, make the gl call
358 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
359 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
363 // check if the value is different from what's already been set
364 if( value0 != mUniformCacheInt[ location ] )
367 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
368 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
370 mUniformCacheInt[ location ] = value0;
375 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
377 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
379 if( UNIFORM_UNKNOWN == location )
381 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
382 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
383 // specified uniform variable will not be changed.following opengl silently do nothing
387 // Not caching these as based on current analysis this is not called that often by our shaders
388 LOG_GL( "Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3 );
389 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4i( location, value0, value1, value2, value3 ) );
392 void Program::SetUniform1f( GLint location, GLfloat value0 )
394 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
396 if( UNIFORM_UNKNOWN == location )
398 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
399 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
400 // specified uniform variable will not be changed.following opengl silently do nothing
404 // check if uniform location fits the cache
405 if( location >= MAX_UNIFORM_CACHE_SIZE )
407 // not cached, make the gl call
408 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
409 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
413 // check if the same value has already been set, reset if it is different
414 if( ( fabsf(value0 - mUniformCacheFloat[ location ]) >= Math::MACHINE_EPSILON_1 ) )
417 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
418 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
421 mUniformCacheFloat[ location ] = value0;
426 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
428 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
430 if( UNIFORM_UNKNOWN == location )
432 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
433 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
434 // specified uniform variable will not be changed.following opengl silently do nothing
438 // check if uniform location fits the cache
439 if( location >= MAX_UNIFORM_CACHE_SIZE )
441 // not cached, make the gl call
442 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
443 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
447 // check if the same value has already been set, reset if it is different
448 if( ( fabsf(value0 - mUniformCacheFloat2[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
449 ( fabsf(value1 - mUniformCacheFloat2[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 ) )
452 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
453 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
456 mUniformCacheFloat2[ location ][ 0 ] = value0;
457 mUniformCacheFloat2[ location ][ 1 ] = value1;
462 void Program::SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
464 if( ( fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1 )||
465 ( fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1 )||
466 ( fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1 ) )
468 mSizeUniformCache.x = value0;
469 mSizeUniformCache.y = value1;
470 mSizeUniformCache.z = value2;
471 SetUniform3f( location, value0, value1, value2 );
475 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
477 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
479 if( UNIFORM_UNKNOWN == location )
481 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
482 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
483 // specified uniform variable will not be changed.following opengl silently do nothing
487 // Not caching these as based on current analysis this is not called that often by our shaders
488 LOG_GL( "Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2 );
489 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform3f( location, value0, value1, value2 ) );
492 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
494 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
496 if( UNIFORM_UNKNOWN == location )
498 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
499 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
500 // specified uniform variable will not be changed.following opengl silently do nothing
504 // check if uniform location fits the cache
505 if( location >= MAX_UNIFORM_CACHE_SIZE )
507 // not cached, make the gl call
508 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
509 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
513 // check if the same value has already been set, reset if any component is different
514 // checking index 3 first because we're often animating alpha (rgba)
515 if( ( fabsf(value3 - mUniformCacheFloat4[ location ][ 3 ]) >= Math::MACHINE_EPSILON_1 )||
516 ( fabsf(value0 - mUniformCacheFloat4[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
517 ( fabsf(value1 - mUniformCacheFloat4[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 )||
518 ( fabsf(value2 - mUniformCacheFloat4[ location ][ 2 ]) >= Math::MACHINE_EPSILON_1 ) )
521 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
522 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
524 mUniformCacheFloat4[ location ][ 0 ] = value0;
525 mUniformCacheFloat4[ location ][ 1 ] = value1;
526 mUniformCacheFloat4[ location ][ 2 ] = value2;
527 mUniformCacheFloat4[ location ][ 3 ] = value3;
532 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
534 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
536 if( UNIFORM_UNKNOWN == location )
538 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
539 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
540 // specified uniform variable will not be changed.following opengl silently do nothing
544 // Not caching these calls. Based on current analysis this is called very often
545 // but with different values (we're using this for MVP matrices)
546 // NOTE! we never want driver or GPU to transpose
547 LOG_GL( "UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
548 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix4fv( location, count, GL_FALSE, value ) );
551 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
553 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
555 if( UNIFORM_UNKNOWN == location )
557 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
558 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
559 // specified uniform variable will not be changed.following opengl silently do nothing
564 // Not caching these calls. Based on current analysis this is called very often
565 // but with different values (we're using this for MVP matrices)
566 // NOTE! we never want driver or GPU to transpose
567 LOG_GL( "UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
568 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix3fv( location, count, GL_FALSE, value ) );
571 void Program::GlContextCreated()
575 void Program::GlContextDestroyed()
579 mFragmentShaderId = 0;
582 ResetAttribsUniformCache();
585 bool Program::ModifiesGeometry()
587 return mModifiesGeometry;
590 Program::Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
592 mGlAbstraction( mCache.GetGlAbstraction() ),
593 mProjectionMatrix( NULL ),
596 mVertexShaderId( 0 ),
597 mFragmentShaderId( 0 ),
599 mProgramData(shaderData),
600 mModifiesGeometry( modifiesGeometry )
602 // reserve space for standard attributes
603 mAttributeLocations.reserve( ATTRIB_TYPE_LAST );
604 for( int i=0; i<ATTRIB_TYPE_LAST; ++i )
606 RegisterCustomAttribute( gStdAttribs[i] );
609 // reserve space for standard uniforms
610 mUniformLocations.reserve( UNIFORM_TYPE_LAST );
611 // reset built in uniform names in cache
612 for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
614 RegisterUniform( gStdUniforms[ i ] );
618 ResetAttribsUniformCache();
628 DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
629 DALI_ASSERT_DEBUG( mProgramId == 0 && "mProgramId != 0, so about to leak a GL resource by overwriting it." );
631 LOG_GL( "CreateProgram()\n" );
632 mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
634 GLint linked = GL_FALSE;
636 const bool binariesSupported = mCache.IsBinarySupported();
638 // if shader binaries are supported and ShaderData contains compiled bytecode?
639 if( binariesSupported && mProgramData->HasBinary() )
641 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
643 CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
645 CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
648 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
650 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
652 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
654 if( GL_FALSE == linked )
656 DALI_LOG_ERROR("Failed to load program binary \n");
659 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
662 Dali::Vector< char > szLog;
663 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
664 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() ) );
665 DALI_LOG_ERROR( "Program Link Error: %s\n", szLog.Begin() );
671 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Reused binary.\n" );
675 // Fall back to compiling and linking the vertex and fragment sources
676 if( GL_FALSE == linked )
678 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
679 if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader() ) )
681 if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader() ) )
685 if( binariesSupported && mLinked )
687 GLint binaryLength = 0;
689 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Compiled and linked.\n\nVS:\n%s\nFS:\n%s\n", mProgramData->GetVertexShader(), mProgramData->GetFragmentShader() );
691 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
692 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
693 if( binaryLength > 0 )
695 // Allocate space for the bytecode in ShaderData
696 mProgramData->AllocateBuffer(binaryLength);
697 // Copy the bytecode to ShaderData
698 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
699 mCache.StoreBinary( mProgramData );
700 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Saved binary.\n" );
707 GetActiveSamplerUniforms();
713 void Program::Unload()
717 if( this == mCache.GetCurrentProgram() )
719 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
721 mCache.SetCurrentProgram( NULL );
726 LOG_GL( "DeleteProgram(%d)\n", mProgramId );
727 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteProgram( mProgramId ) );
735 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
739 LOG_GL( "CreateShader(%d)\n", shaderType );
740 shaderId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateShader( shaderType ) );
741 LOG_GL( "AttachShader(%d,%d)\n", mProgramId, shaderId );
742 CHECK_GL( mGlAbstraction, mGlAbstraction.AttachShader( mProgramId, shaderId ) );
745 LOG_GL( "ShaderSource(%d)\n", shaderId );
746 CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
748 LOG_GL( "CompileShader(%d)\n", shaderId );
749 CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
752 LOG_GL( "GetShaderiv(%d)\n", shaderId );
753 CHECK_GL( mGlAbstraction, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
755 if (compiled == GL_FALSE)
757 DALI_LOG_ERROR("Failed to compile shader\n");
758 LogWithLineNumbers(src);
761 mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
764 Dali::Vector< char > szLog;
765 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
766 mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
767 DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
770 DALI_ASSERT_ALWAYS( 0 && "Shader compilation failure" );
773 return compiled != 0;
778 LOG_GL( "LinkProgram(%d)\n", mProgramId );
779 CHECK_GL( mGlAbstraction, mGlAbstraction.LinkProgram( mProgramId ) );
782 LOG_GL( "GetProgramiv(%d)\n", mProgramId );
783 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
785 if (linked == GL_FALSE)
787 DALI_LOG_ERROR("Shader failed to link \n");
790 mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength);
793 Dali::Vector< char > szLog;
794 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
795 mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() );
796 DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog.Begin() );
799 DALI_ASSERT_ALWAYS( 0 && "Shader linking failure" );
802 mLinked = linked != GL_FALSE;
805 void Program::FreeShaders()
809 LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
810 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
811 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mVertexShaderId ) );
815 if (mFragmentShaderId)
817 LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
818 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
819 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
820 mFragmentShaderId = 0;
824 void Program::ResetAttribsUniformCache()
826 // reset attribute locations
827 for( unsigned i = 0; i < mAttributeLocations.size() ; ++i )
829 mAttributeLocations[ i ].second = ATTRIB_UNKNOWN;
832 // reset all gl uniform locations
833 for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
835 // reset gl program locations and names
836 mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
839 mSamplerUniformLocations.clear();
841 // reset uniform caches
842 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
844 for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
846 // GL initializes uniforms to 0
847 mUniformCacheInt[ i ] = 0;
848 mUniformCacheFloat[ i ] = 0.0f;
849 mUniformCacheFloat2[ i ][ 0 ] = 0.0f;
850 mUniformCacheFloat2[ i ][ 1 ] = 0.0f;
851 mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
852 mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
853 mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
854 mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
858 } // namespace Internal