[3.0] Combine StencilMode and WriteToColorBuffer to RenderMode
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-algorithms.cpp
1 /*
2  * Copyright (c) 2016 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-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
29 using Dali::Internal::SceneGraph::RenderItem;
30 using Dali::Internal::SceneGraph::RenderList;
31 using Dali::Internal::SceneGraph::RenderListContainer;
32 using Dali::Internal::SceneGraph::RenderInstruction;
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace Render
41 {
42
43 namespace
44 {
45
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 };
49
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 };
53
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 };
57
58 } // Unnamed namespace
59
60 /**
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
64  */
65 inline void SetScissorTest( const RenderList& renderList, Context& context )
66 {
67   // Scissor testing
68   if( renderList.IsClipping() )
69   {
70     context.SetScissorTest( true );
71
72     const Dali::ClippingBox& clip = renderList.GetClippingBox();
73     context.Scissor(clip.x, clip.y, clip.width, clip.height);
74   }
75   else
76   {
77     context.SetScissorTest( false );
78   }
79 }
80
81 /**
82  * Sets the render flags for the stencil buffer and clears all required buffers (depth and stencil if required).
83  * @param[in] renderList The render list from which to get the render flags
84  * @param[in] context The context
85  * @param[in] depthTestEnabled True if depth test is enabled for the layer
86  * @param[in] isLayer3D True if the layer is a 3D layer
87  */
88 inline void SetRenderFlags( const RenderList& renderList, Context& context, bool depthTestEnabled )
89 {
90   const unsigned int renderFlags = renderList.GetFlags();
91   GLbitfield clearMask = 0u;
92
93   // Stencil enabled, writing, and clearing...
94   const bool enableStencilBuffer( renderFlags & RenderList::STENCIL_BUFFER_ENABLED );
95   const bool enableStencilWrite( renderFlags & RenderList::STENCIL_WRITE );
96   context.EnableStencilBuffer( enableStencilBuffer );
97   if( enableStencilBuffer )
98   {
99     context.StencilFunc( ( enableStencilWrite ? GL_ALWAYS : GL_EQUAL ), 1, 0xFF );
100     context.StencilOp( GL_KEEP, GL_REPLACE, GL_REPLACE );
101
102     // Write to stencil buffer or color buffer, but not both.
103     // These should only be set if the Actor::DrawMode is managing the stencil (and color) buffer.
104     context.StencilMask( enableStencilWrite ? 0xFF : 0x00 );
105     context.ColorMask( !enableStencilWrite );
106
107     clearMask |= ( renderFlags & RenderList::STENCIL_CLEAR ) ? GL_STENCIL_BUFFER_BIT : 0u;
108   }
109
110   // Enable and Clear the depth buffer if required.
111   if( depthTestEnabled )
112   {
113     clearMask |= GL_DEPTH_BUFFER_BIT;
114
115     // We need to enable the depth buffer to clear it.
116     // Subsequently it is enabled and disabled on a per-RenderItem basis.
117     context.EnableDepthBuffer( true );
118   }
119   else
120   {
121     context.EnableDepthBuffer( false );
122   }
123
124   // Clear Depth and/or stencil buffers as required.
125   // Note: The buffers will only be cleared if written to since a previous clear.
126   context.Clear( clearMask, Context::CHECK_CACHED_VALUES );
127 }
128
129 /**
130  * @brief This method sets up the stencil and color buffer based on the current Renderers flags.
131  * @param[in]     item                     The current RenderItem about to be rendered
132  * @param[in]     context                  The context
133  * @param[in/out] usedStencilBuffer        True if the stencil buffer has been used so far within this RenderList
134  * @param[in]     stencilManagedByDrawMode True if the stencil and color buffer is being managed by DrawMode::STENCIL
135  */
136 inline void SetupPerRendererFlags( const RenderItem& item, Context& context, bool& usedStencilBuffer, bool stencilManagedByDrawMode )
137 {
138   // DrawMode::STENCIL is deprecated, however to support it we must not set
139   // flags based on the renderer properties if it is in use.
140   if( stencilManagedByDrawMode )
141   {
142     return;
143   }
144
145   // Setup the color buffer based on the renderers properties.
146   Renderer *renderer = item.mRenderer;
147   RenderMode::Type renderMode = renderer->GetRenderMode();
148   const bool writeToColorBuffer = ( renderMode == RenderMode::AUTO ) || ( renderMode == RenderMode::COLOR ) || ( renderMode == RenderMode::COLOR_STENCIL );
149   context.ColorMask( writeToColorBuffer );
150
151   // If the stencil buffer is disabled for this renderer, exit now to save unnecessary value setting.
152   if( ( renderMode != RenderMode::STENCIL ) && ( renderMode != RenderMode::COLOR_STENCIL ) )
153   {
154     // No per-renderer stencil setup, exit.
155     context.EnableStencilBuffer( false );
156     return;
157   }
158
159   // At this point, the stencil buffer is enabled.
160   context.EnableStencilBuffer( true );
161
162   // If this is the first use of the stencil buffer within this RenderList, clear it now.
163   // This avoids unnecessary clears.
164   if( !usedStencilBuffer )
165   {
166     context.Clear( GL_STENCIL_BUFFER_BIT, Context::CHECK_CACHED_VALUES );
167     usedStencilBuffer = true;
168   }
169
170   // Setup the stencil buffer based on the renderers properties.
171   context.StencilFunc( DaliStencilFunctionToGL[ renderer->GetStencilFunction() ],
172       renderer->GetStencilFunctionReference(),
173       renderer->GetStencilFunctionMask() );
174   context.StencilOp( DaliStencilOperationToGL[ renderer->GetStencilOperationOnFail() ],
175       DaliStencilOperationToGL[ renderer->GetStencilOperationOnZFail() ],
176       DaliStencilOperationToGL[ renderer->GetStencilOperationOnZPass() ] );
177   context.StencilMask( renderer->GetStencilMask() );
178 }
179
180 /**
181  * Sets up the depth buffer for reading and writing based on the current render item.
182  * The items read and write mode are used if specified.
183  * If AUTO is selected for reading, the decision will be based on the Layer Behavior.
184  * If AUTO is selected for writing, the decision will be based on the items opacity.
185  * @param item The RenderItem to set up the depth buffer for
186  * @param context The context used to execute GL commands.
187  * @param depthTestEnabled True if depth testing has been enabled.
188  */
189 inline void SetupDepthBuffer( const RenderItem& item, Context& context, bool depthTestEnabled )
190 {
191   // Set up whether or not to write to the depth buffer.
192   const DepthWriteMode::Type depthWriteMode = item.mRenderer->GetDepthWriteMode();
193   // Most common mode (AUTO) is tested first.
194   bool enableDepthWrite = ( ( depthWriteMode == DepthWriteMode::AUTO ) && depthTestEnabled && item.mIsOpaque ) ||
195                           ( depthWriteMode == DepthWriteMode::ON );
196   context.DepthMask( enableDepthWrite );
197
198   // Set up whether or not to read from (test) the depth buffer.
199   const DepthTestMode::Type depthTestMode = item.mRenderer->GetDepthTestMode();
200   // Most common mode (AUTO) is tested first.
201   bool enableDepthTest = ( ( depthTestMode == DepthTestMode::AUTO ) && depthTestEnabled ) ||
202                          ( depthTestMode == DepthTestMode::ON );
203   // Look-up the GL depth function from the Dali::DepthFunction enum, and set it.
204   context.DepthFunc( DaliDepthToGLDepthTable[ item.mRenderer->GetDepthFunction() ] );
205
206   // The depth buffer must be enabled if either reading or writing.
207   context.EnableDepthBuffer( enableDepthWrite || enableDepthTest );
208 }
209
210 /**
211  * Process a render-list.
212  * @param[in] renderList The render-list to process.
213  * @param[in] context The GL context.
214  * @param[in] defaultShader The default shader to use.
215  * @param[in] buffer The current render buffer index (previous update buffer)
216  * @param[in] viewMatrix The view matrix from the appropriate camera.
217  * @param[in] projectionMatrix The projection matrix from the appropriate camera.
218  */
219 inline void ProcessRenderList(
220   const RenderList& renderList,
221   Context& context,
222   SceneGraph::TextureCache& textureCache,
223   SceneGraph::Shader& defaultShader,
224   BufferIndex bufferIndex,
225   const Matrix& viewMatrix,
226   const Matrix& projectionMatrix )
227 {
228   DALI_PRINT_RENDER_LIST( renderList );
229
230   bool autoDepthTestMode = !( renderList.GetSourceLayer()->IsDepthTestDisabled() ) && renderList.HasColorRenderItems();
231   bool usedStencilBuffer = false;
232   bool stencilManagedByDrawMode = renderList.GetFlags() & RenderList::STENCIL_BUFFER_ENABLED;
233
234   SetScissorTest( renderList, context );
235   SetRenderFlags( renderList, context, autoDepthTestMode );
236
237   const size_t count = renderList.Count();
238   for ( size_t index = 0; index < count; ++index )
239   {
240     const RenderItem& item = renderList.GetItem( index );
241     DALI_PRINT_RENDER_ITEM( item );
242
243     // Set up the depth buffer based on per-renderer flags.
244     // If the per renderer flags are set to "ON" or "OFF", they will always override any Layer depth mode or
245     // draw-mode state, such as Overlays.
246     // If the flags are set to "AUTO", the behaviour then depends on the type of renderer. Overlay Renderers will always
247     // disable depth testing and writing. Color Renderers will enable them if the Layer does.
248     SetupDepthBuffer( item, context, autoDepthTestMode );
249     SetupPerRendererFlags( item, context, usedStencilBuffer, stencilManagedByDrawMode );
250
251     item.mRenderer->Render( context, textureCache, bufferIndex, *item.mNode, defaultShader,
252                             item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, !item.mIsOpaque );
253   }
254 }
255
256 void ProcessRenderInstruction( const RenderInstruction& instruction,
257                                Context& context,
258                                SceneGraph::TextureCache& textureCache,
259                                SceneGraph::Shader& defaultShader,
260                                BufferIndex bufferIndex )
261 {
262   DALI_PRINT_RENDER_INSTRUCTION( instruction, bufferIndex );
263
264   const Matrix* viewMatrix       = instruction.GetViewMatrix( bufferIndex );
265   const Matrix* projectionMatrix = instruction.GetProjectionMatrix( bufferIndex );
266
267   DALI_ASSERT_DEBUG( NULL != viewMatrix );
268   DALI_ASSERT_DEBUG( NULL != projectionMatrix );
269
270   if( NULL != viewMatrix &&
271       NULL != projectionMatrix )
272   {
273     const RenderListContainer::SizeType count = instruction.RenderListCount();
274
275     // Iterate through each render list in order. If a pair of render lists
276     // are marked as interleaved, then process them together.
277     for( RenderListContainer::SizeType index = 0; index < count; ++index )
278     {
279       const RenderList* renderList = instruction.GetRenderList( index );
280
281       if(  renderList &&
282           !renderList->IsEmpty() )
283       {
284         ProcessRenderList( *renderList, context, textureCache, defaultShader, bufferIndex, *viewMatrix, *projectionMatrix );
285       }
286     }
287   }
288 }
289
290 } // namespace Render
291
292 } // namespace Internal
293
294 } // namespace Dali