Make SceneView FBO multisampling + Sync utc harness
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / scene-view / scene-view-impl.cpp
index 593daf0..e8fb48e 100644 (file)
 #include <dali/devel-api/actors/camera-actor-devel.h>
 #include <dali/devel-api/adaptor-framework/window-devel.h>
 #include <dali/devel-api/common/stage.h>
+#include <dali/devel-api/rendering/frame-buffer-devel.h>
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/object/type-registry-helper.h>
 #include <dali/public-api/object/type-registry.h>
+#include <string_view>
 
 // INTERNAL INCLUDES
 #include <dali-scene3d/internal/controls/model/model-impl.h>
+#include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
 
-#include <dali/integration-api/debug.h>
-
 using namespace Dali;
 
 namespace Dali
@@ -60,11 +61,102 @@ DALI_TYPE_REGISTRATION_END()
 Property::Index   RENDERING_BUFFER    = Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX + 1;
 constexpr int32_t DEFAULT_ORIENTATION = 0;
 
+constexpr uint8_t DEFAULT_FRAME_BUFFER_MULTI_SAMPLING_LEVEL = 4u;
+
+static constexpr std::string_view SKYBOX_INTENSITY_STRING = "uIntensity";
+
+Dali::Actor CreateSkybox(const std::string& skyboxUrl)
+{
+  struct Vertex
+  {
+    Vector3 aPosition;
+  };
+
+  Vertex skyboxVertices[] = {
+    // back
+    {Vector3(-1.0f, 1.0f, -1.0f)},
+    {Vector3(-1.0f, -1.0f, -1.0f)},
+    {Vector3(1.0f, -1.0f, -1.0f)},
+    {Vector3(1.0f, -1.0f, -1.0f)},
+    {Vector3(1.0f, 1.0f, -1.0f)},
+    {Vector3(-1.0f, 1.0f, -1.0f)},
+
+    // left
+    {Vector3(-1.0f, -1.0f, 1.0f)},
+    {Vector3(-1.0f, -1.0f, -1.0f)},
+    {Vector3(-1.0f, 1.0f, -1.0f)},
+    {Vector3(-1.0f, 1.0f, -1.0f)},
+    {Vector3(-1.0f, 1.0f, 1.0f)},
+    {Vector3(-1.0f, -1.0f, 1.0f)},
+
+    // right
+    {Vector3(1.0f, -1.0f, -1.0f)},
+    {Vector3(1.0f, -1.0f, 1.0f)},
+    {Vector3(1.0f, 1.0f, 1.0f)},
+    {Vector3(1.0f, 1.0f, 1.0f)},
+    {Vector3(1.0f, 1.0f, -1.0f)},
+    {Vector3(1.0f, -1.0f, -1.0f)},
+
+    // front
+    {Vector3(-1.0f, -1.0f, 1.0f)},
+    {Vector3(-1.0f, 1.0f, 1.0f)},
+    {Vector3(1.0f, 1.0f, 1.0f)},
+    {Vector3(1.0f, 1.0f, 1.0f)},
+    {Vector3(1.0f, -1.0f, 1.0f)},
+    {Vector3(-1.0f, -1.0f, 1.0f)},
+
+    // botton
+    {Vector3(-1.0f, 1.0f, -1.0f)},
+    {Vector3(1.0f, 1.0f, -1.0f)},
+    {Vector3(1.0f, 1.0f, 1.0f)},
+    {Vector3(1.0f, 1.0f, 1.0f)},
+    {Vector3(-1.0f, 1.0f, 1.0f)},
+    {Vector3(-1.0f, 1.0f, -1.0f)},
+
+    // top
+    {Vector3(-1.0f, -1.0f, -1.0f)},
+    {Vector3(-1.0f, -1.0f, 1.0f)},
+    {Vector3(1.0f, -1.0f, -1.0f)},
+    {Vector3(1.0f, -1.0f, -1.0f)},
+    {Vector3(-1.0f, -1.0f, 1.0f)},
+    {Vector3(1.0f, -1.0f, 1.0f)}};
+
+  Dali::Shader       shaderSkybox = Shader::New(SHADER_SKYBOX_SHADER_VERT.data(), SHADER_SKYBOX_SHADER_FRAG.data());
+  Dali::VertexBuffer vertexBuffer = Dali::VertexBuffer::New(Property::Map().Add("aPosition", Property::VECTOR3));
+  vertexBuffer.SetData(skyboxVertices, sizeof(skyboxVertices) / sizeof(Vertex));
+
+  Dali::Geometry skyboxGeometry = Geometry::New();
+  skyboxGeometry.AddVertexBuffer(vertexBuffer);
+  skyboxGeometry.SetType(Geometry::TRIANGLES);
+
+  Dali::Texture    skyboxTexture  = Dali::Scene3D::Loader::LoadCubeMap(skyboxUrl);
+  Dali::TextureSet skyboxTextures = TextureSet::New();
+  skyboxTextures.SetTexture(0, skyboxTexture);
+
+  Dali::Renderer skyboxRenderer = Renderer::New(skyboxGeometry, shaderSkybox);
+  skyboxRenderer.SetTextures(skyboxTextures);
+  skyboxRenderer.SetProperty(Renderer::Property::DEPTH_INDEX, 2.0f);
+  // Enables the depth test.
+  skyboxRenderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE, DepthTestMode::ON);
+  // The fragment shader will run only is those pixels that have the max depth value.
+  skyboxRenderer.SetProperty(Renderer::Property::DEPTH_FUNCTION, DepthFunction::LESS_EQUAL);
+
+  Dali::Actor skyboxActor = Actor::New();
+  skyboxActor.SetProperty(Dali::Actor::Property::NAME, "SkyBox");
+  skyboxActor.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
+  skyboxActor.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
+  skyboxActor.AddRenderer(skyboxRenderer);
+  return skyboxActor;
+}
+
 } // anonymous namespace
 
 SceneView::SceneView()
 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
