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