Reflection GetSamplers() returns const ref
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-reflection.cpp
index 86a26b7..05a803d 100644 (file)
@@ -94,7 +94,36 @@ bool SortUniformExtraInfoByLocation(Dali::Graphics::GLES::Reflection::UniformExt
   return a.location < b.location;
 }
 
-} // namespace
+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";
+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;
+
+} // anonymous namespace
 
 namespace Dali::Graphics::GLES
 {
@@ -194,9 +223,10 @@ void Reflection::BuildUniformReflection()
     }
 
     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; //IsSampler(type) ? 0 : location;
+    uniformInfo.binding      = 0;        // IsSampler(type) ? location : 0;
     uniformInfo.bufferIndex  = 0;
+    uniformInfo.offset       = 0;
 
     if(IsSampler(type))
     {
@@ -219,7 +249,7 @@ void Reflection::BuildUniformReflection()
 
   if(mUniformOpaques.size() > 1)
   {
-    std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), SortUniformInfoByLocation);
+    SortOpaques();
   }
 
   // Calculate the uniform offset
@@ -490,21 +520,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 +558,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 +585,78 @@ 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          fragShader;
+
+  for(auto& shaderState : *programCreateInfo.shaderState)
+  {
+    if(shaderState.pipelineStage == PipelineStage::FRAGMENT_SHADER)
+    {
+      auto* shader           = static_cast<const 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;
+      fragShader                        = std::string(reinterpret_cast<char*>(&data[0]));
+      break;
+    }
+  }
+
+  if(!fragShader.empty())
+  {
+    char*            shaderStr       = strdup(fragShader.c_str());
+    char*            uniform         = strstr(shaderStr, UNIFORM);
+    int              samplerPosition = 0;
+    std::vector<int> samplerPositions(mUniformOpaques.size(), -1);
+
+    while(uniform)
+    {
+      char* outerToken = strtok_r(uniform + UNIFORM.mLength, ";", &uniform);
+
+      char* nextPtr = nullptr;
+      char* token   = strtok_r(outerToken, DELIMITERS, &nextPtr);
+      while(token)
+      {
+        if(SAMPLER_PREFIX == token)
+        {
+          token += SAMPLER_PREFIX.mLength;
+          if(std::find(SAMPLER_TYPES, END_SAMPLER_TYPES, token) != END_SAMPLER_TYPES)
+          {
+            bool found(false);
+            token = strtok_r(nullptr, DELIMITERS, &nextPtr);
+
+            for(uint32_t i = 0; i < static_cast<uint32_t>(mUniformOpaques.size()); ++i)
+            {
+              if(samplerPositions[i] == -1 &&
+                 strncmp(token, mUniformOpaques[i].name.c_str(), mUniformOpaques[i].name.size()) == 0)
+              {
+                samplerPositions[i] = mUniformOpaques[i].offset = samplerPosition++;
+                found                                           = true;
+                break;
+              }
+            }
+
+            if(!found)
+            {
+              DALI_LOG_ERROR("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);
+  }
+  std::sort(mUniformOpaques.begin(), mUniformOpaques.end(), [](const UniformInfo& a, const UniformInfo& b) { return a.offset < b.offset; });
+}
+
 } // namespace Dali::Graphics::GLES