Create UBO for uniform buffer bindings
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
1 /*
2  * Copyright (c) 2021 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/integration-api/gl-context-helper-abstraction.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/render-tasks/scene-graph-camera.h>
33
34 #include <dali/internal/render/common/render-algorithms.h>
35 #include <dali/internal/render/common/render-debug.h>
36 #include <dali/internal/render/common/render-instruction.h>
37 #include <dali/internal/render/common/render-tracker.h>
38 #include <dali/internal/render/queue/render-queue.h>
39 #include <dali/internal/render/renderers/render-frame-buffer.h>
40 #include <dali/internal/render/renderers/render-texture.h>
41 #include <dali/internal/render/renderers/shader-cache.h>
42 #include <dali/internal/render/renderers/uniform-buffer-manager.h>
43 #include <dali/internal/render/shaders/program-controller.h>
44
45 namespace Dali
46 {
47 namespace Internal
48 {
49 namespace SceneGraph
50 {
51 #if defined(DEBUG_ENABLED)
52 namespace
53 {
54 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER");
55 } // unnamed namespace
56 #endif
57
58 /**
59  * Structure to contain internal data
60  */
61 struct RenderManager::Impl
62 {
63   Impl(Graphics::Controller&               graphicsController,
64        Integration::DepthBufferAvailable   depthBufferAvailableParam,
65        Integration::StencilBufferAvailable stencilBufferAvailableParam,
66        Integration::PartialUpdateAvailable partialUpdateAvailableParam)
67   : context(graphicsController.GetGlAbstraction(), &sceneContextContainer),
68     currentContext(&context),
69     graphicsController(graphicsController),
70     renderQueue(),
71     renderAlgorithms(),
72     frameCount(0u),
73     renderBufferIndex(SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX),
74     rendererContainer(),
75     samplerContainer(),
76     textureContainer(),
77     frameBufferContainer(),
78     lastFrameWasRendered(false),
79     programController(graphicsController),
80     shaderCache(graphicsController),
81     depthBufferAvailable(depthBufferAvailableParam),
82     stencilBufferAvailable(stencilBufferAvailableParam),
83     partialUpdateAvailable(partialUpdateAvailableParam)
84   {
85     // Create thread pool with just one thread ( there may be a need to create more threads in the future ).
86     threadPool = std::unique_ptr<Dali::ThreadPool>(new Dali::ThreadPool());
87     threadPool->Initialize(1u);
88
89     uniformBufferManager.reset(new Render::UniformBufferManager(&graphicsController));
90   }
91
92   ~Impl()
93   {
94     threadPool.reset(nullptr); // reset now to maintain correct destruction order
95   }
96
97   void AddRenderTracker(Render::RenderTracker* renderTracker)
98   {
99     DALI_ASSERT_DEBUG(renderTracker != NULL);
100     mRenderTrackers.PushBack(renderTracker);
101   }
102
103   void RemoveRenderTracker(Render::RenderTracker* renderTracker)
104   {
105     mRenderTrackers.EraseObject(renderTracker);
106   }
107
108   Context* CreateSceneContext()
109   {
110     Context* context = new Context(graphicsController.GetGlAbstraction());
111     sceneContextContainer.PushBack(context);
112     return context;
113   }
114
115   void DestroySceneContext(Context* sceneContext)
116   {
117     auto iter = std::find(sceneContextContainer.Begin(), sceneContextContainer.End(), sceneContext);
118     if(iter != sceneContextContainer.End())
119     {
120       (*iter)->GlContextDestroyed();
121       sceneContextContainer.Erase(iter);
122     }
123   }
124
125   Context* ReplaceSceneContext(Context* oldSceneContext)
126   {
127     Context* newContext = new Context(graphicsController.GetGlAbstraction());
128
129     oldSceneContext->GlContextDestroyed();
130
131     std::replace(sceneContextContainer.begin(), sceneContextContainer.end(), oldSceneContext, newContext);
132     return newContext;
133   }
134
135   void UpdateTrackers()
136   {
137     for(auto&& iter : mRenderTrackers)
138     {
139       iter->PollSyncObject();
140     }
141   }
142
143   // the order is important for destruction,
144   // programs are owned by context at the moment.
145   Context                  context;               ///< Holds the GL state of the share resource context
146   Context*                 currentContext;        ///< Holds the GL state of the current context for rendering
147   OwnerContainer<Context*> sceneContextContainer; ///< List of owned contexts holding the GL state per scene
148   Graphics::Controller&    graphicsController;
149   RenderQueue              renderQueue; ///< A message queue for receiving messages from the update-thread.
150
151   std::vector<SceneGraph::Scene*> sceneContainer; ///< List of pointers to the scene graph objects of the scenes
152
153   Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction
154
155   uint32_t    frameCount;        ///< The current frame count
156   BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer
157
158   OwnerContainer<Render::Renderer*>     rendererContainer;     ///< List of owned renderers
159   OwnerContainer<Render::Sampler*>      samplerContainer;      ///< List of owned samplers
160   OwnerContainer<Render::Texture*>      textureContainer;      ///< List of owned textures
161   OwnerContainer<Render::FrameBuffer*>  frameBufferContainer;  ///< List of owned framebuffers
162   OwnerContainer<Render::VertexBuffer*> vertexBufferContainer; ///< List of owned vertex buffers
163   OwnerContainer<Render::Geometry*>     geometryContainer;     ///< List of owned Geometries
164
165   bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions
166
167   OwnerContainer<Render::RenderTracker*> mRenderTrackers; ///< List of render trackers
168
169   ProgramController   programController; ///< Owner of the GL programs
170   Render::ShaderCache shaderCache;       ///< The cache for the graphics shaders
171
172   std::unique_ptr<Render::UniformBufferManager> uniformBufferManager; ///< The uniform buffer manager
173
174   Integration::DepthBufferAvailable   depthBufferAvailable;   ///< Whether the depth buffer is available
175   Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available
176   Integration::PartialUpdateAvailable partialUpdateAvailable; ///< Whether the partial update is available
177
178   std::unique_ptr<Dali::ThreadPool> threadPool;            ///< The thread pool
179   Vector<Graphics::Texture*>        boundTextures;         ///< The textures bound for rendering
180   Vector<Graphics::Texture*>        textureDependencyList; ///< The dependency list of binded textures
181 };
182
183 RenderManager* RenderManager::New(Graphics::Controller&               graphicsController,
184                                   Integration::DepthBufferAvailable   depthBufferAvailable,
185                                   Integration::StencilBufferAvailable stencilBufferAvailable,
186                                   Integration::PartialUpdateAvailable partialUpdateAvailable)
187 {
188   RenderManager* manager = new RenderManager;
189   manager->mImpl         = new Impl(graphicsController,
190                             depthBufferAvailable,
191                             stencilBufferAvailable,
192                             partialUpdateAvailable);
193   return manager;
194 }
195
196 RenderManager::RenderManager()
197 : mImpl(nullptr)
198 {
199 }
200
201 RenderManager::~RenderManager()
202 {
203   delete mImpl;
204 }
205
206 RenderQueue& RenderManager::GetRenderQueue()
207 {
208   return mImpl->renderQueue;
209 }
210
211 void RenderManager::ContextCreated()
212 {
213   mImpl->context.GlContextCreated();
214   mImpl->programController.GlContextCreated();
215
216   // renderers, textures and gpu buffers cannot reinitialize themselves
217   // so they rely on someone reloading the data for them
218 }
219
220 void RenderManager::ContextDestroyed()
221 {
222   mImpl->context.GlContextDestroyed();
223   mImpl->programController.GlContextDestroyed();
224
225   //Inform textures
226   for(auto&& texture : mImpl->textureContainer)
227   {
228     texture->Destroy();
229   }
230
231   //Inform framebuffers
232   for(auto&& framebuffer : mImpl->frameBufferContainer)
233   {
234     framebuffer->GlContextDestroyed();
235   }
236
237   // inform renderers
238   for(auto&& renderer : mImpl->rendererContainer)
239   {
240     renderer->GlContextDestroyed();
241   }
242
243   // inform context
244   for(auto&& context : mImpl->sceneContextContainer)
245   {
246     context->GlContextDestroyed();
247   }
248 }
249
250 void RenderManager::SetShaderSaver(ShaderSaver& upstream)
251 {
252   mImpl->programController.SetShaderSaver(upstream);
253 }
254
255 void RenderManager::AddRenderer(OwnerPointer<Render::Renderer>& renderer)
256 {
257   // Initialize the renderer as we are now in render thread
258   renderer->Initialize(mImpl->context, mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()));
259
260   mImpl->rendererContainer.PushBack(renderer.Release());
261 }
262
263 void RenderManager::RemoveRenderer(Render::Renderer* renderer)
264 {
265   mImpl->rendererContainer.EraseObject(renderer);
266 }
267
268 void RenderManager::AddSampler(OwnerPointer<Render::Sampler>& sampler)
269 {
270   sampler->Initialize(mImpl->graphicsController);
271   mImpl->samplerContainer.PushBack(sampler.Release());
272 }
273
274 void RenderManager::RemoveSampler(Render::Sampler* sampler)
275 {
276   mImpl->samplerContainer.EraseObject(sampler);
277 }
278
279 void RenderManager::AddTexture(OwnerPointer<Render::Texture>& texture)
280 {
281   texture->Initialize(mImpl->graphicsController);
282   mImpl->textureContainer.PushBack(texture.Release());
283 }
284
285 void RenderManager::RemoveTexture(Render::Texture* texture)
286 {
287   DALI_ASSERT_DEBUG(NULL != texture);
288
289   // Find the texture, use reference to pointer so we can do the erase safely
290   for(auto&& iter : mImpl->textureContainer)
291   {
292     if(iter == texture)
293     {
294       texture->Destroy();
295       mImpl->textureContainer.Erase(&iter); // Texture found; now destroy it
296       return;
297     }
298   }
299 }
300
301 void RenderManager::UploadTexture(Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params)
302 {
303   texture->Upload(pixelData, params);
304 }
305
306 void RenderManager::GenerateMipmaps(Render::Texture* texture)
307 {
308   texture->GenerateMipmaps();
309 }
310
311 void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode)
312 {
313   sampler->SetFilterMode(static_cast<Dali::FilterMode::Type>(minFilterMode),
314                          static_cast<Dali::FilterMode::Type>(magFilterMode));
315 }
316
317 void RenderManager::SetWrapMode(Render::Sampler* sampler, uint32_t rWrapMode, uint32_t sWrapMode, uint32_t tWrapMode)
318 {
319   sampler->SetWrapMode(static_cast<Dali::WrapMode::Type>(rWrapMode),
320                        static_cast<Dali::WrapMode::Type>(sWrapMode),
321                        static_cast<Dali::WrapMode::Type>(tWrapMode));
322 }
323
324 void RenderManager::AddFrameBuffer(OwnerPointer<Render::FrameBuffer>& frameBuffer)
325 {
326   Render::FrameBuffer* frameBufferPtr = frameBuffer.Release();
327   mImpl->frameBufferContainer.PushBack(frameBufferPtr);
328   frameBufferPtr->Initialize(mImpl->context);
329 }
330
331 void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer)
332 {
333   DALI_ASSERT_DEBUG(NULL != frameBuffer);
334
335   // Find the sampler, use reference so we can safely do the erase
336   for(auto&& iter : mImpl->frameBufferContainer)
337   {
338     if(iter == frameBuffer)
339     {
340       frameBuffer->Destroy(mImpl->context);
341       mImpl->frameBufferContainer.Erase(&iter); // frameBuffer found; now destroy it
342
343       break;
344     }
345   }
346 }
347
348 void RenderManager::InitializeScene(SceneGraph::Scene* scene)
349 {
350   scene->Initialize(*mImpl->CreateSceneContext());
351   mImpl->sceneContainer.push_back(scene);
352 }
353
354 void RenderManager::UninitializeScene(SceneGraph::Scene* scene)
355 {
356   mImpl->DestroySceneContext(scene->GetContext());
357
358   auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene);
359   if(iter != mImpl->sceneContainer.end())
360   {
361     mImpl->sceneContainer.erase(iter);
362   }
363 }
364
365 void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene)
366 {
367   Context* newContext = mImpl->ReplaceSceneContext(scene->GetContext());
368   scene->Initialize(*newContext);
369 }
370
371 void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
372 {
373   frameBuffer->AttachColorTexture(mImpl->context, texture, mipmapLevel, layer);
374 }
375
376 void RenderManager::AttachDepthTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
377 {
378   frameBuffer->AttachDepthTexture(mImpl->context, texture, mipmapLevel);
379 }
380
381 void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel)
382 {
383   frameBuffer->AttachDepthStencilTexture(mImpl->context, texture, mipmapLevel);
384 }
385
386 void RenderManager::AddVertexBuffer(OwnerPointer<Render::VertexBuffer>& vertexBuffer)
387 {
388   mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release());
389 }
390
391 void RenderManager::RemoveVertexBuffer(Render::VertexBuffer* vertexBuffer)
392 {
393   mImpl->vertexBufferContainer.EraseObject(vertexBuffer);
394 }
395
396 void RenderManager::SetVertexBufferFormat(Render::VertexBuffer* vertexBuffer, OwnerPointer<Render::VertexBuffer::Format>& format)
397 {
398   vertexBuffer->SetFormat(format.Release());
399 }
400
401 void RenderManager::SetVertexBufferData(Render::VertexBuffer* vertexBuffer, OwnerPointer<Vector<uint8_t>>& data, uint32_t size)
402 {
403   vertexBuffer->SetData(data.Release(), size);
404 }
405
406 void RenderManager::SetIndexBuffer(Render::Geometry* geometry, Dali::Vector<uint16_t>& indices)
407 {
408   geometry->SetIndexBuffer(indices);
409 }
410
411 void RenderManager::AddGeometry(OwnerPointer<Render::Geometry>& geometry)
412 {
413   mImpl->geometryContainer.PushBack(geometry.Release());
414 }
415
416 void RenderManager::RemoveGeometry(Render::Geometry* geometry)
417 {
418   mImpl->geometryContainer.EraseObject(geometry);
419 }
420
421 void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
422 {
423   DALI_ASSERT_DEBUG(NULL != geometry);
424
425   // Find the geometry
426   for(auto&& iter : mImpl->geometryContainer)
427   {
428     if(iter == geometry)
429     {
430       iter->AddVertexBuffer(vertexBuffer);
431       break;
432     }
433   }
434 }
435
436 void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer)
437 {
438   DALI_ASSERT_DEBUG(NULL != geometry);
439
440   // Find the geometry
441   for(auto&& iter : mImpl->geometryContainer)
442   {
443     if(iter == geometry)
444     {
445       iter->RemoveVertexBuffer(vertexBuffer);
446       break;
447     }
448   }
449 }
450
451 void RenderManager::SetGeometryType(Render::Geometry* geometry, uint32_t geometryType)
452 {
453   geometry->SetType(Render::Geometry::Type(geometryType));
454 }
455
456 void RenderManager::AddRenderTracker(Render::RenderTracker* renderTracker)
457 {
458   mImpl->AddRenderTracker(renderTracker);
459 }
460
461 void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker)
462 {
463   mImpl->RemoveRenderTracker(renderTracker);
464 }
465
466 ProgramCache* RenderManager::GetProgramCache()
467 {
468   return &(mImpl->programController);
469 }
470
471 void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear, bool uploadOnly)
472 {
473   DALI_PRINT_RENDER_START(mImpl->renderBufferIndex);
474
475   // Core::Render documents that GL context must be current before calling Render
476   DALI_ASSERT_DEBUG(mImpl->context.IsGlContextCreated());
477
478   // Increment the frame count at the beginning of each frame
479   ++mImpl->frameCount;
480
481   // Process messages queued during previous update
482   mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex);
483
484   uint32_t count = 0u;
485   for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i)
486   {
487     count += mImpl->sceneContainer[i]->GetRenderInstructions().Count(mImpl->renderBufferIndex);
488   }
489
490   const bool haveInstructions = count > 0u;
491
492   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");
493
494   // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required).
495   if(haveInstructions || mImpl->lastFrameWasRendered || forceClear)
496   {
497     DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n");
498
499     // Switch to the shared context
500     if(mImpl->currentContext != &mImpl->context)
501     {
502       mImpl->currentContext = &mImpl->context;
503
504       if(mImpl->currentContext->IsSurfacelessContextSupported())
505       {
506         mImpl->graphicsController.GetGlContextHelperAbstraction().MakeSurfacelessContextCurrent();
507       }
508
509       // Clear the current cached program when the context is switched
510       mImpl->programController.ClearCurrentProgram();
511     }
512
513     // Upload the geometries
514     for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i)
515     {
516       RenderInstructionContainer& instructions = mImpl->sceneContainer[i]->GetRenderInstructions();
517       for(uint32_t j = 0; j < instructions.Count(mImpl->renderBufferIndex); ++j)
518       {
519         RenderInstruction& instruction = instructions.At(mImpl->renderBufferIndex, j);
520
521         const Matrix* viewMatrix       = instruction.GetViewMatrix(mImpl->renderBufferIndex);
522         const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
523
524         DALI_ASSERT_DEBUG(viewMatrix);
525         DALI_ASSERT_DEBUG(projectionMatrix);
526
527         if(viewMatrix && projectionMatrix)
528         {
529           const RenderListContainer::SizeType renderListCount = instruction.RenderListCount();
530
531           // Iterate through each render list.
532           for(RenderListContainer::SizeType index = 0; index < renderListCount; ++index)
533           {
534             const RenderList* renderList = instruction.GetRenderList(index);
535
536             if(renderList && !renderList->IsEmpty())
537             {
538               const std::size_t itemCount = renderList->Count();
539               for(uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex)
540               {
541                 const RenderItem& item = renderList->GetItem(itemIndex);
542                 if(DALI_LIKELY(item.mRenderer))
543                 {
544                   item.mRenderer->Upload();
545                 }
546               }
547             }
548           }
549         }
550       }
551     }
552   }
553 }
554
555 void RenderManager::PreRender(Integration::Scene& scene, std::vector<Rect<int>>& damagedRects)
556 {
557   if(mImpl->partialUpdateAvailable != Integration::PartialUpdateAvailable::TRUE)
558   {
559     return;
560   }
561
562   Internal::Scene&   sceneInternal = GetImplementation(scene);
563   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
564
565   if(sceneObject->IsRenderingSkipped())
566   {
567     // We don't need to calculate dirty rects
568     return;
569   }
570
571   class DamagedRectsCleaner
572   {
573   public:
574     DamagedRectsCleaner(std::vector<Rect<int>>& damagedRects)
575     : mDamagedRects(damagedRects),
576       mCleanOnReturn(true)
577     {
578     }
579
580     void SetCleanOnReturn(bool cleanOnReturn)
581     {
582       mCleanOnReturn = cleanOnReturn;
583     }
584
585     ~DamagedRectsCleaner()
586     {
587       if(mCleanOnReturn)
588       {
589         mDamagedRects.clear();
590       }
591     }
592
593   private:
594     std::vector<Rect<int>>& mDamagedRects;
595     bool                    mCleanOnReturn;
596   };
597
598   Rect<int32_t> surfaceRect = sceneObject->GetSurfaceRect();
599
600   // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions.
601   DamagedRectsCleaner damagedRectCleaner(damagedRects);
602
603   // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number.
604   // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end());
605   std::vector<DirtyRect>& itemsDirtyRects = sceneInternal.GetItemsDirtyRects();
606   for(DirtyRect& dirtyRect : itemsDirtyRects)
607   {
608     dirtyRect.visited = false;
609   }
610
611   uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
612   for(uint32_t i = 0; i < count; ++i)
613   {
614     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
615
616     if(instruction.mFrameBuffer)
617     {
618       return; // TODO: reset, we don't deal with render tasks with framebuffers (for now)
619     }
620
621     const Camera* camera = instruction.GetCamera();
622     if(camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION)
623     {
624       const Node* node = instruction.GetCamera()->GetNode();
625       if(node)
626       {
627         Vector3    position;
628         Vector3    scale;
629         Quaternion orientation;
630         node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale);
631
632         Vector3 orientationAxis;
633         Radian  orientationAngle;
634         orientation.ToAxisAngle(orientationAxis, orientationAngle);
635
636         if(position.x > Math::MACHINE_EPSILON_10000 ||
637            position.y > Math::MACHINE_EPSILON_10000 ||
638            orientationAxis != Vector3(0.0f, 1.0f, 0.0f) ||
639            orientationAngle != ANGLE_180 ||
640            scale != Vector3(1.0f, 1.0f, 1.0f))
641         {
642           return;
643         }
644       }
645     }
646     else
647     {
648       return;
649     }
650
651     Rect<int32_t> viewportRect;
652     if(instruction.mIsViewportSet)
653     {
654       const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
655       viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
656       if(viewportRect.IsEmpty() || !viewportRect.IsValid())
657       {
658         return; // just skip funny use cases for now, empty viewport means it is set somewhere else
659       }
660     }
661     else
662     {
663       viewportRect = surfaceRect;
664     }
665
666     const Matrix* viewMatrix       = instruction.GetViewMatrix(mImpl->renderBufferIndex);
667     const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex);
668     if(viewMatrix && projectionMatrix)
669     {
670       const RenderListContainer::SizeType count = instruction.RenderListCount();
671       for(RenderListContainer::SizeType index = 0u; index < count; ++index)
672       {
673         const RenderList* renderList = instruction.GetRenderList(index);
674         if(renderList && !renderList->IsEmpty())
675         {
676           const std::size_t count = renderList->Count();
677           for(uint32_t index = 0u; index < count; ++index)
678           {
679             RenderItem& item = renderList->GetItem(index);
680             // If the item does 3D transformation, do early exit and clean the damaged rect array
681             if(item.mUpdateSize == Vector3::ZERO)
682             {
683               return;
684             }
685
686             Rect<int> rect;
687             DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect);
688             // If the item refers to updated node or renderer.
689             if(item.mIsUpdated ||
690                (item.mNode &&
691                 (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode)))))
692             {
693               item.mIsUpdated = false;
694               item.mNode->SetUpdated(false);
695
696               rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height);
697               if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty())
698               {
699                 const int left   = rect.x;
700                 const int top    = rect.y;
701                 const int right  = rect.x + rect.width;
702                 const int bottom = rect.y + rect.height;
703                 rect.x           = (left / 16) * 16;
704                 rect.y           = (top / 16) * 16;
705                 rect.width       = ((right + 16) / 16) * 16 - rect.x;
706                 rect.height      = ((bottom + 16) / 16) * 16 - rect.y;
707
708                 // Found valid dirty rect.
709                 // 1. Insert it in the sorted array of the dirty rects.
710                 // 2. Mark the related dirty rects as visited so they will not be removed below.
711                 // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1).
712                 dirtyRect.rect    = rect;
713                 auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
714                 dirtyRectPos      = itemsDirtyRects.insert(dirtyRectPos, dirtyRect);
715
716                 int c = 1;
717                 while(++dirtyRectPos != itemsDirtyRects.end())
718                 {
719                   if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
720                   {
721                     break;
722                   }
723
724                   dirtyRectPos->visited = true;
725                   Rect<int>& dirtRect   = dirtyRectPos->rect;
726                   rect.Merge(dirtRect);
727
728                   c++;
729                   if(c > 3) // no more then 3 previous rects
730                   {
731                     itemsDirtyRects.erase(dirtyRectPos);
732                     break;
733                   }
734                 }
735
736                 damagedRects.push_back(rect);
737               }
738             }
739             else
740             {
741               // 1. The item is not dirty, the node and renderer referenced by the item are still exist.
742               // 2. Mark the related dirty rects as visited so they will not be removed below.
743               auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect);
744               while(dirtyRectPos != itemsDirtyRects.end())
745               {
746                 if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer)
747                 {
748                   break;
749                 }
750
751                 dirtyRectPos->visited = true;
752                 dirtyRectPos++;
753               }
754             }
755           }
756         }
757       }
758     }
759   }
760
761   // Check removed nodes or removed renderers dirty rects
762   auto i = itemsDirtyRects.begin();
763   auto j = itemsDirtyRects.begin();
764   while(i != itemsDirtyRects.end())
765   {
766     if(i->visited)
767     {
768       *j++ = *i;
769     }
770     else
771     {
772       Rect<int>& dirtRect = i->rect;
773       damagedRects.push_back(dirtRect);
774     }
775     i++;
776   }
777
778   itemsDirtyRects.resize(j - itemsDirtyRects.begin());
779   damagedRectCleaner.SetCleanOnReturn(false);
780 }
781
782 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo)
783 {
784   Rect<int> clippingRect;
785   RenderScene(status, scene, renderToFbo, clippingRect);
786 }
787
788 void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect<int>& clippingRect)
789 {
790   Internal::Scene&   sceneInternal = GetImplementation(scene);
791   SceneGraph::Scene* sceneObject   = sceneInternal.GetSceneObject();
792
793   uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex);
794
795   for(uint32_t i = 0; i < count; ++i)
796   {
797     RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i);
798
799     if((renderToFbo && !instruction.mFrameBuffer) || (!renderToFbo && instruction.mFrameBuffer))
800     {
801       continue; // skip
802     }
803
804     // Mark that we will require a post-render step to be performed (includes swap-buffers).
805     status.SetNeedsPostRender(true);
806
807     Rect<int32_t> viewportRect;
808     Vector4       clearColor;
809
810     if(instruction.mIsClearColorSet)
811     {
812       clearColor = instruction.mClearColor;
813     }
814     else
815     {
816       clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR;
817     }
818
819     Rect<int32_t> surfaceRect        = sceneObject->GetSurfaceRect();
820     int32_t       surfaceOrientation = sceneObject->GetSurfaceOrientation();
821
822     Integration::DepthBufferAvailable   depthBufferAvailable   = mImpl->depthBufferAvailable;
823     Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable;
824
825     if(instruction.mFrameBuffer)
826     {
827       // offscreen buffer
828       if(mImpl->currentContext != &mImpl->context)
829       {
830         // Switch to shared context for off-screen buffer
831         mImpl->currentContext = &mImpl->context;
832
833         if(mImpl->currentContext->IsSurfacelessContextSupported())
834         {
835           mImpl->graphicsController.GetGlContextHelperAbstraction().MakeSurfacelessContextCurrent();
836         }
837
838         // Clear the current cached program when the context is switched
839         mImpl->programController.ClearCurrentProgram();
840       }
841     }
842     else
843     {
844       if(mImpl->currentContext->IsSurfacelessContextSupported())
845       {
846         if(mImpl->currentContext != sceneObject->GetContext())
847         {
848           // Switch the correct context if rendering to a surface
849           mImpl->currentContext = sceneObject->GetContext();
850
851           // Clear the current cached program when the context is switched
852           mImpl->programController.ClearCurrentProgram();
853         }
854       }
855     }
856
857     // Make sure that GL context must be created
858     mImpl->currentContext->GlContextCreated();
859
860     // reset the program matrices for all programs once per frame
861     // this ensures we will set view and projection matrix once per program per camera
862     mImpl->programController.ResetProgramMatrices();
863
864     if(instruction.mFrameBuffer)
865     {
866       instruction.mFrameBuffer->Bind(*mImpl->currentContext);
867
868       // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer.
869       for(unsigned int i0 = 0, i1 = instruction.mFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0)
870       {
871         mImpl->textureDependencyList.PushBack(instruction.mFrameBuffer->GetTexture(i0));
872       }
873     }
874     else
875     {
876       mImpl->currentContext->BindFramebuffer(GL_FRAMEBUFFER, 0u);
877     }
878
879     if(!instruction.mFrameBuffer)
880     {
881       mImpl->currentContext->Viewport(surfaceRect.x,
882                                       surfaceRect.y,
883                                       surfaceRect.width,
884                                       surfaceRect.height);
885     }
886
887     // Clear the entire color, depth and stencil buffers for the default framebuffer, if required.
888     // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers
889     // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit",
890     // and then stall. That problem is only noticeable when rendering a large number of vertices per frame.
891     GLbitfield clearMask = GL_COLOR_BUFFER_BIT;
892
893     mImpl->currentContext->ColorMask(true);
894
895     if(depthBufferAvailable == Integration::DepthBufferAvailable::TRUE)
896     {
897       mImpl->currentContext->DepthMask(true);
898       clearMask |= GL_DEPTH_BUFFER_BIT;
899     }
900
901     if(stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE)
902     {
903       mImpl->currentContext->ClearStencil(0);
904       mImpl->currentContext->StencilMask(0xFF); // 8 bit stencil mask, all 1's
905       clearMask |= GL_STENCIL_BUFFER_BIT;
906     }
907
908     if(!instruction.mIgnoreRenderToFbo && (instruction.mFrameBuffer != nullptr))
909     {
910       // Offscreen buffer rendering
911       if(instruction.mIsViewportSet)
912       {
913         // For glViewport the lower-left corner is (0,0)
914         const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y;
915         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
916       }
917       else
918       {
919         viewportRect.Set(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
920       }
921       surfaceOrientation = 0;
922     }
923     else // No Offscreen frame buffer rendering
924     {
925       // Check whether a viewport is specified, otherwise the full surface size is used
926       if(instruction.mIsViewportSet)
927       {
928         // For glViewport the lower-left corner is (0,0)
929         const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y;
930         viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height);
931       }
932       else
933       {
934         viewportRect = surfaceRect;
935       }
936     }
937
938     // Set surface orientation
939     mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation);
940
941     bool clearFullFrameRect = true;
942     if(instruction.mFrameBuffer != nullptr)
943     {
944       Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight());
945       clearFullFrameRect = (frameRect == viewportRect);
946     }
947     else
948     {
949       clearFullFrameRect = (surfaceRect == viewportRect);
950     }
951
952     if(!clippingRect.IsEmpty())
953     {
954       if(!clippingRect.Intersect(viewportRect))
955       {
956         DALI_LOG_ERROR("Invalid clipping rect %d %d %d %d\n", clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
957         clippingRect = Rect<int>();
958       }
959       clearFullFrameRect = false;
960     }
961
962     mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
963
964     if(instruction.mIsClearColorSet)
965     {
966       mImpl->currentContext->ClearColor(clearColor.r,
967                                         clearColor.g,
968                                         clearColor.b,
969                                         clearColor.a);
970       if(!clearFullFrameRect)
971       {
972         if(!clippingRect.IsEmpty())
973         {
974           mImpl->currentContext->SetScissorTest(true);
975           mImpl->currentContext->Scissor(clippingRect.x, clippingRect.y, clippingRect.width, clippingRect.height);
976           mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
977           mImpl->currentContext->SetScissorTest(false);
978         }
979         else
980         {
981           mImpl->currentContext->SetScissorTest(true);
982           mImpl->currentContext->Scissor(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height);
983           mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
984           mImpl->currentContext->SetScissorTest(false);
985         }
986       }
987       else
988       {
989         mImpl->currentContext->SetScissorTest(false);
990         mImpl->currentContext->Clear(clearMask, Context::FORCE_CLEAR);
991       }
992     }
993
994     // Clear the list of bound textures
995     mImpl->boundTextures.Clear();
996
997     mImpl->renderAlgorithms.ProcessRenderInstruction(
998       instruction,
999       *mImpl->currentContext,
1000       mImpl->renderBufferIndex,
1001       depthBufferAvailable,
1002       stencilBufferAvailable,
1003       mImpl->boundTextures,
1004       clippingRect);
1005
1006     // Synchronise the FBO/Texture access when there are multiple contexts
1007     if(mImpl->currentContext->IsSurfacelessContextSupported())
1008     {
1009       // Check whether any bound texture is in the dependency list
1010       bool textureFound = false;
1011
1012       if(mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u)
1013       {
1014         for(auto texture : mImpl->textureDependencyList)
1015         {
1016           textureFound = std::find_if(mImpl->boundTextures.Begin(), mImpl->boundTextures.End(), [texture](Graphics::Texture* graphicsTexture) {
1017                            return texture == graphicsTexture;
1018                          }) != mImpl->boundTextures.End();
1019         }
1020       }
1021
1022       if(textureFound)
1023       {
1024         if(instruction.mFrameBuffer)
1025         {
1026           // For off-screen buffer
1027
1028           // Wait until all rendering calls for the currently context are executed
1029           mImpl->graphicsController.GetGlContextHelperAbstraction().WaitClient();
1030
1031           // Clear the dependency list
1032           mImpl->textureDependencyList.Clear();
1033         }
1034         else
1035         {
1036           // Worker thread lambda function
1037           auto& glContextHelperAbstraction = mImpl->graphicsController.GetGlContextHelperAbstraction();
1038           auto  workerFunction             = [&glContextHelperAbstraction](int workerThread) {
1039             // Switch to the shared context in the worker thread
1040             glContextHelperAbstraction.MakeSurfacelessContextCurrent();
1041
1042             // Wait until all rendering calls for the shared context are executed
1043             glContextHelperAbstraction.WaitClient();
1044
1045             // Must clear the context in the worker thread
1046             // Otherwise the shared context cannot be switched to from the render thread
1047             glContextHelperAbstraction.MakeContextNull();
1048           };
1049
1050           auto future = mImpl->threadPool->SubmitTask(0u, workerFunction);
1051           if(future)
1052           {
1053             mImpl->threadPool->Wait();
1054
1055             // Clear the dependency list
1056             mImpl->textureDependencyList.Clear();
1057           }
1058         }
1059       }
1060     }
1061
1062     if(instruction.mRenderTracker && instruction.mFrameBuffer)
1063     {
1064       // This will create a sync object every frame this render tracker
1065       // is alive (though it should be now be created only for
1066       // render-once render tasks)
1067       instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController.GetGlSyncAbstraction());
1068       instruction.mRenderTracker = nullptr; // Only create once.
1069     }
1070
1071     if(renderToFbo)
1072     {
1073       mImpl->currentContext->Flush();
1074     }
1075   }
1076
1077   GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
1078   mImpl->currentContext->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
1079 }
1080
1081 void RenderManager::PostRender(bool uploadOnly)
1082 {
1083   if(!uploadOnly)
1084   {
1085     if(mImpl->currentContext->IsSurfacelessContextSupported())
1086     {
1087       mImpl->graphicsController.GetGlContextHelperAbstraction().MakeSurfacelessContextCurrent();
1088     }
1089
1090     GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
1091     mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
1092   }
1093
1094   //Notify RenderGeometries that rendering has finished
1095   for(auto&& iter : mImpl->geometryContainer)
1096   {
1097     iter->OnRenderFinished();
1098   }
1099
1100   mImpl->UpdateTrackers();
1101
1102   uint32_t count = 0u;
1103   for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i)
1104   {
1105     count += mImpl->sceneContainer[i]->GetRenderInstructions().Count(mImpl->renderBufferIndex);
1106   }
1107
1108   const bool haveInstructions = count > 0u;
1109
1110   // If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
1111   mImpl->lastFrameWasRendered = haveInstructions;
1112
1113   /**
1114    * The rendering has finished; swap to the next buffer.
1115    * Ideally the update has just finished using this buffer; otherwise the render thread
1116    * should block until the update has finished.
1117    */
1118   mImpl->renderBufferIndex = (0 != mImpl->renderBufferIndex) ? 0 : 1;
1119
1120   DALI_PRINT_RENDER_END();
1121 }
1122
1123 } // namespace SceneGraph
1124
1125 } // namespace Internal
1126
1127 } // namespace Dali