Merge "Add BuildPickingRay to devel api" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-algorithms.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/common/render-algorithms.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/render/common/render-debug.h>
23 #include <dali/internal/render/common/render-instruction.h>
24 #include <dali/internal/render/common/render-list.h>
25 #include <dali/internal/render/renderers/render-renderer.h>
26 #include <dali/internal/update/nodes/scene-graph-layer.h>
27 #include <dali/public-api/math/uint-16-pair.h>
28
29 using Dali::Internal::SceneGraph::RenderInstruction;
30 using Dali::Internal::SceneGraph::RenderItem;
31 using Dali::Internal::SceneGraph::RenderList;
32 using Dali::Internal::SceneGraph::RenderListContainer;
33
34 namespace Dali
35 {
36 namespace Internal
37 {
38 namespace Render
39 {
40 namespace
41 {
42 struct GraphicsDepthCompareOp
43 {
44   constexpr explicit GraphicsDepthCompareOp(DepthFunction::Type compareOp)
45   {
46     switch(compareOp)
47     {
48       case DepthFunction::NEVER:
49         op = Graphics::CompareOp::NEVER;
50         break;
51       case DepthFunction::LESS:
52         op = Graphics::CompareOp::LESS;
53         break;
54       case DepthFunction::EQUAL:
55         op = Graphics::CompareOp::EQUAL;
56         break;
57       case DepthFunction::LESS_EQUAL:
58         op = Graphics::CompareOp::LESS_OR_EQUAL;
59         break;
60       case DepthFunction::GREATER:
61         op = Graphics::CompareOp::GREATER;
62         break;
63       case DepthFunction::NOT_EQUAL:
64         op = Graphics::CompareOp::NOT_EQUAL;
65         break;
66       case DepthFunction::GREATER_EQUAL:
67         op = Graphics::CompareOp::GREATER_OR_EQUAL;
68         break;
69       case DepthFunction::ALWAYS:
70         op = Graphics::CompareOp::ALWAYS;
71         break;
72     }
73   }
74   Graphics::CompareOp op{Graphics::CompareOp::NEVER};
75 };
76
77 struct GraphicsStencilCompareOp
78 {
79   constexpr explicit GraphicsStencilCompareOp(StencilFunction::Type compareOp)
80   {
81     switch(compareOp)
82     {
83       case StencilFunction::NEVER:
84         op = Graphics::CompareOp::NEVER;
85         break;
86       case StencilFunction::LESS:
87         op = Graphics::CompareOp::LESS;
88         break;
89       case StencilFunction::EQUAL:
90         op = Graphics::CompareOp::EQUAL;
91         break;
92       case StencilFunction::LESS_EQUAL:
93         op = Graphics::CompareOp::LESS_OR_EQUAL;
94         break;
95       case StencilFunction::GREATER:
96         op = Graphics::CompareOp::GREATER;
97         break;
98       case StencilFunction::NOT_EQUAL:
99         op = Graphics::CompareOp::NOT_EQUAL;
100         break;
101       case StencilFunction::GREATER_EQUAL:
102         op = Graphics::CompareOp::GREATER_OR_EQUAL;
103         break;
104       case StencilFunction::ALWAYS:
105         op = Graphics::CompareOp::ALWAYS;
106         break;
107     }
108   }
109   Graphics::CompareOp op{Graphics::CompareOp::NEVER};
110 };
111
112 struct GraphicsStencilOp
113 {
114   constexpr explicit GraphicsStencilOp(StencilOperation::Type stencilOp)
115   {
116     switch(stencilOp)
117     {
118       case Dali::StencilOperation::KEEP:
119         op = Graphics::StencilOp::KEEP;
120         break;
121       case Dali::StencilOperation::ZERO:
122         op = Graphics::StencilOp::ZERO;
123         break;
124       case Dali::StencilOperation::REPLACE:
125         op = Graphics::StencilOp::REPLACE;
126         break;
127       case Dali::StencilOperation::INCREMENT:
128         op = Graphics::StencilOp::INCREMENT_AND_CLAMP;
129         break;
130       case Dali::StencilOperation::DECREMENT:
131         op = Graphics::StencilOp::DECREMENT_AND_CLAMP;
132         break;
133       case Dali::StencilOperation::INVERT:
134         op = Graphics::StencilOp::INVERT;
135         break;
136       case Dali::StencilOperation::INCREMENT_WRAP:
137         op = Graphics::StencilOp::INCREMENT_AND_WRAP;
138         break;
139       case Dali::StencilOperation::DECREMENT_WRAP:
140         op = Graphics::StencilOp::DECREMENT_AND_WRAP;
141         break;
142     }
143   }
144   Graphics::StencilOp op{Graphics::StencilOp::KEEP};
145 };
146
147 inline Graphics::Viewport ViewportFromClippingBox(const Uint16Pair& sceneSize, ClippingBox clippingBox, int orientation)
148 {
149   Graphics::Viewport viewport{static_cast<float>(clippingBox.x), static_cast<float>(clippingBox.y), static_cast<float>(clippingBox.width), static_cast<float>(clippingBox.height), 0.0f, 0.0f};
150
151   if(orientation == 90 || orientation == 270)
152   {
153     if(orientation == 90)
154     {
155       viewport.x = sceneSize.GetY() - (clippingBox.y + clippingBox.height);
156       viewport.y = clippingBox.x;
157     }
158     else // orientation == 270
159     {
160       viewport.x = clippingBox.y;
161       viewport.y = sceneSize.GetX() - (clippingBox.x + clippingBox.width);
162     }
163     viewport.width  = static_cast<float>(clippingBox.height);
164     viewport.height = static_cast<float>(clippingBox.width);
165   }
166   else if(orientation == 180)
167   {
168     viewport.x = sceneSize.GetX() - (clippingBox.x + clippingBox.width);
169     viewport.y = sceneSize.GetY() - (clippingBox.y + clippingBox.height);
170   }
171   return viewport;
172 }
173
174 inline Graphics::Rect2D RecalculateRect(Graphics::Rect2D rect, int orientation, Graphics::Viewport viewport)
175 {
176   Graphics::Rect2D newRect;
177
178   // scissor's value should be set based on the default system coordinates.
179   // when the surface is rotated, the input valus already were set with the rotated angle.
180   // So, re-calculation is needed.
181   if(orientation == 90)
182   {
183     newRect.x      = viewport.height - (rect.y + rect.height);
184     newRect.y      = rect.x;
185     newRect.width  = rect.height;
186     newRect.height = rect.width;
187   }
188   else if(orientation == 180)
189   {
190     newRect.x      = viewport.width - (rect.x + rect.width);
191     newRect.y      = viewport.height - (rect.y + rect.height);
192     newRect.width  = rect.width;
193     newRect.height = rect.height;
194   }
195   else if(orientation == 270)
196   {
197     newRect.x      = rect.y;
198     newRect.y      = viewport.width - (rect.x + rect.width);
199     newRect.width  = rect.height;
200     newRect.height = rect.width;
201   }
202   else
203   {
204     newRect.x      = rect.x;
205     newRect.y      = rect.y;
206     newRect.width  = rect.width;
207     newRect.height = rect.height;
208   }
209   return newRect;
210 }
211
212 inline Graphics::Rect2D Rect2DFromClippingBox(ClippingBox clippingBox, int orientation, Graphics::Viewport viewport)
213 {
214   Graphics::Rect2D rect2D{clippingBox.x, clippingBox.y, static_cast<uint32_t>(abs(clippingBox.width)), static_cast<uint32_t>(abs(clippingBox.height))};
215   return RecalculateRect(rect2D, orientation, viewport);
216 }
217
218 inline Graphics::Rect2D Rect2DFromRect(Dali::Rect<int> rect, int orientation, Graphics::Viewport viewport)
219 {
220   Graphics::Rect2D rect2D{rect.x, rect.y, static_cast<uint32_t>(abs(rect.width)), static_cast<uint32_t>(abs(rect.height))};
221   return RecalculateRect(rect2D, orientation, viewport);
222 }
223
224 /**
225  * @brief Find the intersection of two AABB rectangles.
226  * This is a logical AND operation. IE. The intersection is the area overlapped by both rectangles.
227  * @param[in]     aabbA                  Rectangle A
228  * @param[in]     aabbB                  Rectangle B
229  * @return                               The intersection of rectangle A & B (result is a rectangle)
230  */
231 inline ClippingBox IntersectAABB(const ClippingBox& aabbA, const ClippingBox& aabbB)
232 {
233   ClippingBox intersectionBox;
234
235   // First calculate the largest starting positions in X and Y.
236   intersectionBox.x = std::max(aabbA.x, aabbB.x);
237   intersectionBox.y = std::max(aabbA.y, aabbB.y);
238
239   // Now calculate the smallest ending positions, and take the largest starting
240   // positions from the result, to get the width and height respectively.
241   // If the two boxes do not intersect at all, then we need a 0 width and height clipping area.
242   // We use max here to clamp both width and height to >= 0 for this use-case.
243   intersectionBox.width  = std::max(std::min(aabbA.x + aabbA.width, aabbB.x + aabbB.width) - intersectionBox.x, 0);
244   intersectionBox.height = std::max(std::min(aabbA.y + aabbA.height, aabbB.y + aabbB.height) - intersectionBox.y, 0);
245
246   return intersectionBox;
247 }
248
249 /**
250  * @brief Set up the stencil and color buffer for automatic clipping (StencilMode::AUTO).
251  * @param[in]     item                     The current RenderItem about to be rendered
252  * @param[in,out] commandBuffer            The command buffer to write stencil commands into
253  * @param[in,out] lastClippingDepth        The stencil depth of the last renderer drawn.
254  * @param[in,out] lastClippingId           The clipping ID of the last renderer drawn.
255  */
256 inline void SetupStencilClipping(const RenderItem& item, Graphics::CommandBuffer& commandBuffer, uint32_t& lastClippingDepth, uint32_t& lastClippingId)
257 {
258   const Dali::Internal::SceneGraph::Node* node       = item.mNode;
259   const uint32_t                          clippingId = node->GetClippingId();
260   // If there is no clipping Id, then either we haven't reached a clipping Node yet, or there aren't any.
261   // Either way we can skip clipping setup for this renderer.
262   if(clippingId == 0u)
263   {
264     // Exit immediately if there are no clipping actions to perform (EG. we have not yet hit a clipping node).
265     commandBuffer.SetStencilTestEnable(false);
266     return;
267   }
268   commandBuffer.SetStencilTestEnable(true);
269
270   const uint32_t clippingDepth = node->GetClippingDepth();
271
272   // Pre-calculate a mask which has all bits set up to and including the current clipping depth.
273   // EG. If depth is 3, the mask would be "111" in binary.
274   const uint32_t currentDepthMask = (1u << clippingDepth) - 1u;
275
276   // Are we are writing to the stencil buffer?
277   if(item.mNode->GetClippingMode() == Dali::ClippingMode::CLIP_CHILDREN)
278   {
279     // We are writing to the stencil buffer.
280     // If clipping Id is 1, this is the first clipping renderer within this render-list.
281     if(clippingId == 1u)
282     {
283       // We are enabling the stencil-buffer for the first time within this render list.
284       // Clear the buffer at this point.
285
286       commandBuffer.SetStencilWriteMask(0xFF);
287       commandBuffer.ClearStencilBuffer();
288     }
289     else if((clippingDepth < lastClippingDepth) ||
290             ((clippingDepth == lastClippingDepth) && (clippingId > lastClippingId)))
291     {
292       // The above if() statement tests if we need to clear some (not all) stencil bit-planes.
293       // We need to do this if either of the following are true:
294       //   1) We traverse up the scene-graph to a previous stencil depth
295       //   2) We are at the same stencil depth but the clipping Id has increased.
296       //
297       // This calculation takes the new depth to move to, and creates an inverse-mask of that number of consecutive bits.
298       // This has the effect of clearing everything except the bit-planes up to (and including) our current depth.
299       const uint32_t stencilClearMask = (currentDepthMask >> 1u) ^ 0xff;
300
301       commandBuffer.SetStencilWriteMask(stencilClearMask);
302       commandBuffer.ClearStencilBuffer();
303     }
304
305     // We keep track of the last clipping Id and depth so we can determine when we are
306     // moving back up the scene graph and require some of the stencil bit-planes to be deleted.
307     lastClippingDepth = clippingDepth;
308     lastClippingId    = clippingId;
309
310     // We only ever write to bit-planes up to the current depth as we may need
311     // to erase individual bit-planes and revert to a previous clipping area.
312     // Our reference value for testing (in StencilFunc) is written to to the buffer, but we actually
313     // want to test a different value. IE. All the bit-planes up to but not including the current depth.
314     // So we use the Mask parameter of StencilFunc to mask off the top bit-plane when testing.
315     // Here we create our test mask to innore the top bit of the reference test value.
316     // As the mask is made up of contiguous "1" values, we can do this quickly with a bit-shift.
317     const uint32_t testMask = currentDepthMask >> 1u;
318
319     // Test against existing stencil bit-planes. All must match up to (but not including) this depth.
320     commandBuffer.SetStencilFunc(Graphics::CompareOp::EQUAL, currentDepthMask, testMask);
321     // Write to the new stencil bit-plane (the other previous bit-planes are also written to).
322     commandBuffer.SetStencilWriteMask(currentDepthMask);
323     commandBuffer.SetStencilOp(Graphics::StencilOp::KEEP, Graphics::StencilOp::REPLACE, Graphics::StencilOp::REPLACE);
324   }
325   else
326   {
327     // We are reading from the stencil buffer. Set up the stencil accordingly
328     // This calculation sets all the bits up to the current depth bit.
329     // This has the effect of testing that the pixel being written to exists in every bit-plane up to the current depth.
330     commandBuffer.SetStencilFunc(Graphics::CompareOp::EQUAL, currentDepthMask, currentDepthMask);
331     commandBuffer.SetStencilOp(Graphics::StencilOp::KEEP, Graphics::StencilOp::KEEP, Graphics::StencilOp::KEEP);
332   }
333 }
334
335 /**
336  * @brief Sets up the depth buffer for reading and writing based on the current render item.
337  * The items read and write mode are used if specified.
338  *  - If AUTO is selected for reading, the decision will be based on the Layer Behavior.
339  *  - If AUTO is selected for writing, the decision will be based on the items opacity.
340  * @param[in]     item                The RenderItem to set up the depth buffer for.
341  * @param[in,out] secondaryCommandBuffer The secondary command buffer to write depth commands to
342  * @param[in]     depthTestEnabled    True if depth testing has been enabled.
343  * @param[in/out] firstDepthBufferUse Initialize to true on the first call, this method will set it to false afterwards.
344  */
345 inline void SetupDepthBuffer(const RenderItem& item, Graphics::CommandBuffer& commandBuffer, bool depthTestEnabled, bool& firstDepthBufferUse)
346 {
347   // Set up whether or not to write to the depth buffer.
348   const DepthWriteMode::Type depthWriteMode = item.mRenderer->GetDepthWriteMode();
349   // Most common mode (AUTO) is tested first.
350   const bool enableDepthWrite = ((depthWriteMode == DepthWriteMode::AUTO) && depthTestEnabled && item.mIsOpaque) ||
351                                 (depthWriteMode == DepthWriteMode::ON);
352
353   // Set up whether or not to read from (test) the depth buffer.
354   const DepthTestMode::Type depthTestMode = item.mRenderer->GetDepthTestMode();
355
356   // Most common mode (AUTO) is tested first.
357   const bool enableDepthTest = ((depthTestMode == DepthTestMode::AUTO) && depthTestEnabled) ||
358                                (depthTestMode == DepthTestMode::ON);
359
360   // Is the depth buffer in use?
361   if(enableDepthWrite || enableDepthTest)
362   {
363     // The depth buffer must be enabled if either reading or writing.
364     commandBuffer.SetDepthTestEnable(true);
365
366     // Look-up the depth function from the Dali::DepthFunction enum, and set it.
367     commandBuffer.SetDepthCompareOp(GraphicsDepthCompareOp(item.mRenderer->GetDepthFunction()).op);
368
369     // If this is the first use of the depth buffer this RenderTask, perform a clear.
370     // Note: We could do this at the beginning of the RenderTask and rely on the
371     // graphics implementation to ignore the clear if not required, but, we would
372     // have to enable the depth buffer to do so, which could be a redundant enable.
373     if(DALI_UNLIKELY(firstDepthBufferUse))
374     {
375       // This is the first time the depth buffer is being written to or read.
376       firstDepthBufferUse = false;
377
378       // Note: The buffer will only be cleared if written to since a previous clear.
379       commandBuffer.SetDepthWriteEnable(true);
380       commandBuffer.ClearDepthBuffer();
381     }
382
383     // Set up the depth mask based on our depth write setting.
384     commandBuffer.SetDepthWriteEnable(enableDepthWrite);
385   }
386   else
387   {
388     // The depth buffer is not being used by this renderer, so we must disable it to stop it being tested.
389     commandBuffer.SetDepthTestEnable(false);
390   }
391 }
392
393 } // Unnamed namespace
394
395 /**
396  * @brief This method is responsible for making decisions on when to apply and unapply scissor clipping, and what rectangular dimensions should be used.
397  * A stack of scissor clips at each depth of clipping is maintained, so it can be applied and unapplied.
398  * As the clips are hierarchical, this RenderItems AABB is clipped against the current "active" scissor bounds via an intersection operation.
399  * @param[in]     item                     The current RenderItem about to be rendered
400  * @param[in,out] commandBuffer            The command buffer to write into
401  * @param[in]     instruction              The render-instruction to process.
402  * @param[in]     orientation              The Scene's surface orientation.
403  */
404 inline void RenderAlgorithms::SetupScissorClipping(
405   const RenderItem&        item,
406   Graphics::CommandBuffer& commandBuffer,
407   const RenderInstruction& instruction,
408   int                      orientation)
409 {
410   // Get the number of child scissors in the stack (do not include layer or root box).
411   size_t         childStackDepth = mScissorStack.size() - 1u;
412   const uint32_t scissorDepth    = item.mNode->GetScissorDepth();
413   const bool     clippingNode    = item.mNode->GetClippingMode() == Dali::ClippingMode::CLIP_TO_BOUNDING_BOX;
414   bool           traversedUpTree = false;
415
416   // If we are using scissor clipping and we are at the same depth (or less), we need to undo previous clips.
417   // We do this by traversing up the scissor clip stack and then apply the appropriate clip for the current render item.
418   // To know this, we use clippingDepth. This value is set on *every* node, but only increased as clipping nodes are hit depth-wise.
419   // So we know if we are at depth 4 and the stackDepth is 5, that we have gone up.
420   // If the depth is the same then we are effectively part of a different sub-tree from the parent, we must also remove the current clip.
421   // Note: Stack depth must always be at least 1, as we will have the layer or stage size as the root value.
422   if((childStackDepth > 0u) && (scissorDepth < childStackDepth))
423   {
424     while(scissorDepth < childStackDepth)
425     {
426       mScissorStack.pop_back();
427       --childStackDepth;
428     }
429
430     // We traversed up the tree, we need to apply a new scissor rectangle (unless we are at the root).
431     traversedUpTree = true;
432   }
433   if(clippingNode && childStackDepth > 0u && childStackDepth == scissorDepth) // case of sibling clip area
434   {
435     mScissorStack.pop_back();
436     --childStackDepth;
437   }
438
439   // If we are on a clipping node, or we have traveled up the tree and gone back past a clipping node, may need to apply a new scissor clip.
440   if(clippingNode || traversedUpTree)
441   {
442     // First, check if we are a clipping node.
443     if(clippingNode)
444     {
445       // This is a clipping node. We generate the AABB for this node and intersect it with the previous intersection further up the tree.
446
447       // Get the AABB bounding box for the current render item.
448       const ClippingBox scissorBox(RenderItem::CalculateViewportSpaceAABB(item.mModelViewMatrix, Vector3::ZERO, item.mSize, mViewportRectangle.width, mViewportRectangle.height));
449
450       // Get the AABB for the parent item that we must intersect with.
451       const ClippingBox& parentBox(mScissorStack.back());
452
453       // We must reduce the clipping area based on the parents area to allow nested clips. This is a set intersection function.
454       // We add the new scissor box to the stack so we can return to it if needed.
455       mScissorStack.emplace_back(IntersectAABB(parentBox, scissorBox));
456     }
457
458     // The scissor test is enabled if we have any children on the stack, OR, if there are none but it is a user specified layer scissor box.
459     // IE. It is not enabled if we are at the top of the stack and the layer does not have a specified clipping box.
460     const bool scissorEnabled = (!mScissorStack.empty()) || mHasLayerScissor;
461
462     // Enable the scissor test based on the above calculation
463     if(scissorEnabled)
464     {
465       commandBuffer.SetScissorTestEnable(scissorEnabled);
466     }
467
468     // If scissor is enabled, we use the calculated screen-space coordinates (now in the stack).
469     if(scissorEnabled)
470     {
471       ClippingBox useScissorBox(mScissorStack.back());
472
473       if(instruction.mFrameBuffer && instruction.GetCamera()->IsYAxisInverted())
474       {
475         useScissorBox.y = (instruction.mFrameBuffer->GetHeight() - useScissorBox.height) - useScissorBox.y;
476       }
477
478       Graphics::Viewport graphicsViewport = ViewportFromClippingBox(Uint16Pair{0, 0}, mViewportRectangle, 0);
479       commandBuffer.SetScissor(Rect2DFromClippingBox(useScissorBox, orientation, graphicsViewport));
480     }
481   }
482   else
483   {
484     // If there is render callback on the Renderer we need to calculate the scissor box and provide it to the
485     // callback so it may be clipped
486     if(item.mRenderer->GetRenderCallback())
487     {
488       // store clipping box inside the render callback input structure
489       auto& input       = item.mRenderer->GetRenderCallbackInput();
490       input.clippingBox = ClippingBox(RenderItem::CalculateViewportSpaceAABB(item.mModelViewMatrix, Vector3::ZERO, item.mSize, mViewportRectangle.width, mViewportRectangle.height));
491     }
492   }
493 }
494
495 inline void RenderAlgorithms::SetupClipping(const RenderItem&                   item,
496                                             Graphics::CommandBuffer&            commandBuffer,
497                                             bool&                               usedStencilBuffer,
498                                             uint32_t&                           lastClippingDepth,
499                                             uint32_t&                           lastClippingId,
500                                             Integration::StencilBufferAvailable stencilBufferAvailable,
501                                             const RenderInstruction&            instruction,
502                                             int                                 orientation)
503 {
504   RenderMode::Type renderMode = RenderMode::AUTO;
505   RendererKey      renderer   = item.mRenderer;
506   if(renderer)
507   {
508     renderMode = renderer->GetRenderMode();
509   }
510
511   // Setup the stencil using either the automatic clipping feature, or, the manual per-renderer stencil API.
512   // Note: This switch is in order of most likely value first.
513   switch(renderMode)
514   {
515     case RenderMode::AUTO:
516     {
517       // Turn the color buffer on as we always want to render this renderer, regardless of clipping hierarchy.
518       commandBuffer.SetColorMask(true);
519
520       // The automatic clipping feature will manage the scissor and stencil functions, only if stencil buffer is available for the latter.
521       // As both scissor and stencil clips can be nested, we may be simultaneously traversing up the scissor tree, requiring a scissor to be un-done. Whilst simultaneously adding a new stencil clip.
522       // We process both based on our current and old clipping depths for each mode.
523       // Both methods with return rapidly if there is nothing to be done for that type of clipping.
524       SetupScissorClipping(item, commandBuffer, instruction, orientation);
525
526       if(stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
527       {
528         SetupStencilClipping(item, commandBuffer, lastClippingDepth, lastClippingId);
529       }
530       break;
531     }
532
533     case RenderMode::NONE:
534     case RenderMode::COLOR:
535     {
536       // No clipping is performed for these modes.
537       // Note: We do not turn off scissor clipping as it may be used for the whole layer.
538       // The stencil buffer will not be used at all, but we only need to disable it if it's available.
539       if(stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
540       {
541         commandBuffer.SetStencilTestEnable(false);
542       }
543
544       // Setup the color buffer based on the RenderMode.
545       commandBuffer.SetColorMask(renderMode == RenderMode::COLOR);
546       break;
547     }
548
549     case RenderMode::STENCIL:
550     case RenderMode::COLOR_STENCIL:
551     {
552       if(stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
553       {
554         // We are using the low-level Renderer Stencil API.
555         // The stencil buffer must be enabled for every renderer with stencil mode on, as renderers in between can disable it.
556         // Note: As the command state is cached, it is only sent when needed.
557         commandBuffer.SetStencilTestEnable(true);
558
559         // Setup the color buffer based on the RenderMode.
560         commandBuffer.SetColorMask(renderMode == RenderMode::COLOR_STENCIL);
561
562         // If this is the first use of the stencil buffer within this RenderList, clear it (this avoids unnecessary clears).
563         if(!usedStencilBuffer)
564         {
565           commandBuffer.ClearStencilBuffer();
566           usedStencilBuffer = true;
567         }
568
569         // Setup the stencil buffer based on the renderer's properties.
570         commandBuffer.SetStencilOp(GraphicsStencilOp(renderer->GetStencilOperationOnFail()).op,
571                                    GraphicsStencilOp(renderer->GetStencilOperationOnZPass()).op,
572                                    GraphicsStencilOp(renderer->GetStencilOperationOnZFail()).op);
573         commandBuffer.SetStencilFunc(GraphicsStencilCompareOp(renderer->GetStencilFunction()).op,
574                                      renderer->GetStencilFunctionReference(),
575                                      renderer->GetStencilFunctionMask());
576         commandBuffer.SetStencilWriteMask(renderer->GetStencilMask());
577       }
578       break;
579     }
580   }
581 }
582
583 inline void RenderAlgorithms::ProcessRenderList(const RenderList&                   renderList,
584                                                 BufferIndex                         bufferIndex,
585                                                 const Matrix&                       viewMatrix,
586                                                 const Matrix&                       projectionMatrix,
587                                                 Integration::DepthBufferAvailable   depthBufferAvailable,
588                                                 Integration::StencilBufferAvailable stencilBufferAvailable,
589                                                 Vector<Graphics::Texture*>&         boundTextures,
590                                                 const RenderInstruction&            instruction,
591                                                 const Rect<int32_t>&                viewport,
592                                                 const Rect<int>&                    rootClippingRect,
593                                                 int                                 orientation,
594                                                 const Uint16Pair&                   sceneSize)
595 {
596   DALI_PRINT_RENDER_LIST(renderList);
597
598   // Note: The depth buffer is enabled or disabled on a per-renderer basis.
599   // Here we pre-calculate the value to use if these modes are set to AUTO.
600   const bool        autoDepthTestMode((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE) &&
601                                !(renderList.GetSourceLayer()->IsDepthTestDisabled()) &&
602                                renderList.HasColorRenderItems());
603   const std::size_t count = renderList.Count();
604   uint32_t          lastClippingDepth(0u);
605   uint32_t          lastClippingId(0u);
606   bool              usedStencilBuffer(false);
607   bool              firstDepthBufferUse(true);
608
609   mViewportRectangle = viewport;
610
611   auto* mutableRenderList      = const_cast<RenderList*>(&renderList);
612   auto& secondaryCommandBuffer = mutableRenderList->GetCommandBuffer(mGraphicsController);
613   secondaryCommandBuffer.Reset();
614   secondaryCommandBuffer.SetViewport(ViewportFromClippingBox(sceneSize, mViewportRectangle, orientation));
615   mHasLayerScissor = false;
616
617   // Setup Scissor testing (for both viewport and per-node scissor)
618   mScissorStack.clear();
619
620   // Add root clipping rect (set manually for Render function by partial update for example)
621   // on the bottom of the stack
622   if(!rootClippingRect.IsEmpty())
623   {
624     Graphics::Viewport graphicsViewport = ViewportFromClippingBox(sceneSize, mViewportRectangle, 0);
625     secondaryCommandBuffer.SetScissorTestEnable(true);
626     secondaryCommandBuffer.SetScissor(Rect2DFromRect(rootClippingRect, orientation, graphicsViewport));
627     mScissorStack.push_back(rootClippingRect);
628   }
629   // We are not performing a layer clip and no clipping rect set. Add the viewport as the root scissor rectangle.
630   else if(!renderList.IsClipping())
631   {
632     secondaryCommandBuffer.SetScissorTestEnable(false);
633     mScissorStack.push_back(mViewportRectangle);
634   }
635
636   if(renderList.IsClipping())
637   {
638     Graphics::Viewport graphicsViewport = ViewportFromClippingBox(sceneSize, mViewportRectangle, 0);
639     secondaryCommandBuffer.SetScissorTestEnable(true);
640     const ClippingBox& layerScissorBox = renderList.GetClippingBox();
641     secondaryCommandBuffer.SetScissor(Rect2DFromClippingBox(layerScissorBox, orientation, graphicsViewport));
642     mScissorStack.push_back(layerScissorBox);
643     mHasLayerScissor = true;
644   }
645
646   // Prepare Render::Renderer Render for this secondary command buffer.
647   Renderer::PrepareCommandBuffer();
648
649   // Loop through all RenderItems in the RenderList, set up any prerequisites to render them, then perform the render.
650   for(uint32_t index = 0u; index < count; ++index)
651   {
652     const RenderItem& item = renderList.GetItem(index);
653
654     // Discard renderers outside the root clipping rect
655     bool skip = true;
656     if(!rootClippingRect.IsEmpty())
657     {
658       Vector4 updateArea = item.mRenderer ? item.mRenderer->GetVisualTransformedUpdateArea(bufferIndex, item.mUpdateArea) : item.mUpdateArea;
659       auto    rect       = RenderItem::CalculateViewportSpaceAABB(item.mModelViewMatrix, Vector3(updateArea.x, updateArea.y, 0.0f), Vector3(updateArea.z, updateArea.w, 0.0f), mViewportRectangle.width, mViewportRectangle.height);
660
661       if(rect.Intersect(rootClippingRect))
662       {
663         skip = false;
664       }
665     }
666     else
667     {
668       skip = false;
669     }
670
671     DALI_PRINT_RENDER_ITEM(item);
672
673     // Set up clipping based on both the Renderer and Actor APIs.
674     // The Renderer API will be used if specified. If AUTO, the Actors automatic clipping feature will be used.
675     SetupClipping(item, secondaryCommandBuffer, usedStencilBuffer, lastClippingDepth, lastClippingId, stencilBufferAvailable, instruction, orientation);
676
677     if(DALI_LIKELY(item.mRenderer))
678     {
679       // Set up the depth buffer based on per-renderer flags if depth buffer is available
680       // If the per renderer flags are set to "ON" or "OFF", they will always override any Layer depth mode or
681       // draw-mode state, such as Overlays.
682       // If the flags are set to "AUTO", the behavior then depends on the type of renderer. Overlay Renderers will
683       // always disable depth testing and writing. Color Renderers will enable them if the Layer does.
684       if(depthBufferAvailable == Integration::DepthBufferAvailable::TRUE)
685       {
686         SetupDepthBuffer(item, secondaryCommandBuffer, autoDepthTestMode, firstDepthBufferUse);
687       }
688
689       // Depending on whether the renderer has draw commands attached or not the rendering process will
690       // iterate through all the render queues. If there are no draw commands attached, only one
691       // iteration must be done and the default behaviour of the renderer will be executed.
692       // The queues allow to iterate over the same renderer multiple times changing the state of the renderer.
693       // It is similar to the multi-pass rendering.
694       if(!skip)
695       {
696         auto const MAX_QUEUE = item.mRenderer->GetDrawCommands().empty() ? 1 : DevelRenderer::RENDER_QUEUE_MAX;
697         for(auto queue = 0u; queue < MAX_QUEUE; ++queue)
698         {
699           // Render the item. It will write into the command buffer everything it has to render
700           item.mRenderer->Render(secondaryCommandBuffer, bufferIndex, *item.mNode, item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, !item.mIsOpaque, boundTextures, instruction, queue);
701         }
702       }
703     }
704   }
705 }
706
707 RenderAlgorithms::RenderAlgorithms(Graphics::Controller& graphicsController)
708 : mGraphicsController(graphicsController),
709   mViewportRectangle(),
710   mHasLayerScissor(false)
711 {
712 }
713
714 void RenderAlgorithms::ResetCommandBuffer()
715 {
716   // Reset main command buffer
717   if(!mGraphicsCommandBuffer)
718   {
719     mGraphicsCommandBuffer = mGraphicsController.CreateCommandBuffer(
720       Graphics::CommandBufferCreateInfo()
721         .SetLevel(Graphics::CommandBufferLevel::PRIMARY),
722       nullptr);
723   }
724   else
725   {
726     mGraphicsCommandBuffer->Reset();
727   }
728 }
729
730 void RenderAlgorithms::SubmitCommandBuffer()
731 {
732   // Submit main command buffer
733   Graphics::SubmitInfo submitInfo;
734   submitInfo.cmdBuffer.push_back(mGraphicsCommandBuffer.get());
735   submitInfo.flags = 0 | Graphics::SubmitFlagBits::FLUSH;
736   mGraphicsController.SubmitCommandBuffers(submitInfo);
737 }
738
739 void RenderAlgorithms::ProcessRenderInstruction(const RenderInstruction&            instruction,
740                                                 BufferIndex                         bufferIndex,
741                                                 Integration::DepthBufferAvailable   depthBufferAvailable,
742                                                 Integration::StencilBufferAvailable stencilBufferAvailable,
743                                                 Vector<Graphics::Texture*>&         boundTextures,
744                                                 const Rect<int32_t>&                viewport,
745                                                 const Rect<int>&                    rootClippingRect,
746                                                 int                                 orientation,
747                                                 const Uint16Pair&                   sceneSize)
748 {
749   DALI_PRINT_RENDER_INSTRUCTION(instruction, bufferIndex);
750
751   const Matrix* viewMatrix       = instruction.GetViewMatrix(bufferIndex);
752   const Matrix* projectionMatrix = instruction.GetProjectionMatrix(bufferIndex);
753
754   DALI_ASSERT_DEBUG(viewMatrix);
755   DALI_ASSERT_DEBUG(projectionMatrix);
756
757   if(viewMatrix && projectionMatrix)
758   {
759     std::vector<const Graphics::CommandBuffer*> buffers;
760     const RenderListContainer::SizeType         count = instruction.RenderListCount();
761
762     // Iterate through each render list in order. If a pair of render lists
763     // are marked as interleaved, then process them together.
764     for(RenderListContainer::SizeType index = 0; index < count; ++index)
765     {
766       const RenderList* renderList = instruction.GetRenderList(index);
767
768       if(renderList && !renderList->IsEmpty())
769       {
770         ProcessRenderList(*renderList,
771                           bufferIndex,
772                           *viewMatrix,
773                           *projectionMatrix,
774                           depthBufferAvailable,
775                           stencilBufferAvailable,
776                           boundTextures,
777                           instruction, //added for reflection effect
778                           viewport,
779                           rootClippingRect,
780                           orientation,
781                           sceneSize);
782
783         // Execute command buffer
784         auto* commandBuffer = renderList->GetCommandBuffer();
785         if(commandBuffer)
786         {
787           buffers.push_back(commandBuffer);
788         }
789       }
790     }
791     if(!buffers.empty())
792     {
793       mGraphicsCommandBuffer->ExecuteCommandBuffers(std::move(buffers));
794     }
795   }
796 }
797
798 } // namespace Render
799
800 } // namespace Internal
801
802 } // namespace Dali