[dali_2.3.23] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-Model.cpp
index 1b898a0..6b6670c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
 #include <dali-toolkit-test-suite-utils.h>
 #include <dali-toolkit/dali-toolkit.h>
 #include <dali/devel-api/common/map-wrapper.h>
+#include <dali/public-api/common/vector-wrapper.h>
 #include <stdlib.h>
 #include <iostream>
 
 #include <dali-toolkit/devel-api/focus-manager/keyboard-focus-manager-devel.h>
 #include <dali/integration-api/events/touch-event-integ.h>
+#include <toolkit-environment-variable.h>
 #include <toolkit-event-thread-callback.h>
 
 #include <dali-scene3d/public-api/controls/model/model.h>
@@ -32,6 +34,8 @@
 #include <dali-scene3d/public-api/model-motion/motion-index/blend-shape-index.h>
 #include <dali-scene3d/public-api/model-motion/motion-index/motion-transform-index.h>
 
+#include <dali-scene3d/public-api/loader/node-definition.h>
+
 #include <dali/devel-api/actors/camera-actor-devel.h>
 
 using namespace Dali;
@@ -57,6 +61,8 @@ const bool DEFAULT_MODEL_CHILDREN_FOCUSABLE = false;
  * Take from https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/AnimatedCube
  */
 const char* TEST_GLTF_FILE_NAME                    = TEST_RESOURCE_DIR "/AnimatedCube.gltf";
+const char* TEST_GLTF_FILE_NAME_SAME_FILE          = TEST_RESOURCE_DIR "/AnimatedCube2.gltf";
+const char* TEST_GLTF_FILE_NAME_DIFF_META_FILE     = TEST_RESOURCE_DIR "/AnimatedCube3.gltf";
 const char* TEST_GLTF_ANIMATION_TEST_FILE_NAME     = TEST_RESOURCE_DIR "/animationTest.gltf";
 const char* TEST_GLTF_EXTRAS_FILE_NAME             = TEST_RESOURCE_DIR "/AnimatedMorphCubeAnimateNonZeroFrame.gltf";
 const char* TEST_GLTF_MULTIPLE_PRIMITIVE_FILE_NAME = TEST_RESOURCE_DIR "/simpleMultiplePrimitiveTest.gltf";
@@ -69,7 +75,7 @@ const char* TEST_DLI_EXERCISE_FILE_NAME            = TEST_RESOURCE_DIR "/exercis
  * These textures are based off version of Wave engine sample
  * Take from https://github.com/WaveEngine/Samples
  *
- * Copyright (c) 2023 Wave Coorporation
+ * Copyright (c) 2024 Wave Coorporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
@@ -118,6 +124,39 @@ void        OnResourceReady(Control control)
   gResourceReadyCalled = true;
 }
 
+void ApplyAllMaterialPropertyRecursively(Scene3D::ModelNode modelNode, const std::vector<KeyValuePair>& materialPropertyValues)
+{
+  if(!modelNode)
+  {
+    return;
+  }
+
+  for(uint32_t primitiveIndex = 0u; primitiveIndex < modelNode.GetModelPrimitiveCount(); ++primitiveIndex)
+  {
+    Scene3D::ModelPrimitive primitive = modelNode.GetModelPrimitive(primitiveIndex);
+    if(primitive)
+    {
+      Scene3D::Material material = primitive.GetMaterial();
+      if(material)
+      {
+        for(const auto& keyValuePair : materialPropertyValues)
+        {
+          if(keyValuePair.first.type == Property::Key::Type::INDEX)
+          {
+            material.SetProperty(keyValuePair.first.indexKey, keyValuePair.second);
+          }
+        }
+      }
+    }
+  }
+
+  for(uint32_t childIndex = 0u; childIndex < modelNode.GetChildCount(); ++childIndex)
+  {
+    Scene3D::ModelNode childNode = Scene3D::ModelNode::DownCast(modelNode.GetChildAt(childIndex));
+    ApplyAllMaterialPropertyRecursively(childNode, materialPropertyValues);
+  }
+}
+
 } // namespace
 
 // Negative test case for a method
