2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/internal/visuals/image-visual-shader-factory.h>
21 #include <dali/devel-api/rendering/texture-devel.h>
24 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
25 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
26 #include <dali/integration-api/debug.h>
36 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
38 // global string variable to caching complate vertex shader
39 static std::string gVertexShader;
41 // global string variable to caching complate fragment shader (no atlas)
42 static std::string gFragmentShaderNoAtlas;
44 const int NATIVE_SHADER_TYPE_OFFSET = VisualFactoryCache::ShaderType::NATIVE_IMAGE_SHADER - VisualFactoryCache::ShaderType::IMAGE_SHADER;
46 // enum of required list when we select shader
47 enum class ImageVisualRequireFlag : uint32_t
50 ROUNDED_CORNER = 1 << 0,
52 ALPHA_MASKING = 1 << 2,
55 static constexpr auto SHADER_TYPE_COUNT = 8u;
56 VisualFactoryCache::ShaderType SHADER_TYPE_TABLE[SHADER_TYPE_COUNT] =
58 VisualFactoryCache::IMAGE_SHADER,
59 VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER,
60 VisualFactoryCache::IMAGE_SHADER_BORDERLINE,
61 VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE,
62 VisualFactoryCache::IMAGE_SHADER_MASKING,
63 VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER_MASKING,
64 VisualFactoryCache::IMAGE_SHADER_BORDERLINE_MASKING,
65 VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE_MASKING};
67 } // unnamed namespace
69 namespace ImageVisualShaderFeature
71 FeatureBuilder& FeatureBuilder::EnableTextureAtlas(bool enableAtlas)
73 mTextureAtlas = (enableAtlas ? TextureAtlas::ENABLED : TextureAtlas::DISABLED);
76 FeatureBuilder& FeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
78 mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? DefaultTextureWrapMode::APPLY : DefaultTextureWrapMode::DO_NOT_APPLY);
81 FeatureBuilder& FeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
83 mRoundedCorner = (enableRoundedCorner ? RoundedCorner::ENABLED : RoundedCorner::DISABLED);
86 FeatureBuilder& FeatureBuilder::EnableBorderline(bool enableBorderline)
88 mBorderline = (enableBorderline ? Borderline::ENABLED : Borderline::DISABLED);
91 FeatureBuilder& FeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
96 FeatureBuilder& FeatureBuilder::EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering)
98 mAlphaMaskingOnRendering = (enableAlphaMaskingOnRendering ? AlphaMaskingOnRendering::ENABLED : AlphaMaskingOnRendering::DISABLED);
101 } // namespace ImageVisualShaderFeature
103 ImageVisualShaderFactory::ImageVisualShaderFactory()
104 : mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED)
108 ImageVisualShaderFactory::~ImageVisualShaderFactory()
112 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder)
115 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
117 const auto& atlasing = featureBuilder.mTextureAtlas;
118 const auto& defaultTextureWrapping = featureBuilder.mDefaultTextureWrapMode;
119 const auto& roundedCorner = featureBuilder.mRoundedCorner;
120 const auto& borderline = featureBuilder.mBorderline;
121 const auto& alphaMaskingOnRendering = featureBuilder.mAlphaMaskingOnRendering;
122 const auto& changeFragmentShader = (featureBuilder.mTexture && DevelTexture::IsNative(featureBuilder.mTexture))
123 ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
124 : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
126 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
128 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
130 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
134 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
139 uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
140 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
142 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
144 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
146 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
148 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
150 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
152 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
155 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE &&
156 (mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED ||
157 mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE))
159 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) + NATIVE_SHADER_TYPE_OFFSET);
162 shader = factoryCache.GetShader(shaderType);
165 std::string vertexShaderPrefixList;
166 std::string fragmentShaderPrefixList;
167 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
169 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
171 fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP\n";
175 fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP\n";
180 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
182 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
183 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER\n";
185 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
187 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
188 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE\n";
190 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
192 vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
193 fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING\n";
197 std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_VERT.data());
198 std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
200 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
202 if(DALI_UNLIKELY(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED))
204 // NOTE : This routine will run exist one times.
206 // First, we will run ApplyNativeFragmentShader
207 // - If fragment shader is modified, then current platform allow to change fragment shader.
208 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::NEED_CHANGE.
209 // - If fragment shader is not modified, then current platform will always don't change fragment shader.
210 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::DONT_CHANGE.
211 // And change current shaderType into normal image range.
212 // After cached the result, shaderType never become NATIVE_IMAGE_SHADER anymore.
213 // Second, save shader result.
215 // Try to apply fragmentShader
216 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
219 // Now we know that fragment shader need to change.
220 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE;
224 // Now we know that fragment shader even don't need to change.
225 // We can skip ApplyNativeFragmentShader routine after now.
226 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
228 // Now we need normal shader type
229 // So decrease NATIVE_SHADER_TYPE_OFFSET.
230 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) - NATIVE_SHADER_TYPE_OFFSET);
232 // If we already compiled this type already, just use that cached shader.
233 // Else, just go forward.
234 shader = factoryCache.GetShader(shaderType);
241 else if(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
243 // Always need to apply fragmentShader
244 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
245 DALI_ASSERT_ALWAYS(modified && "NativeImageTexture need to change fragment shader. But DALI default image shader doesn't changed!");
248 shader = Shader::New(vertexShader, fragmentShader);
249 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
250 factoryCache.SaveShader(shaderType, shader);
256 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
258 if(gVertexShader.empty())
260 gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
263 return gVertexShader;
266 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
268 if(gFragmentShaderNoAtlas.empty())
270 gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
272 return gFragmentShaderNoAtlas;
275 } // namespace Internal
277 } // namespace Toolkit