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>
28 #include <dali/internal/event/common/scene-impl.h>
30 #include <dali/internal/update/common/scene-graph-scene.h>
31 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
33 #include <dali/internal/render/common/render-algorithms.h>
34 #include <dali/internal/render/common/render-debug.h>
35 #include <dali/internal/render/common/render-instruction.h>
36 #include <dali/internal/render/common/render-tracker.h>
37 #include <dali/internal/render/queue/render-queue.h>
38 #include <dali/internal/render/renderers/render-frame-buffer.h>
39 #include <dali/internal/render/renderers/render-texture.h>
40 #include <dali/internal/render/renderers/shader-cache.h>
41 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
42 #include <dali/internal/render/renderers/uniform-buffer-view-pool.h>
43 #include <dali/internal/render/shaders/program-controller.h>
44 #include <dali/internal/render/renderers/pipeline-cache.h>
54 #if defined(DEBUG_ENABLED)
57 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
58 } // unnamed namespace
63 inline Graphics::Rect2D RecalculateScissorArea(Graphics::Rect2D scissorArea, int orientation, Rect<int32_t> viewportRect)
65 Graphics::Rect2D newScissorArea;
69 newScissorArea.x = viewportRect.height - (scissorArea.y + scissorArea.height);
70 newScissorArea.y = scissorArea.x;
71 newScissorArea.width = scissorArea.height;
72 newScissorArea.height = scissorArea.width;
74 else if(orientation == 180)
76 newScissorArea.x = viewportRect.width - (scissorArea.x + scissorArea.width);
77 newScissorArea.y = viewportRect.height - (scissorArea.y + scissorArea.height);
78 newScissorArea.width = scissorArea.width;
79 newScissorArea.height = scissorArea.height;
81 else if(orientation == 270)
83 newScissorArea.x = scissorArea.y;
84 newScissorArea.y = viewportRect.width - (scissorArea.x + scissorArea.width);
85 newScissorArea.width = scissorArea.height;
86 newScissorArea.height = scissorArea.width;
90 newScissorArea.x = scissorArea.x;
91 newScissorArea.y = scissorArea.y;
92 newScissorArea.width = scissorArea.width;
93 newScissorArea.height = scissorArea.height;
95 return newScissorArea;
99 * Structure to contain internal data
101 struct RenderManager::Impl
103 Impl(Graphics::Controller& graphicsController,
104 Integration::DepthBufferAvailable depthBufferAvailableParam,
105 Integration::StencilBufferAvailable stencilBufferAvailableParam,
106 Integration::PartialUpdateAvailable partialUpdateAvailableParam)
107 : graphicsController(graphicsController),
109 renderAlgorithms(graphicsController),
111 renderBufferIndex(SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX),
115 frameBufferContainer(),
116 lastFrameWasRendered(false),
117 programController(graphicsController),
118 shaderCache(graphicsController),
119 depthBufferAvailable(depthBufferAvailableParam),
120 stencilBufferAvailable(stencilBufferAvailableParam),
121 partialUpdateAvailable(partialUpdateAvailableParam)
123 // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
124 threadPool = std::make_unique<Dali::ThreadPool>();
125 threadPool->Initialize(1u);
127 uniformBufferManager = std::make_unique<Render::UniformBufferManager>(&graphicsController);
128 pipelineCache = std::make_unique<Render::PipelineCache>(graphicsController);
133 threadPool.reset(nullptr); // reset now to maintain correct destruction order
136 void AddRenderTracker(Render::RenderTracker* renderTracker)
138 DALI_ASSERT_DEBUG(renderTracker != nullptr);
139 mRenderTrackers.PushBack(renderTracker);
142 void RemoveRenderTracker(Render::RenderTracker* renderTracker)
144 mRenderTrackers.EraseObject(renderTracker);
147 void UpdateTrackers()
149 for(auto&& iter : mRenderTrackers)
151 iter->PollSyncObject();
155 // the order is important for destruction,
156 Graphics::Controller& graphicsController;
157 RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread.
159 std::vector<SceneGraph::Scene*> sceneContainer; ///< List of pointers to the scene graph objects of the scenes
161 Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
163 uint32_t frameCount; ///< The current frame count
164 BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer
166 OwnerContainer<Render::Renderer*> rendererContainer; ///< List of owned renderers
167 OwnerContainer<Render::Sampler*> samplerContainer; ///< List of owned samplers
168 OwnerContainer<Render::Texture*> textureContainer; ///< List of owned textures
169 OwnerContainer<Render::FrameBuffer*> frameBufferContainer; ///< List of owned framebuffers
170 OwnerContainer<Render::VertexBuffer*> vertexBufferContainer; ///< List of owned vertex buffers
171 OwnerContainer<Render::Geometry*> geometryContainer; ///< List of owned Geometries
173 bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions
175 OwnerContainer<Render::RenderTracker*> mRenderTrackers; ///< List of render trackers
177 ProgramController programController; ///< Owner of the programs
178 Render::ShaderCache shaderCache; ///< The cache for the graphics shaders
180 std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
181 std::unique_ptr<Render::PipelineCache> pipelineCache;
183 Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available
184 Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
185 Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
187 std::unique_ptr<Dali::ThreadPool> threadPool; ///< The thread pool
188 Vector<Graphics::Texture*> boundTextures; ///< The textures bound for rendering
189 Vector<Graphics::Texture*> textureDependencyList; ///< The dependency list of bound textures
192 RenderManager* RenderManager::New(Graphics::Controller& graphicsController,
193 Integration::DepthBufferAvailable depthBufferAvailable,
194 Integration::StencilBufferAvailable stencilBufferAvailable,
195 Integration::PartialUpdateAvailable partialUpdateAvailable)
197 auto* manager = new RenderManager;
198 manager->mImpl = new Impl(graphicsController,
199 depthBufferAvailable,
200 stencilBufferAvailable,
201 partialUpdateAvailable);
205 RenderManager::RenderManager()
210 RenderManager::~RenderManager()
215 RenderQueue& RenderManager::GetRenderQueue()
217 return mImpl->renderQueue;
220 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
224 void RenderManager::AddRenderer(OwnerPointer<Render::Renderer>& renderer)
226 // Initialize the renderer as we are now in render thread
227 renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()),
228 *(mImpl->pipelineCache.get()));
230 mImpl->rendererContainer.PushBack(renderer.Release());
233 void RenderManager::RemoveRenderer(Render::Renderer* renderer)
235 mImpl->rendererContainer.EraseObject(renderer);
238 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
240 sampler->Initialize(mImpl->graphicsController);
241 mImpl->samplerContainer.PushBack(sampler.Release());
244 void RenderManager::RemoveSampler(Render::Sampler* sampler)
246 mImpl->samplerContainer.EraseObject(sampler);
249 void RenderManager::AddTexture(OwnerPointer<Render::Texture>& texture)
251 texture->Initialize(mImpl->graphicsController);
252 mImpl->textureContainer.PushBack(texture.Release());
255 void RenderManager::RemoveTexture(Render::Texture* texture)
257 DALI_ASSERT_DEBUG(NULL != texture);
259 // Find the texture, use reference to pointer so we can do the erase safely
260 for(auto&& iter : mImpl->textureContainer)
265 mImpl->textureContainer.Erase(&iter); // Texture found; now destroy it
271 void RenderManager::UploadTexture(Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params)
273 texture->Upload(pixelData, params);
276 void RenderManager::GenerateMipmaps(Render::Texture* texture)
278 texture->GenerateMipmaps();
281 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
283 sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
284 static_cast<Dali::FilterMode::Type>(magFilterMode));
287 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
289 sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
290 static_cast<Dali::WrapMode::Type>(sWrapMode),
291 static_cast<Dali::WrapMode::Type>(tWrapMode));
294 void RenderManager::AddFrameBuffer(OwnerPointer<Render::FrameBuffer>& frameBuffer)
296 Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
297 mImpl->frameBufferContainer.PushBack(frameBufferPtr);
298 frameBufferPtr->Initialize(mImpl->graphicsController);
301 void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
303 DALI_ASSERT_DEBUG(nullptr != frameBuffer);
305 // Find the sampler, use reference so we can safely do the erase
306 for(auto&& iter : mImpl->frameBufferContainer)
308 if(iter == frameBuffer)
310 frameBuffer->Destroy();
311 mImpl->frameBufferContainer.Erase(&iter); // frameBuffer found; now destroy it
318 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
320 scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
321 mImpl->sceneContainer.push_back(scene);
324 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
326 auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
327 if(iter != mImpl->sceneContainer.end())
329 mImpl->sceneContainer.erase(iter);
333 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
335 scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
338 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
340 frameBuffer->AttachColorTexture(texture, mipmapLevel, layer);
343 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
345 frameBuffer->AttachDepthTexture(texture, mipmapLevel);
348 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
350 frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel);
353 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
355 mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
358 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
360 mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
363 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
365 vertexBuffer->SetFormat(format.Release());
368 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
370 vertexBuffer->SetData(data.Release(), size);
373 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Dali::Vector<uint16_t>& indices)
375 geometry->SetIndexBuffer(indices);
378 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
380 mImpl->geometryContainer.PushBack(geometry.Release());
383 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
385 mImpl->geometryContainer.EraseObject(geometry);
388 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
390 DALI_ASSERT_DEBUG(nullptr != geometry);
393 for(auto&& iter : mImpl->geometryContainer)
397 iter->AddVertexBuffer(vertexBuffer);
403 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
405 DALI_ASSERT_DEBUG(nullptr != geometry);
408 for(auto&& iter : mImpl->geometryContainer)
412 iter->RemoveVertexBuffer(vertexBuffer);
418 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
420 geometry->SetType(Render::Geometry::Type(geometryType));
423 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
425 mImpl->AddRenderTracker(renderTracker);
428 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
430 mImpl->RemoveRenderTracker(renderTracker);
433 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear, bool uploadOnly)
435 DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
438 mImpl->uniformBufferManager->GetUniformBufferViewPool(mImpl->renderBufferIndex)->Rollback();
440 // Increment the frame count at the beginning of each frame
443 // Process messages queued during previous update
444 mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
447 for(auto& i : mImpl->sceneContainer)
449 count += i->GetRenderInstructions().Count(mImpl->renderBufferIndex);
452 const bool haveInstructions = count > 0u;
454 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");
456 // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
457 if(haveInstructions || mImpl->lastFrameWasRendered || forceClear)
459 DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n");
461 // Upload the geometries
462 for(auto& i : mImpl->sceneContainer)
464 RenderInstructionContainer& instructions = i->GetRenderInstructions();
465 for(uint32_t j = 0; j < instructions.Count(mImpl->renderBufferIndex); ++j)
467 RenderInstruction& instruction = instructions.At(mImpl->renderBufferIndex, j);
469 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
470 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
472 DALI_ASSERT_DEBUG(viewMatrix);
473 DALI_ASSERT_DEBUG(projectionMatrix);
475 if(viewMatrix && projectionMatrix)
477 const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
479 // Iterate through each render list.
480 for(RenderListContainer::SizeType index = 0; index < renderListCount; ++index)
482 const RenderList* renderList = instruction.GetRenderList(index);
484 if(renderList && !renderList->IsEmpty())
486 const std::size_t itemCount = renderList->Count();
487 for(uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex)
489 const RenderItem& item = renderList->GetItem(itemIndex);
490 if(DALI_LIKELY(item.mRenderer))
492 item.mRenderer->Upload();
503 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
505 if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
510 Internal::Scene& sceneInternal = GetImplementation(scene);
511 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
513 if(sceneObject->IsRenderingSkipped())
515 // We don't need to calculate dirty rects
519 class DamagedRectsCleaner
522 explicit DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects)
523 : mDamagedRects(damagedRects),
528 void SetCleanOnReturn(bool cleanOnReturn)
530 mCleanOnReturn = cleanOnReturn;
533 ~DamagedRectsCleaner()
537 mDamagedRects.clear();
542 std::vector<Rect<int>>& mDamagedRects;
546 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
548 // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
549 DamagedRectsCleaner damagedRectCleaner(damagedRects);
551 // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number.
552 // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end());
553 std::vector<DirtyRect>& itemsDirtyRects = sceneInternal.GetItemsDirtyRects();
554 for(DirtyRect& dirtyRect : itemsDirtyRects)
556 dirtyRect.visited = false;
559 uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
560 for(uint32_t i = 0; i < instructionCount; ++i)
562 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
564 if(instruction.mFrameBuffer)
566 return; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
569 const Camera* camera = instruction.GetCamera();
570 if(camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
572 const Node* node = instruction.GetCamera()->GetNode();
577 Quaternion orientation;
578 node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
580 Vector3 orientationAxis;
581 Radian orientationAngle;
582 orientation.ToAxisAngle(orientationAxis, orientationAngle);
584 if(position.x > Math::MACHINE_EPSILON_10000 ||
585 position.y > Math::MACHINE_EPSILON_10000 ||
586 orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
587 orientationAngle != ANGLE_180 ||
588 scale != Vector3(1.0f, 1.0f, 1.0f))
599 Rect<int32_t> viewportRect;
600 if(instruction.mIsViewportSet)
602 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
603 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
604 if(viewportRect.IsEmpty() || !viewportRect.IsValid())
606 return; // just skip funny use cases for now, empty viewport means it is set somewhere else
611 viewportRect = surfaceRect;
614 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
615 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
616 if(viewMatrix && projectionMatrix)
618 const RenderListContainer::SizeType count = instruction.RenderListCount();
619 for(RenderListContainer::SizeType index = 0u; index < count; ++index)
621 const RenderList* renderList = instruction.GetRenderList(index);
622 if(renderList && !renderList->IsEmpty())
624 const std::size_t listCount = renderList->Count();
625 for(uint32_t listIndex = 0u; listIndex < listCount; ++listIndex)
627 RenderItem& item = renderList->GetItem(listIndex);
628 // If the item does 3D transformation, do early exit and clean the damaged rect array
629 if(item.mUpdateSize == Vector3::ZERO)
635 DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect);
636 // If the item refers to updated node or renderer.
637 if(item.mIsUpdated ||
639 (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode)))))
641 item.mIsUpdated = false;
642 item.mNode->SetUpdated(false);
644 rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height);
645 if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
647 const int left = rect.x;
648 const int top = rect.y;
649 const int right = rect.x + rect.width;
650 const int bottom = rect.y + rect.height;
651 rect.x = (left / 16) * 16;
652 rect.y = (top / 16) * 16;
653 rect.width = ((right + 16) / 16) * 16 - rect.x;
654 rect.height = ((bottom + 16) / 16) * 16 - rect.y;
656 // Found valid dirty rect.
657 // 1. Insert it in the sorted array of the dirty rects.
658 // 2. Mark the related dirty rects as visited so they will not be removed below.
659 // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1).
660 dirtyRect.rect = rect;
661 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
662 dirtyRectPos = itemsDirtyRects.insert(dirtyRectPos, dirtyRect);
665 while(++dirtyRectPos != itemsDirtyRects.end())
667 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
672 dirtyRectPos->visited = true;
673 Rect<int>& dirtRect = dirtyRectPos->rect;
674 rect.Merge(dirtRect);
677 if(c > 3) // no more then 3 previous rects
679 itemsDirtyRects.erase(dirtyRectPos);
684 damagedRects.push_back(rect);
689 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
690 // 2. Mark the related dirty rects as visited so they will not be removed below.
691 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
692 while(dirtyRectPos != itemsDirtyRects.end())
694 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
699 dirtyRectPos->visited = true;
709 // Check removed nodes or removed renderers dirty rects
710 auto i = itemsDirtyRects.begin();
711 auto j = itemsDirtyRects.begin();
712 while(i != itemsDirtyRects.end())
720 Rect<int>& dirtRect = i->rect;
721 damagedRects.push_back(dirtRect);
726 itemsDirtyRects.resize(j - itemsDirtyRects.begin());
727 damagedRectCleaner.SetCleanOnReturn(false);
730 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
732 Rect<int> clippingRect;
733 RenderScene(status, scene, renderToFbo, clippingRect);
736 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
738 // Reset main algorithms command buffer
739 mImpl->renderAlgorithms.ResetCommandBuffer();
741 auto mainCommandBuffer = mImpl->renderAlgorithms.GetMainCommandBuffer();
743 Internal::Scene& sceneInternal = GetImplementation(scene);
744 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
746 uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
748 std::vector<Graphics::RenderTarget*> targetstoPresent;
750 for(uint32_t i = 0; i < count; ++i)
752 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
754 if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
759 // Mark that we will require a post-render step to be performed (includes swap-buffers).
760 status.SetNeedsPostRender(true);
762 Rect<int32_t> viewportRect;
764 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
765 int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation();
767 // @todo Should these be part of scene?
768 Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable;
769 Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
771 Graphics::RenderTarget* currentRenderTarget = nullptr;
772 Graphics::RenderPass* currentRenderPass = nullptr;
773 std::vector<Graphics::ClearValue> currentClearValues{};
775 if(instruction.mFrameBuffer)
777 // Ensure graphics framebuffer is created, bind attachments and create render passes
778 // Only happens once per framebuffer. If the create fails, e.g. no attachments yet,
779 // then don't render to this framebuffer.
780 if(!instruction.mFrameBuffer->GetGraphicsObject())
782 const bool created = instruction.mFrameBuffer->CreateGraphicsObjects();
789 auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues();
791 // Set the clear color for first color attachment
792 if(instruction.mIsClearColorSet && !clearValues.empty())
794 clearValues[0].color = {
795 instruction.mClearColor.r,
796 instruction.mClearColor.g,
797 instruction.mClearColor.b,
798 instruction.mClearColor.a};
801 currentClearValues = clearValues;
803 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
806 currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget();
807 currentRenderPass = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
809 else // no framebuffer
812 auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues();
814 if(instruction.mIsClearColorSet)
816 clearValues[0].color = {
817 instruction.mClearColor.r,
818 instruction.mClearColor.g,
819 instruction.mClearColor.b,
820 instruction.mClearColor.a};
823 currentClearValues = clearValues;
825 // @todo SceneObject should already have the depth clear / stencil clear in the clearValues array.
826 // if the window has a depth/stencil buffer.
827 if((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
828 stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) &&
829 (currentClearValues.size() <= 1))
831 currentClearValues.emplace_back();
832 currentClearValues.back().depthStencil.depth = 0;
833 currentClearValues.back().depthStencil.stencil = 0;
836 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
838 currentRenderTarget = sceneObject->GetSurfaceRenderTarget();
839 currentRenderPass = sceneObject->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
842 targetstoPresent.emplace_back(currentRenderTarget);
844 // reset the program matrices for all programs once per frame
845 // this ensures we will set view and projection matrix once per program per camera
846 mImpl->programController.ResetProgramMatrices();
848 if(instruction.mFrameBuffer)
850 // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
851 for(unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
853 mImpl->textureDependencyList.PushBack(instruction.mFrameBuffer->GetTexture(i0));
857 if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
859 // Offscreen buffer rendering
860 if(instruction.mIsViewportSet)
862 // For Viewport the lower-left corner is (0,0)
863 const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
864 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
868 viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
870 surfaceOrientation = 0;
872 else // No Offscreen frame buffer rendering
874 // Check whether a viewport is specified, otherwise the full surface size is used
875 if(instruction.mIsViewportSet)
877 // For Viewport the lower-left corner is (0,0)
878 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
879 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
883 viewportRect = surfaceRect;
887 // Set surface orientation
888 // @todo Inform graphics impl by another route.
889 // was: mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
891 /*** Clear region of framebuffer or surface before drawing ***/
892 bool clearFullFrameRect = (surfaceRect == viewportRect);
893 if(instruction.mFrameBuffer != nullptr)
895 Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
896 clearFullFrameRect = (frameRect == viewportRect);
899 if(!clippingRect.IsEmpty())
901 if(!clippingRect.Intersect(viewportRect))
903 DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
904 clippingRect = Rect<int>();
906 clearFullFrameRect = false;
909 Graphics::Rect2D scissorArea{viewportRect.x, viewportRect.y, uint32_t(viewportRect.width), uint32_t(viewportRect.height)};
910 if(instruction.mIsClearColorSet)
912 if(!clearFullFrameRect)
914 if(!clippingRect.IsEmpty())
916 scissorArea = {clippingRect.x, clippingRect.y, uint32_t(clippingRect.width), uint32_t(clippingRect.height)};
921 // Scissor's value should be set based on the default system coordinates.
922 // When the surface is rotated, the input values already were set with the rotated angle.
923 // So, re-calculation is needed.
924 scissorArea = RecalculateScissorArea(scissorArea, surfaceOrientation, viewportRect);
927 mainCommandBuffer->BeginRenderPass(
933 mainCommandBuffer->SetViewport({float(viewportRect.x),
934 float(viewportRect.y),
935 float(viewportRect.width),
936 float(viewportRect.height)});
938 // Clear the list of bound textures
939 mImpl->boundTextures.Clear();
941 mImpl->renderAlgorithms.ProcessRenderInstruction(
943 mImpl->renderBufferIndex,
944 depthBufferAvailable,
945 stencilBufferAvailable,
946 mImpl->boundTextures,
951 Graphics::SyncObject* syncObject{nullptr};
952 // If the render instruction has an associated render tracker (owned separately)
953 // and framebuffer, create a one shot sync object, and use it to determine when
954 // the render pass has finished executing on GPU.
955 if(instruction.mRenderTracker && instruction.mFrameBuffer)
957 syncObject = instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController);
958 instruction.mRenderTracker = nullptr;
960 mainCommandBuffer->EndRenderPass(syncObject);
962 mImpl->renderAlgorithms.SubmitCommandBuffer();
964 std::sort(targetstoPresent.begin(), targetstoPresent.end());
966 Graphics::RenderTarget* rt = nullptr;
967 for(auto& target : targetstoPresent)
971 mImpl->graphicsController.PresentRenderTarget(target);
977 void RenderManager::PostRender(bool uploadOnly)
979 // Notify RenderGeometries that rendering has finished
980 for(auto&& iter : mImpl->geometryContainer)
982 iter->OnRenderFinished();
985 mImpl->UpdateTrackers();
988 for(auto& scene : mImpl->sceneContainer)
990 count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
993 const bool haveInstructions = count > 0u;
995 // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
996 mImpl->lastFrameWasRendered = haveInstructions;
999 * The rendering has finished; swap to the next buffer.
1000 * Ideally the update has just finished using this buffer; otherwise the render thread
1001 * should block until the update has finished.
1003 mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1005 DALI_PRINT_RENDER_END();
1008 } // namespace SceneGraph
1010 } // namespace Internal