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());
122 if(MaskMatch(meshDef.mFlags, MeshDefinition::FLIP_UVS_VERTICAL))
124 option.AddOption(ShaderOption::Type::FLIP_UVS_VERTICAL);
127 if(!meshDef.mColors.empty() && meshDef.mColors[0].IsDefined())
129 option.AddOption(ShaderOption::Type::COLOR_ATTRIBUTE);
132 if(meshDef.mTangentType == Property::VECTOR4)
134 option.AddOption(ShaderOption::Type::VEC4_TANGENT);
137 if(meshDef.HasBlendShapes())
139 bool hasPositions = false;
140 bool hasNormals = false;
141 bool hasTangents = false;
142 meshDef.RetrieveBlendShapeComponents(hasPositions, hasNormals, hasTangents);
145 option.AddOption(ShaderOption::Type::MORPH_POSITION);
150 option.AddOption(ShaderOption::Type::MORPH_NORMAL);
155 option.AddOption(ShaderOption::Type::MORPH_TANGENT);
158 if(hasPositions || hasNormals || hasTangents)
160 if(BlendShapes::Version::VERSION_2_0 == meshDef.mBlendShapeVersion)
162 option.AddOption(ShaderOption::Type::MORPH_VERSION_2_0);
171 struct ShaderManager::Impl
173 std::map<uint64_t, Index> mShaderMap;
174 std::vector<Dali::Shader> mShaders;
175 std::vector<Scene3D::Light> mLights;
177 Scene3D::Light mShadowLight;
180 ShaderManager::ShaderManager()
185 ShaderManager::~ShaderManager() = default;
187 ShaderOption ShaderManager::ProduceShaderOption(const MaterialDefinition& materialDefinition, const MeshDefinition& meshDefinition)
189 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Defining shader from mat/mesh definitions\n");
190 return MakeOption(materialDefinition, meshDefinition);
193 Dali::Shader ShaderManager::ProduceShader(const ShaderOption& shaderOption)
197 auto& shaderMap = mImpl->mShaderMap;
198 uint64_t hash = shaderOption.GetOptionHash();
200 #if defined(DEBUG_ENABLED)
201 std::ostringstream oss;
202 oss << " ShaderOption defines:";
203 std::vector<std::string> defines;
204 shaderOption.GetDefines(defines);
205 for(auto& def : defines)
210 << " ShaderOption macro definitions:" << std::endl;
211 for(auto& macro : shaderOption.GetMacroDefinitions())
213 oss << macro.macro << " : " << macro.definition << std::endl;
215 DALI_LOG_INFO(gLogFilter, Debug::Concise, "ShaderOption:\n%s\n", oss.str().c_str());
218 auto iFind = shaderMap.find(hash);
219 if(iFind != shaderMap.end())
221 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Defining Shader found: hash: %lx", hash);
222 result = mImpl->mShaders[iFind->second];
226 DALI_LOG_INFO(gLogFilter, Debug::Concise, "Creating new shader: hash: %lx\n", hash);
227 ShaderDefinition shaderDef;
228 shaderDef.mUseBuiltInShader = true;
230 shaderOption.GetDefines(shaderDef.mDefines);
231 shaderDef.mMacros = shaderOption.GetMacroDefinitions();
232 shaderDef.mUniforms["uCubeMatrix"] = Matrix::IDENTITY;
234 shaderMap[hash] = mImpl->mShaders.size();
236 auto raw = shaderDef.LoadRaw("");
237 mImpl->mShaders.emplace_back(shaderDef.Load(std::move(raw)));
238 result = mImpl->mShaders.back();
240 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
241 result.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
243 for(uint32_t index = 0; index < mImpl->mLights.size(); ++index)
245 SetLightConstraintToShader(index, result);
248 result.RegisterProperty("uIsShadowEnabled", static_cast<int32_t>(!!mImpl->mShadowLight));
249 if(!!mImpl->mShadowLight)
251 SetShadowConstraintToShader(result);
252 SetShadowUniformToShader(result);
259 RendererState::Type ShaderManager::GetRendererState(const MaterialDefinition& materialDefinition)
261 RendererState::Type rendererState = RendererState::DEPTH_TEST;
263 if(!materialDefinition.mDoubleSided)
265 rendererState |= RendererState::CULL_BACK;
268 const bool hasTransparency = MaskMatch(materialDefinition.mFlags, MaterialDefinition::TRANSPARENCY);
271 // TODO: this requires more granularity
272 rendererState = (rendererState | RendererState::ALPHA_BLEND);
274 return rendererState;
277 bool ShaderManager::AddLight(Scene3D::Light light)
279 if(!light || mImpl->mLights.size() >= Scene3D::Internal::Light::GetMaximumEnabledLightCount())
284 uint32_t lightIndex = mImpl->mLights.size();
285 mImpl->mLights.push_back(light);
287 for(auto&& shader : mImpl->mShaders)
289 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
290 shader.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
293 SetLightConstraint(lightIndex);
298 void ShaderManager::RemoveLight(Scene3D::Light light)
300 uint32_t lightCount = mImpl->mLights.size();
301 for(uint32_t index = 0; index < lightCount; ++index)
303 if(mImpl->mLights[index] != light)
308 RemoveLightConstraint(index);
310 if(!mImpl->mLights.empty() && light != mImpl->mLights.back())
312 RemoveLightConstraint(mImpl->mLights.size() - 1);
313 mImpl->mLights[index] = mImpl->mLights.back();
314 SetLightConstraint(index);
317 mImpl->mLights.pop_back();
319 for(auto&& shader : mImpl->mShaders)
321 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
322 shader.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
328 uint32_t ShaderManager::GetLightCount() const
330 return mImpl->mLights.size();
333 void ShaderManager::SetShadow(Scene3D::Light light)
335 mImpl->mShadowLight = light;
336 for(auto&& shader : mImpl->mShaders)
338 std::string shadowEnabledPropertyName(Scene3D::Internal::Light::GetShadowEnabledUniformName());
339 shader.RegisterProperty(shadowEnabledPropertyName, static_cast<int32_t>(true));
345 void ShaderManager::RemoveShadow()
347 for(auto&& shader : mImpl->mShaders)
349 std::string shadowEnabledPropertyName(Scene3D::Internal::Light::GetShadowEnabledUniformName());
350 shader.RegisterProperty(shadowEnabledPropertyName, static_cast<int32_t>(false));
351 shader.RemoveConstraints(INDEX_FOR_SHADOW_CONSTRAINT_TAG);
353 mImpl->mShadowLight.Reset();
356 void ShaderManager::UpdateShadowUniform(Scene3D::Light light)
358 if(light != mImpl->mShadowLight)
363 for(auto&& shader : mImpl->mShaders)
365 SetShadowUniformToShader(shader);
369 void ShaderManager::SetLightConstraint(uint32_t lightIndex)
371 for(auto&& shader : mImpl->mShaders)
373 SetLightConstraintToShader(lightIndex, shader);
377 void ShaderManager::SetLightConstraintToShader(uint32_t lightIndex, Dali::Shader shader)
379 std::string lightDirectionPropertyName(Scene3D::Internal::Light::GetLightDirectionUniformName());
380 lightDirectionPropertyName += "[" + std::to_string(lightIndex) + "]";
381 auto lightDirectionPropertyIndex = shader.RegisterProperty(lightDirectionPropertyName, Vector3::ZAXIS);
382 Dali::Constraint lightDirectionConstraint = Dali::Constraint::New<Vector3>(shader, lightDirectionPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) { output = inputs[0]->GetQuaternion().Rotate(Vector3::ZAXIS); });
383 lightDirectionConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::WORLD_ORIENTATION});
384 lightDirectionConstraint.ApplyPost();
385 lightDirectionConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
387 std::string lightColorPropertyName(Scene3D::Internal::Light::GetLightColorUniformName());
388 lightColorPropertyName += "[" + std::to_string(lightIndex) + "]";
389 auto lightColorPropertyIndex = shader.RegisterProperty(lightColorPropertyName, Vector3(Color::WHITE));
390 Dali::Constraint lightColorConstraint = Dali::Constraint::New<Vector3>(shader, lightColorPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) { output = Vector3(inputs[0]->GetVector4()); });
391 lightColorConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::COLOR});
392 lightColorConstraint.ApplyPost();
393 lightColorConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
396 void ShaderManager::RemoveLightConstraint(uint32_t lightIndex)
398 for(auto&& shader : mImpl->mShaders)
400 shader.RemoveConstraints(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
404 void ShaderManager::SetShadowUniformToShader(Dali::Shader shader)
406 shader.RegisterProperty("uShadowIntensity", mImpl->mShadowLight.GetShadowIntensity());
407 shader.RegisterProperty("uShadowBias", mImpl->mShadowLight.GetShadowBias());
408 shader.RegisterProperty("uEnableShadowSoftFiltering", static_cast<int>(mImpl->mShadowLight.IsShadowSoftFilteringEnabled()));
411 void ShaderManager::SetShadowProperty()
413 for(auto&& shader : mImpl->mShaders)
415 SetShadowUniformToShader(shader);
416 SetShadowConstraintToShader(shader);
420 void ShaderManager::SetShadowConstraintToShader(Dali::Shader shader)
422 // Constraint is applied before View/Projection Matrix is computed in update thread.
423 // So, it could show not plausible result if camera properties are changed discontinuesly.
424 // If we want to make it be synchronized, View/Projection matrix are needed to be conputed in below constraint.
426 std::string shadowViewProjectionPropertyName(Scene3D::Internal::Light::GetShadowViewProjectionMatrixUniformName());
427 auto shadowViewProjectionPropertyIndex = shader.RegisterProperty(shadowViewProjectionPropertyName, Matrix::IDENTITY);
428 Dali::CameraActor shadowLightCamera = Dali::Scene3D::Internal::GetImplementation(mImpl->mShadowLight).GetCamera();
429 auto tempViewProjectionMatrixIndex = shadowLightCamera.GetPropertyIndex("tempViewProjectionMatrix");
430 if(tempViewProjectionMatrixIndex != Dali::Property::INVALID_INDEX)
432 tempViewProjectionMatrixIndex = shadowLightCamera.RegisterProperty("tempViewProjectionMatrix", Matrix::IDENTITY);
434 Dali::Constraint shadowViewProjectionConstraint = Dali::Constraint::New<Matrix>(shader, shadowViewProjectionPropertyIndex, [](Matrix& output, const PropertyInputContainer& inputs) { output = inputs[0]->GetMatrix(); });
435 shadowViewProjectionConstraint.AddSource(Source{shadowLightCamera, tempViewProjectionMatrixIndex});
436 shadowViewProjectionConstraint.ApplyPost();
437 shadowViewProjectionConstraint.SetTag(INDEX_FOR_SHADOW_CONSTRAINT_TAG);
440 } // namespace Dali::Scene3D::Loader