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