2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/update/manager/render-instruction-processor.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/common/matrix-utils.h>
24 #include <dali/internal/event/actors/layer-impl.h> // for the default sorting function
25 #include <dali/internal/render/common/render-instruction-container.h>
26 #include <dali/internal/render/common/render-instruction.h>
27 #include <dali/internal/render/common/render-item.h>
28 #include <dali/internal/render/common/render-tracker.h>
29 #include <dali/internal/render/renderers/render-renderer.h>
30 #include <dali/internal/render/shaders/render-shader.h>
31 #include <dali/internal/update/manager/sorted-layers.h>
32 #include <dali/internal/update/nodes/partial-rendering-data.h>
33 #include <dali/internal/update/nodes/scene-graph-layer.h>
34 #include <dali/internal/update/render-tasks/scene-graph-render-task.h>
35 #include <dali/internal/update/rendering/scene-graph-texture-set.h>
36 #include <dali/public-api/actors/layer.h>
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gRenderListLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_LISTS");
54 * Function which compares render items by shader/textureSet/geometry
55 * @param[in] lhs Left hand side item
56 * @param[in] rhs Right hand side item
57 * @return True if left item is greater than right
59 inline bool PartialCompareItems(const RenderInstructionProcessor::SortAttributes& lhs,
60 const RenderInstructionProcessor::SortAttributes& rhs)
62 if(lhs.shader == rhs.shader)
64 if(lhs.textureSet == rhs.textureSet)
66 return lhs.geometry < rhs.geometry;
68 return lhs.textureSet < rhs.textureSet;
70 return lhs.shader < rhs.shader;
74 * Function which sorts render items by depth index then by instance
75 * ptrs of shader/textureSet/geometry.
76 * @param[in] lhs Left hand side item
77 * @param[in] rhs Right hand side item
78 * @return True if left item is greater than right
80 bool CompareItems(const RenderInstructionProcessor::SortAttributes& lhs, const RenderInstructionProcessor::SortAttributes& rhs)
82 // @todo Consider replacing all these sortAttributes with a single long int that
83 // encapsulates the same data (e.g. the middle-order bits of the ptrs).
84 if(lhs.renderItem->mDepthIndex == rhs.renderItem->mDepthIndex)
86 return PartialCompareItems(lhs, rhs);
88 return lhs.renderItem->mDepthIndex < rhs.renderItem->mDepthIndex;
92 * Function which sorts the render items by Z function, then
93 * by instance ptrs of shader / geometry / material.
94 * @param[in] lhs Left hand side item
95 * @param[in] rhs Right hand side item
96 * @return True if left item is greater than right
98 bool CompareItems3D(const RenderInstructionProcessor::SortAttributes& lhs, const RenderInstructionProcessor::SortAttributes& rhs)
100 const bool lhsIsOpaque = lhs.renderItem->mIsOpaque;
101 if(lhsIsOpaque == rhs.renderItem->mIsOpaque)
105 // If both RenderItems are opaque, sort using shader, then material then geometry.
106 return PartialCompareItems(lhs, rhs);
110 if(lhs.renderItem->mDepthIndex != rhs.renderItem->mDepthIndex)
112 return lhs.renderItem->mDepthIndex < rhs.renderItem->mDepthIndex;
115 // If both RenderItems are transparent, sort using Z, then shader, then material, then geometry.
116 if(Equals(lhs.zValue, rhs.zValue))
118 return PartialCompareItems(lhs, rhs);
120 return lhs.zValue > rhs.zValue;
130 * Function which sorts render items by clipping hierarchy, then Z function and instance ptrs of shader / geometry / material.
131 * @param[in] lhs Left hand side item
132 * @param[in] rhs Right hand side item
133 * @return True if left item is greater than right
135 bool CompareItems3DWithClipping(const RenderInstructionProcessor::SortAttributes& lhs, const RenderInstructionProcessor::SortAttributes& rhs)
137 // Items must be sorted in order of clipping first, otherwise incorrect clipping regions could be used.
138 if(lhs.renderItem->mNode->mClippingSortModifier == rhs.renderItem->mNode->mClippingSortModifier)
140 return CompareItems3D(lhs, rhs);
143 return lhs.renderItem->mNode->mClippingSortModifier < rhs.renderItem->mNode->mClippingSortModifier;
147 * Set the update area of the node
148 * @param[in] node The node of the renderer
149 * @param[in] isLayer3d Whether we are processing a 3D layer or not
150 * @param[in,out] nodeWorldMatrix The world matrix of the node
151 * @param[in,out] nodeSize The size of the node
152 * @param[in,out] nodeUpdateArea The update area of the node
154 * @return True if node use it's own UpdateAreaHint, or z transform occured. False if we use nodeUpdateArea equal with Vector4(0, 0, nodeSize.width, nodeSize.height).
156 inline bool SetNodeUpdateArea(Node* node, bool isLayer3d, Matrix& nodeWorldMatrix, Vector3& nodeSize, Vector4& nodeUpdateArea)
158 node->GetWorldMatrixAndSize(nodeWorldMatrix, nodeSize);
160 if(node->GetUpdateAreaHint() == Vector4::ZERO)
166 // RenderItem::CalculateViewportSpaceAABB cannot cope with z transform
167 // I don't use item.mModelMatrix.GetTransformComponents() for z transform, would be too slow
168 Vector3 zaxis = nodeWorldMatrix.GetZAxis();
169 if(EqualsZero(zaxis.x) && EqualsZero(zaxis.y))
171 nodeUpdateArea = Vector4(0.0f, 0.0f, nodeSize.width, nodeSize.height);
174 // Keep nodeUpdateArea as Vector4::ZERO, and return true.
179 nodeUpdateArea = node->GetUpdateAreaHint();
185 * Add a renderer to the list
186 * @param updateBufferIndex to read the model matrix from
187 * @param renderPass render pass for this render instruction
188 * @param renderList to add the item to
189 * @param renderable Node-Renderer pair
190 * @param viewMatrix used to calculate modelview matrix for the item
191 * @param viewMatrixChanged Whether view matrix chagned for this time or not.
192 * @param camera The camera used to render
193 * @param isLayer3d Whether we are processing a 3D layer or not
194 * @param viewportSet Whether the viewport is set or not
195 * @param viewport The viewport
196 * @param cullingEnabled Whether frustum culling is enabled or not
197 * @param stopperNode Marker node that stops rendering(must be rendered)
199 inline void AddRendererToRenderList(BufferIndex updateBufferIndex,
201 RenderList& renderList,
202 Renderable& renderable,
203 const Matrix& viewMatrix,
204 bool viewMatrixChanged,
205 const SceneGraph::Camera& camera,
208 const Viewport& viewport,
213 Node* node = renderable.mNode;
214 Matrix nodeWorldMatrix(false);
217 Vector4 nodeUpdateArea;
218 bool nodeUpdateAreaSet(false);
219 Matrix nodeModelViewMatrix(false);
220 bool nodeModelViewMatrixSet(false);
222 const bool rendererExist(renderable.mRenderer);
224 // Don't cull items which have render callback
225 bool hasRenderCallback = (rendererExist && renderable.mRenderer->GetRenderCallback());
227 auto requiredInsideCheck = [&]() {
228 if(node != stopperNode &&
230 !hasRenderCallback &&
231 node->GetClippingMode() == ClippingMode::DISABLED &&
234 const auto& shaderData = renderable.mRenderer->GetShader().GetShaderData(renderPass);
235 return (shaderData && !shaderData->HintEnabled(Dali::Shader::Hint::MODIFIES_GEOMETRY));
240 if(requiredInsideCheck())
242 const Vector4& boundingSphere = node->GetBoundingSphere();
244 inside = (boundingSphere.w > Math::MACHINE_EPSILON_1000) &&
245 (camera.CheckSphereInFrustum(updateBufferIndex, Vector3(boundingSphere), boundingSphere.w));
247 if(inside && !isLayer3d && viewportSet)
249 SetNodeUpdateArea(node, isLayer3d, nodeWorldMatrix, nodeSize, nodeUpdateArea);
250 nodeUpdateAreaSet = true;
252 nodeScale = nodeWorldMatrix.GetScale();
254 const Vector3& size = Vector3(nodeUpdateArea.z, nodeUpdateArea.w, 0.0f) * nodeScale;
256 if(size.LengthSquared() > Math::MACHINE_EPSILON_1000)
258 MatrixUtils::MultiplyTransformMatrix(nodeModelViewMatrix, nodeWorldMatrix, viewMatrix);
259 nodeModelViewMatrixSet = true;
261 // Assume actors are at z=0, compute AABB in view space & test rect intersection
262 // against z=0 plane boundaries for frustum. (NOT viewport). This should take into account
263 // magnification due to FOV etc.
265 // TODO : Below logic might need to refactor it.
266 // If camera is Perspective, we need to calculate clipping box by FoV. Currently, we just believe default camera setup OrthographicSize well.
267 // - If then, It must use math calculate like tan(fov) internally. So, we might need calculate it only one times, and cache.
268 ClippingBox boundingBox = RenderItem::CalculateTransformSpaceAABB(nodeModelViewMatrix, Vector3(nodeUpdateArea.x, nodeUpdateArea.y, 0.0f), Vector3(nodeUpdateArea.z, nodeUpdateArea.w, 0.0f));
269 ClippingBox clippingBox = camera.GetOrthographicClippingBox(updateBufferIndex);
270 inside = clippingBox.Intersects(boundingBox);
274 * Note, the API Camera::CheckAABBInFrustum() can be used to test if a bounding box is (partially/fully) inside the view frustum.
280 bool skipRender(false);
281 bool isOpaque = true;
282 if(!hasRenderCallback)
284 const bool isVisualRendererUnder3D = (isLayer3d && !!(renderable.mRenderer && renderable.mRenderer->GetVisualProperties()));
286 const Renderer::OpacityType opacityType = rendererExist ? (isVisualRendererUnder3D ? Renderer::TRANSLUCENT : renderable.mRenderer->GetOpacityType(updateBufferIndex, renderPass, *node)) : Renderer::OPAQUE;
288 // We can skip render when node is not clipping and transparent
289 // We must not skip when node is a stopper
290 skipRender = (opacityType == Renderer::TRANSPARENT &&
291 node->GetClippingMode() == ClippingMode::DISABLED &&
292 node != stopperNode);
294 isOpaque = (opacityType == Renderer::OPAQUE);
299 // Get the next free RenderItem.
300 RenderItem& item = renderList.GetNextFreeItem();
303 item.mIsOpaque = isOpaque;
304 item.mDepthIndex = isLayer3d ? 0 : node->GetDepthIndex();
306 if(DALI_LIKELY(rendererExist))
308 item.mRenderer = renderable.mRenderer->GetRenderer();
309 item.mTextureSet = renderable.mRenderer->GetTextureSet();
310 item.mDepthIndex += renderable.mRenderer->GetDepthIndex();
314 item.mRenderer = Render::RendererKey{};
317 item.mIsUpdated = (viewMatrixChanged || isLayer3d);
319 if(!nodeUpdateAreaSet)
321 SetNodeUpdateArea(node, isLayer3d, nodeWorldMatrix, nodeSize, nodeUpdateArea);
322 nodeScale = nodeWorldMatrix.GetScale();
325 item.mScale = nodeScale;
326 item.mSize = nodeSize;
327 item.mUpdateArea = nodeUpdateArea;
328 item.mModelMatrix = nodeWorldMatrix;
330 if(!nodeModelViewMatrixSet)
332 MatrixUtils::MultiplyTransformMatrix(nodeModelViewMatrix, nodeWorldMatrix, viewMatrix);
334 item.mModelViewMatrix = nodeModelViewMatrix;
336 auto& nodePartialRenderingData = node->GetPartialRenderingData();
338 if(nodePartialRenderingData.mUpdateDecay == PartialRenderingData::Decay::UPDATED_CURRENT_FRAME)
340 item.mIsUpdated = nodePartialRenderingData.mUpdated;
344 PartialRenderingData partialRenderingData;
345 partialRenderingData.color = node->GetWorldColor(updateBufferIndex);
346 partialRenderingData.matrix = item.mModelMatrix;
347 partialRenderingData.updatedPositionSize = item.mUpdateArea;
348 partialRenderingData.size = item.mSize;
350 // Update current node's partial update data as latest.
351 if(nodePartialRenderingData.IsUpdated(partialRenderingData))
353 item.mIsUpdated = true;
354 nodePartialRenderingData.Update(partialRenderingData);
359 node->SetCulled(updateBufferIndex, false);
363 node->SetCulled(updateBufferIndex, true);
368 * Add all renderers to the list
369 * @param updateBufferIndex to read the model matrix from
370 * @param renderPass render pass for this render instruction
371 * @param renderList to add the items to
372 * @param renderers to render NodeRendererContainer Node-Renderer pairs
373 * @param viewMatrix used to calculate modelview matrix for the items
374 * @param viewMatrixChanged Whether view matrix chagned for this time or not.
375 * @param camera The camera used to render
376 * @param isLayer3d Whether we are processing a 3D layer or not
377 * @param viewportSet Whether the viewport is set or not
378 * @param viewport The viewport
379 * @param cullingEnabled Whether frustum culling is enabled or not
380 * @param stopperNode Marker node that stops rendering(must be rendered)
382 inline void AddRenderersToRenderList(BufferIndex updateBufferIndex,
384 RenderList& renderList,
385 RenderableContainer& renderers,
386 const Matrix& viewMatrix,
387 bool viewMatrixChanged,
388 const SceneGraph::Camera& camera,
391 const Viewport& viewport,
395 DALI_LOG_INFO(gRenderListLogFilter, Debug::Verbose, "AddRenderersToRenderList()\n");
397 for(auto&& renderer : renderers)
399 AddRendererToRenderList(updateBufferIndex,
415 * Try to reuse cached RenderItems from the RenderList
416 * This avoids recalculating the model view matrices in case this part of the scene was static
417 * An example case is a toolbar layer that rarely changes or a popup on top of the rest of the stage
418 * @param layer that is being processed
419 * @param renderList that is cached from frame N-1
420 * @param renderables list of renderables
422 inline bool TryReuseCachedRenderers(Layer& layer,
423 RenderList& renderList,
424 RenderableContainer& renderables)
426 bool retValue = false;
427 uint32_t renderableCount = static_cast<uint32_t>(renderables.Size());
428 // Check that the cached list originates from this layer and that the counts match
429 if((renderList.GetSourceLayer() == &layer) &&
430 (renderList.GetCachedItemCount() == renderableCount))
432 // Check that all the same renderers are there. This gives us additional security in avoiding rendering the wrong things.
433 // Render list is sorted so at this stage renderers may be in different order.
434 // Therefore we check a combined sum of all renderer addresses.
435 size_t checkSumNew = 0;
436 size_t checkSumOld = 0;
437 //@todo just use keys, don't deref.
438 for(uint32_t index = 0; index < renderableCount; ++index)
440 if(DALI_LIKELY(renderables[index].mRenderer))
442 Render::RendererKey renderer = renderables[index].mRenderer->GetRenderer();
443 checkSumNew += renderer.Value();
445 if(DALI_LIKELY(renderList.GetItem(index).mRenderer))
447 checkSumOld += renderList.GetItem(index).mRenderer.Value();
450 if(checkSumNew == checkSumOld)
452 // tell list to reuse its existing items
453 renderList.ReuseCachedItems();
461 inline bool SetupRenderList(RenderableContainer& renderables,
463 RenderInstruction& instruction,
464 bool tryReuseRenderList,
465 RenderList** renderList)
467 *renderList = &(instruction.GetNextFreeRenderList(renderables.Size()));
468 (*renderList)->SetClipping(layer.IsClipping(), layer.GetClippingBox());
469 (*renderList)->SetSourceLayer(&layer);
471 // Try to reuse cached RenderItems from last time around.
472 return (tryReuseRenderList && TryReuseCachedRenderers(layer, **renderList, renderables));
475 } // Anonymous namespace.
477 RenderInstructionProcessor::RenderInstructionProcessor()
480 // Set up a container of comparators for fast run-time selection.
481 mSortComparitors.Reserve(3u);
483 mSortComparitors.PushBack(CompareItems);
484 mSortComparitors.PushBack(CompareItems3D);
485 mSortComparitors.PushBack(CompareItems3DWithClipping);
488 RenderInstructionProcessor::~RenderInstructionProcessor() = default;
490 inline void RenderInstructionProcessor::SortRenderItems(BufferIndex bufferIndex, RenderList& renderList, Layer& layer, bool respectClippingOrder, bool isOrthographicCamera)
492 const uint32_t renderableCount = static_cast<uint32_t>(renderList.Count());
493 // Reserve space if needed.
494 const uint32_t oldcapacity = static_cast<uint32_t>(mSortingHelper.size());
495 if(oldcapacity < renderableCount)
497 mSortingHelper.reserve(renderableCount);
498 // Add real objects (reserve does not construct objects).
499 mSortingHelper.insert(mSortingHelper.begin() + oldcapacity,
500 (renderableCount - oldcapacity),
501 RenderInstructionProcessor::SortAttributes());
505 // Clear extra elements from helper, does not decrease capability.
506 mSortingHelper.resize(renderableCount);
509 // Calculate the sorting value, once per item by calling the layers sort function.
510 // Using an if and two for-loops rather than if inside for as its better for branch prediction.
512 // List of zValue calculating functions.
513 const Dali::Layer::SortFunctionType zValueFunctionFromVector3[] = {
514 [](const Vector3& position) { return position.z; },
515 [](const Vector3& position) { return position.LengthSquared(); },
516 layer.GetSortFunction(),
519 // Determine whether we need to use zValue as Euclidean distance or translatoin's z value.
520 // If layer is LAYER_UI or camera is OrthographicProjection mode, we don't need to calculate
521 // renderItem's distance from camera.
523 // Here we determine which zValue SortFunctionType (of the 3) to use.
524 // 0 is position z value : Default LAYER_UI or Orthographic camera
525 // 1 is distance square value : Default LAYER_3D and Perspective camera
526 // 2 is user defined function.
527 const int zValueFunctionIndex = layer.UsesDefaultSortFunction() ? ((layer.GetBehavior() == Dali::Layer::LAYER_UI || isOrthographicCamera) ? 0 : 1) : 2;
529 for(uint32_t index = 0; index < renderableCount; ++index)
531 RenderItemKey itemKey = renderList.GetItemKey(index);
532 RenderItem& item = *itemKey.Get();
533 if(DALI_LIKELY(item.mRenderer))
535 item.mRenderer->SetSortAttributes(mSortingHelper[index]);
539 mSortingHelper[index].textureSet = item.mTextureSet;
541 mSortingHelper[index].zValue = zValueFunctionFromVector3[zValueFunctionIndex](item.mModelViewMatrix.GetTranslation3()) - static_cast<float>(item.mDepthIndex);
543 // Keep the renderitem pointer in the helper so we can quickly reorder items after sort.
544 mSortingHelper[index].renderItem = itemKey;
547 // Here we determine which comparitor (of the 3) to use.
550 // 2 is LAYER_3D + Clipping
551 const unsigned int comparitorIndex = layer.GetBehavior() == Dali::Layer::LAYER_3D ? respectClippingOrder ? 2u : 1u : 0u;
553 std::stable_sort(mSortingHelper.begin(), mSortingHelper.end(), mSortComparitors[comparitorIndex]);
555 // Reorder / re-populate the RenderItems in the RenderList to correct order based on the sortinghelper.
556 DALI_LOG_INFO(gRenderListLogFilter, Debug::Verbose, "Sorted Transparent List:\n");
557 RenderItemContainer::Iterator renderListIter = renderList.GetContainer().Begin();
558 for(uint32_t index = 0; index < renderableCount; ++index, ++renderListIter)
560 *renderListIter = mSortingHelper[index].renderItem;
561 DALI_LOG_INFO(gRenderListLogFilter, Debug::Verbose, " sortedList[%d] = node : %x renderer : %x\n", index, mSortingHelper[index].renderItem->mNode, mSortingHelper[index].renderItem->mRenderer.Get());
565 void RenderInstructionProcessor::Prepare(BufferIndex updateBufferIndex,
566 SortedLayerPointers& sortedLayers,
567 RenderTask& renderTask,
569 bool hasClippingNodes,
570 RenderInstructionContainer& instructions)
572 // Retrieve the RenderInstruction buffer from the RenderInstructionContainer
573 // then populate with instructions.
574 RenderInstruction& instruction = renderTask.PrepareRenderInstruction(updateBufferIndex);
575 bool viewMatrixHasNotChanged = !renderTask.ViewMatrixUpdated();
576 bool isRenderListAdded = false;
577 bool isRootLayerDirty = false;
579 const Matrix& viewMatrix = renderTask.GetViewMatrix(updateBufferIndex);
580 const SceneGraph::Camera& camera = renderTask.GetCamera();
581 const bool isOrthographicCamera = camera.mProjectionMode[0] == Dali::Camera::ProjectionMode::ORTHOGRAPHIC_PROJECTION;
584 bool viewportSet = renderTask.QueryViewport(updateBufferIndex, viewport);
586 Node* stopperNode = renderTask.GetStopperNode();
588 const SortedLayersIter endIter = sortedLayers.end();
589 for(SortedLayersIter iter = sortedLayers.begin(); iter != endIter; ++iter)
591 Layer& layer = **iter;
592 const bool tryReuseRenderList(viewMatrixHasNotChanged && layer.CanReuseRenderers(&camera));
593 const bool isLayer3D = layer.GetBehavior() == Dali::Layer::LAYER_3D;
594 RenderList* renderList = nullptr;
596 if(layer.IsRoot() && (layer.GetDirtyFlags() != NodePropertyFlags::NOTHING))
598 // If root-layer & dirty, i.e. a property has changed or a child has been deleted, then we need to ensure we render once more
599 isRootLayerDirty = true;
602 if(!layer.colorRenderables.Empty())
604 RenderableContainer& renderables = layer.colorRenderables;
606 if(!SetupRenderList(renderables, layer, instruction, tryReuseRenderList, &renderList))
608 renderList->SetHasColorRenderItems(true);
609 AddRenderersToRenderList(updateBufferIndex,
610 instruction.mRenderPassTag,
614 !viewMatrixHasNotChanged,
622 // We only use the clipping version of the sort comparitor if any clipping nodes exist within the RenderList.
623 SortRenderItems(updateBufferIndex, *renderList, layer, hasClippingNodes, isOrthographicCamera);
627 renderList->SetHasColorRenderItems(true);
630 isRenderListAdded = true;
632 if(stopperNode && renderList->RenderUntil(stopperNode))
638 if(!layer.overlayRenderables.Empty())
640 RenderableContainer& renderables = layer.overlayRenderables;
642 if(!SetupRenderList(renderables, layer, instruction, tryReuseRenderList, &renderList))
644 renderList->SetHasColorRenderItems(false);
645 AddRenderersToRenderList(updateBufferIndex,
646 instruction.mRenderPassTag,
650 !viewMatrixHasNotChanged,
658 // Clipping hierarchy is irrelevant when sorting overlay items, so we specify using the non-clipping version of the sort comparitor.
659 SortRenderItems(updateBufferIndex, *renderList, layer, false, isOrthographicCamera);
663 renderList->SetHasColorRenderItems(false);
666 isRenderListAdded = true;
668 if(stopperNode && renderList->RenderUntil(stopperNode))
675 // Inform the render instruction that all renderers have been added and this frame is complete.
676 instruction.UpdateCompleted();
678 if(isRenderListAdded || instruction.mIsClearColorSet || isRootLayerDirty)
680 instructions.PushBack(updateBufferIndex, &instruction);
684 } // namespace SceneGraph
686 } // namespace Internal