From: sunghyun kim Date: Thu, 21 Sep 2023 06:01:48 +0000 (+0900) Subject: Apply precompile shader X-Git-Tag: dali_2.3.1~6^2 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=commitdiff_plain;h=dfa81c33e6f04763eeba303359b8b25c8c23f10d Apply precompile shader Change-Id: I4e1262f2265799af9ea076bda222c1348873dd21 --- diff --git a/automated-tests/src/dali-toolkit/utc-Dali-VisualFactory.cpp b/automated-tests/src/dali-toolkit/utc-Dali-VisualFactory.cpp index 92e54d4..726a7b8 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-VisualFactory.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-VisualFactory.cpp @@ -29,6 +29,7 @@ #include #include #include +#include using namespace Dali; using namespace Dali::Toolkit; @@ -2788,3 +2789,41 @@ int UtcDaliVisualFactoryGetAnimatedImageVisual2(void) END_TEST; } + + +int UtcDaliVisualFactoryGetPreCompiler(void) +{ + ToolkitTestApplication application; + tet_infoline("UtcDaliVisualFactoryGetAnimatedImageVisual2: Request animated image visual with a Property::Map, test custom wrap mode and pixel area"); + + + std::vector precompiledShaderList; + DALI_TEST_CHECK(precompiledShaderList.size() == 0u); // before Get Shader + ShaderPreCompiler::Get().GetPreCompileShaderList(precompiledShaderList); + DALI_TEST_CHECK(precompiledShaderList.size() == 0u); // after Get Shader + + VisualFactory factory = VisualFactory::Get(); + DALI_TEST_CHECK(factory); + + factory.UsePreCompiledShader(); + + ShaderPreCompiler::Get().GetPreCompileShaderList(precompiledShaderList); + DALI_TEST_CHECK(precompiledShaderList.size() != 0u); // after Get Shader + + Property::Map propertyMap; + propertyMap.Insert(Toolkit::Visual::Property::TYPE, Visual::IMAGE); + propertyMap.Insert(ImageVisual::Property::URL, TEST_IMAGE_FILE_NAME); + Visual::Base visual = factory.CreateVisual(propertyMap); + DALI_TEST_CHECK(visual); + + DummyControl actor = DummyControl::New(true); + DummyControlImpl& dummyImpl = static_cast(actor.GetImplementation()); + dummyImpl.RegisterVisual(Control::CONTROL_PROPERTY_END_INDEX + 1, visual); + actor.SetProperty(Actor::Property::SIZE, Vector2(200.f, 200.f)); + application.GetScene().Add(actor); + + application.SendNotification(); + application.Render(); + + END_TEST; +} diff --git a/dali-toolkit/devel-api/visual-factory/visual-factory.cpp b/dali-toolkit/devel-api/visual-factory/visual-factory.cpp index 93dcb4b..368f8d5 100644 --- a/dali-toolkit/devel-api/visual-factory/visual-factory.cpp +++ b/dali-toolkit/devel-api/visual-factory/visual-factory.cpp @@ -116,6 +116,11 @@ void VisualFactory::DiscardVisual(Visual::Base visual) GetImplementation(*this).DiscardVisual(visual); } +void VisualFactory::UsePreCompiledShader() +{ + GetImplementation(*this).UsePreCompiledShader(); +} + } // namespace Toolkit } // namespace Dali diff --git a/dali-toolkit/devel-api/visual-factory/visual-factory.h b/dali-toolkit/devel-api/visual-factory/visual-factory.h index 1b7d9b5..cc62c2b 100644 --- a/dali-toolkit/devel-api/visual-factory/visual-factory.h +++ b/dali-toolkit/devel-api/visual-factory/visual-factory.h @@ -133,6 +133,17 @@ public: */ void DiscardVisual(Visual::Base visual); + /** + * @brief Compile the visual shader in advance. Afterwards, + * when a visual using a new shader is requested, the pre-compiled shader is used. + * + * @note It is recommended that this method be called at the top of the application code. + * @note Using precompiled shaders is helpful when the application is complex and uses + * many different styles of visual options. On the other hand,if most visuals are the same + * and the application is simple, it may use memory unnecessarily or slow down the application launching speed. + */ + void UsePreCompiledShader(); + private: explicit DALI_INTERNAL VisualFactory(Internal::VisualFactory* impl); }; diff --git a/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp b/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp index 9761015..c193fe7 100644 --- a/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp +++ b/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp @@ -36,8 +36,30 @@ namespace const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f); const int NATIVE_SHADER_TYPE_OFFSET = VisualFactoryCache::ShaderType::NATIVE_IMAGE_SHADER - VisualFactoryCache::ShaderType::IMAGE_SHADER; + } // unnamed namespace +static constexpr auto SHADER_TYPE_COUNT = 6u; + +const std::string_view VertexPredefines[SHADER_TYPE_COUNT] +{ + "", // VisualFactoryCache::IMAGE_SHADER, + "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER, + "",//VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB, + "#define IS_REQUIRED_ROUNDED_CORNER\n",//VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_TO_RGB, + "",//VisualFactoryCache::IMAGE_SHADER_YUV_AND_RGB, + "#define IS_REQUIRED_ROUNDED_CORNER\n",//VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_AND_RGB, +}; +const std::string_view FragmentPredefines[SHADER_TYPE_COUNT] +{ + "", // VisualFactoryCache::IMAGE_SHADER, + "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER, + "#define IS_REQUIRED_YUV_TO_RGB\n",//VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB, + "#define IS_REQUIRED_YUV_TO_RGB\n#define IS_REQUIRED_ROUNDED_CORNER\n",//VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_TO_RGB, + "#define IS_REQUIRED_UNIFIED_YUV_AND_RGB\n",//VisualFactoryCache::IMAGE_SHADER_YUV_AND_RGB, + "#define IS_REQUIRED_UNIFIED_YUV_AND_RGB\n#define IS_REQUIRED_ROUNDED_CORNER\n",//VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_YUV_AND_RGB, +}; + ImageVisualShaderFactory::ImageVisualShaderFactory() : mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED) { @@ -127,6 +149,26 @@ std::string_view ImageVisualShaderFactory::GetFragmentShaderSource() return gFragmentShaderNoAtlas; } +void ImageVisualShaderFactory::GetPreCompiledShader(RawShaderData& shaders) +{ + std::vector vertexPrefix; + std::vector fragmentPrefix; + shaders.shaderCount = 0; + int shaderCount = 0; + for(uint32_t i=0; i< SHADER_TYPE_COUNT; ++i) + { + vertexPrefix.push_back(VertexPredefines[i]); + fragmentPrefix.push_back(FragmentPredefines[i]); + shaderCount++; + } + + shaders.vertexPrefix= vertexPrefix; + shaders.fragmentPrefix = fragmentPrefix; + shaders.vertexShader = SHADER_IMAGE_VISUAL_SHADER_VERT; + shaders.fragmentShader = SHADER_IMAGE_VISUAL_SHADER_FRAG; + shaders.shaderCount = shaderCount; +} + } // namespace Internal } // namespace Toolkit diff --git a/dali-toolkit/internal/visuals/image-visual-shader-factory.h b/dali-toolkit/internal/visuals/image-visual-shader-factory.h index cf1c25b..8b50669 100644 --- a/dali-toolkit/internal/visuals/image-visual-shader-factory.h +++ b/dali-toolkit/internal/visuals/image-visual-shader-factory.h @@ -67,6 +67,12 @@ public: */ std::string_view GetFragmentShaderSource(); + /** + * @brief Get the default shader source. + * @param[in] shaders shaderList for precompile + */ + void GetPreCompiledShader(RawShaderData& shaders); + protected: /** * Undefined copy constructor. diff --git a/dali-toolkit/internal/visuals/text-visual-shader-factory.cpp b/dali-toolkit/internal/visuals/text-visual-shader-factory.cpp index 5f9b04b..62d82f8 100644 --- a/dali-toolkit/internal/visuals/text-visual-shader-factory.cpp +++ b/dali-toolkit/internal/visuals/text-visual-shader-factory.cpp @@ -56,6 +56,16 @@ const VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[] = VisualFactoryCache::TEXT_SHADER_MULTI_COLOR_TEXT_WITH_STYLE_AND_OVERLAY, }; +static constexpr auto SHADER_TYPE_COUNT = 1u; +const std::string_view VertexPredefines[SHADER_TYPE_COUNT] +{ + "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT +}; +const std::string_view FragmentPredefines[SHADER_TYPE_COUNT] +{ + "", // VisualFactoryCache::TEXT_SHADER_SINGLE_COLOR_TEXT +}; + } // unnamed namespace namespace TextVisualShaderFeature @@ -154,6 +164,25 @@ Shader TextVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, cons return shader; } +void TextVisualShaderFactory::GetPreCompiledShader(RawShaderData& shaders) +{ + std::vector vertexPrefix; + std::vector fragmentPrefix; + int shaderCount = 0; + for(uint32_t i=0; i< SHADER_TYPE_COUNT; ++i) + { + vertexPrefix.push_back(VertexPredefines[i]); + fragmentPrefix.push_back(FragmentPredefines[i]); + shaderCount++; + } + + shaders.vertexPrefix= vertexPrefix; + shaders.fragmentPrefix = fragmentPrefix; + shaders.vertexShader = SHADER_TEXT_VISUAL_SHADER_VERT; + shaders.fragmentShader = SHADER_TEXT_VISUAL_SHADER_FRAG; + shaders.shaderCount = shaderCount; +} + } // namespace Internal } // namespace Toolkit diff --git a/dali-toolkit/internal/visuals/text-visual-shader-factory.h b/dali-toolkit/internal/visuals/text-visual-shader-factory.h index 4a9969b..ba4f606 100644 --- a/dali-toolkit/internal/visuals/text-visual-shader-factory.h +++ b/dali-toolkit/internal/visuals/text-visual-shader-factory.h @@ -149,6 +149,12 @@ public: */ Shader GetShader(VisualFactoryCache& factoryCache, const TextVisualShaderFeature::FeatureBuilder& featureBuilder); + /** + * @brief Get the default shader source. + * @param[in] shaders shaderList for precompile + */ + void GetPreCompiledShader(RawShaderData& shaders); + protected: /** * Undefined copy constructor. diff --git a/dali-toolkit/internal/visuals/visual-factory-cache.h b/dali-toolkit/internal/visuals/visual-factory-cache.h index f1d9703..7e651a6 100644 --- a/dali-toolkit/internal/visuals/visual-factory-cache.h +++ b/dali-toolkit/internal/visuals/visual-factory-cache.h @@ -23,6 +23,7 @@ #include #include #include +#include // INTERNAL INCLUDES #include diff --git a/dali-toolkit/internal/visuals/visual-factory-impl.cpp b/dali-toolkit/internal/visuals/visual-factory-impl.cpp index 7cc5119..4a8a6d4 100644 --- a/dali-toolkit/internal/visuals/visual-factory-impl.cpp +++ b/dali-toolkit/internal/visuals/visual-factory-impl.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,7 @@ #include #include + namespace Dali { namespace Toolkit @@ -75,6 +77,18 @@ DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::VisualFactory, Dali::BaseHandle, Cr DALI_TYPE_REGISTRATION_END() const char* const BROKEN_IMAGE_FILE_NAME = "broken.png"; ///< The file name of the broken image. +static constexpr auto SHADER_TYPE_COUNT = 2u; +const std::string_view VertexPredefines[SHADER_TYPE_COUNT] +{ + "", //VisualFactoryCache::COLOR_SHADER + "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER +}; +const std::string_view FragmentPredefines[SHADER_TYPE_COUNT] +{ + "", //VisualFactoryCache::COLOR_SHADER + "#define IS_REQUIRED_ROUNDED_CORNER\n", //VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER +}; + } // namespace VisualFactory::VisualFactory(bool debugEnabled) @@ -84,7 +98,8 @@ VisualFactory::VisualFactory(bool debugEnabled) mSlotDelegate(this), mIdleCallback(nullptr), mDebugEnabled(debugEnabled), - mPreMultiplyOnLoad(true) + mPreMultiplyOnLoad(true), + mPrecompiledShaderRequested(false) { } @@ -393,6 +408,37 @@ void VisualFactory::DiscardVisual(Toolkit::Visual::Base visual) RegisterDiscardCallback(); } +void VisualFactory::UsePreCompiledShader() +{ + if(mPrecompiledShaderRequested) + { + return; + } + mPrecompiledShaderRequested = true; + + ShaderPreCompiler::Get().Enable(); + + // Get image shader + std::vector rawShaderList; + RawShaderData imageShaderData; + GetImageVisualShaderFactory().GetPreCompiledShader(imageShaderData); + rawShaderList.push_back(imageShaderData); + + // Get text shader + RawShaderData textShaderData; + GetTextVisualShaderFactory().GetPreCompiledShader(textShaderData); + rawShaderList.push_back(textShaderData); + + // Get color shader + RawShaderData colorShaderData; + GetPreCompiledShader(colorShaderData); + rawShaderList.push_back(colorShaderData); + + + // Save all shader + ShaderPreCompiler::Get().SavePreCompileShaderList(rawShaderList); +} + Internal::TextureManager& VisualFactory::GetTextureManager() { return GetFactoryCache().GetTextureManager(); @@ -418,6 +464,26 @@ void VisualFactory::SetBrokenImageUrl(Toolkit::StyleManager& styleManager) mFactoryCache->SetBrokenImageUrl(brokenImageUrl, customBrokenImageUrlList); } +void VisualFactory::GetPreCompiledShader(RawShaderData& shaders) +{ + std::vector vertexPrefix; + std::vector fragmentPrefix; + int shaderCount = 0; + shaders.shaderCount = 0; + for(uint32_t i=0u; i< SHADER_TYPE_COUNT; ++i) + { + vertexPrefix.push_back(VertexPredefines[i]); + fragmentPrefix.push_back(FragmentPredefines[i]); + shaderCount++; + } + + shaders.vertexPrefix = vertexPrefix; + shaders.fragmentPrefix = fragmentPrefix; + shaders.vertexShader = SHADER_COLOR_VISUAL_SHADER_VERT; + shaders.fragmentShader = SHADER_COLOR_VISUAL_SHADER_FRAG; + shaders.shaderCount = shaderCount; +} + Internal::VisualFactoryCache& VisualFactory::GetFactoryCache() { if(!mFactoryCache) @@ -431,6 +497,7 @@ Internal::VisualFactoryCache& VisualFactory::GetFactoryCache() } SetBrokenImageUrl(styleManager); } + return *mFactoryCache; } diff --git a/dali-toolkit/internal/visuals/visual-factory-impl.h b/dali-toolkit/internal/visuals/visual-factory-impl.h index c0b95b4..76bacf9 100644 --- a/dali-toolkit/internal/visuals/visual-factory-impl.h +++ b/dali-toolkit/internal/visuals/visual-factory-impl.h @@ -20,6 +20,7 @@ // EXTERNAL INCLUDES #include #include +#include // INTERNAL INCLUDES #include @@ -91,6 +92,11 @@ public: void DiscardVisual(Toolkit::Visual::Base visual); /** + * @copydoc Toolkit::VisualFactory::UsePreCompiledShader() + */ + void UsePreCompiledShader(); + + /** * @return the reference to texture manager */ Internal::TextureManager& GetTextureManager(); @@ -109,6 +115,12 @@ private: void SetBrokenImageUrl(Toolkit::StyleManager& styleManager); /** + * @brief Get the default shader source. + * @param[in] shaders shaderList for precompile + */ + void GetPreCompiledShader(RawShaderData& shaders); + + /** * Get the factory cache, creating it if necessary. */ Internal::VisualFactoryCache& GetFactoryCache(); @@ -143,12 +155,11 @@ private: std::unique_ptr mTextVisualShaderFactory; SlotDelegate mSlotDelegate; CallbackBase* mIdleCallback; - using DiscardedVisualContainer = std::vector; DiscardedVisualContainer mDiscardedVisuals{}; - - bool mDebugEnabled : 1; - bool mPreMultiplyOnLoad : 1; ///< Local store for this flag + bool mDebugEnabled : 1; + bool mPreMultiplyOnLoad : 1; ///< Local store for this flag + bool mPrecompiledShaderRequested : 1; }; /**