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>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/common/constants.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/internal/common/shader-data.h>
30 #include <dali/integration-api/gl-defines.h>
31 #include <dali/internal/render/common/performance-monitor.h>
32 #include <dali/internal/render/shaders/program-cache.h>
33 #include <dali/internal/render/gl-resources/gl-call-debug.h>
37 void LogWithLineNumbers( const char * source )
39 unsigned int lineNumber = 0u;
40 const char *prev = source;
41 const char *ptr = prev;
49 // seek the next end of line or end of text
50 while( *ptr!='\n' && *ptr != '\0' )
55 std::string line( prev, ptr-prev );
56 Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%4d %s\n", lineNumber, line.c_str());
79 const char* gStdAttribs[ Program::ATTRIB_TYPE_LAST ] =
81 "aPosition", // ATTRIB_POSITION
82 "aTexCoord", // ATTRIB_TEXCOORD
85 const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
87 "uMvpMatrix", // UNIFORM_MVP_MATRIX
88 "uModelView", // UNIFORM_MODELVIEW_MATRIX
89 "uProjection", // UNIFORM_PROJECTION_MATRIX
90 "uModelMatrix", // UNIFORM_MODEL_MATRIX,
91 "uViewMatrix", // UNIFORM_VIEW_MATRIX,
92 "uNormalMatrix", // UNIFORM_NORMAL_MATRIX
93 "uColor", // UNIFORM_COLOR
94 "sTexture", // UNIFORM_SAMPLER
95 "sTextureRect", // UNIFORM_SAMPLER_RECT
96 "sEffect", // UNIFORM_EFFECT_SAMPLER
97 "uSize" // UNIFORM_SIZE
100 } // <unnamed> namespace
104 Program* Program::New( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
106 size_t shaderHash = shaderData->GetHashValue();
107 Program* program = cache.GetProgram( shaderHash );
109 if( NULL == program )
111 // program not found so create it
112 program = new Program( cache, shaderData, modifiesGeometry );
114 // we want to lazy load programs so dont do a Load yet, it gets done in Use()
115 cache.AddProgram( shaderHash, program );
130 if ( this != mCache.GetCurrentProgram() )
132 LOG_GL( "UseProgram(%d)\n", mProgramId );
133 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(mProgramId) );
135 mCache.SetCurrentProgram( this );
140 bool Program::IsUsed()
142 return ( this == mCache.GetCurrentProgram() );
145 GLint Program::GetAttribLocation( AttribType type )
147 DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
149 return GetCustomAttributeLocation( type );
152 unsigned int Program::RegisterCustomAttribute( const std::string& name )
154 unsigned int index = 0;
155 // find the value from cache
156 for( ;index < mAttributeLocations.size(); ++index )
158 if( mAttributeLocations[ index ].first == name )
160 // name found so return index
164 // if we get here, index is one past end so push back the new name
165 mAttributeLocations.push_back( std::make_pair( name, ATTRIB_UNKNOWN ) );
169 GLint Program::GetCustomAttributeLocation( unsigned int attributeIndex )
171 // debug check that index is within name cache
172 DALI_ASSERT_DEBUG( mAttributeLocations.size() > attributeIndex );
174 // check if we have already queried the location of the attribute
175 GLint location = mAttributeLocations[ attributeIndex ].second;
177 if( location == ATTRIB_UNKNOWN )
179 location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetAttribLocation( mProgramId, mAttributeLocations[ attributeIndex ].first.c_str() ) );
181 mAttributeLocations[ attributeIndex ].second = location;
182 LOG_GL( "GetAttributeLocation(program=%d,%s) = %d\n", mProgramId, mAttributeLocations[ attributeIndex ].first.c_str(), mAttributeLocations[ attributeIndex ].second );
189 unsigned int Program::RegisterUniform( const std::string& name )
191 unsigned int index = 0;
192 // find the value from cache
193 for( ;index < mUniformLocations.size(); ++index )
195 if( mUniformLocations[ index ].first == name )
197 // name found so return index
201 // if we get here, index is one past end so push back the new name
202 mUniformLocations.push_back( std::make_pair( name, UNIFORM_NOT_QUERIED ) );
206 GLint Program::GetUniformLocation( unsigned int uniformIndex )
208 // debug check that index is within name cache
209 DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
211 // check if we have already queried the location of the uniform
212 GLint location = mUniformLocations[ uniformIndex ].second;
214 if( location == UNIFORM_NOT_QUERIED )
216 location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, mUniformLocations[ uniformIndex ].first.c_str() ) );
218 mUniformLocations[ uniformIndex ].second = location;
219 LOG_GL( "GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[ uniformIndex ].first.c_str(), mUniformLocations[ uniformIndex ].second );
228 * This struct is used to record the position of a uniform declaration
229 * within the fragment shader source code.
231 struct LocationPosition
233 GLint uniformLocation; ///< The location of the uniform (used as an identifier)
234 int characterPosition; ///< the position of the uniform declaration
235 LocationPosition( GLint uniformLocation, int characterPosition )
236 : uniformLocation(uniformLocation), characterPosition(characterPosition)
241 bool sortByPosition( LocationPosition a, LocationPosition b )
243 return a.characterPosition < b.characterPosition;
247 void Program::GetActiveSamplerUniforms()
249 GLint numberOfActiveUniforms = -1;
250 GLint uniformMaxNameLength=-1;
252 mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms );
253 mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength );
255 std::vector<std::string> samplerNames;
256 char name[uniformMaxNameLength+1]; // Allow for null terminator
257 std::vector< LocationPosition > samplerUniformLocations;
262 GLenum type = GL_ZERO;
264 for( int i=0; i<numberOfActiveUniforms; ++i )
266 mGlAbstraction.GetActiveUniform( mProgramId, (GLuint)i, uniformMaxNameLength,
267 &nameLength, &number, &type, name );
269 if( type == GL_SAMPLER_2D ) /// Is there a native sampler type?
271 GLuint location = mGlAbstraction.GetUniformLocation( mProgramId, name );
272 samplerNames.push_back(name);
273 samplerUniformLocations.push_back(LocationPosition(location, 0u));
278 if( samplerUniformLocations.size() > 1 )
280 // Now, re-order according to declaration order in the fragment source.
281 std::string fragShader( mProgramData->GetFragmentShader() );
282 for( unsigned int i=0; i<samplerUniformLocations.size(); ++i )
284 // Better to write own search algorithm that searches for all of
285 // the sampler names simultaneously, ensuring only one iteration
287 size_t characterPosition = fragShader.find( samplerNames[i] );
288 samplerUniformLocations[i].characterPosition = characterPosition;
290 std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
293 for( unsigned int i=0; i<samplerUniformLocations.size(); ++i )
295 mSamplerUniformLocations.push_back( samplerUniformLocations[i].uniformLocation );
299 bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location )
302 if( index < mSamplerUniformLocations.size() )
304 location = mSamplerUniformLocations[index];
310 void Program::SetUniform1i( GLint location, GLint value0 )
312 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
314 if( UNIFORM_UNKNOWN == location )
316 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
317 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
318 // specified uniform variable will not be changed.following opengl silently do nothing
322 // check if uniform location fits the cache
323 if( location >= MAX_UNIFORM_CACHE_SIZE )
325 // not cached, make the gl call
326 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
327 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
331 // check if the value is different from what's already been set
332 if( value0 != mUniformCacheInt[ location ] )
335 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
336 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
338 mUniformCacheInt[ location ] = value0;
343 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
345 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
347 if( UNIFORM_UNKNOWN == location )
349 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
350 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
351 // specified uniform variable will not be changed.following opengl silently do nothing
355 // Not caching these as based on current analysis this is not called that often by our shaders
356 LOG_GL( "Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3 );
357 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4i( location, value0, value1, value2, value3 ) );
360 void Program::SetUniform1f( GLint location, GLfloat value0 )
362 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
364 if( UNIFORM_UNKNOWN == location )
366 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
367 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
368 // specified uniform variable will not be changed.following opengl silently do nothing
372 // check if uniform location fits the cache
373 if( location >= MAX_UNIFORM_CACHE_SIZE )
375 // not cached, make the gl call
376 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
377 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
381 // check if the same value has already been set, reset if it is different
382 if( ( fabsf(value0 - mUniformCacheFloat[ location ]) >= Math::MACHINE_EPSILON_1 ) )
385 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
386 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
389 mUniformCacheFloat[ location ] = value0;
394 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
396 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
398 if( UNIFORM_UNKNOWN == location )
400 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
401 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
402 // specified uniform variable will not be changed.following opengl silently do nothing
406 // check if uniform location fits the cache
407 if( location >= MAX_UNIFORM_CACHE_SIZE )
409 // not cached, make the gl call
410 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
411 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
415 // check if the same value has already been set, reset if it is different
416 if( ( fabsf(value0 - mUniformCacheFloat2[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
417 ( fabsf(value1 - mUniformCacheFloat2[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 ) )
420 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
421 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
424 mUniformCacheFloat2[ location ][ 0 ] = value0;
425 mUniformCacheFloat2[ location ][ 1 ] = value1;
430 void Program::SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
432 if( ( fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1 )||
433 ( fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1 )||
434 ( fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1 ) )
436 mSizeUniformCache.x = value0;
437 mSizeUniformCache.y = value1;
438 mSizeUniformCache.z = value2;
439 SetUniform3f( location, value0, value1, value2 );
443 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
445 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
447 if( UNIFORM_UNKNOWN == location )
449 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
450 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
451 // specified uniform variable will not be changed.following opengl silently do nothing
455 // Not caching these as based on current analysis this is not called that often by our shaders
456 LOG_GL( "Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2 );
457 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform3f( location, value0, value1, value2 ) );
460 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
462 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
464 if( UNIFORM_UNKNOWN == location )
466 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
467 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
468 // specified uniform variable will not be changed.following opengl silently do nothing
472 // check if uniform location fits the cache
473 if( location >= MAX_UNIFORM_CACHE_SIZE )
475 // not cached, make the gl call
476 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
477 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
481 // check if the same value has already been set, reset if any component is different
482 // checking index 3 first because we're often animating alpha (rgba)
483 if( ( fabsf(value3 - mUniformCacheFloat4[ location ][ 3 ]) >= Math::MACHINE_EPSILON_1 )||
484 ( fabsf(value0 - mUniformCacheFloat4[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
485 ( fabsf(value1 - mUniformCacheFloat4[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 )||
486 ( fabsf(value2 - mUniformCacheFloat4[ location ][ 2 ]) >= Math::MACHINE_EPSILON_1 ) )
489 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
490 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
492 mUniformCacheFloat4[ location ][ 0 ] = value0;
493 mUniformCacheFloat4[ location ][ 1 ] = value1;
494 mUniformCacheFloat4[ location ][ 2 ] = value2;
495 mUniformCacheFloat4[ location ][ 3 ] = value3;
500 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
502 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
504 if( UNIFORM_UNKNOWN == location )
506 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
507 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
508 // specified uniform variable will not be changed.following opengl silently do nothing
512 // Not caching these calls. Based on current analysis this is called very often
513 // but with different values (we're using this for MVP matrices)
514 // NOTE! we never want driver or GPU to transpose
515 LOG_GL( "UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
516 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix4fv( location, count, GL_FALSE, value ) );
519 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
521 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
523 if( UNIFORM_UNKNOWN == location )
525 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
526 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
527 // specified uniform variable will not be changed.following opengl silently do nothing
532 // Not caching these calls. Based on current analysis this is called very often
533 // but with different values (we're using this for MVP matrices)
534 // NOTE! we never want driver or GPU to transpose
535 LOG_GL( "UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
536 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix3fv( location, count, GL_FALSE, value ) );
539 void Program::GlContextCreated()
543 void Program::GlContextDestroyed()
547 mFragmentShaderId = 0;
550 ResetAttribsUniformCache();
553 bool Program::ModifiesGeometry()
555 return mModifiesGeometry;
558 Program::Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
560 mGlAbstraction( mCache.GetGlAbstraction() ),
561 mProjectionMatrix( NULL ),
564 mVertexShaderId( 0 ),
565 mFragmentShaderId( 0 ),
567 mProgramData(shaderData),
568 mModifiesGeometry( modifiesGeometry )
570 // reserve space for standard attributes
571 mAttributeLocations.reserve( ATTRIB_TYPE_LAST );
572 for( int i=0; i<ATTRIB_TYPE_LAST; ++i )
574 RegisterCustomAttribute( gStdAttribs[i] );
577 // reserve space for standard uniforms
578 mUniformLocations.reserve( UNIFORM_TYPE_LAST );
579 // reset built in uniform names in cache
580 for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
582 RegisterUniform( gStdUniforms[ i ] );
586 ResetAttribsUniformCache();
596 DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
597 DALI_ASSERT_DEBUG( mProgramId == 0 && "mProgramId != 0, so about to leak a GL resource by overwriting it." );
599 LOG_GL( "CreateProgram()\n" );
600 mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
602 GLint linked = GL_FALSE;
604 const bool binariesSupported = mCache.IsBinarySupported();
606 // if shader binaries are supported and ShaderData contains compiled bytecode?
607 if( binariesSupported && mProgramData->HasBinary() )
609 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
611 CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
613 CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
616 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
618 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
620 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
622 if( GL_FALSE == linked )
624 DALI_LOG_ERROR("Failed to load program binary \n");
627 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
630 Dali::Vector< char > szLog;
631 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
632 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() ) );
633 DALI_LOG_ERROR( "Program Link Error: %s\n", szLog.Begin() );
639 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Reused binary.\n" );
643 // Fall back to compiling and linking the vertex and fragment sources
644 if( GL_FALSE == linked )
646 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
647 if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader() ) )
649 if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader() ) )
653 if( binariesSupported && mLinked )
655 GLint binaryLength = 0;
657 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Compiled and linked.\n\nVS:\n%s\nFS:\n%s\n", mProgramData->GetVertexShader(), mProgramData->GetFragmentShader() );
659 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
660 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
661 if( binaryLength > 0 )
663 // Allocate space for the bytecode in ShaderData
664 mProgramData->AllocateBuffer(binaryLength);
665 // Copy the bytecode to ShaderData
666 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
667 mCache.StoreBinary( mProgramData );
668 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Saved binary.\n" );
675 GetActiveSamplerUniforms();
681 void Program::Unload()
685 if( this == mCache.GetCurrentProgram() )
687 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
689 mCache.SetCurrentProgram( NULL );
694 LOG_GL( "DeleteProgram(%d)\n", mProgramId );
695 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteProgram( mProgramId ) );
703 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
707 LOG_GL( "CreateShader(%d)\n", shaderType );
708 shaderId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateShader( shaderType ) );
709 LOG_GL( "AttachShader(%d,%d)\n", mProgramId, shaderId );
710 CHECK_GL( mGlAbstraction, mGlAbstraction.AttachShader( mProgramId, shaderId ) );
713 LOG_GL( "ShaderSource(%d)\n", shaderId );
714 CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
716 LOG_GL( "CompileShader(%d)\n", shaderId );
717 CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
720 LOG_GL( "GetShaderiv(%d)\n", shaderId );
721 CHECK_GL( mGlAbstraction, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
723 if (compiled == GL_FALSE)
725 DALI_LOG_ERROR("Failed to compile shader\n");
726 LogWithLineNumbers(src);
729 mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
732 Dali::Vector< char > szLog;
733 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
734 mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
735 DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
738 DALI_ASSERT_ALWAYS( 0 && "Shader compilation failure" );
741 return compiled != 0;
746 LOG_GL( "LinkProgram(%d)\n", mProgramId );
747 CHECK_GL( mGlAbstraction, mGlAbstraction.LinkProgram( mProgramId ) );
750 LOG_GL( "GetProgramiv(%d)\n", mProgramId );
751 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
753 if (linked == GL_FALSE)
755 DALI_LOG_ERROR("Shader failed to link \n");
758 mGlAbstraction.GetProgramiv( mProgramId, 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.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() );
764 DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog.Begin() );
767 DALI_ASSERT_ALWAYS( 0 && "Shader linking failure" );
770 mLinked = linked != GL_FALSE;
773 void Program::FreeShaders()
777 LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
778 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
779 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mVertexShaderId ) );
783 if (mFragmentShaderId)
785 LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
786 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
787 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
788 mFragmentShaderId = 0;
792 void Program::ResetAttribsUniformCache()
794 // reset attribute locations
795 for( unsigned i = 0; i < mAttributeLocations.size() ; ++i )
797 mAttributeLocations[ i ].second = ATTRIB_UNKNOWN;
800 // reset all gl uniform locations
801 for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
803 // reset gl program locations and names
804 mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
807 mSamplerUniformLocations.clear();
809 // reset uniform caches
810 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
812 for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
814 // GL initializes uniforms to 0
815 mUniformCacheInt[ i ] = 0;
816 mUniformCacheFloat[ i ] = 0.0f;
817 mUniformCacheFloat2[ i ][ 0 ] = 0.0f;
818 mUniformCacheFloat2[ i ][ 1 ] = 0.0f;
819 mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
820 mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
821 mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
822 mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
826 } // namespace Internal