-  mWindowOrientation(DEFAULT_ORIENTATION)
+  mWindowOrientation(DEFAULT_ORIENTATION),
+  mSkybox(),
+  mSkyboxOrientation(Quaternion()),
+  mSkyboxIntensity(1.0f)
 {
 }
 
@@ -166,24 +258,24 @@ void SceneView::SelectCamera(const std::string& name)
   UpdateCamera(GetCamera(name));
 }
 
-void SceneView::RegisterModel(Scene3D::Model model)
+void SceneView::RegisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
 {
-  if(model)
+  if(item)
   {
-    model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
-    mModels.push_back(model);
+    item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
+    mItems.push_back(item);
   }
 }
 
-void SceneView::UnregisterModel(Scene3D::Model model)
+void SceneView::UnregisterSceneItem(Scene3D::Internal::ImageBasedLightObserver* item)
 {
-  if(model)
+  if(item)
   {
-    for(uint32_t i = 0; i < mModels.size(); ++i)
+    for(uint32_t i = 0; i < mItems.size(); ++i)
     {
-      if(mModels[i] == model)
+      if(mItems[i] == item)
       {
-        mModels.erase(mModels.begin() + i);
+        mItems.erase(mItems.begin() + i);
         break;
       }
     }
@@ -192,38 +284,37 @@ void SceneView::UnregisterModel(Scene3D::Model model)
 
 void SceneView::SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor)
 {
-  mIBLResourceReady      = false;
-  Texture diffuseTexture = Dali::Scene3D::Loader::LoadCubeMap(diffuseUrl);
-  if(diffuseTexture)
+  mIBLResourceReady = false;
+
+  // If url is empty or invalid, reset IBL.
+  mDiffuseTexture  = (!diffuseUrl.empty()) ? Dali::Scene3D::Loader::LoadCubeMap(diffuseUrl) : Texture();
+  mSpecularTexture = (!specularUrl.empty()) ? Dali::Scene3D::Loader::LoadCubeMap(specularUrl) : Texture();
+
+  mIblScaleFactor = scaleFactor;
+
+  for(auto&& item : mItems)
   {
-    Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(specularUrl);
-    if(specularTexture)
+    if(item)
     {
-      mDiffuseTexture  = diffuseTexture;
-      mSpecularTexture = specularTexture;
-      mIblScaleFactor  = scaleFactor;
-
-      for(auto&& model : mModels)
-      {
-        if(model)
-        {
-          model.SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
-        }
-      }
+      item->NotifyImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor);
     }
   }
+
   mIBLResourceReady = true;
-  Control::SetResourceReady(false);
+  if(IsResourceReady())
+  {
+    Control::SetResourceReady(false);
+  }
 }
 
 void SceneView::SetImageBasedLightScaleFactor(float scaleFactor)
 {
   mIblScaleFactor = scaleFactor;
-  for(auto&& model : mModels)
+  for(auto&& item : mItems)
   {
-    if(model)
+    if(item)
     {
-      model.SetImageBasedLightScaleFactor(scaleFactor);
+      item->NotifyImageBasedLightScaleFactor(scaleFactor);
     }
   }
 }
