X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Frender%2Fcommon%2Frender-manager.cpp;h=715c3e0d701d2df89b09e839d55e9f8b4c17e1b5;hb=9fb36bda980bff7a7e3d02aa3a7125dd7d56fda8;hp=28b29e7facb80c3070a30df857dc28155516df36;hpb=05833e368a62563bee31689952d11f23a51a7834;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/render/common/render-manager.cpp b/dali/internal/render/common/render-manager.cpp index 28b29e7..715c3e0 100644 --- a/dali/internal/render/common/render-manager.cpp +++ b/dali/internal/render/common/render-manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,24 +24,30 @@ // INTERNAL INCLUDES #include #include -#include #include #include +#include #include +#include + #include #include #include #include #include +#include #include #include #include #include +#include #include +#include + namespace Dali { namespace Internal @@ -55,6 +61,44 @@ Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_REN } // unnamed namespace #endif +namespace +{ +inline Graphics::Rect2D RecalculateScissorArea(Graphics::Rect2D scissorArea, int orientation, Rect viewportRect) +{ + Graphics::Rect2D newScissorArea; + + if(orientation == 90) + { + newScissorArea.x = viewportRect.height - (scissorArea.y + scissorArea.height); + newScissorArea.y = scissorArea.x; + newScissorArea.width = scissorArea.height; + newScissorArea.height = scissorArea.width; + } + else if(orientation == 180) + { + newScissorArea.x = viewportRect.width - (scissorArea.x + scissorArea.width); + newScissorArea.y = viewportRect.height - (scissorArea.y + scissorArea.height); + newScissorArea.width = scissorArea.width; + newScissorArea.height = scissorArea.height; + } + else if(orientation == 270) + { + newScissorArea.x = scissorArea.y; + newScissorArea.y = viewportRect.width - (scissorArea.x + scissorArea.width); + newScissorArea.width = scissorArea.height; + newScissorArea.height = scissorArea.width; + } + else + { + newScissorArea.x = scissorArea.x; + newScissorArea.y = scissorArea.y; + newScissorArea.width = scissorArea.width; + newScissorArea.height = scissorArea.height; + } + return newScissorArea; +} +} // namespace + /** * Structure to contain internal data */ @@ -64,18 +108,8 @@ struct RenderManager::Impl Integration::DepthBufferAvailable depthBufferAvailableParam, Integration::StencilBufferAvailable stencilBufferAvailableParam, Integration::PartialUpdateAvailable partialUpdateAvailableParam) - : context(graphicsController.GetGlAbstraction(), &sceneContextContainer), - currentContext(&context), - graphicsController(graphicsController), - renderQueue(), + : graphicsController(graphicsController), renderAlgorithms(graphicsController), - frameCount(0u), - renderBufferIndex(SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX), - rendererContainer(), - samplerContainer(), - textureContainer(), - frameBufferContainer(), - lastFrameWasRendered(false), programController(graphicsController), shaderCache(graphicsController), depthBufferAvailable(depthBufferAvailableParam), @@ -83,10 +117,11 @@ struct RenderManager::Impl partialUpdateAvailable(partialUpdateAvailableParam) { // Create thread pool with just one thread ( there may be a need to create more threads in the future ). - threadPool = std::unique_ptr(new Dali::ThreadPool()); + threadPool = std::make_unique(); threadPool->Initialize(1u); - uniformBufferManager.reset(new Render::UniformBufferManager(&graphicsController)); + uniformBufferManager = std::make_unique(&graphicsController); + pipelineCache = std::make_unique(graphicsController); } ~Impl() @@ -96,7 +131,7 @@ struct RenderManager::Impl void AddRenderTracker(Render::RenderTracker* renderTracker) { - DALI_ASSERT_DEBUG(renderTracker != NULL); + DALI_ASSERT_DEBUG(renderTracker != nullptr); mRenderTrackers.PushBack(renderTracker); } @@ -105,33 +140,6 @@ struct RenderManager::Impl mRenderTrackers.EraseObject(renderTracker); } - Context* CreateSceneContext() - { - Context* context = new Context(graphicsController.GetGlAbstraction()); - sceneContextContainer.PushBack(context); - return context; - } - - void DestroySceneContext(Context* sceneContext) - { - auto iter = std::find(sceneContextContainer.Begin(), sceneContextContainer.End(), sceneContext); - if(iter != sceneContextContainer.End()) - { - (*iter)->GlContextDestroyed(); - sceneContextContainer.Erase(iter); - } - } - - Context* ReplaceSceneContext(Context* oldSceneContext) - { - Context* newContext = new Context(graphicsController.GetGlAbstraction()); - - oldSceneContext->GlContextDestroyed(); - - std::replace(sceneContextContainer.begin(), sceneContextContainer.end(), oldSceneContext, newContext); - return newContext; - } - void UpdateTrackers() { for(auto&& iter : mRenderTrackers) @@ -141,35 +149,24 @@ struct RenderManager::Impl } // the order is important for destruction, - // programs are owned by context at the moment. - Context context; ///< Holds the GL state of the share resource context - Context* currentContext; ///< Holds the GL state of the current context for rendering - OwnerContainer sceneContextContainer; ///< List of owned contexts holding the GL state per scene - Graphics::Controller& graphicsController; - RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread. - - std::vector sceneContainer; ///< List of pointers to the scene graph objects of the scenes - - Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction - - uint32_t frameCount; ///< The current frame count - BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer - - OwnerContainer rendererContainer; ///< List of owned renderers - OwnerContainer samplerContainer; ///< List of owned samplers - OwnerContainer textureContainer; ///< List of owned textures - OwnerContainer frameBufferContainer; ///< List of owned framebuffers - OwnerContainer vertexBufferContainer; ///< List of owned vertex buffers - OwnerContainer geometryContainer; ///< List of owned Geometries - - bool lastFrameWasRendered; ///< Keeps track of the last frame being rendered due to having render instructions - - OwnerContainer mRenderTrackers; ///< List of render trackers - - ProgramController programController; ///< Owner of the GL programs + Graphics::Controller& graphicsController; + RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread. + std::vector sceneContainer; ///< List of pointers to the scene graph objects of the scenes + Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction + + OwnerContainer rendererContainer; ///< List of owned renderers + OwnerContainer samplerContainer; ///< List of owned samplers + OwnerContainer frameBufferContainer; ///< List of owned framebuffers + OwnerContainer vertexBufferContainer; ///< List of owned vertex buffers + OwnerContainer geometryContainer; ///< List of owned Geometries + OwnerContainer mRenderTrackers; ///< List of render trackers + OwnerKeyContainer textureContainer; ///< List of owned textures + + ProgramController programController; ///< Owner of the programs Render::ShaderCache shaderCache; ///< The cache for the graphics shaders std::unique_ptr uniformBufferManager; ///< The uniform buffer manager + std::unique_ptr pipelineCache; Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available @@ -178,6 +175,12 @@ struct RenderManager::Impl std::unique_ptr threadPool; ///< The thread pool Vector boundTextures; ///< The textures bound for rendering Vector textureDependencyList; ///< The dependency list of bound textures + + uint32_t frameCount{0u}; ///< The current frame count + BufferIndex renderBufferIndex{SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX}; ///< The index of the buffer to read from; this is opposite of the "update" buffer + + bool lastFrameWasRendered{false}; ///< Keeps track of the last frame being rendered due to having render instructions + bool commandBufferSubmitted{false}; }; RenderManager* RenderManager::New(Graphics::Controller& graphicsController, @@ -185,8 +188,8 @@ RenderManager* RenderManager::New(Graphics::Controller& graphicsCo Integration::StencilBufferAvailable stencilBufferAvailable, Integration::PartialUpdateAvailable partialUpdateAvailable) { - RenderManager* manager = new RenderManager; - manager->mImpl = new Impl(graphicsController, + auto* manager = new RenderManager; + manager->mImpl = new Impl(graphicsController, depthBufferAvailable, stencilBufferAvailable, partialUpdateAvailable); @@ -208,48 +211,14 @@ RenderQueue& RenderManager::GetRenderQueue() return mImpl->renderQueue; } -void RenderManager::ContextCreated() -{ - mImpl->context.GlContextCreated(); - mImpl->programController.GlContextCreated(); - - // renderers, textures and gpu buffers cannot reinitialize themselves - // so they rely on someone reloading the data for them -} - -void RenderManager::ContextDestroyed() -{ - mImpl->context.GlContextDestroyed(); - mImpl->programController.GlContextDestroyed(); - - //Inform textures - for(auto&& texture : mImpl->textureContainer) - { - texture->Destroy(); - } - - //Inform framebuffers - for(auto&& framebuffer : mImpl->frameBufferContainer) - { - framebuffer->Destroy(); - } - - // inform context - for(auto&& context : mImpl->sceneContextContainer) - { - context->GlContextDestroyed(); - } -} - void RenderManager::SetShaderSaver(ShaderSaver& upstream) { - mImpl->programController.SetShaderSaver(upstream); } void RenderManager::AddRenderer(OwnerPointer& renderer) { // Initialize the renderer as we are now in render thread - renderer->Initialize(mImpl->context, mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get())); + renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get())); mImpl->rendererContainer.PushBack(renderer.Release()); } @@ -270,36 +239,38 @@ void RenderManager::RemoveSampler(Render::Sampler* sampler) mImpl->samplerContainer.EraseObject(sampler); } -void RenderManager::AddTexture(OwnerPointer& texture) +void RenderManager::AddTexture(const Render::TextureKey& textureKey) { - texture->Initialize(mImpl->graphicsController); - mImpl->textureContainer.PushBack(texture.Release()); + DALI_ASSERT_DEBUG(textureKey && "Trying to add empty texture key"); + + textureKey->Initialize(mImpl->graphicsController); + mImpl->textureContainer.PushBack(textureKey); } -void RenderManager::RemoveTexture(Render::Texture* texture) +void RenderManager::RemoveTexture(const Render::TextureKey& textureKey) { - DALI_ASSERT_DEBUG(NULL != texture); + DALI_ASSERT_DEBUG(textureKey && "Trying to remove empty texture key"); - // Find the texture, use reference to pointer so we can do the erase safely - for(auto&& iter : mImpl->textureContainer) + // Find the texture, use std::find so we can do the erase safely + auto iter = std::find(mImpl->textureContainer.begin(), mImpl->textureContainer.end(), textureKey); + + if(iter != mImpl->textureContainer.end()) { - if(iter == texture) - { - texture->Destroy(); - mImpl->textureContainer.Erase(&iter); // Texture found; now destroy it - return; - } + textureKey->Destroy(); + mImpl->textureContainer.Erase(iter); // Texture found; now destroy it } } -void RenderManager::UploadTexture(Render::Texture* texture, PixelDataPtr pixelData, const Texture::UploadParams& params) +void RenderManager::UploadTexture(const Render::TextureKey& textureKey, PixelDataPtr pixelData, const Texture::UploadParams& params) { - texture->Upload(pixelData, params); + DALI_ASSERT_DEBUG(textureKey && "Trying to upload to empty texture key"); + textureKey->Upload(pixelData, params); } -void RenderManager::GenerateMipmaps(Render::Texture* texture) +void RenderManager::GenerateMipmaps(const Render::TextureKey& textureKey) { - texture->GenerateMipmaps(); + DALI_ASSERT_DEBUG(textureKey && "Trying to generate mipmaps on empty texture key"); + textureKey->GenerateMipmaps(); } void RenderManager::SetFilterMode(Render::Sampler* sampler, uint32_t minFilterMode, uint32_t magFilterMode) @@ -324,31 +295,26 @@ void RenderManager::AddFrameBuffer(OwnerPointer& frameBuffe void RenderManager::RemoveFrameBuffer(Render::FrameBuffer* frameBuffer) { - DALI_ASSERT_DEBUG(NULL != frameBuffer); + DALI_ASSERT_DEBUG(nullptr != frameBuffer); - // Find the sampler, use reference so we can safely do the erase - for(auto&& iter : mImpl->frameBufferContainer) - { - if(iter == frameBuffer) - { - frameBuffer->Destroy(); - mImpl->frameBufferContainer.Erase(&iter); // frameBuffer found; now destroy it + // Find the framebuffer, use std:find so we can safely do the erase + auto iter = std::find(mImpl->frameBufferContainer.begin(), mImpl->frameBufferContainer.end(), frameBuffer); - break; - } + if(iter != mImpl->frameBufferContainer.end()) + { + frameBuffer->Destroy(); + mImpl->frameBufferContainer.Erase(iter); // frameBuffer found; now destroy it } } void RenderManager::InitializeScene(SceneGraph::Scene* scene) { - scene->Initialize(*mImpl->CreateSceneContext(), mImpl->graphicsController); + scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable); mImpl->sceneContainer.push_back(scene); } void RenderManager::UninitializeScene(SceneGraph::Scene* scene) { - mImpl->DestroySceneContext(scene->GetContext()); - auto iter = std::find(mImpl->sceneContainer.begin(), mImpl->sceneContainer.end(), scene); if(iter != mImpl->sceneContainer.end()) { @@ -358,8 +324,7 @@ void RenderManager::UninitializeScene(SceneGraph::Scene* scene) void RenderManager::SurfaceReplaced(SceneGraph::Scene* scene) { - Context* newContext = mImpl->ReplaceSceneContext(scene->GetContext()); - scene->Initialize(*newContext, mImpl->graphicsController); + scene->Initialize(mImpl->graphicsController, mImpl->depthBufferAvailable, mImpl->stencilBufferAvailable); } void RenderManager::AttachColorTextureToFrameBuffer(Render::FrameBuffer* frameBuffer, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer) @@ -377,6 +342,11 @@ void RenderManager::AttachDepthStencilTextureToFrameBuffer(Render::FrameBuffer* frameBuffer->AttachDepthStencilTexture(texture, mipmapLevel); } +void RenderManager::SetMultiSamplingLevelToFrameBuffer(Render::FrameBuffer* frameBuffer, uint8_t multiSamplingLevel) +{ + frameBuffer->SetMultiSamplingLevel(multiSamplingLevel); +} + void RenderManager::AddVertexBuffer(OwnerPointer& vertexBuffer) { mImpl->vertexBufferContainer.PushBack(vertexBuffer.Release()); @@ -409,12 +379,17 @@ void RenderManager::AddGeometry(OwnerPointer& geometry) void RenderManager::RemoveGeometry(Render::Geometry* geometry) { - mImpl->geometryContainer.EraseObject(geometry); + auto iter = std::find(mImpl->geometryContainer.begin(), mImpl->geometryContainer.end(), geometry); + + if(iter != mImpl->geometryContainer.end()) + { + mImpl->geometryContainer.Erase(iter); + } } void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer) { - DALI_ASSERT_DEBUG(NULL != geometry); + DALI_ASSERT_DEBUG(nullptr != geometry); // Find the geometry for(auto&& iter : mImpl->geometryContainer) @@ -429,7 +404,7 @@ void RenderManager::AttachVertexBuffer(Render::Geometry* geometry, Render::Verte void RenderManager::RemoveVertexBuffer(Render::Geometry* geometry, Render::VertexBuffer* vertexBuffer) { - DALI_ASSERT_DEBUG(NULL != geometry); + DALI_ASSERT_DEBUG(nullptr != geometry); // Find the geometry for(auto&& iter : mImpl->geometryContainer) @@ -457,17 +432,12 @@ void RenderManager::RemoveRenderTracker(Render::RenderTracker* renderTracker) mImpl->RemoveRenderTracker(renderTracker); } -ProgramCache* RenderManager::GetProgramCache() -{ - return &(mImpl->programController); -} - -void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear, bool uploadOnly) +void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear) { DALI_PRINT_RENDER_START(mImpl->renderBufferIndex); - // Core::Render documents that GL context must be current before calling Render - DALI_ASSERT_DEBUG(mImpl->context.IsGlContextCreated()); + // Rollback + mImpl->uniformBufferManager->GetUniformBufferViewPool(mImpl->renderBufferIndex)->Rollback(); // Increment the frame count at the beginning of each frame ++mImpl->frameCount; @@ -476,9 +446,9 @@ void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear mImpl->renderQueue.ProcessMessages(mImpl->renderBufferIndex); uint32_t count = 0u; - for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i) + for(auto& i : mImpl->sceneContainer) { - count += mImpl->sceneContainer[i]->GetRenderInstructions().Count(mImpl->renderBufferIndex); + count += i->GetRenderInstructions().Count(mImpl->renderBufferIndex); } const bool haveInstructions = count > 0u; @@ -490,55 +460,14 @@ void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear { DALI_LOG_INFO(gLogFilter, Debug::General, "Render: Processing\n"); - // Switch to the shared context - if(mImpl->currentContext != &mImpl->context) - { - mImpl->currentContext = &mImpl->context; - - // Clear the current cached program when the context is switched - mImpl->programController.ClearCurrentProgram(); - } - // Upload the geometries - for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i) + for(auto&& geom : mImpl->geometryContainer) { - RenderInstructionContainer& instructions = mImpl->sceneContainer[i]->GetRenderInstructions(); - for(uint32_t j = 0; j < instructions.Count(mImpl->renderBufferIndex); ++j) - { - RenderInstruction& instruction = instructions.At(mImpl->renderBufferIndex, j); - - const Matrix* viewMatrix = instruction.GetViewMatrix(mImpl->renderBufferIndex); - const Matrix* projectionMatrix = instruction.GetProjectionMatrix(mImpl->renderBufferIndex); - - DALI_ASSERT_DEBUG(viewMatrix); - DALI_ASSERT_DEBUG(projectionMatrix); - - if(viewMatrix && projectionMatrix) - { - const RenderListContainer::SizeType renderListCount = instruction.RenderListCount(); - - // Iterate through each render list. - for(RenderListContainer::SizeType index = 0; index < renderListCount; ++index) - { - const RenderList* renderList = instruction.GetRenderList(index); - - if(renderList && !renderList->IsEmpty()) - { - const std::size_t itemCount = renderList->Count(); - for(uint32_t itemIndex = 0u; itemIndex < itemCount; ++itemIndex) - { - const RenderItem& item = renderList->GetItem(itemIndex); - if(DALI_LIKELY(item.mRenderer)) - { - item.mRenderer->Upload(); - } - } - } - } - } - } + geom->Upload(mImpl->graphicsController); } } + + mImpl->commandBufferSubmitted = false; } void RenderManager::PreRender(Integration::Scene& scene, std::vector>& damagedRects) @@ -560,8 +489,9 @@ void RenderManager::PreRender(Integration::Scene& scene, std::vector>& class DamagedRectsCleaner { public: - DamagedRectsCleaner(std::vector>& damagedRects) + explicit DamagedRectsCleaner(std::vector>& damagedRects, Rect& surfaceRect) : mDamagedRects(damagedRects), + mSurfaceRect(surfaceRect), mCleanOnReturn(true) { } @@ -576,65 +506,67 @@ void RenderManager::PreRender(Integration::Scene& scene, std::vector>& if(mCleanOnReturn) { mDamagedRects.clear(); + mDamagedRects.push_back(mSurfaceRect); } } private: std::vector>& mDamagedRects; + Rect mSurfaceRect; bool mCleanOnReturn; }; Rect surfaceRect = sceneObject->GetSurfaceRect(); // Clean collected dirty/damaged rects on exit if 3d layer or 3d node or other conditions. - DamagedRectsCleaner damagedRectCleaner(damagedRects); + DamagedRectsCleaner damagedRectCleaner(damagedRects, surfaceRect); + bool cleanDamagedRect = false; // Mark previous dirty rects in the sorted array. The array is already sorted by node and renderer, frame number. // so you don't need to sort: std::stable_sort(itemsDirtyRects.begin(), itemsDirtyRects.end()); - std::vector& itemsDirtyRects = sceneInternal.GetItemsDirtyRects(); + std::vector& itemsDirtyRects = sceneObject->GetItemsDirtyRects(); for(DirtyRect& dirtyRect : itemsDirtyRects) { dirtyRect.visited = false; } - uint32_t count = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex); - for(uint32_t i = 0; i < count; ++i) + uint32_t instructionCount = sceneObject->GetRenderInstructions().Count(mImpl->renderBufferIndex); + for(uint32_t i = 0; i < instructionCount; ++i) { RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i); if(instruction.mFrameBuffer) { - return; // TODO: reset, we don't deal with render tasks with framebuffers (for now) + cleanDamagedRect = true; + continue; // TODO: reset, we don't deal with render tasks with framebuffers (for now) } const Camera* camera = instruction.GetCamera(); - if(camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION) + if(camera && camera->mType == Camera::DEFAULT_TYPE && camera->mTargetPosition == Camera::DEFAULT_TARGET_POSITION) { - const Node* node = instruction.GetCamera()->GetNode(); - if(node) + Vector3 position; + Vector3 scale; + Quaternion orientation; + camera->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale); + + Vector3 orientationAxis; + Radian orientationAngle; + orientation.ToAxisAngle(orientationAxis, orientationAngle); + + if(position.x > Math::MACHINE_EPSILON_10000 || + position.y > Math::MACHINE_EPSILON_10000 || + orientationAxis != Vector3(0.0f, 1.0f, 0.0f) || + orientationAngle != ANGLE_180 || + scale != Vector3(1.0f, 1.0f, 1.0f)) { - Vector3 position; - Vector3 scale; - Quaternion orientation; - node->GetWorldMatrix(mImpl->renderBufferIndex).GetTransformComponents(position, orientation, scale); - - Vector3 orientationAxis; - Radian orientationAngle; - orientation.ToAxisAngle(orientationAxis, orientationAngle); - - if(position.x > Math::MACHINE_EPSILON_10000 || - position.y > Math::MACHINE_EPSILON_10000 || - orientationAxis != Vector3(0.0f, 1.0f, 0.0f) || - orientationAngle != ANGLE_180 || - scale != Vector3(1.0f, 1.0f, 1.0f)) - { - return; - } + cleanDamagedRect = true; + continue; } } else { - return; + cleanDamagedRect = true; + continue; } Rect viewportRect; @@ -644,7 +576,8 @@ void RenderManager::PreRender(Integration::Scene& scene, std::vector>& viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height); if(viewportRect.IsEmpty() || !viewportRect.IsValid()) { - return; // just skip funny use cases for now, empty viewport means it is set somewhere else + cleanDamagedRect = true; + continue; // just skip funny use cases for now, empty viewport means it is set somewhere else } } else @@ -660,85 +593,97 @@ void RenderManager::PreRender(Integration::Scene& scene, std::vector>& for(RenderListContainer::SizeType index = 0u; index < count; ++index) { const RenderList* renderList = instruction.GetRenderList(index); - if(renderList && !renderList->IsEmpty()) + if(renderList) { - const std::size_t count = renderList->Count(); - for(uint32_t index = 0u; index < count; ++index) + if(!renderList->IsEmpty()) { - RenderItem& item = renderList->GetItem(index); - // If the item does 3D transformation, do early exit and clean the damaged rect array - if(item.mUpdateSize == Vector3::ZERO) + const std::size_t listCount = renderList->Count(); + for(uint32_t listIndex = 0u; listIndex < listCount; ++listIndex) { - return; - } + RenderItem& item = renderList->GetItem(listIndex); + // If the item does 3D transformation, make full update + if(item.mUpdateArea == Vector4::ZERO) + { + cleanDamagedRect = true; - Rect rect; - DirtyRect dirtyRect(item.mNode, item.mRenderer, mImpl->frameCount, rect); - // If the item refers to updated node or renderer. - if(item.mIsUpdated || - (item.mNode && - (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex, item.mNode))))) - { - item.mIsUpdated = false; - item.mNode->SetUpdated(false); + // Save the full rect in the damaged list. We need it when this item is removed + DirtyRect dirtyRect(item.mNode, item.mRenderer, surfaceRect); + auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect); + if(dirtyRectPos != itemsDirtyRects.end() && dirtyRectPos->node == item.mNode && dirtyRectPos->renderer == item.mRenderer) + { + // Replace the rect + dirtyRectPos->visited = true; + dirtyRectPos->rect = dirtyRect.rect; + } + else + { + // Else, just insert the new dirtyrect in the correct position + itemsDirtyRects.insert(dirtyRectPos, dirtyRect); + } + continue; + } - rect = item.CalculateViewportSpaceAABB(item.mUpdateSize, viewportRect.width, viewportRect.height); - if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty()) + Rect rect; + DirtyRect dirtyRect(item.mNode, item.mRenderer, rect); + // If the item refers to updated node or renderer. + if(item.mIsUpdated || + (item.mNode && + (item.mNode->Updated() || (item.mRenderer && item.mRenderer->Updated(mImpl->renderBufferIndex))))) { - const int left = rect.x; - const int top = rect.y; - const int right = rect.x + rect.width; - const int bottom = rect.y + rect.height; - rect.x = (left / 16) * 16; - rect.y = (top / 16) * 16; - rect.width = ((right + 16) / 16) * 16 - rect.x; - rect.height = ((bottom + 16) / 16) * 16 - rect.y; - - // Found valid dirty rect. - // 1. Insert it in the sorted array of the dirty rects. - // 2. Mark the related dirty rects as visited so they will not be removed below. - // 3. Keep only last 3 dirty rects for the same node and renderer (Tizen uses 3 back buffers, Ubuntu 1). - dirtyRect.rect = rect; - auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect); - dirtyRectPos = itemsDirtyRects.insert(dirtyRectPos, dirtyRect); + item.mIsUpdated = false; - int c = 1; - while(++dirtyRectPos != itemsDirtyRects.end()) + Vector4 updateArea = item.mRenderer ? item.mRenderer->GetVisualTransformedUpdateArea(mImpl->renderBufferIndex, item.mUpdateArea) : item.mUpdateArea; + + rect = RenderItem::CalculateViewportSpaceAABB(item.mModelViewMatrix, Vector3(updateArea.x, updateArea.y, 0.0f), Vector3(updateArea.z, updateArea.w, 0.0f), viewportRect.width, viewportRect.height); + if(rect.IsValid() && rect.Intersect(viewportRect) && !rect.IsEmpty()) { - if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer) + const int left = rect.x; + const int top = rect.y; + const int right = rect.x + rect.width; + const int bottom = rect.y + rect.height; + rect.x = (left / 16) * 16; + rect.y = (top / 16) * 16; + rect.width = ((right + 16) / 16) * 16 - rect.x; + rect.height = ((bottom + 16) / 16) * 16 - rect.y; + + // Found valid dirty rect. + dirtyRect.rect = rect; + auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect); + + if(dirtyRectPos != itemsDirtyRects.end() && dirtyRectPos->node == item.mNode && dirtyRectPos->renderer == item.mRenderer) { - break; - } - - dirtyRectPos->visited = true; - Rect& dirtRect = dirtyRectPos->rect; - rect.Merge(dirtRect); + // Same item, merge it with the previous rect + rect.Merge(dirtyRectPos->rect); - c++; - if(c > 3) // no more then 3 previous rects + // Replace the rect + dirtyRectPos->visited = true; + dirtyRectPos->rect = dirtyRect.rect; + } + else { - itemsDirtyRects.erase(dirtyRectPos); - break; + // Else, just insert the new dirtyrect in the correct position + itemsDirtyRects.insert(dirtyRectPos, dirtyRect); } - } - damagedRects.push_back(rect); + damagedRects.push_back(rect); + } } - } - else - { - // 1. The item is not dirty, the node and renderer referenced by the item are still exist. - // 2. Mark the related dirty rects as visited so they will not be removed below. - auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect); - while(dirtyRectPos != itemsDirtyRects.end()) + else { - if(dirtyRectPos->node != item.mNode || dirtyRectPos->renderer != item.mRenderer) + // 1. The item is not dirty, the node and renderer referenced by the item are still exist. + // 2. Mark the related dirty rects as visited so they will not be removed below. + auto dirtyRectPos = std::lower_bound(itemsDirtyRects.begin(), itemsDirtyRects.end(), dirtyRect); + if(dirtyRectPos != itemsDirtyRects.end() && dirtyRectPos->node == item.mNode && dirtyRectPos->renderer == item.mRenderer) { - break; + dirtyRectPos->visited = true; + } + else + { + // The item is not in the list for some reason. Add it! + dirtyRect.rect = surfaceRect; + itemsDirtyRects.insert(dirtyRectPos, dirtyRect); + cleanDamagedRect = true; // And make full update at this frame } - - dirtyRectPos->visited = true; - dirtyRectPos++; } } } @@ -765,17 +710,29 @@ void RenderManager::PreRender(Integration::Scene& scene, std::vector>& } itemsDirtyRects.resize(j - itemsDirtyRects.begin()); - damagedRectCleaner.SetCleanOnReturn(false); + + if(!cleanDamagedRect) + { + damagedRectCleaner.SetCleanOnReturn(false); + } } void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo) { - Rect clippingRect; + SceneGraph::Scene* sceneObject = GetImplementation(scene).GetSceneObject(); + Rect clippingRect = sceneObject->GetSurfaceRect(); + RenderScene(status, scene, renderToFbo, clippingRect); } void RenderManager::RenderScene(Integration::RenderStatus& status, Integration::Scene& scene, bool renderToFbo, Rect& clippingRect) { + if(mImpl->partialUpdateAvailable == Integration::PartialUpdateAvailable::TRUE && !renderToFbo && clippingRect.IsEmpty()) + { + // ClippingRect is empty. Skip rendering + return; + } + // Reset main algorithms command buffer mImpl->renderAlgorithms.ResetCommandBuffer(); @@ -788,6 +745,18 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: std::vector targetstoPresent; + Rect surfaceRect = sceneObject->GetSurfaceRect(); + if(clippingRect == surfaceRect) + { + // Full rendering case + // Make clippingRect empty because we're doing full rendering now if the clippingRect is empty. + // To reduce side effects, keep this logic now. + clippingRect = Rect(); + } + + // Prepare to lock and map standalone uniform buffer. + mImpl->uniformBufferManager->ReadyToLockUniformBuffer(mImpl->renderBufferIndex); + for(uint32_t i = 0; i < count; ++i) { RenderInstruction& instruction = sceneObject->GetRenderInstructions().At(mImpl->renderBufferIndex, i); @@ -801,20 +770,14 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: status.SetNeedsPostRender(true); Rect viewportRect; - Vector4 clearColor; - if(instruction.mIsClearColorSet) - { - clearColor = instruction.mClearColor; - } - else + int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation() + sceneObject->GetScreenOrientation(); + if(surfaceOrientation >= 360) { - clearColor = Dali::RenderTask::DEFAULT_CLEAR_COLOR; + surfaceOrientation -= 360; } - Rect surfaceRect = sceneObject->GetSurfaceRect(); - int32_t surfaceOrientation = sceneObject->GetSurfaceOrientation(); - + // @todo Should these be part of scene? Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable; Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable; @@ -824,7 +787,7 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: if(instruction.mFrameBuffer) { - // Ensure graphics framebuffer is created, bind atachments and create render passes + // Ensure graphics framebuffer is created, bind attachments and create render passes // Only happens once per framebuffer. If the create fails, e.g. no attachments yet, // then don't render to this framebuffer. if(!instruction.mFrameBuffer->GetGraphicsObject()) @@ -839,7 +802,7 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: auto& clearValues = instruction.mFrameBuffer->GetGraphicsRenderPassClearValues(); // Set the clear color for first color attachment - if(instruction.mIsClearColorSet && clearValues.size() > 0) + if(instruction.mIsClearColorSet && !clearValues.empty()) { clearValues[0].color = { instruction.mClearColor.r, @@ -855,30 +818,9 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: // offscreen buffer currentRenderTarget = instruction.mFrameBuffer->GetGraphicsRenderTarget(); currentRenderPass = instruction.mFrameBuffer->GetGraphicsRenderPass(loadOp, Graphics::AttachmentStoreOp::STORE); - - if(mImpl->currentContext != &mImpl->context) - { - // Switch to shared context for off-screen buffer - mImpl->currentContext = &mImpl->context; - - // Clear the current cached program when the context is switched - mImpl->programController.ClearCurrentProgram(); - } } - else + else // no framebuffer { - if(mImpl->currentContext->IsSurfacelessContextSupported()) - { - if(mImpl->currentContext != sceneObject->GetContext()) - { - // Switch the correct context if rendering to a surface - mImpl->currentContext = sceneObject->GetContext(); - - // Clear the current cached program when the context is switched - mImpl->programController.ClearCurrentProgram(); - } - } - // surface auto& clearValues = sceneObject->GetGraphicsRenderPassClearValues(); @@ -893,6 +835,17 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: currentClearValues = clearValues; + // @todo SceneObject should already have the depth clear / stencil clear in the clearValues array. + // if the window has a depth/stencil buffer. + if((depthBufferAvailable == Integration::DepthBufferAvailable::TRUE || + stencilBufferAvailable == Integration::StencilBufferAvailable::TRUE) && + (currentClearValues.size() <= 1)) + { + currentClearValues.emplace_back(); + currentClearValues.back().depthStencil.depth = 0; + currentClearValues.back().depthStencil.stencil = 0; + } + auto loadOp = instruction.mIsClearColorSet ? Graphics::AttachmentLoadOp::CLEAR : Graphics::AttachmentLoadOp::LOAD; currentRenderTarget = sceneObject->GetSurfaceRenderTarget(); @@ -901,9 +854,6 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: targetstoPresent.emplace_back(currentRenderTarget); - // Make sure that GL context must be created - mImpl->currentContext->GlContextCreated(); - // reset the program matrices for all programs once per frame // this ensures we will set view and projection matrix once per program per camera mImpl->programController.ResetProgramMatrices(); @@ -922,7 +872,7 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: // Offscreen buffer rendering if(instruction.mIsViewportSet) { - // For glViewport the lower-left corner is (0,0) + // For Viewport the lower-left corner is (0,0) const int32_t y = (instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height) - instruction.mViewport.y; viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height); } @@ -937,7 +887,7 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: // Check whether a viewport is specified, otherwise the full surface size is used if(instruction.mIsViewportSet) { - // For glViewport the lower-left corner is (0,0) + // For Viewport the lower-left corner is (0,0) const int32_t y = (surfaceRect.height - instruction.mViewport.height) - instruction.mViewport.y; viewportRect.Set(instruction.mViewport.x, y, instruction.mViewport.width, instruction.mViewport.height); } @@ -948,20 +898,16 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: } // Set surface orientation - mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation); + // @todo Inform graphics impl by another route. + // was: mImpl->currentContext->SetSurfaceOrientation(surfaceOrientation); /*** Clear region of framebuffer or surface before drawing ***/ - - bool clearFullFrameRect = true; + bool clearFullFrameRect = (surfaceRect == viewportRect); if(instruction.mFrameBuffer != nullptr) { Viewport frameRect(0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight()); clearFullFrameRect = (frameRect == viewportRect); } - else - { - clearFullFrameRect = (surfaceRect == viewportRect); - } if(!clippingRect.IsEmpty()) { @@ -985,6 +931,11 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: } } + // Scissor's value should be set based on the default system coordinates. + // When the surface is rotated, the input values already were set with the rotated angle. + // So, re-calculation is needed. + scissorArea = RecalculateScissorArea(scissorArea, surfaceOrientation, surfaceRect); + // Begin render pass mainCommandBuffer->BeginRenderPass( currentRenderPass, @@ -1002,81 +953,32 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: mImpl->renderAlgorithms.ProcessRenderInstruction( instruction, - *mImpl->currentContext, mImpl->renderBufferIndex, depthBufferAvailable, stencilBufferAvailable, mImpl->boundTextures, viewportRect, clippingRect, - surfaceOrientation); - - // Synchronise the FBO/Texture access when there are multiple contexts - if(mImpl->currentContext->IsSurfacelessContextSupported()) - { - // Check whether any bound texture is in the dependency list - bool textureFound = false; - - if(mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u) - { - for(auto texture : mImpl->textureDependencyList) - { - textureFound = std::find_if(mImpl->boundTextures.Begin(), mImpl->boundTextures.End(), [texture](Graphics::Texture* graphicsTexture) { - return texture == graphicsTexture; - }) != mImpl->boundTextures.End(); - } - } - - if(textureFound) - { - if(instruction.mFrameBuffer) - { - // For off-screen buffer - - // Clear the dependency list - mImpl->textureDependencyList.Clear(); - } - else - { - // Worker thread lambda function - auto& glContextHelperAbstraction = mImpl->graphicsController.GetGlContextHelperAbstraction(); - auto workerFunction = [&glContextHelperAbstraction](int workerThread) { - // Switch to the shared context in the worker thread - glContextHelperAbstraction.MakeSurfacelessContextCurrent(); - - // Wait until all rendering calls for the shared context are executed - glContextHelperAbstraction.WaitClient(); - - // Must clear the context in the worker thread - // Otherwise the shared context cannot be switched to from the render thread - glContextHelperAbstraction.MakeContextNull(); - }; - - auto future = mImpl->threadPool->SubmitTask(0u, workerFunction); - if(future) - { - mImpl->threadPool->Wait(); - - // Clear the dependency list - mImpl->textureDependencyList.Clear(); - } - } - } - } + surfaceOrientation, + Uint16Pair(surfaceRect.width, surfaceRect.height)); + Graphics::SyncObject* syncObject{nullptr}; + // If the render instruction has an associated render tracker (owned separately) + // and framebuffer, create a one shot sync object, and use it to determine when + // the render pass has finished executing on GPU. if(instruction.mRenderTracker && instruction.mFrameBuffer) { - // This will create a sync object every frame this render tracker - // is alive (though it should be now be created only for - // render-once render tasks) - instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController.GetGlSyncAbstraction()); - instruction.mRenderTracker = nullptr; // Only create once. + syncObject = instruction.mRenderTracker->CreateSyncObject(mImpl->graphicsController); + instruction.mRenderTracker = nullptr; } - - // End render pass - mainCommandBuffer->EndRenderPass(); + mainCommandBuffer->EndRenderPass(syncObject); } + + // Unlock standalone uniform buffer. + mImpl->uniformBufferManager->UnlockUniformBuffer(mImpl->renderBufferIndex); + mImpl->renderAlgorithms.SubmitCommandBuffer(); + mImpl->commandBufferSubmitted = true; std::sort(targetstoPresent.begin(), targetstoPresent.end()); @@ -1091,20 +993,37 @@ void RenderManager::RenderScene(Integration::RenderStatus& status, Integration:: } } -void RenderManager::PostRender(bool uploadOnly) +void RenderManager::PostRender() { + if(!mImpl->commandBufferSubmitted) + { + // Rendering is skipped but there may be pending tasks. Flush them. + Graphics::SubmitInfo submitInfo; + submitInfo.cmdBuffer.clear(); // Only flush + submitInfo.flags = 0 | Graphics::SubmitFlagBits::FLUSH; + mImpl->graphicsController.SubmitCommandBuffers(submitInfo); + + mImpl->commandBufferSubmitted = true; + } + // Notify RenderGeometries that rendering has finished for(auto&& iter : mImpl->geometryContainer) { iter->OnRenderFinished(); } + // Notify RenderTexture that rendering has finished + for(auto&& iter : mImpl->textureContainer) + { + iter->OnRenderFinished(); + } + mImpl->UpdateTrackers(); uint32_t count = 0u; - for(uint32_t i = 0; i < mImpl->sceneContainer.size(); ++i) + for(auto& scene : mImpl->sceneContainer) { - count += mImpl->sceneContainer[i]->GetRenderInstructions().Count(mImpl->renderBufferIndex); + count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex); } const bool haveInstructions = count > 0u;