3a47253a754eccbe6442d69d6f88dae9dc72f1a7
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/shaders/program.h>
20
21 // EXTERNAL INCLUDES
22 #include <iomanip>
23
24 // INTERNAL INCLUDES
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>
34
35 namespace
36 {
37 void LogWithLineNumbers( const char * source )
38 {
39   unsigned int lineNumber = 0u;
40   const char *prev = source;
41   const char *ptr = prev;
42
43   while( true )
44   {
45     if(lineNumber > 200u)
46     {
47       break;
48     }
49     // seek the next end of line or end of text
50     while( *ptr!='\n' && *ptr != '\0' )
51     {
52       ++ptr;
53     }
54
55     std::string line( prev, ptr-prev );
56     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%4d %s\n", lineNumber, line.c_str());
57
58     if( *ptr == '\0' )
59     {
60       break;
61     }
62     prev = ++ptr;
63     ++lineNumber;
64   }
65 }
66
67 } //namespace
68
69 namespace Dali
70 {
71
72 namespace Internal
73 {
74
75 // LOCAL STUFF
76 namespace
77 {
78
79 const char* gStdAttribs[ Program::ATTRIB_TYPE_LAST ] =
80 {
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
87 };
88
89 const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
90 {
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
118 };
119
120 }  // <unnamed> namespace
121
122 // IMPLEMENTATION
123
124 Program* Program::New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
125 {
126   size_t shaderHash = shaderData->GetHashValue();
127   Program* program = cache.GetProgram( shaderHash );
128
129   if( NULL == program )
130   {
131     // program not found so create it
132     program = new Program( cache, shaderData, modifiesGeometry );
133
134     // we want to lazy load programs so dont do a Load yet, it gets done in Use()
135
136     cache.AddProgram( shaderHash, program );
137   }
138
139   return program;
140 }
141
142 void Program::Use()
143 {
144   if ( !mLinked )
145   {
146     Load();
147   }
148
149   if ( mLinked )
150   {
151     if ( this != mCache.GetCurrentProgram() )
152     {
153       LOG_GL( "UseProgram(%d)\n", mProgramId );
154       CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(mProgramId) );
155       INCREASE_COUNTER(PerformanceMonitor::SHADER_STATE_CHANGES);
156
157       mCache.SetCurrentProgram( this );
158     }
159   }
160 }
161
162 bool Program::IsUsed()
163 {
164   return ( this == mCache.GetCurrentProgram() );
165 }
166
167 GLint Program::GetAttribLocation( AttribType type )
168 {
169   DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
170
171   if( mAttribLocations[ type ] == ATTRIB_UNKNOWN )
172   {
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] );
176   }
177
178   return mAttribLocations[type];
179 }
180
181 unsigned int Program::RegisterUniform( const std::string& name )
182 {
183   unsigned int index = 0;
184   // find the value from cache
185   for( ;index < mUniformLocations.size(); ++index )
186   {
187     if( mUniformLocations[ index ].first == name )
188     {
189       // name found so return index
190       return index;
191     }
192   }
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 ) );
195   return index;
196 }
197
198 GLint Program::GetUniformLocation( unsigned int uniformIndex )
199 {
200   // debug check that index is within name cache
201   DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
202
203   // check if we have already queried the location of the uniform
204   GLint location = mUniformLocations[ uniformIndex ].second;
205
206   if( location == UNIFORM_NOT_QUERIED )
207   {
208     location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, mUniformLocations[ uniformIndex ].first.c_str() ) );
209
210     mUniformLocations[ uniformIndex ].second = location;
211     LOG_GL( "GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[ uniformIndex ].first.c_str(), mUniformLocations[ uniformIndex ].second );
212   }
213
214   return location;
215 }
216
217 void Program::SetUniform1i( GLint location, GLint value0 )
218 {
219   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
220
221   if( UNIFORM_UNKNOWN == location )
222   {
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
226     return;
227   }
228
229   // check if uniform location fits the cache
230   if( location >= MAX_UNIFORM_CACHE_SIZE )
231   {
232     // not cached, make the gl call
233     LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
234     CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
235   }
236   else
237   {
238     // check if the value is different from what's already been set
239     if( value0 != mUniformCacheInt[ location ] )
240     {
241       // make the gl call
242       LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
243       CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
244       // update cache
245       mUniformCacheInt[ location ] = value0;
246     }
247   }
248 }
249
250 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
251 {
252   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
253
254   if( UNIFORM_UNKNOWN == location )
255   {
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
259     return;
260   }
261
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 ) );
265 }
266
267 void Program::SetUniform1f( GLint location, GLfloat value0 )
268 {
269   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
270
271   if( UNIFORM_UNKNOWN == location )
272   {
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
276     return;
277   }
278
279   // check if uniform location fits the cache
280   if( location >= MAX_UNIFORM_CACHE_SIZE )
281   {
282     // not cached, make the gl call
283     LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
284     CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
285   }
286   else
287   {
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 ) )
290     {
291       // make the gl call
292       LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
293       CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
294
295       // update cache
296       mUniformCacheFloat[ location ] = value0;
297     }
298   }
299 }
300
301 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
302 {
303   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
304
305   if( UNIFORM_UNKNOWN == location )
306   {
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
310     return;
311   }
312
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 ) );
316 }
317
318 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
319 {
320   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
321
322   if( UNIFORM_UNKNOWN == location )
323   {
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
327     return;
328   }
329
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 ) );
333 }
334
335 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
336 {
337   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
338
339   if( UNIFORM_UNKNOWN == location )
340   {
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
344     return;
345   }
346
347   // check if uniform location fits the cache
348   if( location >= MAX_UNIFORM_CACHE_SIZE )
349   {
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 ) );
353   }
354   else
355   {
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 ) )
362     {
363       // make the gl call
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 ) );
366       // update cache
367       mUniformCacheFloat4[ location ][ 0 ] = value0;
368       mUniformCacheFloat4[ location ][ 1 ] = value1;
369       mUniformCacheFloat4[ location ][ 2 ] = value2;
370       mUniformCacheFloat4[ location ][ 3 ] = value3;
371     }
372   }
373 }
374
375 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
376 {
377   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
378
379   if( UNIFORM_UNKNOWN == location )
380   {
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
384     return;
385   }
386
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 ) );
392 }
393
394 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
395 {
396   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
397
398   if( UNIFORM_UNKNOWN == location )
399   {
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
403     return;
404   }
405
406
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 ) );
412 }
413
414 void Program::GlContextCreated()
415 {
416 }
417
418 void Program::GlContextDestroyed()
419 {
420   mLinked = false;
421   mVertexShaderId = 0;
422   mFragmentShaderId = 0;
423   mProgramId = 0;
424
425   ResetAttribsUniformCache();
426 }
427
428 bool Program::ModifiesGeometry()
429 {
430   return mModifiesGeometry;
431 }
432
433 Program::Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
434 : mCache( cache ),
435   mGlAbstraction( mCache.GetGlAbstraction() ),
436   mProjectionMatrix( NULL ),
437   mViewMatrix( NULL ),
438   mLinked( false ),
439   mVertexShaderId( 0 ),
440   mFragmentShaderId( 0 ),
441   mProgramId( 0 ),
442   mProgramData(shaderData),
443   mModifiesGeometry( modifiesGeometry )
444 {
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 )
449   {
450     RegisterUniform( gStdUniforms[ i ] );
451   }
452   // reset values
453   ResetAttribsUniformCache();
454 }
455
456 Program::~Program()
457 {
458   Unload();
459 }
460
461 void Program::Load()
462 {
463   DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
464
465   LOG_GL( "CreateProgram()\n" );
466   mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
467
468   GLint linked = GL_FALSE;
469
470   const bool binariesSupported = mCache.IsBinarySupported();
471
472   // if shader binaries are supported and ShaderData contains compiled bytecode?
473   if( binariesSupported && mProgramData->HasBinary() )
474   {
475     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
476
477     CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
478
479     CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
480
481     GLint success;
482     CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
483
484     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
485
486     CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
487
488     if( GL_FALSE == linked )
489     {
490       DALI_LOG_ERROR("Failed to load program binary \n");
491
492       char* szLog = NULL;
493       GLint nLength;
494       CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
495       if(nLength > 0)
496       {
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 );
500
501         delete [] szLog;
502       }
503     }
504     else
505     {
506       mLinked = true;
507     }
508   }
509
510   // Fall back to compiling and linking the vertex and fragment sources
511   if( GL_FALSE == linked )
512   {
513     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
514     if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader() ) )
515     {
516       if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader() ) )
517       {
518         Link();
519
520         if( binariesSupported && mLinked )
521         {
522           GLint  binaryLength = 0;
523           GLenum binaryFormat;
524
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 )
528           {
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 );
534           }
535         }
536       }
537     }
538   }
539
540   // No longer needed
541   FreeShaders();
542 }
543
544 void Program::Unload()
545 {
546   FreeShaders();
547
548   if( this == mCache.GetCurrentProgram() )
549   {
550     CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
551
552     mCache.SetCurrentProgram( NULL );
553   }
554
555   if (mProgramId)
556   {
557     LOG_GL( "DeleteProgram(%d)\n", mProgramId );
558     CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteProgram( mProgramId ) );
559     mProgramId = 0;
560   }
561
562   mLinked = false;
563
564 }
565
566 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
567 {
568   if (!shaderId)
569   {
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 ) );
574   }
575
576   LOG_GL( "ShaderSource(%d)\n", shaderId );
577   CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
578
579   LOG_GL( "CompileShader(%d)\n", shaderId );
580   CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
581
582   GLint compiled;
583   LOG_GL( "GetShaderiv(%d)\n", shaderId );
584   CHECK_GL( mGlAbstraction, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
585
586   if (compiled == GL_FALSE)
587   {
588     DALI_LOG_ERROR("Failed to compile shader\n");
589     LogWithLineNumbers(src);
590
591     GLint nLength;
592     mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
593     if(nLength > 0)
594     {
595       Dali::Vector< char > szLog;
596       szLog.Resize( nLength );
597       mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
598       DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
599     }
600
601     DALI_ASSERT_ALWAYS( 0 && "Shader compilation failure" );
602   }
603
604   return compiled != 0;
605 }
606
607 void Program::Link()
608 {
609   LOG_GL( "LinkProgram(%d)\n", mProgramId );
610   CHECK_GL( mGlAbstraction, mGlAbstraction.LinkProgram( mProgramId ) );
611
612   GLint linked;
613   LOG_GL( "GetProgramiv(%d)\n", mProgramId );
614   CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
615
616   if (linked == GL_FALSE)
617   {
618     DALI_LOG_ERROR("Shader failed to link \n");
619
620     GLint nLength;
621     mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength);
622     if(nLength > 0)
623     {
624       Dali::Vector< char > szLog;
625       szLog.Resize( nLength );
626       mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() );
627       DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog.Begin() );
628     }
629
630     DALI_ASSERT_ALWAYS( 0 && "Shader linking failure" );
631   }
632
633   mLinked = linked != GL_FALSE;
634 }
635
636 void Program::FreeShaders()
637 {
638   if (mVertexShaderId)
639   {
640     LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
641     CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
642     CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mVertexShaderId ) );
643     mVertexShaderId = 0;
644   }
645
646   if (mFragmentShaderId)
647   {
648     LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
649     CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
650     CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
651     mFragmentShaderId = 0;
652   }
653 }
654
655 void Program::ResetAttribsUniformCache()
656 {
657   // reset attribute locations
658   for( unsigned i = 0; i < ATTRIB_TYPE_LAST; ++i )
659   {
660     mAttribLocations[ i ] = ATTRIB_UNKNOWN;
661   }
662
663   // reset all gl uniform locations
664   for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
665   {
666     // reset gl program locations and names
667     mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
668   }
669
670   // reset uniform cache
671   for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
672   {
673     // GL initializes uniforms to 0
674     mUniformCacheInt[ i ] = 0;
675     mUniformCacheFloat[ i ] = 0.0f;
676     mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
677     mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
678     mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
679     mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
680   }
681 }
682
683 } // namespace Internal
684
685 } // namespace Dali