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