@@ -247,6 +338,63 @@ bool SceneView::IsUsingFramebuffer() const
   return mUseFrameBuffer;
 }
 
+void SceneView::SetSkybox(const std::string& skyboxUrl)
+{
+  mSkyboxResourceReady = false;
+  if(mSkybox)
+  {
+    mSkybox.Unparent();
+    mSkybox.Reset();
+  }
+  mSkybox = CreateSkybox(skyboxUrl);
+  SetSkyboxIntensity(mSkyboxIntensity);
+  SetSkyboxOrientation(mSkyboxOrientation);
+  if(mRootLayer)
+  {
+    mRootLayer.Add(mSkybox);
+  }
+
+  mSkyboxResourceReady = true;
+  if(IsResourceReady())
+  {
+    Control::SetResourceReady(false);
+  }
+}
+
+void SceneView::SetSkyboxIntensity(float intensity)
+{
+  mSkyboxIntensity = intensity;
+  if(intensity < 0)
+  {
+    DALI_LOG_ERROR("Intensity should be greater than or equal to 0.\n");
+    mSkyboxIntensity = 0.0f;
+  }
+
+  if(mSkybox)
+  {
+    mSkybox.RegisterProperty(SKYBOX_INTENSITY_STRING.data(), mSkyboxIntensity);
+  }
+}
+
+float SceneView::GetSkyboxIntensity() const
+{
+  return mSkyboxIntensity;
+}
+
+void SceneView::SetSkyboxOrientation(const Quaternion& orientation)
+{
+  mSkyboxOrientation = orientation;
+  if(mSkybox)
+  {
+    mSkybox.SetProperty(Dali::Actor::Property::ORIENTATION, orientation);
+  }
+}
+
+Quaternion SceneView::GetSkyboxOrientation() const
+{
+  return mSkyboxOrientation;
+}
+
 ///////////////////////////////////////////////////////////
 //
 // Private methods
