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