Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-reflection.cpp
index 86a26b7..04247c8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
 
 namespace
 {
+#if defined(DEBUG_ENABLED)
+Debug::Filter* gGraphicsReflectionLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_GRAPHICS_REFLECTION");
+#endif
+
+struct StringSize
+{
+  const char* const mString;
+  const uint32_t    mLength;
+
+  template<uint32_t kLength>
+  constexpr StringSize(const char (&string)[kLength])
+  : mString(string),
+    mLength(kLength - 1) // remove terminating null; N.B. there should be no other null.
+  {
+  }
+
+  operator const char*() const
+  {
+    return mString;
+  }
+};
+
+bool operator==(const StringSize& lhs, const char* rhs)
+{
+  return strncmp(lhs.mString, rhs, lhs.mLength) == 0;
+}
+
+const char* const    DELIMITERS           = " \t\n";
+const char* const    DELIMITERS_INC_INDEX = " \t\n[]";
+constexpr StringSize UNIFORM{"uniform"};
+constexpr StringSize SAMPLER_PREFIX{"sampler"};
+constexpr StringSize SAMPLER_TYPES[]   = {"2D", "Cube", "ExternalOES"};
+constexpr auto       END_SAMPLER_TYPES = SAMPLER_TYPES + std::extent<decltype(SAMPLER_TYPES)>::value;
+
 Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeTypeFormat(GLenum type)
 {
   switch(type)
@@ -94,7 +128,93 @@ bool SortUniformExtraInfoByLocation(Dali::Graphics::GLES::Reflection::UniformExt
   return a.location < b.location;
 }
 
-} // namespace
+std::string GetShaderSource(Dali::Graphics::ShaderState shaderState)
+{
+  std::vector<uint8_t> data;
+  auto*                shader           = static_cast<const Dali::Graphics::GLES::Shader*>(shaderState.shader);
+  auto&                shaderCreateInfo = shader->GetCreateInfo();
+  data.resize(shaderCreateInfo.sourceSize + 1);
+  std::memcpy(&data[0], shaderCreateInfo.sourceData, shaderCreateInfo.sourceSize);
+  data[shaderCreateInfo.sourceSize] = 0;
+
+  return std::string(reinterpret_cast<char*>(&data[0]));
+}
+
+void ParseShaderSamplers(std::string shaderSource, std::vector<Dali::Graphics::UniformInfo>& uniformOpaques, int& samplerPosition, std::vector<int>& samplerPositions)
+{
+  if(!shaderSource.empty())
+  {
+    char* shaderStr = strdup(shaderSource.c_str());
+    char* uniform   = strstr(shaderStr, UNIFORM);
+
+    while(uniform)
+    {
+      // From "uniform" to ";", not ignoring comments.
+      char* outerToken = strtok_r(uniform + UNIFORM.mLength, ";", &uniform);
+
+      char* nextPtr = nullptr;
+      char* token   = strtok_r(outerToken, DELIMITERS, &nextPtr);
+      while(token)
+      {
+        // Ignore any token up to "sampler"
+        if(SAMPLER_PREFIX == token)
+        {
+          token += SAMPLER_PREFIX.mLength;
+          if(std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES)
+          {
+            bool found(false);
+            // We now are at next token after "samplerxxx" in outerToken token "stream"
+
+            // Does it use array notation?
+            int  arraySize = 0; // 0 = No array
+            auto iter      = std::string(token).find("[", 0);
+            if(iter != std::string::npos)
+            {
+              // Get Array size from source. (Warning, may be higher than GetActiveUniform suggests)
+              iter++;
+              arraySize = int(strtol(token + iter, nullptr, 0));
+            }
+
+            token = strtok_r(nullptr, DELIMITERS_INC_INDEX, &nextPtr); // " ", "\t", "\n", "[", "]"
+
+            for(uint32_t i = 0; i < static_cast<uint32_t>(uniformOpaques.size()); ++i)
+            {
+              if(samplerPositions[i] == -1 &&
+                 strncmp(token, uniformOpaques[i].name.c_str(), uniformOpaques[i].name.size()) == 0)
+              {
+                // We have found a matching name.
+                samplerPositions[i] = uniformOpaques[i].offset = samplerPosition;
+                if(arraySize == 0)
+                {
+                  ++samplerPosition;
+                }
+                else
+                {
+                  samplerPosition += arraySize;
+                }
+                found = true;
+                break;
+              }
+            }
+
+            if(!found)
+            {
+              DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Sampler uniform %s declared but not used in the shader\n", token);
+            }
+            break;
+          }
+        }
+
+        token = strtok_r(nullptr, DELIMITERS, &nextPtr);
+      }
+
+      uniform = strstr(uniform, UNIFORM);
+    }
+    free(shaderStr);
+  }
+}
+
+} // anonymous namespace
 
 namespace Dali::Graphics::GLES
 {
@@ -122,12 +242,16 @@ void Reflection::BuildVertexAttributeReflection()
     return;
   }
 
