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/devel-api/threading/thread-pool.h>
26 #include <dali/integration-api/core.h>
27 #include <dali/integration-api/gl-context-helper-abstraction.h>
28 #include <dali/internal/event/common/scene-impl.h>
29 #include <dali/internal/render/common/render-algorithms.h>
30 #include <dali/internal/render/common/render-debug.h>
31 #include <dali/internal/render/common/render-tracker.h>
32 #include <dali/internal/render/queue/render-queue.h>
33 #include <dali/internal/render/shaders/program-controller.h>
44 #if defined(DEBUG_ENABLED)
47 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER" );
48 } // unnamed namespace
52 * Structure to contain internal data
54 struct RenderManager::Impl
56 Impl( Integration::GlAbstraction& glAbstraction,
57 Integration::GlSyncAbstraction& glSyncAbstraction,
58 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
59 Integration::DepthBufferAvailable depthBufferAvailableParam,
60 Integration::StencilBufferAvailable stencilBufferAvailableParam,
61 Integration::PartialUpdateAvailable partialUpdateAvailableParam )
62 : context( glAbstraction, &sceneContextContainer ),
63 currentContext( &context ),
64 glAbstraction( glAbstraction ),
65 glSyncAbstraction( glSyncAbstraction ),
66 glContextHelperAbstraction( glContextHelperAbstraction ),
70 renderBufferIndex( SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX ),
75 frameBufferContainer(),
76 lastFrameWasRendered( false ),
77 programController( glAbstraction ),
78 depthBufferAvailable( depthBufferAvailableParam ),
79 stencilBufferAvailable( stencilBufferAvailableParam ),
80 partialUpdateAvailable( partialUpdateAvailableParam ),
81 defaultSurfaceOrientation( 0 )
83 // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
84 threadPool = std::unique_ptr<Dali::ThreadPool>( new Dali::ThreadPool() );
85 threadPool->Initialize( 1u );
90 threadPool.reset( nullptr ); // reset now to maintain correct destruction order
93 void AddRenderTracker( Render::RenderTracker* renderTracker )
95 DALI_ASSERT_DEBUG( renderTracker != NULL );
96 mRenderTrackers.PushBack( renderTracker );
99 void RemoveRenderTracker( Render::RenderTracker* renderTracker )
101 mRenderTrackers.EraseObject( renderTracker );
104 Context* CreateSceneContext()
106 Context* context = new Context( glAbstraction );
107 sceneContextContainer.PushBack( context );
111 void DestroySceneContext( Context* sceneContext )
113 auto iter = std::find( sceneContextContainer.Begin(), sceneContextContainer.End(), sceneContext );
114 if( iter != sceneContextContainer.End() )
116 ( *iter )->GlContextDestroyed();
117 sceneContextContainer.Erase( iter );
121 Context* ReplaceSceneContext( Context* oldSceneContext )
123 Context* newContext = new Context( glAbstraction );
125 oldSceneContext->GlContextDestroyed();
127 std::replace( sceneContextContainer.begin(), sceneContextContainer.end(), oldSceneContext, newContext );
131 void UpdateTrackers()
133 for( auto&& iter : mRenderTrackers )
135 iter->PollSyncObject();
139 // the order is important for destruction,
140 // programs are owned by context at the moment.
141 Context context; ///< Holds the GL state of the share resource context
142 Context* currentContext; ///< Holds the GL state of the current context for rendering
143 OwnerContainer< Context* > sceneContextContainer; ///< List of owned contexts holding the GL state per scene
144 Integration::GlAbstraction& glAbstraction; ///< GL abstraction
145 Integration::GlSyncAbstraction& glSyncAbstraction; ///< GL sync abstraction
146 Integration::GlContextHelperAbstraction& glContextHelperAbstraction; ///< GL context helper abstraction
147 RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread.
149 std::vector< SceneGraph::Scene* > sceneContainer; ///< List of pointers to the scene graph objects of the scenes
151 Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
153 uint32_t frameCount; ///< The current frame count
154 BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer
156 Rect<int32_t> defaultSurfaceRect; ///< Rectangle for the default surface we are rendering to
158 OwnerContainer< Render::Renderer* > rendererContainer; ///< List of owned renderers
159 OwnerContainer< Render::Sampler* > samplerContainer; ///< List of owned samplers
160 OwnerContainer< Render::Texture* > textureContainer; ///< List of owned textures
161 OwnerContainer< Render::FrameBuffer* > frameBufferContainer; ///< List of owned framebuffers
162 OwnerContainer< Render::VertexBuffer* > vertexBufferContainer; ///< List of owned vertex buffers
163 OwnerContainer< Render::Geometry* > geometryContainer; ///< List of owned Geometries
165 bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions
167 OwnerContainer< Render::RenderTracker* > mRenderTrackers; ///< List of render trackers
169 ProgramController programController; ///< Owner of the GL programs
171 Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available
172 Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
173 Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
175 std::unique_ptr<Dali::ThreadPool> threadPool; ///< The thread pool
176 Vector<GLuint> boundTextures; ///< The textures bound for rendering
177 Vector<GLuint> textureDependencyList; ///< The dependency list of binded textures
178 int defaultSurfaceOrientation; ///< defaultSurfaceOrientation for the default surface we are rendering to
181 RenderManager* RenderManager::New( Integration::GlAbstraction& glAbstraction,
182 Integration::GlSyncAbstraction& glSyncAbstraction,
183 Integration::GlContextHelperAbstraction& glContextHelperAbstraction,
184 Integration::DepthBufferAvailable depthBufferAvailable,
185 Integration::StencilBufferAvailable stencilBufferAvailable,
186 Integration::PartialUpdateAvailable partialUpdateAvailable )
188 RenderManager* manager = new RenderManager;
189 manager->mImpl = new Impl( glAbstraction,
191 glContextHelperAbstraction,
192 depthBufferAvailable,
193 stencilBufferAvailable,
194 partialUpdateAvailable );
198 RenderManager::RenderManager()
203 RenderManager::~RenderManager()
208 RenderQueue& RenderManager::GetRenderQueue()
210 return mImpl->renderQueue;
213 void RenderManager::ContextCreated()
215 mImpl->context.GlContextCreated();
216 mImpl->programController.GlContextCreated();
218 // renderers, textures and gpu buffers cannot reinitialize themselves
219 // so they rely on someone reloading the data for them
222 void RenderManager::ContextDestroyed()
224 mImpl->context.GlContextDestroyed();
225 mImpl->programController.GlContextDestroyed();
228 for( auto&& texture : mImpl->textureContainer )
230 texture->GlContextDestroyed();
233 //Inform framebuffers
234 for( auto&& framebuffer : mImpl->frameBufferContainer )
236 framebuffer->GlContextDestroyed();
240 for( auto&& renderer : mImpl->rendererContainer )
242 renderer->GlContextDestroyed();
246 for( auto&& context : mImpl->sceneContextContainer )
248 context->GlContextDestroyed();
252 void RenderManager::SetShaderSaver( ShaderSaver& upstream )
254 mImpl->programController.SetShaderSaver( upstream );
257 void RenderManager::SetDefaultSurfaceRect(const Rect<int32_t>& rect)
259 mImpl->defaultSurfaceRect = rect;
262 void RenderManager::SetDefaultSurfaceOrientation( int orientation )
264 mImpl->defaultSurfaceOrientation = orientation;
267 void RenderManager::AddRenderer( OwnerPointer< Render::Renderer >& renderer )
269 // Initialize the renderer as we are now in render thread
270 renderer->Initialize( mImpl->context );
272 mImpl->rendererContainer.PushBack( renderer.Release() );
275 void RenderManager::RemoveRenderer( Render::Renderer* renderer )
277 mImpl->rendererContainer.EraseObject( renderer );
280 void RenderManager::AddSampler( OwnerPointer< Render::Sampler >& sampler )
282 mImpl->samplerContainer.PushBack( sampler.Release() );
285 void RenderManager::RemoveSampler( Render::Sampler* sampler )
287 mImpl->samplerContainer.EraseObject( sampler );
290 void RenderManager::AddTexture( OwnerPointer< Render::Texture >& texture )
292 texture->Initialize( mImpl->context );
293 mImpl->textureContainer.PushBack( texture.Release() );
296 void RenderManager::RemoveTexture( Render::Texture* texture )
298 DALI_ASSERT_DEBUG( NULL != texture );
300 // Find the texture, use reference to pointer so we can do the erase safely
301 for ( auto&& iter : mImpl->textureContainer )
303 if ( iter == texture )
305 texture->Destroy( mImpl->context );
306 mImpl->textureContainer.Erase( &iter ); // Texture found; now destroy it
312 void RenderManager::UploadTexture( Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params )
314 texture->Upload( mImpl->context, pixelData, params );
317 void RenderManager::GenerateMipmaps( Render::Texture* texture )
319 texture->GenerateMipmaps( mImpl->context );
322 void RenderManager::SetFilterMode( Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode )
324 sampler->mMinificationFilter = static_cast<Dali::FilterMode::Type>(minFilterMode);
325 sampler->mMagnificationFilter = static_cast<Dali::FilterMode::Type>(magFilterMode );
328 void RenderManager::SetWrapMode( Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode )
330 sampler->mRWrapMode = static_cast<Dali::WrapMode::Type>(rWrapMode);
331 sampler->mSWrapMode = static_cast<Dali::WrapMode::Type>(sWrapMode);
332 sampler->mTWrapMode = static_cast<Dali::WrapMode::Type>(tWrapMode);
335 void RenderManager::AddFrameBuffer( OwnerPointer< Render::FrameBuffer >& frameBuffer )
337 Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
338 mImpl->frameBufferContainer.PushBack( frameBufferPtr );
339 frameBufferPtr->Initialize( mImpl->context );
342 void RenderManager::RemoveFrameBuffer( Render::FrameBuffer* frameBuffer )
344 DALI_ASSERT_DEBUG( NULL != frameBuffer );
346 // Find the sampler, use reference so we can safely do the erase
347 for ( auto&& iter : mImpl->frameBufferContainer )
349 if ( iter == frameBuffer )
351 frameBuffer->Destroy( mImpl->context );
352 mImpl->frameBufferContainer.Erase( &iter ); // frameBuffer found; now destroy it
358 void RenderManager::InitializeScene( SceneGraph::Scene* scene )
360 scene->Initialize( *mImpl->CreateSceneContext() );
361 mImpl->sceneContainer.push_back( scene );
364 void RenderManager::UninitializeScene( SceneGraph::Scene* scene )
366 mImpl->DestroySceneContext( scene->GetContext() );
368 auto iter = std::find( mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene );
369 if( iter != mImpl->sceneContainer.end() )
371 mImpl->sceneContainer.erase( iter );
375 void RenderManager::SurfaceReplaced( SceneGraph::Scene* scene )
377 Context* newContext = mImpl->ReplaceSceneContext( scene->GetContext() );
378 scene->Initialize( *newContext );
381 void RenderManager::AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
383 frameBuffer->AttachColorTexture( mImpl->context, texture, mipmapLevel, layer );
386 void RenderManager::AttachDepthTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel )
388 frameBuffer->AttachDepthTexture( mImpl->context, texture, mipmapLevel );
391 void RenderManager::AttachDepthStencilTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel )
393 frameBuffer->AttachDepthStencilTexture( mImpl->context, texture, mipmapLevel );
396 void RenderManager::AddVertexBuffer( OwnerPointer< Render::VertexBuffer >& vertexBuffer )
398 mImpl->vertexBufferContainer.PushBack( vertexBuffer.Release() );
401 void RenderManager::RemoveVertexBuffer( Render::VertexBuffer* vertexBuffer )
403 mImpl->vertexBufferContainer.EraseObject( vertexBuffer );
406 void RenderManager::SetVertexBufferFormat( Render::VertexBuffer* vertexBuffer, OwnerPointer< Render::VertexBuffer::Format>& format )
408 vertexBuffer->SetFormat( format.Release() );
411 void RenderManager::SetVertexBufferData( Render::VertexBuffer* vertexBuffer, OwnerPointer< Vector<uint8_t> >& data, uint32_t size )
413 vertexBuffer->SetData( data.Release(), size );
416 void RenderManager::SetIndexBuffer( Render::Geometry* geometry, Dali::Vector<uint16_t>& indices )
418 geometry->SetIndexBuffer( indices );
421 void RenderManager::AddGeometry( OwnerPointer< Render::Geometry >& geometry )
423 mImpl->geometryContainer.PushBack( geometry.Release() );
426 void RenderManager::RemoveGeometry( Render::Geometry* geometry )
428 mImpl->geometryContainer.EraseObject( geometry );
431 void RenderManager::AttachVertexBuffer( Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer )
433 DALI_ASSERT_DEBUG( NULL != geometry );
436 for ( auto&& iter : mImpl->geometryContainer )
438 if ( iter == geometry )
440 iter->AddVertexBuffer( vertexBuffer );
446 void RenderManager::RemoveVertexBuffer( Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer )
448 DALI_ASSERT_DEBUG( NULL != geometry );
451 for ( auto&& iter : mImpl->geometryContainer )
453 if ( iter == geometry )
455 iter->RemoveVertexBuffer( vertexBuffer );
461 void RenderManager::SetGeometryType( Render::Geometry* geometry, uint32_t geometryType )
463 geometry->SetType( Render::Geometry::Type(geometryType) );
466 void RenderManager::AddRenderTracker( Render::RenderTracker* renderTracker )
468 mImpl->AddRenderTracker(renderTracker);
471 void RenderManager::RemoveRenderTracker( Render::RenderTracker* renderTracker )
473 mImpl->RemoveRenderTracker(renderTracker);
476 ProgramCache* RenderManager::GetProgramCache()
478 return &(mImpl->programController);
481 void RenderManager::PreRender( Integration::RenderStatus& status, bool forceClear, bool uploadOnly )
483 DALI_PRINT_RENDER_START( mImpl->renderBufferIndex );
485 // Core::Render documents that GL context must be current before calling Render
486 DALI_ASSERT_DEBUG( mImpl->context.IsGlContextCreated() );
488 // Increment the frame count at the beginning of each frame
491 // Process messages queued during previous update
492 mImpl->renderQueue.ProcessMessages( mImpl->renderBufferIndex );
495 for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i )
497 count += mImpl->sceneContainer[i]->GetRenderInstructions().Count( mImpl->renderBufferIndex );
500 const bool haveInstructions = count > 0u;
502 DALI_LOG_INFO( gLogFilter, Debug::General,
503 "Render: haveInstructions(%s) || mImpl->lastFrameWasRendered(%s) || forceClear(%s)\n",
504 haveInstructions ? "true" : "false",
505 mImpl->lastFrameWasRendered ? "true" : "false",
506 forceClear ? "true" : "false" );
508 // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
509 if( haveInstructions || mImpl->lastFrameWasRendered || forceClear )
511 DALI_LOG_INFO( gLogFilter, Debug::General, "Render: Processing\n" );
513 // Switch to the shared context
514 if ( mImpl->currentContext != &mImpl->context )
516 mImpl->currentContext = &mImpl->context;
518 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
520 mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
523 // Clear the current cached program when the context is switched
524 mImpl->programController.ClearCurrentProgram();
527 // Upload the geometries
528 for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i )
530 RenderInstructionContainer& instructions = mImpl->sceneContainer[i]->GetRenderInstructions();
531 for ( uint32_t j = 0; j < instructions.Count( mImpl->renderBufferIndex ); ++j )
533 RenderInstruction& instruction = instructions.At( mImpl->renderBufferIndex, j );
535 const Matrix* viewMatrix = instruction.GetViewMatrix( mImpl->renderBufferIndex );
536 const Matrix* projectionMatrix = instruction.GetProjectionMatrix( mImpl->renderBufferIndex );
538 DALI_ASSERT_DEBUG( viewMatrix );
539 DALI_ASSERT_DEBUG( projectionMatrix );
541 if( viewMatrix && projectionMatrix )
543 const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
545 // Iterate through each render list.
546 for( RenderListContainer::SizeType index = 0; index < renderListCount; ++index )
548 const RenderList* renderList = instruction.GetRenderList( index );
550 if( renderList && !renderList->IsEmpty() )
552 const std::size_t itemCount = renderList->Count();
553 for( uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex )
555 const RenderItem& item = renderList->GetItem( itemIndex );
556 if( DALI_LIKELY( item.mRenderer ) )
558 item.mRenderer->Upload( *mImpl->currentContext );
569 void RenderManager::PreRender( Integration::Scene& scene, std::vector<Rect<int>>& damagedRects )
571 if (mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
576 // @TODO We need to do partial rendering rotation.
577 if( mImpl->defaultSurfaceOrientation != 0 )
582 class DamagedRectsCleaner
585 DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects)
586 : mDamagedRects(damagedRects),
591 void SetCleanOnReturn(bool cleanOnReturn)
593 mCleanOnReturn = cleanOnReturn;
596 ~DamagedRectsCleaner()
600 mDamagedRects.clear();
605 std::vector<Rect<int>>& mDamagedRects;
609 Rect<int32_t> surfaceRect = Rect<int32_t>(0, 0, static_cast<int32_t>( scene.GetSize().width ), static_cast<int32_t>( scene.GetSize().height ));
611 // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
612 DamagedRectsCleaner damagedRectCleaner(damagedRects);
616 Internal::Scene& sceneInternal = GetImplementation(scene);
617 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
619 // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number.
620 // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end());
621 std::vector<DirtyRect>& itemsDirtyRects = sceneInternal.GetItemsDirtyRects();
622 for (DirtyRect& dirtyRect : itemsDirtyRects)
624 dirtyRect.visited = false;
627 uint32_t count = sceneObject->GetRenderInstructions().Count( mImpl->renderBufferIndex );
628 for (uint32_t i = 0; i < count; ++i)
630 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At( mImpl->renderBufferIndex, i );
632 if (instruction.mFrameBuffer)
634 return; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
637 const Camera* camera = instruction.GetCamera();
638 if (camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
640 const Node* node = instruction.GetCamera()->GetNode();
645 Quaternion orientation;
646 node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
648 Vector3 orientationAxis;
649 Radian orientationAngle;
650 orientation.ToAxisAngle( orientationAxis, orientationAngle );
652 if (position.x > Math::MACHINE_EPSILON_10000 ||
653 position.y > Math::MACHINE_EPSILON_10000 ||
654 orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
655 orientationAngle != ANGLE_180 ||
656 scale != Vector3(1.0f, 1.0f, 1.0f))
667 Rect<int32_t> viewportRect;
668 if (instruction.mIsViewportSet)
670 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
671 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
672 if (viewportRect.IsEmpty() || !viewportRect.IsValid())
674 return; // just skip funny use cases for now, empty viewport means it is set somewhere else
679 viewportRect = surfaceRect;
682 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
683 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
684 if (viewMatrix && projectionMatrix)
686 const RenderListContainer::SizeType count = instruction.RenderListCount();
687 for (RenderListContainer::SizeType index = 0u; index < count; ++index)
689 const RenderList* renderList = instruction.GetRenderList( index );
690 if (renderList && !renderList->IsEmpty())
692 const std::size_t count = renderList->Count();
693 for (uint32_t index = 0u; index < count; ++index)
695 RenderItem& item = renderList->GetItem( index );
696 // If the item does 3D transformation, do early exit and clean the damaged rect array
697 if (item.mUpdateSize == Vector3::ZERO)
703 DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect);
704 // If the item refers to updated node or renderer.
705 if (item.mIsUpdated ||
707 (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode)))))
709 item.mIsUpdated = false;
710 item.mNode->SetUpdated(false);
712 rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height);
713 if (rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
715 const int left = rect.x;
716 const int top = rect.y;
717 const int right = rect.x + rect.width;
718 const int bottom = rect.y + rect.height;
719 rect.x = (left / 16) * 16;
720 rect.y = (top / 16) * 16;
721 rect.width = ((right + 16) / 16) * 16 - rect.x;
722 rect.height = ((bottom + 16) / 16) * 16 - rect.y;
724 // Found valid dirty rect.
725 // 1. Insert it in the sorted array of the dirty rects.
726 // 2. Mark the related dirty rects as visited so they will not be removed below.
727 // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1).
728 dirtyRect.rect = rect;
729 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
730 dirtyRectPos = itemsDirtyRects.insert(dirtyRectPos, dirtyRect);
733 while (++dirtyRectPos != itemsDirtyRects.end())
735 if (dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
740 dirtyRectPos->visited = true;
741 Rect<int>& dirtRect = dirtyRectPos->rect;
742 rect.Merge(dirtRect);
745 if (c > 3) // no more then 3 previous rects
747 itemsDirtyRects.erase(dirtyRectPos);
752 damagedRects.push_back(rect);
757 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
758 // 2. Mark the related dirty rects as visited so they will not be removed below.
759 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
760 while (dirtyRectPos != itemsDirtyRects.end())
762 if (dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
767 dirtyRectPos->visited = true;
777 // Check removed nodes or removed renderers dirty rects
778 auto i = itemsDirtyRects.begin();
779 auto j = itemsDirtyRects.begin();
780 while (i != itemsDirtyRects.end())
788 Rect<int>& dirtRect = i->rect;
789 damagedRects.push_back(dirtRect);
794 itemsDirtyRects.resize(j - itemsDirtyRects.begin());
795 damagedRectCleaner.SetCleanOnReturn(false);
798 void RenderManager::RenderScene( Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo )
800 Rect<int> clippingRect;
801 RenderScene( status, scene, renderToFbo, clippingRect);
804 void RenderManager::RenderScene( Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect )
806 Internal::Scene& sceneInternal = GetImplementation( scene );
807 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
809 uint32_t count = sceneObject->GetRenderInstructions().Count( mImpl->renderBufferIndex );
811 for( uint32_t i = 0; i < count; ++i )
813 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At( mImpl->renderBufferIndex, i );
815 if ( ( renderToFbo && !instruction.mFrameBuffer ) || ( !renderToFbo && instruction.mFrameBuffer ) )
820 // Mark that we will require a post-render step to be performed (includes swap-buffers).
821 status.SetNeedsPostRender( true );
823 Rect<int32_t> viewportRect;
826 if ( instruction.mIsClearColorSet )
828 clearColor = instruction.mClearColor;
832 clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR;
835 Rect<int32_t> surfaceRect = mImpl->defaultSurfaceRect;
836 Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable;
837 Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
838 int surfaceOrientation = mImpl->defaultSurfaceOrientation;
840 if ( instruction.mFrameBuffer )
843 if ( mImpl->currentContext != &mImpl->context )
845 // Switch to shared context for off-screen buffer
846 mImpl->currentContext = &mImpl->context;
848 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
850 mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
853 // Clear the current cached program when the context is switched
854 mImpl->programController.ClearCurrentProgram();
859 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
861 if ( mImpl->currentContext != sceneObject->GetContext() )
863 // Switch the correct context if rendering to a surface
864 mImpl->currentContext = sceneObject->GetContext();
866 // Clear the current cached program when the context is switched
867 mImpl->programController.ClearCurrentProgram();
871 surfaceRect = Rect<int32_t>( 0, 0, static_cast<int32_t>( scene.GetSize().width ), static_cast<int32_t>( scene.GetSize().height ) );
874 // Make sure that GL context must be created
875 mImpl->currentContext->GlContextCreated();
877 // reset the program matrices for all programs once per frame
878 // this ensures we will set view and projection matrix once per program per camera
879 mImpl->programController.ResetProgramMatrices();
881 if( instruction.mFrameBuffer )
883 instruction.mFrameBuffer->Bind( *mImpl->currentContext );
885 // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
886 for (unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
888 mImpl->textureDependencyList.PushBack( instruction.mFrameBuffer->GetTextureId(i0) );
893 mImpl->currentContext->BindFramebuffer( GL_FRAMEBUFFER, 0u );
896 if ( !instruction.mFrameBuffer )
898 mImpl->currentContext->Viewport( surfaceRect.x,
901 surfaceRect.height );
904 // Clear the entire color, depth and stencil buffers for the default framebuffer, if required.
905 // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers
906 // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit",
907 // and then stall. That problem is only noticeable when rendering a large number of vertices per frame.
908 GLbitfield clearMask = GL_COLOR_BUFFER_BIT;
910 mImpl->currentContext->ColorMask( true );
912 if( depthBufferAvailable == Integration::DepthBufferAvailable::TRUE )
914 mImpl->currentContext->DepthMask( true );
915 clearMask |= GL_DEPTH_BUFFER_BIT;
918 if( stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
920 mImpl->currentContext->ClearStencil( 0 );
921 mImpl->currentContext->StencilMask( 0xFF ); // 8 bit stencil mask, all 1's
922 clearMask |= GL_STENCIL_BUFFER_BIT;
925 if( !instruction.mIgnoreRenderToFbo && ( instruction.mFrameBuffer != nullptr ) )
927 // Offscreen buffer rendering
928 if ( instruction.mIsViewportSet )
930 // For glViewport the lower-left corner is (0,0)
931 const int32_t y = ( instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height ) - instruction.mViewport.y;
932 viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height );
936 viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
938 surfaceOrientation = 0;
940 else // No Offscreen frame buffer rendering
942 // Check whether a viewport is specified, otherwise the full surface size is used
943 if ( instruction.mIsViewportSet )
945 // For glViewport the lower-left corner is (0,0)
946 const int32_t y = ( surfaceRect.height - instruction.mViewport.height ) - instruction.mViewport.y;
947 viewportRect.Set( instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height );
951 viewportRect = surfaceRect;
955 if( surfaceOrientation == 90 || surfaceOrientation == 270 )
957 int temp = viewportRect.width;
958 viewportRect.width = viewportRect.height;
959 viewportRect.height = temp;
962 bool clearFullFrameRect = true;
963 if( instruction.mFrameBuffer != nullptr )
965 Viewport frameRect( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
966 clearFullFrameRect = ( frameRect == viewportRect );
970 clearFullFrameRect = ( surfaceRect == viewportRect );
973 if (!clippingRect.IsEmpty())
975 if (!clippingRect.Intersect(viewportRect))
977 DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
978 clippingRect = Rect<int>();
980 clearFullFrameRect = false;
983 mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
985 if (instruction.mIsClearColorSet)
987 mImpl->currentContext->ClearColor(clearColor.r,
991 if (!clearFullFrameRect)
993 if (!clippingRect.IsEmpty())
995 mImpl->currentContext->SetScissorTest(true);
996 mImpl->currentContext->Scissor(clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
997 mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
998 mImpl->currentContext->SetScissorTest(false);
1002 mImpl->currentContext->SetScissorTest(true);
1003 mImpl->currentContext->Scissor(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
1004 mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
1005 mImpl->currentContext->SetScissorTest(false);
1010 mImpl->currentContext->SetScissorTest(false);
1011 mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
1015 // Clear the list of bound textures
1016 mImpl->boundTextures.Clear();
1018 mImpl->renderAlgorithms.ProcessRenderInstruction(
1020 *mImpl->currentContext,
1021 mImpl->renderBufferIndex,
1022 depthBufferAvailable,
1023 stencilBufferAvailable,
1024 mImpl->boundTextures,
1026 surfaceOrientation );
1028 // Synchronise the FBO/Texture access when there are multiple contexts
1029 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
1031 // Check whether any binded texture is in the dependency list
1032 bool textureFound = false;
1034 if ( mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u )
1036 for ( auto textureId : mImpl->textureDependencyList )
1039 textureFound = std::find_if( mImpl->boundTextures.Begin(), mImpl->boundTextures.End(),
1040 [textureId]( GLuint id )
1042 return textureId == id;
1043 } ) != mImpl->boundTextures.End();
1049 if ( instruction.mFrameBuffer )
1051 // For off-screen buffer
1053 // Wait until all rendering calls for the currently context are executed
1054 mImpl->glContextHelperAbstraction.WaitClient();
1056 // Clear the dependency list
1057 mImpl->textureDependencyList.Clear();
1061 // Worker thread lambda function
1062 auto& glContextHelperAbstraction = mImpl->glContextHelperAbstraction;
1063 auto workerFunction = [&glContextHelperAbstraction]( int workerThread )
1065 // Switch to the shared context in the worker thread
1066 glContextHelperAbstraction.MakeSurfacelessContextCurrent();
1068 // Wait until all rendering calls for the shared context are executed
1069 glContextHelperAbstraction.WaitClient();
1071 // Must clear the context in the worker thread
1072 // Otherwise the shared context cannot be switched to from the render thread
1073 glContextHelperAbstraction.MakeContextNull();
1076 auto future = mImpl->threadPool->SubmitTask( 0u, workerFunction );
1079 mImpl->threadPool->Wait();
1081 // Clear the dependency list
1082 mImpl->textureDependencyList.Clear();
1088 if( instruction.mRenderTracker && instruction.mFrameBuffer )
1090 // This will create a sync object every frame this render tracker
1091 // is alive (though it should be now be created only for
1092 // render-once render tasks)
1093 instruction.mRenderTracker->CreateSyncObject( mImpl->glSyncAbstraction );
1094 instruction.mRenderTracker = nullptr; // Only create once.
1099 mImpl->currentContext->Flush();
1103 GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
1104 mImpl->currentContext->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
1107 void RenderManager::PostRender( bool uploadOnly )
1111 if ( mImpl->currentContext->IsSurfacelessContextSupported() )
1113 mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent();
1116 GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
1117 mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
1120 //Notify RenderGeometries that rendering has finished
1121 for ( auto&& iter : mImpl->geometryContainer )
1123 iter->OnRenderFinished();
1126 mImpl->UpdateTrackers();
1129 uint32_t count = 0u;
1130 for( uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i )
1132 count += mImpl->sceneContainer[i]->GetRenderInstructions().Count( mImpl->renderBufferIndex );
1135 const bool haveInstructions = count > 0u;
1137 // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
1138 mImpl->lastFrameWasRendered = haveInstructions;
1141 * The rendering has finished; swap to the next buffer.
1142 * Ideally the update has just finished using this buffer; otherwise the render thread
1143 * should block until the update has finished.
1145 mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1147 DALI_PRINT_RENDER_END();
1150 } // namespace SceneGraph
1152 } // namespace Internal