Revert "[Tizen] Add codes for Dali Windows Backend"
[platform/core/uifw/dali-core.git] / dali / internal / render / shaders / program.cpp
index e1dbe8a..451de0b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
 
 // EXTERNAL INCLUDES
 #include <iomanip>
+#include <cstring>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
@@ -92,7 +93,7 @@ const char* gStdUniforms[ Program::UNIFORM_TYPE_LAST ] =
   "uNormalMatrix",        // UNIFORM_NORMAL_MATRIX
   "uColor",               // UNIFORM_COLOR
   "sTexture",             // UNIFORM_SAMPLER
-  "uTextureRect",         // UNIFORM_SAMPLER_RECT
+  "sTextureRect",         // UNIFORM_SAMPLER_RECT
   "sEffect",              // UNIFORM_EFFECT_SAMPLER
   "uSize"                 // UNIFORM_SIZE
 };
@@ -110,8 +111,7 @@ Program* Program::New( ProgramCache& cache, Internal::ShaderDataPtr shaderData,
   {
     // program not found so create it
     program = new Program( cache, shaderData, modifiesGeometry );
-
-    // we want to lazy load programs so dont do a Load yet, it gets done in Use()
+    program->Load();
     cache.AddProgram( shaderHash, program );
   }
 
@@ -120,11 +120,6 @@ Program* Program::New( ProgramCache& cache, Internal::ShaderDataPtr shaderData,
 
 void Program::Use()
 {
-  if ( !mLinked )
-  {
-    Load();
-  }
-
   if ( mLinked )
   {
     if ( this != mCache.GetCurrentProgram() )
@@ -222,29 +217,123 @@ GLint Program::GetUniformLocation( unsigned int uniformIndex )
   return location;
 }
 
-GLint Program::GetSamplerUniformLocation( int32_t uniqueIndex, const std::string& samplerName  )
+namespace
+{
+/**
+ * This struct is used to record the position of a uniform declaration
+ * within the fragment shader source code.
+ */
+struct LocationPosition
 {
-  // don't accept negative values (should never happen)
-  DALI_ASSERT_DEBUG( 0 <= uniqueIndex );
-  const uint32_t index( uniqueIndex ); // avoid compiler warning of signed vs unsigned comparisons
+  GLint uniformLocation; ///< The location of the uniform (used as an identifier)
+  int position;          ///< the position of the uniform declaration
+  LocationPosition( GLint uniformLocation, int position )
+  : uniformLocation(uniformLocation), position(position)
+  {
+  }
+};
 
-  GLint location = UNIFORM_NOT_QUERIED;
+bool sortByPosition( LocationPosition a, LocationPosition b )
+{
+  return a.position < b.position;
+}
+}
+
+void Program::GetActiveSamplerUniforms()
+{
+  GLint numberOfActiveUniforms = -1;
+  GLint uniformMaxNameLength=-1;
+
+  mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORMS, &numberOfActiveUniforms );
+  mGlAbstraction.GetProgramiv( mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &uniformMaxNameLength );
+
+  std::vector<std::string> samplerNames;
+  char name[uniformMaxNameLength+1]; // Allow for null terminator
+  std::vector< LocationPosition >  samplerUniformLocations;
 
-  if( index < mSamplerUniformLocations.Size() )
   {
-    location = mSamplerUniformLocations[ index ];
+    int nameLength = -1;
+    int number = -1;
+    GLenum type = GL_ZERO;
+
+    for( int i=0; i<numberOfActiveUniforms; ++i )
+    {
+      mGlAbstraction.GetActiveUniform( mProgramId, static_cast< GLuint >( i ), uniformMaxNameLength,
+                                       &nameLength, &number, &type, name );
+
+      if( type == GL_SAMPLER_2D || type == GL_SAMPLER_CUBE || type == GL_SAMPLER_EXTERNAL_OES )
+      {
+        GLuint location = mGlAbstraction.GetUniformLocation( mProgramId, name );
+        samplerNames.push_back(name);
+        samplerUniformLocations.push_back(LocationPosition(location, -1));
+      }
+    }
   }
-  else
+
+  //Determine declaration order of each sampler
+  char* fragShader = strdup( mProgramData->GetFragmentShader() );
+  char* nextPtr = NULL;
+  const char* token = strtok_r( fragShader, " ;\n", &nextPtr );
+  int samplerPosition = 0;
+  while( token )
+  {
+    if( ( strncmp( token, "sampler2D", 9u )    == 0 ) ||
+        ( strncmp( token, "samplerCube", 11u ) == 0 ) ||
+        ( strncmp( token, "samplerExternalOES", 18u ) == 0 ) )
+    {
+      bool found( false );
+      token = strtok_r( NULL, " ;\n", &nextPtr );
+      for( size_t i=0; i<samplerUniformLocations.size(); ++i )
+      {
+        if( samplerUniformLocations[i].position == -1 &&
+            strncmp( token, samplerNames[i].c_str(), samplerNames[i].size() ) == 0 )
+        {
+          samplerUniformLocations[i].position = samplerPosition++;
+          found = true;
+          break;
+        }
+      }
+      if( !found )
+      {
+        DALI_LOG_ERROR("Sampler uniform %s declared but not used in the shader\n", token );
+      }
+    }
+    else
+    {
+      token = strtok_r( NULL, " ;\n", &nextPtr );
+    }
+  }
+
+  free( fragShader );
+
+  // Re-order according to declaration order in the fragment source.
+  size_t samplerUniformCount = samplerUniformLocations.size();
+  if( samplerUniformCount > 1 )
   {
-    // not in cache yet, make space and initialize value to not queried
-    mSamplerUniformLocations.Resize( index + 1, UNIFORM_NOT_QUERIED );
+    std::sort( samplerUniformLocations.begin(), samplerUniformLocations.end(), sortByPosition);
   }
-  if( location == UNIFORM_NOT_QUERIED )
+
+  mSamplerUniformLocations.resize( samplerUniformCount );
+  for( size_t i=0; i<samplerUniformCount; ++i )
   {
-    location = CHECK_GL( mGlAbstraction, mGlAbstraction.GetUniformLocation( mProgramId, samplerName.c_str() ) );
-    mSamplerUniformLocations[ index ] = location;
+    mSamplerUniformLocations[i] = samplerUniformLocations[i].uniformLocation;
   }
-  return location;
+}
+
+bool Program::GetSamplerUniformLocation( unsigned int index, GLint& location  )
+{
+  bool result = false;
+  if( index < mSamplerUniformLocations.size() )
+  {
+    location = mSamplerUniformLocations[index];
+    result = true;
+  }
+  return result;
+}
+
+size_t Program::GetActiveSamplerCount() const
+{
+  return mSamplerUniformLocations.size();
 }
 
 void Program::SetUniform1i( GLint location, GLint value0 )
@@ -612,6 +701,8 @@ void Program::Load()
     }
   }
 
+  GetActiveSamplerUniforms();
+
   // No longer needed
   FreeShaders();
 }
@@ -742,10 +833,7 @@ void Program::ResetAttribsUniformCache()
     mUniformLocations[ i ].second = UNIFORM_NOT_QUERIED;
   }
 
-  for( unsigned int i = 0; i < mSamplerUniformLocations.Size(); ++i )
-  {
-    mSamplerUniformLocations[ i ] = UNIFORM_NOT_QUERIED;
-  }
+  mSamplerUniformLocations.clear();
 
   // reset uniform caches
   mSizeUniformCache.x = mSizeUniformCache.y = mSizeUniformCache.z = 0.f;