2 * Copyright (c) 2023 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.
19 #include <dali-scene3d/public-api/loader/shader-definition.h>
22 #include <dali/public-api/object/property-array.h>
26 #include <dali-scene3d/internal/graphics/builtin-shader-extern-gen.h>
27 #include <dali-scene3d/public-api/loader/utils.h>
29 namespace Dali::Scene3D::Loader
33 const char* SHADER_HINT_OUTPUT_IS_TRANSPARENT("OUTPUT_IS_TRANSPARENT"); ///< Might generate transparent alpha from opaque inputs.
34 const char* SHADER_HINT_MODIFIES_GEOMETRY("MODIFIES_GEOMETRY"); ///< Might change position of vertices, this option disables any culling optimizations.
37 ShaderDefinition::ShaderDefinition(const ShaderDefinition& other)
38 : mRendererState(other.mRendererState),
39 mVertexShaderPath(other.mVertexShaderPath),
40 mFragmentShaderPath(other.mFragmentShaderPath),
41 mDefines(other.mDefines),
43 mUniforms(other.mUniforms),
44 mUseBuiltInShader(other.mUseBuiltInShader)
48 void ApplyDefine(std::string& shaderCode, const std::string& definevar)
50 const std::string IF_1 = "#if 1";
52 std::size_t found = shaderCode.find(definevar);
53 while(found != std::string::npos)
55 // Greater then "@" character means is a letter,
56 // therefore is not has the definevar we looking for.
57 if((found + definevar.length()) < shaderCode.length() && shaderCode.at(found + definevar.length()) > '@')
59 found = shaderCode.find(definevar, found + definevar.length());
62 if(found > 0u && shaderCode.at(found - 1) > '@')
64 found = shaderCode.find(definevar, found + definevar.length());
68 std::size_t defidx = shaderCode.rfind("#ifdef", found);
69 std::size_t newlineidx = shaderCode.rfind("\n", found);
70 if(newlineidx < defidx && defidx != std::string::npos)
72 shaderCode.replace(defidx, found - defidx + definevar.length(), IF_1);
73 found = defidx + IF_1.length();
77 found += definevar.length();
79 found = shaderCode.find(definevar, found);
83 void RedefineMacro(std::string& shaderCode, const std::string& macro, const std::string& value)
85 std::string definition = "#define " + macro;
86 std::size_t found = shaderCode.find(definition);
87 if(found != std::string::npos)
89 std::size_t insertionPoint = found + definition.length();
91 // Automatically insert line-continuation character into value
93 std::sregex_token_iterator first{value.begin(), value.end(), re, -1}, last;
94 for(auto i = first; i != last; ++i)
96 std::string line = std::string("\\\n") + (*i).str();
97 shaderCode.insert(insertionPoint, line);
98 insertionPoint += line.length();
103 ShaderDefinition::RawData
104 ShaderDefinition::LoadRaw(const std::string& shadersPath) const
109 if(!mUseBuiltInShader)
111 raw.mVertexShaderSource = LoadTextFile((shadersPath + mVertexShaderPath).c_str(), &fail);
114 raw.mFragmentShaderSource = LoadTextFile((shadersPath + mFragmentShaderPath).c_str(), &fail);
117 ExceptionFlinger(ASSERT_LOCATION) << "Failed to load shader source from '" << shadersPath + mFragmentShaderPath << "'.";
122 ExceptionFlinger(ASSERT_LOCATION) << "Failed to load shader source from '" << shadersPath + mVertexShaderPath << "'.";
127 raw.mVertexShaderSource = SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_VERT.data();
128 raw.mFragmentShaderSource = SHADER_DEFAULT_PHYSICALLY_BASED_SHADER_FRAG.data();
129 raw.mShadowVertexShaderSource = SHADER_SHADOW_MAP_SHADER_VERT.data();
130 raw.mShadowFragmentShaderSource = SHADER_SHADOW_MAP_SHADER_FRAG.data();
135 for(const auto& definevar : mDefines)
137 ApplyDefine(raw.mVertexShaderSource, definevar);
138 ApplyDefine(raw.mFragmentShaderSource, definevar);
139 ApplyDefine(raw.mShadowVertexShaderSource, definevar);
141 for(const auto& macroDef : mMacros)
143 RedefineMacro(raw.mVertexShaderSource, macroDef.macro, macroDef.definition);
144 RedefineMacro(raw.mFragmentShaderSource, macroDef.macro, macroDef.definition);
145 RedefineMacro(raw.mShadowVertexShaderSource, macroDef.macro, macroDef.definition);
152 Shader ShaderDefinition::Load(RawData&& raw) const
154 uint32_t hints = Shader::Hint::NONE;
155 for(const auto& hint : mHints)
157 if(hint == SHADER_HINT_OUTPUT_IS_TRANSPARENT)
159 hints |= Shader::Hint::OUTPUT_IS_TRANSPARENT;
161 else if(hint == SHADER_HINT_MODIFIES_GEOMETRY)
163 hints |= Shader::Hint::MODIFIES_GEOMETRY;
167 Property::Map map[2];
168 map[0]["vertex"] = raw.mVertexShaderSource;
169 map[0]["fragment"] = raw.mFragmentShaderSource;
170 map[0]["renderPassTag"] = 0;
171 map[0]["hints"] = static_cast<Shader::Hint::Value>(hints);
173 map[1]["vertex"] = raw.mShadowVertexShaderSource;
174 map[1]["fragment"] = raw.mShadowFragmentShaderSource;
175 map[1]["renderPassTag"] = 10;
177 Property::Array array;
178 array.PushBack(map[0]);
179 array.PushBack(map[1]);
181 Shader shader = Shader::New(array);
182 for(Property::Map::SizeType i0 = 0, i1 = mUniforms.Count(); i0 != i1; ++i0)
184 auto pair = mUniforms.GetKeyValue(i0);
185 DALI_ASSERT_ALWAYS(pair.first.type == Property::Key::STRING);
186 shader.RegisterProperty(pair.first.stringKey, pair.second);
192 } // namespace Dali::Scene3D::Loader