+  DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Build vertex attribute reflection for glProgram : %u\n", glProgram);
+
   gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxLength);
   gl->GetProgramiv(glProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
 
   mVertexInputAttributes.clear();
   mVertexInputAttributes.resize(nAttribs);
 
+  int maximumLocation = nAttribs - 1;
+
   name = new GLchar[maxLength];
   for(int i = 0; i < nAttribs; i++)
   {
@@ -136,13 +260,21 @@ void Reflection::BuildVertexAttributeReflection()
 
     if(location >= 0)
     {
+      if(maximumLocation < location)
+      {
+        maximumLocation = location;
+        // Increate continer size s.t. we can use maximumLocation as index.
+        mVertexInputAttributes.resize(maximumLocation + 1u);
+      }
+
       AttributeInfo attributeInfo;
-      attributeInfo.location = location;
-      attributeInfo.name     = name;
-      attributeInfo.format   = GetVertexAttributeTypeFormat(type);
-      mVertexInputAttributes.insert(mVertexInputAttributes.begin() + location, attributeInfo);
+      attributeInfo.location           = location;
+      attributeInfo.name               = name;
+      attributeInfo.format             = GetVertexAttributeTypeFormat(type);
+      mVertexInputAttributes[location] = std::move(attributeInfo);
     }
   }
+
   delete[] name;
 }
 
@@ -162,6 +294,8 @@ void Reflection::BuildUniformReflection()
     return;
   }
 
+  DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Build uniform reflection for glProgram : %u\n", glProgram);
+
   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen);
   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORMS, &numUniforms);
 
@@ -179,6 +313,7 @@ void Reflection::BuildUniformReflection()
     GLenum type;
     int    written;
     gl->GetActiveUniform(glProgram, i, maxLen, &written, &elementCount, &type, name);
+
     int location = gl->GetUniformLocation(glProgram, name);
 
     Dali::Graphics::UniformInfo uniformInfo;
@@ -186,17 +321,23 @@ void Reflection::BuildUniformReflection()
     uniformInfo.name = name;
     if(elementCount > 1)
     {
+      // If we have an active uniform that refers to an array, only the first element
+      // is present in this list, and is referenced as "uniform[0]", but the element
+      // count is non-zero to indicate how many uniforms there are in the array.
+
+      // Strip off the array, but store the element count
       auto iter = std::string(uniformInfo.name).find("[", 0);
       if(iter != std::string::npos)
       {
-        uniformInfo.name = std::string(name).substr(0, iter);
+        uniformInfo.name         = std::string(name).substr(0, iter);
+        uniformInfo.elementCount = elementCount;
       }
     }
-
     uniformInfo.uniformClass = IsSampler(type) ? Dali::Graphics::UniformClass::COMBINED_IMAGE_SAMPLER : Dali::Graphics::UniformClass::UNIFORM;
-    uniformInfo.location     = IsSampler(type) ? 0 : location;
-    uniformInfo.binding      = IsSampler(type) ? location : 0;
+    uniformInfo.location     = location; // GL doesn't guarantee that consecutive array elements have sequential locations. But, we only store location of first element.
+    uniformInfo.binding      = 0;
     uniformInfo.bufferIndex  = 0;
