Get world scale more faster
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / render-instruction-processor.cpp
index f3c0440..87c3c98 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 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.
@@ -29,6 +29,7 @@
 #include <dali/internal/render/renderers/render-renderer.h>
 #include <dali/internal/render/shaders/render-shader.h>
 #include <dali/internal/update/manager/sorted-layers.h>
+#include <dali/internal/update/nodes/partial-rendering-data.h>
 #include <dali/internal/update/nodes/scene-graph-layer.h>
 #include <dali/internal/update/render-tasks/scene-graph-render-task.h>
 #include <dali/internal/update/rendering/scene-graph-texture-set.h>
@@ -198,14 +199,13 @@ inline void AddRendererToRenderList(BufferIndex               updateBufferIndex,
   Vector3 nodeSize;
   Vector4 nodeUpdateArea;
   bool    nodeUpdateAreaSet(false);
-  bool    nodeUpdateAreaUseHint(false);
   Matrix  nodeModelViewMatrix(false);
   bool    nodeModelViewMatrixSet(false);
 
   // Don't cull items which have render callback
   bool hasRenderCallback = (renderable.mRenderer && renderable.mRenderer->GetRenderCallback());
 
-  if(cull && renderable.mRenderer && (hasRenderCallback || (!renderable.mRenderer->GetShader().HintEnabled(Dali::Shader::Hint::MODIFIES_GEOMETRY) && node->GetClippingMode() == ClippingMode::DISABLED)))
+  if(cull && renderable.mRenderer && !hasRenderCallback && !renderable.mRenderer->GetShader().HintEnabled(Dali::Shader::Hint::MODIFIES_GEOMETRY) && node->GetClippingMode() == ClippingMode::DISABLED)
   {
     const Vector4& boundingSphere = node->GetBoundingSphere();
     inside                        = (boundingSphere.w > Math::MACHINE_EPSILON_1000) &&
@@ -213,23 +213,27 @@ inline void AddRendererToRenderList(BufferIndex               updateBufferIndex,
 
     if(inside && !isLayer3d && viewportSet)
     {
-      nodeUpdateAreaUseHint = SetNodeUpdateArea(node, isLayer3d, nodeWorldMatrix, nodeSize, nodeUpdateArea);
-      nodeUpdateAreaSet     = true;
+      SetNodeUpdateArea(node, isLayer3d, nodeWorldMatrix, nodeSize, nodeUpdateArea);
+      nodeUpdateAreaSet = true;
 
-      const Vector3& scale = node->GetWorldScale(updateBufferIndex);
+      const Vector3& scale = nodeWorldMatrix.GetScale();
       const Vector3& size  = Vector3(nodeUpdateArea.z, nodeUpdateArea.w, 1.0f) * scale;
 
       if(size.LengthSquared() > Math::MACHINE_EPSILON_1000)
       {
-        MatrixUtils::Multiply(nodeModelViewMatrix, nodeWorldMatrix, viewMatrix);
+        MatrixUtils::MultiplyTransformMatrix(nodeModelViewMatrix, nodeWorldMatrix, viewMatrix);
         nodeModelViewMatrixSet = true;
 
         // Assume actors are at z=0, compute AABB in view space & test rect intersection
         // against z=0 plane boundaries for frustum. (NOT viewport). This should take into account
         // magnification due to FOV etc.
+
+        // TODO : Below logic might need to refactor it.
+        // If camera is Perspective, we need to calculate clipping box by FoV. Currently, we just believe default camera setup OrthographicSize well.
+        //  - If then, It must use math calculate like tan(fov) internally. So, we might need calculate it only one times, and cache.
         ClippingBox boundingBox = RenderItem::CalculateTransformSpaceAABB(nodeModelViewMatrix, Vector3(nodeUpdateArea.x, nodeUpdateArea.y, 0.0f), Vector3(nodeUpdateArea.z, nodeUpdateArea.w, 0.0f));
-        ClippingBox clippingBox(camera.mLeftClippingPlane, camera.mBottomClippingPlane, camera.mRightClippingPlane - camera.mLeftClippingPlane, fabsf(camera.mBottomClippingPlane - camera.mTopClippingPlane));
-        inside = clippingBox.Intersects(boundingBox);
+        ClippingBox clippingBox = camera.GetOrthographicClippingBox(updateBufferIndex);
+        inside                  = clippingBox.Intersects(boundingBox);
       }
     }
     /*
@@ -256,87 +260,54 @@ inline void AddRendererToRenderList(BufferIndex               updateBufferIndex,
       // Get the next free RenderItem.
       RenderItem& item = renderList.GetNextFreeItem();
 
-      // Get cached values
-      auto& partialRenderingData = node->GetPartialRenderingData();
-
-      auto& partialRenderingCacheInfo = node->GetPartialRenderingData().GetCurrentCacheInfo();
-
-      partialRenderingCacheInfo.node       = node;
-      partialRenderingCacheInfo.isOpaque   = isOpaque;
-      partialRenderingCacheInfo.renderer   = renderable.mRenderer;
-      partialRenderingCacheInfo.color      = node->GetWorldColor(updateBufferIndex);
-      partialRenderingCacheInfo.depthIndex = node->GetDepthIndex();
+      item.mNode       = node;
+      item.mIsOpaque   = isOpaque;
+      item.mDepthIndex = isLayer3d ? 0 : node->GetDepthIndex();
 
       if(DALI_LIKELY(renderable.mRenderer))
       {
-        partialRenderingCacheInfo.color.a *= renderable.mRenderer->GetOpacity(updateBufferIndex);
-        partialRenderingCacheInfo.textureSet = renderable.mRenderer->GetTextureSet();
-      }
-
-      item.mNode     = node;
-      item.mIsOpaque = isOpaque;
-      item.mColor    = node->GetColor(updateBufferIndex);
-
-      item.mDepthIndex = 0;
-      if(!isLayer3d)
-      {
-        item.mDepthIndex = node->GetDepthIndex();
-      }
-
-      if(DALI_LIKELY(renderable.mRenderer))
-      {
-        item.mRenderer   = &renderable.mRenderer->GetRenderer();
+        item.mRenderer   = renderable.mRenderer->GetRenderer();
         item.mTextureSet = renderable.mRenderer->GetTextureSet();
         item.mDepthIndex += renderable.mRenderer->GetDepthIndex();
-
-        // Get whether collected map is up to date
-        item.mIsUpdated |= renderable.mRenderer->UniformMapUpdated();
       }
       else
       {
-        item.mRenderer = nullptr;
+        item.mRenderer = Render::RendererKey{};
       }
 
       item.mIsUpdated |= isLayer3d;
 
       if(!nodeUpdateAreaSet)
       {
-        nodeUpdateAreaUseHint = SetNodeUpdateArea(node, isLayer3d, nodeWorldMatrix, nodeSize, nodeUpdateArea);
+        SetNodeUpdateArea(node, isLayer3d, nodeWorldMatrix, nodeSize, nodeUpdateArea);
       }
 
       item.mSize        = nodeSize;
       item.mUpdateArea  = nodeUpdateArea;
       item.mModelMatrix = nodeWorldMatrix;
 
-      // Apply transform informations if node doesn't have update size hint and use VisualRenderer.
-      if(!nodeUpdateAreaUseHint && renderable.mRenderer && renderable.mRenderer->GetVisualProperties())
-      {
-        Vector3 updateSize = renderable.mRenderer->CalculateVisualTransformedUpdateSize(updateBufferIndex, Vector3(item.mUpdateArea.z, item.mUpdateArea.w, 0.0f));
-        item.mUpdateArea.z = updateSize.x;
-        item.mUpdateArea.w = updateSize.y;
-      }
-
       if(!nodeModelViewMatrixSet)
       {
-        MatrixUtils::Multiply(nodeModelViewMatrix, nodeWorldMatrix, viewMatrix);
+        MatrixUtils::MultiplyTransformMatrix(nodeModelViewMatrix, nodeWorldMatrix, viewMatrix);
       }
       item.mModelViewMatrix = nodeModelViewMatrix;
 
-      partialRenderingCacheInfo.matrix              = item.mModelViewMatrix;
-      partialRenderingCacheInfo.size                = item.mSize;
-      partialRenderingCacheInfo.updatedPositionSize = item.mUpdateArea;
+      PartialRenderingData partialRenderingData;
+      partialRenderingData.color               = node->GetWorldColor(updateBufferIndex);
+      partialRenderingData.matrix              = item.mModelViewMatrix;
+      partialRenderingData.updatedPositionSize = item.mUpdateArea;
+      partialRenderingData.size                = item.mSize;
 
-      item.mIsUpdated = partialRenderingData.IsUpdated() || item.mIsUpdated;
+      auto& nodePartialRenderingData = node->GetPartialRenderingData();
+      item.mIsUpdated                = nodePartialRenderingData.IsUpdated(partialRenderingData) || item.mIsUpdated;
 
-      partialRenderingData.mRendered = true;
-
-      partialRenderingData.SwapBuffers();
+      nodePartialRenderingData.Update(partialRenderingData);
     }
     else
     {
       // Mark as not rendered
-      auto& partialRenderingData     = node->GetPartialRenderingData();
-      partialRenderingData.mRendered = false;
+      auto& nodePartialRenderingData     = node->GetPartialRenderingData();
+      nodePartialRenderingData.mRendered = false;
     }
 
     node->SetCulled(updateBufferIndex, false);
@@ -344,8 +315,8 @@ inline void AddRendererToRenderList(BufferIndex               updateBufferIndex,
   else
   {
     // Mark as not rendered
-    auto& partialRenderingData     = node->GetPartialRenderingData();
-    partialRenderingData.mRendered = false;
+    auto& nodePartialRenderingData     = node->GetPartialRenderingData();
+    nodePartialRenderingData.mRendered = false;
 
     node->SetCulled(updateBufferIndex, true);
   }
@@ -411,16 +382,17 @@ inline bool TryReuseCachedRenderers(Layer&               layer,
     // Therefore we check a combined sum of all renderer addresses.
     size_t checkSumNew = 0;
     size_t checkSumOld = 0;
+    //@todo just use keys, don't deref.
     for(uint32_t index = 0; index < renderableCount; ++index)
     {
       if(DALI_LIKELY(renderables[index].mRenderer))
       {
-        const Render::Renderer& renderer = renderables[index].mRenderer->GetRenderer();
-        checkSumNew += reinterpret_cast<std::size_t>(&renderer);
+        Render::RendererKey renderer = renderables[index].mRenderer->GetRenderer();
+        checkSumNew += renderer.Value();
       }
       if(DALI_LIKELY(renderList.GetItem(index).mRenderer))
       {
-        checkSumOld += reinterpret_cast<std::size_t>(&renderList.GetRenderer(index));
+        checkSumOld += renderList.GetItem(index).mRenderer.Value();
       }
     }
     if(checkSumNew == checkSumOld)
@@ -463,7 +435,7 @@ RenderInstructionProcessor::RenderInstructionProcessor()
 
 RenderInstructionProcessor::~RenderInstructionProcessor() = default;
 
-inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex, RenderList& renderList, Layer& layer, bool respectClippingOrder)
+inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex, RenderList& renderList, Layer& layer, bool respectClippingOrder, bool isOrthographicCamera)
 {
   const uint32_t renderableCount = static_cast<uint32_t>(renderList.Count());
   // Reserve space if needed.
@@ -484,47 +456,40 @@ inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex,
 
   // Calculate the sorting value, once per item by calling the layers sort function.
   // Using an if and two for-loops rather than if inside for as its better for branch prediction.
-  if(layer.UsesDefaultSortFunction())
-  {
-    for(uint32_t index = 0; index < renderableCount; ++index)
-    {
-      RenderItem& item = renderList.GetItem(index);
 
-      if(DALI_LIKELY(item.mRenderer))
-      {
-        item.mRenderer->SetSortAttributes(mSortingHelper[index]);
-      }
+  // List of zValue calculating functions.
+  const Dali::Layer::SortFunctionType zValueFunctionFromVector3[] = {
+    [](const Vector3& position) { return position.z; },
+    [](const Vector3& position) { return position.LengthSquared(); },
+    layer.GetSortFunction(),
+  };
 
-      // texture set
-      mSortingHelper[index].textureSet = item.mTextureSet;
+  // Determine whether we need to use zValue as Euclidean distance or translatoin's z value.
+  // If layer is LAYER_UI or camera is OrthographicProjection mode, we don't need to calculate
+  // renderItem's distance from camera.
 
-      // The default sorting function should get inlined here.
-      mSortingHelper[index].zValue = Internal::Layer::ZValue(item.mModelViewMatrix.GetTranslation3()) - static_cast<float>(item.mDepthIndex);
+  // Here we determine which zValue SortFunctionType (of the 3) to use.
+  //   0 is position z value : Default LAYER_UI or Orthographic camera
+  //   1 is distance square value : Default LAYER_3D and Perspective camera
+  //   2 is user defined function.
+  const int zValueFunctionIndex = layer.UsesDefaultSortFunction() ? ((layer.GetBehavior() == Dali::Layer::LAYER_UI || isOrthographicCamera) ? 0 : 1) : 2;
 
-      // Keep the renderitem pointer in the helper so we can quickly reorder items after sort.
-      mSortingHelper[index].renderItem = &item;
-    }
-  }
-  else
+  for(uint32_t index = 0; index < renderableCount; ++index)
   {
-    const Dali::Layer::SortFunctionType sortFunction = layer.GetSortFunction();
-    for(uint32_t index = 0; index < renderableCount; ++index)
+    RenderItemKey itemKey = renderList.GetItemKey(index);
+    RenderItem&   item    = *itemKey.Get();
+    if(DALI_LIKELY(item.mRenderer))
     {
-      RenderItem& item = renderList.GetItem(index);
-
-      if(DALI_LIKELY(item.mRenderer))
-      {
-        item.mRenderer->SetSortAttributes(mSortingHelper[index]);
-      }
+      item.mRenderer->SetSortAttributes(mSortingHelper[index]);
+    }
 
-      // texture set
-      mSortingHelper[index].textureSet = item.mTextureSet;
+    // texture set
+    mSortingHelper[index].textureSet = item.mTextureSet;
 
-      mSortingHelper[index].zValue = (*sortFunction)(item.mModelViewMatrix.GetTranslation3()) - static_cast<float>(item.mDepthIndex);
+    mSortingHelper[index].zValue = zValueFunctionFromVector3[zValueFunctionIndex](item.mModelViewMatrix.GetTranslation3()) - static_cast<float>(item.mDepthIndex);
 
-      // Keep the RenderItem pointer in the helper so we can quickly reorder items after sort.
-      mSortingHelper[index].renderItem = &item;
-    }
+    // Keep the renderitem pointer in the helper so we can quickly reorder items after sort.
+    mSortingHelper[index].renderItem = itemKey;
   }
 
   // Here we determine which comparitor (of the 3) to use.
@@ -541,7 +506,7 @@ inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex,
   for(uint32_t index = 0; index < renderableCount; ++index, ++renderListIter)
   {
     *renderListIter = mSortingHelper[index].renderItem;
-    DALI_LOG_INFO(gRenderListLogFilter, Debug::Verbose, "  sortedList[%d] = %p\n", index, mSortingHelper[index].renderItem->mRenderer);
+    DALI_LOG_INFO(gRenderListLogFilter, Debug::Verbose, "  sortedList[%d] = %x\n", index, mSortingHelper[index].renderItem->mRenderer);
   }
 }
 
@@ -559,8 +524,9 @@ void RenderInstructionProcessor::Prepare(BufferIndex                 updateBuffe
   bool               isRenderListAdded       = false;
   bool               isRootLayerDirty        = false;
 
-  const Matrix&             viewMatrix = renderTask.GetViewMatrix(updateBufferIndex);
-  const SceneGraph::Camera& camera     = renderTask.GetCamera();
+  const Matrix&             viewMatrix           = renderTask.GetViewMatrix(updateBufferIndex);
+  const SceneGraph::Camera& camera               = renderTask.GetCamera();
+  const bool                isOrthographicCamera = camera.mProjectionMode == Dali::Camera::ProjectionMode::ORTHOGRAPHIC_PROJECTION;
 
   Viewport viewport;
   bool     viewportSet = renderTask.QueryViewport(updateBufferIndex, viewport);
@@ -597,7 +563,7 @@ void RenderInstructionProcessor::Prepare(BufferIndex                 updateBuffe
                                  cull);
 
         // We only use the clipping version of the sort comparitor if any clipping nodes exist within the RenderList.
-        SortRenderItems(updateBufferIndex, *renderList, layer, hasClippingNodes);
+        SortRenderItems(updateBufferIndex, *renderList, layer, hasClippingNodes, isOrthographicCamera);
       }
 
       isRenderListAdded = true;
@@ -621,7 +587,7 @@ void RenderInstructionProcessor::Prepare(BufferIndex                 updateBuffe
                                  cull);
 
         // Clipping hierarchy is irrelevant when sorting overlay items, so we specify using the non-clipping version of the sort comparitor.
-        SortRenderItems(updateBufferIndex, *renderList, layer, false);
+        SortRenderItems(updateBufferIndex, *renderList, layer, false, isOrthographicCamera);
       }
 
       isRenderListAdded = true;