2 * Copyright (c) 2017 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/render/common/render-algorithms.h>
22 #include <dali/internal/render/common/render-debug.h>
23 #include <dali/internal/render/common/render-list.h>
24 #include <dali/internal/render/common/render-instruction.h>
25 #include <dali/internal/render/gl-resources/context.h>
26 #include <dali/internal/render/renderers/render-renderer.h>
27 #include <dali/internal/update/nodes/scene-graph-layer.h>
29 using Dali::Internal::SceneGraph::RenderItem;
30 using Dali::Internal::SceneGraph::RenderList;
31 using Dali::Internal::SceneGraph::RenderListContainer;
32 using Dali::Internal::SceneGraph::RenderInstruction;
46 // Table for fast look-up of Dali::DepthFunction enum to a GL depth function.
47 // Note: These MUST be in the same order as Dali::DepthFunction enum.
48 const int DaliDepthToGLDepthTable[] = { GL_NEVER, GL_ALWAYS, GL_LESS, GL_GREATER, GL_EQUAL, GL_NOTEQUAL, GL_LEQUAL, GL_GEQUAL };
50 // Table for fast look-up of Dali::StencilFunction enum to a GL stencil function.
51 // Note: These MUST be in the same order as Dali::StencilFunction enum.
52 const int DaliStencilFunctionToGL[] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, GL_ALWAYS };
54 // Table for fast look-up of Dali::StencilOperation enum to a GL stencil operation.
55 // Note: These MUST be in the same order as Dali::StencilOperation enum.
56 const int DaliStencilOperationToGL[] = { GL_ZERO, GL_KEEP, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP };
58 } // Unnamed namespace
61 * Sets up the scissor test if required.
62 * @param[in] renderList The render list from which to get the clipping flag
63 * @param[in] context The context
65 inline void SetScissorTest( const RenderList& renderList, Context& context )
68 if( renderList.IsClipping() )
70 context.SetScissorTest( true );
72 const Dali::ClippingBox& clip = renderList.GetClippingBox();
73 context.Scissor( clip.x, clip.y, clip.width, clip.height );
77 context.SetScissorTest( false );
82 * @brief Set up the stencil and color buffer for automatic clipping (StencilMode::AUTO).
83 * @param[in] item The current RenderItem about to be rendered
84 * @param[in] context The context
85 * @param[in/out] lastStencilDepth The stencil depth of the last renderer drawn.
86 * @param[in/out] lastClippingId The clipping ID of the last renderer drawn.
88 inline void SetupClipping( const RenderItem& item, Context& context, uint32_t& lastStencilDepth, uint32_t& lastClippingId )
90 const Dali::Internal::SceneGraph::Node* node = item.mNode;
91 const uint32_t clippingId = node->GetClippingId();
93 // Turn the color buffer on as we always want to render this renderer, regardless of clipping hierarchy.
94 context.ColorMask( true );
96 // If there is no clipping Id, then either we haven't reached a clipping Node yet, or there aren't any.
97 // Either way we can skip clipping setup for this renderer.
98 if( clippingId == 0u )
100 // Exit immediately if there are no clipping actions to perform (EG. we have not yet hit a clipping node).
101 context.EnableStencilBuffer( false );
105 const ClippingMode::Type clippingMode( node->GetClippingMode() );
106 const uint32_t currentStencilDepth( node->GetClippingDepth() );
108 context.EnableStencilBuffer( true );
110 // Pre-calculate a mask which has all bits set up to and including the current clipping depth.
111 // EG. If depth is 3, the mask would be "111" in binary.
112 const uint32_t currentDepthMask = ( 1u << currentStencilDepth ) - 1u;
114 // If we have a clipping mode specified, we are writing to the stencil buffer.
115 if( clippingMode != ClippingMode::DISABLED )
117 // We are writing to the stencil buffer.
118 // If clipping Id is 1, this is the first clipping renderer within this render-list.
119 if( clippingId == 1u )
121 // We are enabling the stencil-buffer for the first time within this render list.
122 // Clear the buffer at this point.
123 context.StencilMask( 0xff );
124 context.Clear( GL_STENCIL_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
126 else if( ( currentStencilDepth < lastStencilDepth ) ||
127 ( ( currentStencilDepth == lastStencilDepth ) && ( clippingId > lastClippingId ) ) )
129 // The above if() statement tests if we need to clear some (not all) stencil bit-planes.
130 // We need to do this if either of the following are true:
131 // 1) We traverse up the scene-graph to a previous stencil depth
132 // 2) We are at the same stencil depth but the clipping Id has increased.
134 // This calculation takes the new depth to move to, and creates an inverse-mask of that number of consecutive bits.
135 // This has the effect of clearing everything except the bit-planes up to (and including) our current depth.
136 const uint32_t stencilClearMask = ( currentDepthMask >> 1u ) ^ 0xff;
138 context.StencilMask( stencilClearMask );
139 context.Clear( GL_STENCIL_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
142 // We keep track of the last clipping Id and depth so we can determine when we are
143 // moving back up the scene graph and require some of the stencil bit-planes to be deleted.
144 lastStencilDepth = currentStencilDepth;
145 lastClippingId = clippingId;
147 // We only ever write to bit-planes up to the current depth as we may need
148 // to erase individual bit-planes and revert to a previous clipping area.
149 // Our reference value for testing (in StencilFunc) is written to to the buffer, but we actually
150 // want to test a different value. IE. All the bit-planes up to but not including the current depth.
151 // So we use the Mask parameter of StencilFunc to mask off the top bit-plane when testing.
152 // Here we create our test mask to innore the top bit of the reference test value.
153 // As the mask is made up of contiguous "1" values, we can do this quickly with a bit-shift.
154 const uint32_t testMask = currentDepthMask >> 1u;
156 context.StencilFunc( GL_EQUAL, currentDepthMask, testMask ); // Test against existing stencil bit-planes. All must match up to (but not including) this depth.
157 context.StencilMask( currentDepthMask ); // Write to the new stencil bit-plane (the other previous bit-planes are also written to).
158 context.StencilOp( GL_KEEP, GL_REPLACE, GL_REPLACE );
162 // We are reading from the stencil buffer. Set up the stencil accordingly
163 // This calculation sets all the bits up to the current depth bit.
164 // This has the effect of testing that the pixel being written to exists in every bit-plane up to the current depth.
165 context.StencilFunc( GL_EQUAL, currentDepthMask, 0xff );
166 context.StencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
171 * @brief Set up the stencil and color buffer based on the current Renderers properties.
172 * @param[in] item The current RenderItem about to be rendered
173 * @param[in] context The context
174 * @param[in/out] usedStencilBuffer True if the stencil buffer has been used so far within this RenderList. Used by StencilMode::ON.
175 * @param[in/out] lastStencilDepth The stencil depth of the last renderer drawn. Used by the clipping feature.
176 * @param[in/out] lastClippingId The clipping ID of the last renderer drawn. Used by the clipping feature.
178 inline void SetupStencilBuffer( const RenderItem& item, Context& context, bool& usedStencilBuffer, uint32_t& lastStencilDepth, uint32_t& lastClippingId )
180 const Renderer *renderer = item.mRenderer;
182 // Setup the stencil using either the automatic clipping feature, or, the manual per-renderer stencil API.
183 // Note: This switch is in order of most likely value first.
184 RenderMode::Type renderMode = renderer->GetRenderMode();
187 case RenderMode::AUTO:
189 // The automatic clipping feature will manage the stencil functions and color buffer mask.
190 SetupClipping( item, context, lastStencilDepth, lastClippingId );
194 case RenderMode::NONE:
195 case RenderMode::COLOR:
197 // The stencil buffer will not be used at all.
198 context.EnableStencilBuffer( false );
200 // Setup the color buffer based on the RenderMode.
201 context.ColorMask( renderMode == RenderMode::COLOR );
203 break; // Break statement for consistency (although return will be called instead).
206 case RenderMode::STENCIL:
207 case RenderMode::COLOR_STENCIL:
209 // We are using the low-level Renderer Stencil API.
210 // The stencil buffer must be enabled for every renderer with stencil mode on, as renderers in between can disable it.
211 // Note: As the command state is cached, it is only sent when needed.
212 context.EnableStencilBuffer( true );
214 // Setup the color buffer based on the RenderMode.
215 context.ColorMask( renderMode == RenderMode::COLOR_STENCIL );
217 // If this is the first use of the stencil buffer within this RenderList, clear it (this avoids unnecessary clears).
218 if( !usedStencilBuffer )
220 context.Clear( GL_STENCIL_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
221 usedStencilBuffer = true;
224 // Setup the stencil buffer based on the renderers properties.
225 context.StencilFunc( DaliStencilFunctionToGL[ renderer->GetStencilFunction() ],
226 renderer->GetStencilFunctionReference(),
227 renderer->GetStencilFunctionMask() );
228 context.StencilOp( DaliStencilOperationToGL[ renderer->GetStencilOperationOnFail() ],
229 DaliStencilOperationToGL[ renderer->GetStencilOperationOnZFail() ],
230 DaliStencilOperationToGL[ renderer->GetStencilOperationOnZPass() ] );
231 context.StencilMask( renderer->GetStencilMask() );
238 * @brief Sets up the depth buffer for reading and writing based on the current render item.
239 * The items read and write mode are used if specified.
240 * - If AUTO is selected for reading, the decision will be based on the Layer Behavior.
241 * - If AUTO is selected for writing, the decision will be based on the items opacity.
242 * @param[in] item The RenderItem to set up the depth buffer for.
243 * @param[in] context The context used to execute GL commands.
244 * @param[in] depthTestEnabled True if depth testing has been enabled.
245 * @param[in/out] firstDepthBufferUse Initialise to true on the first call, this method will set it to false afterwards.
247 inline void SetupDepthBuffer( const RenderItem& item, Context& context, bool depthTestEnabled, bool& firstDepthBufferUse )
249 // Set up whether or not to write to the depth buffer.
250 const DepthWriteMode::Type depthWriteMode = item.mRenderer->GetDepthWriteMode();
251 // Most common mode (AUTO) is tested first.
252 const bool enableDepthWrite = ( ( depthWriteMode == DepthWriteMode::AUTO ) && depthTestEnabled && item.mIsOpaque ) ||
253 ( depthWriteMode == DepthWriteMode::ON );
255 // Set up whether or not to read from (test) the depth buffer.
256 const DepthTestMode::Type depthTestMode = item.mRenderer->GetDepthTestMode();
257 // Most common mode (AUTO) is tested first.
258 const bool enableDepthTest = ( ( depthTestMode == DepthTestMode::AUTO ) && depthTestEnabled ) ||
259 ( depthTestMode == DepthTestMode::ON );
261 // Is the depth buffer in use?
262 if( enableDepthWrite || enableDepthTest )
264 // The depth buffer must be enabled if either reading or writing.
265 context.EnableDepthBuffer( true );
267 // Set up the depth mask based on our depth write setting.
268 context.DepthMask( enableDepthWrite );
270 // Look-up the GL depth function from the Dali::DepthFunction enum, and set it.
271 context.DepthFunc( DaliDepthToGLDepthTable[ item.mRenderer->GetDepthFunction() ] );
273 // If this is the first use of the depth buffer this RenderTask, perform a clear.
274 // Note: We could do this at the beginning of the RenderTask and rely on the
275 // context cache to ignore the clear if not required, but, we would have to enable
276 // the depth buffer to do so, which could be a redundant enable.
277 if( DALI_UNLIKELY( firstDepthBufferUse ) )
279 // This is the first time the depth buffer is being written to or read.
280 firstDepthBufferUse = false;
282 // Note: The buffer will only be cleared if written to since a previous clear.
283 context.Clear( GL_DEPTH_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
288 // The depth buffer is not being used by this renderer, so we must disable it to stop it being tested.
289 context.EnableDepthBuffer( false );
294 * @brief Process a render-list.
295 * @param[in] renderList The render-list to process.
296 * @param[in] context The GL context.
297 * @param[in] buffer The current render buffer index (previous update buffer)
298 * @param[in] viewMatrix The view matrix from the appropriate camera.
299 * @param[in] projectionMatrix The projection matrix from the appropriate camera.
301 inline void ProcessRenderList(
302 const RenderList& renderList,
304 BufferIndex bufferIndex,
305 const Matrix& viewMatrix,
306 const Matrix& projectionMatrix )
308 DALI_PRINT_RENDER_LIST( renderList );
310 SetScissorTest( renderList, context );
312 // Note: The depth buffer is enabled or disabled on a per-renderer basis.
313 // Here we pre-calculate the value to use if these modes are set to AUTO.
314 const bool autoDepthTestMode( !( renderList.GetSourceLayer()->IsDepthTestDisabled() ) && renderList.HasColorRenderItems() );
315 const std::size_t count = renderList.Count();
316 uint32_t lastStencilDepth( 0u );
317 uint32_t lastClippingId( 0u );
318 bool usedStencilBuffer( false );
319 bool firstDepthBufferUse( true );
321 for( size_t index( 0u ); index < count; ++index )
323 const RenderItem& item = renderList.GetItem( index );
324 DALI_PRINT_RENDER_ITEM( item );
326 // Set up the depth buffer based on per-renderer flags.
327 // If the per renderer flags are set to "ON" or "OFF", they will always override any Layer depth mode or
328 // draw-mode state, such as Overlays.
329 // If the flags are set to "AUTO", the behaviour then depends on the type of renderer. Overlay Renderers will always
330 // disable depth testing and writing. Color Renderers will enable them if the Layer does.
331 SetupDepthBuffer( item, context, autoDepthTestMode, firstDepthBufferUse );
333 // Set up the stencil buffer based on both the Renderer and Actor APIs.
334 // The Renderer API will be used if specified. If AUTO, the Actors automatic clipping feature will be used.
335 SetupStencilBuffer( item, context, usedStencilBuffer, lastStencilDepth, lastClippingId );
338 item.mRenderer->Render( context,
342 item.mModelViewMatrix,
350 void ProcessRenderInstruction( const RenderInstruction& instruction,
352 BufferIndex bufferIndex )
354 DALI_PRINT_RENDER_INSTRUCTION( instruction, bufferIndex );
356 const Matrix* viewMatrix = instruction.GetViewMatrix( bufferIndex );
357 const Matrix* projectionMatrix = instruction.GetProjectionMatrix( bufferIndex );
359 DALI_ASSERT_DEBUG( viewMatrix );
360 DALI_ASSERT_DEBUG( projectionMatrix );
362 if( viewMatrix && projectionMatrix )
364 const RenderListContainer::SizeType count = instruction.RenderListCount();
366 // Iterate through each render list in order. If a pair of render lists
367 // are marked as interleaved, then process them together.
368 for( RenderListContainer::SizeType index = 0; index < count; ++index )
370 const RenderList* renderList = instruction.GetRenderList( index );
372 if( renderList && !renderList->IsEmpty() )
374 ProcessRenderList( *renderList,
384 } // namespace Render
386 } // namespace Internal