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>
28 #include <dali/internal/update/manager/geometry-batcher.h>
30 using Dali::Internal::SceneGraph::RenderItem;
31 using Dali::Internal::SceneGraph::RenderList;
32 using Dali::Internal::SceneGraph::RenderListContainer;
33 using Dali::Internal::SceneGraph::RenderInstruction;
34 using Dali::Internal::SceneGraph::GeometryBatcher;
48 // Table for fast look-up of Dali::DepthFunction enum to a GL depth function.
49 // Note: These MUST be in the same order as Dali::DepthFunction enum.
50 const int DaliDepthToGLDepthTable[] = { GL_NEVER, GL_ALWAYS, GL_LESS, GL_GREATER, GL_EQUAL, GL_NOTEQUAL, GL_LEQUAL, GL_GEQUAL };
52 // Table for fast look-up of Dali::StencilFunction enum to a GL stencil function.
53 // Note: These MUST be in the same order as Dali::StencilFunction enum.
54 const int DaliStencilFunctionToGL[] = { GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, GL_ALWAYS };
56 // Table for fast look-up of Dali::StencilOperation enum to a GL stencil operation.
57 // Note: These MUST be in the same order as Dali::StencilOperation enum.
58 const int DaliStencilOperationToGL[] = { GL_ZERO, GL_KEEP, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT, GL_INCR_WRAP, GL_DECR_WRAP };
60 } // Unnamed namespace
63 * Sets up the scissor test if required.
64 * @param[in] renderList The render list from which to get the clipping flag
65 * @param[in] context The context
67 inline void SetScissorTest( const RenderList& renderList, Context& context )
70 if( renderList.IsClipping() )
72 context.SetScissorTest( true );
74 const Dali::ClippingBox& clip = renderList.GetClippingBox();
75 context.Scissor(clip.x, clip.y, clip.width, clip.height);
79 context.SetScissorTest( false );
84 * Sets the render flags for the stencil buffer and clears all required buffers (depth and stencil if required).
85 * @param[in] renderList The render list from which to get the render flags
86 * @param[in] context The context
87 * @param[in] depthTestEnabled True if depth test is enabled for the layer
89 inline void SetRenderFlags( const RenderList& renderList, Context& context, bool depthTestEnabled )
91 const unsigned int renderFlags = renderList.GetFlags();
92 GLbitfield clearMask = 0u;
94 // Stencil enabled, writing, and clearing...
95 const bool enableStencilBuffer( renderFlags & RenderList::STENCIL_BUFFER_ENABLED );
96 const bool enableStencilWrite( renderFlags & RenderList::STENCIL_WRITE );
97 context.EnableStencilBuffer( enableStencilBuffer );
98 if( enableStencilBuffer )
100 context.StencilFunc( ( enableStencilWrite ? GL_ALWAYS : GL_EQUAL ), 1, 0xFF );
101 context.StencilOp( GL_KEEP, GL_REPLACE, GL_REPLACE );
103 // Write to stencil buffer or color buffer, but not both.
104 // These should only be set if the Actor::DrawMode is managing the stencil (and color) buffer.
105 context.StencilMask( enableStencilWrite ? 0xFF : 0x00 );
106 context.ColorMask( !enableStencilWrite );
108 clearMask |= ( renderFlags & RenderList::STENCIL_CLEAR ) ? GL_STENCIL_BUFFER_BIT : 0u;
111 // Enable and Clear the depth buffer if required.
112 if( depthTestEnabled )
114 clearMask |= GL_DEPTH_BUFFER_BIT;
116 // We need to enable the depth buffer to clear it.
117 // Subsequently it is enabled and disabled on a per-RenderItem basis.
118 context.EnableDepthBuffer( true );
122 context.EnableDepthBuffer( false );
125 // Clear Depth and/or stencil buffers as required.
126 // Note: The buffers will only be cleared if written to since a previous clear.
127 context.Clear( clearMask, Context::CHECK_CACHED_VALUES );
131 * @brief This method sets up the stencil and color buffer based on the current Renderers flags.
132 * @param[in] item The current RenderItem about to be rendered
133 * @param[in] context The context
134 * @param[in/out] usedStencilBuffer True if the stencil buffer has been used so far within this RenderList
135 * @param[in] stencilManagedByDrawMode True if the stencil and color buffer is being managed by DrawMode::STENCIL
137 inline void SetupPerRendererFlags( const RenderItem& item, Context& context, bool& usedStencilBuffer, bool stencilManagedByDrawMode )
139 // DrawMode::STENCIL is deprecated, however to support it we must not set
140 // flags based on the renderer properties if it is in use.
141 if( stencilManagedByDrawMode )
146 // Setup the color buffer based on the renderers properties.
147 Renderer *renderer = item.mRenderer;
148 context.ColorMask( renderer->GetWriteToColorBuffer() );
150 // If the stencil buffer is disabled for this renderer, exit now to save unnecessary value setting.
151 if( renderer->GetStencilMode() != StencilMode::ON )
153 // No per-renderer stencil setup, exit.
154 context.EnableStencilBuffer( false );
158 // At this point, the stencil buffer is enabled.
159 context.EnableStencilBuffer( true );
161 // If this is the first use of the stencil buffer within this RenderList, clear it now.
162 // This avoids unnecessary clears.
163 if( !usedStencilBuffer )
165 context.Clear( GL_STENCIL_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
166 usedStencilBuffer = true;
169 // Setup the stencil buffer based on the renderers properties.
170 context.StencilFunc( DaliStencilFunctionToGL[ renderer->GetStencilFunction() ],
171 renderer->GetStencilFunctionReference(),
172 renderer->GetStencilFunctionMask() );
173 context.StencilOp( DaliStencilOperationToGL[ renderer->GetStencilOperationOnFail() ],
174 DaliStencilOperationToGL[ renderer->GetStencilOperationOnZFail() ],
175 DaliStencilOperationToGL[ renderer->GetStencilOperationOnZPass() ] );
176 context.StencilMask( renderer->GetStencilMask() );
180 * Sets up the depth buffer for reading and writing based on the current render item.
181 * The items read and write mode are used if specified.
182 * If AUTO is selected for reading, the decision will be based on the Layer Behavior.
183 * If AUTO is selected for writing, the decision will be based on the items opacity.
184 * @param item The RenderItem to set up the depth buffer for
185 * @param context The context used to execute GL commands.
186 * @param depthTestEnabled True if depth testing has been enabled.
188 inline void SetupDepthBuffer( const RenderItem& item, Context& context, bool depthTestEnabled )
190 // Set up whether or not to write to the depth buffer.
191 const DepthWriteMode::Type depthWriteMode = item.mRenderer->GetDepthWriteMode();
192 // Most common mode (AUTO) is tested first.
193 bool enableDepthWrite = ( ( depthWriteMode == DepthWriteMode::AUTO ) && depthTestEnabled && item.mIsOpaque ) ||
194 ( depthWriteMode == DepthWriteMode::ON );
195 context.DepthMask( enableDepthWrite );
197 // Set up whether or not to read from (test) the depth buffer.
198 const DepthTestMode::Type depthTestMode = item.mRenderer->GetDepthTestMode();
199 // Most common mode (AUTO) is tested first.
200 bool enableDepthTest = ( ( depthTestMode == DepthTestMode::AUTO ) && depthTestEnabled ) ||
201 ( depthTestMode == DepthTestMode::ON );
202 // Look-up the GL depth function from the Dali::DepthFunction enum, and set it.
203 context.DepthFunc( DaliDepthToGLDepthTable[ item.mRenderer->GetDepthFunction() ] );
205 // The depth buffer must be enabled if either reading or writing.
206 context.EnableDepthBuffer( enableDepthWrite || enableDepthTest );
210 * Process a render-list.
211 * @param[in] renderList The render-list to process.
212 * @param[in] context The GL context.
213 * @param[in] defaultShader The default shader to use.
214 * @param[in] buffer The current render buffer index (previous update buffer)
215 * @param[in] viewMatrix The view matrix from the appropriate camera.
216 * @param[in] projectionMatrix The projection matrix from the appropriate camera.
217 * @param[in] geometryBatcher The instance of the geometry batcher
219 inline void ProcessRenderList(
220 const RenderList& renderList,
222 SceneGraph::TextureCache& textureCache,
223 SceneGraph::Shader& defaultShader,
224 BufferIndex bufferIndex,
225 const Matrix& viewMatrix,
226 const Matrix& projectionMatrix,
227 GeometryBatcher* geometryBatcher )
229 DALI_PRINT_RENDER_LIST( renderList );
231 bool autoDepthTestMode = !( renderList.GetSourceLayer()->IsDepthTestDisabled() ) && renderList.HasColorRenderItems();
232 bool usedStencilBuffer = false;
233 bool stencilManagedByDrawMode = renderList.GetFlags() & RenderList::STENCIL_BUFFER_ENABLED;
235 SetScissorTest( renderList, context );
236 SetRenderFlags( renderList, context, autoDepthTestMode );
238 const std::size_t count = renderList.Count();
240 for ( size_t index = 0; index < count; ++index )
242 const RenderItem& item = renderList.GetItem( index );
243 DALI_PRINT_RENDER_ITEM( item );
245 // Set up the depth buffer based on per-renderer flags.
246 // If the per renderer flags are set to "ON" or "OFF", they will always override any Layer depth mode or
247 // draw-mode state, such as Overlays.
248 // If the flags are set to "AUTO", the behaviour then depends on the type of renderer. Overlay Renderers will always
249 // disable depth testing and writing. Color Renderers will enable them if the Layer does.
250 SetupDepthBuffer( item, context, autoDepthTestMode );
251 SetupPerRendererFlags( item, context, usedStencilBuffer, stencilManagedByDrawMode );
253 // Check if the node has a valid batch index value ( set previously by
254 // GeometryBatcher ). If so, then it queries the geometry object for this particular batch.
255 // If not, it still checks if the batch parent is set as it is possible, batching may
256 // fail ( for example if vertex format or buffers are not set ). In that case we need
257 // to skip rendering, otherwise unwanted GPU buffers will get uploaded. This is very rare case.
258 uint32_t batchIndex = item.mNode->mBatchIndex;
259 if( batchIndex != BATCH_NULL_HANDLE )
261 item.mBatchRenderGeometry = geometryBatcher->GetGeometry( batchIndex );
265 skip = item.mNode->GetBatchParent();
266 item.mBatchRenderGeometry = NULL;
268 if( DALI_LIKELY( !skip ) )
270 item.mRenderer->Render( context, textureCache, bufferIndex, *item.mNode, defaultShader,
271 item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, item.mBatchRenderGeometry, !item.mIsOpaque );
276 void ProcessRenderInstruction( const RenderInstruction& instruction,
278 SceneGraph::TextureCache& textureCache,
279 SceneGraph::Shader& defaultShader,
280 GeometryBatcher& geometryBatcher,
281 BufferIndex bufferIndex )
283 DALI_PRINT_RENDER_INSTRUCTION( instruction, bufferIndex );
285 const Matrix* viewMatrix = instruction.GetViewMatrix( bufferIndex );
286 const Matrix* projectionMatrix = instruction.GetProjectionMatrix( bufferIndex );
288 DALI_ASSERT_DEBUG( NULL != viewMatrix );
289 DALI_ASSERT_DEBUG( NULL != projectionMatrix );
291 if( NULL != viewMatrix &&
292 NULL != projectionMatrix )
294 const RenderListContainer::SizeType count = instruction.RenderListCount();
296 // Iterate through each render list in order. If a pair of render lists
297 // are marked as interleaved, then process them together.
298 for( RenderListContainer::SizeType index = 0; index < count; ++index )
300 const RenderList* renderList = instruction.GetRenderList( index );
303 !renderList->IsEmpty() )
305 ProcessRenderList( *renderList, context, textureCache, defaultShader, bufferIndex, *viewMatrix, *projectionMatrix, &geometryBatcher );
311 } // namespace Render
313 } // namespace Internal