@@ -175,6 +214,133 @@ int UtcDaliModelNewP2(void)
   END_TEST;
 }
 
+int UtcDaliModelNewSameModelUrlCached(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliModelNew with same model");
+
+  // Set up trace debug
+  TestGlAbstraction& gl           = application.GetGlAbstraction();
+  TraceCallStack&    textureTrace = gl.GetTextureTrace();
+  textureTrace.Enable(true);
+
+  Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
+  DALI_TEST_CHECK(model);
+  Scene3D::Model model2 = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
+  DALI_TEST_CHECK(model2);
+
+  application.GetScene().Add(model);
+
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  tet_printf("Test if there is at least 1 texture.\n");
+  int expectTextureCount = textureTrace.CountMethod("GenTextures");
+  DALI_TEST_GREATER(expectTextureCount, 0, TEST_LOCATION);
+
+  application.GetScene().Add(model2);
+
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  tet_printf("Test if we reuse cached texture or not.\n");
+  int currentTextureCount = textureTrace.CountMethod("GenTextures");
+  DALI_TEST_EQUALS(currentTextureCount, expectTextureCount, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  END_TEST;
+}
+
+int UtcDaliModelNewSameResourceUrlCached01(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliModelNew with difference url but same model");
+
+  // Set up trace debug
+  TestGlAbstraction& gl           = application.GetGlAbstraction();
+  TraceCallStack&    textureTrace = gl.GetTextureTrace();
+  textureTrace.Enable(true);
+  textureTrace.EnableLogging(true);
+
+  Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
+  DALI_TEST_CHECK(model);
+  Scene3D::Model model2 = Scene3D::Model::New(TEST_GLTF_FILE_NAME_SAME_FILE); // Difference model that use same Images.
+  DALI_TEST_CHECK(model2);
+  Scene3D::Model model3 = Scene3D::Model::New(TEST_GLTF_FILE_NAME_DIFF_META_FILE); // Difference model that use same Images, but difference metadata.
+  DALI_TEST_CHECK(model3);
+
+  application.GetScene().Add(model);
+
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  tet_printf("Test if there is at least 1 texture.\n");
+  int expectTextureCount = textureTrace.CountMethod("GenTextures");
+  DALI_TEST_GREATER(expectTextureCount, 0, TEST_LOCATION);
+
+  application.GetScene().Add(model2);
+
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  tet_printf("Test if we reuse cached texture or not.\n");
+  int currentTextureCount = textureTrace.CountMethod("GenTextures");
+  DALI_TEST_EQUALS(currentTextureCount, expectTextureCount, TEST_LOCATION);
+
+  application.GetScene().Add(model3);
+
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  tet_printf("Test if we don't reuse cached texture, due to the metadata difference.\n");
+  currentTextureCount = textureTrace.CountMethod("GenTextures");
+  DALI_TEST_GREATER(currentTextureCount, expectTextureCount, TEST_LOCATION);
+
+  application.SendNotification();
+  application.Render();
+
+  END_TEST;
+}
+
+int UtcDaliModelNewSameResourceUrlCached02(void)
+{
+  /// Make we don't use mutiple thread loading for this UTC.
+  EnvironmentVariable::SetTestEnvironmentVariable("DALI_ASYNC_MANAGER_THREAD_POOL_SIZE", "1");
+  EnvironmentVariable::SetTestEnvironmentVariable("DALI_ASYNC_MANAGER_LOW_PRIORITY_THREAD_POOL_SIZE", "1");
+
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliModelNew with difference url but same model");
+
+  Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
+  DALI_TEST_CHECK(model);
+  Scene3D::Model model2 = Scene3D::Model::New(TEST_GLTF_FILE_NAME_SAME_FILE);
+  DALI_TEST_CHECK(model2);
+
+  application.GetScene().Add(model);
+  application.GetScene().Add(model2);
+
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
+  application.SendNotification();
+  application.Render();
+
+  END_TEST;
+}
+
 // Positive test case for a method
 int UtcDaliModelDownCast(void)
 {
@@ -1264,6 +1430,72 @@ int UtcDaliModelResourceReady(void)
   END_TEST;
 }
 
+int UtcDaliModelResourceReady02(void)
+{
+  tet_infoline("Test model load successfully even if shader language version is low\n");
+  ToolkitTestApplication application;
+
+  auto originalShaderVersion = application.GetGlAbstraction().GetShaderLanguageVersion();
+
+  // Change the shader language version forcely!
+  application.GetGlAbstraction().mShaderLanguageVersion = 200;
+
+  try
+  {
+    gOnRelayoutCallBackCalled = false;
+    gResourceReadyCalled      = false;
+    Scene3D::Model model      = Scene3D::Model::New(TEST_GLTF_MORPH_FILE_NAME);
+    model.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
+    model.OnRelayoutSignal().Connect(OnRelayoutCallback);
+    model.ResourceReadySignal().Connect(OnResourceReady);
+    DALI_TEST_EQUALS(model.IsResourceReady(), false, TEST_LOCATION);
+
+    // Sanity check
+    DALI_TEST_CHECK(!gOnRelayoutCallBackCalled);
+    DALI_TEST_CHECK(!gResourceReadyCalled);
+
+    application.GetScene().Add(model);
+
+    application.SendNotification();
+    application.Render();
+
+    DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+    application.SendNotification();
+    application.Render();
+
+    DALI_TEST_EQUALS(gOnRelayoutCallBackCalled, false, TEST_LOCATION);
+    DALI_TEST_EQUALS(model.IsResourceReady(), true, TEST_LOCATION);
+    DALI_TEST_EQUALS(gResourceReadyCalled, true, TEST_LOCATION);
+
+    // Change material information, for line coverage.
+    auto modelNode = model.FindChildModelNodeByName("AnimatedMorphCube");
+    DALI_TEST_CHECK(modelNode);
+    DALI_TEST_GREATER(modelNode.GetModelPrimitiveCount(), 0u, TEST_LOCATION);
+    auto modelPrimitive = modelNode.GetModelPrimitive(0u);
+    DALI_TEST_CHECK(modelPrimitive);
+    auto material = modelPrimitive.GetMaterial();
+    DALI_TEST_CHECK(material);
+
+    auto originBaseColorFactor = material.GetProperty<Dali::Vector4>(Dali::Scene3D::Material::Property::BASE_COLOR_FACTOR);
+    auto expectBaseColorFactor = Vector4(originBaseColorFactor.r + 0.05f, originBaseColorFactor.g - 0.05f, originBaseColorFactor.b, originBaseColorFactor.a);
+    material.SetProperty(Dali::Scene3D::Material::Property::BASE_COLOR_FACTOR, expectBaseColorFactor);
+
+    application.SendNotification();
+    application.Render();
+
+    DALI_TEST_EQUALS(material.GetProperty<Dali::Vector4>(Dali::Scene3D::Material::Property::BASE_COLOR_FACTOR), expectBaseColorFactor, TEST_LOCATION);
+  }
+  catch(...)
+  {
+    DALI_TEST_CHECK(false);
+  }
+
+  // Revert shader version. We should revert it even if UTC failed.
+  application.GetGlAbstraction().mShaderLanguageVersion = originalShaderVersion;
+
+  END_TEST;
+}
+
 int UtcDaliModelResourceCacheCheck(void)
 {
   ToolkitTestApplication application;
@@ -1335,7 +1567,7 @@ int UtcDaliModelResourceCacheCheck(void)
   application.SendNotification();
   application.Render();
 
-  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(1), true, TEST_LOCATION);
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(2), true, TEST_LOCATION);
   application.SendNotification();
   application.Render();
 
