Reflection GetSamplers() returns const ref
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-reflection.cpp
index 9bce79a..8493992 100644 (file)
  */
 
 #include "test-graphics-reflection.h"
+#include "test-graphics-shader.h"
 
+#include <dali/public-api/object/property-map.h>
+#include <string>
+#include <vector>
 namespace Dali
 {
-TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl)
-: mGl(gl)
+namespace
 {
+static const std::vector<UniformData> UNIFORMS =
+  {
+    UniformData("uRendererColor", Property::Type::FLOAT),
+    UniformData("uCustom", Property::Type::INTEGER),
+    UniformData("uCustom3", Property::Type::VECTOR3),
+    UniformData("uFadeColor", Property::Type::VECTOR4),
+    UniformData("uUniform1", Property::Type::VECTOR4),
+    UniformData("uUniform2", Property::Type::VECTOR4),
+    UniformData("uUniform3", Property::Type::VECTOR4),
+    UniformData("uFadeProgress", Property::Type::FLOAT),
+    UniformData("uANormalMatrix", Property::Type::MATRIX3),
+    UniformData("sEffect", Property::Type::FLOAT),
+    UniformData("sTexture", Property::Type::FLOAT),
+    UniformData("sTextureRect", Property::Type::FLOAT),
+    UniformData("sGloss", Property::Type::FLOAT),
+    UniformData("uColor", Property::Type::VECTOR4),
+    UniformData("uModelMatrix", Property::Type::MATRIX),
+    UniformData("uModelView", Property::Type::MATRIX),
+    UniformData("uMvpMatrix", Property::Type::MATRIX),
+    UniformData("uNormalMatrix", Property::Type::MATRIX3),
+    UniformData("uProjection", Property::Type::MATRIX),
+    UniformData("uSize", Property::Type::VECTOR3),
+    UniformData("uViewMatrix", Property::Type::MATRIX),
+    UniformData("uLightCameraProjectionMatrix", Property::Type::MATRIX),
+    UniformData("uLightCameraViewMatrix", Property::Type::MATRIX),
+};
+}
+
+TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, Property::Array& vfs, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms)
+: mGl(gl),
+  mCustomUniforms(customUniforms)
+{
+  for(Property::Array::SizeType i = 0; i < vfs.Count(); ++i)
+  {
+    Property::Map* vertexFormat = vfs[i].GetMap();
+    if(vertexFormat)
+    {
+      for(Property::Map::SizeType j = 0; j < vertexFormat->Count(); ++j)
+      {
+        auto key = vertexFormat->GetKeyAt(j);
+        if(key.type == Property::Key::STRING)
+        {
+          mAttributes.push_back(key.stringKey);
+        }
+      }
+    }
+  }
+
+  mDefaultUniformBlock.name          = "";
+  mDefaultUniformBlock.members       = {};
+  mDefaultUniformBlock.binding       = 0;
+  mDefaultUniformBlock.size          = 64 * (UNIFORMS.size() + mCustomUniforms.size());
+  mDefaultUniformBlock.descriptorSet = 0;
+  mDefaultUniformBlock.members.clear();
+  int loc = 0;
+  for(const auto& data : UNIFORMS)
+  {
+    mDefaultUniformBlock.members.emplace_back();
+    auto& item        = mDefaultUniformBlock.members.back();
+    item.name         = data.name;
+    item.binding      = 0;
+    item.offset       = loc * 64;
+    item.location     = loc++;
+    item.bufferIndex  = 0;
+    item.uniformClass = Graphics::UniformClass::UNIFORM;
+  }
+
+  for(const auto& data : mCustomUniforms)
+  {
+    fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
+    mDefaultUniformBlock.members.emplace_back();
+    auto& item        = mDefaultUniformBlock.members.back();
+    item.name         = data.name;
+    item.binding      = 0;
+    item.offset       = loc * 64;
+    item.location     = loc++;
+    item.bufferIndex  = 0;
+    item.uniformClass = Graphics::UniformClass::UNIFORM;
+  }
+
+  mUniformBlocks.push_back(mDefaultUniformBlock);
 }
 
 uint32_t TestGraphicsReflection::GetVertexAttributeLocation(const std::string& name) const
 {
+  // Automatically assign locations to named attributes when requested
+  auto iter = std::find(mAttributes.begin(), mAttributes.end(), name);
+  if(iter != mAttributes.end())
+  {
+    return iter - mAttributes.begin();
+  }
+  else
+  {
+    uint32_t location = mAttributes.size();
+    mAttributes.push_back(name);
+    return location;
+  }
   return 0u;
 }
 
