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-manager.h>
22 #include <dali/devel-api/common/map-wrapper.h>
23 #include <dali/public-api/animation/constraint.h>
27 #include <dali-scene3d/internal/light/light-impl.h>
28 #include <dali-scene3d/internal/loader/hash.h>
29 #include <dali-scene3d/public-api/loader/blend-shape-details.h>
30 #include <dali-scene3d/public-api/loader/node-definition.h>
32 #include <dali/integration-api/debug.h>
36 #if defined(DEBUG_ENABLED)
37 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_MODEL_SHADER_MANAGER");
41 namespace Dali::Scene3D::Loader
45 static constexpr uint32_t INDEX_FOR_LIGHT_CONSTRAINT_TAG = 10;
46 static constexpr uint32_t INDEX_FOR_SHADOW_CONSTRAINT_TAG = 100;
48 ShaderOption MakeOption(const MaterialDefinition& materialDef, const MeshDefinition& meshDef)
52 const bool hasTransparency = MaskMatch(materialDef.mFlags, MaterialDefinition::TRANSPARENCY);
55 option.SetTransparency();
59 !materialDef.CheckTextures(MaterialDefinition::ALBEDO | MaterialDefinition::METALLIC) ||
60 !materialDef.CheckTextures(MaterialDefinition::NORMAL | MaterialDefinition::ROUGHNESS))
62 option.AddOption(ShaderOption::Type::THREE_TEXTURE);
64 // For the glTF, each of basecolor, metallic_roughness, normal texture is not essential.
65 if(MaskMatch(materialDef.mFlags, MaterialDefinition::ALBEDO))
67 option.AddOption(ShaderOption::Type::BASE_COLOR_TEXTURE);
70 if(materialDef.CheckTextures(MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS))
72 option.AddOption(ShaderOption::Type::METALLIC_ROUGHNESS_TEXTURE);
75 if(MaskMatch(materialDef.mFlags, MaterialDefinition::NORMAL))
77 option.AddOption(ShaderOption::Type::NORMAL_TEXTURE);
81 if(materialDef.GetAlphaCutoff() > 0.f)
83 option.AddOption(ShaderOption::Type::ALPHA_TEST);
86 if(MaskMatch(materialDef.mFlags, MaterialDefinition::SUBSURFACE))
88 option.AddOption(ShaderOption::Type::SUBSURFACE);
91 if(MaskMatch(materialDef.mFlags, MaterialDefinition::OCCLUSION))
93 option.AddOption(ShaderOption::Type::OCCLUSION);
96 if(MaskMatch(materialDef.mFlags, MaterialDefinition::EMISSIVE))
98 option.AddOption(ShaderOption::Type::EMISSIVE);
101 if(MaskMatch(materialDef.mFlags, MaterialDefinition::SPECULAR))
103 option.AddOption(ShaderOption::Type::SPECULAR);
106 if(MaskMatch(materialDef.mFlags, MaterialDefinition::SPECULAR_COLOR))
108 option.AddOption(ShaderOption::Type::SPECULAR_COLOR);
111 if(MaskMatch(materialDef.mFlags, MaterialDefinition::GLTF_CHANNELS))
113 option.AddOption(ShaderOption::Type::GLTF_CHANNELS);
116 if(meshDef.IsSkinned())
118 option.AddOption(ShaderOption::Type::SKINNING);
119 option.AddJointMacros(meshDef.mJoints.size());
123 option.AddJointMacros(0);
126 if(MaskMatch(meshDef.mFlags, MeshDefinition::FLIP_UVS_VERTICAL))
128 option.AddOption(ShaderOption::Type::FLIP_UVS_VERTICAL);
131 if(!meshDef.mColors.empty() && meshDef.mColors[0].IsDefined())
133 option.AddOption(ShaderOption::Type::COLOR_ATTRIBUTE);
136 if(meshDef.mTangentType == Property::VECTOR4)
138 option.AddOption(ShaderOption::Type::VEC4_TANGENT);
141 if(meshDef.HasBlendShapes())
143 bool hasPositions = false;
144 bool hasNormals = false;
145 bool hasTangents = false;
146 meshDef.RetrieveBlendShapeComponents(hasPositions, hasNormals, hasTangents);
149 option.AddOption(ShaderOption::Type::MORPH_POSITION);
154 option.AddOption(ShaderOption::Type::MORPH_NORMAL);
159 option.AddOption(ShaderOption::Type::MORPH_TANGENT);
162 if(hasPositions || hasNormals || hasTangents)
164 if(BlendShapes::Version::VERSION_2_0 == meshDef.mBlendShapeVersion)
166 option.AddOption(ShaderOption::Type::MORPH_VERSION_2_0);
175 struct ShaderManager::Impl
177 std::map<uint64_t, Index> mShaderMap;
178 std::vector<Dali::Shader> mShaders;
179 std::vector<Scene3D::Light> mLights;
181 Scene3D::Light mShadowLight;
184 ShaderManager::ShaderManager()
189 ShaderManager::~ShaderManager() = default;
191 ShaderOption ShaderManager::ProduceShaderOption(const MaterialDefinition& materialDefinition, const MeshDefinition& meshDefinition)
193 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Defining shader from mat/mesh definitions\n");
194 return MakeOption(materialDefinition, meshDefinition);
197 Dali::Shader ShaderManager::ProduceShader(const ShaderOption& shaderOption)
201 auto& shaderMap = mImpl->mShaderMap;
202 uint64_t hash = shaderOption.GetOptionHash();
204 #if defined(DEBUG_ENABLED)
205 std::ostringstream oss;
206 oss << " ShaderOption defines:";
207 std::vector<std::string> defines;
208 shaderOption.GetDefines(defines);
209 for(auto& def : defines)
214 << " ShaderOption macro definitions:" << std::endl;
215 for(auto& macro : shaderOption.GetMacroDefinitions())
217 oss << macro.macro << " : " << macro.definition << std::endl;
219 DALI_LOG_INFO(gLogFilter, Debug::Concise, "ShaderOption:\n%s\n", oss.str().c_str());
222 auto iFind = shaderMap.find(hash);
223 if(iFind != shaderMap.end())
225 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Defining Shader found: hash: %lx", hash);
226 result = mImpl->mShaders[iFind->second];
230 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Creating new shader: hash: %lx\n", hash);
231 ShaderDefinition shaderDef;
232 shaderDef.mUseBuiltInShader = true;
234 shaderOption.GetDefines(shaderDef.mDefines);
235 shaderDef.mMacros = shaderOption.GetMacroDefinitions();
236 shaderDef.mUniforms["uCubeMatrix"] = Matrix::IDENTITY;
238 shaderMap[hash] = mImpl->mShaders.size();
240 auto raw = shaderDef.LoadRaw("");
241 mImpl->mShaders.emplace_back(shaderDef.Load(std::move(raw)));
242 result = mImpl->mShaders.back();
244 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
245 result.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
247 for(uint32_t index = 0; index < mImpl->mLights.size(); ++index)
249 SetLightConstraintToShader(index, result);
252 result.RegisterProperty("uIsShadowEnabled", static_cast<int32_t>(!!mImpl->mShadowLight));
253 if(!!mImpl->mShadowLight)
255 SetShadowConstraintToShader(result);
256 SetShadowUniformToShader(result);
263 RendererState::Type ShaderManager::GetRendererState(const MaterialDefinition& materialDefinition)
265 RendererState::Type rendererState = RendererState::DEPTH_TEST;
267 if(!materialDefinition.mDoubleSided)
269 rendererState |= RendererState::CULL_BACK;
272 const bool hasTransparency = MaskMatch(materialDefinition.mFlags, MaterialDefinition::TRANSPARENCY);
275 // TODO: this requires more granularity
276 rendererState = (rendererState | RendererState::ALPHA_BLEND);
278 return rendererState;
281 bool ShaderManager::AddLight(Scene3D::Light light)
283 if(!light || mImpl->mLights.size() >= Scene3D::Internal::Light::GetMaximumEnabledLightCount())
288 uint32_t lightIndex = mImpl->mLights.size();
289 mImpl->mLights.push_back(light);
291 for(auto&& shader : mImpl->mShaders)
293 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
294 shader.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
297 SetLightConstraint(lightIndex);
302 void ShaderManager::RemoveLight(Scene3D::Light light)
304 uint32_t lightCount = mImpl->mLights.size();
305 for(uint32_t index = 0; index < lightCount; ++index)
307 if(mImpl->mLights[index] != light)
312 RemoveLightConstraint(index);
314 if(!mImpl->mLights.empty() && light != mImpl->mLights.back())
316 RemoveLightConstraint(mImpl->mLights.size() - 1);
317 mImpl->mLights[index] = mImpl->mLights.back();
318 SetLightConstraint(index);
321 mImpl->mLights.pop_back();
323 for(auto&& shader : mImpl->mShaders)
325 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
326 shader.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
332 uint32_t ShaderManager::GetLightCount() const
334 return mImpl->mLights.size();
337 void ShaderManager::SetShadow(Scene3D::Light light)
339 mImpl->mShadowLight = light;
340 for(auto&& shader : mImpl->mShaders)
342 std::string shadowEnabledPropertyName(Scene3D::Internal::Light::GetShadowEnabledUniformName());
343 shader.RegisterProperty(shadowEnabledPropertyName, static_cast<int32_t>(true));
349 void ShaderManager::RemoveShadow()
351 for(auto&& shader : mImpl->mShaders)
353 std::string shadowEnabledPropertyName(Scene3D::Internal::Light::GetShadowEnabledUniformName());
354 shader.RegisterProperty(shadowEnabledPropertyName, static_cast<int32_t>(false));
355 shader.RemoveConstraints(INDEX_FOR_SHADOW_CONSTRAINT_TAG);
357 mImpl->mShadowLight.Reset();
360 void ShaderManager::UpdateShadowUniform(Scene3D::Light light)
362 if(light != mImpl->mShadowLight)
367 for(auto&& shader : mImpl->mShaders)
369 SetShadowUniformToShader(shader);
373 void ShaderManager::SetLightConstraint(uint32_t lightIndex)
375 for(auto&& shader : mImpl->mShaders)
377 SetLightConstraintToShader(lightIndex, shader);
381 void ShaderManager::SetLightConstraintToShader(uint32_t lightIndex, Dali::Shader shader)
383 std::string lightDirectionPropertyName(Scene3D::Internal::Light::GetLightDirectionUniformName());
384 lightDirectionPropertyName += "[" + std::to_string(lightIndex) + "]";
385 auto lightDirectionPropertyIndex = shader.RegisterProperty(lightDirectionPropertyName, Vector3::ZAXIS);
386 Dali::Constraint lightDirectionConstraint = Dali::Constraint::New<Vector3>(shader, lightDirectionPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) { output = inputs[0]->GetQuaternion().Rotate(Vector3::ZAXIS); });
387 lightDirectionConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::WORLD_ORIENTATION});
388 lightDirectionConstraint.ApplyPost();
389 lightDirectionConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
391 std::string lightColorPropertyName(Scene3D::Internal::Light::GetLightColorUniformName());
392 lightColorPropertyName += "[" + std::to_string(lightIndex) + "]";
393 auto lightColorPropertyIndex = shader.RegisterProperty(lightColorPropertyName, Vector3(Color::WHITE));
394 Dali::Constraint lightColorConstraint = Dali::Constraint::New<Vector3>(shader, lightColorPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) { output = Vector3(inputs[0]->GetVector4()); });
395 lightColorConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::COLOR});
396 lightColorConstraint.ApplyPost();
397 lightColorConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
400 void ShaderManager::RemoveLightConstraint(uint32_t lightIndex)
402 for(auto&& shader : mImpl->mShaders)
404 shader.RemoveConstraints(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
408 void ShaderManager::SetShadowUniformToShader(Dali::Shader shader)
410 shader.RegisterProperty("uShadowIntensity", mImpl->mShadowLight.GetShadowIntensity());
411 shader.RegisterProperty("uShadowBias", mImpl->mShadowLight.GetShadowBias());
412 shader.RegisterProperty("uEnableShadowSoftFiltering", static_cast<int>(mImpl->mShadowLight.IsShadowSoftFilteringEnabled()));
415 void ShaderManager::SetShadowProperty()
417 for(auto&& shader : mImpl->mShaders)
419 SetShadowUniformToShader(shader);
420 SetShadowConstraintToShader(shader);
424 void ShaderManager::SetShadowConstraintToShader(Dali::Shader shader)
426 // Constraint is applied before View/Projection Matrix is computed in update thread.
427 // So, it could show not plausible result if camera properties are changed discontinuesly.
428 // If we want to make it be synchronized, View/Projection matrix are needed to be conputed in below constraint.
430 std::string shadowViewProjectionPropertyName(Scene3D::Internal::Light::GetShadowViewProjectionMatrixUniformName());
431 auto shadowViewProjectionPropertyIndex = shader.RegisterProperty(shadowViewProjectionPropertyName, Matrix::IDENTITY);
432 Dali::CameraActor shadowLightCamera = Dali::Scene3D::Internal::GetImplementation(mImpl->mShadowLight).GetCamera();
433 auto tempViewProjectionMatrixIndex = shadowLightCamera.GetPropertyIndex("tempViewProjectionMatrix");
434 if(tempViewProjectionMatrixIndex != Dali::Property::INVALID_INDEX)
436 tempViewProjectionMatrixIndex = shadowLightCamera.RegisterProperty("tempViewProjectionMatrix", Matrix::IDENTITY);
438 Dali::Constraint shadowViewProjectionConstraint = Dali::Constraint::New<Matrix>(shader, shadowViewProjectionPropertyIndex, [](Matrix& output, const PropertyInputContainer& inputs) { output = inputs[0]->GetMatrix(); });
439 shadowViewProjectionConstraint.AddSource(Source{shadowLightCamera, tempViewProjectionMatrixIndex});
440 shadowViewProjectionConstraint.ApplyPost();
441 shadowViewProjectionConstraint.SetTag(INDEX_FOR_SHADOW_CONSTRAINT_TAG);
444 } // namespace Dali::Scene3D::Loader