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>
45 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
53 #if defined(DEBUG_ENABLED)
56 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
57 } // unnamed namespace
62 inline Graphics::Rect2D RecalculateScissorArea(Graphics::Rect2D scissorArea, int orientation, Rect<int32_t> viewportRect)
64 Graphics::Rect2D newScissorArea;
68 newScissorArea.x = viewportRect.height - (scissorArea.y + scissorArea.height);
69 newScissorArea.y = scissorArea.x;
70 newScissorArea.width = scissorArea.height;
71 newScissorArea.height = scissorArea.width;
73 else if(orientation == 180)
75 newScissorArea.x = viewportRect.width - (scissorArea.x + scissorArea.width);
76 newScissorArea.y = viewportRect.height - (scissorArea.y + scissorArea.height);
77 newScissorArea.width = scissorArea.width;
78 newScissorArea.height = scissorArea.height;
80 else if(orientation == 270)
82 newScissorArea.x = scissorArea.y;
83 newScissorArea.y = viewportRect.width - (scissorArea.x + scissorArea.width);
84 newScissorArea.width = scissorArea.height;
85 newScissorArea.height = scissorArea.width;
89 newScissorArea.x = scissorArea.x;
90 newScissorArea.y = scissorArea.y;
91 newScissorArea.width = scissorArea.width;
92 newScissorArea.height = scissorArea.height;
94 return newScissorArea;
98 * Structure to contain internal data
100 struct RenderManager::Impl
102 Impl(Graphics::Controller& graphicsController,
103 Integration::DepthBufferAvailable depthBufferAvailableParam,
104 Integration::StencilBufferAvailable stencilBufferAvailableParam,
105 Integration::PartialUpdateAvailable partialUpdateAvailableParam)
106 : graphicsController(graphicsController),
108 renderAlgorithms(graphicsController),
110 renderBufferIndex(SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX),
114 frameBufferContainer(),
115 lastFrameWasRendered(false),
116 programController(graphicsController),
117 shaderCache(graphicsController),
118 depthBufferAvailable(depthBufferAvailableParam),
119 stencilBufferAvailable(stencilBufferAvailableParam),
120 partialUpdateAvailable(partialUpdateAvailableParam)
122 // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
123 threadPool = std::unique_ptr<Dali::ThreadPool>(new Dali::ThreadPool());
124 threadPool->Initialize(1u);
126 uniformBufferManager.reset(new Render::UniformBufferManager(&graphicsController));
131 threadPool.reset(nullptr); // reset now to maintain correct destruction order
134 void AddRenderTracker(Render::RenderTracker* renderTracker)
136 DALI_ASSERT_DEBUG(renderTracker != nullptr);
137 mRenderTrackers.PushBack(renderTracker);
140 void RemoveRenderTracker(Render::RenderTracker* renderTracker)
142 mRenderTrackers.EraseObject(renderTracker);
145 void UpdateTrackers()
147 for(auto&& iter : mRenderTrackers)
149 iter->PollSyncObject();
153 // the order is important for destruction,
154 Graphics::Controller& graphicsController;
155 RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread.
157 std::vector<SceneGraph::Scene*> sceneContainer; ///< List of pointers to the scene graph objects of the scenes
159 Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
161 uint32_t frameCount; ///< The current frame count
162 BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer
164 OwnerContainer<Render::Renderer*> rendererContainer; ///< List of owned renderers
165 OwnerContainer<Render::Sampler*> samplerContainer; ///< List of owned samplers
166 OwnerContainer<Render::Texture*> textureContainer; ///< List of owned textures
167 OwnerContainer<Render::FrameBuffer*> frameBufferContainer; ///< List of owned framebuffers
168 OwnerContainer<Render::VertexBuffer*> vertexBufferContainer; ///< List of owned vertex buffers
169 OwnerContainer<Render::Geometry*> geometryContainer; ///< List of owned Geometries
171 bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions
173 OwnerContainer<Render::RenderTracker*> mRenderTrackers; ///< List of render trackers
175 ProgramController programController; ///< Owner of the programs
176 Render::ShaderCache shaderCache; ///< The cache for the graphics shaders
178 std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
180 Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available
181 Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
182 Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
184 std::unique_ptr<Dali::ThreadPool> threadPool; ///< The thread pool
185 Vector<Graphics::Texture*> boundTextures; ///< The textures bound for rendering
186 Vector<Graphics::Texture*> textureDependencyList; ///< The dependency list of bound textures
189 RenderManager* RenderManager::New(Graphics::Controller& graphicsController,
190 Integration::DepthBufferAvailable depthBufferAvailable,
191 Integration::StencilBufferAvailable stencilBufferAvailable,
192 Integration::PartialUpdateAvailable partialUpdateAvailable)
194 auto* manager = new RenderManager;
195 manager->mImpl = new Impl(graphicsController,
196 depthBufferAvailable,
197 stencilBufferAvailable,
198 partialUpdateAvailable);
202 RenderManager::RenderManager()
207 RenderManager::~RenderManager()
212 RenderQueue& RenderManager::GetRenderQueue()
214 return mImpl->renderQueue;
217 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
221 void RenderManager::AddRenderer(OwnerPointer<Render::Renderer>& renderer)
223 // Initialize the renderer as we are now in render thread
224 renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()));
226 mImpl->rendererContainer.PushBack(renderer.Release());
229 void RenderManager::RemoveRenderer(Render::Renderer* renderer)
231 mImpl->rendererContainer.EraseObject(renderer);
234 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
236 sampler->Initialize(mImpl->graphicsController);
237 mImpl->samplerContainer.PushBack(sampler.Release());
240 void RenderManager::RemoveSampler(Render::Sampler* sampler)
242 mImpl->samplerContainer.EraseObject(sampler);
245 void RenderManager::AddTexture(OwnerPointer<Render::Texture>& texture)
247 texture->Initialize(mImpl->graphicsController);
248 mImpl->textureContainer.PushBack(texture.Release());
251 void RenderManager::RemoveTexture(Render::Texture* texture)
253 DALI_ASSERT_DEBUG(NULL != texture);
255 // Find the texture, use reference to pointer so we can do the erase safely
256 for(auto&& iter : mImpl->textureContainer)
261 mImpl->textureContainer.Erase(&iter); // Texture found; now destroy it
267 void RenderManager::UploadTexture(Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params)
269 texture->Upload(pixelData, params);
272 void RenderManager::GenerateMipmaps(Render::Texture* texture)
274 texture->GenerateMipmaps();
277 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
279 sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
280 static_cast<Dali::FilterMode::Type>(magFilterMode));
283 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
285 sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
286 static_cast<Dali::WrapMode::Type>(sWrapMode),
287 static_cast<Dali::WrapMode::Type>(tWrapMode));
290 void RenderManager::AddFrameBuffer(OwnerPointer<Render::FrameBuffer>& frameBuffer)
292 Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
293 mImpl->frameBufferContainer.PushBack(frameBufferPtr);
294 frameBufferPtr->Initialize(mImpl->graphicsController);
297 void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
299 DALI_ASSERT_DEBUG(nullptr != frameBuffer);
301 // Find the sampler, use reference so we can safely do the erase
302 for(auto&& iter : mImpl->frameBufferContainer)
304 if(iter == frameBuffer)
306 frameBuffer->Destroy();
307 mImpl->frameBufferContainer.Erase(&iter); // frameBuffer found; now destroy it
314 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
316 scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
317 mImpl->sceneContainer.push_back(scene);
320 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
322 auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
323 if(iter != mImpl->sceneContainer.end())
325 mImpl->sceneContainer.erase(iter);
329 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
331 scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
334 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
336 frameBuffer->AttachColorTexture(texture, mipmapLevel, layer);
339 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
341 frameBuffer->AttachDepthTexture(texture, mipmapLevel);
344 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
346 frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel);
349 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
351 mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
354 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
356 mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
359 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
361 vertexBuffer->SetFormat(format.Release());
364 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
366 vertexBuffer->SetData(data.Release(), size);
369 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Dali::Vector<uint16_t>& indices)
371 geometry->SetIndexBuffer(indices);
374 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
376 mImpl->geometryContainer.PushBack(geometry.Release());
379 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
381 mImpl->geometryContainer.EraseObject(geometry);
384 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
386 DALI_ASSERT_DEBUG(nullptr != geometry);
389 for(auto&& iter : mImpl->geometryContainer)
393 iter->AddVertexBuffer(vertexBuffer);
399 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
401 DALI_ASSERT_DEBUG(nullptr != geometry);
404 for(auto&& iter : mImpl->geometryContainer)
408 iter->RemoveVertexBuffer(vertexBuffer);
414 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
416 geometry->SetType(Render::Geometry::Type(geometryType));
419 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
421 mImpl->AddRenderTracker(renderTracker);
424 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
426 mImpl->RemoveRenderTracker(renderTracker);
429 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear, bool uploadOnly)
431 DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
434 mImpl->uniformBufferManager->GetUniformBufferViewPool(mImpl->renderBufferIndex)->Rollback();
436 // Increment the frame count at the beginning of each frame
439 // Process messages queued during previous update
440 mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
443 for(auto& i : mImpl->sceneContainer)
445 count += i->GetRenderInstructions().Count(mImpl->renderBufferIndex);
448 const bool haveInstructions = count > 0u;
450 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");
452 // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
453 if(haveInstructions || mImpl->lastFrameWasRendered || forceClear)
455 DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n");
457 // Upload the geometries
458 for(auto& i : mImpl->sceneContainer)
460 RenderInstructionContainer& instructions = i->GetRenderInstructions();
461 for(uint32_t j = 0; j < instructions.Count(mImpl->renderBufferIndex); ++j)
463 RenderInstruction& instruction = instructions.At(mImpl->renderBufferIndex, j);
465 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
466 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
468 DALI_ASSERT_DEBUG(viewMatrix);
469 DALI_ASSERT_DEBUG(projectionMatrix);
471 if(viewMatrix && projectionMatrix)
473 const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
475 // Iterate through each render list.
476 for(RenderListContainer::SizeType index = 0; index < renderListCount; ++index)
478 const RenderList* renderList = instruction.GetRenderList(index);
480 if(renderList && !renderList->IsEmpty())
482 const std::size_t itemCount = renderList->Count();
483 for(uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex)
485 const RenderItem& item = renderList->GetItem(itemIndex);
486 if(DALI_LIKELY(item.mRenderer))
488 item.mRenderer->Upload();
499 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
501 if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
506 Internal::Scene& sceneInternal = GetImplementation(scene);
507 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
509 if(sceneObject->IsRenderingSkipped())
511 // We don't need to calculate dirty rects
515 class DamagedRectsCleaner
518 explicit DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects)
519 : mDamagedRects(damagedRects),
524 void SetCleanOnReturn(bool cleanOnReturn)
526 mCleanOnReturn = cleanOnReturn;
529 ~DamagedRectsCleaner()
533 mDamagedRects.clear();
538 std::vector<Rect<int>>& mDamagedRects;
542 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
544 // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
545 DamagedRectsCleaner damagedRectCleaner(damagedRects);
547 // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number.
548 // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end());
549 std::vector<DirtyRect>& itemsDirtyRects = sceneInternal.GetItemsDirtyRects();
550 for(DirtyRect& dirtyRect : itemsDirtyRects)
552 dirtyRect.visited = false;
555 uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
556 for(uint32_t i = 0; i < instructionCount; ++i)
558 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
560 if(instruction.mFrameBuffer)
562 return; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
565 const Camera* camera = instruction.GetCamera();
566 if(camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
568 const Node* node = instruction.GetCamera()->GetNode();
573 Quaternion orientation;
574 node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
576 Vector3 orientationAxis;
577 Radian orientationAngle;
578 orientation.ToAxisAngle(orientationAxis, orientationAngle);
580 if(position.x > Math::MACHINE_EPSILON_10000 ||
581 position.y > Math::MACHINE_EPSILON_10000 ||
582 orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
583 orientationAngle != ANGLE_180 ||
584 scale != Vector3(1.0f, 1.0f, 1.0f))
595 Rect<int32_t> viewportRect;
596 if(instruction.mIsViewportSet)
598 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
599 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
600 if(viewportRect.IsEmpty() || !viewportRect.IsValid())
602 return; // just skip funny use cases for now, empty viewport means it is set somewhere else
607 viewportRect = surfaceRect;
610 const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex);
611 const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
612 if(viewMatrix && projectionMatrix)
614 const RenderListContainer::SizeType count = instruction.RenderListCount();
615 for(RenderListContainer::SizeType index = 0u; index < count; ++index)
617 const RenderList* renderList = instruction.GetRenderList(index);
618 if(renderList && !renderList->IsEmpty())
620 const std::size_t listCount = renderList->Count();
621 for(uint32_t listIndex = 0u; listIndex < listCount; ++listIndex)
623 RenderItem& item = renderList->GetItem(listIndex);
624 // If the item does 3D transformation, do early exit and clean the damaged rect array
625 if(item.mUpdateSize == Vector3::ZERO)
631 DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect);
632 // If the item refers to updated node or renderer.
633 if(item.mIsUpdated ||
635 (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode)))))
637 item.mIsUpdated = false;
638 item.mNode->SetUpdated(false);
640 rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height);
641 if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
643 const int left = rect.x;
644 const int top = rect.y;
645 const int right = rect.x + rect.width;
646 const int bottom = rect.y + rect.height;
647 rect.x = (left / 16) * 16;
648 rect.y = (top / 16) * 16;
649 rect.width = ((right + 16) / 16) * 16 - rect.x;
650 rect.height = ((bottom + 16) / 16) * 16 - rect.y;
652 // Found valid dirty rect.
653 // 1. Insert it in the sorted array of the dirty rects.
654 // 2. Mark the related dirty rects as visited so they will not be removed below.
655 // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1).
656 dirtyRect.rect = rect;
657 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
658 dirtyRectPos = itemsDirtyRects.insert(dirtyRectPos, dirtyRect);
661 while(++dirtyRectPos != itemsDirtyRects.end())
663 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
668 dirtyRectPos->visited = true;
669 Rect<int>& dirtRect = dirtyRectPos->rect;
670 rect.Merge(dirtRect);
673 if(c > 3) // no more then 3 previous rects
675 itemsDirtyRects.erase(dirtyRectPos);
680 damagedRects.push_back(rect);
685 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
686 // 2. Mark the related dirty rects as visited so they will not be removed below.
687 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
688 while(dirtyRectPos != itemsDirtyRects.end())
690 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
695 dirtyRectPos->visited = true;
705 // Check removed nodes or removed renderers dirty rects
706 auto i = itemsDirtyRects.begin();
707 auto j = itemsDirtyRects.begin();
708 while(i != itemsDirtyRects.end())
716 Rect<int>& dirtRect = i->rect;
717 damagedRects.push_back(dirtRect);
722 itemsDirtyRects.resize(j - itemsDirtyRects.begin());
723 damagedRectCleaner.SetCleanOnReturn(false);
726 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
728 Rect<int> clippingRect;
729 RenderScene(status, scene, renderToFbo, clippingRect);
732 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
734 // Reset main algorithms command buffer
735 mImpl->renderAlgorithms.ResetCommandBuffer();
737 auto mainCommandBuffer = mImpl->renderAlgorithms.GetMainCommandBuffer();
739 Internal::Scene& sceneInternal = GetImplementation(scene);
740 SceneGraph::Scene* sceneObject = sceneInternal.GetSceneObject();
742 uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
744 std::vector<Graphics::RenderTarget*> targetstoPresent;
746 for(uint32_t i = 0; i < count; ++i)
748 RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
750 if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
755 // Mark that we will require a post-render step to be performed (includes swap-buffers).
756 status.SetNeedsPostRender(true);
758 Rect<int32_t> viewportRect;
760 Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
761 int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation();
763 // @todo Should these be part of scene?
764 Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable;
765 Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
767 Graphics::RenderTarget* currentRenderTarget = nullptr;
768 Graphics::RenderPass* currentRenderPass = nullptr;
769 std::vector<Graphics::ClearValue> currentClearValues{};
771 if(instruction.mFrameBuffer)
773 // Ensure graphics framebuffer is created, bind attachments and create render passes
774 // Only happens once per framebuffer. If the create fails, e.g. no attachments yet,
775 // then don't render to this framebuffer.
776 if(!instruction.mFrameBuffer->GetGraphicsObject())
778 const bool created = instruction.mFrameBuffer->CreateGraphicsObjects();
785 auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues();
787 // Set the clear color for first color attachment
788 if(instruction.mIsClearColorSet && !clearValues.empty())
790 clearValues[0].color = {
791 instruction.mClearColor.r,
792 instruction.mClearColor.g,
793 instruction.mClearColor.b,
794 instruction.mClearColor.a};
797 currentClearValues = clearValues;
799 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
802 currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget();
803 currentRenderPass = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
805 else // no framebuffer
808 auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues();
810 if(instruction.mIsClearColorSet)
812 clearValues[0].color = {
813 instruction.mClearColor.r,
814 instruction.mClearColor.g,
815 instruction.mClearColor.b,
816 instruction.mClearColor.a};
819 currentClearValues = clearValues;
821 // @todo SceneObject should already have the depth clear / stencil clear in the clearValues array.
822 // if the window has a depth/stencil buffer.
823 if((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
824 stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) &&
825 (currentClearValues.size() <= 1))
827 currentClearValues.emplace_back();
828 currentClearValues.back().depthStencil.depth = 0;
829 currentClearValues.back().depthStencil.stencil = 0;
832 auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
834 currentRenderTarget = sceneObject->GetSurfaceRenderTarget();
835 currentRenderPass = sceneObject->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
838 targetstoPresent.emplace_back(currentRenderTarget);
840 // reset the program matrices for all programs once per frame
841 // this ensures we will set view and projection matrix once per program per camera
842 mImpl->programController.ResetProgramMatrices();
844 if(instruction.mFrameBuffer)
846 // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
847 for(unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
849 mImpl->textureDependencyList.PushBack(instruction.mFrameBuffer->GetTexture(i0));
853 if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
855 // Offscreen buffer rendering
856 if(instruction.mIsViewportSet)
858 // For Viewport the lower-left corner is (0,0)
859 const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
860 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
864 viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
866 surfaceOrientation = 0;
868 else // No Offscreen frame buffer rendering
870 // Check whether a viewport is specified, otherwise the full surface size is used
871 if(instruction.mIsViewportSet)
873 // For Viewport the lower-left corner is (0,0)
874 const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
875 viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
879 viewportRect = surfaceRect;
883 // Set surface orientation
884 // @todo Inform graphics impl by another route.
885 // was: mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
887 /*** Clear region of framebuffer or surface before drawing ***/
888 bool clearFullFrameRect = (surfaceRect == viewportRect);
889 if(instruction.mFrameBuffer != nullptr)
891 Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
892 clearFullFrameRect = (frameRect == viewportRect);
895 if(!clippingRect.IsEmpty())
897 if(!clippingRect.Intersect(viewportRect))
899 DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
900 clippingRect = Rect<int>();
902 clearFullFrameRect = false;
905 Graphics::Rect2D scissorArea{viewportRect.x, viewportRect.y, uint32_t(viewportRect.width), uint32_t(viewportRect.height)};
906 if(instruction.mIsClearColorSet)
908 if(!clearFullFrameRect)
910 if(!clippingRect.IsEmpty())
912 scissorArea = {clippingRect.x, clippingRect.y, uint32_t(clippingRect.width), uint32_t(clippingRect.height)};
917 // Scissor's value should be set based on the default system coordinates.
918 // When the surface is rotated, the input values already were set with the rotated angle.
919 // So, re-calculation is needed.
920 scissorArea = RecalculateScissorArea(scissorArea, surfaceOrientation, viewportRect);
923 mainCommandBuffer->BeginRenderPass(
929 mainCommandBuffer->SetViewport({float(viewportRect.x),
930 float(viewportRect.y),
931 float(viewportRect.width),
932 float(viewportRect.height)});
934 // Clear the list of bound textures
935 mImpl->boundTextures.Clear();
937 mImpl->renderAlgorithms.ProcessRenderInstruction(
939 mImpl->renderBufferIndex,
940 depthBufferAvailable,
941 stencilBufferAvailable,
942 mImpl->boundTextures,
947 Graphics::SyncObject* syncObject{nullptr};
948 // If the render instruction has an associated render tracker (owned separately)
949 // and framebuffer, create a one shot sync object, and use it to determine when
950 // the render pass has finished executing on GPU.
951 if(instruction.mRenderTracker && instruction.mFrameBuffer)
953 syncObject = instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController);
954 instruction.mRenderTracker = nullptr;
956 mainCommandBuffer->EndRenderPass(syncObject);
958 mImpl->renderAlgorithms.SubmitCommandBuffer();
960 std::sort(targetstoPresent.begin(), targetstoPresent.end());
962 Graphics::RenderTarget* rt = nullptr;
963 for(auto& target : targetstoPresent)
967 mImpl->graphicsController.PresentRenderTarget(target);
973 void RenderManager::PostRender(bool uploadOnly)
975 // Notify RenderGeometries that rendering has finished
976 for(auto&& iter : mImpl->geometryContainer)
978 iter->OnRenderFinished();
981 mImpl->UpdateTrackers();
984 for(auto& scene : mImpl->sceneContainer)
986 count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
989 const bool haveInstructions = count > 0u;
991 // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
992 mImpl->lastFrameWasRendered = haveInstructions;
995 * The rendering has finished; swap to the next buffer.
996 * Ideally the update has just finished using this buffer; otherwise the render thread
997 * should block until the update has finished.
999 mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1001 DALI_PRINT_RENDER_END();
1004 } // namespace SceneGraph
1006 } // namespace Internal