2 * Copyright (c) 2021 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>
29 #include <dali/internal/event/common/scene-impl.h>
31 #include <dali/internal/update/common/scene-graph-scene.h>
32 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
34 #include <dali/internal/render/common/render-algorithms.h>
35 #include <dali/internal/render/common/render-debug.h>
36 #include <dali/internal/render/common/render-instruction.h>
37 #include <dali/internal/render/common/render-tracker.h>
38 #include <dali/internal/render/queue/render-queue.h>
39 #include <dali/internal/render/renderers/render-frame-buffer.h>
40 #include <dali/internal/render/renderers/render-texture.h>
41 #include <dali/internal/render/renderers/shader-cache.h>
42 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
43 #include <dali/internal/render/shaders/program-controller.h>
51 #if defined(DEBUG_ENABLED)
54 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
55 } // unnamed namespace
59 * Structure to contain internal data
61 struct RenderManager::Impl
63 Impl(Graphics::Controller& graphicsController,
64 Integration::DepthBufferAvailable depthBufferAvailableParam,
65 Integration::StencilBufferAvailable stencilBufferAvailableParam,
66 Integration::PartialUpdateAvailable partialUpdateAvailableParam)
67 : context(graphicsController.GetGlAbstraction(), &sceneContextContainer),
68 currentContext(&context),
69 graphicsController(graphicsController),
71 renderAlgorithms(graphicsController),
73 renderBufferIndex(SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX),
77 frameBufferContainer(),
78 lastFrameWasRendered(false),
79 programController(graphicsController),
80 shaderCache(graphicsController),
81 depthBufferAvailable(depthBufferAvailableParam),
82 stencilBufferAvailable(stencilBufferAvailableParam),
83 partialUpdateAvailable(partialUpdateAvailableParam)
85 // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
86 threadPool = std::unique_ptr<Dali::ThreadPool>(new Dali::ThreadPool());
87 threadPool->Initialize(1u);
89 uniformBufferManager.reset(new Render::UniformBufferManager(&graphicsController));
91 // initialize main command buffer
92 auto info = Graphics::CommandBufferCreateInfo().SetLevel(Graphics::CommandBufferLevel::PRIMARY);
93 mainCommandBuffer = graphicsController.CreateCommandBuffer(info, nullptr);
98 threadPool.reset(nullptr); // reset now to maintain correct destruction order
101 void AddRenderTracker(Render::RenderTracker* renderTracker)
103 DALI_ASSERT_DEBUG(renderTracker != NULL);
104 mRenderTrackers.PushBack(renderTracker);
107 void RemoveRenderTracker(Render::RenderTracker* renderTracker)
109 mRenderTrackers.EraseObject(renderTracker);
112 Context* CreateSceneContext()
114 Context* context = new Context(graphicsController.GetGlAbstraction());
115 sceneContextContainer.PushBack(context);
119 void DestroySceneContext(Context* sceneContext)
121 auto iter = std::find(sceneContextContainer.Begin(), sceneContextContainer.End(), sceneContext);
122 if(iter != sceneContextContainer.End())
124 (*iter)->GlContextDestroyed();
125 sceneContextContainer.Erase(iter);
129 Context* ReplaceSceneContext(Context* oldSceneContext)
131 Context* newContext = new Context(graphicsController.GetGlAbstraction());
133 oldSceneContext->GlContextDestroyed();
135 std::replace(sceneContextContainer.begin(), sceneContextContainer.end(), oldSceneContext, newContext);
139 void UpdateTrackers()
141 for(auto&& iter : mRenderTrackers)
143 iter->PollSyncObject();
147 // the order is important for destruction,
148 // programs are owned by context at the moment.
149 Context context; ///< Holds the GL state of the share resource context
150 Context* currentContext; ///< Holds the GL state of the current context for rendering
151 OwnerContainer<Context*> sceneContextContainer; ///< List of owned contexts holding the GL state per scene
152 Graphics::Controller& graphicsController;
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 OwnerContainer<Render::Renderer*> rendererContainer; ///< List of owned renderers
163 OwnerContainer<Render::Sampler*> samplerContainer; ///< List of owned samplers
164 OwnerContainer<Render::Texture*> textureContainer; ///< List of owned textures
165 OwnerContainer<Render::FrameBuffer*> frameBufferContainer; ///< List of owned framebuffers
166 OwnerContainer<Render::VertexBuffer*> vertexBufferContainer; ///< List of owned vertex buffers
167 OwnerContainer<Render::Geometry*> geometryContainer; ///< List of owned Geometries
169 bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions
171 OwnerContainer<Render::RenderTracker*> mRenderTrackers; ///< List of render trackers
173 ProgramController programController; ///< Owner of the GL programs
174 Render::ShaderCache shaderCache; ///< The cache for the graphics shaders
176 std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
178 Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available
179 Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
180 Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
182 std::unique_ptr<Dali::ThreadPool> threadPool; ///< The thread pool
183 Vector<Graphics::Texture*> boundTextures; ///< The textures bound for rendering
184 Vector<Graphics::Texture*> textureDependencyList; ///< The dependency list of binded textures
186 Graphics::UniquePtr<Graphics::CommandBuffer> mainCommandBuffer; ///< Main command buffer
188 Graphics::UniquePtr<Graphics::RenderPass> mainRenderPass; ///< Main renderpass
191 RenderManager* RenderManager::New(Graphics::Controller& graphicsController,
192 Integration::DepthBufferAvailable depthBufferAvailable,
193 Integration::StencilBufferAvailable stencilBufferAvailable,
194 Integration::PartialUpdateAvailable partialUpdateAvailable)
196 RenderManager* manager = new RenderManager;
197 manager->mImpl = new Impl(graphicsController,
198 depthBufferAvailable,
199 stencilBufferAvailable,
200 partialUpdateAvailable);
204 RenderManager::RenderManager()
209 RenderManager::~RenderManager()
214 RenderQueue& RenderManager::GetRenderQueue()
216 return mImpl->renderQueue;
219 void RenderManager::ContextCreated()
221 mImpl->context.GlContextCreated();
222 mImpl->programController.GlContextCreated();
224 // renderers, textures and gpu buffers cannot reinitialize themselves
225 // so they rely on someone reloading the data for them
228 void RenderManager::ContextDestroyed()
230 mImpl->context.GlContextDestroyed();
231 mImpl->programController.GlContextDestroyed();
234 for(auto&& texture : mImpl->textureContainer)
239 //Inform framebuffers
240 for(auto&& framebuffer : mImpl->frameBufferContainer)
242 framebuffer->Destroy();
246 for(auto&& renderer : mImpl->rendererContainer)
248 renderer->GlContextDestroyed();
252 for(auto&& context : mImpl->sceneContextContainer)
254 context->GlContextDestroyed();
258 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
260 mImpl->programController.SetShaderSaver(upstream);
263 void RenderManager::AddRenderer(OwnerPointer<Render::Renderer>& renderer)
265 // Initialize the renderer as we are now in render thread
266 renderer->Initialize(mImpl->context, mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()));
268 mImpl->rendererContainer.PushBack(renderer.Release());
271 void RenderManager::RemoveRenderer(Render::Renderer* renderer)
273 mImpl->rendererContainer.EraseObject(renderer);
276 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
278 sampler->Initialize(mImpl->graphicsController);
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->graphicsController);
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)
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(pixelData, params);
314 void RenderManager::GenerateMipmaps(Render::Texture* texture)
316 texture->GenerateMipmaps();
319 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
321 sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
322 static_cast<Dali::FilterMode::Type>(magFilterMode));
325 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
327 sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
328 static_cast<Dali::WrapMode::Type>(sWrapMode),
329 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->graphicsController);
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();
349 mImpl->frameBufferContainer.Erase(&iter); // frameBuffer found; now destroy it
356 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
358 scene->Initialize(*mImpl->CreateSceneContext(), mImpl->graphicsController);
359 mImpl->sceneContainer.push_back(scene);
362 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
364 mImpl->DestroySceneContext(scene->GetContext());
366 auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
367 if(iter != mImpl->sceneContainer.end())
369 mImpl->sceneContainer.erase(iter);
373 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
375 Context* newContext = mImpl->ReplaceSceneContext(scene->GetContext());
376 scene->Initialize(*newContext, mImpl->graphicsController);
379 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
381 frameBuffer->AttachColorTexture(texture, mipmapLevel, layer);
384 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
386 frameBuffer->AttachDepthTexture(texture, mipmapLevel);
389 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
391 frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel);
394 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
396 mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
399 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
401 mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
404 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
406 vertexBuffer->SetFormat(format.Release());
409 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
411 vertexBuffer->SetData(data.Release(), size);
414 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Dali::Vector<uint16_t>& indices)
416 geometry->SetIndexBuffer(indices);
419 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
421 mImpl->geometryContainer.PushBack(geometry.Release());
424 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
426 mImpl->geometryContainer.EraseObject(geometry);
429 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
431 DALI_ASSERT_DEBUG(NULL != geometry);
434 for(auto&& iter : mImpl->geometryContainer)
438 iter->AddVertexBuffer(vertexBuffer);
444 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
446 DALI_ASSERT_DEBUG(NULL != geometry);
449 for(auto&& iter : mImpl->geometryContainer)
453 iter->RemoveVertexBuffer(vertexBuffer);
459 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
461 geometry->SetType(Render::Geometry::Type(geometryType));
464 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
466 mImpl->AddRenderTracker(renderTracker);
469 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
471 mImpl->RemoveRenderTracker(renderTracker);
474 ProgramCache* RenderManager::GetProgramCache()
476 return &(mImpl->programController);
479 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear, bool uploadOnly)
481 DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
483 // Core::Render documents that GL context must be current before calling Render
484 DALI_ASSERT_DEBUG(mImpl->context.IsGlContextCreated());
486 // Increment the frame count at the beginning of each frame
489 // Process messages queued during previous update
490 mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
493 for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i)
495 count += mImpl->sceneContainer[i]->GetRenderInstructions().Count(mImpl->renderBufferIndex);
498 const bool haveInstructions = count > 0u;
500 // Reset main command buffer
501 mImpl->mainCommandBuffer->Reset();
503 DALI_LOG_INFO(gLogFilter, Debug::General, "Render: haveInstructions(%s) || mImpl->lastFrameWasRendered(%s) || forceClear(%s)\n", haveInstructions ? "true" : "false", mImpl->lastFrameWasRendered ? "true" : "false", 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 // Context switch now happens when the uploading happens in graphics side
516 // if(mImpl->currentContext->IsSurfacelessContextSupported())
518 // mImpl->graphicsController.GetGlContextHelperAbstraction().MakeSurfacelessContextCurrent();
521 // Clear the current cached program when the context is switched
522 mImpl->programController.ClearCurrentProgram();
525 // Upload the geometries
526 for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i)
528 RenderInstructionContainer& instructions = mImpl->sceneContainer[i]->GetRenderInstructions();
529 for(uint32_t j = 0; j < instructions.Count(mImpl->renderBufferIndex); ++j)
531 RenderInstruction& instruction = instructions.At(mImpl->renderBufferIndex, j);
533 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
534 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
536 DALI_ASSERT_DEBUG(viewMatrix);
537 DALI_ASSERT_DEBUG(projectionMatrix);
539 if(viewMatrix && projectionMatrix)
541 const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
543 // Iterate through each render list.
544 for(RenderListContainer::SizeType index = 0; index < renderListCount; ++index)
546 const RenderList* renderList = instruction.GetRenderList(index);
548 if(renderList && !renderList->IsEmpty())
550 const std::size_t itemCount = renderList->Count();
551 for(uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex)
553 const RenderItem& item = renderList->GetItem(itemIndex);
554 if(DALI_LIKELY(item.mRenderer))
556 item.mRenderer->Upload();
567 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
569 if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
574 Internal::Scene& sceneInternal = GetImplementation(scene);
575 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
577 if(sceneObject->IsRenderingSkipped())
579 // We don't need to calculate dirty rects
583 class DamagedRectsCleaner
586 DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects)
587 : mDamagedRects(damagedRects),
592 void SetCleanOnReturn(bool cleanOnReturn)
594 mCleanOnReturn = cleanOnReturn;
597 ~DamagedRectsCleaner()
601 mDamagedRects.clear();
606 std::vector<Rect<int>>& mDamagedRects;
610 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
612 // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
613 DamagedRectsCleaner damagedRectCleaner(damagedRects);
615 // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number.
616 // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end());
617 std::vector<DirtyRect>& itemsDirtyRects = sceneInternal.GetItemsDirtyRects();
618 for(DirtyRect& dirtyRect : itemsDirtyRects)
620 dirtyRect.visited = false;
623 uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
624 for(uint32_t i = 0; i < count; ++i)
626 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
628 if(instruction.mFrameBuffer)
630 return; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
633 const Camera* camera = instruction.GetCamera();
634 if(camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
636 const Node* node = instruction.GetCamera()->GetNode();
641 Quaternion orientation;
642 node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
644 Vector3 orientationAxis;
645 Radian orientationAngle;
646 orientation.ToAxisAngle(orientationAxis, orientationAngle);
648 if(position.x > Math::MACHINE_EPSILON_10000 ||
649 position.y > Math::MACHINE_EPSILON_10000 ||
650 orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
651 orientationAngle != ANGLE_180 ||
652 scale != Vector3(1.0f, 1.0f, 1.0f))
663 Rect<int32_t> viewportRect;
664 if(instruction.mIsViewportSet)
666 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
667 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
668 if(viewportRect.IsEmpty() || !viewportRect.IsValid())
670 return; // just skip funny use cases for now, empty viewport means it is set somewhere else
675 viewportRect = surfaceRect;
678 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
679 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
680 if(viewMatrix && projectionMatrix)
682 const RenderListContainer::SizeType count = instruction.RenderListCount();
683 for(RenderListContainer::SizeType index = 0u; index < count; ++index)
685 const RenderList* renderList = instruction.GetRenderList(index);
686 if(renderList && !renderList->IsEmpty())
688 const std::size_t count = renderList->Count();
689 for(uint32_t index = 0u; index < count; ++index)
691 RenderItem& item = renderList->GetItem(index);
692 // If the item does 3D transformation, do early exit and clean the damaged rect array
693 if(item.mUpdateSize == Vector3::ZERO)
699 DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect);
700 // If the item refers to updated node or renderer.
701 if(item.mIsUpdated ||
703 (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode)))))
705 item.mIsUpdated = false;
706 item.mNode->SetUpdated(false);
708 rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height);
709 if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
711 const int left = rect.x;
712 const int top = rect.y;
713 const int right = rect.x + rect.width;
714 const int bottom = rect.y + rect.height;
715 rect.x = (left / 16) * 16;
716 rect.y = (top / 16) * 16;
717 rect.width = ((right + 16) / 16) * 16 - rect.x;
718 rect.height = ((bottom + 16) / 16) * 16 - rect.y;
720 // Found valid dirty rect.
721 // 1. Insert it in the sorted array of the dirty rects.
722 // 2. Mark the related dirty rects as visited so they will not be removed below.
723 // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1).
724 dirtyRect.rect = rect;
725 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
726 dirtyRectPos = itemsDirtyRects.insert(dirtyRectPos, dirtyRect);
729 while(++dirtyRectPos != itemsDirtyRects.end())
731 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
736 dirtyRectPos->visited = true;
737 Rect<int>& dirtRect = dirtyRectPos->rect;
738 rect.Merge(dirtRect);
741 if(c > 3) // no more then 3 previous rects
743 itemsDirtyRects.erase(dirtyRectPos);
748 damagedRects.push_back(rect);
753 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
754 // 2. Mark the related dirty rects as visited so they will not be removed below.
755 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
756 while(dirtyRectPos != itemsDirtyRects.end())
758 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
763 dirtyRectPos->visited = true;
773 // Check removed nodes or removed renderers dirty rects
774 auto i = itemsDirtyRects.begin();
775 auto j = itemsDirtyRects.begin();
776 while(i != itemsDirtyRects.end())
784 Rect<int>& dirtRect = i->rect;
785 damagedRects.push_back(dirtRect);
790 itemsDirtyRects.resize(j - itemsDirtyRects.begin());
791 damagedRectCleaner.SetCleanOnReturn(false);
794 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
796 Rect<int> clippingRect;
797 RenderScene(status, scene, renderToFbo, clippingRect);
800 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
802 // Reset main algorithms command buffer
803 mImpl->renderAlgorithms.ResetCommandBuffer();
805 auto mainCommandBuffer = mImpl->renderAlgorithms.GetMainCommandBuffer();
807 Internal::Scene& sceneInternal = GetImplementation(scene);
808 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
810 uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
812 std::vector<Graphics::RenderTarget*> targetstoPresent;
814 for(uint32_t i = 0; i < count; ++i)
816 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
818 if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
823 // Mark that we will require a post-render step to be performed (includes swap-buffers).
824 status.SetNeedsPostRender(true);
826 Rect<int32_t> viewportRect;
829 if(instruction.mIsClearColorSet)
831 clearColor = instruction.mClearColor;
835 clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR;
838 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
839 int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation();
841 Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable;
842 Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
844 Graphics::RenderTarget* currentRenderTarget = nullptr;
845 Graphics::RenderPass* currentRenderPass = nullptr;
846 std::vector<Graphics::ClearValue> currentClearValues {};
848 if(instruction.mFrameBuffer)
850 // Ensure graphics framebuffer is created, bind atachments and create render passes
851 // Only happens once per framebuffer. If the create fails, e.g. no attachments yet,
852 // then don't render to this framebuffer.
853 if(!instruction.mFrameBuffer->GetGraphicsObject())
855 const bool created = instruction.mFrameBuffer->CreateGraphicsObjects();
862 auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues();
864 // Set the clear color for first color attachment
865 if(instruction.mIsClearColorSet && clearValues.size() > 0)
867 clearValues[0].color = {
868 instruction.mClearColor.r,
869 instruction.mClearColor.g,
870 instruction.mClearColor.b,
871 instruction.mClearColor.a};
874 currentClearValues = clearValues;
876 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
879 currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget();
880 currentRenderPass = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
882 if(mImpl->currentContext != &mImpl->context)
884 // Switch to shared context for off-screen buffer
885 mImpl->currentContext = &mImpl->context;
887 // Context switch now happens when render pass starts
888 // if(mImpl->currentContext->IsSurfacelessContextSupported())
890 // mImpl->graphicsController.GetGlContextHelperAbstraction().MakeSurfacelessContextCurrent();
893 // Clear the current cached program when the context is switched
894 mImpl->programController.ClearCurrentProgram();
899 if(mImpl->currentContext->IsSurfacelessContextSupported())
901 if(mImpl->currentContext != sceneObject->GetContext())
903 // Switch the correct context if rendering to a surface
904 mImpl->currentContext = sceneObject->GetContext();
906 // Clear the current cached program when the context is switched
907 mImpl->programController.ClearCurrentProgram();
912 auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues();
914 if(instruction.mIsClearColorSet)
916 clearValues[0].color = {
917 instruction.mClearColor.r,
918 instruction.mClearColor.g,
919 instruction.mClearColor.b,
920 instruction.mClearColor.a};
923 currentClearValues = clearValues;
925 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
927 currentRenderTarget = sceneObject->GetSurfaceRenderTarget();
928 currentRenderPass = sceneObject->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
931 targetstoPresent.emplace_back( currentRenderTarget );
933 // Make sure that GL context must be created
934 mImpl->currentContext->GlContextCreated();
936 // reset the program matrices for all programs once per frame
937 // this ensures we will set view and projection matrix once per program per camera
938 mImpl->programController.ResetProgramMatrices();
940 if(instruction.mFrameBuffer)
942 // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
943 for(unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
945 mImpl->textureDependencyList.PushBack(instruction.mFrameBuffer->GetTexture(i0));
950 //mImpl->currentContext->BindFramebuffer(GL_FRAMEBUFFER, 0u);
953 // @todo Should this be a command in it's own right?
955 if(!instruction.mFrameBuffer)
958 mImpl->currentContext->Viewport(surfaceRect.x,
965 mainCommandBuffer->SetViewport( {surfaceRect.x,
968 surfaceRect.height} );
972 // Clear the entire color, depth and stencil buffers for the default framebuffer, if required.
973 // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers
974 // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit",
975 // and then stall. That problem is only noticeable when rendering a large number of vertices per frame.
978 GLbitfield clearMask = GL_COLOR_BUFFER_BIT;
980 mImpl->currentContext->ColorMask(true);
982 if(depthBufferAvailable == Integration::DepthBufferAvailable::TRUE)
984 mImpl->currentContext->DepthMask(true);
985 clearMask |= GL_DEPTH_BUFFER_BIT;
988 if(stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
990 mImpl->currentContext->ClearStencil(0);
991 mImpl->currentContext->StencilMask(0xFF); // 8 bit stencil mask, all 1's
992 clearMask |= GL_STENCIL_BUFFER_BIT;
995 if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
997 // Offscreen buffer rendering
998 if(instruction.mIsViewportSet)
1000 // For glViewport the lower-left corner is (0,0)
1001 const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
1002 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
1006 viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
1008 surfaceOrientation = 0;
1010 else // No Offscreen frame buffer rendering
1012 // Check whether a viewport is specified, otherwise the full surface size is used
1013 if(instruction.mIsViewportSet)
1015 // For glViewport the lower-left corner is (0,0)
1016 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
1017 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
1021 viewportRect = surfaceRect;
1025 // Set surface orientation
1026 mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
1028 /*** Clear region of framebuffer or surface before drawing ***/
1030 bool clearFullFrameRect = true;
1031 if(instruction.mFrameBuffer != nullptr)
1033 Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
1034 clearFullFrameRect = (frameRect == viewportRect);
1038 clearFullFrameRect = (surfaceRect == viewportRect);
1041 if(!clippingRect.IsEmpty())
1043 if(!clippingRect.Intersect(viewportRect))
1045 DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
1046 clippingRect = Rect<int>();
1048 clearFullFrameRect = false;
1051 // Begin render pass
1052 mainCommandBuffer->BeginRenderPass(
1054 currentRenderTarget,
1056 viewportRect.x, viewportRect.y,
1057 uint32_t(viewportRect.width), uint32_t(viewportRect.height)
1059 currentClearValues);
1061 // @todo The following block should be a command in it's own right.
1062 // Currently takes account of surface orientation in Context.
1063 // Or move entirely to RenderPass implementation
1064 mainCommandBuffer->SetViewport({float(viewportRect.x),
1065 float(viewportRect.y),
1066 float(viewportRect.width),
1067 float(viewportRect.height)});
1069 //mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
1070 if(instruction.mIsClearColorSet)
1072 if(!clearFullFrameRect)
1074 if(!clippingRect.IsEmpty())
1076 Graphics::Rect2D scissorArea = {clippingRect.x, clippingRect.y, uint32_t(clippingRect.width), uint32_t(clippingRect.height)};
1077 mainCommandBuffer->SetScissor( scissorArea );
1078 mainCommandBuffer->SetScissorTestEnable( true );
1079 //mImpl->currentContext->SetScissorTest(true);
1080 //mImpl->currentContext->Scissor(clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
1081 //mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
1082 //mImpl->currentContext->SetScissorTest(false);
1086 //mImpl->currentContext->SetScissorTest(true);
1087 //mImpl->currentContext->Scissor(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
1088 //mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
1089 //mImpl->currentContext->SetScissorTest(false);
1094 //mImpl->currentContext->SetScissorTest(false);
1095 //mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
1099 // Clear the list of bound textures
1100 mImpl->boundTextures.Clear();
1102 mImpl->renderAlgorithms.ProcessRenderInstruction(
1104 *mImpl->currentContext,
1105 mImpl->renderBufferIndex,
1106 depthBufferAvailable,
1107 stencilBufferAvailable,
1108 mImpl->boundTextures,
1111 surfaceOrientation);
1113 // Synchronise the FBO/Texture access when there are multiple contexts
1114 if(mImpl->currentContext->IsSurfacelessContextSupported())
1116 // Check whether any bound texture is in the dependency list
1117 bool textureFound = false;
1119 if(mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u)
1121 for(auto texture : mImpl->textureDependencyList)
1123 textureFound = std::find_if(mImpl->boundTextures.Begin(), mImpl->boundTextures.End(), [texture](Graphics::Texture* graphicsTexture) {
1124 return texture == graphicsTexture;
1125 }) != mImpl->boundTextures.End();
1131 if(instruction.mFrameBuffer)
1133 // For off-screen buffer
1135 // Wait until all rendering calls for the currently context are executed
1136 // mImpl->graphicsController.GetGlContextHelperAbstraction().WaitClient();
1138 // Clear the dependency list
1139 mImpl->textureDependencyList.Clear();
1143 // Worker thread lambda function
1144 auto& glContextHelperAbstraction = mImpl->graphicsController.GetGlContextHelperAbstraction();
1145 auto workerFunction = [&glContextHelperAbstraction](int workerThread) {
1146 // Switch to the shared context in the worker thread
1147 glContextHelperAbstraction.MakeSurfacelessContextCurrent();
1149 // Wait until all rendering calls for the shared context are executed
1150 glContextHelperAbstraction.WaitClient();
1152 // Must clear the context in the worker thread
1153 // Otherwise the shared context cannot be switched to from the render thread
1154 glContextHelperAbstraction.MakeContextNull();
1157 auto future = mImpl->threadPool->SubmitTask(0u, workerFunction);
1160 mImpl->threadPool->Wait();
1162 // Clear the dependency list
1163 mImpl->textureDependencyList.Clear();
1169 if(instruction.mRenderTracker && instruction.mFrameBuffer)
1171 // This will create a sync object every frame this render tracker
1172 // is alive (though it should be now be created only for
1173 // render-once render tasks)
1174 instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController.GetGlSyncAbstraction());
1175 instruction.mRenderTracker = nullptr; // Only create once.
1178 // This now happens when the render pass for frame buffer finishes
1181 // mImpl->currentContext->Flush();
1185 mainCommandBuffer->EndRenderPass();
1187 mImpl->renderAlgorithms.SubmitCommandBuffer();
1189 std::sort( targetstoPresent.begin(), targetstoPresent.end() );
1191 Graphics::RenderTarget* rt = nullptr;
1192 for( auto& target : targetstoPresent )
1196 mImpl->graphicsController.PresentRenderTarget(target);
1201 GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
1202 mImpl->currentContext->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
1205 void RenderManager::PostRender(bool uploadOnly)
1207 // Submit main command buffer
1208 //mImpl->renderAlgorithms.SubmitCommandBuffer();
1212 // Context switch now happens outside the render manager
1213 // if(mImpl->currentContext->IsSurfacelessContextSupported())
1215 // mImpl->graphicsController.GetGlContextHelperAbstraction().MakeSurfacelessContextCurrent();
1218 GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
1219 mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
1222 //Notify RenderGeometries that rendering has finished
1223 for(auto&& iter : mImpl->geometryContainer)
1225 iter->OnRenderFinished();
1228 mImpl->UpdateTrackers();
1230 uint32_t count = 0u;
1231 for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i)
1233 count += mImpl->sceneContainer[i]->GetRenderInstructions().Count(mImpl->renderBufferIndex);
1236 const bool haveInstructions = count > 0u;
1238 // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
1239 mImpl->lastFrameWasRendered = haveInstructions;
1242 * The rendering has finished; swap to the next buffer.
1243 * Ideally the update has just finished using this buffer; otherwise the render thread
1244 * should block until the update has finished.
1246 mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1248 DALI_PRINT_RENDER_END();
1251 } // namespace SceneGraph
1253 } // namespace Internal