2 * Copyright (c) 2016 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 ) || ( clippingId != lastClippingId ) )
128 // The above if() statement tests if we need to clear some (not all) stencil bit-planes.
129 // We need to do this if either of the following are true:
130 // 1) We traverse up the scene-graph to a previous stencil
131 // 2) We are at the same stencil depth but the clipping Id has changed.
133 // This calculation takes the new depth to move to, and creates an inverse-mask of that number of consecutive bits.
134 // This has the effect of clearing everything except the bit-planes up to (and including) our current depth.
135 const uint32_t stencilClearMask = ( currentDepthMask >> 1u ) ^ 0xff;
137 context.StencilMask( stencilClearMask );
138 context.Clear( GL_STENCIL_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
141 // We keep track of the last clipping Id and depth so we can determine when we are
142 // moving back up the scene graph and require some of the stencil bit-planes to be deleted.
143 lastStencilDepth = currentStencilDepth;
144 lastClippingId = clippingId;
146 // We only ever write to bit-planes up to the current depth as we may need
147 // to erase individual bit-planes and revert to a previous clipping area.
148 // Our reference value for testing (in StencilFunc) is written to to the buffer, but we actually
149 // want to test a different value. IE. All the bit-planes up to but not including the current depth.
150 // So we use the Mask parameter of StencilFunc to mask off the top bit-plane when testing.
151 // Here we create our test mask to innore the top bit of the reference test value.
152 // As the mask is made up of contiguous "1" values, we can do this quickly with a bit-shift.
153 const uint32_t testMask = currentDepthMask >> 1u;
155 context.StencilFunc( GL_EQUAL, currentDepthMask, testMask ); // Test against existing stencil bit-planes. All must match up to (but not including) this depth.
156 context.StencilMask( currentDepthMask ); // Write to the new stencil bit-plane (the other previous bit-planes are also written to).
157 context.StencilOp( GL_KEEP, GL_REPLACE, GL_REPLACE );
161 // We are reading from the stencil buffer. Set up the stencil accordingly
162 // This calculation sets all the bits up to the current depth bit.
163 // This has the effect of testing that the pixel being written to exists in every bit-plane up to the current depth.
164 context.StencilFunc( GL_EQUAL, currentDepthMask, 0xff );
165 context.StencilOp( GL_KEEP, GL_KEEP, GL_KEEP );
170 * @brief Set up the stencil and color buffer based on the current Renderers properties.
171 * @param[in] item The current RenderItem about to be rendered
172 * @param[in] context The context
173 * @param[in/out] usedStencilBuffer True if the stencil buffer has been used so far within this RenderList. Used by StencilMode::ON.
174 * @param[in/out] lastStencilDepth The stencil depth of the last renderer drawn. Used by the clipping feature.
175 * @param[in/out] lastClippingId The clipping ID of the last renderer drawn. Used by the clipping feature.
177 inline void SetupStencilBuffer( const RenderItem& item, Context& context, bool& usedStencilBuffer, uint32_t& lastStencilDepth, uint32_t& lastClippingId )
179 const Renderer *renderer = item.mRenderer;
181 // Setup the stencil using either the automatic clipping feature, or, the manual per-renderer stencil API.
182 // Note: This switch is in order of most likely value first.
183 RenderMode::Type renderMode = renderer->GetRenderMode();
186 case RenderMode::AUTO:
188 // The automatic clipping feature will manage the stencil functions and color buffer mask.
189 SetupClipping( item, context, lastStencilDepth, lastClippingId );
193 case RenderMode::NONE:
194 case RenderMode::COLOR:
196 // The stencil buffer will not be used at all.
197 context.EnableStencilBuffer( false );
199 // Setup the color buffer based on the RenderMode.
200 context.ColorMask( renderMode == RenderMode::COLOR );
202 break; // Break statement for consistency (although return will be called instead).
205 case RenderMode::STENCIL:
206 case RenderMode::COLOR_STENCIL:
208 // We are using the low-level Renderer Stencil API.
209 // The stencil buffer must be enabled for every renderer with stencil mode on, as renderers in between can disable it.
210 // Note: As the command state is cached, it is only sent when needed.
211 context.EnableStencilBuffer( true );
213 // Setup the color buffer based on the RenderMode.
214 context.ColorMask( renderMode == RenderMode::COLOR_STENCIL );
216 // If this is the first use of the stencil buffer within this RenderList, clear it (this avoids unnecessary clears).
217 if( !usedStencilBuffer )
219 context.Clear( GL_STENCIL_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
220 usedStencilBuffer = true;
223 // Setup the stencil buffer based on the renderers properties.
224 context.StencilFunc( DaliStencilFunctionToGL[ renderer->GetStencilFunction() ],
225 renderer->GetStencilFunctionReference(),
226 renderer->GetStencilFunctionMask() );
227 context.StencilOp( DaliStencilOperationToGL[ renderer->GetStencilOperationOnFail() ],
228 DaliStencilOperationToGL[ renderer->GetStencilOperationOnZFail() ],
229 DaliStencilOperationToGL[ renderer->GetStencilOperationOnZPass() ] );
230 context.StencilMask( renderer->GetStencilMask() );
237 * @brief Sets up the depth buffer for reading and writing based on the current render item.
238 * The items read and write mode are used if specified.
239 * - If AUTO is selected for reading, the decision will be based on the Layer Behavior.
240 * - If AUTO is selected for writing, the decision will be based on the items opacity.
241 * @param[in] item The RenderItem to set up the depth buffer for.
242 * @param[in] context The context used to execute GL commands.
243 * @param[in] depthTestEnabled True if depth testing has been enabled.
244 * @param[in/out] firstDepthBufferUse Initialise to true on the first call, this method will set it to false afterwards.
246 inline void SetupDepthBuffer( const RenderItem& item, Context& context, bool depthTestEnabled, bool& firstDepthBufferUse )
248 // Set up whether or not to write to the depth buffer.
249 const DepthWriteMode::Type depthWriteMode = item.mRenderer->GetDepthWriteMode();
250 // Most common mode (AUTO) is tested first.
251 const bool enableDepthWrite = ( ( depthWriteMode == DepthWriteMode::AUTO ) && depthTestEnabled && item.mIsOpaque ) ||
252 ( depthWriteMode == DepthWriteMode::ON );
254 // Set up whether or not to read from (test) the depth buffer.
255 const DepthTestMode::Type depthTestMode = item.mRenderer->GetDepthTestMode();
256 // Most common mode (AUTO) is tested first.
257 const bool enableDepthTest = ( ( depthTestMode == DepthTestMode::AUTO ) && depthTestEnabled ) ||
258 ( depthTestMode == DepthTestMode::ON );
260 // Is the depth buffer in use?
261 if( enableDepthWrite || enableDepthTest )
263 // The depth buffer must be enabled if either reading or writing.
264 context.EnableDepthBuffer( true );
266 // Set up the depth mask based on our depth write setting.
267 context.DepthMask( enableDepthWrite );
269 // Look-up the GL depth function from the Dali::DepthFunction enum, and set it.
270 context.DepthFunc( DaliDepthToGLDepthTable[ item.mRenderer->GetDepthFunction() ] );
272 // If this is the first use of the depth buffer this RenderTask, perform a clear.
273 // Note: We could do this at the beginning of the RenderTask and rely on the
274 // context cache to ignore the clear if not required, but, we would have to enable
275 // the depth buffer to do so, which could be a redundant enable.
276 if( DALI_UNLIKELY( firstDepthBufferUse ) )
278 // This is the first time the depth buffer is being written to or read.
279 firstDepthBufferUse = false;
281 // Note: The buffer will only be cleared if written to since a previous clear.
282 context.Clear( GL_DEPTH_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
287 // The depth buffer is not being used by this renderer, so we must disable it to stop it being tested.
288 context.EnableDepthBuffer( false );
293 * @brief Process a render-list.
294 * @param[in] renderList The render-list to process.
295 * @param[in] context The GL context.
296 * @param[in] defaultShader The default shader to use.
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 SceneGraph::TextureCache& textureCache,
305 SceneGraph::Shader& defaultShader,
306 BufferIndex bufferIndex,
307 const Matrix& viewMatrix,
308 const Matrix& projectionMatrix )
310 DALI_PRINT_RENDER_LIST( renderList );
312 SetScissorTest( renderList, context );
314 // Note: The depth buffer is enabled or disabled on a per-renderer basis.
315 // Here we pre-calculate the value to use if these modes are set to AUTO.
316 const bool autoDepthTestMode( !( renderList.GetSourceLayer()->IsDepthTestDisabled() ) && renderList.HasColorRenderItems() );
317 const std::size_t count = renderList.Count();
318 uint32_t lastStencilDepth( 0u );
319 uint32_t lastClippingId( 0u );
320 bool usedStencilBuffer( false );
321 bool firstDepthBufferUse( true );
323 for( size_t index( 0u ); index < count; ++index )
325 const RenderItem& item = renderList.GetItem( index );
326 DALI_PRINT_RENDER_ITEM( item );
328 // Set up the depth buffer based on per-renderer flags.
329 // If the per renderer flags are set to "ON" or "OFF", they will always override any Layer depth mode or
330 // draw-mode state, such as Overlays.
331 // If the flags are set to "AUTO", the behaviour then depends on the type of renderer. Overlay Renderers will always
332 // disable depth testing and writing. Color Renderers will enable them if the Layer does.
333 SetupDepthBuffer( item, context, autoDepthTestMode, firstDepthBufferUse );
335 // Set up the stencil buffer based on both the Renderer and Actor APIs.
336 // The Renderer API will be used if specified. If AUTO, the Actors automatic clipping feature will be used.
337 SetupStencilBuffer( item, context, usedStencilBuffer, lastStencilDepth, lastClippingId );
340 item.mRenderer->Render( context,
346 item.mModelViewMatrix,
354 void ProcessRenderInstruction( const RenderInstruction& instruction,
356 SceneGraph::TextureCache& textureCache,
357 SceneGraph::Shader& defaultShader,
358 BufferIndex bufferIndex )
360 DALI_PRINT_RENDER_INSTRUCTION( instruction, bufferIndex );
362 const Matrix* viewMatrix = instruction.GetViewMatrix( bufferIndex );
363 const Matrix* projectionMatrix = instruction.GetProjectionMatrix( bufferIndex );
365 DALI_ASSERT_DEBUG( viewMatrix );
366 DALI_ASSERT_DEBUG( projectionMatrix );
368 if( viewMatrix && projectionMatrix )
370 const RenderListContainer::SizeType count = instruction.RenderListCount();
372 // Iterate through each render list in order. If a pair of render lists
373 // are marked as interleaved, then process them together.
374 for( RenderListContainer::SizeType index = 0; index < count; ++index )
376 const RenderList* renderList = instruction.GetRenderList( index );
378 if( renderList && !renderList->IsEmpty() )
380 ProcessRenderList( *renderList,
392 } // namespace Render
394 } // namespace Internal