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/renderers/uniform-buffer-view-pool.h>
44 #include <dali/internal/render/shaders/program-controller.h>
46 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
54 #if defined(DEBUG_ENABLED)
57 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
58 } // unnamed namespace
62 * Structure to contain internal data
64 struct RenderManager::Impl
66 Impl(Graphics::Controller& graphicsController,
67 Integration::DepthBufferAvailable depthBufferAvailableParam,
68 Integration::StencilBufferAvailable stencilBufferAvailableParam,
69 Integration::PartialUpdateAvailable partialUpdateAvailableParam)
70 : graphicsController(graphicsController),
72 renderAlgorithms(graphicsController),
74 renderBufferIndex(SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX),
78 frameBufferContainer(),
79 lastFrameWasRendered(false),
80 programController(graphicsController),
81 shaderCache(graphicsController),
82 depthBufferAvailable(depthBufferAvailableParam),
83 stencilBufferAvailable(stencilBufferAvailableParam),
84 partialUpdateAvailable(partialUpdateAvailableParam)
86 // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
87 threadPool = std::unique_ptr<Dali::ThreadPool>(new Dali::ThreadPool());
88 threadPool->Initialize(1u);
90 uniformBufferManager.reset(new Render::UniformBufferManager(&graphicsController));
95 threadPool.reset(nullptr); // reset now to maintain correct destruction order
98 void AddRenderTracker(Render::RenderTracker* renderTracker)
100 DALI_ASSERT_DEBUG(renderTracker != nullptr);
101 mRenderTrackers.PushBack(renderTracker);
104 void RemoveRenderTracker(Render::RenderTracker* renderTracker)
106 mRenderTrackers.EraseObject(renderTracker);
109 void UpdateTrackers()
111 for(auto&& iter : mRenderTrackers)
113 iter->PollSyncObject();
117 // the order is important for destruction,
118 Graphics::Controller& graphicsController;
119 RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread.
121 std::vector<SceneGraph::Scene*> sceneContainer; ///< List of pointers to the scene graph objects of the scenes
123 Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
125 uint32_t frameCount; ///< The current frame count
126 BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer
128 OwnerContainer<Render::Renderer*> rendererContainer; ///< List of owned renderers
129 OwnerContainer<Render::Sampler*> samplerContainer; ///< List of owned samplers
130 OwnerContainer<Render::Texture*> textureContainer; ///< List of owned textures
131 OwnerContainer<Render::FrameBuffer*> frameBufferContainer; ///< List of owned framebuffers
132 OwnerContainer<Render::VertexBuffer*> vertexBufferContainer; ///< List of owned vertex buffers
133 OwnerContainer<Render::Geometry*> geometryContainer; ///< List of owned Geometries
135 bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions
137 OwnerContainer<Render::RenderTracker*> mRenderTrackers; ///< List of render trackers
139 ProgramController programController; ///< Owner of the GL programs
140 Render::ShaderCache shaderCache; ///< The cache for the graphics shaders
142 std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
144 Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available
145 Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
146 Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
148 std::unique_ptr<Dali::ThreadPool> threadPool; ///< The thread pool
149 Vector<Graphics::Texture*> boundTextures; ///< The textures bound for rendering
150 Vector<Graphics::Texture*> textureDependencyList; ///< The dependency list of bound textures
153 RenderManager* RenderManager::New(Graphics::Controller& graphicsController,
154 Integration::DepthBufferAvailable depthBufferAvailable,
155 Integration::StencilBufferAvailable stencilBufferAvailable,
156 Integration::PartialUpdateAvailable partialUpdateAvailable)
158 auto* manager = new RenderManager;
159 manager->mImpl = new Impl(graphicsController,
160 depthBufferAvailable,
161 stencilBufferAvailable,
162 partialUpdateAvailable);
166 RenderManager::RenderManager()
171 RenderManager::~RenderManager()
176 RenderQueue& RenderManager::GetRenderQueue()
178 return mImpl->renderQueue;
181 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
185 void RenderManager::AddRenderer(OwnerPointer<Render::Renderer>& renderer)
187 // Initialize the renderer as we are now in render thread
188 renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()));
190 mImpl->rendererContainer.PushBack(renderer.Release());
193 void RenderManager::RemoveRenderer(Render::Renderer* renderer)
195 mImpl->rendererContainer.EraseObject(renderer);
198 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
200 sampler->Initialize(mImpl->graphicsController);
201 mImpl->samplerContainer.PushBack(sampler.Release());
204 void RenderManager::RemoveSampler(Render::Sampler* sampler)
206 mImpl->samplerContainer.EraseObject(sampler);
209 void RenderManager::AddTexture(OwnerPointer<Render::Texture>& texture)
211 texture->Initialize(mImpl->graphicsController);
212 mImpl->textureContainer.PushBack(texture.Release());
215 void RenderManager::RemoveTexture(Render::Texture* texture)
217 DALI_ASSERT_DEBUG(NULL != texture);
219 // Find the texture, use reference to pointer so we can do the erase safely
220 for(auto&& iter : mImpl->textureContainer)
225 mImpl->textureContainer.Erase(&iter); // Texture found; now destroy it
231 void RenderManager::UploadTexture(Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params)
233 texture->Upload(pixelData, params);
236 void RenderManager::GenerateMipmaps(Render::Texture* texture)
238 texture->GenerateMipmaps();
241 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
243 sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
244 static_cast<Dali::FilterMode::Type>(magFilterMode));
247 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
249 sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
250 static_cast<Dali::WrapMode::Type>(sWrapMode),
251 static_cast<Dali::WrapMode::Type>(tWrapMode));
254 void RenderManager::AddFrameBuffer(OwnerPointer<Render::FrameBuffer>& frameBuffer)
256 Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
257 mImpl->frameBufferContainer.PushBack(frameBufferPtr);
258 frameBufferPtr->Initialize(mImpl->graphicsController);
261 void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
263 DALI_ASSERT_DEBUG(nullptr != frameBuffer);
265 // Find the sampler, use reference so we can safely do the erase
266 for(auto&& iter : mImpl->frameBufferContainer)
268 if(iter == frameBuffer)
270 frameBuffer->Destroy();
271 mImpl->frameBufferContainer.Erase(&iter); // frameBuffer found; now destroy it
278 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
280 scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
281 mImpl->sceneContainer.push_back(scene);
284 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
286 auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
287 if(iter != mImpl->sceneContainer.end())
289 mImpl->sceneContainer.erase(iter);
293 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
295 scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
298 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
300 frameBuffer->AttachColorTexture(texture, mipmapLevel, layer);
303 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
305 frameBuffer->AttachDepthTexture(texture, mipmapLevel);
308 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
310 frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel);
313 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
315 mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
318 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
320 mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
323 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
325 vertexBuffer->SetFormat(format.Release());
328 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
330 vertexBuffer->SetData(data.Release(), size);
333 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Dali::Vector<uint16_t>& indices)
335 geometry->SetIndexBuffer(indices);
338 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
340 mImpl->geometryContainer.PushBack(geometry.Release());
343 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
345 mImpl->geometryContainer.EraseObject(geometry);
348 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
350 DALI_ASSERT_DEBUG(nullptr != geometry);
353 for(auto&& iter : mImpl->geometryContainer)
357 iter->AddVertexBuffer(vertexBuffer);
363 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
365 DALI_ASSERT_DEBUG(nullptr != geometry);
368 for(auto&& iter : mImpl->geometryContainer)
372 iter->RemoveVertexBuffer(vertexBuffer);
378 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
380 geometry->SetType(Render::Geometry::Type(geometryType));
383 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
385 mImpl->AddRenderTracker(renderTracker);
388 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
390 mImpl->RemoveRenderTracker(renderTracker);
393 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear, bool uploadOnly)
395 DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
398 mImpl->uniformBufferManager->GetUniformBufferViewPool(mImpl->renderBufferIndex)->Rollback();
400 // Increment the frame count at the beginning of each frame
403 // Process messages queued during previous update
404 mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
407 for(auto& i : mImpl->sceneContainer)
409 count += i->GetRenderInstructions().Count(mImpl->renderBufferIndex);
412 const bool haveInstructions = count > 0u;
414 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");
416 // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
417 if(haveInstructions || mImpl->lastFrameWasRendered || forceClear)
419 DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n");
421 // Upload the geometries
422 for(auto& i : mImpl->sceneContainer)
424 RenderInstructionContainer& instructions = i->GetRenderInstructions();
425 for(uint32_t j = 0; j < instructions.Count(mImpl->renderBufferIndex); ++j)
427 RenderInstruction& instruction = instructions.At(mImpl->renderBufferIndex, j);
429 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
430 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
432 DALI_ASSERT_DEBUG(viewMatrix);
433 DALI_ASSERT_DEBUG(projectionMatrix);
435 if(viewMatrix && projectionMatrix)
437 const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
439 // Iterate through each render list.
440 for(RenderListContainer::SizeType index = 0; index < renderListCount; ++index)
442 const RenderList* renderList = instruction.GetRenderList(index);
444 if(renderList && !renderList->IsEmpty())
446 const std::size_t itemCount = renderList->Count();
447 for(uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex)
449 const RenderItem& item = renderList->GetItem(itemIndex);
450 if(DALI_LIKELY(item.mRenderer))
452 item.mRenderer->Upload();
463 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
465 if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
470 Internal::Scene& sceneInternal = GetImplementation(scene);
471 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
473 if(sceneObject->IsRenderingSkipped())
475 // We don't need to calculate dirty rects
479 class DamagedRectsCleaner
482 explicit DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects)
483 : mDamagedRects(damagedRects),
488 void SetCleanOnReturn(bool cleanOnReturn)
490 mCleanOnReturn = cleanOnReturn;
493 ~DamagedRectsCleaner()
497 mDamagedRects.clear();
502 std::vector<Rect<int>>& mDamagedRects;
506 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
508 // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
509 DamagedRectsCleaner damagedRectCleaner(damagedRects);
511 // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number.
512 // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end());
513 std::vector<DirtyRect>& itemsDirtyRects = sceneInternal.GetItemsDirtyRects();
514 for(DirtyRect& dirtyRect : itemsDirtyRects)
516 dirtyRect.visited = false;
519 uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
520 for(uint32_t i = 0; i < instructionCount; ++i)
522 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
524 if(instruction.mFrameBuffer)
526 return; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
529 const Camera* camera = instruction.GetCamera();
530 if(camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
532 const Node* node = instruction.GetCamera()->GetNode();
537 Quaternion orientation;
538 node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
540 Vector3 orientationAxis;
541 Radian orientationAngle;
542 orientation.ToAxisAngle(orientationAxis, orientationAngle);
544 if(position.x > Math::MACHINE_EPSILON_10000 ||
545 position.y > Math::MACHINE_EPSILON_10000 ||
546 orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
547 orientationAngle != ANGLE_180 ||
548 scale != Vector3(1.0f, 1.0f, 1.0f))
559 Rect<int32_t> viewportRect;
560 if(instruction.mIsViewportSet)
562 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
563 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
564 if(viewportRect.IsEmpty() || !viewportRect.IsValid())
566 return; // just skip funny use cases for now, empty viewport means it is set somewhere else
571 viewportRect = surfaceRect;
574 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
575 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
576 if(viewMatrix && projectionMatrix)
578 const RenderListContainer::SizeType count = instruction.RenderListCount();
579 for(RenderListContainer::SizeType index = 0u; index < count; ++index)
581 const RenderList* renderList = instruction.GetRenderList(index);
582 if(renderList && !renderList->IsEmpty())
584 const std::size_t listCount = renderList->Count();
585 for(uint32_t listIndex = 0u; listIndex < listCount; ++listIndex)
587 RenderItem& item = renderList->GetItem(listIndex);
588 // If the item does 3D transformation, do early exit and clean the damaged rect array
589 if(item.mUpdateSize == Vector3::ZERO)
595 DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect);
596 // If the item refers to updated node or renderer.
597 if(item.mIsUpdated ||
599 (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode)))))
601 item.mIsUpdated = false;
602 item.mNode->SetUpdated(false);
604 rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height);
605 if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
607 const int left = rect.x;
608 const int top = rect.y;
609 const int right = rect.x + rect.width;
610 const int bottom = rect.y + rect.height;
611 rect.x = (left / 16) * 16;
612 rect.y = (top / 16) * 16;
613 rect.width = ((right + 16) / 16) * 16 - rect.x;
614 rect.height = ((bottom + 16) / 16) * 16 - rect.y;
616 // Found valid dirty rect.
617 // 1. Insert it in the sorted array of the dirty rects.
618 // 2. Mark the related dirty rects as visited so they will not be removed below.
619 // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1).
620 dirtyRect.rect = rect;
621 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
622 dirtyRectPos = itemsDirtyRects.insert(dirtyRectPos, dirtyRect);
625 while(++dirtyRectPos != itemsDirtyRects.end())
627 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
632 dirtyRectPos->visited = true;
633 Rect<int>& dirtRect = dirtyRectPos->rect;
634 rect.Merge(dirtRect);
637 if(c > 3) // no more then 3 previous rects
639 itemsDirtyRects.erase(dirtyRectPos);
644 damagedRects.push_back(rect);
649 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
650 // 2. Mark the related dirty rects as visited so they will not be removed below.
651 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
652 while(dirtyRectPos != itemsDirtyRects.end())
654 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
659 dirtyRectPos->visited = true;
669 // Check removed nodes or removed renderers dirty rects
670 auto i = itemsDirtyRects.begin();
671 auto j = itemsDirtyRects.begin();
672 while(i != itemsDirtyRects.end())
680 Rect<int>& dirtRect = i->rect;
681 damagedRects.push_back(dirtRect);
686 itemsDirtyRects.resize(j - itemsDirtyRects.begin());
687 damagedRectCleaner.SetCleanOnReturn(false);
690 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
692 Rect<int> clippingRect;
693 RenderScene(status, scene, renderToFbo, clippingRect);
696 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
698 // Reset main algorithms command buffer
699 mImpl->renderAlgorithms.ResetCommandBuffer();
701 auto mainCommandBuffer = mImpl->renderAlgorithms.GetMainCommandBuffer();
703 Internal::Scene& sceneInternal = GetImplementation(scene);
704 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
706 uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
708 std::vector<Graphics::RenderTarget*> targetstoPresent;
710 for(uint32_t i = 0; i < count; ++i)
712 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
714 if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
719 // Mark that we will require a post-render step to be performed (includes swap-buffers).
720 status.SetNeedsPostRender(true);
722 Rect<int32_t> viewportRect;
724 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
725 int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation();
727 // @todo Should these be part of scene?
728 Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable;
729 Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
731 Graphics::RenderTarget* currentRenderTarget = nullptr;
732 Graphics::RenderPass* currentRenderPass = nullptr;
733 std::vector<Graphics::ClearValue> currentClearValues{};
735 if(instruction.mFrameBuffer)
737 // Ensure graphics framebuffer is created, bind attachments and create render passes
738 // Only happens once per framebuffer. If the create fails, e.g. no attachments yet,
739 // then don't render to this framebuffer.
740 if(!instruction.mFrameBuffer->GetGraphicsObject())
742 const bool created = instruction.mFrameBuffer->CreateGraphicsObjects();
749 auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues();
751 // Set the clear color for first color attachment
752 if(instruction.mIsClearColorSet && !clearValues.empty())
754 clearValues[0].color = {
755 instruction.mClearColor.r,
756 instruction.mClearColor.g,
757 instruction.mClearColor.b,
758 instruction.mClearColor.a};
761 currentClearValues = clearValues;
763 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
766 currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget();
767 currentRenderPass = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
769 else // no framebuffer
772 auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues();
774 if(instruction.mIsClearColorSet)
776 clearValues[0].color = {
777 instruction.mClearColor.r,
778 instruction.mClearColor.g,
779 instruction.mClearColor.b,
780 instruction.mClearColor.a};
783 currentClearValues = clearValues;
785 // @todo SceneObject should already have the depth clear / stencil clear in the clearValues array.
786 // if the window has a depth/stencil buffer.
787 if((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
788 stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) &&
789 (currentClearValues.size() <= 1))
791 currentClearValues.emplace_back();
792 currentClearValues.back().depthStencil.depth = 0;
793 currentClearValues.back().depthStencil.stencil = 0;
796 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
798 currentRenderTarget = sceneObject->GetSurfaceRenderTarget();
799 currentRenderPass = sceneObject->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
802 targetstoPresent.emplace_back(currentRenderTarget);
804 // reset the program matrices for all programs once per frame
805 // this ensures we will set view and projection matrix once per program per camera
806 mImpl->programController.ResetProgramMatrices();
808 if(instruction.mFrameBuffer)
810 // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
811 for(unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
813 mImpl->textureDependencyList.PushBack(instruction.mFrameBuffer->GetTexture(i0));
817 if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
819 // Offscreen buffer rendering
820 if(instruction.mIsViewportSet)
822 // For glViewport the lower-left corner is (0,0)
823 const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
824 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
828 viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
830 surfaceOrientation = 0;
832 else // No Offscreen frame buffer rendering
834 // Check whether a viewport is specified, otherwise the full surface size is used
835 if(instruction.mIsViewportSet)
837 // For glViewport the lower-left corner is (0,0)
838 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
839 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
843 viewportRect = surfaceRect;
847 // Set surface orientation
848 // @todo Inform graphics impl by another route.
849 // was: mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
851 /*** Clear region of framebuffer or surface before drawing ***/
852 bool clearFullFrameRect = (surfaceRect == viewportRect);
853 if(instruction.mFrameBuffer != nullptr)
855 Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
856 clearFullFrameRect = (frameRect == viewportRect);
859 if(!clippingRect.IsEmpty())
861 if(!clippingRect.Intersect(viewportRect))
863 DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
864 clippingRect = Rect<int>();
866 clearFullFrameRect = false;
869 Graphics::Rect2D scissorArea{viewportRect.x, viewportRect.y, uint32_t(viewportRect.width), uint32_t(viewportRect.height)};
870 if(instruction.mIsClearColorSet)
872 if(!clearFullFrameRect)
874 if(!clippingRect.IsEmpty())
876 scissorArea = {clippingRect.x, clippingRect.y, uint32_t(clippingRect.width), uint32_t(clippingRect.height)};
882 mainCommandBuffer->BeginRenderPass(
888 mainCommandBuffer->SetViewport({float(viewportRect.x),
889 float(viewportRect.y),
890 float(viewportRect.width),
891 float(viewportRect.height)});
893 // Clear the list of bound textures
894 mImpl->boundTextures.Clear();
896 mImpl->renderAlgorithms.ProcessRenderInstruction(
898 mImpl->renderBufferIndex,
899 depthBufferAvailable,
900 stencilBufferAvailable,
901 mImpl->boundTextures,
906 // Synchronise the FBO/Texture access
908 // Check whether any bound texture is in the dependency list
909 bool textureFound = false;
911 if(mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u)
913 for(auto texture : mImpl->textureDependencyList)
915 textureFound = std::find_if(mImpl->boundTextures.Begin(), mImpl->boundTextures.End(), [texture](Graphics::Texture* graphicsTexture) {
916 return texture == graphicsTexture;
917 }) != mImpl->boundTextures.End();
923 if(instruction.mFrameBuffer)
925 // For off-screen buffer
927 // Clear the dependency list
928 mImpl->textureDependencyList.Clear();
932 // Worker thread lambda function
933 auto& glContextHelperAbstraction = mImpl->graphicsController.GetGlContextHelperAbstraction();
934 auto workerFunction = [&glContextHelperAbstraction](int workerThread) {
935 // Switch to the shared context in the worker thread
936 glContextHelperAbstraction.MakeSurfacelessContextCurrent();
938 // Wait until all rendering calls for the shared context are executed
939 glContextHelperAbstraction.WaitClient();
941 // Must clear the context in the worker thread
942 // Otherwise the shared context cannot be switched to from the render thread
943 glContextHelperAbstraction.MakeContextNull();
946 auto future = mImpl->threadPool->SubmitTask(0u, workerFunction);
949 mImpl->threadPool->Wait();
951 // Clear the dependency list
952 mImpl->textureDependencyList.Clear();
957 Graphics::SyncObject* syncObject{nullptr};
958 // If the render instruction has an associated render tracker (owned separately)
959 // and framebuffer, create a one shot sync object, and use it to determine when
960 // the render pass has finished executing on GPU.
961 if(instruction.mRenderTracker && instruction.mFrameBuffer)
963 syncObject = instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController);
964 instruction.mRenderTracker = nullptr;
966 mainCommandBuffer->EndRenderPass(syncObject);
968 mImpl->renderAlgorithms.SubmitCommandBuffer();
970 std::sort(targetstoPresent.begin(), targetstoPresent.end());
972 Graphics::RenderTarget* rt = nullptr;
973 for(auto& target : targetstoPresent)
977 mImpl->graphicsController.PresentRenderTarget(target);
983 void RenderManager::PostRender(bool uploadOnly)
985 // Notify RenderGeometries that rendering has finished
986 for(auto&& iter : mImpl->geometryContainer)
988 iter->OnRenderFinished();
991 mImpl->UpdateTrackers();
994 for(auto& scene : mImpl->sceneContainer)
996 count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
999 const bool haveInstructions = count > 0u;
1001 // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
1002 mImpl->lastFrameWasRendered = haveInstructions;
1005 * The rendering has finished; swap to the next buffer.
1006 * Ideally the update has just finished using this buffer; otherwise the render thread
1007 * should block until the update has finished.
1009 mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1011 DALI_PRINT_RENDER_END();
1014 } // namespace SceneGraph
1016 } // namespace Internal