Texture size reduction on the fly for 3D model using metadata
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / material-definition.cpp
1 /*
2  * Copyright (c) 2022 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 // INTERNAL INCLUDES
19 #include "dali-scene3d/public-api/loader/material-definition.h"
20
21 // EXTERNAL INCLUDES
22 #include "dali-toolkit/public-api/image-loader/sync-image-loader.h"
23
24 namespace Dali
25 {
26 using namespace Toolkit;
27
28 namespace Scene3D
29 {
30 namespace Loader
31 {
32 namespace
33 {
34 constexpr SamplerFlags::Type FILTER_MODES_FROM_DALI[]{
35   SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_NEAREST,
36   SamplerFlags::FILTER_LINEAR,
37   SamplerFlags::FILTER_NEAREST,
38   SamplerFlags::FILTER_LINEAR,
39   SamplerFlags::FILTER_NEAREST | SamplerFlags::FILTER_MIPMAP_NEAREST,
40   SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_NEAREST,
41   SamplerFlags::FILTER_NEAREST | SamplerFlags::FILTER_MIPMAP_LINEAR,
42   SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_LINEAR,
43 };
44
45 constexpr SamplerFlags::Type WRAP_MODES_FROM_DALI[]{
46   SamplerFlags::WRAP_CLAMP,
47   SamplerFlags::WRAP_CLAMP,
48   SamplerFlags::WRAP_REPEAT,
49   SamplerFlags::WRAP_MIRROR,
50 };
51
52 constexpr FilterMode::Type FILTER_MODES_TO_DALI[]{
53   FilterMode::NEAREST,
54   FilterMode::LINEAR,
55   FilterMode::NEAREST_MIPMAP_NEAREST,
56   FilterMode::LINEAR_MIPMAP_NEAREST,
57   FilterMode::NEAREST_MIPMAP_LINEAR,
58   FilterMode::LINEAR_MIPMAP_LINEAR,
59 };
60
61 constexpr WrapMode::Type WRAP_MODES_TO_DALI[]{
62   WrapMode::REPEAT,
63   WrapMode::CLAMP_TO_EDGE,
64   WrapMode::MIRRORED_REPEAT};
65
66 const SamplerFlags::Type SINGLE_VALUE_SAMPLER = SamplerFlags::Encode(FilterMode::NEAREST, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
67 } // namespace
68
69 SamplerFlags::Type SamplerFlags::Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT)
70 {
71   return FILTER_MODES_FROM_DALI[minFilter] | ((FILTER_MODES_FROM_DALI[magFilter] & FILTER_MAG_BITS) << FILTER_MAG_SHIFT) |
72          (WRAP_MODES_FROM_DALI[wrapS] << WRAP_S_SHIFT) | (WRAP_MODES_FROM_DALI[wrapT] << WRAP_T_SHIFT);
73 }
74
75 FilterMode::Type SamplerFlags::GetMinFilter(Type flags)
76 {
77   return FILTER_MODES_TO_DALI[flags & FILTER_MIN_MASK];
78 }
79
80 FilterMode::Type SamplerFlags::GetMagFilter(Type flags)
81 {
82   return FILTER_MODES_TO_DALI[(flags >> FILTER_MAG_SHIFT) & FILTER_MAG_MASK];
83 }
84
85 WrapMode::Type SamplerFlags::GetWrapS(Type flags)
86 {
87   return WRAP_MODES_TO_DALI[(flags >> WRAP_S_SHIFT) & WRAP_S_MASK];
88 }
89
90 WrapMode::Type SamplerFlags::GetWrapT(Type flags)
91 {
92   return WRAP_MODES_TO_DALI[(flags >> WRAP_T_SHIFT) & WRAP_T_MASK];
93 }
94
95 Sampler SamplerFlags::MakeSampler(Type flags)
96 {
97   auto sampler = Sampler::New();
98   sampler.SetFilterMode(GetMinFilter(flags), GetMagFilter(flags));
99   sampler.SetWrapMode(GetWrapS(flags), GetWrapT(flags));
100   return sampler;
101 }
102
103 TextureDefinition::TextureDefinition(const std::string& imageUri, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode)
104 : mImageUri(imageUri),
105   mSamplerFlags(samplerFlags),
106   mMinImageDimensions(minImageDimensions),
107   mSamplingMode(samplingMode)
108 {
109 }
110
111 MaterialDefinition::RawData
112 MaterialDefinition::LoadRaw(const std::string& imagesPath) const
113 {
114   RawData raw;
115
116   const bool hasTransparency = MaskMatch(mFlags, TRANSPARENCY);
117   // Why we add additional count here?
118   uint32_t numBuffers = static_cast<uint32_t>(mTextureStages.size()) + (hasTransparency ? !CheckTextures(ALBEDO) + !CheckTextures(METALLIC | ROUGHNESS) + !CheckTextures(NORMAL)
119                                                                                         : !CheckTextures(ALBEDO | METALLIC) + !CheckTextures(NORMAL | ROUGHNESS));
120   if(numBuffers == 0)
121   {
122     return raw;
123   }
124   raw.mTextures.reserve(numBuffers);
125
126   // Load textures
127   auto iTexture   = mTextureStages.cbegin();
128   auto checkStage = [&](uint32_t flags) {
129     return iTexture != mTextureStages.end() && MaskMatch(iTexture->mSemantic, flags);
130   };
131
132   // Check for compulsory textures: Albedo, Metallic, Roughness, Normal
133   if(checkStage(ALBEDO | METALLIC))
134   {
135     raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
136     ++iTexture;
137
138     if(checkStage(NORMAL | ROUGHNESS))
139     {
140       raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
141       ++iTexture;
142     }
143     else // single value normal-roughness
144     {
145       const auto bufferSize = 4;
146       uint8_t*   buffer     = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1
147       raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
148     }
149   }
150   else
151   {
152     if(checkStage(ALBEDO))
153     {
154       raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
155       ++iTexture;
156     }
157     else if(mNeedAlbedoTexture) // single value albedo, albedo-alpha or albedo-metallic
158     {
159       uint32_t bufferSize = 4;
160       uint8_t* buffer     = nullptr;
161       auto     format     = Pixel::Format::RGBA8888;
162       if(hasTransparency) // albedo-alpha
163       {
164         buffer    = new uint8_t[bufferSize];
165         buffer[3] = static_cast<uint8_t>(mColor.a * 255.f);
166       }
167       else if(!checkStage(METALLIC | ROUGHNESS)) // albedo-metallic
168       {
169         buffer    = new uint8_t[bufferSize];
170         buffer[3] = 0xff; // metallic of 1.0
171       }
172       else // albedo
173       {
174         bufferSize = 3;
175         buffer     = new uint8_t[bufferSize];
176         format     = Pixel::Format::RGB888;
177       }
178       buffer[0] = static_cast<uint8_t>(mColor.r * 255.f);
179       buffer[1] = static_cast<uint8_t>(mColor.g * 255.f);
180       buffer[2] = static_cast<uint8_t>(mColor.b * 255.f);
181       raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, format, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
182     }
183
184     // If we have transparency, or an image based albedo map, we will have to continue with separate metallicRoughness + normal.
185     const bool createMetallicRoughnessAndNormal = hasTransparency || std::distance(mTextureStages.begin(), iTexture) > 0;
186     if(checkStage(METALLIC | ROUGHNESS))
187     {
188       raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
189       ++iTexture;
190     }
191     else if(createMetallicRoughnessAndNormal && mNeedMetallicRoughnessTexture)
192     {
193       // NOTE: we want to set both metallic and roughness to 1.0; dli uses the R & A channels,
194       // glTF2 uses B & G, so we might as well just set all components to 1.0.
195       const auto bufferSize = 4;
196       uint8_t*   buffer     = new uint8_t[bufferSize]{0xff, 0xff, 0xff, 0xff};
197       raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
198     }
199
200     if(checkStage(NORMAL))
201     {
202       raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
203       ++iTexture;
204     }
205     else if(mNeedNormalTexture)
206     {
207       if(createMetallicRoughnessAndNormal)
208       {
209         const auto bufferSize = 3;
210         uint8_t*   buffer     = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff}; // normal of (0, 0, 1)
211         raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
212       }
213       else // single-value normal-roughness
214       {
215         const auto bufferSize = 4;
216         uint8_t*   buffer     = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1.0
217         raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
218       }
219     }
220   }
221
222   // Extra textures.
223   if(checkStage(SUBSURFACE))
224   {
225     raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
226     ++iTexture;
227   }
228
229   if(checkStage(OCCLUSION))
230   {
231     raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
232     ++iTexture;
233   }
234
235   if(checkStage(EMISSIVE))
236   {
237     raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
238     ++iTexture;
239   }
240
241   if(checkStage(SPECULAR))
242   {
243     raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
244     ++iTexture;
245   }
246
247   if(checkStage(SPECULAR_COLOR))
248   {
249     raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri, iTexture->mTexture.mMinImageDimensions, FittingMode::DEFAULT, iTexture->mTexture.mSamplingMode, true), iTexture->mTexture.mSamplerFlags});
250     ++iTexture;
251   }
252
253   return raw;
254 }
255
256 TextureSet MaterialDefinition::Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const
257 {
258   auto textureSet = TextureSet::New();
259
260   uint32_t n = 0;
261   for(auto& tData : raw.mTextures)
262   {
263     auto& pixels  = tData.mPixels;
264     auto  texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
265     texture.Upload(tData.mPixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
266     if(tData.mSamplerFlags & SamplerFlags::MIPMAP_MASK)
267     {
268       texture.GenerateMipmaps();
269     }
270
271     textureSet.SetTexture(n, texture);
272     textureSet.SetSampler(n, SamplerFlags::MakeSampler(tData.mSamplerFlags));
273
274     ++n;
275   }
276
277   // Assign textures to slots -- starting with 2D ones, then cubemaps, if any.
278   if(mEnvironmentIdx < static_cast<Index>(environments.size()))
279   {
280     auto& envTextures = environments[mEnvironmentIdx].second;
281     // If pre-computed brdf texture is defined, set the texture.
282     if(envTextures.mBrdf)
283     {
284       textureSet.SetTexture(n, envTextures.mBrdf);
285       ++n;
286     }
287
288     if(envTextures.mDiffuse)
289     {
290       textureSet.SetTexture(n, envTextures.mDiffuse);
291       ++n;
292     }
293
294     if(envTextures.mSpecular)
295     {
296       auto specularSampler = Sampler::New();
297       specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
298       specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
299
300       textureSet.SetTexture(n, envTextures.mSpecular);
301       textureSet.SetSampler(n, specularSampler);
302       ++n;
303     }
304   }
305   else
306   {
307     ExceptionFlinger(ASSERT_LOCATION) << "Environment index (" << mEnvironmentIdx << ") out of bounds (" << environments.size() << ").";
308   }
309
310   return textureSet;
311 }
312
313 bool MaterialDefinition::CheckTextures(uint32_t flags) const
314 {
315   return std::find_if(mTextureStages.begin(), mTextureStages.end(), [flags](const TextureStage& ts) {
316            return MaskMatch(ts.mSemantic, flags);
317          }) != mTextureStages.end();
318 }
319
320 } // namespace Loader
321 } // namespace Scene3D
322 } // namespace Dali