namespace
{
+constexpr uint32_t CACHE_CLEAN_FRAME_COUNT = 600u; // 60fps * 10sec
+
inline Graphics::Rect2D RecalculateScissorArea(const Graphics::Rect2D& scissorArea, int orientation, const Rect<int32_t>& viewportRect)
{
Graphics::Rect2D newScissorArea;
// Reset pipeline cache before rendering
mImpl->pipelineCache->PreRender();
+ // Let we collect reference counts during CACHE_CLEAN_FRAME_COUNT frames.
+ if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 1)
+ {
+ mImpl->programController.ResetReferenceCount();
+ mImpl->shaderCache.ResetReferenceCount();
+ }
+
mImpl->commandBufferSubmitted = false;
}
count += scene->GetRenderInstructions().Count(mImpl->renderBufferIndex);
}
+ // Remove unused shader and programs during CACHE_CLEAN_FRAME_COUNT frames.
+ if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 0)
+ {
+ mImpl->programController.ClearUnusedCache();
+ mImpl->shaderCache.ClearUnusedCache();
+ }
+
const bool haveInstructions = count > 0u;
// If this frame was rendered due to instructions existing, we mark this so we know to clear the next frame.
/*
- * 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.
{
if(item.shaderCode == shaderCode && item.stage == stage && item.type == type)
{
+ ++item.refCount;
return *item.shader.get();
}
}
return *mItems.back().shader.get();
}
+void ShaderCache::ResetReferenceCount()
+{
+ for(auto&& item : mItems)
+ {
+ item.refCount = 0u;
+ }
+}
+
+void ShaderCache::ClearUnusedCache()
+{
+ for(auto iter = mItems.begin(); iter != mItems.end();)
+ {
+ if(iter->refCount == 0u)
+ {
+ iter = mItems.erase(iter);
+ }
+ else
+ {
+ ++iter;
+ }
+ }
+}
+
} // namespace Render
} // namespace Internal
} // namespace Dali
#define DALI_INTERNAL_SHADER_CACHE_H
/*
- * 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.
Item() = default;
Item(const Item&) = delete;
Item(Item&&) = default;
+ Item& operator=(const Item&) = delete;
+ Item& operator=(Item&&) = default;
Item(Graphics::UniquePtr<Dali::Graphics::Shader> shader,
const std::vector<char>& shaderCode,
: shader(std::move(shader)),
shaderCode(shaderCode),
stage(stage),
- type(type)
+ type(type),
+ refCount{1u}
{
}
std::vector<char> shaderCode;
Graphics::PipelineStage stage;
Graphics::ShaderSourceMode type;
+ uint16_t refCount;
};
/**
/**
* Get a shader from it's source code
+ * It will increate getted shader item reference count.
*
* @param[in] shaderCode The shader code
* @param[in] stage The pipeline stage (e.g. VERTEX_SHADER or FRAGMENT_SHADER etc.)
*/
Dali::Graphics::Shader& GetShader(const std::vector<char>& shaderCode, Graphics::PipelineStage stage, Graphics::ShaderSourceMode type);
+ /**
+ * @brief Reset all items reference count as 0.
+ */
+ void ResetReferenceCount();
+
+ /**
+ * @brief Clear items who the reference count is 0.
+ */
+ void ClearUnusedCache();
+
private:
std::vector<Item> mItems;
Dali::Graphics::Controller& mController;
/*
- * 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.
}
}
+void ProgramController::ResetReferenceCount()
+{
+ for(auto&& item : mProgramCache)
+ {
+ item->ClearReferenceCount();
+ }
+}
+
+void ProgramController::ClearUnusedCache()
+{
+ for(auto iter = mProgramCache.Begin(); iter != mProgramCache.End();)
+ {
+ if((*iter)->GetReferenceCount() == 0u)
+ {
+ iter = mProgramCache.Erase(iter);
+ }
+ else
+ {
+ ++iter;
+ }
+ }
+}
+
Program* ProgramController::GetProgram(size_t shaderHash)
{
Program* program = nullptr;
size_t hash = (*iter)->GetHash();
if(shaderHash == hash)
{
+ (*iter)->Reference();
program = (*iter)->GetProgram();
break;
}
#define DALI_INTERNAL_PROGRAM_CONTROLLER_H
/*
- * 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.
*/
ProgramPair(Program* program, size_t shaderHash)
: mProgram(program),
- mShaderHash(shaderHash)
+ mShaderHash(shaderHash),
+ mRefCount{1u}
{
}
return mShaderHash;
}
+ [[nodiscard]] inline uint16_t GetReferenceCount() const
+ {
+ return mRefCount;
+ }
+
+ void Reference()
+ {
+ ++mRefCount;
+ }
+
+ void ClearReferenceCount()
+ {
+ mRefCount = 0u;
+ }
+
ProgramPair(const ProgramPair&) = delete;
ProgramPair& operator=(const ProgramPair&) = delete;
private: // Data
Program* mProgram;
size_t mShaderHash;
+ uint16_t mRefCount;
};
/**
*/
void ResetProgramMatrices();
+ /**
+ * @brief Reset all program reference count as 0.
+ */
+ void ResetReferenceCount();
+
+ /**
+ * @brief Clear program who the reference count is 0.
+ */
+ void ClearUnusedCache();
+
private: // From ProgramCache
/**
* @copydoc ProgramCache::GetProgram