Lazy load programs
[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 <dali/public-api/common/vector-wrapper.h>
23 #include <iomanip>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/constants.h>
28 #include <dali/internal/render/common/performance-monitor.h>
29 #include <dali/integration-api/debug.h>
30 #include <dali/integration-api/shader-data.h>
31
32 namespace
33 {
34 void LogWithLineNumbers( const char * source )
35 {
36   unsigned int lineNumber = 0u;
37   const char *prev = source;
38   const char *ptr = prev;
39
40   while( true )
41   {
42     if(lineNumber > 200u)
43     {
44       break;
45     }
46     // seek the next end of line or end of text
47     while( *ptr!='\n' && *ptr != '\0' )
48     {
49       ++ptr;
50     }
51
52     std::string line( prev, ptr-prev );
53     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%4d %s\n", lineNumber, line.c_str());
54
55     if( *ptr == '\0' )
56     {
57       break;
58     }
59     prev = ++ptr;
60     ++lineNumber;
61   }
62 }
63
64 } //namespace
65
66 namespace Dali
67 {
68
69 namespace Internal
70 {
71
72 // LOCAL STUFF
73 namespace
74 {
75
76 const char* gStdAttribs[ Program::ATTRIB_TYPE_LAST ] =
77 {
78   "aPosition",    // ATTRIB_POSITION
79   "aNormal",      // ATTRIB_NORMAL
80   "aTexCoord",    // ATTRIB_TEXCOORD
81   "aColor",       // ATTRIB_COLOR
82   "aBoneWeights", // ATTRIB_BONE_WEIGHTS
83   "aBoneIndices"  // ATTRIB_BONE_INDICES
84 };
85
86 const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
87 {
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   "uCustomTextureCoords", // UNIFORM_CUSTOM_TEXTURE_COORDS
96   "sTexture",             // UNIFORM_SAMPLER
97   "sTextureRect",         // UNIFORM_SAMPLER_RECT
98   "sEffect",              // UNIFORM_EFFECT_SAMPLER
99   "sEffectRect",          // UNIFORM_EFFECT_SAMPLER_RECT
100   "uTimeDelta",           // UNIFORM_TIME_DELTA
101   "sOpacityTexture",      // UNIFORM_SAMPLER_OPACITY
102   "sNormalMapTexture",    // UNIFORM_SAMPLER_NORMAL_MAP
103   "uTextColor",           // UNIFORM_TEXT_COLOR
104   "uSmoothing",           // UNIFORM_SMOOTHING
105   "uOutline",             // UNIFORM_OUTLINE
106   "uOutlineColor",        // UNIFORM_OUTLINE_COLOR
107   "uGlow",                // UNIFORM_GLOW
108   "uGlowColor",           // UNIFORM_GLOW_COLOR
109   "uShadow",              // UNIFORM_SHADOW
110   "uShadowColor",         // UNIFORM_SHADOW_COLOR
111   "uShadowSmoothing",     // UNIFORM_SHADOW_SMOOTHING
112   "uGradientColor",       // UNIFORM_GRADIENT_COLOR
113   "uGradientLine",        // UNIFORM_GRADIENT_LINE
114   "uInvTextSize"          // UNIFORM_INVERSE_TEXT_SIZE
115 };
116
117 }  // <unnamed> namespace
118
119 // IMPLEMENTATION
120
121 Program* Program::New( const Integration::ResourceId& resourceId, Integration::ShaderData* shaderData, Context& context, bool modifiesGeometry )
122 {
123   size_t shaderHash = shaderData->GetHashValue();
124   Program* program = context.GetCachedProgram( shaderHash );
125
126   if( NULL == program )
127   {
128     // program not found so create it
129     program = new Program( shaderData, context, modifiesGeometry );
130
131     // we want to lazy load programs so dont do a Load yet, it gets done in Use
132
133     // tell context to cache it
134     context.CacheProgram( shaderHash, program );
135   }
136
137   return program;
138 }
139
140 void Program::Use()
141 {
142   if ( !mLinked &&
143        mContext.IsGlContextCreated() )
144   {
145     Load();
146   }
147
148   if ( mLinked )
149   {
150     if ( this != mContext.GetCurrentProgram() )
151     {
152       LOG_GL( "UseProgram(%d)\n", mProgramId );
153       CHECK_GL( mContext, mGlAbstraction.UseProgram(mProgramId) );
154       INCREASE_COUNTER(PerformanceMonitor::SHADER_STATE_CHANGES);
155
156       mContext.SetCurrentProgram( this );
157     }
158   }
159 }
160
161 bool Program::IsUsed()
162 {
163   return ( this == mContext.GetCurrentProgram() );
164 }
165
166 GLint Program::GetAttribLocation( AttribType type )
167 {
168   DALI_ASSERT_DEBUG(type != ATTRIB_UNKNOWN);
169
170   if( mAttribLocations[ type ] == ATTRIB_UNKNOWN )
171   {
172     LOG_GL( "GetAttribLocation(program=%d,%s) = %d\n", mProgramId, gStdAttribs[type], mAttribLocations[type] );
173     GLint loc = CHECK_GL( mContext, mGlAbstraction.GetAttribLocation( mProgramId, gStdAttribs[type] ) );
174     mAttribLocations[ type ] = loc;
175   }
176
177   return mAttribLocations[type];
178 }
179
180 unsigned int Program::RegisterUniform( const std::string& name )
181 {
182   unsigned int index = 0;
183   // find the value from cache
184   for( ;index < mUniformLocations.size(); ++index )
185   {
186     if( mUniformLocations[ index ].first == name )
187     {
188       // name found so return index
189       return index;
190     }
191   }
192   // if we get here, index is one past end so push back the new name
193   mUniformLocations.push_back( std::make_pair( name, UNIFORM_NOT_QUERIED ) );
194   return index;
195 }
196
197 GLint Program::GetUniformLocation( unsigned int uniformIndex )
198 {
199   // debug check that index is within name cache
200   DALI_ASSERT_DEBUG( mUniformLocations.size() > uniformIndex );
201
202   // check if we have already queried the location of the uniform
203   GLint location = mUniformLocations[ uniformIndex ].second;
204
205   if( location == UNIFORM_NOT_QUERIED )
206   {
207     LOG_GL( "GetUniformLocation(program=%d,%s) = %d\n", mProgramId, mUniformLocations[ uniformIndex ].first.c_str(), mUniformLocations[ uniformIndex ].second );
208     location = CHECK_GL( mContext, mGlAbstraction.GetUniformLocation( mProgramId, mUniformLocations[ uniformIndex ].first.c_str() ) );
209
210     mUniformLocations[ uniformIndex ].second = location;
211   }
212
213   return location;
214 }
215
216 void Program::SetUniform1i( GLint location, GLint value0 )
217 {
218   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
219
220   if( UNIFORM_UNKNOWN == location )
221   {
222     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
223     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
224     // specified uniform variable will not be changed.following opengl silently do nothing
225     return;
226   }
227
228   // check if uniform location fits the cache
229   if( location >= MAX_UNIFORM_CACHE_SIZE )
230   {
231     // not cached, make the gl call
232     LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
233     CHECK_GL( mContext, mGlAbstraction.Uniform1i( location, value0 ) );
234   }
235   else
236   {
237     // check if the value is different from what's already been set
238     if( value0 != mUniformCacheInt[ location ] )
239     {
240       // make the gl call
241       LOG_GL( "Uniform1i(%d,%d)\n", location, value0 );
242       CHECK_GL( mContext, mGlAbstraction.Uniform1i( location, value0 ) );
243       // update cache
244       mUniformCacheInt[ location ] = value0;
245     }
246   }
247 }
248
249 void Program::SetUniform4i( GLint location, GLint value0, GLint value1, GLint value2, GLint value3 )
250 {
251   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
252
253   if( UNIFORM_UNKNOWN == location )
254   {
255     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
256     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
257     // specified uniform variable will not be changed.following opengl silently do nothing
258     return;
259   }
260
261   // Not caching these as based on current analysis this is not called that often by our shaders
262   LOG_GL( "Uniform4i(%d,%d,%d,%d,%d)\n", location, value0, value1, value2, value3 );
263   CHECK_GL( mContext, mGlAbstraction.Uniform4i( location, value0, value1, value2, value3 ) );
264 }
265
266 void Program::SetUniform1f( GLint location, GLfloat value0 )
267 {
268   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
269
270   if( UNIFORM_UNKNOWN == location )
271   {
272     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
273     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
274     // specified uniform variable will not be changed.following opengl silently do nothing
275     return;
276   }
277
278   // check if uniform location fits the cache
279   if( location >= MAX_UNIFORM_CACHE_SIZE )
280   {
281     // not cached, make the gl call
282     LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
283     CHECK_GL( mContext, mGlAbstraction.Uniform1f( location, value0 ) );
284   }
285   else
286   {
287     // check if the same value has already been set, reset if it is different
288     if( ( fabsf(value0 - mUniformCacheFloat[ location ]) >= Math::MACHINE_EPSILON_1 ) )
289     {
290       // make the gl call
291       LOG_GL( "Uniform1f(%d,%f)\n", location, value0 );
292       CHECK_GL( mContext, mGlAbstraction.Uniform1f( location, value0 ) );
293
294       // update cache
295       mUniformCacheFloat[ location ] = value0;
296     }
297   }
298 }
299
300 void Program::SetUniform2f( GLint location, GLfloat value0, GLfloat value1 )
301 {
302   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
303
304   if( UNIFORM_UNKNOWN == location )
305   {
306     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
307     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
308     // specified uniform variable will not be changed.following opengl silently do nothing
309     return;
310   }
311
312   // Not caching these as based on current analysis this is not called that often by our shaders
313   LOG_GL( "Uniform2f(%d,%f,%f)\n", location, value0, value1 );
314   CHECK_GL( mContext, mGlAbstraction.Uniform2f( location, value0, value1 ) );
315 }
316
317 void Program::SetUniform3f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2 )
318 {
319   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
320
321   if( UNIFORM_UNKNOWN == location )
322   {
323     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
324     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
325     // specified uniform variable will not be changed.following opengl silently do nothing
326     return;
327   }
328
329   // Not caching these as based on current analysis this is not called that often by our shaders
330   LOG_GL( "Uniform3f(%d,%f,%f,%f)\n", location, value0, value1, value2 );
331   CHECK_GL( mContext, mGlAbstraction.Uniform3f( location, value0, value1, value2 ) );
332 }
333
334 void Program::SetUniform4f( GLint location, GLfloat value0, GLfloat value1, GLfloat value2, GLfloat value3 )
335 {
336   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
337
338   if( UNIFORM_UNKNOWN == location )
339   {
340     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
341     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
342     // specified uniform variable will not be changed.following opengl silently do nothing
343     return;
344   }
345
346   // check if uniform location fits the cache
347   if( location >= MAX_UNIFORM_CACHE_SIZE )
348   {
349     // not cached, make the gl call
350     LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
351     CHECK_GL( mContext, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
352   }
353   else
354   {
355     // check if the same value has already been set, reset if any component is different
356     // checking index 3 first because we're often animating alpha (rgba)
357     if( ( fabsf(value3 - mUniformCacheFloat4[ location ][ 3 ]) >= Math::MACHINE_EPSILON_1 )||
358         ( fabsf(value0 - mUniformCacheFloat4[ location ][ 0 ]) >= Math::MACHINE_EPSILON_1 )||
359         ( fabsf(value1 - mUniformCacheFloat4[ location ][ 1 ]) >= Math::MACHINE_EPSILON_1 )||
360         ( fabsf(value2 - mUniformCacheFloat4[ location ][ 2 ]) >= Math::MACHINE_EPSILON_1 ) )
361     {
362       // make the gl call
363       LOG_GL( "Uniform4f(%d,%f,%f,%f,%f)\n", location, value0, value1, value2, value3 );
364       CHECK_GL( mContext, mGlAbstraction.Uniform4f( location, value0, value1, value2, value3 ) );
365       // update cache
366       mUniformCacheFloat4[ location ][ 0 ] = value0;
367       mUniformCacheFloat4[ location ][ 1 ] = value1;
368       mUniformCacheFloat4[ location ][ 2 ] = value2;
369       mUniformCacheFloat4[ location ][ 3 ] = value3;
370     }
371   }
372 }
373
374 void Program::SetUniformMatrix4fv( GLint location, GLsizei count, const GLfloat* value )
375 {
376   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
377
378   if( UNIFORM_UNKNOWN == location )
379   {
380     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
381     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
382     // specified uniform variable will not be changed.following opengl silently do nothing
383     return;
384   }
385
386   // Not caching these calls. Based on current analysis this is called very often
387   // but with different values (we're using this for MVP matrices)
388   // NOTE! we never want driver or GPU to transpose
389   LOG_GL( "UniformMatrix4fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
390   CHECK_GL( mContext, mGlAbstraction.UniformMatrix4fv( location, count, GL_FALSE, value ) );
391 }
392
393 void Program::SetUniformMatrix3fv( GLint location, GLsizei count, const GLfloat* value )
394 {
395   DALI_ASSERT_DEBUG( IsUsed() ); // should not call this if this program is not used
396
397   if( UNIFORM_UNKNOWN == location )
398   {
399     // From http://www.khronos.org/opengles/sdk/docs/man/xhtml/glUniform.xml : Notes
400     // If location is equal to UNIFORM_UNKNOWN, the data passed in will be silently ignored and the
401     // specified uniform variable will not be changed.following opengl silently do nothing
402     return;
403   }
404
405
406   // Not caching these calls. Based on current analysis this is called very often
407   // but with different values (we're using this for MVP matrices)
408   // NOTE! we never want driver or GPU to transpose
409   LOG_GL( "UniformMatrix3fv(%d,%d,GL_FALSE,%x)\n", location, count, value );
410   CHECK_GL( mContext, mGlAbstraction.UniformMatrix3fv( location, count, GL_FALSE, value ) );
411 }
412
413 void Program::GlContextCreated()
414 {
415   if (!mLinked)
416   {
417     Load();
418   }
419 }
420
421 void Program::GlContextDestroyed()
422 {
423   Unload();
424
425   mVertexShaderId = 0;
426   mFragmentShaderId = 0;
427   mProgramId = 0;
428   mContext.SetCurrentProgram( NULL );
429
430   ResetAttribsUniforms();
431 }
432
433 bool Program::ModifiesGeometry()
434 {
435   return mModifiesGeometry;
436 }
437
438 Program::Program(Integration::ShaderData* shaderData, Context& context, bool modifiesGeometry )
439 : mContext( context ),
440   mGlAbstraction( context.GetAbstraction() ),
441   mProjectionMatrix( NULL ),
442   mViewMatrix( NULL ),
443   mLinked( false ),
444   mVertexShaderId( 0 ),
445   mFragmentShaderId( 0 ),
446   mProgramId( 0 ),
447   mProgramData(shaderData),
448   mModifiesGeometry( modifiesGeometry )
449 {
450   // reserve space for standard uniforms
451   mUniformLocations.reserve( UNIFORM_TYPE_LAST );
452   // reset built in uniform names in cache
453   for( int i = 0; i < UNIFORM_TYPE_LAST; ++i )
454   {
455     RegisterUniform( gStdUniforms[ i ] );
456   }
457   // reset values
458   ResetAttribsUniforms();
459 }
460
461 Program::~Program()
462 {
463   Unload();
464 }
465
466 void Program::Load()
467 {
468   DALI_ASSERT_ALWAYS( NULL != mProgramData && "Program data is not initialized" );
469
470   Unload();
471
472   LOG_GL( "CreateProgram()\n" );
473   mProgramId = CHECK_GL( mContext, mGlAbstraction.CreateProgram() );
474
475   GLint linked = GL_FALSE;
476
477   // ShaderData contains compiled bytecode?
478   if( 0 != mContext.CachedNumberOfProgramBinaryFormats() && mProgramData->HasBinary() )
479   {
480     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Using Compiled Shader, Size = %d\n", mProgramData->buffer.size());
481
482     CHECK_GL( mContext, mGlAbstraction.ProgramBinary(mProgramId, mContext.CachedProgramBinaryFormat(), mProgramData->buffer.data(), mProgramData->buffer.size()) );
483
484     CHECK_GL( mContext, mGlAbstraction.ValidateProgram(mProgramId) );
485
486     GLint success;
487     CHECK_GL( mContext, mGlAbstraction.GetProgramiv( mProgramId, GL_VALIDATE_STATUS, &success ) );
488
489     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "ValidateProgram Status = %d\n", success);
490
491     CHECK_GL( mContext, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
492
493     if( GL_FALSE == linked )
494     {
495       DALI_LOG_ERROR("Failed to load program binary \n");
496
497       char* szLog = NULL;
498       GLint nLength;
499       CHECK_GL( mContext, mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength) );
500       if(nLength > 0)
501       {
502         szLog = new char[ nLength ];
503         CHECK_GL( mContext, mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog ) );
504         DALI_LOG_ERROR( "Program Link Error: %s\n", szLog );
505
506         delete [] szLog;
507       }
508     }
509     else
510     {
511       mLinked = true;
512     }
513   }
514
515   // Fall back to compiling and linking the vertex and fragment sources
516   if( GL_FALSE == linked )
517   {
518     DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - Runtime compilation\n");
519     if( CompileShader( GL_VERTEX_SHADER, mVertexShaderId, mProgramData->vertexShader.c_str() ) )
520     {
521       if( CompileShader( GL_FRAGMENT_SHADER, mFragmentShaderId, mProgramData->fragmentShader.c_str() ) )
522       {
523         Link();
524
525         if( mLinked )
526         {
527           GLint  binaryLength = 0;
528           GLenum binaryFormat;
529
530           CHECK_GL( mContext, mGlAbstraction.GetProgramiv(mProgramId, GL_PROGRAM_BINARY_LENGTH_OES, &binaryLength) );
531           DALI_LOG_INFO(Debug::Filter::gShader, Debug::General, "Program::Load() - GL_PROGRAM_BINARY_LENGTH_OES: %d\n", binaryLength);
532
533           // Allocate space for the bytecode in ShaderData
534           mProgramData->AllocateBuffer(binaryLength);
535           // Copy the bytecode to ShaderData
536           CHECK_GL( mContext, mGlAbstraction.GetProgramBinary(mProgramId, binaryLength, NULL, &binaryFormat, mProgramData->buffer.data()) );
537         }
538       }
539     }
540   }
541
542   // No longer needed
543   FreeShaders();
544
545 }
546
547 void Program::Unload()
548 {
549   FreeShaders();
550
551   if( this == mContext.GetCurrentProgram() )
552   {
553     CHECK_GL( mContext, mGlAbstraction.UseProgram(0) );
554
555     mContext.SetCurrentProgram( NULL );
556   }
557
558   if (mProgramId)
559   {
560     LOG_GL( "DeleteProgram(%d)\n", mProgramId );
561     CHECK_GL( mContext, mGlAbstraction.DeleteProgram( mProgramId ) );
562     mProgramId = 0;
563   }
564
565   mLinked = false;
566
567 }
568
569 bool Program::CompileShader( GLenum shaderType, GLuint& shaderId, const char* src )
570 {
571   if (!shaderId)
572   {
573     LOG_GL( "CreateShader(%d)\n", shaderType );
574     shaderId = CHECK_GL( mContext, mGlAbstraction.CreateShader( shaderType ) );
575     LOG_GL( "AttachShader(%d,%d)\n", mProgramId, shaderId );
576     CHECK_GL( mContext, mGlAbstraction.AttachShader( mProgramId, shaderId ) );
577   }
578
579   LOG_GL( "ShaderSource(%d)\n", shaderId );
580   CHECK_GL( mContext, mGlAbstraction.ShaderSource(shaderId, 1, &src, NULL ) );
581
582   LOG_GL( "CompileShader(%d)\n", shaderId );
583   CHECK_GL( mContext, mGlAbstraction.CompileShader( shaderId ) );
584
585   GLint compiled;
586   LOG_GL( "GetShaderiv(%d)\n", shaderId );
587   CHECK_GL( mContext, mGlAbstraction.GetShaderiv( shaderId, GL_COMPILE_STATUS, &compiled ) );
588
589   if (compiled == GL_FALSE)
590   {
591     DALI_LOG_ERROR("Failed to compile shader\n");
592     LogWithLineNumbers(src);
593
594     std::vector< char > szLog;
595     GLint nLength;
596     mGlAbstraction.GetShaderiv( shaderId, GL_INFO_LOG_LENGTH, &nLength);
597     if(nLength > 0)
598     {
599       szLog.reserve( nLength );
600       mGlAbstraction.GetShaderInfoLog( shaderId, nLength, &nLength, &szLog[ 0 ] );
601       DALI_LOG_ERROR( "Shader Compiler Error: %s\n", &szLog[ 0 ] );
602     }
603
604     throw DaliException( "Shader compilation failure", &szLog[ 0 ] );
605   }
606
607   return compiled != 0;
608 }
609
610 void Program::Link()
611 {
612   LOG_GL( "LinkProgram(%d)\n", mProgramId );
613   CHECK_GL( mContext, mGlAbstraction.LinkProgram( mProgramId ) );
614
615   GLint linked;
616   LOG_GL( "GetProgramiv(%d)\n", mProgramId );
617   CHECK_GL( mContext, mGlAbstraction.GetProgramiv( mProgramId, GL_LINK_STATUS, &linked ) );
618
619   if (linked == GL_FALSE)
620   {
621     DALI_LOG_ERROR("Shader failed to link \n");
622
623     char* szLog = NULL;
624     GLint nLength;
625     mGlAbstraction.GetProgramiv( mProgramId, GL_INFO_LOG_LENGTH, &nLength);
626     if(nLength > 0)
627     {
628       szLog = new char[ nLength ];
629       mGlAbstraction.GetProgramInfoLog( mProgramId, nLength, &nLength, szLog );
630       DALI_LOG_ERROR( "Shader Link Error: %s\n", szLog );
631       delete [] szLog;
632     }
633
634     DALI_ASSERT_DEBUG(0);
635   }
636
637   mLinked = linked != GL_FALSE;
638 }
639
640 void Program::FreeShaders()
641 {
642   if (mVertexShaderId)
643   {
644     LOG_GL( "DeleteShader(%d)\n", mVertexShaderId );
645     CHECK_GL( mContext, mGlAbstraction.DetachShader( mProgramId, mVertexShaderId ) );
646     CHECK_GL( mContext, mGlAbstraction.DeleteShader( mVertexShaderId ) );
647     mVertexShaderId = 0;
648   }
649
650   if (mFragmentShaderId)
651   {
652     LOG_GL( "DeleteShader(%d)\n", mFragmentShaderId );
653     CHECK_GL( mContext, mGlAbstraction.DetachShader( mProgramId, mFragmentShaderId ) );
654     CHECK_GL( mContext, mGlAbstraction.DeleteShader( mFragmentShaderId ) );
655     mFragmentShaderId = 0;
656   }
657 }
658
659 void Program::ResetAttribsUniforms()
660 {
661   // reset attribute locations
662   for( unsigned i = 0; i < ATTRIB_TYPE_LAST; ++i )
663   {
664     mAttribLocations[ i ] = ATTRIB_UNKNOWN;
665   }
666
667   // reset all gl uniform locations
668   for( unsigned int i = 0; i < mUniformLocations.size(); ++i )
669   {
670     // reset gl program locations and names
671     mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
672   }
673
674   // reset uniform cache
675   for( int i = 0; i < MAX_UNIFORM_CACHE_SIZE; ++i )
676   {
677     // GL initializes uniforms to 0
678     mUniformCacheInt[ i ] = 0;
679     mUniformCacheFloat[ i ] = 0.0f;
680     mUniformCacheFloat4[ i ][ 0 ] = 0.0f;
681     mUniformCacheFloat4[ i ][ 1 ] = 0.0f;
682     mUniformCacheFloat4[ i ][ 2 ] = 0.0f;
683     mUniformCacheFloat4[ i ][ 3 ] = 0.0f;
684   }
685 }
686
687 } // namespace Internal
688
689 } // namespace Dali