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);
77 FeatureBuilder& FeatureBuilder::ApplyDefaultTextureWrapMode(bool applyDefaultTextureWrapMode)
79 mDefaultTextureWrapMode = (applyDefaultTextureWrapMode ? DefaultTextureWrapMode::APPLY : DefaultTextureWrapMode::DO_NOT_APPLY);
83 FeatureBuilder& FeatureBuilder::EnableRoundedCorner(bool enableRoundedCorner)
85 mRoundedCorner = (enableRoundedCorner ? RoundedCorner::ENABLED : RoundedCorner::DISABLED);
89 FeatureBuilder& FeatureBuilder::EnableBorderline(bool enableBorderline)
91 mBorderline = (enableBorderline ? Borderline::ENABLED : Borderline::DISABLED);
95 FeatureBuilder& FeatureBuilder::SetTextureForFragmentShaderCheck(const Dali::Texture& texture)
101 FeatureBuilder& FeatureBuilder::EnableAlphaMasking(bool enableAlphaMasking)
103 mAlphaMasking = (enableAlphaMasking ? AlphaMasking::ENABLED : AlphaMasking::DISABLED);
107 FeatureBuilder& FeatureBuilder::EnableYuvToRgb(bool enableYuvToRgb)
109 mColorConversion = (enableYuvToRgb ? ColorConversion::YUV_TO_RGB : ColorConversion::DONT_NEED);
112 } // namespace ImageVisualShaderFeature
114 ImageVisualShaderFactory::ImageVisualShaderFactory()
115 : mFragmentShaderNeedChange(ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED)
119 ImageVisualShaderFactory::~ImageVisualShaderFactory()
123 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, const ImageVisualShaderFeature::FeatureBuilder& featureBuilder)
126 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
128 const auto& atlasing = featureBuilder.mTextureAtlas;
129 const auto& defaultTextureWrapping = featureBuilder.mDefaultTextureWrapMode;
130 const auto& roundedCorner = featureBuilder.mRoundedCorner;
131 const auto& borderline = featureBuilder.mBorderline;
132 const auto& alphaMasking = featureBuilder.mAlphaMasking;
133 const auto& colorConversion = featureBuilder.mColorConversion;
134 const auto& changeFragmentShader = (featureBuilder.mTexture && DevelTexture::IsNative(featureBuilder.mTexture))
135 ? ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE
136 : ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
138 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
140 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
142 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
146 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
151 uint32_t shaderTypeFlag = static_cast<uint32_t>(ImageVisualRequireFlag::DEFAULT);
152 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
154 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ROUNDED_CORNER);
156 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
158 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::BORDERLINE);
160 if(alphaMasking == ImageVisualShaderFeature::AlphaMasking::ENABLED)
162 shaderTypeFlag |= static_cast<uint32_t>(ImageVisualRequireFlag::ALPHA_MASKING);
164 shaderType = SHADER_TYPE_TABLE[shaderTypeFlag];
167 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE &&
168 (mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED ||
169 mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE))
171 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) + NATIVE_SHADER_TYPE_OFFSET);
174 if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB)
176 shaderType = VisualFactoryCache::IMAGE_SHADER_YUV_TO_RGB;
179 shader = factoryCache.GetShader(shaderType);
182 std::string vertexShaderPrefixList;
183 std::string fragmentShaderPrefixList;
185 // We don't support other properties in case of yuv format for now.
186 if(colorConversion == ImageVisualShaderFeature::ColorConversion::YUV_TO_RGB)
188 fragmentShaderPrefixList += "#define IS_REQUIRED_YUV_TO_RGB 1\n";
192 if(atlasing == ImageVisualShaderFeature::TextureAtlas::ENABLED)
194 if(defaultTextureWrapping == ImageVisualShaderFeature::DefaultTextureWrapMode::APPLY)
196 fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP 1\n";
200 fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP 1\n";
205 if(roundedCorner == ImageVisualShaderFeature::RoundedCorner::ENABLED)
207 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
208 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
210 if(borderline == ImageVisualShaderFeature::Borderline::ENABLED)
212 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
213 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
215 if(alphaMasking == ImageVisualShaderFeature::AlphaMasking::ENABLED)
217 vertexShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING 1\n";
218 fragmentShaderPrefixList += "#define IS_REQUIRED_ALPHA_MASKING 1\n";
223 std::string vertexShader = std::string(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_VERT.data());
224 std::string fragmentShader = std::string(Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
226 if(changeFragmentShader == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
228 if(DALI_UNLIKELY(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::UNDECIDED))
230 // NOTE : This routine will run exist one times.
232 // First, we will run ApplyNativeFragmentShader
233 // - If fragment shader is modified, then current platform allow to change fragment shader.
234 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::NEED_CHANGE.
235 // - If fragment shader is not modified, then current platform will always don't change fragment shader.
236 // We cache this result mFragmentShaderNeedChange = ChangeFragmentShader::DONT_CHANGE.
237 // And change current shaderType into normal image range.
238 // After cached the result, shaderType never become NATIVE_IMAGE_SHADER anymore.
239 // Second, save shader result.
241 // Try to apply fragmentShader
242 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
245 // Now we know that fragment shader need to change.
246 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE;
250 // Now we know that fragment shader even don't need to change.
251 // We can skip ApplyNativeFragmentShader routine after now.
252 mFragmentShaderNeedChange = ImageVisualShaderFeature::ChangeFragmentShader::DONT_CHANGE;
254 // Now we need normal shader type
255 // So decrease NATIVE_SHADER_TYPE_OFFSET.
256 shaderType = static_cast<VisualFactoryCache::ShaderType>(static_cast<int>(shaderType) - NATIVE_SHADER_TYPE_OFFSET);
258 // If we already compiled this type already, just use that cached shader.
259 // Else, just go forward.
260 shader = factoryCache.GetShader(shaderType);
267 else if(mFragmentShaderNeedChange == ImageVisualShaderFeature::ChangeFragmentShader::NEED_CHANGE)
269 // Always need to apply fragmentShader
270 bool modified = DevelTexture::ApplyNativeFragmentShader(featureBuilder.mTexture, fragmentShader);
271 DALI_ASSERT_ALWAYS(modified && "NativeImageTexture need to change fragment shader. But DALI default image shader doesn't changed!");
274 shader = Shader::New(vertexShader, fragmentShader);
275 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
276 factoryCache.SaveShader(shaderType, shader);
282 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
284 if(gVertexShader.empty())
286 gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
289 return gVertexShader;
292 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
294 if(gFragmentShaderNoAtlas.empty())
296 gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
298 return gFragmentShaderNoAtlas;
301 } // namespace Internal
303 } // namespace Toolkit