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>
23 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
24 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
25 #include <dali/integration-api/debug.h>
35 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
37 // global string variable to caching complate vertex shader
38 static std::string gVertexShader;
40 // global string variable to caching complate fragment shader (no atlas)
41 static std::string gFragmentShaderNoAtlas;
43 } // unnamed namespace
45 ImageVisualShaderFactory::ImageVisualShaderFactory()
49 ImageVisualShaderFactory::~ImageVisualShaderFactory()
53 Shader ImageVisualShaderFactory::GetShader(VisualFactoryCache& factoryCache, TextureAtlas atlasing, DefaultTextureWrapMode defaultTextureWrapping, RoundedCorner roundedCorner, Borderline borderline)
56 VisualFactoryCache::ShaderType shaderType = VisualFactoryCache::IMAGE_SHADER;
57 if(atlasing == TextureAtlas::ENABLED)
59 if(defaultTextureWrapping == DefaultTextureWrapMode::APPLY)
61 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_DEFAULT_WRAP;
65 shaderType = VisualFactoryCache::IMAGE_SHADER_ATLAS_CUSTOM_WRAP;
70 if(roundedCorner == RoundedCorner::ENABLED)
72 if(borderline == Borderline::ENABLED)
74 shaderType = VisualFactoryCache::IMAGE_SHADER_ROUNDED_BORDERLINE;
78 shaderType = VisualFactoryCache::IMAGE_SHADER_ROUNDED_CORNER;
83 if(borderline == Borderline::ENABLED)
85 shaderType = VisualFactoryCache::IMAGE_SHADER_BORDERLINE;
90 shader = factoryCache.GetShader(shaderType);
93 std::string vertexShaderPrefixList;
94 std::string fragmentShaderPrefixList;
95 if(atlasing == TextureAtlas::ENABLED)
97 if(defaultTextureWrapping == DefaultTextureWrapMode::APPLY)
99 fragmentShaderPrefixList += "#define ATLAS_DEFAULT_WARP 1\n";
103 fragmentShaderPrefixList += "#define ATLAS_CUSTOM_WARP 1\n";
108 if(roundedCorner == RoundedCorner::ENABLED)
110 vertexShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
111 fragmentShaderPrefixList += "#define IS_REQUIRED_ROUNDED_CORNER 1\n";
113 if(borderline == Borderline::ENABLED)
115 vertexShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
116 fragmentShaderPrefixList += "#define IS_REQUIRED_BORDERLINE 1\n";
120 shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + vertexShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_VERT.data(),
121 Dali::Shader::GetFragmentShaderPrefix() + fragmentShaderPrefixList + SHADER_IMAGE_VISUAL_SHADER_FRAG.data());
122 shader.RegisterProperty(PIXEL_AREA_UNIFORM_NAME, FULL_TEXTURE_RECT);
123 factoryCache.SaveShader(shaderType, shader);
129 std::string_view ImageVisualShaderFactory::GetVertexShaderSource()
131 if(gVertexShader.empty())
133 gVertexShader = Dali::Shader::GetVertexShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_VERT.data();
136 return gVertexShader;
139 std::string_view ImageVisualShaderFactory::GetFragmentShaderSource()
141 if(gFragmentShaderNoAtlas.empty())
143 gFragmentShaderNoAtlas = Dali::Shader::GetFragmentShaderPrefix() + SHADER_IMAGE_VISUAL_SHADER_FRAG.data();
145 return gFragmentShaderNoAtlas;
148 } // namespace Internal
150 } // namespace Toolkit