Revert "Remove unused program / shader caches"
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/common/render-manager.h>
20
21 // EXTERNAL INCLUDES
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/threading/thread-pool.h>
26 #include <dali/integration-api/core.h>
27 #include <dali/internal/common/ordered-set.h>
28
29 #include <dali/internal/event/common/scene-impl.h>
30
31 #include <dali/internal/update/common/scene-graph-scene.h>
32 #include <dali/internal/update/nodes/scene-graph-layer.h>
33 #include <dali/internal/update/render-tasks/scene-graph-camera.h>
34
35 #include <dali/internal/common/owner-key-container.h>
36
37 #include <dali/internal/render/common/render-algorithms.h>
38 #include <dali/internal/render/common/render-debug.h>
39 #include <dali/internal/render/common/render-instruction.h>
40 #include <dali/internal/render/common/render-tracker.h>
41 #include <dali/internal/render/queue/render-queue.h>
42 #include <dali/internal/render/renderers/pipeline-cache.h>
43 #include <dali/internal/render/renderers/render-frame-buffer.h>
44 #include <dali/internal/render/renderers/render-texture.h>
45 #include <dali/internal/render/renderers/shader-cache.h>
46 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
47 #include <dali/internal/render/renderers/uniform-buffer.h>
48 #include <dali/internal/render/shaders/program-controller.h>
49
50 #include <memory>
51
52 namespace Dali
53 {
54 namespace Internal
55 {
56 namespace SceneGraph
57 {
58 #if defined(DEBUG_ENABLED)
59 namespace
60 {
61 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
62 } // unnamed namespace
63 #endif
64
65 namespace
66 {
67 // TODO : Cache clean logic have some problem now. Just block it until bug resolved
68 //constexpr uint32_t CACHE_CLEAN_FRAME_COUNT = 600u; // 60fps * 10sec
69
70 inline Graphics::Rect2D RecalculateScissorArea(const Graphics::Rect2D& scissorArea, int orientation, const Rect<int32_t>& viewportRect)
71 {
72   Graphics::Rect2D newScissorArea;
73
74   if(orientation == 90)
75   {
76     newScissorArea.x      = viewportRect.height - (scissorArea.y + scissorArea.height);
77     newScissorArea.y      = scissorArea.x;
78     newScissorArea.width  = scissorArea.height;
79     newScissorArea.height = scissorArea.width;
80   }
81   else if(orientation == 180)
82   {
83     newScissorArea.x      = viewportRect.width - (scissorArea.x + scissorArea.width);
84     newScissorArea.y      = viewportRect.height - (scissorArea.y + scissorArea.height);
85     newScissorArea.width  = scissorArea.width;
86     newScissorArea.height = scissorArea.height;
87   }
88   else if(orientation == 270)
89   {
90     newScissorArea.x      = scissorArea.y;
91     newScissorArea.y      = viewportRect.width - (scissorArea.x + scissorArea.width);
92     newScissorArea.width  = scissorArea.height;
93     newScissorArea.height = scissorArea.width;
94   }
95   else
96   {
97     newScissorArea.x      = scissorArea.x;
98     newScissorArea.y      = scissorArea.y;
99     newScissorArea.width  = scissorArea.width;
100     newScissorArea.height = scissorArea.height;
101   }
102   return newScissorArea;
103 }
104
105 inline Rect<int32_t> CalculateUpdateArea(RenderItem& item, BufferIndex renderBufferIndex, const Rect<int32_t>& viewportRect)
106 {
107   Vector4 updateArea;
108   if(item.mNode && item.mNode->IsTextureUpdateAreaUsed() && item.mRenderer)
109   {
110     updateArea = item.mRenderer->GetTextureUpdateArea();
111   }
112   else
113   {
114     updateArea = item.mRenderer ? item.mRenderer->GetVisualTransformedUpdateArea(renderBufferIndex, item.mUpdateArea) : item.mUpdateArea;
115   }
116
117   return RenderItem::CalculateViewportSpaceAABB(item.mModelViewMatrix, Vector3(updateArea.x, updateArea.y, 0.0f), Vector3(updateArea.z, updateArea.w, 0.0f), viewportRect.width, viewportRect.height);
118 }
119
120 inline void AlignDamagedRect(Rect<int32_t>& rect)
121 {
122   const int left   = rect.x;
123   const int top    = rect.y;
124   const int right  = rect.x + rect.width;
125   const int bottom = rect.y + rect.height;
126   rect.x           = (left / 16) * 16;
127   rect.y           = (top / 16) * 16;
128   rect.width       = ((right + 16) / 16) * 16 - rect.x;
129   rect.height      = ((bottom + 16) / 16) * 16 - rect.y;
130 }
131 } // namespace
132
133 /**
134  * Structure to contain internal data
135  */
136 struct RenderManager::Impl
137 {
138   Impl(Graphics::Controller&               graphicsController,
139        Integration::DepthBufferAvailable   depthBufferAvailableParam,
140        Integration::StencilBufferAvailable stencilBufferAvailableParam,
141        Integration::PartialUpdateAvailable partialUpdateAvailableParam)
142   : graphicsController(graphicsController),
143     renderAlgorithms(graphicsController),
144     programController(graphicsController),
145     shaderCache(graphicsController),
146     depthBufferAvailable(depthBufferAvailableParam),
147     stencilBufferAvailable(stencilBufferAvailableParam),
148     partialUpdateAvailable(partialUpdateAvailableParam)
149   {
150     // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
151     threadPool = std::make_unique<Dali::ThreadPool>();
152     threadPool->Initialize(1u);
153
154     uniformBufferManager = std::make_unique<Render::UniformBufferManager>(&graphicsController);
155     pipelineCache        = std::make_unique<Render::PipelineCache>(graphicsController);
156   }
157
158   ~Impl()
159   {
160     threadPool.reset(nullptr); // reset now to maintain correct destruction order
161     rendererContainer.Clear(); // clear now before the pipeline cache is deleted
162   }
163
164   void AddRenderTracker(Render::RenderTracker* renderTracker)
165   {
166     DALI_ASSERT_DEBUG(renderTracker != nullptr);
167     mRenderTrackers.PushBack(renderTracker);
168   }
169
170   void RemoveRenderTracker(Render::RenderTracker* renderTracker)
171   {
172     mRenderTrackers.EraseObject(renderTracker);
173   }
174
175   void UpdateTrackers()
176   {
177     for(auto&& iter : mRenderTrackers)
178     {
179       iter->PollSyncObject();
180     }
181   }
182
183   // the order is important for destruction,
184   Graphics::Controller&           graphicsController;
185   RenderQueue                     renderQueue;      ///< A message queue for receiving messages from the update-thread.
186   std::vector<SceneGraph::Scene*> sceneContainer;   ///< List of pointers to the scene graph objects of the scenes
187   Render::RenderAlgorithms        renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
188
189   OrderedSet<Render::Sampler>         samplerContainer;      ///< List of owned samplers
190   OrderedSet<Render::FrameBuffer>     frameBufferContainer;  ///< List of owned framebuffers
191   OrderedSet<Render::VertexBuffer>    vertexBufferContainer; ///< List of owned vertex buffers
192   OrderedSet<Render::Geometry>        geometryContainer;     ///< List of owned Geometries
193   OwnerKeyContainer<Render::Renderer> rendererContainer;     ///< List of owned renderers
194   OwnerKeyContainer<Render::Texture>  textureContainer;      ///< List of owned textures
195
196   OrderedSet<Render::RenderTracker> mRenderTrackers; ///< List of owned render trackers
197
198   OwnerKeyContainer<Render::Texture> textureDiscardQueue; ///< Discarded textures
199
200   ProgramController   programController; ///< Owner of the programs
201   Render::ShaderCache shaderCache;       ///< The cache for the graphics shaders
202
203   std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
204   std::unique_ptr<Render::PipelineCache>        pipelineCache;
205
206   Integration::DepthBufferAvailable   depthBufferAvailable;   ///< Whether the depth buffer is available
207   Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
208   Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
209
210   std::unique_ptr<Dali::ThreadPool> threadPool;        ///< The thread pool
211   Vector<Render::TextureKey>        updatedTextures{}; ///< The updated texture list
212
213   uint32_t    frameCount{0u};                                                    ///< The current frame count
214   BufferIndex renderBufferIndex{SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX}; ///< The index of the buffer to read from;
215
216   bool lastFrameWasRendered{false}; ///< Keeps track of the last frame being rendered due to having render instructions
217   bool commandBufferSubmitted{false};
218 };
219
220 RenderManager* RenderManager::New(Graphics::Controller&               graphicsController,
221                                   Integration::DepthBufferAvailable   depthBufferAvailable,
222                                   Integration::StencilBufferAvailable stencilBufferAvailable,
223                                   Integration::PartialUpdateAvailable partialUpdateAvailable)
224 {
225   auto* manager  = new RenderManager;
226   manager->mImpl = new Impl(graphicsController,
227                             depthBufferAvailable,
228                             stencilBufferAvailable,
229                             partialUpdateAvailable);
230   return manager;
231 }
232
233 RenderManager::RenderManager()
234 : mImpl(nullptr)
235 {
236 }
237
238 RenderManager::~RenderManager()
239 {
240   delete mImpl;
241 }
242
243 RenderQueue& RenderManager::GetRenderQueue()
244 {
245   return mImpl->renderQueue;
246 }
247
248 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
249 {
250 }
251
252 void RenderManager::AddRenderer(const Render::RendererKey& renderer)
253 {
254   // Initialize the renderer as we are now in render thread
255   renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get()));
256
257   mImpl->rendererContainer.PushBack(renderer);
258 }
259
260 void RenderManager::RemoveRenderer(const Render::RendererKey& renderer)
261 {
262   mImpl->rendererContainer.EraseKey(renderer);
263 }
264
265 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
266 {
267   sampler->Initialize(mImpl->graphicsController);
268   mImpl->samplerContainer.PushBack(sampler.Release());
269 }
270
271 void RenderManager::RemoveSampler(Render::Sampler* sampler)
272 {
273   mImpl->samplerContainer.EraseObject(sampler);
274 }
275
276 void RenderManager::AddTexture(const Render::TextureKey& textureKey)
277 {
278   DALI_ASSERT_DEBUG(textureKey && "Trying to add empty texture key");
279
280   textureKey->Initialize(mImpl->graphicsController, *this);
281   mImpl->textureContainer.PushBack(textureKey);
282   mImpl->updatedTextures.PushBack(textureKey);
283 }
284
285 void RenderManager::RemoveTexture(const Render::TextureKey& textureKey)
286 {
287   DALI_ASSERT_DEBUG(textureKey && "Trying to remove empty texture key");
288
289   // Find the texture, use std::find so we can do the erase by iterator safely
290   auto iter = std::find(mImpl->textureContainer.Begin(), mImpl->textureContainer.End(), textureKey);
291
292   if(iter != mImpl->textureContainer.End())
293   {
294     // Destroy texture.
295     textureKey->Destroy();
296
297     // Transfer ownership to the discard queue, this keeps the object alive, until the render-thread has finished with it
298     mImpl->textureDiscardQueue.PushBack(mImpl->textureContainer.Release(iter));
299   }
300 }
301
302 void RenderManager::UploadTexture(const Render::TextureKey& textureKey, PixelDataPtr pixelData, const Graphics::UploadParams& params)
303 {
304   DALI_ASSERT_DEBUG(textureKey && "Trying to upload to empty texture key");
305   textureKey->Upload(pixelData, params);
306
307   mImpl->updatedTextures.PushBack(textureKey);
308 }
309
310 void RenderManager::GenerateMipmaps(const Render::TextureKey& textureKey)
311 {
312   DALI_ASSERT_DEBUG(textureKey && "Trying to generate mipmaps on empty texture key");
313   textureKey->GenerateMipmaps();
314
315   mImpl->updatedTextures.PushBack(textureKey);
316 }
317
318 void RenderManager::SetTextureSize(const Render::TextureKey& textureKey, const Dali::ImageDimensions& size)
319 {
320   DALI_ASSERT_DEBUG(textureKey && "Trying to set size on empty texture key");
321   textureKey->SetWidth(size.GetWidth());
322   textureKey->SetHeight(size.GetHeight());
323 }
324
325 void RenderManager::SetTextureFormat(const Render::TextureKey& textureKey, Dali::Pixel::Format pixelFormat)
326 {
327   DALI_ASSERT_DEBUG(textureKey && "Trying to set pixel format on empty texture key");
328   textureKey->SetPixelFormat(pixelFormat);
329 }
330
331 void RenderManager::SetTextureUpdated(const Render::TextureKey& textureKey)
332 {
333   DALI_ASSERT_DEBUG(textureKey && "Trying to set updated on empty texture key");
334   textureKey->SetUpdated(true);
335
336   mImpl->updatedTextures.PushBack(textureKey);
337 }
338
339 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
340 {
341   sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
342                          static_cast<Dali::FilterMode::Type>(magFilterMode));
343 }
344
345 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
346 {
347   sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
348                        static_cast<Dali::WrapMode::Type>(sWrapMode),
349                        static_cast<Dali::WrapMode::Type>(tWrapMode));
350 }
351
352 void RenderManager::AddFrameBuffer(OwnerPointer<Render::FrameBuffer>& frameBuffer)
353 {
354   Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
355   mImpl->frameBufferContainer.PushBack(frameBufferPtr);
356   frameBufferPtr->Initialize(mImpl->graphicsController);
357 }
358
359 void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
360 {
361   DALI_ASSERT_DEBUG(nullptr != frameBuffer);
362
363   // Find the framebuffer, use OrderedSet.Find so we can safely do the erase
364   auto iter = mImpl->frameBufferContainer.Find(frameBuffer);
365
366   if(iter != mImpl->frameBufferContainer.End())
367   {
368     frameBuffer->Destroy();
369     mImpl->frameBufferContainer.Erase(iter); // frameBuffer found; now destroy it
370   }
371 }
372
373 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
374 {
375   scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
376   mImpl->sceneContainer.push_back(scene);
377   mImpl->uniformBufferManager->RegisterScene(scene);
378 }
379
380 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
381 {
382   mImpl->uniformBufferManager->UnregisterScene(scene);
383   auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
384   if(iter != mImpl->sceneContainer.end())
385   {
386     mImpl->sceneContainer.erase(iter);
387   }
388 }
389
390 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
391 {
392   scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable);
393 }
394
395 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
396 {
397   frameBuffer->AttachColorTexture(texture, mipmapLevel, layer);
398 }
399
400 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
401 {
402   frameBuffer->AttachDepthTexture(texture, mipmapLevel);
403 }
404
405 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
406 {
407   frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel);
408 }
409
410 void RenderManager::SetMultiSamplingLevelToFrameBuffer(Render::FrameBuffer* frameBuffer, uint8_t multiSamplingLevel)
411 {
412   frameBuffer->SetMultiSamplingLevel(multiSamplingLevel);
413 }
414
415 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
416 {
417   mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
418 }
419
420 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
421 {
422   mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
423 }
424
425 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
426 {
427   vertexBuffer->SetFormat(format.Release());
428 }
429
430 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
431 {
432   vertexBuffer->SetData(data.Release(), size);
433 }
434
435 void RenderManager::SetVertexBufferUpdateCallback(Render::VertexBuffer* vertexBuffer, Dali::VertexBufferUpdateCallback* callback)
436 {
437   vertexBuffer->SetVertexBufferUpdateCallback(callback);
438 }
439
440 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Render::Geometry::Uint16ContainerType& indices)
441 {
442   geometry->SetIndexBuffer(indices);
443 }
444
445 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Render::Geometry::Uint32ContainerType& indices)
446 {
447   geometry->SetIndexBuffer(indices);
448 }
449
450 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
451 {
452   mImpl->geometryContainer.PushBack(geometry.Release());
453 }
454
455 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
456 {
457   mImpl->geometryContainer.EraseObject(geometry);
458 }
459
460 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
461 {
462   geometry->AddVertexBuffer(vertexBuffer);
463 }
464
465 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
466 {
467   geometry->RemoveVertexBuffer(vertexBuffer);
468 }
469
470 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
471 {
472   geometry->SetType(Render::Geometry::Type(geometryType));
473 }
474
475 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
476 {
477   mImpl->AddRenderTracker(renderTracker);
478 }
479
480 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
481 {
482   mImpl->RemoveRenderTracker(renderTracker);
483 }
484
485 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear)
486 {
487   DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
488   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "\n\nNewFrame %d\n", mImpl->frameCount);
489
490   // Increment the frame count at the beginning of each frame
491   ++mImpl->frameCount;
492
493   // Process messages queued during previous update
494   mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
495
496   uint32_t totalInstructionCount = 0u;
497   for(auto& i : mImpl->sceneContainer)
498   {
499     totalInstructionCount += i->GetRenderInstructions().Count(mImpl->renderBufferIndex);
500   }
501
502   const bool haveInstructions = totalInstructionCount > 0u;
503
504   DALI_LOG_INFO(gLogFilter, Debug::General, "Render: haveInstructions(%s) || mImpl->lastFrameWasRendered(%s) || forceClear(%s)\n", haveInstructions ? "true" : "false", mImpl->lastFrameWasRendered ? "true" : "false", forceClear ? "true" : "false");
505
506   // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
507   if(haveInstructions || mImpl->lastFrameWasRendered || forceClear)
508   {
509     DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n");
510
511     // Upload the geometries
512     for(auto&& geom : mImpl->geometryContainer)
513     {
514       geom->Upload(mImpl->graphicsController);
515     }
516   }
517
518   // Reset pipeline cache before rendering
519   mImpl->pipelineCache->PreRender();
520
521   // Let we collect reference counts during CACHE_CLEAN_FRAME_COUNT frames.
522   // TODO : Cache clean logic have some problem now. Just block it until bug resolved
523   /*
524   if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 1)
525   {
526     mImpl->programController.ResetReferenceCount();
527     mImpl->shaderCache.ResetReferenceCount();
528   }
529   */
530
531   mImpl->commandBufferSubmitted = false;
532 }
533
534 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
535 {
536   if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
537   {
538     return;
539   }
540
541   Internal::Scene&   sceneInternal = GetImplementation(scene);
542   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
543
544   if(!sceneObject || sceneObject->IsRenderingSkipped())
545   {
546     // We don't need to calculate dirty rects
547     return;
548   }
549
550   class DamagedRectsCleaner
551   {
552   public:
553     explicit DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects, Rect<int>& surfaceRect)
554     : mDamagedRects(damagedRects),
555       mSurfaceRect(surfaceRect),
556       mCleanOnReturn(true)
557     {
558     }
559
560     void SetCleanOnReturn(bool cleanOnReturn)
561     {
562       mCleanOnReturn = cleanOnReturn;
563     }
564
565     ~DamagedRectsCleaner()
566     {
567       if(mCleanOnReturn)
568       {
569         mDamagedRects.clear();
570         mDamagedRects.push_back(mSurfaceRect);
571       }
572     }
573
574   private:
575     std::vector<Rect<int>>& mDamagedRects;
576     Rect<int>               mSurfaceRect;
577     bool                    mCleanOnReturn;
578   };
579
580   Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
581
582   // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
583   DamagedRectsCleaner damagedRectCleaner(damagedRects, surfaceRect);
584   bool                cleanDamagedRect = false;
585
586   Scene::ItemsDirtyRectsContainer& itemsDirtyRects = sceneObject->GetItemsDirtyRects();
587
588   if(!sceneObject->IsPartialUpdateEnabled())
589   {
590     // Clear all dirty rects
591     // The rects will be added when partial updated is enabled again
592     itemsDirtyRects.clear();
593     return;
594   }
595
596   // Mark previous dirty rects in the std::unordered_map.
597   for(auto& dirtyRectPair : itemsDirtyRects)
598   {
599     dirtyRectPair.second.visited = false;
600   }
601
602   uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
603   for(uint32_t i = 0; i < instructionCount; ++i)
604   {
605     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
606
607     if(instruction.mFrameBuffer)
608     {
609       cleanDamagedRect = true;
610       continue; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
611     }
612
613     const Camera* camera = instruction.GetCamera();
614     if(camera && camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
615     {
616       Vector3    position;
617       Vector3    scale;
618       Quaternion orientation;
619       camera->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
620
621       Vector3 orientationAxis;
622       Radian  orientationAngle;
623       orientation.ToAxisAngle(orientationAxis, orientationAngle);
624
625       if(position.x > Math::MACHINE_EPSILON_10000 ||
626          position.y > Math::MACHINE_EPSILON_10000 ||
627          orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
628          orientationAngle != ANGLE_180 ||
629          scale != Vector3(1.0f, 1.0f, 1.0f))
630       {
631         cleanDamagedRect = true;
632         continue;
633       }
634     }
635     else
636     {
637       cleanDamagedRect = true;
638       continue;
639     }
640
641     Rect<int32_t> viewportRect;
642     if(instruction.mIsViewportSet)
643     {
644       const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
645       viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
646       if(viewportRect.IsEmpty() || !viewportRect.IsValid())
647       {
648         cleanDamagedRect = true;
649         continue; // just skip funny use cases for now, empty viewport means it is set somewhere else
650       }
651     }
652     else
653     {
654       viewportRect = surfaceRect;
655     }
656
657     const Matrix* viewMatrix       = instruction.GetViewMatrix(mImpl->renderBufferIndex);
658     const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
659     if(viewMatrix && projectionMatrix)
660     {
661       const RenderListContainer::SizeType count = instruction.RenderListCount();
662       for(RenderListContainer::SizeType index = 0u; index < count; ++index)
663       {
664         const RenderList* renderList = instruction.GetRenderList(index);
665         if(renderList)
666         {
667           if(!renderList->IsEmpty())
668           {
669             const std::size_t listCount = renderList->Count();
670             for(uint32_t listIndex = 0u; listIndex < listCount; ++listIndex)
671             {
672               RenderItem& item = renderList->GetItem(listIndex);
673               // If the item does 3D transformation, make full update
674               if(item.mUpdateArea == Vector4::ZERO)
675               {
676                 cleanDamagedRect = true;
677
678                 // Save the full rect in the damaged list. We need it when this item is removed
679                 DirtyRectKey dirtyRectKey(item.mNode, item.mRenderer);
680                 auto         dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
681                 if(dirtyRectPos != itemsDirtyRects.end())
682                 {
683                   // Replace the rect
684                   dirtyRectPos->second.visited = true;
685                   dirtyRectPos->second.rect    = surfaceRect;
686                 }
687                 else
688                 {
689                   // Else, just insert the new dirtyrect
690                   itemsDirtyRects.insert({dirtyRectKey, surfaceRect});
691                 }
692                 continue;
693               }
694
695               Rect<int>    rect;
696               DirtyRectKey dirtyRectKey(item.mNode, item.mRenderer);
697               // If the item refers to updated node or renderer.
698               if(item.mIsUpdated ||
699                  (item.mNode &&
700                   (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated()))))
701               {
702                 item.mIsUpdated = false;
703
704                 rect = CalculateUpdateArea(item, mImpl->renderBufferIndex, viewportRect);
705                 if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
706                 {
707                   AlignDamagedRect(rect);
708
709                   // Found valid dirty rect.
710                   auto dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
711                   if(dirtyRectPos != itemsDirtyRects.end())
712                   {
713                     Rect<int> currentRect = rect;
714
715                     // Same item, merge it with the previous rect
716                     rect.Merge(dirtyRectPos->second.rect);
717
718                     // Replace the rect as current
719                     dirtyRectPos->second.visited = true;
720                     dirtyRectPos->second.rect    = currentRect;
721                   }
722                   else
723                   {
724                     // Else, just insert the new dirtyrect
725                     itemsDirtyRects.insert({dirtyRectKey, rect});
726                   }
727
728                   damagedRects.push_back(rect);
729                 }
730               }
731               else
732               {
733                 // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
734                 // 2. Mark the related dirty rects as visited so they will not be removed below.
735                 auto dirtyRectPos = itemsDirtyRects.find(dirtyRectKey);
736                 if(dirtyRectPos != itemsDirtyRects.end())
737                 {
738                   dirtyRectPos->second.visited = true;
739                 }
740                 else
741                 {
742                   // The item is not in the list for some reason. Add the current rect!
743                   rect = CalculateUpdateArea(item, mImpl->renderBufferIndex, viewportRect);
744                   if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
745                   {
746                     AlignDamagedRect(rect);
747
748                     itemsDirtyRects.insert({dirtyRectKey, rect});
749                   }
750                   cleanDamagedRect = true; // And make full update at this frame
751                 }
752               }
753             }
754           }
755         }
756       }
757     }
758   }
759
760   // Check removed nodes or removed renderers dirty rects
761   // Note, std::unordered_map end iterator is validate if we call erase.
762   for(auto iter = itemsDirtyRects.cbegin(), iterEnd = itemsDirtyRects.cend(); iter != iterEnd;)
763   {
764     if(!iter->second.visited)
765     {
766       damagedRects.push_back(iter->second.rect);
767       iter = itemsDirtyRects.erase(iter);
768     }
769     else
770     {
771       ++iter;
772     }
773   }
774
775   if(sceneObject->IsNeededFullUpdate())
776   {
777     cleanDamagedRect = true; // And make full update at this frame
778   }
779
780   if(!cleanDamagedRect)
781   {
782     damagedRectCleaner.SetCleanOnReturn(false);
783   }
784 }
785
786 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
787 {
788   SceneGraph::Scene* sceneObject = GetImplementation(scene).GetSceneObject();
789   if(!sceneObject)
790   {
791     return;
792   }
793
794   Rect<int> clippingRect = sceneObject->GetSurfaceRect();
795   RenderScene(status, scene, renderToFbo, clippingRect);
796 }
797
798 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
799 {
800   if(mImpl->partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE && !renderToFbo && clippingRect.IsEmpty())
801   {
802     // ClippingRect is empty. Skip rendering
803     return;
804   }
805
806   // Reset main algorithms command buffer
807   mImpl->renderAlgorithms.ResetCommandBuffer();
808
809   auto mainCommandBuffer = mImpl->renderAlgorithms.GetMainCommandBuffer();
810
811   Internal::Scene&   sceneInternal = GetImplementation(scene);
812   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
813   if(!sceneObject)
814   {
815     return;
816   }
817
818   uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
819
820   std::vector<Graphics::RenderTarget*> targetstoPresent;
821
822   Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
823   if(clippingRect == surfaceRect)
824   {
825     // Full rendering case
826     // Make clippingRect empty because we're doing full rendering now if the clippingRect is empty.
827     // To reduce side effects, keep this logic now.
828     clippingRect = Rect<int>();
829   }
830
831   // Prefetch programs before we start rendering so reflection is
832   // ready, and we can pull exact size of UBO needed (no need to resize during drawing)
833   auto totalSizeCPU = 0u;
834   auto totalSizeGPU = 0u;
835
836   for(uint32_t i = 0; i < instructionCount; ++i)
837   {
838     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
839
840     if((instruction.mFrameBuffer != nullptr && renderToFbo) ||
841        (instruction.mFrameBuffer == nullptr && !renderToFbo))
842     {
843       for(auto j = 0u; j < instruction.RenderListCount(); ++j)
844       {
845         const auto& renderList = instruction.GetRenderList(j);
846         for(auto k = 0u; k < renderList->Count(); ++k)
847         {
848           auto& item = renderList->GetItem(k);
849           if(item.mRenderer && item.mRenderer->NeedsProgram())
850           {
851             // Prepare and store used programs for further processing
852             auto program = item.mRenderer->PrepareProgram(instruction);
853             if(program)
854             {
855               auto memoryRequirements = program->GetUniformBlocksMemoryRequirements();
856
857               totalSizeCPU += memoryRequirements.totalCpuSizeRequired;
858               totalSizeGPU += memoryRequirements.totalGpuSizeRequired;
859             }
860           }
861         }
862       }
863     }
864   }
865
866   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Render scene (%s), CPU:%d GPU:%d\n", renderToFbo ? "Offscreen" : "Onscreen", totalSizeCPU, totalSizeGPU);
867
868   auto& uboManager = mImpl->uniformBufferManager;
869
870   uboManager->SetCurrentSceneRenderInfo(sceneObject, renderToFbo);
871   uboManager->Rollback(sceneObject, renderToFbo);
872
873   // Respec UBOs for this frame (orphan buffers or double buffer in the GPU)
874   if(instructionCount)
875   {
876     uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, true)->ReSpecify(totalSizeCPU);
877     uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, false)->ReSpecify(totalSizeGPU);
878   }
879
880 #if defined(DEBUG_ENABLED)
881   auto uniformBuffer1 = uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, true);
882   auto uniformBuffer2 = uboManager->GetUniformBufferForScene(sceneObject, renderToFbo, false);
883   if(uniformBuffer1)
884   {
885     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "CPU buffer: Offset(%d), Cap(%d)\n", uniformBuffer1->GetCurrentOffset(), uniformBuffer1->GetCurrentCapacity());
886   }
887   else
888   {
889     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "CPU buffer: nil\n");
890   }
891   if(uniformBuffer2)
892   {
893     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GPU buffer: Offset(%d), Cap(%d)\n", uniformBuffer2->GetCurrentOffset(), uniformBuffer2->GetCurrentCapacity());
894   }
895   else
896   {
897     DALI_LOG_INFO(gLogFilter, Debug::Verbose, "GPU buffer: nil\n");
898   }
899 #endif
900
901   for(uint32_t i = 0; i < instructionCount; ++i)
902   {
903     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
904
905     if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
906     {
907       continue; // skip
908     }
909
910     // Mark that we will require a post-render step to be performed (includes swap-buffers).
911     status.SetNeedsPostRender(true);
912
913     Rect<int32_t> viewportRect;
914
915     int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation() + sceneObject->GetScreenOrientation();
916     if(surfaceOrientation >= 360)
917     {
918       surfaceOrientation -= 360;
919     }
920
921     // @todo Should these be part of scene?
922     Integration::DepthBufferAvailable   depthBufferAvailable   = mImpl->depthBufferAvailable;
923     Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
924
925     Graphics::RenderTarget*           currentRenderTarget = nullptr;
926     Graphics::RenderPass*             currentRenderPass   = nullptr;
927     std::vector<Graphics::ClearValue> currentClearValues{};
928
929     if(instruction.mFrameBuffer)
930     {
931       // Ensure graphics framebuffer is created, bind attachments and create render passes
932       // Only happens once per framebuffer. If the create fails, e.g. no attachments yet,
933       // then don't render to this framebuffer.
934       if(!instruction.mFrameBuffer->GetGraphicsObject())
935       {
936         const bool created = instruction.mFrameBuffer->CreateGraphicsObjects();
937         if(!created)
938         {
939           continue;
940         }
941       }
942
943       auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues();
944
945       // Set the clear color for first color attachment
946       if(instruction.mIsClearColorSet && !clearValues.empty())
947       {
948         clearValues[0].color = {
949           instruction.mClearColor.r,
950           instruction.mClearColor.g,
951           instruction.mClearColor.b,
952           instruction.mClearColor.a};
953       }
954
955       currentClearValues = clearValues;
956
957       auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
958
959       // offscreen buffer
960       currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget();
961       currentRenderPass   = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
962     }
963     else // no framebuffer
964     {
965       // surface
966       auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues();
967
968       if(instruction.mIsClearColorSet)
969       {
970         clearValues[0].color = {
971           instruction.mClearColor.r,
972           instruction.mClearColor.g,
973           instruction.mClearColor.b,
974           instruction.mClearColor.a};
975       }
976
977       currentClearValues = clearValues;
978
979       // @todo SceneObject should already have the depth clear / stencil clear in the clearValues array.
980       // if the window has a depth/stencil buffer.
981       if((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE ||
982           stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) &&
983          (currentClearValues.size() <= 1))
984       {
985         currentClearValues.emplace_back();
986         currentClearValues.back().depthStencil.depth   = 0;
987         currentClearValues.back().depthStencil.stencil = 0;
988       }
989
990       auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD;
991
992       currentRenderTarget = sceneObject->GetSurfaceRenderTarget();
993       currentRenderPass   = sceneObject->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE);
994     }
995
996     targetstoPresent.emplace_back(currentRenderTarget);
997
998     // reset the program matrices for all programs once per frame
999     // this ensures we will set view and projection matrix once per program per camera
1000     mImpl->programController.ResetProgramMatrices();
1001
1002     if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
1003     {
1004       // Offscreen buffer rendering
1005       if(instruction.mIsViewportSet)
1006       {
1007         // For Viewport the lower-left corner is (0,0)
1008         const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
1009         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
1010       }
1011       else
1012       {
1013         viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
1014       }
1015       surfaceOrientation = 0;
1016     }
1017     else // No Offscreen frame buffer rendering
1018     {
1019       // Check whether a viewport is specified, otherwise the full surface size is used
1020       if(instruction.mIsViewportSet)
1021       {
1022         // For Viewport the lower-left corner is (0,0)
1023         const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
1024         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
1025       }
1026       else
1027       {
1028         viewportRect = surfaceRect;
1029       }
1030     }
1031
1032     // Set surface orientation
1033     // @todo Inform graphics impl by another route.
1034     // was: mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
1035
1036     /*** Clear region of framebuffer or surface before drawing ***/
1037     bool clearFullFrameRect = (surfaceRect == viewportRect);
1038     if(instruction.mFrameBuffer != nullptr)
1039     {
1040       Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
1041       clearFullFrameRect = (frameRect == viewportRect);
1042     }
1043
1044     if(!clippingRect.IsEmpty())
1045     {
1046       if(!clippingRect.Intersect(viewportRect))
1047       {
1048         DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
1049         clippingRect = Rect<int>();
1050       }
1051       clearFullFrameRect = false;
1052     }
1053
1054     Graphics::Rect2D scissorArea{viewportRect.x, viewportRect.y, uint32_t(viewportRect.width), uint32_t(viewportRect.height)};
1055     if(instruction.mIsClearColorSet)
1056     {
1057       if(!clearFullFrameRect)
1058       {
1059         if(!clippingRect.IsEmpty())
1060         {
1061           scissorArea = {clippingRect.x, clippingRect.y, uint32_t(clippingRect.width), uint32_t(clippingRect.height)};
1062         }
1063       }
1064     }
1065
1066     // Scissor's value should be set based on the default system coordinates.
1067     // When the surface is rotated, the input values already were set with the rotated angle.
1068     // So, re-calculation is needed.
1069     scissorArea = RecalculateScissorArea(scissorArea, surfaceOrientation, surfaceRect);
1070
1071     // Begin render pass
1072     mainCommandBuffer->BeginRenderPass(
1073       currentRenderPass,
1074       currentRenderTarget,
1075       scissorArea,
1076       currentClearValues);
1077
1078     mainCommandBuffer->SetViewport({float(viewportRect.x),
1079                                     float(viewportRect.y),
1080                                     float(viewportRect.width),
1081                                     float(viewportRect.height)});
1082
1083     mImpl->renderAlgorithms.ProcessRenderInstruction(
1084       instruction,
1085       mImpl->renderBufferIndex,
1086       depthBufferAvailable,
1087       stencilBufferAvailable,
1088       viewportRect,
1089       clippingRect,
1090       surfaceOrientation,
1091       Uint16Pair(surfaceRect.width, surfaceRect.height));
1092
1093     Graphics::SyncObject* syncObject{nullptr};
1094     // If the render instruction has an associated render tracker (owned separately)
1095     // and framebuffer, create a one shot sync object, and use it to determine when
1096     // the render pass has finished executing on GPU.
1097     if(instruction.mRenderTracker && instruction.mFrameBuffer)
1098     {
1099       syncObject                 = instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController);
1100       instruction.mRenderTracker = nullptr;
1101     }
1102     mainCommandBuffer->EndRenderPass(syncObject);
1103   }
1104
1105   // Flush UBOs
1106   mImpl->uniformBufferManager->Flush(sceneObject, renderToFbo);
1107
1108   mImpl->renderAlgorithms.SubmitCommandBuffer();
1109   mImpl->commandBufferSubmitted = true;
1110
1111   std::sort(targetstoPresent.begin(), targetstoPresent.end());
1112
1113   Graphics::RenderTarget* rt = nullptr;
1114   for(auto& target : targetstoPresent)
1115   {
1116     if(target != rt)
1117     {
1118       mImpl->graphicsController.PresentRenderTarget(target);
1119       rt = target;
1120     }
1121   }
1122 }
1123
1124 void RenderManager::PostRender()
1125 {
1126   if(!mImpl->commandBufferSubmitted)
1127   {
1128     // Rendering is skipped but there may be pending tasks. Flush them.
1129     Graphics::SubmitInfo submitInfo;
1130     submitInfo.cmdBuffer.clear(); // Only flush
1131     submitInfo.flags = 0 | Graphics::SubmitFlagBits::FLUSH;
1132     mImpl->graphicsController.SubmitCommandBuffers(submitInfo);
1133
1134     mImpl->commandBufferSubmitted = true;
1135   }
1136
1137   // Notify RenderGeometries that rendering has finished
1138   for(auto&& iter : mImpl->geometryContainer)
1139   {
1140     iter->OnRenderFinished();
1141   }
1142
1143   // Notify updated RenderTexture that rendering has finished
1144   for(auto&& iter : mImpl->updatedTextures)
1145   {
1146     iter->OnRenderFinished();
1147   }
1148   mImpl->updatedTextures.Clear();
1149
1150   // Remove discarded textures after OnRenderFinished called
1151   mImpl->textureDiscardQueue.Clear();
1152
1153   mImpl->UpdateTrackers();
1154
1155   uint32_t count = 0u;
1156   for(auto& scene : mImpl->sceneContainer)
1157   {
1158     count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
1159   }
1160
1161   // Remove unused shader and programs during CACHE_CLEAN_FRAME_COUNT frames.
1162   // TODO : Cache clean logic have some problem now. Just block it until bug resolved
1163   /*
1164   if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 0)
1165   {
1166     mImpl->programController.ClearUnusedCache();
1167     mImpl->shaderCache.ClearUnusedCache();
1168   }
1169   */
1170
1171   const bool haveInstructions = count > 0u;
1172
1173   // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
1174   mImpl->lastFrameWasRendered = haveInstructions;
1175
1176   /**
1177    * The rendering has finished; swap to the next buffer.
1178    * Ideally the update has just finished using this buffer; otherwise the render thread
1179    * should block until the update has finished.
1180    */
1181   mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1182
1183   DALI_PRINT_RENDER_END();
1184 }
1185
1186 } // namespace SceneGraph
1187
1188 } // namespace Internal
1189
1190 } // namespace Dali