From 5402aeac11d588e5d906f8e9f87076584b181339 Mon Sep 17 00:00:00 2001 From: David Steele Date: Mon, 13 Nov 2023 17:58:22 +0000 Subject: [PATCH] Moved shader cache to graphics backend Change-Id: Ie1b01412bc9fd588ddfe5b511b510297dfaa9484 --- .../dali-test-suite-utils/test-gl-abstraction.h | 27 +++++- dali/internal/file.list | 1 - dali/internal/render/common/render-manager.cpp | 9 +- dali/internal/render/renderers/render-renderer.cpp | 34 +++---- dali/internal/render/renderers/render-renderer.h | 6 +- dali/internal/render/renderers/shader-cache.cpp | 79 ---------------- dali/internal/render/renderers/shader-cache.h | 102 --------------------- 7 files changed, 47 insertions(+), 211 deletions(-) delete mode 100644 dali/internal/render/renderers/shader-cache.cpp delete mode 100644 dali/internal/render/renderers/shader-cache.h diff --git a/automated-tests/src/dali/dali-test-suite-utils/test-gl-abstraction.h b/automated-tests/src/dali/dali-test-suite-utils/test-gl-abstraction.h index 2ca98dc..3f04a6b 100644 --- a/automated-tests/src/dali/dali-test-suite-utils/test-gl-abstraction.h +++ b/automated-tests/src/dali/dali-test-suite-utils/test-gl-abstraction.h @@ -1041,7 +1041,32 @@ public: for(const auto& uniform : mActiveUniforms) { - GetUniformLocation(program, uniform.name.c_str()); + std::string name = uniform.name; + if(uniform.size <= 1) + { + GetUniformLocation(program, name.c_str()); + } + else + { + // Convert single active uniform from "uBlah[0]" or "uStruct[0].element" to N versions of the same + std::string suffix; + auto iter = name.find("["); // Search for index operator + if(iter != std::string::npos) + { + name = uniform.name.substr(0, iter); // Strip off index operator + iter = uniform.name.find("]"); + if(iter != std::string::npos && iter + 1 != uniform.name.length()) + { + suffix = uniform.name.substr(iter + 1); + } + } + for(int i = 0; i < uniform.size; ++i) + { + std::stringstream nss; + nss << name << "[" << i << "]" << suffix; + GetUniformLocation(program, nss.str().c_str()); // Generate N uniforms in the uniform map + } + } } for(const auto& uniform : mCustomUniformData) diff --git a/dali/internal/file.list b/dali/internal/file.list index 2ec62c0..7556532 100644 --- a/dali/internal/file.list +++ b/dali/internal/file.list @@ -122,7 +122,6 @@ SET( internal_src_files ${internal_src_dir}/render/renderers/render-sampler.cpp ${internal_src_dir}/render/renderers/render-texture.cpp ${internal_src_dir}/render/renderers/render-vertex-buffer.cpp - ${internal_src_dir}/render/renderers/shader-cache.cpp ${internal_src_dir}/render/renderers/uniform-buffer.cpp ${internal_src_dir}/render/renderers/uniform-buffer-manager.cpp ${internal_src_dir}/render/renderers/uniform-buffer-view.cpp diff --git a/dali/internal/render/common/render-manager.cpp b/dali/internal/render/common/render-manager.cpp index 6d42e84..48464b0 100644 --- a/dali/internal/render/common/render-manager.cpp +++ b/dali/internal/render/common/render-manager.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -142,7 +141,6 @@ struct RenderManager::Impl : graphicsController(graphicsController), renderAlgorithms(graphicsController), programController(graphicsController), - shaderCache(graphicsController), depthBufferAvailable(depthBufferAvailableParam), stencilBufferAvailable(stencilBufferAvailableParam), partialUpdateAvailable(partialUpdateAvailableParam) @@ -197,8 +195,7 @@ struct RenderManager::Impl OwnerKeyContainer textureDiscardQueue; ///< Discarded textures - ProgramController programController; ///< Owner of the programs - Render::ShaderCache shaderCache; ///< The cache for the graphics shaders + ProgramController programController; ///< Owner of the programs std::unique_ptr uniformBufferManager; ///< The uniform buffer manager std::unique_ptr pipelineCache; @@ -252,7 +249,7 @@ void RenderManager::SetShaderSaver(ShaderSaver& upstream) void RenderManager::AddRenderer(const Render::RendererKey& renderer) { // Initialize the renderer as we are now in render thread - renderer->Initialize(mImpl->graphicsController, mImpl->programController, mImpl->shaderCache, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get())); + renderer->Initialize(mImpl->graphicsController, mImpl->programController, *(mImpl->uniformBufferManager.get()), *(mImpl->pipelineCache.get())); mImpl->rendererContainer.PushBack(renderer); } @@ -524,7 +521,6 @@ void RenderManager::PreRender(Integration::RenderStatus& status, bool forceClear if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 1) { mImpl->programController.ResetReferenceCount(); - mImpl->shaderCache.ResetReferenceCount(); } */ @@ -1159,7 +1155,6 @@ void RenderManager::PostRender() if(mImpl->frameCount % CACHE_CLEAN_FRAME_COUNT == 0) { mImpl->programController.ClearUnusedCache(); - mImpl->shaderCache.ClearUnusedCache(); } */ diff --git a/dali/internal/render/renderers/render-renderer.cpp b/dali/internal/render/renderers/render-renderer.cpp index 8eb8120..e4aa1b9 100644 --- a/dali/internal/render/renderers/render-renderer.cpp +++ b/dali/internal/render/renderers/render-renderer.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -240,11 +239,10 @@ Renderer::Renderer(SceneGraph::RenderDataProvider* dataProvider, mBlendingOptions.SetBlendColor(blendColor); } -void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager, Render::PipelineCache& pipelineCache) +void Renderer::Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, Render::UniformBufferManager& uniformBufferManager, Render::PipelineCache& pipelineCache) { mGraphicsController = &graphicsController; mProgramCache = &programCache; - mShaderCache = &shaderCache; mUniformBufferManager = &uniformBufferManager; mPipelineCache = &pipelineCache; } @@ -509,24 +507,28 @@ Program* Renderer::PrepareProgram(const SceneGraph::RenderInstruction& instructi // If program doesn't have Gfx program object assigned yet, prepare it. if(!program->GetGraphicsProgramPtr()) { - const std::vector& vertShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER); - const std::vector& fragShader = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER); - Dali::Graphics::Shader& vertexShader = mShaderCache->GetShader( - vertShader, - Graphics::PipelineStage::VERTEX_SHADER, - shaderData->GetSourceMode()); - - Dali::Graphics::Shader& fragmentShader = mShaderCache->GetShader( - fragShader, - Graphics::PipelineStage::FRAGMENT_SHADER, - shaderData->GetSourceMode()); + Graphics::ShaderCreateInfo vertexShaderCreateInfo; + vertexShaderCreateInfo.SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER); + vertexShaderCreateInfo.SetSourceMode(shaderData->GetSourceMode()); + const std::vector& vertexShaderSrc = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER); + vertexShaderCreateInfo.SetSourceSize(vertexShaderSrc.size()); + vertexShaderCreateInfo.SetSourceData(static_cast(vertexShaderSrc.data())); + auto vertexShader = mGraphicsController->CreateShader(vertexShaderCreateInfo, nullptr); + + Graphics::ShaderCreateInfo fragmentShaderCreateInfo; + fragmentShaderCreateInfo.SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER); + fragmentShaderCreateInfo.SetSourceMode(shaderData->GetSourceMode()); + const std::vector& fragmentShaderSrc = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER); + fragmentShaderCreateInfo.SetSourceSize(fragmentShaderSrc.size()); + fragmentShaderCreateInfo.SetSourceData(static_cast(fragmentShaderSrc.data())); + auto fragmentShader = mGraphicsController->CreateShader(fragmentShaderCreateInfo, nullptr); std::vector shaderStates{ Graphics::ShaderState() - .SetShader(vertexShader) + .SetShader(*vertexShader.get()) .SetPipelineStage(Graphics::PipelineStage::VERTEX_SHADER), Graphics::ShaderState() - .SetShader(fragmentShader) + .SetShader(*fragmentShader.get()) .SetPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER)}; auto createInfo = Graphics::ProgramCreateInfo(); diff --git a/dali/internal/render/renderers/render-renderer.h b/dali/internal/render/renderers/render-renderer.h index ce270ee..361840f 100644 --- a/dali/internal/render/renderers/render-renderer.h +++ b/dali/internal/render/renderers/render-renderer.h @@ -56,7 +56,6 @@ class RenderInstruction; //for reflection effect namespace Render { -struct ShaderCache; class PipelineCache; struct PipelineCacheL2; class UniformBufferManager; @@ -177,13 +176,11 @@ public: * This is called when the renderer is inside render thread * @param[in] graphicsController The graphics controller to use * @param[in] programCache Cache of program objects - * @param[in] shaderCache Cache of shaders * @param[in] uniformBufferManager Uniform buffer manager * @param[in] pipelineCache Cache of pipelines */ void Initialize(Graphics::Controller& graphicsController, ProgramCache& programCache, - Render::ShaderCache& shaderCache, Render::UniformBufferManager& uniformBufferManager, Render::PipelineCache& pipelineCache); @@ -620,8 +617,7 @@ private: Render::Geometry* mGeometry; - ProgramCache* mProgramCache{nullptr}; - Render::ShaderCache* mShaderCache{nullptr}; + ProgramCache* mProgramCache{nullptr}; Render::UniformBufferManager* mUniformBufferManager{}; std::vector mUniformBufferBindings{}; diff --git a/dali/internal/render/renderers/shader-cache.cpp b/dali/internal/render/renderers/shader-cache.cpp deleted file mode 100644 index 90d042c..0000000 --- a/dali/internal/render/renderers/shader-cache.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include - -namespace Dali -{ -namespace Internal -{ -namespace Render -{ -ShaderCache::ShaderCache(Dali::Graphics::Controller& controller) -: mController(controller) -{ -} - -Dali::Graphics::Shader& ShaderCache::GetShader(const std::vector& shaderCode, Graphics::PipelineStage stage, Graphics::ShaderSourceMode type) -{ - for(auto&& item : mItems) - { - if(item.shaderCode == shaderCode && item.stage == stage && item.type == type) - { - ++item.refCount; - return *item.shader.get(); - } - } - - Graphics::ShaderCreateInfo shaderCreateInfo; - shaderCreateInfo.SetPipelineStage(stage); - shaderCreateInfo.SetSourceData(static_cast(shaderCode.data())); - shaderCreateInfo.SetSourceSize(shaderCode.size()); - shaderCreateInfo.SetSourceMode(type); - - Graphics::UniquePtr shader = mController.CreateShader(shaderCreateInfo, nullptr); - - mItems.emplace_back(std::move(shader), shaderCode, stage, type); - 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 diff --git a/dali/internal/render/renderers/shader-cache.h b/dali/internal/render/renderers/shader-cache.h deleted file mode 100644 index 9ed2034..0000000 --- a/dali/internal/render/renderers/shader-cache.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef DALI_INTERNAL_SHADER_CACHE_H -#define DALI_INTERNAL_SHADER_CACHE_H - -/* - * 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include - -namespace Dali -{ -namespace Internal -{ -namespace Render -{ -/** - * Caches graphics shaders as they are created by SceneGraph::Shader. - */ -struct ShaderCache -{ - struct Item - { - Item() = default; - Item(const Item&) = delete; - Item(Item&&) = default; - Item& operator=(const Item&) = delete; - Item& operator=(Item&&) = default; - - Item(Graphics::UniquePtr shader, - const std::vector& shaderCode, - Graphics::PipelineStage stage, - Graphics::ShaderSourceMode type) - : shader(std::move(shader)), - shaderCode(shaderCode), - stage(stage), - type(type), - refCount{1u} - { - } - - ~Item() = default; - - Graphics::UniquePtr shader{nullptr}; - std::vector shaderCode; - Graphics::PipelineStage stage; - Graphics::ShaderSourceMode type; - uint16_t refCount; - }; - - /** - * Constructor - * - * @param[in] controller The graphics controller - */ - explicit ShaderCache(Dali::Graphics::Controller& controller); - - /** - * 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.) - * @param[in] type The type of the shader (i.e. text or binary) - * @return the graphics shader - */ - Dali::Graphics::Shader& GetShader(const std::vector& 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 mItems; - Dali::Graphics::Controller& mController; -}; - -} // namespace Render -} // namespace Internal -} // namespace Dali - -#endif //DALI_INTERNAL_SHADER_CACHE_H -- 2.7.4