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 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, (GLuint)i, uniformMaxNameLength,
262 &nameLength, &number, &type, name );
264 if( type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE ) /// Is there a native sampler type?
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 ) || ( strncmp( token, "samplerCube", 11u ) == 0 ) )
283 token = strtok_r( NULL, " ;\n", &nextPtr );
284 for( size_t i=0; i<samplerUniformLocations.size(); ++i )
286 if( samplerUniformLocations[i].position == -1 &&
287 strncmp( token, samplerNames[i].c_str(), samplerNames[i].size() ) == 0 )
289 samplerUniformLocations[i].position = samplerPosition++;
296 DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token );
301 token = strtok_r( NULL, " ;\n", &nextPtr );
307 // Re-order according to declaration order in the fragment source.
308 size_t samplerUniformCount = samplerUniformLocations.size();
309 if( samplerUniformCount > 1 )
311 std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
314 mSamplerUniformLocations.resize( samplerUniformCount );
315 for( size_t i=0; i<samplerUniformCount; ++i )
317 mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
321 bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location )
324 if( index < mSamplerUniformLocations.size() )
326 location = mSamplerUniformLocations[index];
332 size_t Program::GetActiveSamplerCount() const
334 return mSamplerUniformLocations.size();
337 void Program::SetUniform1i( GLint location, GLint value0 )
339 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
341 if( UNIFORM_UNKNOWN == location )
343 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
344 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
345 // specified uniform variable will not be changed.following opengl silently do nothing
349 // check if uniform location fits the cache
350 if( location >= MAX_UNIFORM_CACHE_SIZE )
352 // not cached, make the gl call
353 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
354 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
358 // check if the value is different from what's already been set
359 if( value0 != mUniformCacheInt[ location ] )
362 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
363 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
365 mUniformCacheInt[ location ] = value0;
370 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
372 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
374 if( UNIFORM_UNKNOWN == location )
376 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
377 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
378 // specified uniform variable will not be changed.following opengl silently do nothing
382 // Not caching these as based on current analysis this is not called that often by our shaders
383 LOG_GL( "Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3 );
384 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4i( location, value0, value1, value2, value3 ) );
387 void Program::SetUniform1f( GLint location, GLfloat value0 )
389 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
391 if( UNIFORM_UNKNOWN == location )
393 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
394 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
395 // specified uniform variable will not be changed.following opengl silently do nothing
399 // check if uniform location fits the cache
400 if( location >= MAX_UNIFORM_CACHE_SIZE )
402 // not cached, make the gl call
403 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
404 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
408 // check if the same value has already been set, reset if it is different
409 if( ( fabsf(value0 - mUniformCacheFloat[ location ]) >= Math::MACHINE_EPSILON_1 ) )
412 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
413 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
416 mUniformCacheFloat[ location ] = value0;
421 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
423 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
425 if( UNIFORM_UNKNOWN == location )
427 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
428 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
429 // specified uniform variable will not be changed.following opengl silently do nothing
433 // check if uniform location fits the cache
434 if( location >= MAX_UNIFORM_CACHE_SIZE )
436 // not cached, make the gl call
437 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
438 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
442 // check if the same value has already been set, reset if it is different
443 if( ( fabsf(value0 - mUniformCacheFloat2[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
444 ( fabsf(value1 - mUniformCacheFloat2[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 ) )
447 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
448 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
451 mUniformCacheFloat2[ location ][ 0 ] = value0;
452 mUniformCacheFloat2[ location ][ 1 ] = value1;
457 void Program::SetSizeUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
459 if( ( fabsf(value0 - mSizeUniformCache.x) >= Math::MACHINE_EPSILON_1 )||
460 ( fabsf(value1 - mSizeUniformCache.y) >= Math::MACHINE_EPSILON_1 )||
461 ( fabsf(value2 - mSizeUniformCache.z) >= Math::MACHINE_EPSILON_1 ) )
463 mSizeUniformCache.x = value0;
464 mSizeUniformCache.y = value1;
465 mSizeUniformCache.z = value2;
466 SetUniform3f( location, value0, value1, value2 );
470 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
472 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
474 if( UNIFORM_UNKNOWN == location )
476 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
477 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
478 // specified uniform variable will not be changed.following opengl silently do nothing
482 // Not caching these as based on current analysis this is not called that often by our shaders
483 LOG_GL( "Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2 );
484 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform3f( location, value0, value1, value2 ) );
487 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
489 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
491 if( UNIFORM_UNKNOWN == location )
493 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
494 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
495 // specified uniform variable will not be changed.following opengl silently do nothing
499 // check if uniform location fits the cache
500 if( location >= MAX_UNIFORM_CACHE_SIZE )
502 // not cached, make the gl call
503 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
504 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
508 // check if the same value has already been set, reset if any component is different
509 // checking index 3 first because we're often animating alpha (rgba)
510 if( ( fabsf(value3 - mUniformCacheFloat4[ location ][ 3 ]) >= Math::MACHINE_EPSILON_1 )||
511 ( fabsf(value0 - mUniformCacheFloat4[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
512 ( fabsf(value1 - mUniformCacheFloat4[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 )||
513 ( fabsf(value2 - mUniformCacheFloat4[ location ][ 2 ]) >= Math::MACHINE_EPSILON_1 ) )
516 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
517 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
519 mUniformCacheFloat4[ location ][ 0 ] = value0;
520 mUniformCacheFloat4[ location ][ 1 ] = value1;
521 mUniformCacheFloat4[ location ][ 2 ] = value2;
522 mUniformCacheFloat4[ location ][ 3 ] = value3;
527 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
529 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
531 if( UNIFORM_UNKNOWN == location )
533 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
534 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
535 // specified uniform variable will not be changed.following opengl silently do nothing
539 // Not caching these calls. Based on current analysis this is called very often
540 // but with different values (we're using this for MVP matrices)
541 // NOTE! we never want driver or GPU to transpose
542 LOG_GL( "UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
543 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix4fv( location, count, GL_FALSE, value ) );
546 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
548 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
550 if( UNIFORM_UNKNOWN == location )
552 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
553 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
554 // specified uniform variable will not be changed.following opengl silently do nothing
559 // Not caching these calls. Based on current analysis this is called very often
560 // but with different values (we're using this for MVP matrices)
561 // NOTE! we never want driver or GPU to transpose
562 LOG_GL( "UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
563 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix3fv( location, count, GL_FALSE, value ) );
566 void Program::GlContextCreated()
570 void Program::GlContextDestroyed()
574 mFragmentShaderId = 0;
577 ResetAttribsUniformCache();
580 bool Program::ModifiesGeometry()
582 return mModifiesGeometry;
585 Program::Program( ProgramCache& cache, Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
587 mGlAbstraction( mCache.GetGlAbstraction() ),
588 mProjectionMatrix( NULL ),
591 mVertexShaderId( 0 ),
592 mFragmentShaderId( 0 ),
594 mProgramData(shaderData),
595 mModifiesGeometry( modifiesGeometry )
597 // reserve space for standard attributes
598 mAttributeLocations.reserve( ATTRIB_TYPE_LAST );
599 for( int i=0; i<ATTRIB_TYPE_LAST; ++i )
601 RegisterCustomAttribute( gStdAttribs[i] );
604 // reserve space for standard uniforms
605 mUniformLocations.reserve( UNIFORM_TYPE_LAST );
606 // reset built in uniform names in cache
607 for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
609 RegisterUniform( gStdUniforms[ i ] );
613 ResetAttribsUniformCache();
623 DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
624 DALI_ASSERT_DEBUG( mProgramId == 0 && "mProgramId != 0, so about to leak a GL resource by overwriting it." );
626 LOG_GL( "CreateProgram()\n" );
627 mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
629 GLint linked = GL_FALSE;
631 const bool binariesSupported = mCache.IsBinarySupported();
633 // if shader binaries are supported and ShaderData contains compiled bytecode?
634 if( binariesSupported && mProgramData->HasBinary() )
636 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
638 CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
640 CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
643 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
645 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
647 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
649 if( GL_FALSE == linked )
651 DALI_LOG_ERROR("Failed to load program binary \n");
654 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
657 Dali::Vector< char > szLog;
658 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
659 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() ) );
660 DALI_LOG_ERROR( "Program Link Error: %s\n", szLog.Begin() );
666 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Reused binary.\n" );
670 // Fall back to compiling and linking the vertex and fragment sources
671 if( GL_FALSE == linked )
673 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
674 if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader() ) )
676 if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader() ) )
680 if( binariesSupported && mLinked )
682 GLint binaryLength = 0;
684 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Compiled and linked.\n\nVS:\n%s\nFS:\n%s\n", mProgramData->GetVertexShader(), mProgramData->GetFragmentShader() );
686 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
687 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
688 if( binaryLength > 0 )
690 // Allocate space for the bytecode in ShaderData
691 mProgramData->AllocateBuffer(binaryLength);
692 // Copy the bytecode to ShaderData
693 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
694 mCache.StoreBinary( mProgramData );
695 DALI_LOG_INFO( Debug::Filter::gShader, Debug::General, "Saved binary.\n" );
702 GetActiveSamplerUniforms();
708 void Program::Unload()
712 if( this == mCache.GetCurrentProgram() )
714 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
716 mCache.SetCurrentProgram( NULL );
721 LOG_GL( "DeleteProgram(%d)\n", mProgramId );
722 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteProgram( mProgramId ) );
730 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
734 LOG_GL( "CreateShader(%d)\n", shaderType );
735 shaderId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateShader( shaderType ) );
736 LOG_GL( "AttachShader(%d,%d)\n", mProgramId, shaderId );
737 CHECK_GL( mGlAbstraction, mGlAbstraction.AttachShader( mProgramId, shaderId ) );
740 LOG_GL( "ShaderSource(%d)\n", shaderId );
741 CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
743 LOG_GL( "CompileShader(%d)\n", shaderId );
744 CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
747 LOG_GL( "GetShaderiv(%d)\n", shaderId );
748 CHECK_GL( mGlAbstraction, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
750 if (compiled == GL_FALSE)
752 DALI_LOG_ERROR("Failed to compile shader\n");
753 LogWithLineNumbers(src);
756 mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
759 Dali::Vector< char > szLog;
760 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
761 mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
762 DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
765 DALI_ASSERT_ALWAYS( 0 && "Shader compilation failure" );
768 return compiled != 0;
773 LOG_GL( "LinkProgram(%d)\n", mProgramId );
774 CHECK_GL( mGlAbstraction, mGlAbstraction.LinkProgram( mProgramId ) );
777 LOG_GL( "GetProgramiv(%d)\n", mProgramId );
778 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
780 if (linked == GL_FALSE)
782 DALI_LOG_ERROR("Shader failed to link \n");
785 mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength);
788 Dali::Vector< char > szLog;
789 szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
790 mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() );
791 DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog.Begin() );
794 DALI_ASSERT_ALWAYS( 0 && "Shader linking failure" );
797 mLinked = linked != GL_FALSE;
800 void Program::FreeShaders()
804 LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
805 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
806 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mVertexShaderId ) );
810 if (mFragmentShaderId)
812 LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
813 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
814 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
815 mFragmentShaderId = 0;
819 void Program::ResetAttribsUniformCache()
821 // reset attribute locations
822 for( unsigned i = 0; i < mAttributeLocations.size() ; ++i )
824 mAttributeLocations[ i ].second = ATTRIB_UNKNOWN;
827 // reset all gl uniform locations
828 for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
830 // reset gl program locations and names
831 mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
834 mSamplerUniformLocations.clear();
836 // reset uniform caches
837 mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;
839 for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
841 // GL initializes uniforms to 0
842 mUniformCacheInt[ i ] = 0;
843 mUniformCacheFloat[ i ] = 0.0f;
844 mUniformCacheFloat2[ i ][ 0 ] = 0.0f;
845 mUniformCacheFloat2[ i ][ 1 ] = 0.0f;
846 mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
847 mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
848 mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
849 mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
853 } // namespace Internal