2 * Copyright (c) 2020 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-manager.h>
25 #include <dali/public-api/actors/sampling.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/render-tasks/render-task.h>
29 #include <dali/devel-api/threading/thread-pool.h>
30 #include <dali/integration-api/debug.h>
31 #include <dali/integration-api/core.h>
32 #include <dali/integration-api/gl-context-helper-abstraction.h>
33 #include <dali/internal/common/owner-pointer.h>
34 #include <dali/internal/event/common/scene-impl.h>
35 #include <dali/internal/render/common/render-algorithms.h>
36 #include <dali/internal/render/common/render-debug.h>
37 #include <dali/internal/render/common/render-tracker.h>
38 #include <dali/internal/render/common/render-instruction-container.h>
39 #include <dali/internal/render/common/render-instruction.h>
40 #include <dali/internal/render/gl-resources/context.h>
41 #include <dali/internal/render/queue/render-queue.h>
42 #include <dali/internal/render/renderers/render-frame-buffer.h>
43 #include <dali/internal/render/renderers/render-geometry.h>
44 #include <dali/internal/render/renderers/render-renderer.h>
45 #include <dali/internal/render/renderers/render-sampler.h>
46 #include <dali/internal/render/shaders/program-controller.h>
47 #include <dali/internal/update/common/scene-graph-scene.h>
58 #if defined(DEBUG_ENABLED)
61 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER" );
62 } // unnamed namespace
66 * Structure to contain internal data
68 struct RenderManager::Impl
70 Impl( Integration::GlAbstraction& glAbstraction,
71 Integration::GlSyncAbstraction& glSyncAbstraction,
72 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
73 Integration::DepthBufferAvailable depthBufferAvailableParam,
74 Integration::StencilBufferAvailable stencilBufferAvailableParam )
75 : context( glAbstraction, &sceneContextContainer ),
76 currentContext( &context ),
77 glAbstraction( glAbstraction ),
78 glSyncAbstraction( glSyncAbstraction ),
79 glContextHelperAbstraction( glContextHelperAbstraction ),
83 renderBufferIndex( SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX ),
88 frameBufferContainer(),
89 lastFrameWasRendered( false ),
90 programController( glAbstraction ),
91 depthBufferAvailable( depthBufferAvailableParam ),
92 stencilBufferAvailable( stencilBufferAvailableParam )
94 // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
95 threadPool = std::unique_ptr<Dali::ThreadPool>( new Dali::ThreadPool() );
96 threadPool->Initialize( 1u );
101 threadPool.reset( nullptr ); // reset now to maintain correct destruction order
104 void AddRenderTracker( Render::RenderTracker* renderTracker )
106 DALI_ASSERT_DEBUG( renderTracker != NULL );
107 mRenderTrackers.PushBack( renderTracker );
110 void RemoveRenderTracker( Render::RenderTracker* renderTracker )
112 mRenderTrackers.EraseObject( renderTracker );
115 Context* CreateSceneContext()
117 sceneContextContainer.push_back( new Context( glAbstraction ) );
118 return sceneContextContainer[ sceneContextContainer.size() - 1 ];
121 void DestroySceneContext( Context* sceneContext )
123 auto iter = std::find( sceneContextContainer.begin(), sceneContextContainer.end(), sceneContext );
124 if( iter != sceneContextContainer.end() )
126 sceneContextContainer.erase( iter );
130 Context* ReplaceSceneContext( Context* oldSceneContext )
132 Context* newContext = new Context( glAbstraction );
133 std::replace( sceneContextContainer.begin(), sceneContextContainer.end(), oldSceneContext, newContext );
137 void UpdateTrackers()
139 for( auto&& iter : mRenderTrackers )
141 iter->PollSyncObject();
145 // the order is important for destruction,
146 // programs are owned by context at the moment.
147 Context context; ///< Holds the GL state of the share resource context
148 Context* currentContext; ///< Holds the GL state of the current context for rendering
149 std::vector< Context* > sceneContextContainer; ///< List of owned contexts holding the GL state per scene
150 Integration::GlAbstraction& glAbstraction; ///< GL abstraction
151 Integration::GlSyncAbstraction& glSyncAbstraction; ///< GL sync abstraction
152 Integration::GlContextHelperAbstraction& glContextHelperAbstraction; ///< GL context helper abstraction
153 RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread.
155 std::vector< SceneGraph::Scene* > sceneContainer; ///< List of pointers to the scene graph objects of the scenes
157 Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
159 uint32_t frameCount; ///< The current frame count
160 BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer
162 Rect<int32_t> defaultSurfaceRect; ///< Rectangle for the default surface we are rendering to
164 OwnerContainer< Render::Renderer* > rendererContainer; ///< List of owned renderers
165 OwnerContainer< Render::Sampler* > samplerContainer; ///< List of owned samplers
166 OwnerContainer< Render::Texture* > textureContainer; ///< List of owned textures
167 OwnerContainer< Render::FrameBuffer* > frameBufferContainer; ///< List of owned framebuffers
168 OwnerContainer< Render::PropertyBuffer* > propertyBufferContainer; ///< List of owned property buffers
169 OwnerContainer< Render::Geometry* > geometryContainer; ///< List of owned Geometries
171 bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions
173 OwnerContainer< Render::RenderTracker* > mRenderTrackers; ///< List of render trackers
175 ProgramController programController; ///< Owner of the GL programs
177 Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available
178 Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
180 std::unique_ptr<Dali::ThreadPool> threadPool; ///< The thread pool
181 Vector<GLuint> boundTextures; ///< The textures bound for rendering
182 Vector<GLuint> textureDependencyList; ///< The dependency list of binded textures
185 RenderManager* RenderManager::New( Integration::GlAbstraction& glAbstraction,
186 Integration::GlSyncAbstraction& glSyncAbstraction,
187 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
188 Integration::DepthBufferAvailable depthBufferAvailable,
189 Integration::StencilBufferAvailable stencilBufferAvailable )
191 RenderManager* manager = new RenderManager;
192 manager->mImpl = new Impl( glAbstraction,
194 glContextHelperAbstraction,
195 depthBufferAvailable,
196 stencilBufferAvailable );
200 RenderManager::RenderManager()
205 RenderManager::~RenderManager()
210 RenderQueue& RenderManager::GetRenderQueue()
212 return mImpl->renderQueue;
215 void RenderManager::ContextCreated()
217 mImpl->context.GlContextCreated();
218 mImpl->programController.GlContextCreated();
220 // renderers, textures and gpu buffers cannot reinitialize themselves
221 // so they rely on someone reloading the data for them
224 void RenderManager::ContextDestroyed()
226 mImpl->context.GlContextDestroyed();
227 mImpl->programController.GlContextDestroyed();
230 for( auto&& texture : mImpl->textureContainer )
232 texture->GlContextDestroyed();
235 //Inform framebuffers
236 for( auto&& framebuffer : mImpl->frameBufferContainer )
238 framebuffer->GlContextDestroyed();
242 for( auto&& renderer : mImpl->rendererContainer )
244 renderer->GlContextDestroyed();
248 for( auto&& scene : mImpl->sceneContainer )
250 scene->GlContextDestroyed();
254 void RenderManager::SetShaderSaver( ShaderSaver& upstream )
256 mImpl->programController.SetShaderSaver( upstream );
259 void RenderManager::SetDefaultSurfaceRect(const Rect<int32_t>& rect)
261 mImpl->defaultSurfaceRect = rect;
264 void RenderManager::AddRenderer( OwnerPointer< Render::Renderer >& renderer )
266 // Initialize the renderer as we are now in render thread
267 renderer->Initialize( mImpl->context );
269 mImpl->rendererContainer.PushBack( renderer.Release() );
272 void RenderManager::RemoveRenderer( Render::Renderer* renderer )
274 mImpl->rendererContainer.EraseObject( renderer );
277 void RenderManager::AddSampler( OwnerPointer< Render::Sampler >& sampler )
279 mImpl->samplerContainer.PushBack( sampler.Release() );
282 void RenderManager::RemoveSampler( Render::Sampler* sampler )
284 mImpl->samplerContainer.EraseObject( sampler );
287 void RenderManager::AddTexture( OwnerPointer< Render::Texture >& texture )
289 texture->Initialize( mImpl->context );
290 mImpl->textureContainer.PushBack( texture.Release() );
293 void RenderManager::RemoveTexture( Render::Texture* texture )
295 DALI_ASSERT_DEBUG( NULL != texture );
297 // Find the texture, use reference to pointer so we can do the erase safely
298 for ( auto&& iter : mImpl->textureContainer )
300 if ( iter == texture )
302 texture->Destroy( mImpl->context );
303 mImpl->textureContainer.Erase( &iter ); // Texture found; now destroy it
309 void RenderManager::UploadTexture( Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params )
311 texture->Upload( mImpl->context, pixelData, params );
314 void RenderManager::GenerateMipmaps( Render::Texture* texture )
316 texture->GenerateMipmaps( mImpl->context );
319 void RenderManager::SetFilterMode( Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode )
321 sampler->mMinificationFilter = static_cast<Dali::FilterMode::Type>(minFilterMode);
322 sampler->mMagnificationFilter = static_cast<Dali::FilterMode::Type>(magFilterMode );
325 void RenderManager::SetWrapMode( Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode )
327 sampler->mRWrapMode = static_cast<Dali::WrapMode::Type>(rWrapMode);
328 sampler->mSWrapMode = static_cast<Dali::WrapMode::Type>(sWrapMode);
329 sampler->mTWrapMode = static_cast<Dali::WrapMode::Type>(tWrapMode);
332 void RenderManager::AddFrameBuffer( OwnerPointer< Render::FrameBuffer >& frameBuffer )
334 Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
335 mImpl->frameBufferContainer.PushBack( frameBufferPtr );
336 frameBufferPtr->Initialize( mImpl->context );
339 void RenderManager::RemoveFrameBuffer( Render::FrameBuffer* frameBuffer )
341 DALI_ASSERT_DEBUG( NULL != frameBuffer );
343 // Find the sampler, use reference so we can safely do the erase
344 for ( auto&& iter : mImpl->frameBufferContainer )
346 if ( iter == frameBuffer )
348 frameBuffer->Destroy( mImpl->context );
349 mImpl->frameBufferContainer.Erase( &iter ); // frameBuffer found; now destroy it
355 void RenderManager::InitializeScene( SceneGraph::Scene* scene )
357 scene->Initialize( *mImpl->CreateSceneContext() );
358 mImpl->sceneContainer.push_back( scene );
361 void RenderManager::UninitializeScene( SceneGraph::Scene* scene )
363 mImpl->DestroySceneContext( scene->GetContext() );
365 auto iter = std::find( mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene );
366 if( iter != mImpl->sceneContainer.end() )
368 mImpl->sceneContainer.erase( iter );
372 void RenderManager::SurfaceReplaced( SceneGraph::Scene* scene )
374 Context* newContext = mImpl->ReplaceSceneContext( scene->GetContext() );
375 scene->Initialize( *newContext );
378 void RenderManager::AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
380 frameBuffer->AttachColorTexture( mImpl->context, texture, mipmapLevel, layer );
383 void RenderManager::AttachDepthTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel )
385 frameBuffer->AttachDepthTexture( mImpl->context, texture, mipmapLevel );
388 void RenderManager::AttachDepthStencilTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel )
390 frameBuffer->AttachDepthStencilTexture( mImpl->context, texture, mipmapLevel );
393 void RenderManager::AddPropertyBuffer( OwnerPointer< Render::PropertyBuffer >& propertyBuffer )
395 mImpl->propertyBufferContainer.PushBack( propertyBuffer.Release() );
398 void RenderManager::RemovePropertyBuffer( Render::PropertyBuffer* propertyBuffer )
400 mImpl->propertyBufferContainer.EraseObject( propertyBuffer );
403 void RenderManager::SetPropertyBufferFormat( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Render::PropertyBuffer::Format>& format )
405 propertyBuffer->SetFormat( format.Release() );
408 void RenderManager::SetPropertyBufferData( Render::PropertyBuffer* propertyBuffer, OwnerPointer< Vector<uint8_t> >& data, uint32_t size )
410 propertyBuffer->SetData( data.Release(), size );
413 void RenderManager::SetIndexBuffer( Render::Geometry* geometry, Dali::Vector<uint16_t>& indices )
415 geometry->SetIndexBuffer( indices );
418 void RenderManager::AddGeometry( OwnerPointer< Render::Geometry >& geometry )
420 mImpl->geometryContainer.PushBack( geometry.Release() );
423 void RenderManager::RemoveGeometry( Render::Geometry* geometry )
425 mImpl->geometryContainer.EraseObject( geometry );
428 void RenderManager::AttachVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer )
430 DALI_ASSERT_DEBUG( NULL != geometry );
433 for ( auto&& iter : mImpl->geometryContainer )
435 if ( iter == geometry )
437 iter->AddPropertyBuffer( propertyBuffer );
443 void RenderManager::RemoveVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer )
445 DALI_ASSERT_DEBUG( NULL != geometry );
448 for ( auto&& iter : mImpl->geometryContainer )
450 if ( iter == geometry )
452 iter->RemovePropertyBuffer( propertyBuffer );
458 void RenderManager::SetGeometryType( Render::Geometry* geometry, uint32_t geometryType )
460 geometry->SetType( Render::Geometry::Type(geometryType) );
463 void RenderManager::AddRenderTracker( Render::RenderTracker* renderTracker )
465 mImpl->AddRenderTracker(renderTracker);
468 void RenderManager::RemoveRenderTracker( Render::RenderTracker* renderTracker )
470 mImpl->RemoveRenderTracker(renderTracker);
473 ProgramCache* RenderManager::GetProgramCache()
475 return &(mImpl->programController);
478 void RenderManager::PreRender( Integration::RenderStatus& status, bool forceClear, bool uploadOnly )
480 DALI_PRINT_RENDER_START( mImpl->renderBufferIndex );
482 // Core::Render documents that GL context must be current before calling Render
483 DALI_ASSERT_DEBUG( mImpl->context.IsGlContextCreated() );
485 // Increment the frame count at the beginning of each frame
488 // Process messages queued during previous update
489 mImpl->renderQueue.ProcessMessages( mImpl->renderBufferIndex );
492 for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i )
494 count += mImpl->sceneContainer[i]->GetRenderInstructions().Count( mImpl->renderBufferIndex );
497 const bool haveInstructions = count > 0u;
499 DALI_LOG_INFO( gLogFilter, Debug::General,
500 "Render: haveInstructions(%s) || mImpl->lastFrameWasRendered(%s) || forceClear(%s)\n",
501 haveInstructions ? "true" : "false",
502 mImpl->lastFrameWasRendered ? "true" : "false",
503 forceClear ? "true" : "false" );
505 // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
506 if( haveInstructions || mImpl->lastFrameWasRendered || forceClear )
508 DALI_LOG_INFO( gLogFilter, Debug::General, "Render: Processing\n" );
510 // Switch to the shared context
511 if ( mImpl->currentContext != &mImpl->context )
513 mImpl->currentContext = &mImpl->context;
515 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
517 mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
520 // Clear the current cached program when the context is switched
521 mImpl->programController.ClearCurrentProgram();
524 // Upload the geometries
525 for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i )
527 RenderInstructionContainer& instructions = mImpl->sceneContainer[i]->GetRenderInstructions();
528 for ( uint32_t j = 0; j < instructions.Count( mImpl->renderBufferIndex ); ++j )
530 RenderInstruction& instruction = instructions.At( mImpl->renderBufferIndex, j );
532 const Matrix* viewMatrix = instruction.GetViewMatrix( mImpl->renderBufferIndex );
533 const Matrix* projectionMatrix = instruction.GetProjectionMatrix( mImpl->renderBufferIndex );
535 DALI_ASSERT_DEBUG( viewMatrix );
536 DALI_ASSERT_DEBUG( projectionMatrix );
538 if( viewMatrix && projectionMatrix )
540 const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
542 // Iterate through each render list.
543 for( RenderListContainer::SizeType index = 0; index < renderListCount; ++index )
545 const RenderList* renderList = instruction.GetRenderList( index );
547 if( renderList && !renderList->IsEmpty() )
549 const std::size_t itemCount = renderList->Count();
550 for( uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex )
552 const RenderItem& item = renderList->GetItem( itemIndex );
553 if( DALI_LIKELY( item.mRenderer ) )
555 item.mRenderer->Upload( *mImpl->currentContext );
567 void RenderManager::RenderScene( Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo )
569 Internal::Scene& sceneInternal = GetImplementation( scene );
570 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
572 uint32_t count = sceneObject->GetRenderInstructions().Count( mImpl->renderBufferIndex );
574 for( uint32_t i = 0; i < count; ++i )
576 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At( mImpl->renderBufferIndex, i );
578 if ( ( renderToFbo && !instruction.mFrameBuffer ) || ( !renderToFbo && instruction.mFrameBuffer ) )
583 // Mark that we will require a post-render step to be performed (includes swap-buffers).
584 status.SetNeedsPostRender( true );
586 Rect<int32_t> viewportRect;
589 if ( instruction.mIsClearColorSet )
591 clearColor = instruction.mClearColor;
595 clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR;
598 Rect<int32_t> surfaceRect = mImpl->defaultSurfaceRect;
599 Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable;
600 Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
602 if ( instruction.mFrameBuffer )
605 if ( mImpl->currentContext != &mImpl->context )
607 // Switch to shared context for off-screen buffer
608 mImpl->currentContext = &mImpl->context;
610 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
612 mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
615 // Clear the current cached program when the context is switched
616 mImpl->programController.ClearCurrentProgram();
621 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
623 if ( mImpl->currentContext != sceneObject->GetContext() )
625 // Switch the correct context if rendering to a surface
626 mImpl->currentContext = sceneObject->GetContext();
628 // Clear the current cached program when the context is switched
629 mImpl->programController.ClearCurrentProgram();
633 surfaceRect = Rect<int32_t>( 0, 0, static_cast<int32_t>( scene.GetSize().width ), static_cast<int32_t>( scene.GetSize().height ) );
636 DALI_ASSERT_DEBUG( mImpl->currentContext->IsGlContextCreated() );
638 // reset the program matrices for all programs once per frame
639 // this ensures we will set view and projection matrix once per program per camera
640 mImpl->programController.ResetProgramMatrices();
642 if( instruction.mFrameBuffer )
644 instruction.mFrameBuffer->Bind( *mImpl->currentContext );
646 // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
647 for (unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
649 mImpl->textureDependencyList.PushBack( instruction.mFrameBuffer->GetTextureId(i0) );
654 mImpl->currentContext->BindFramebuffer( GL_FRAMEBUFFER, 0u );
657 if ( !instruction.mFrameBuffer )
659 mImpl->currentContext->Viewport( surfaceRect.x,
662 surfaceRect.height );
665 // Clear the entire color, depth and stencil buffers for the default framebuffer, if required.
666 // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers
667 // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit",
668 // and then stall. That problem is only noticeable when rendering a large number of vertices per frame.
669 GLbitfield clearMask = GL_COLOR_BUFFER_BIT;
671 mImpl->currentContext->ColorMask( true );
673 if( depthBufferAvailable == Integration::DepthBufferAvailable::TRUE )
675 mImpl->currentContext->DepthMask( true );
676 clearMask |= GL_DEPTH_BUFFER_BIT;
679 if( stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
681 mImpl->currentContext->ClearStencil( 0 );
682 mImpl->currentContext->StencilMask( 0xFF ); // 8 bit stencil mask, all 1's
683 clearMask |= GL_STENCIL_BUFFER_BIT;
686 if( !instruction.mIgnoreRenderToFbo && ( instruction.mFrameBuffer != 0 ) )
688 // Offscreen buffer rendering
689 if ( instruction.mIsViewportSet )
691 // For glViewport the lower-left corner is (0,0)
692 const int32_t y = ( instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height ) - instruction.mViewport.y;
693 viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height );
697 viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
700 else // No Offscreen frame buffer rendering
702 // Check whether a viewport is specified, otherwise the full surface size is used
703 if ( instruction.mIsViewportSet )
705 // For glViewport the lower-left corner is (0,0)
706 const int32_t y = ( surfaceRect.height - instruction.mViewport.height ) - instruction.mViewport.y;
707 viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height );
711 viewportRect = surfaceRect;
715 bool clearFullFrameRect = true;
716 if( instruction.mFrameBuffer != 0 )
718 Viewport frameRect( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
719 clearFullFrameRect = ( frameRect == viewportRect );
723 clearFullFrameRect = ( surfaceRect == viewportRect );
726 mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
728 if( instruction.mIsClearColorSet )
730 mImpl->currentContext->ClearColor( clearColor.r,
735 if( !clearFullFrameRect )
737 mImpl->currentContext->SetScissorTest( true );
738 mImpl->currentContext->Scissor( viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height );
739 mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR );
740 mImpl->currentContext->SetScissorTest( false );
744 mImpl->currentContext->SetScissorTest( false );
745 mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR );
749 // Clear the list of bound textures
750 mImpl->boundTextures.Clear();
752 mImpl->renderAlgorithms.ProcessRenderInstruction(
754 *mImpl->currentContext,
755 mImpl->renderBufferIndex,
756 depthBufferAvailable,
757 stencilBufferAvailable,
758 mImpl->boundTextures );
760 // Synchronise the FBO/Texture access when there are multiple contexts
761 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
763 // Check whether any binded texture is in the dependency list
764 bool textureFound = false;
766 if ( mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u )
768 for ( auto textureId : mImpl->textureDependencyList )
771 textureFound = std::find_if( mImpl->boundTextures.Begin(), mImpl->boundTextures.End(),
772 [textureId]( GLuint id )
774 return textureId == id;
775 } ) != mImpl->boundTextures.End();
781 if ( instruction.mFrameBuffer )
783 // For off-screen buffer
785 // Wait until all rendering calls for the currently context are executed
786 mImpl->glContextHelperAbstraction.WaitClient();
788 // Clear the dependency list
789 mImpl->textureDependencyList.Clear();
793 // Worker thread lambda function
794 auto& glContextHelperAbstraction = mImpl->glContextHelperAbstraction;
795 auto workerFunction = [&glContextHelperAbstraction]( int workerThread )
797 // Switch to the shared context in the worker thread
798 glContextHelperAbstraction.MakeSurfacelessContextCurrent();
800 // Wait until all rendering calls for the shared context are executed
801 glContextHelperAbstraction.WaitClient();
803 // Must clear the context in the worker thread
804 // Otherwise the shared context cannot be switched to from the render thread
805 glContextHelperAbstraction.MakeContextNull();
808 auto future = mImpl->threadPool->SubmitTask( 0u, workerFunction );
811 mImpl->threadPool->Wait();
813 // Clear the dependency list
814 mImpl->textureDependencyList.Clear();
820 if( instruction.mRenderTracker && instruction.mFrameBuffer )
822 // This will create a sync object every frame this render tracker
823 // is alive (though it should be now be created only for
824 // render-once render tasks)
825 instruction.mRenderTracker->CreateSyncObject( mImpl->glSyncAbstraction );
826 instruction.mRenderTracker = nullptr; // Only create once.
831 mImpl->currentContext->Flush();
835 GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
836 mImpl->currentContext->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
839 void RenderManager::PostRender( bool uploadOnly )
843 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
845 mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
848 GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
849 mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
852 //Notify RenderGeometries that rendering has finished
853 for ( auto&& iter : mImpl->geometryContainer )
855 iter->OnRenderFinished();
858 mImpl->UpdateTrackers();
862 for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i )
864 count += mImpl->sceneContainer[i]->GetRenderInstructions().Count( mImpl->renderBufferIndex );
867 const bool haveInstructions = count > 0u;
869 // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
870 mImpl->lastFrameWasRendered = haveInstructions;
873 * The rendering has finished; swap to the next buffer.
874 * Ideally the update has just finished using this buffer; otherwise the render thread
875 * should block until the update has finished.
877 mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
879 DALI_PRINT_RENDER_END();
882 } // namespace SceneGraph
884 } // namespace Internal