2 * Copyright (c) 2021 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
68 } // unnamed namespace
70 namespace ImageVisualShaderFeature
72 FeatureBuilder& FeatureBuilder::EnableTextureAtlas(bool enableAtlas)
74 mTextureAtlas = (enableAtlas ? TextureAtlas::ENABLED : TextureAtlas::DISABLED);
77 FeatureBuilder& FeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
79 mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? DefaultTextureWrapMode::APPLY : DefaultTextureWrapMode::DO_NOT_APPLY);
82 FeatureBuilder& FeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
84 mRoundedCorner = (enableRoundedCorner ? RoundedCorner::ENABLED : RoundedCorner::DISABLED);
87 FeatureBuilder& FeatureBuilder::EnableBorderline(bool enableBorderline)
89 mBorderline = (enableBorderline ? Borderline::ENABLED : Borderline::DISABLED);
92 FeatureBuilder& FeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
97 FeatureBuilder& FeatureBuilder::EnableAlphaMaskingOnRendering(bool enableAlphaMaskingOnRendering)
99 mAlphaMaskingOnRendering = (enableAlphaMaskingOnRendering ? AlphaMaskingOnRendering::ENABLED : AlphaMaskingOnRendering::DISABLED);
102 } // namespace ImageVisualShaderFeature
104 ImageVisualShaderFactory::ImageVisualShaderFactory()
105 : mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED)
109 ImageVisualShaderFactory::~ImageVisualShaderFactory()
113 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder)
116 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
118 const auto& atlasing = featureBuilder.mTextureAtlas;
119 const auto& defaultTextureWrapping = featureBuilder.mDefaultTextureWrapMode;
120 const auto& roundedCorner = featureBuilder.mRoundedCorner;
121 const auto& borderline = featureBuilder.mBorderline;
122 const auto& alphaMaskingOnRendering = featureBuilder.mAlphaMaskingOnRendering;
123 const auto& changeFragmentShader = (featureBuilder.mTexture && DevelTexture::IsNative(featureBuilder.mTexture))
124 ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
125 : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
127 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
129 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
131 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
135 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
140 uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
141 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
143 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
145 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
147 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
149 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
151 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
153 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
156 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE &&
157 (mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED ||
158 mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE))
160 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) + NATIVE_SHADER_TYPE_OFFSET);
163 shader = factoryCache.GetShader(shaderType);
166 std::string vertexShaderPrefixList;
167 std::string fragmentShaderPrefixList;
168 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
170 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
172 fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP 1\n";
176 fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP 1\n";
181 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
183 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
184 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
186 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
188 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
189 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
191 if(alphaMaskingOnRendering == ImageVisualShaderFeature::AlphaMaskingOnRendering::ENABLED)
193 vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING 1\n";
194 fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING 1\n";
198 std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_VERT.data());
199 std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
201 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
203 if(DALI_UNLIKELY(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED))
205 // NOTE : This routine will run exist one times.
207 // First, we will run ApplyNativeFragmentShader
208 // - If fragment shader is modified, then current platform allow to change fragment shader.
209 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::NEED_CHANGE.
210 // - If fragment shader is not modified, then current platform will always don't change fragment shader.
211 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::DONT_CHANGE.
212 // And change current shaderType into normal image range.
213 // After cached the result, shaderType never become NATIVE_IMAGE_SHADER anymore.
214 // Second, save shader result.
216 // Try to apply fragmentShader
217 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
220 // Now we know that fragment shader need to change.
221 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE;
225 // Now we know that fragment shader even don't need to change.
226 // We can skip ApplyNativeFragmentShader routine after now.
227 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
229 // Now we need normal shader type
230 // So decrease NATIVE_SHADER_TYPE_OFFSET.
231 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) - NATIVE_SHADER_TYPE_OFFSET);
233 // If we already compiled this type already, just use that cached shader.
234 // Else, just go forward.
235 shader = factoryCache.GetShader(shaderType);
242 else if(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
244 // Always need to apply fragmentShader
245 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
246 DALI_ASSERT_ALWAYS(modified && "NativeImageTexture need to change fragment shader. But DALI default image shader doesn't changed!");
249 shader = Shader::New(vertexShader, fragmentShader);
250 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
251 factoryCache.SaveShader(shaderType, shader);
257 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
259 if(gVertexShader.empty())
261 gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
264 return gVertexShader;
267 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
269 if(gFragmentShaderNoAtlas.empty())
271 gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
273 return gFragmentShaderNoAtlas;
276 } // namespace Internal
278 } // namespace Toolkit