+    uniformInfo.offset       = 0;
 
     if(IsSampler(type))
     {
@@ -219,7 +360,7 @@ void Reflection::BuildUniformReflection()
 
   if(mUniformOpaques.size() > 1)
   {
-    std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortUniformInfoByLocation);
+    SortOpaques();
   }
 
   // Calculate the uniform offset
@@ -272,6 +413,8 @@ void Reflection::BuildUniformBlockReflection()
     return;
   }
 
+  DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::General, "Build uniform block reflection for glProgram : %u\n", glProgram);
+
   gl->GetProgramiv(glProgram, GL_ACTIVE_UNIFORM_BLOCKS, &numUniformBlocks);
 
   mUniformBlocks.clear();
@@ -327,6 +470,7 @@ void Reflection::BuildUniformBlockReflection()
 
 uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
 {
+  DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::Verbose, "name : %s\n", name.c_str());
   for(auto&& attr : mVertexInputAttributes)
   {
     if(attr.name == name)
@@ -339,6 +483,7 @@ uint32_t Reflection::GetVertexAttributeLocation(const std::string& name) const
 
 Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(uint32_t location) const
 {
+  DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::Verbose, "location : %u\n", location);
   if(location >= mVertexInputAttributes.size())
   {
     return Dali::Graphics::VertexInputAttributeFormat::UNDEFINED;
@@ -349,6 +494,7 @@ Dali::Graphics::VertexInputAttributeFormat Reflection::GetVertexAttributeFormat(
 
 std::string Reflection::GetVertexAttributeName(uint32_t location) const
 {
+  DALI_LOG_INFO(gGraphicsReflectionLogFilter, Debug::Verbose, "location : %u\n", location);
   if(location >= mVertexInputAttributes.size())
   {
     return std::string();
@@ -490,21 +636,23 @@ bool Reflection::GetNamedUniform(const std::string& name, Dali::Graphics::Unifor
         return true;
       }
     }
-    index++;
+    ++index;
   }
 
   // check samplers
+  index = 0u;
   for(auto&& uniform : mUniformOpaques)
   {
     if(uniform.name == name)
     {
       out.uniformClass = Graphics::UniformClass::COMBINED_IMAGE_SAMPLER;
-      out.binding      = uniform.binding;
+      out.binding      = 0;
       out.name         = name;
-      out.offset       = 0;
-      out.location     = uniform.location;
+      out.offset       = index;            // lexical location in shader
+      out.location     = uniform.location; // uniform location mapping
       return true;
     }
+    ++index;
   }
 
   return false;
@@ -526,7 +674,7 @@ const std::vector<Reflection::UniformExtraInfo>& Reflection::GetStandaloneUnifor
   return mStandaloneUniformExtraInfos;
 }
 
-std::vector<Dali::Graphics::UniformInfo> Reflection::GetSamplers() const
+const std::vector<Dali::Graphics::UniformInfo>& Reflection::GetSamplers() const
 {
   return mUniformOpaques;
 }
@@ -553,4 +701,34 @@ Graphics::ShaderLanguage Reflection::GetLanguage() const
   return version;
 }
 
+void Reflection::SortOpaques()
+{
+  //Determine declaration order of each sampler
+  auto& programCreateInfo = mProgram.GetCreateInfo();
+
+  std::vector<uint8_t> data;
+  std::string          vertShader;
+  std::string          fragShader;
+
+  for(auto& shaderState : *programCreateInfo.shaderState)
+  {
+    if(shaderState.pipelineStage == PipelineStage::VERTEX_SHADER)
+    {
+      vertShader = GetShaderSource(shaderState);
+    }
+    else if(shaderState.pipelineStage == PipelineStage::FRAGMENT_SHADER)
+    {
+      fragShader = GetShaderSource(shaderState);
+    }
+  }
+
+  int              samplerPosition = 0;
+  std::vector<int> samplerPositions(mUniformOpaques.size(), -1);
+
+  ParseShaderSamplers(vertShader, mUniformOpaques, samplerPosition, samplerPositions);
+  ParseShaderSamplers(fragShader, mUniformOpaques, samplerPosition, samplerPositions);
+
+  std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), [](const UniformInfo& a, const UniformInfo& b) { return a.offset < b.offset; });
+}
+
 } // namespace Dali::Graphics::GLES