@@ -40,12 +136,17 @@ std::string TestGraphicsReflection::GetVertexAttributeName(uint32_t location) co
 
 std::vector<uint32_t> TestGraphicsReflection::GetVertexAttributeLocations() const
 {
-  return std::vector<uint32_t>{};
+  std::vector<uint32_t> locs;
+  for(uint32_t i = 0; i < mAttributes.size(); ++i)
+  {
+    locs.push_back(i);
+  }
+  return locs;
 }
 
 uint32_t TestGraphicsReflection::GetUniformBlockCount() const
 {
-  return 0u;
+  return mUniformBlocks.size();
 }
 
 uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
@@ -55,11 +156,36 @@ uint32_t TestGraphicsReflection::GetUniformBlockBinding(uint32_t index) const
 
 uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
 {
-  return 0u;
+  // 64 bytes per uniform (64 = 4x4 matrix)
+  // TODO: fix if array will be used
+  return 64 * (UNIFORMS.size() + mCustomUniforms.size());
 }
 
 bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
 {
+  if(index >= mUniformBlocks.size())
+  {
+    return false;
+  }
+
+  const auto& block = mUniformBlocks[index];
+
+  out.name          = block.name;
+  out.binding       = block.binding;
+  out.descriptorSet = block.descriptorSet;
+  auto membersSize  = block.members.size();
+  out.members.resize(membersSize);
+  out.size = block.size;
+  for(auto i = 0u; i < out.members.size(); ++i)
+  {
+    const auto& memberUniform   = block.members[i];
+    out.members[i].name         = memberUniform.name;
+    out.members[i].binding      = block.binding;
+    out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
+    out.members[i].offset       = memberUniform.offset;
+    out.members[i].location     = memberUniform.location;
+  }
+
   return true;
 }
 
@@ -75,17 +201,38 @@ std::string TestGraphicsReflection::GetUniformBlockName(uint32_t blockIndex) con
 
 uint32_t TestGraphicsReflection::GetUniformBlockMemberCount(uint32_t blockIndex) const
 {
-  return 0u;
+  if(blockIndex < mUniformBlocks.size())
+  {
+    return static_cast<uint32_t>(mUniformBlocks[blockIndex].members.size());
+  }
+  else
+  {
+    return 0u;
+  }
 }
 
 std::string TestGraphicsReflection::GetUniformBlockMemberName(uint32_t blockIndex, uint32_t memberLocation) const
 {
-  return std::string{};
+  if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
+  {
+    return mUniformBlocks[blockIndex].members[memberLocation].name;
+  }
+  else
+  {
+    return std::string();
+  }
 }
 
 uint32_t TestGraphicsReflection::GetUniformBlockMemberOffset(uint32_t blockIndex, uint32_t memberLocation) const
 {
-  return 0u;
+  if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
+  {
+    return mUniformBlocks[blockIndex].members[memberLocation].offset;
+  }
+  else
+  {
+    return 0u;
+  }
 }
 
 bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Graphics::UniformInfo& out) const
@@ -93,9 +240,10 @@ bool TestGraphicsReflection::GetNamedUniform(const std::string& name, Dali::Grap
   return true;
 }
 
-std::vector<Dali::Graphics::UniformInfo> TestGraphicsReflection::GetSamplers() const
+const std::vector<Dali::Graphics::UniformInfo>& TestGraphicsReflection::GetSamplers() const
 {
-  return std::vector<Dali::Graphics::UniformInfo>{};
+  static std::vector<Dali::Graphics::UniformInfo> samplers{};
+  return samplers;
 }
 
 Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
@@ -103,4 +251,9 @@ Graphics::ShaderLanguage TestGraphicsReflection::GetLanguage() const
   return Graphics::ShaderLanguage::GLSL_3_1;
 }
 
+Dali::Property::Type TestGraphicsReflection::GetMemberType(int blockIndex, int location) const
+{
+  return location < static_cast<int>(UNIFORMS.size()) ? UNIFORMS[location].type : mCustomUniforms[location - UNIFORMS.size()].type;
+}
+
 } // namespace Dali