Merge "Let Anti-Alias consider scale factor" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / model-components / model-primitive-impl.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/model-components/model-primitive-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/adaptor-framework/image-loading.h>
23 #include <dali/public-api/animation/constraint.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/public-api/object/type-registry.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-scene3d/internal/common/image-resource-loader.h>
29 #include <dali-scene3d/internal/light/light-impl.h>
30 #include <dali-scene3d/internal/model-components/material-impl.h>
31 #include <dali-scene3d/public-api/loader/environment-definition.h>
32
33 #include <dali/integration-api/debug.h>
34 #include <dali/public-api/object/property-array.h>
35 #include <dali/public-api/object/property-map.h>
36
37 #if defined(DEBUG_ENABLED)
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <filesystem>
41 namespace fs = std::filesystem;
42 #endif
43
44 namespace Dali
45 {
46 namespace Scene3D
47 {
48 namespace Internal
49 {
50 namespace
51 {
52 #if defined(DEBUG_ENABLED)
53 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE3D_MODEL_PRIMITIVE");
54
55 std::string tmpFilename(std::string prefix, std::string suffix)
56 {
57   static int id = 0;
58   id++;
59   std::ostringstream oss;
60   oss << prefix << getpid() << "_" << std::setfill('0') << std::setw(4) << id << suffix;
61   return oss.str();
62 }
63
64 #define DALI_LOG_WRITE_FILE(filename, stream) \
65   {                                           \
66     fs::path tmp = fs::temp_directory_path(); \
67     tmp /= filename;                          \
68     std::ofstream ostrm(tmp, std::ios::out);  \
69     ostrm << stream;                          \
70     ostrm.flush();                            \
71   }
72
73 inline Property::Map GetMap(Shader shader)
74 {
75   Property::Value program = shader[Shader::Property::PROGRAM];
76   Property::Map*  map{nullptr};
77   if(program.GetType() == Property::ARRAY)
78   {
79     Property::Array* array = program.GetArray();
80     if(array)
81     {
82       Property::Value& value = array->GetElementAt(0);
83       if(value.GetType() == Property::MAP)
84       {
85         map = value.GetMap();
86       }
87     }
88   }
89   else if(program.GetType() == Property::MAP)
90   {
91     map = program.GetMap();
92   }
93   if(map)
94   {
95     return *map;
96   }
97   return Property::Map();
98 }
99
100 #endif
101 /**
102  * Creates control through type registry
103  */
104 BaseHandle Create()
105 {
106   return Scene3D::ModelPrimitive::New();
107 }
108
109 // Setup properties, signals and actions using the type-registry.
110 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::ModelPrimitive, Dali::BaseHandle, Create);
111 DALI_TYPE_REGISTRATION_END()
112
113 static constexpr uint32_t INDEX_FOR_LIGHT_CONSTRAINT_TAG = 10;
114
115 } // unnamed namespace
116
117 ModelPrimitivePtr ModelPrimitive::New()
118 {
119   ModelPrimitivePtr primitive = new ModelPrimitive();
120
121   primitive->Initialize();
122
123   return primitive;
124 }
125
126 ModelPrimitive::ModelPrimitive()
127 : mShaderManager(new Scene3D::Loader::ShaderManager())
128 {
129 }
130
131 ModelPrimitive::~ModelPrimitive()
132 {
133   if(mMaterial)
134   {
135     GetImplementation(mMaterial).RemoveObserver(this);
136   }
137   mMaterial.Reset();
138 }
139
140 void ModelPrimitive::Initialize()
141 {
142 }
143
144 void ModelPrimitive::SetRenderer(Dali::Renderer renderer)
145 {
146   mRenderer   = renderer;
147   mGeometry   = renderer.GetGeometry();
148   mTextureSet = renderer.GetTextures();
149   mShader     = renderer.GetShader();
150 }
151
152 Dali::Renderer ModelPrimitive::GetRenderer() const
153 {
154   return mRenderer;
155 }
156
157 void ModelPrimitive::SetGeometry(Dali::Geometry geometry)
158 {
159   mGeometry = geometry;
160   CreateRenderer();
161 }
162
163 Dali::Geometry ModelPrimitive::GetGeometry() const
164 {
165   return mGeometry;
166 }
167
168 void ModelPrimitive::SetMaterial(Dali::Scene3D::Material material, bool updateRenderer)
169 {
170   if(!material)
171   {
172     return;
173   }
174
175   if(mMaterial != material)
176   {
177     // Stop observe from previous material.
178     if(mMaterial)
179     {
180       GetImplementation(mMaterial).RemoveObserver(this);
181     }
182
183     mMaterial = material;
184
185     // Start observe from new material.
186
187     if(mMaterial)
188     {
189       GetImplementation(mMaterial).AddObserver(this);
190     }
191
192     if(updateRenderer)
193     {
194       mIsMaterialChanged = true;
195       if(GetImplementation(mMaterial).IsResourceReady())
196       {
197         GetImplementation(mMaterial).UpdateMaterialData();
198         ApplyMaterialToRenderer();
199       }
200     }
201     UpdateShadowMapTexture();
202     UpdateImageBasedLightTexture();
203   }
204 }
205
206 Dali::Scene3D::Material ModelPrimitive::GetMaterial() const
207 {
208   return mMaterial;
209 }
210
211 void ModelPrimitive::AddPrimitiveObserver(ModelPrimitiveModifyObserver* observer)
212 {
213   mObservers.insert(observer);
214 }
215
216 void ModelPrimitive::RemovePrimitiveObserver(ModelPrimitiveModifyObserver* observer)
217 {
218   mObservers.erase(observer);
219 }
220
221 void ModelPrimitive::SetShadowMapTexture(Dali::Texture shadowMapTexture)
222 {
223   mShadowMapTexture = shadowMapTexture;
224   UpdateShadowMapTexture();
225 }
226
227 void ModelPrimitive::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
228 {
229   mDiffuseTexture       = diffuseTexture;
230   mSpecularTexture      = specularTexture;
231   mIblScaleFactor       = iblScaleFactor;
232   mSpecularMipmapLevels = specularMipmapLevels;
233
234   UpdateImageBasedLightTexture();
235 }
236
237 void ModelPrimitive::SetImageBasedLightScaleFactor(float iblScaleFactor)
238 {
239   mIblScaleFactor = iblScaleFactor;
240   if(mRenderer && mMaterial)
241   {
242     mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), iblScaleFactor);
243   }
244 }
245
246 void ModelPrimitive::UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager, Loader::ShaderOption::HashType hash)
247 {
248   if(mShaderManager != shaderManager)
249   {
250     mShaderManager = (shaderManager) ? shaderManager : new Scene3D::Loader::ShaderManager();
251     if(mMaterial && GetImplementation(mMaterial).IsResourceReady())
252     {
253       ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag::SHADER, hash);
254     }
255   }
256 }
257
258 void ModelPrimitive::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data)
259 {
260   mBlendShapeData = std::move(data);
261   Scene3D::Loader::BlendShapes::ConfigureProperties(mBlendShapeData, mRenderer);
262 }
263
264 void ModelPrimitive::SetBlendShapeGeometry(Dali::Texture blendShapeGeometry)
265 {
266   mBlendShapeGeometry = blendShapeGeometry;
267 }
268
269 void ModelPrimitive::SetBlendShapeOptions(bool hasPositions, bool hasNormals, bool hasTangents, Scene3D::Loader::BlendShapes::Version version)
270 {
271   mHasPositions      = hasPositions;
272   mHasNormals        = hasNormals;
273   mHasTangents       = hasTangents;
274   mBlendShapeVersion = version;
275 }
276
277 void ModelPrimitive::SetSkinned(bool isSkinned, uint32_t numberOfJointSets)
278 {
279   mHasSkinning       = isSkinned;
280   mNumberOfJointSets = numberOfJointSets;
281 }
282
283 void ModelPrimitive::SetVertexColor(bool hasVertexColor)
284 {
285   mHasVertexColor = hasVertexColor;
286 }
287
288 // From MaterialModifyObserver
289
290 void ModelPrimitive::OnMaterialModified(Dali::Scene3D::Material material, MaterialModifyObserver::ModifyFlag flag)
291 {
292   ApplyMaterialToRenderer(flag);
293 }
294
295 void ModelPrimitive::ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag flag, Loader::ShaderOption::HashType oldHash)
296 {
297   if(!mMaterial)
298   {
299     return;
300   }
301
302   uint32_t shaderFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER));
303   if(mIsMaterialChanged || shaderFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::SHADER))
304   {
305     Scene3D::Loader::ShaderOption shaderOption = GetImplementation(mMaterial).GetShaderOption();
306
307     shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::VEC4_TANGENT);
308     if(mHasSkinning)
309     {
310       shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::SKINNING);
311       shaderOption.AddJointMacros(mNumberOfJointSets);
312     }
313     if(mHasVertexColor)
314     {
315       shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::COLOR_ATTRIBUTE);
316     }
317     if(mHasPositions || mHasNormals || mHasTangents)
318     {
319       if(mHasPositions)
320       {
321         shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_POSITION);
322       }
323       if(mHasNormals)
324       {
325         shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_NORMAL);
326       }
327       if(mHasTangents)
328       {
329         shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_TANGENT);
330       }
331       if(mBlendShapeVersion == Scene3D::Loader::BlendShapes::Version::VERSION_2_0)
332       {
333         shaderOption.AddOption(Scene3D::Loader::ShaderOption::Type::MORPH_VERSION_2_0);
334       }
335     }
336
337     Shader newShader = mShaderManager->ProduceShader(shaderOption);
338     if(mShader != newShader)
339     {
340       DALI_LOG_INFO(gLogFilter, Debug::General, "Warning!  Model primitive shader changed: OldHash:%x NewHash:%x\n", oldHash, shaderOption.GetOptionHash());
341
342 #if defined(DEBUG_ENABLED)
343       if(mShader)
344       {
345         Property::Map oldMap = GetMap(mShader);
346         DALI_LOG_WRITE_FILE(tmpFilename("oldShader", ".txt"), "Vertex Shader:\n"
347                             << oldMap["vertex"] << "\n\nFragmentShader: " << oldMap["fragment"] << "\n");
348       }
349       if(newShader)
350       {
351         Property::Map newMap = GetMap(newShader);
352         DALI_LOG_WRITE_FILE(tmpFilename("newShader", ".txt"), "Vertex Shader:\n"
353                             << newMap["vertex"] << "\n\nFragmentShader: " << newMap["fragment"] << "\n");
354       }
355 #endif
356     }
357     mShader = newShader;
358
359     if(!mRenderer)
360     {
361       CreateRenderer();
362     }
363     else
364     {
365       mRenderer.SetShader(mShader);
366     }
367   }
368
369   uint32_t textureFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE));
370   if(mIsMaterialChanged || textureFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::TEXTURE))
371   {
372     mTextureSet = GetImplementation(mMaterial).GetTextureSet();
373
374     if(mBlendShapeGeometry)
375     {
376       TextureSet newTextureSet = TextureSet::New();
377       newTextureSet.SetTexture(0u, mBlendShapeGeometry);
378
379       const unsigned int numberOfTextures = mTextureSet.GetTextureCount();
380       for(unsigned int index = 0u; index < numberOfTextures; ++index)
381       {
382         const unsigned int newIndex = index + 1u;
383         newTextureSet.SetTexture(newIndex, mTextureSet.GetTexture(index));
384         newTextureSet.SetSampler(newIndex, mTextureSet.GetSampler(index));
385       }
386
387       mTextureSet = newTextureSet;
388     }
389
390     uint32_t textureCount = mTextureSet.GetTextureCount();
391
392     if(!mShadowMapTexture)
393     {
394       mShadowMapTexture = Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
395     }
396     mTextureSet.SetTexture(textureCount++, mShadowMapTexture);
397
398     Texture brdfTexture = Scene3D::Loader::EnvironmentDefinition::GetBrdfTexture();
399     if(!mSpecularTexture || !mDiffuseTexture)
400     {
401       Scene3D::Loader::EnvironmentMapData environmentMapData;
402       environmentMapData.mPixelData.resize(6);
403       for(auto& face : environmentMapData.mPixelData)
404       {
405         face.push_back(PixelData::New(new uint8_t[3]{0xff, 0xff, 0xff}, 3, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY));
406       }
407       environmentMapData.SetEnvironmentMapType(Dali::Scene3D::EnvironmentMapType::CUBEMAP);
408       Texture iblTexture = environmentMapData.GetTexture();
409       mDiffuseTexture    = iblTexture;
410       mSpecularTexture   = iblTexture;
411     }
412
413     mTextureSet.SetTexture(textureCount++, brdfTexture);
414     mTextureSet.SetTexture(textureCount++, mDiffuseTexture);
415     mTextureSet.SetTexture(textureCount, mSpecularTexture);
416
417     auto specularSampler = Sampler::New();
418     specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
419     specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
420     mTextureSet.SetSampler(textureCount, specularSampler);
421
422     if(!mRenderer)
423     {
424       CreateRenderer();
425     }
426     else
427     {
428       mRenderer.SetTextures(mTextureSet);
429     }
430   }
431
432   uint32_t uniformFlag = (flag & static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM));
433   if(mIsMaterialChanged || uniformFlag == static_cast<uint32_t>(MaterialModifyObserver::ModifyFlag::UNIFORM))
434   {
435     if(!mRenderer)
436     {
437       mNeedToSetRendererUniform = true;
438     }
439     else
440     {
441       UpdateRendererUniform();
442     }
443   }
444   mIsMaterialChanged = false;
445 }
446
447 void ModelPrimitive::CreateRenderer()
448 {
449   if(!mShader || !mGeometry || !mTextureSet || mRenderer)
450   {
451     return;
452   }
453
454   mRenderer = Renderer::New(mGeometry, mShader);
455   mRenderer.SetTextures(mTextureSet);
456   UpdateRendererUniform();
457
458   for(auto* observer : mObservers)
459   {
460     observer->OnRendererCreated(mRenderer);
461   }
462 }
463
464 void ModelPrimitive::UpdateShadowMapTexture()
465 {
466   if(mRenderer && mMaterial)
467   {
468     Dali::TextureSet textures = mRenderer.GetTextures();
469     if(!textures)
470     {
471       return;
472     }
473
474     uint32_t textureCount = textures.GetTextureCount();
475     if(mShadowMapTexture &&
476        textureCount >= GetImplementation(mMaterial).GetShadowMapTextureOffset() &&
477        textures.GetTexture(textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset()) != mShadowMapTexture)
478     {
479       Dali::TextureSet newTextures = Dali::TextureSet::New();
480
481       for(uint32_t index = 0u; index < textureCount; ++index)
482       {
483         Dali::Texture texture = textures.GetTexture(index);
484         if(index == textureCount - GetImplementation(mMaterial).GetShadowMapTextureOffset())
485         {
486           texture = (!!mShadowMapTexture) ? mShadowMapTexture : Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB();
487         }
488
489         newTextures.SetTexture(index, texture);
490         newTextures.SetSampler(index, textures.GetSampler(index));
491       }
492
493       mRenderer.SetTextures(newTextures);
494     }
495   }
496 }
497
498 void ModelPrimitive::UpdateImageBasedLightTexture()
499 {
500   if(mRenderer && mMaterial)
501   {
502     Dali::TextureSet textures = mRenderer.GetTextures();
503     if(!textures)
504     {
505       return;
506     }
507
508     uint32_t textureCount = textures.GetTextureCount();
509     if(textureCount > 2u &&
510        (textures.GetTexture(textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset()) != mDiffuseTexture ||
511         textures.GetTexture(textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset()) != mSpecularTexture))
512     {
513       Dali::TextureSet newTextures = Dali::TextureSet::New();
514
515       for(uint32_t index = 0u; index < textureCount; ++index)
516       {
517         Dali::Texture texture = textures.GetTexture(index);
518         if(index == textureCount - GetImplementation(mMaterial).GetDiffuseImageBasedLightTextureOffset())
519         {
520           texture = mDiffuseTexture;
521         }
522         else if(index == textureCount - GetImplementation(mMaterial).GetSpecularImageBasedLightTextureOffset())
523         {
524           texture = mSpecularTexture;
525         }
526
527         newTextures.SetTexture(index, texture);
528         newTextures.SetSampler(index, textures.GetSampler(index));
529       }
530
531       mRenderer.SetTextures(newTextures);
532     }
533     mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
534     mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
535   }
536 }
537
538 void ModelPrimitive::UpdateRendererUniform()
539 {
540   if(mMaterial)
541   {
542     mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightScaleFactorName().data(), mIblScaleFactor);
543     mRenderer.RegisterProperty(GetImplementation(mMaterial).GetImageBasedLightMaxLodUniformName().data(), static_cast<float>(mSpecularMipmapLevels));
544     GetImplementation(mMaterial).SetRendererUniform(mRenderer);
545   }
546 }
547
548 } // namespace Internal
549
550 } // namespace Scene3D
551
552 } // namespace Dali