2 * Copyright (c) 2014 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>
28 using Dali::Internal::SceneGraph::RenderItem;
29 using Dali::Internal::SceneGraph::RenderList;
30 using Dali::Internal::SceneGraph::RenderListContainer;
31 using Dali::Internal::SceneGraph::RenderInstruction;
43 * Helper to set the depth function
44 * @param[in] context The GL context
45 * @param[in] depthFunction The depth function
47 inline void SetDepthFunction( Context& context, DepthFunction::Type depthFunction )
49 switch( depthFunction )
51 case DepthFunction::OFF:
53 context.EnableDepthBuffer( false );
56 case DepthFunction::NEVER:
58 context.EnableDepthBuffer( true );
59 context.DepthFunc( GL_NEVER );
62 case DepthFunction::ALWAYS:
64 context.EnableDepthBuffer( true );
65 context.DepthFunc( GL_ALWAYS );
68 case DepthFunction::LESS:
70 context.EnableDepthBuffer( true );
71 context.DepthFunc( GL_LESS );
74 case DepthFunction::GREATER:
76 context.EnableDepthBuffer( true );
77 context.DepthFunc( GL_GREATER );
80 case DepthFunction::EQUAL:
82 context.EnableDepthBuffer( true );
83 context.DepthFunc( GL_EQUAL );
86 case DepthFunction::NOT_EQUAL:
88 context.EnableDepthBuffer( true );
89 context.DepthFunc( GL_NOTEQUAL );
92 case DepthFunction::LESS_EQUAL:
94 context.EnableDepthBuffer( true );
95 context.DepthFunc( GL_LEQUAL );
98 case DepthFunction::GREATER_EQUAL:
100 context.EnableDepthBuffer( true );
101 context.DepthFunc( GL_GEQUAL );
108 * Sets up the scissor test if required.
109 * @param[in] renderList The render list from which to get the clipping flag
110 * @param[in] context The context
112 inline void SetScissorTest( const RenderList& renderList, Context& context )
115 if( renderList.IsClipping() )
117 context.SetScissorTest( true );
119 const Dali::ClippingBox& clip = renderList.GetClippingBox();
120 context.Scissor(clip.x, clip.y, clip.width, clip.height);
124 context.SetScissorTest( false );
129 * Sets the render flags for depth testing and stencil buffer
131 * @param[in] renderList The render list from which to get the render flags
132 * @param[in] context The context
134 inline void SetRenderFlags( const RenderList& renderList, Context& context )
136 const unsigned int renderFlags = renderList.GetFlags();
138 if( ( renderFlags & RenderList::DEPTH_BUFFER_ENABLED ) != 0u )
140 //Enable depth testing
141 context.EnableDepthBuffer( true );
145 //Disable depth test and depth write
146 context.EnableDepthBuffer( false );
147 context.DepthMask( false );
150 GLbitfield clearMask = ( renderFlags & RenderList::DEPTH_CLEAR ) ? GL_DEPTH_BUFFER_BIT : 0u;
152 // Stencil enabled, writing, and clearing...
153 const bool enableStencilBuffer( renderFlags & RenderList::STENCIL_BUFFER_ENABLED );
154 const bool enableStencilWrite( renderFlags & RenderList::STENCIL_WRITE );
155 context.EnableStencilBuffer( enableStencilBuffer );
156 if( enableStencilBuffer )
158 context.StencilFunc( (enableStencilWrite ? GL_ALWAYS : GL_EQUAL), 1, 0xFF );
159 context.StencilOp(GL_KEEP, GL_REPLACE, GL_REPLACE);
161 clearMask |= (renderFlags & RenderList::STENCIL_CLEAR) ? GL_STENCIL_BUFFER_BIT : 0u;
164 // Write to stencil buffer or color buffer, but not both
165 context.StencilMask( enableStencilWrite ? 0xFF : 0x00 );
166 context.ColorMask( !enableStencilWrite );
168 // Clear depth and/or stencil buffer.
171 // only clear if the depth and/or stencil buffer have been written to after a previous clear
172 context.Clear( clearMask, Context::CHECK_CACHED_VALUES );
178 * Process a render-list.
179 * @param[in] renderList The render-list to process.
180 * @param[in] context The GL context.
181 * @param[in] defaultShader The default shader to use.
182 * @param[in] buffer The current render buffer index (previous update buffer)
183 * @param[in] viewMatrix The view matrix from the appropriate camera.
184 * @param[in] projectionMatrix The projection matrix from the appropriate camera.
186 inline void ProcessRenderList(
187 const RenderList& renderList,
189 SceneGraph::TextureCache& textureCache,
190 SceneGraph::Shader& defaultShader,
191 BufferIndex bufferIndex,
192 const Matrix& viewMatrix,
193 const Matrix& projectionMatrix )
195 DALI_PRINT_RENDER_LIST( renderList );
197 SetScissorTest( renderList, context );
198 SetRenderFlags( renderList, context );
200 if( renderList.HasColorRenderItems() )
202 bool depthBufferEnabled = ( ( renderList.GetFlags() & RenderList::DEPTH_BUFFER_ENABLED ) != 0u );
203 size_t count = renderList.Count();
205 if( depthBufferEnabled )
207 for ( size_t index = 0; index < count; ++index )
209 const RenderItem& item = renderList.GetItem( index );
210 DALI_PRINT_RENDER_ITEM( item );
212 DepthWriteMode::Type depthWriteMode = item.mRenderer->GetDepthWriteMode();
213 context.DepthMask( ( depthWriteMode == DepthWriteMode::AUTO && item.mIsOpaque ) ||
214 ( depthWriteMode == DepthWriteMode::ON ) );
216 SetDepthFunction( context, item.mRenderer->GetDepthFunction() );
217 item.mRenderer->Render( context, textureCache, bufferIndex, *item.mNode, defaultShader,
218 item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, !item.mIsOpaque );
223 for ( size_t index = 0; index < count; ++index )
225 const RenderItem& item = renderList.GetItem( index );
226 DALI_PRINT_RENDER_ITEM( item );
227 item.mRenderer->Render( context, textureCache, bufferIndex, *item.mNode, defaultShader,
228 item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, !item.mIsOpaque );
234 size_t count = renderList.Count();
235 for ( size_t index = 0; index < count; ++index )
237 const RenderItem& item = renderList.GetItem( index );
238 DALI_PRINT_RENDER_ITEM( item );
240 item.mRenderer->Render( context, textureCache, bufferIndex, *item.mNode, defaultShader,
241 item.mModelMatrix, item.mModelViewMatrix, viewMatrix, projectionMatrix, item.mSize, !item.mIsOpaque );
247 void ProcessRenderInstruction( const RenderInstruction& instruction,
249 SceneGraph::TextureCache& textureCache,
250 SceneGraph::Shader& defaultShader,
251 BufferIndex bufferIndex )
253 DALI_PRINT_RENDER_INSTRUCTION( instruction, bufferIndex );
255 const Matrix* viewMatrix = instruction.GetViewMatrix( bufferIndex );
256 const Matrix* projectionMatrix = instruction.GetProjectionMatrix( bufferIndex );
258 DALI_ASSERT_DEBUG( NULL != viewMatrix );
259 DALI_ASSERT_DEBUG( NULL != projectionMatrix );
261 if( NULL != viewMatrix &&
262 NULL != projectionMatrix )
264 const RenderListContainer::SizeType count = instruction.RenderListCount();
266 // Iterate through each render list in order. If a pair of render lists
267 // are marked as interleaved, then process them together.
268 for( RenderListContainer::SizeType index = 0; index < count; ++index )
270 const RenderList* renderList = instruction.GetRenderList( index );
273 !renderList->IsEmpty() )
275 ProcessRenderList( *renderList, context, textureCache, defaultShader, bufferIndex, *viewMatrix, *projectionMatrix );
281 } // namespace Render
283 } // namespace Internal