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/integration-api/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 "aNormal", // ATTRIB_NORMAL
83 "aTexCoord", // ATTRIB_TEXCOORD
84 "aColor", // ATTRIB_COLOR
85 "aBoneWeights", // ATTRIB_BONE_WEIGHTS
86 "aBoneIndices" // ATTRIB_BONE_INDICES
89 const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
91 "uMvpMatrix", // UNIFORM_MVP_MATRIX
92 "uModelView", // UNIFORM_MODELVIEW_MATRIX
93 "uProjection", // UNIFORM_PROJECTION_MATRIX
94 "uModelMatrix", // UNIFORM_MODEL_MATRIX,
95 "uViewMatrix", // UNIFORM_VIEW_MATRIX,
96 "uNormalMatrix", // UNIFORM_NORMAL_MATRIX
97 "uColor", // UNIFORM_COLOR
98 "uCustomTextureCoords", // UNIFORM_CUSTOM_TEXTURE_COORDS
99 "sTexture", // UNIFORM_SAMPLER
100 "sTextureRect", // UNIFORM_SAMPLER_RECT
101 "sEffect", // UNIFORM_EFFECT_SAMPLER
102 "sEffectRect", // UNIFORM_EFFECT_SAMPLER_RECT
103 "uTimeDelta", // UNIFORM_TIME_DELTA
104 "sOpacityTexture", // UNIFORM_SAMPLER_OPACITY
105 "sNormalMapTexture", // UNIFORM_SAMPLER_NORMAL_MAP
106 "uTextColor", // UNIFORM_TEXT_COLOR
107 "uSmoothing", // UNIFORM_SMOOTHING
108 "uOutline", // UNIFORM_OUTLINE
109 "uOutlineColor", // UNIFORM_OUTLINE_COLOR
110 "uGlow", // UNIFORM_GLOW
111 "uGlowColor", // UNIFORM_GLOW_COLOR
112 "uShadow", // UNIFORM_SHADOW
113 "uShadowColor", // UNIFORM_SHADOW_COLOR
114 "uShadowSmoothing", // UNIFORM_SHADOW_SMOOTHING
115 "uGradientColor", // UNIFORM_GRADIENT_COLOR
116 "uGradientLine", // UNIFORM_GRADIENT_LINE
117 "uInvTextSize" // UNIFORM_INVERSE_TEXT_SIZE
120 } // <unnamed> namespace
124 Program* Program::New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
126 size_t shaderHash = shaderData->GetHashValue();
127 Program* program = cache.GetProgram( shaderHash );
129 if( NULL == program )
131 // program not found so create it
132 program = new Program( cache, shaderData, modifiesGeometry );
134 // we want to lazy load programs so dont do a Load yet, it gets done in Use()
136 cache.AddProgram( shaderHash, program );
151 if ( this != mCache.GetCurrentProgram() )
153 LOG_GL( "UseProgram(%d)\n", mProgramId );
154 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(mProgramId) );
155 INCREASE_COUNTER(PerformanceMonitor::SHADER_STATE_CHANGES);
157 mCache.SetCurrentProgram( this );
162 bool Program::IsUsed()
164 return ( this == mCache.GetCurrentProgram() );
167 GLint Program::GetAttribLocation( AttribType type )
169 DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
171 if( mAttribLocations[ type ] == ATTRIB_UNKNOWN )
173 GLint loc = CHECK_GL( mGlAbstraction, mGlAbstraction.GetAttribLocation( mProgramId, gStdAttribs[type] ) );
174 mAttribLocations[ type ] = loc;
175 LOG_GL( "GetAttribLocation(program=%d,%s) = %d\n", mProgramId, gStdAttribs[type], mAttribLocations[type] );
178 return mAttribLocations[type];
181 unsigned int Program::RegisterUniform( const std::string& name )
183 unsigned int index = 0;
184 // find the value from cache
185 for( ;index < mUniformLocations.size(); ++index )
187 if( mUniformLocations[ index ].first == name )
189 // name found so return index
193 // if we get here, index is one past end so push back the new name
194 mUniformLocations.push_back( std::make_pair( name, UNIFORM_NOT_QUERIED ) );
198 GLint Program::GetUniformLocation( unsigned int uniformIndex )
200 // debug check that index is within name cache
201 DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
203 // check if we have already queried the location of the uniform
204 GLint location = mUniformLocations[ uniformIndex ].second;
206 if( location == UNIFORM_NOT_QUERIED )
208 location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, mUniformLocations[ uniformIndex ].first.c_str() ) );
210 mUniformLocations[ uniformIndex ].second = location;
211 LOG_GL( "GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[ uniformIndex ].first.c_str(), mUniformLocations[ uniformIndex ].second );
217 void Program::SetUniform1i( GLint location, GLint value0 )
219 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
221 if( UNIFORM_UNKNOWN == location )
223 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
224 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
225 // specified uniform variable will not be changed.following opengl silently do nothing
229 // check if uniform location fits the cache
230 if( location >= MAX_UNIFORM_CACHE_SIZE )
232 // not cached, make the gl call
233 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
234 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
238 // check if the value is different from what's already been set
239 if( value0 != mUniformCacheInt[ location ] )
242 LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
243 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
245 mUniformCacheInt[ location ] = value0;
250 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
252 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
254 if( UNIFORM_UNKNOWN == location )
256 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
257 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
258 // specified uniform variable will not be changed.following opengl silently do nothing
262 // Not caching these as based on current analysis this is not called that often by our shaders
263 LOG_GL( "Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3 );
264 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4i( location, value0, value1, value2, value3 ) );
267 void Program::SetUniform1f( GLint location, GLfloat value0 )
269 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
271 if( UNIFORM_UNKNOWN == location )
273 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
274 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
275 // specified uniform variable will not be changed.following opengl silently do nothing
279 // check if uniform location fits the cache
280 if( location >= MAX_UNIFORM_CACHE_SIZE )
282 // not cached, make the gl call
283 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
284 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
288 // check if the same value has already been set, reset if it is different
289 if( ( fabsf(value0 - mUniformCacheFloat[ location ]) >= Math::MACHINE_EPSILON_1 ) )
292 LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
293 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
296 mUniformCacheFloat[ location ] = value0;
301 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
303 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
305 if( UNIFORM_UNKNOWN == location )
307 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
308 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
309 // specified uniform variable will not be changed.following opengl silently do nothing
313 // Not caching these as based on current analysis this is not called that often by our shaders
314 LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
315 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
318 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
320 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
322 if( UNIFORM_UNKNOWN == location )
324 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
325 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
326 // specified uniform variable will not be changed.following opengl silently do nothing
330 // Not caching these as based on current analysis this is not called that often by our shaders
331 LOG_GL( "Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2 );
332 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform3f( location, value0, value1, value2 ) );
335 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
337 DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
339 if( UNIFORM_UNKNOWN == location )
341 // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
342 // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
343 // specified uniform variable will not be changed.following opengl silently do nothing
347 // check if uniform location fits the cache
348 if( location >= MAX_UNIFORM_CACHE_SIZE )
350 // not cached, make the gl call
351 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
352 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
356 // check if the same value has already been set, reset if any component is different
357 // checking index 3 first because we're often animating alpha (rgba)
358 if( ( fabsf(value3 - mUniformCacheFloat4[ location ][ 3 ]) >= Math::MACHINE_EPSILON_1 )||
359 ( fabsf(value0 - mUniformCacheFloat4[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
360 ( fabsf(value1 - mUniformCacheFloat4[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 )||
361 ( fabsf(value2 - mUniformCacheFloat4[ location ][ 2 ]) >= Math::MACHINE_EPSILON_1 ) )
364 LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
365 CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
367 mUniformCacheFloat4[ location ][ 0 ] = value0;
368 mUniformCacheFloat4[ location ][ 1 ] = value1;
369 mUniformCacheFloat4[ location ][ 2 ] = value2;
370 mUniformCacheFloat4[ location ][ 3 ] = value3;
375 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
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 calls. Based on current analysis this is called very often
388 // but with different values (we're using this for MVP matrices)
389 // NOTE! we never want driver or GPU to transpose
390 LOG_GL( "UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
391 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix4fv( location, count, GL_FALSE, value ) );
394 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
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
407 // Not caching these calls. Based on current analysis this is called very often
408 // but with different values (we're using this for MVP matrices)
409 // NOTE! we never want driver or GPU to transpose
410 LOG_GL( "UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
411 CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix3fv( location, count, GL_FALSE, value ) );
414 void Program::GlContextCreated()
418 void Program::GlContextDestroyed()
422 mFragmentShaderId = 0;
425 ResetAttribsUniformCache();
428 bool Program::ModifiesGeometry()
430 return mModifiesGeometry;
433 Program::Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
435 mGlAbstraction( mCache.GetGlAbstraction() ),
436 mProjectionMatrix( NULL ),
439 mVertexShaderId( 0 ),
440 mFragmentShaderId( 0 ),
442 mProgramData(shaderData),
443 mModifiesGeometry( modifiesGeometry )
445 // reserve space for standard uniforms
446 mUniformLocations.reserve( UNIFORM_TYPE_LAST );
447 // reset built in uniform names in cache
448 for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
450 RegisterUniform( gStdUniforms[ i ] );
453 ResetAttribsUniformCache();
463 DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
465 LOG_GL( "CreateProgram()\n" );
466 mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
468 GLint linked = GL_FALSE;
470 const bool binariesSupported = mCache.IsBinarySupported();
472 // if shader binaries are supported and ShaderData contains compiled bytecode?
473 if( binariesSupported && mProgramData->HasBinary() )
475 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
477 CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
479 CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
482 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
484 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
486 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
488 if( GL_FALSE == linked )
490 DALI_LOG_ERROR("Failed to load program binary \n");
494 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
497 szLog = new char[ nLength ];
498 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog ) );
499 DALI_LOG_ERROR( "Program Link Error: %s\n", szLog );
510 // Fall back to compiling and linking the vertex and fragment sources
511 if( GL_FALSE == linked )
513 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
514 if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader() ) )
516 if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader() ) )
520 if( binariesSupported && mLinked )
522 GLint binaryLength = 0;
525 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
526 DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
527 if( binaryLength > 0 )
529 // Allocate space for the bytecode in ShaderData
530 mProgramData->AllocateBuffer(binaryLength);
531 // Copy the bytecode to ShaderData
532 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
533 mCache.StoreBinary( mProgramData );
544 void Program::Unload()
548 if( this == mCache.GetCurrentProgram() )
550 CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
552 mCache.SetCurrentProgram( NULL );
557 LOG_GL( "DeleteProgram(%d)\n", mProgramId );
558 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteProgram( mProgramId ) );
566 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
570 LOG_GL( "CreateShader(%d)\n", shaderType );
571 shaderId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateShader( shaderType ) );
572 LOG_GL( "AttachShader(%d,%d)\n", mProgramId, shaderId );
573 CHECK_GL( mGlAbstraction, mGlAbstraction.AttachShader( mProgramId, shaderId ) );
576 LOG_GL( "ShaderSource(%d)\n", shaderId );
577 CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
579 LOG_GL( "CompileShader(%d)\n", shaderId );
580 CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
583 LOG_GL( "GetShaderiv(%d)\n", shaderId );
584 CHECK_GL( mGlAbstraction, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
586 if (compiled == GL_FALSE)
588 DALI_LOG_ERROR("Failed to compile shader\n");
589 LogWithLineNumbers(src);
592 mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
595 Dali::Vector< char > szLog;
596 szLog.Reserve( nLength );
597 mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
598 DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
601 DALI_ASSERT_ALWAYS( 0 && "Shader compilation failure" );
604 return compiled != 0;
609 LOG_GL( "LinkProgram(%d)\n", mProgramId );
610 CHECK_GL( mGlAbstraction, mGlAbstraction.LinkProgram( mProgramId ) );
613 LOG_GL( "GetProgramiv(%d)\n", mProgramId );
614 CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
616 if (linked == GL_FALSE)
618 DALI_LOG_ERROR("Shader failed to link \n");
622 mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength);
625 szLog = new char[ nLength ];
626 mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog );
627 DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog );
631 DALI_ASSERT_DEBUG(0);
634 mLinked = linked != GL_FALSE;
637 void Program::FreeShaders()
641 LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
642 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
643 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mVertexShaderId ) );
647 if (mFragmentShaderId)
649 LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
650 CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
651 CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
652 mFragmentShaderId = 0;
656 void Program::ResetAttribsUniformCache()
658 // reset attribute locations
659 for( unsigned i = 0; i < ATTRIB_TYPE_LAST; ++i )
661 mAttribLocations[ i ] = ATTRIB_UNKNOWN;
664 // reset all gl uniform locations
665 for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
667 // reset gl program locations and names
668 mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
671 // reset uniform cache
672 for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
674 // GL initializes uniforms to 0
675 mUniformCacheInt[ i ] = 0;
676 mUniformCacheFloat[ i ] = 0.0f;
677 mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
678 mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
679 mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
680 mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
684 } // namespace Internal