@@ -1687,3 +1919,175 @@ int UtcDaliModelBlendShapeMotionDataByName(void)
 
   END_TEST;
 }
+
+int UtcDaliModelMaterialUniformChange(void)
+{
+  ToolkitTestApplication application;
+
+  static std::vector<UniformData> customUniforms =
+    {
+      UniformData("uColorFactor", Property::Type::VECTOR4),
+      UniformData("uBaseColorTextureTransformAvailable", Property::Type::FLOAT),
+      UniformData(Scene3D::Loader::NodeDefinition::GetIblMaxLodUniformName().data(), Property::Type::FLOAT),
+      UniformData(Scene3D::Loader::NodeDefinition::GetIblScaleFactorUniformName().data(), Property::Type::FLOAT),
+    };
+
+  TestGraphicsController& graphics = application.GetGraphicsController();
+  graphics.AddCustomUniforms(customUniforms);
+
+  auto& gl = application.GetGlAbstraction();
+
+  Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
+
+  gResourceReadyCalled = false;
+  model.ResourceReadySignal().Connect(&OnResourceReady);
+
+  float expectIblFactor = 0.5f;
+  model.SetImageBasedLightSource(TEST_DIFFUSE_TEXTURE, TEST_SPECULAR_TEXTURE, expectIblFactor);
+  DALI_TEST_EQUALS(model.GetImageBasedLightScaleFactor(), expectIblFactor, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(gResourceReadyCalled, false, TEST_LOCATION);
+  application.GetScene().Add(model);
+
+  application.SendNotification();
+  application.Render();
+
+  // Wait 3 task. (Load 1 model + Load 2 IBL)
+  DALI_TEST_EQUALS(Test::WaitForEventThreadTrigger(3), true, TEST_LOCATION);
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS(gResourceReadyCalled, true, TEST_LOCATION);
+  DALI_TEST_EQUALS(model.GetImageBasedLightScaleFactor(), expectIblFactor, TEST_LOCATION);
+
+  // Check uniform values before change material value
+  Vector4 expectBaseColorFactor = Vector4(1.000f, 0.766f, 0.336f, 1.0f); // Defined at AnimatedCube.gltf
+  float   expectTransformValid  = 0.0f;                                  ///< Note : This value will be true when gltf have BaseColorTexture, and use KHR_texture_transform extension.
+  float   expectMaxLOD          = 5.0f;                                  ///< Note : The number of LOD what TEST_SPECULAR_TEXTURE file has is 5.
+
+  tet_printf("Check uniform value result\n");
+  DALI_TEST_EQUALS(gl.CheckUniformValue<Vector4>("uColorFactor", expectBaseColorFactor), true, TEST_LOCATION);
+  DALI_TEST_EQUALS(gl.CheckUniformValue<float>("uBaseColorTextureTransformAvailable", expectTransformValid), true, TEST_LOCATION);
+  DALI_TEST_EQUALS(gl.CheckUniformValue<float>(Scene3D::Loader::NodeDefinition::GetIblMaxLodUniformName().data(), expectMaxLOD), true, TEST_LOCATION);
+  DALI_TEST_EQUALS(gl.CheckUniformValue<float>(Scene3D::Loader::NodeDefinition::GetIblScaleFactorUniformName().data(), expectIblFactor), true, TEST_LOCATION);
+
+  // Change all materials in Model.
+  expectBaseColorFactor = Color::BLUE;
+
+  Scene3D::ModelNode rootModelNode = model.GetModelRoot();
+  DALI_TEST_CHECK(rootModelNode);
+  ApplyAllMaterialPropertyRecursively(rootModelNode, {{Dali::Scene3D::Material::Property::BASE_COLOR_FACTOR, expectBaseColorFactor}});
+
+  application.SendNotification();
+  application.Render();
+
+  tet_printf("Check whether uniform values are not changed instead what we change now\n");
+  DALI_TEST_EQUALS(gl.CheckUniformValue<Vector4>("uColorFactor", expectBaseColorFactor), true, TEST_LOCATION);
+  DALI_TEST_EQUALS(gl.CheckUniformValue<float>("uBaseColorTextureTransformAvailable", expectTransformValid), true, TEST_LOCATION);
+  DALI_TEST_EQUALS(gl.CheckUniformValue<float>(Scene3D::Loader::NodeDefinition::GetIblMaxLodUniformName().data(), expectMaxLOD), true, TEST_LOCATION);
+  DALI_TEST_EQUALS(gl.CheckUniformValue<float>(Scene3D::Loader::NodeDefinition::GetIblScaleFactorUniformName().data(), expectIblFactor), true, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliModelCastShadow(void)
+{
+  ToolkitTestApplication application;
+
+  Scene3D::Model model = Scene3D::Model::New();
+  application.GetScene().Add(model);
+
+  Scene3D::ModelNode modelNode = Scene3D::ModelNode::New();
+  model.AddModelNode(modelNode);
+
+  DALI_TEST_EQUALS(model.IsShadowCasting(), true, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowCasting(), true, TEST_LOCATION);
+
+  auto shadowCastingIndex = modelNode.GetPropertyIndex("uIsShadowCasting");
+  DALI_TEST_EQUALS(modelNode.GetProperty<int>(shadowCastingIndex), 1, TEST_LOCATION);
+
+  model.CastShadow(false);
+
+  DALI_TEST_EQUALS(model.IsShadowCasting(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowCasting(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.GetProperty<int>(shadowCastingIndex), 0, TEST_LOCATION);
+
+  Scene3D::ModelNode modelNode2 = Scene3D::ModelNode::New();
+  model.AddModelNode(modelNode2);
+
+  DALI_TEST_EQUALS(modelNode2.IsShadowCasting(), true, TEST_LOCATION);
+
+  auto shadowCastingIndex2 = modelNode2.GetPropertyIndex("uIsShadowCasting");
+  DALI_TEST_EQUALS(modelNode2.GetProperty<int>(shadowCastingIndex2), 1, TEST_LOCATION);
+
+  modelNode.CastShadow(true);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowCasting(), true, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(model.IsShadowCasting(), false, TEST_LOCATION);
+
+  model.CastShadow(false);
+
+  DALI_TEST_EQUALS(model.IsShadowCasting(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowCasting(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.GetProperty<int>(shadowCastingIndex), 0, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode2.GetProperty<int>(shadowCastingIndex2), 0, TEST_LOCATION);
+  END_TEST;
+}
+
+int UtcDaliModelReceiveShadow(void)
+{
+  ToolkitTestApplication application;
+
+  Scene3D::Model model = Scene3D::Model::New();
+  application.GetScene().Add(model);
+
+  Scene3D::ModelNode modelNode = Scene3D::ModelNode::New();
+  model.AddModelNode(modelNode);
+
+  DALI_TEST_EQUALS(model.IsShadowReceiving(), true, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowReceiving(), true, TEST_LOCATION);
+
+  auto shadowReceivingIndex = modelNode.GetPropertyIndex("uIsShadowReceiving");
+  DALI_TEST_EQUALS(modelNode.GetProperty<int>(shadowReceivingIndex), 1, TEST_LOCATION);
+
+  model.ReceiveShadow(false);
+
+  DALI_TEST_EQUALS(model.IsShadowReceiving(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowReceiving(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.GetProperty<int>(shadowReceivingIndex), 0, TEST_LOCATION);
+
+  Scene3D::ModelNode modelNode2 = Scene3D::ModelNode::New();
+  model.AddModelNode(modelNode2);
+
+  DALI_TEST_EQUALS(modelNode2.IsShadowReceiving(), true, TEST_LOCATION);
+
+  auto shadowReceivingIndex2 = modelNode2.GetPropertyIndex("uIsShadowReceiving");
+  DALI_TEST_EQUALS(modelNode2.GetProperty<int>(shadowReceivingIndex2), 1, TEST_LOCATION);
+
+  modelNode.ReceiveShadow(true);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowReceiving(), true, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(model.IsShadowReceiving(), false, TEST_LOCATION);
+
+  model.ReceiveShadow(false);
+
+  DALI_TEST_EQUALS(model.IsShadowReceiving(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.IsShadowReceiving(), false, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode.GetProperty<int>(shadowReceivingIndex), 0, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(modelNode2.GetProperty<int>(shadowReceivingIndex2), 0, TEST_LOCATION);
+  END_TEST;
+}
\ No newline at end of file