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>
34 namespace Dali::Scene3D::Loader
38 static constexpr uint32_t INDEX_FOR_LIGHT_CONSTRAINT_TAG = 10;
39 static constexpr uint32_t INDEX_FOR_SHADOW_CONSTRAINT_TAG = 100;
41 ShaderOption MakeOption(const MaterialDefinition& materialDef, const MeshDefinition& meshDef)
45 const bool hasTransparency = MaskMatch(materialDef.mFlags, MaterialDefinition::TRANSPARENCY);
48 option.SetTransparency();
52 !materialDef.CheckTextures(MaterialDefinition::ALBEDO | MaterialDefinition::METALLIC) ||
53 !materialDef.CheckTextures(MaterialDefinition::NORMAL | MaterialDefinition::ROUGHNESS))
55 option.AddOption(ShaderOption::Type::THREE_TEXTURE);
57 // For the glTF, each of basecolor, metallic_roughness, normal texture is not essential.
58 if(MaskMatch(materialDef.mFlags, MaterialDefinition::ALBEDO))
60 option.AddOption(ShaderOption::Type::BASE_COLOR_TEXTURE);
63 if(materialDef.CheckTextures(MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS))
65 option.AddOption(ShaderOption::Type::METALLIC_ROUGHNESS_TEXTURE);
68 if(MaskMatch(materialDef.mFlags, MaterialDefinition::NORMAL))
70 option.AddOption(ShaderOption::Type::NORMAL_TEXTURE);
74 if(materialDef.GetAlphaCutoff() > 0.f)
76 option.AddOption(ShaderOption::Type::ALPHA_TEST);
79 if(MaskMatch(materialDef.mFlags, MaterialDefinition::SUBSURFACE))
81 option.AddOption(ShaderOption::Type::SUBSURFACE);
84 if(MaskMatch(materialDef.mFlags, MaterialDefinition::OCCLUSION))
86 option.AddOption(ShaderOption::Type::OCCLUSION);
89 if(MaskMatch(materialDef.mFlags, MaterialDefinition::EMISSIVE))
91 option.AddOption(ShaderOption::Type::EMISSIVE);
94 if(MaskMatch(materialDef.mFlags, MaterialDefinition::SPECULAR))
96 option.AddOption(ShaderOption::Type::SPECULAR);
99 if(MaskMatch(materialDef.mFlags, MaterialDefinition::SPECULAR_COLOR))
101 option.AddOption(ShaderOption::Type::SPECULAR_COLOR);
104 if(MaskMatch(materialDef.mFlags, MaterialDefinition::GLTF_CHANNELS))
106 option.AddOption(ShaderOption::Type::GLTF_CHANNELS);
109 if(meshDef.IsSkinned())
111 option.AddOption(ShaderOption::Type::SKINNING);
114 if(MaskMatch(meshDef.mFlags, MeshDefinition::FLIP_UVS_VERTICAL))
116 option.AddOption(ShaderOption::Type::FLIP_UVS_VERTICAL);
119 if(meshDef.mColors.IsDefined())
121 option.AddOption(ShaderOption::Type::COLOR_ATTRIBUTE);
124 if(meshDef.mTangentType == Property::VECTOR4)
126 option.AddOption(ShaderOption::Type::VEC4_TANGENT);
129 if(meshDef.HasBlendShapes())
131 bool hasPositions = false;
132 bool hasNormals = false;
133 bool hasTangents = false;
134 meshDef.RetrieveBlendShapeComponents(hasPositions, hasNormals, hasTangents);
137 option.AddOption(ShaderOption::Type::MORPH_POSITION);
142 option.AddOption(ShaderOption::Type::MORPH_NORMAL);
147 option.AddOption(ShaderOption::Type::MORPH_TANGENT);
150 if(hasPositions || hasNormals || hasTangents)
152 if(BlendShapes::Version::VERSION_2_0 == meshDef.mBlendShapeVersion)
154 option.AddOption(ShaderOption::Type::MORPH_VERSION_2_0);
163 struct ShaderManager::Impl
165 std::map<uint64_t, Index> mShaderMap;
166 std::vector<Dali::Shader> mShaders;
167 std::vector<Scene3D::Light> mLights;
169 Scene3D::Light mShadowLight;
172 ShaderManager::ShaderManager()
177 ShaderManager::~ShaderManager() = default;
179 Dali::Shader ShaderManager::ProduceShader(const MaterialDefinition& materialDefinition, const MeshDefinition& meshDefinition)
181 ShaderOption option = MakeOption(materialDefinition, meshDefinition);
182 return ProduceShader(option);
185 Dali::Shader ShaderManager::ProduceShader(const ShaderOption& shaderOption)
189 auto& shaderMap = mImpl->mShaderMap;
190 uint64_t hash = shaderOption.GetOptionHash();
191 auto iFind = shaderMap.find(hash);
192 if(iFind != shaderMap.end())
194 result = mImpl->mShaders[iFind->second];
198 ShaderDefinition shaderDef;
199 shaderDef.mUseBuiltInShader = true;
201 shaderOption.GetDefines(shaderDef.mDefines);
202 shaderDef.mUniforms["uCubeMatrix"] = Matrix::IDENTITY;
204 shaderMap[hash] = mImpl->mShaders.size();
206 auto raw = shaderDef.LoadRaw("");
207 mImpl->mShaders.emplace_back(shaderDef.Load(std::move(raw)));
208 result = mImpl->mShaders.back();
210 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
211 result.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
213 for(uint32_t index = 0; index < mImpl->mLights.size(); ++index)
215 SetLightConstraintToShader(index, result);
218 result.RegisterProperty("uIsShadowEnabled", static_cast<int32_t>(!!mImpl->mShadowLight));
219 if(!!mImpl->mShadowLight)
221 SetShadowConstraintToShader(result);
222 SetShadowUniformToShader(result);
229 RendererState::Type ShaderManager::GetRendererState(const MaterialDefinition& materialDefinition)
231 RendererState::Type rendererState = RendererState::DEPTH_TEST;
233 if(!materialDefinition.mDoubleSided)
235 rendererState |= RendererState::CULL_BACK;
238 const bool hasTransparency = MaskMatch(materialDefinition.mFlags, MaterialDefinition::TRANSPARENCY);
241 // TODO: this requires more granularity
242 rendererState = (rendererState | RendererState::ALPHA_BLEND);
244 return rendererState;
247 bool ShaderManager::AddLight(Scene3D::Light light)
249 if(!light || mImpl->mLights.size() >= Scene3D::Internal::Light::GetMaximumEnabledLightCount())
254 uint32_t lightIndex = mImpl->mLights.size();
255 mImpl->mLights.push_back(light);
257 for(auto&& shader : mImpl->mShaders)
259 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
260 shader.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
263 SetLightConstraint(lightIndex);
268 void ShaderManager::RemoveLight(Scene3D::Light light)
270 uint32_t lightCount = mImpl->mLights.size();
271 for(uint32_t index = 0; index < lightCount; ++index)
273 if(mImpl->mLights[index] != light)
278 RemoveLightConstraint(index);
280 if(!mImpl->mLights.empty() && light != mImpl->mLights.back())
282 RemoveLightConstraint(mImpl->mLights.size() - 1);
283 mImpl->mLights[index] = mImpl->mLights.back();
284 SetLightConstraint(index);
287 mImpl->mLights.pop_back();
289 for(auto&& shader : mImpl->mShaders)
291 std::string lightCountPropertyName(Scene3D::Internal::Light::GetLightCountUniformName());
292 shader.RegisterProperty(lightCountPropertyName, static_cast<int32_t>(mImpl->mLights.size()));
298 uint32_t ShaderManager::GetLightCount() const
300 return mImpl->mLights.size();
303 void ShaderManager::SetShadow(Scene3D::Light light)
305 mImpl->mShadowLight = light;
306 for(auto&& shader : mImpl->mShaders)
308 std::string shadowEnabledPropertyName(Scene3D::Internal::Light::GetShadowEnabledUniformName());
309 shader.RegisterProperty(shadowEnabledPropertyName, static_cast<int32_t>(true));
315 void ShaderManager::RemoveShadow()
317 for(auto&& shader : mImpl->mShaders)
319 std::string shadowEnabledPropertyName(Scene3D::Internal::Light::GetShadowEnabledUniformName());
320 shader.RegisterProperty(shadowEnabledPropertyName, static_cast<int32_t>(false));
321 shader.RemoveConstraints(INDEX_FOR_SHADOW_CONSTRAINT_TAG);
323 mImpl->mShadowLight.Reset();
326 void ShaderManager::UpdateShadowUniform(Scene3D::Light light)
328 if(light != mImpl->mShadowLight)
333 for(auto&& shader : mImpl->mShaders)
335 SetShadowUniformToShader(shader);
339 void ShaderManager::SetLightConstraint(uint32_t lightIndex)
341 for(auto&& shader : mImpl->mShaders)
343 SetLightConstraintToShader(lightIndex, shader);
347 void ShaderManager::SetLightConstraintToShader(uint32_t lightIndex, Dali::Shader shader)
349 std::string lightDirectionPropertyName(Scene3D::Internal::Light::GetLightDirectionUniformName());
350 lightDirectionPropertyName += "[" + std::to_string(lightIndex) + "]";
351 auto lightDirectionPropertyIndex = shader.RegisterProperty(lightDirectionPropertyName, Vector3::ZAXIS);
352 Dali::Constraint lightDirectionConstraint = Dali::Constraint::New<Vector3>(shader, lightDirectionPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) { output = inputs[0]->GetQuaternion().Rotate(Vector3::ZAXIS); });
353 lightDirectionConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::WORLD_ORIENTATION});
354 lightDirectionConstraint.ApplyPost();
355 lightDirectionConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
357 std::string lightColorPropertyName(Scene3D::Internal::Light::GetLightColorUniformName());
358 lightColorPropertyName += "[" + std::to_string(lightIndex) + "]";
359 auto lightColorPropertyIndex = shader.RegisterProperty(lightColorPropertyName, Vector3(Color::WHITE));
360 Dali::Constraint lightColorConstraint = Dali::Constraint::New<Vector3>(shader, lightColorPropertyIndex, [](Vector3& output, const PropertyInputContainer& inputs) { output = Vector3(inputs[0]->GetVector4()); });
361 lightColorConstraint.AddSource(Source{mImpl->mLights[lightIndex], Dali::Actor::Property::COLOR});
362 lightColorConstraint.ApplyPost();
363 lightColorConstraint.SetTag(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
366 void ShaderManager::RemoveLightConstraint(uint32_t lightIndex)
368 for(auto&& shader : mImpl->mShaders)
370 shader.RemoveConstraints(INDEX_FOR_LIGHT_CONSTRAINT_TAG + lightIndex);
374 void ShaderManager::SetShadowUniformToShader(Dali::Shader shader)
376 shader.RegisterProperty("uShadowIntensity", mImpl->mShadowLight.GetShadowIntensity());
377 shader.RegisterProperty("uShadowBias", mImpl->mShadowLight.GetShadowBias());
378 shader.RegisterProperty("uEnableShadowSoftFiltering", static_cast<int>(mImpl->mShadowLight.IsShadowSoftFilteringEnabled()));
381 void ShaderManager::SetShadowProperty()
383 for(auto&& shader : mImpl->mShaders)
385 SetShadowUniformToShader(shader);
386 SetShadowConstraintToShader(shader);
390 void ShaderManager::SetShadowConstraintToShader(Dali::Shader shader)
392 // Constraint is applied before View/Projection Matrix is computed in update thread.
393 // So, it could show not plausible result if camera properties are changed discontinuesly.
394 // If we want to make it be synchronized, View/Projection matrix are needed to be conputed in below constraint.
396 std::string shadowViewProjectionPropertyName(Scene3D::Internal::Light::GetShadowViewProjectionMatrixUniformName());
397 auto shadowViewProjectionPropertyIndex = shader.RegisterProperty(shadowViewProjectionPropertyName, Matrix::IDENTITY);
398 Dali::CameraActor shadowLightCamera = Dali::Scene3D::Internal::GetImplementation(mImpl->mShadowLight).GetCamera();
399 auto tempViewProjectionMatrixIndex = shadowLightCamera.GetPropertyIndex("tempViewProjectionMatrix");
400 if(tempViewProjectionMatrixIndex != Dali::Property::INVALID_INDEX)
402 tempViewProjectionMatrixIndex = shadowLightCamera.RegisterProperty("tempViewProjectionMatrix", Matrix::IDENTITY);
404 Dali::Constraint shadowViewProjectionConstraint = Dali::Constraint::New<Matrix>(shader, shadowViewProjectionPropertyIndex, [](Matrix& output, const PropertyInputContainer& inputs)
406 output = inputs[0]->GetMatrix(); });
407 shadowViewProjectionConstraint.AddSource(Source{shadowLightCamera, tempViewProjectionMatrixIndex});
408 shadowViewProjectionConstraint.ApplyPost();
409 shadowViewProjectionConstraint.SetTag(INDEX_FOR_SHADOW_CONSTRAINT_TAG);
412 } // namespace Dali::Scene3D::Loader