@@ -254,12 +402,20 @@ bool SceneView::IsUsingFramebuffer() const
 
 void SceneView::OnSceneConnection(int depth)
 {
-  UpdateRenderTask();
-
   Window window = DevelWindow::Get(Self());
   if(window)
   {
     window.ResizeSignal().Connect(this, &SceneView::OnWindowResized);
+    RenderTaskList taskList = window.GetRenderTaskList();
+    mRenderTask             = taskList.CreateTask();
+    mRenderTask.SetSourceActor(mRootLayer);
+    mRenderTask.SetExclusive(true);
+    mRenderTask.SetInputEnabled(true);
+    mRenderTask.SetCullMode(false);
+    mRenderTask.SetScreenToFrameBufferMappingActor(Self());
+
+    UpdateRenderTask();
+    mWindow = window;
   }
 
   Control::OnSceneConnection(depth);
@@ -267,13 +423,20 @@ void SceneView::OnSceneConnection(int depth)
 
 void SceneView::OnSceneDisconnection()
 {
-  mModels.clear();
+  mItems.clear();
 
-  Window window = DevelWindow::Get(Self());
+  Window window = mWindow.GetHandle();
   if(window)
   {
     window.ResizeSignal().Disconnect(this, &SceneView::OnWindowResized);
+    RenderTaskList taskList = window.GetRenderTaskList();
+    if(mRenderTask)
+    {
+      taskList.RemoveTask(mRenderTask);
+      mFrameBuffer.Reset();
+    }
   }
+  mWindow.Reset();
 
   Control::OnSceneDisconnection();
 }
@@ -290,14 +453,6 @@ void SceneView::OnInitialize()
   mRootLayer.SetProperty(Dali::Actor::Property::INHERIT_SCALE, false);
   self.Add(mRootLayer);
 
-  RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
-  mRenderTask             = taskList.CreateTask();
-  mRenderTask.SetSourceActor(mRootLayer);
-  mRenderTask.SetExclusive(true);
-  mRenderTask.SetInputEnabled(true);
-  mRenderTask.SetCullMode(false);
-  mRenderTask.SetScreenToFrameBufferMappingActor(Self());
-
   mDefaultCamera = Dali::CameraActor::New();
   mDefaultCamera.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
   mDefaultCamera.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
@@ -344,7 +499,7 @@ void SceneView::OnRelayout(const Vector2& size, RelayoutContainer& container)
 
 bool SceneView::IsResourceReady() const
 {
-  return mIBLResourceReady;
+  return mIBLResourceReady & mSkyboxResourceReady;
 }
 
 void SceneView::UpdateCamera(CameraActor camera)
@@ -374,18 +529,6 @@ void SceneView::UpdateRenderTask()
     Vector3     size        = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
     const float aspectRatio = size.width / size.height;
     mSelectedCamera.SetAspectRatio(aspectRatio);
-    const bool projectionVertical = mSelectedCamera.GetProperty<int>(Dali::DevelCameraActor::Property::PROJECTION_DIRECTION) == Dali::DevelCameraActor::VERTICAL;
-
-    // if projectionVertical, Top / Bottom is +-ve to keep consistency with orthographic values
-    // else, Left / Right is +-ve to keep consistency with orthographic values
-    const float orthographicSize = DALI_LIKELY(projectionVertical) ? mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE] : mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE];
-    const float halfHeight       = DALI_LIKELY(projectionVertical) ? orthographicSize : orthographicSize / aspectRatio;
-    const float halfWidth        = DALI_LIKELY(projectionVertical) ? orthographicSize * aspectRatio : orthographicSize;
-
-    mSelectedCamera[Dali::CameraActor::Property::LEFT_PLANE_DISTANCE]   = -halfWidth;
-    mSelectedCamera[Dali::CameraActor::Property::RIGHT_PLANE_DISTANCE]  = halfWidth;
-    mSelectedCamera[Dali::CameraActor::Property::TOP_PLANE_DISTANCE]    = halfHeight;
-    mSelectedCamera[Dali::CameraActor::Property::BOTTOM_PLANE_DISTANCE] = -halfHeight;
 
     if(mUseFrameBuffer)
     {
@@ -398,10 +541,11 @@ void SceneView::UpdateRenderTask()
         mRenderTask.SetViewport(Dali::Viewport(Vector4::ZERO));
 
         // create offscreen buffer of new size to render our child actors to
-        mTexture      = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
-        mRenderTarget = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
-        mRenderTarget.AttachColorTexture(mTexture);
-        Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mRenderTarget, 0u);
+        mTexture     = Dali::Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, unsigned(size.width), unsigned(size.height));
+        mFrameBuffer = FrameBuffer::New(size.width, size.height, FrameBuffer::Attachment::DEPTH_STENCIL);
+        mFrameBuffer.AttachColorTexture(mTexture);
+        DevelFrameBuffer::SetMultiSamplingLevel(mFrameBuffer, DEFAULT_FRAME_BUFFER_MULTI_SAMPLING_LEVEL);
+        Dali::Toolkit::ImageUrl imageUrl = Dali::Toolkit::Image::GenerateUrl(mFrameBuffer, 0u);
 
         Property::Map imagePropertyMap;
         imagePropertyMap.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::IMAGE);
@@ -412,7 +556,7 @@ void SceneView::UpdateRenderTask()
 
         Toolkit::DevelControl::RegisterVisual(*this, RENDERING_BUFFER, mVisual);
 
-        mRenderTask.SetFrameBuffer(mRenderTarget);
+        mRenderTask.SetFrameBuffer(mFrameBuffer);
         mRenderTask.SetClearEnabled(true);
         mRenderTask.SetClearColor(Color::TRANSPARENT);
       }
@@ -429,7 +573,7 @@ void SceneView::UpdateRenderTask()
         Toolkit::DevelControl::UnregisterVisual(*this, RENDERING_BUFFER);
 
         mVisual.Reset();
-        mRenderTarget.Reset();
+        mFrameBuffer.Reset();
         mTexture.Reset();
       }
     }