Merge remote-tracking branch 'origin/tizen' into new_text
[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 };
107
108 }  // <unnamed> namespace
109
110 // IMPLEMENTATION
111
112 Program* Program::New( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
113 {
114   size_t shaderHash = shaderData->GetHashValue();
115   Program* program = cache.GetProgram( shaderHash );
116
117   if( NULL == program )
118   {
119     // program not found so create it
120     program = new Program( cache, shaderData, modifiesGeometry );
121
122     // we want to lazy load programs so dont do a Load yet, it gets done in Use()
123
124     cache.AddProgram( shaderHash, program );
125   }
126
127   return program;
128 }
129
130 void Program::Use()
131 {
132   if ( !mLinked )
133   {
134     Load();
135   }
136
137   if ( mLinked )
138   {
139     if ( this != mCache.GetCurrentProgram() )
140     {
141       LOG_GL( "UseProgram(%d)\n", mProgramId );
142       CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(mProgramId) );
143
144       mCache.SetCurrentProgram( this );
145     }
146   }
147 }
148
149 bool Program::IsUsed()
150 {
151   return ( this == mCache.GetCurrentProgram() );
152 }
153
154 GLint Program::GetAttribLocation( AttribType type )
155 {
156   DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
157
158   if( mAttribLocations[ type ] == ATTRIB_UNKNOWN )
159   {
160     GLint loc = CHECK_GL( mGlAbstraction, mGlAbstraction.GetAttribLocation( mProgramId, gStdAttribs[type] ) );
161     mAttribLocations[ type ] = loc;
162     LOG_GL( "GetAttribLocation(program=%d,%s) = %d\n", mProgramId, gStdAttribs[type], mAttribLocations[type] );
163   }
164
165   return mAttribLocations[type];
166 }
167
168 unsigned int Program::RegisterUniform( const std::string& name )
169 {
170   unsigned int index = 0;
171   // find the value from cache
172   for( ;index < mUniformLocations.size(); ++index )
173   {
174     if( mUniformLocations[ index ].first == name )
175     {
176       // name found so return index
177       return index;
178     }
179   }
180   // if we get here, index is one past end so push back the new name
181   mUniformLocations.push_back( std::make_pair( name, UNIFORM_NOT_QUERIED ) );
182   return index;
183 }
184
185 GLint Program::GetUniformLocation( unsigned int uniformIndex )
186 {
187   // debug check that index is within name cache
188   DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
189
190   // check if we have already queried the location of the uniform
191   GLint location = mUniformLocations[ uniformIndex ].second;
192
193   if( location == UNIFORM_NOT_QUERIED )
194   {
195     location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, mUniformLocations[ uniformIndex ].first.c_str() ) );
196
197     mUniformLocations[ uniformIndex ].second = location;
198     LOG_GL( "GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[ uniformIndex ].first.c_str(), mUniformLocations[ uniformIndex ].second );
199   }
200
201   return location;
202 }
203
204 void Program::SetUniform1i( GLint location, GLint value0 )
205 {
206   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
207
208   if( UNIFORM_UNKNOWN == location )
209   {
210     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
211     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
212     // specified uniform variable will not be changed.following opengl silently do nothing
213     return;
214   }
215
216   // check if uniform location fits the cache
217   if( location >= MAX_UNIFORM_CACHE_SIZE )
218   {
219     // not cached, make the gl call
220     LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
221     CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
222   }
223   else
224   {
225     // check if the value is different from what's already been set
226     if( value0 != mUniformCacheInt[ location ] )
227     {
228       // make the gl call
229       LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
230       CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1i( location, value0 ) );
231       // update cache
232       mUniformCacheInt[ location ] = value0;
233     }
234   }
235 }
236
237 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
238 {
239   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
240
241   if( UNIFORM_UNKNOWN == location )
242   {
243     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
244     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
245     // specified uniform variable will not be changed.following opengl silently do nothing
246     return;
247   }
248
249   // Not caching these as based on current analysis this is not called that often by our shaders
250   LOG_GL( "Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3 );
251   CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4i( location, value0, value1, value2, value3 ) );
252 }
253
254 void Program::SetUniform1f( GLint location, GLfloat value0 )
255 {
256   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
257
258   if( UNIFORM_UNKNOWN == location )
259   {
260     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
261     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
262     // specified uniform variable will not be changed.following opengl silently do nothing
263     return;
264   }
265
266   // check if uniform location fits the cache
267   if( location >= MAX_UNIFORM_CACHE_SIZE )
268   {
269     // not cached, make the gl call
270     LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
271     CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
272   }
273   else
274   {
275     // check if the same value has already been set, reset if it is different
276     if( ( fabsf(value0 - mUniformCacheFloat[ location ]) >= Math::MACHINE_EPSILON_1 ) )
277     {
278       // make the gl call
279       LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
280       CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform1f( location, value0 ) );
281
282       // update cache
283       mUniformCacheFloat[ location ] = value0;
284     }
285   }
286 }
287
288 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
289 {
290   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
291
292   if( UNIFORM_UNKNOWN == location )
293   {
294     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
295     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
296     // specified uniform variable will not be changed.following opengl silently do nothing
297     return;
298   }
299
300   // Not caching these as based on current analysis this is not called that often by our shaders
301   LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
302   CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform2f( location, value0, value1 ) );
303 }
304
305 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
306 {
307   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
308
309   if( UNIFORM_UNKNOWN == location )
310   {
311     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
312     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
313     // specified uniform variable will not be changed.following opengl silently do nothing
314     return;
315   }
316
317   // Not caching these as based on current analysis this is not called that often by our shaders
318   LOG_GL( "Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2 );
319   CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform3f( location, value0, value1, value2 ) );
320 }
321
322 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
323 {
324   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
325
326   if( UNIFORM_UNKNOWN == location )
327   {
328     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
329     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
330     // specified uniform variable will not be changed.following opengl silently do nothing
331     return;
332   }
333
334   // check if uniform location fits the cache
335   if( location >= MAX_UNIFORM_CACHE_SIZE )
336   {
337     // not cached, make the gl call
338     LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
339     CHECK_GL( mGlAbstraction, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
340   }
341   else
342   {
343     // check if the same value has already been set, reset if any component is different
344     // checking index 3 first because we're often animating alpha (rgba)
345     if( ( fabsf(value3 - mUniformCacheFloat4[ location ][ 3 ]) >= Math::MACHINE_EPSILON_1 )||
346         ( fabsf(value0 - mUniformCacheFloat4[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
347         ( fabsf(value1 - mUniformCacheFloat4[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 )||
348         ( fabsf(value2 - mUniformCacheFloat4[ location ][ 2 ]) >= Math::MACHINE_EPSILON_1 ) )
349     {
350       // 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       // update cache
354       mUniformCacheFloat4[ location ][ 0 ] = value0;
355       mUniformCacheFloat4[ location ][ 1 ] = value1;
356       mUniformCacheFloat4[ location ][ 2 ] = value2;
357       mUniformCacheFloat4[ location ][ 3 ] = value3;
358     }
359   }
360 }
361
362 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
363 {
364   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
365
366   if( UNIFORM_UNKNOWN == location )
367   {
368     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
369     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
370     // specified uniform variable will not be changed.following opengl silently do nothing
371     return;
372   }
373
374   // Not caching these calls. Based on current analysis this is called very often
375   // but with different values (we're using this for MVP matrices)
376   // NOTE! we never want driver or GPU to transpose
377   LOG_GL( "UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
378   CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix4fv( location, count, GL_FALSE, value ) );
379 }
380
381 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
382 {
383   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
384
385   if( UNIFORM_UNKNOWN == location )
386   {
387     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
388     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
389     // specified uniform variable will not be changed.following opengl silently do nothing
390     return;
391   }
392
393
394   // Not caching these calls. Based on current analysis this is called very often
395   // but with different values (we're using this for MVP matrices)
396   // NOTE! we never want driver or GPU to transpose
397   LOG_GL( "UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
398   CHECK_GL( mGlAbstraction, mGlAbstraction.UniformMatrix3fv( location, count, GL_FALSE, value ) );
399 }
400
401 void Program::GlContextCreated()
402 {
403 }
404
405 void Program::GlContextDestroyed()
406 {
407   mLinked = false;
408   mVertexShaderId = 0;
409   mFragmentShaderId = 0;
410   mProgramId = 0;
411
412   ResetAttribsUniformCache();
413 }
414
415 bool Program::ModifiesGeometry()
416 {
417   return mModifiesGeometry;
418 }
419
420 Program::Program( ProgramCache& cache, Integration::ShaderDataPtr shaderData, bool modifiesGeometry )
421 : mCache( cache ),
422   mGlAbstraction( mCache.GetGlAbstraction() ),
423   mProjectionMatrix( NULL ),
424   mViewMatrix( NULL ),
425   mLinked( false ),
426   mVertexShaderId( 0 ),
427   mFragmentShaderId( 0 ),
428   mProgramId( 0 ),
429   mProgramData(shaderData),
430   mModifiesGeometry( modifiesGeometry )
431 {
432   // reserve space for standard uniforms
433   mUniformLocations.reserve( UNIFORM_TYPE_LAST );
434   // reset built in uniform names in cache
435   for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
436   {
437     RegisterUniform( gStdUniforms[ i ] );
438   }
439   // reset values
440   ResetAttribsUniformCache();
441 }
442
443 Program::~Program()
444 {
445   Unload();
446 }
447
448 void Program::Load()
449 {
450   DALI_ASSERT_ALWAYS( NULL != mProgramData.Get() && "Program data is not initialized" );
451
452   LOG_GL( "CreateProgram()\n" );
453   mProgramId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateProgram() );
454
455   GLint linked = GL_FALSE;
456
457   const bool binariesSupported = mCache.IsBinarySupported();
458
459   // if shader binaries are supported and ShaderData contains compiled bytecode?
460   if( binariesSupported && mProgramData->HasBinary() )
461   {
462     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->GetBufferSize());
463
464     CHECK_GL( mGlAbstraction, mGlAbstraction.ProgramBinary(mProgramId, mCache.ProgramBinaryFormat(), mProgramData->GetBufferData(), mProgramData->GetBufferSize()) );
465
466     CHECK_GL( mGlAbstraction, mGlAbstraction.ValidateProgram(mProgramId) );
467
468     GLint success;
469     CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
470
471     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
472
473     CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
474
475     if( GL_FALSE == linked )
476     {
477       DALI_LOG_ERROR("Failed to load program binary \n");
478
479       GLint nLength;
480       CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
481       if(nLength > 0)
482       {
483         Dali::Vector< char > szLog;
484         szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
485         CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() ) );
486         DALI_LOG_ERROR( "Program Link Error: %s\n", szLog.Begin() );
487       }
488     }
489     else
490     {
491       mLinked = true;
492     }
493   }
494
495   // Fall back to compiling and linking the vertex and fragment sources
496   if( GL_FALSE == linked )
497   {
498     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
499     if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->GetVertexShader() ) )
500     {
501       if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->GetFragmentShader() ) )
502       {
503         Link();
504
505         if( binariesSupported && mLinked )
506         {
507           GLint  binaryLength = 0;
508           GLenum binaryFormat;
509
510           CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
511           DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
512           if( binaryLength > 0 )
513           {
514             // Allocate space for the bytecode in ShaderData
515             mProgramData->AllocateBuffer(binaryLength);
516             // Copy the bytecode to ShaderData
517             CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->GetBufferData()) );
518             mCache.StoreBinary( mProgramData );
519           }
520         }
521       }
522     }
523   }
524
525   // No longer needed
526   FreeShaders();
527 }
528
529 void Program::Unload()
530 {
531   FreeShaders();
532
533   if( this == mCache.GetCurrentProgram() )
534   {
535     CHECK_GL( mGlAbstraction, mGlAbstraction.UseProgram(0) );
536
537     mCache.SetCurrentProgram( NULL );
538   }
539
540   if (mProgramId)
541   {
542     LOG_GL( "DeleteProgram(%d)\n", mProgramId );
543     CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteProgram( mProgramId ) );
544     mProgramId = 0;
545   }
546
547   mLinked = false;
548
549 }
550
551 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
552 {
553   if (!shaderId)
554   {
555     LOG_GL( "CreateShader(%d)\n", shaderType );
556     shaderId = CHECK_GL( mGlAbstraction, mGlAbstraction.CreateShader( shaderType ) );
557     LOG_GL( "AttachShader(%d,%d)\n", mProgramId, shaderId );
558     CHECK_GL( mGlAbstraction, mGlAbstraction.AttachShader( mProgramId, shaderId ) );
559   }
560
561   LOG_GL( "ShaderSource(%d)\n", shaderId );
562   CHECK_GL( mGlAbstraction, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
563
564   LOG_GL( "CompileShader(%d)\n", shaderId );
565   CHECK_GL( mGlAbstraction, mGlAbstraction.CompileShader( shaderId ) );
566
567   GLint compiled;
568   LOG_GL( "GetShaderiv(%d)\n", shaderId );
569   CHECK_GL( mGlAbstraction, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
570
571   if (compiled == GL_FALSE)
572   {
573     DALI_LOG_ERROR("Failed to compile shader\n");
574     LogWithLineNumbers(src);
575
576     GLint nLength;
577     mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
578     if(nLength > 0)
579     {
580       Dali::Vector< char > szLog;
581       szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
582       mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, szLog.Begin() );
583       DALI_LOG_ERROR( "Shader Compiler Error: %s\n", szLog.Begin() );
584     }
585
586     DALI_ASSERT_ALWAYS( 0 && "Shader compilation failure" );
587   }
588
589   return compiled != 0;
590 }
591
592 void Program::Link()
593 {
594   LOG_GL( "LinkProgram(%d)\n", mProgramId );
595   CHECK_GL( mGlAbstraction, mGlAbstraction.LinkProgram( mProgramId ) );
596
597   GLint linked;
598   LOG_GL( "GetProgramiv(%d)\n", mProgramId );
599   CHECK_GL( mGlAbstraction, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
600
601   if (linked == GL_FALSE)
602   {
603     DALI_LOG_ERROR("Shader failed to link \n");
604
605     GLint nLength;
606     mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength);
607     if(nLength > 0)
608     {
609       Dali::Vector< char > szLog;
610       szLog.Reserve( nLength ); // Don't call Resize as we don't want to initialise the data, just reserve a buffer
611       mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog.Begin() );
612       DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog.Begin() );
613     }
614
615     DALI_ASSERT_ALWAYS( 0 && "Shader linking failure" );
616   }
617
618   mLinked = linked != GL_FALSE;
619 }
620
621 void Program::FreeShaders()
622 {
623   if (mVertexShaderId)
624   {
625     LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
626     CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
627     CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mVertexShaderId ) );
628     mVertexShaderId = 0;
629   }
630
631   if (mFragmentShaderId)
632   {
633     LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
634     CHECK_GL( mGlAbstraction, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
635     CHECK_GL( mGlAbstraction, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
636     mFragmentShaderId = 0;
637   }
638 }
639
640 void Program::ResetAttribsUniformCache()
641 {
642   // reset attribute locations
643   for( unsigned i = 0; i < ATTRIB_TYPE_LAST; ++i )
644   {
645     mAttribLocations[ i ] = ATTRIB_UNKNOWN;
646   }
647
648   // reset all gl uniform locations
649   for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
650   {
651     // reset gl program locations and names
652     mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
653   }
654
655   // reset uniform cache
656   for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
657   {
658     // GL initializes uniforms to 0
659     mUniformCacheInt[ i ] = 0;
660     mUniformCacheFloat[ i ] = 0.0f;
661     mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
662     mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
663     mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
664     mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
665   }
666 }
667
668 } // namespace Internal
669
670 } // namespace Dali