2 * Copyright (c) 2022 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/material-definition.h>
22 #include <dali-toolkit/devel-api/builder/base64-encoding.h>
23 #include <dali-toolkit/public-api/image-loader/sync-image-loader.h>
24 #include <dali/devel-api/adaptor-framework/image-loading.h>
28 using namespace Toolkit;
36 constexpr SamplerFlags::Type FILTER_MODES_FROM_DALI[]{
37 SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_NEAREST,
38 SamplerFlags::FILTER_LINEAR,
39 SamplerFlags::FILTER_NEAREST,
40 SamplerFlags::FILTER_LINEAR,
41 SamplerFlags::FILTER_NEAREST | SamplerFlags::FILTER_MIPMAP_NEAREST,
42 SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_NEAREST,
43 SamplerFlags::FILTER_NEAREST | SamplerFlags::FILTER_MIPMAP_LINEAR,
44 SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_LINEAR,
47 constexpr SamplerFlags::Type WRAP_MODES_FROM_DALI[]{
48 SamplerFlags::WRAP_CLAMP,
49 SamplerFlags::WRAP_CLAMP,
50 SamplerFlags::WRAP_REPEAT,
51 SamplerFlags::WRAP_MIRROR,
54 constexpr FilterMode::Type FILTER_MODES_TO_DALI[]{
57 FilterMode::NEAREST_MIPMAP_NEAREST,
58 FilterMode::LINEAR_MIPMAP_NEAREST,
59 FilterMode::NEAREST_MIPMAP_LINEAR,
60 FilterMode::LINEAR_MIPMAP_LINEAR,
63 constexpr WrapMode::Type WRAP_MODES_TO_DALI[]{
65 WrapMode::CLAMP_TO_EDGE,
66 WrapMode::MIRRORED_REPEAT};
68 const SamplerFlags::Type SINGLE_VALUE_SAMPLER = SamplerFlags::Encode(FilterMode::NEAREST, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
70 static constexpr std::string_view EMBEDDED_DATA_PREFIX = "data:";
71 static constexpr std::string_view EMBEDDED_DATA_IMAGE_MEDIA_TYPE = "image/";
72 static constexpr std::string_view EMBEDDED_DATA_BASE64_ENCODING_TYPE = "base64,";
74 Dali::PixelData LoadImageResource(const std::string& resourcePath,
75 TextureDefinition& textureDefinition,
76 FittingMode::Type fittingMode,
77 bool orientationCorrection)
79 Dali::PixelData pixelData;
80 if(!textureDefinition.mTextureBuffer.empty())
82 Dali::Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromBuffer(textureDefinition.mTextureBuffer.data(), textureDefinition.mTextureBuffer.size(), textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
85 pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
88 else if(textureDefinition.mImageUri.find(EMBEDDED_DATA_PREFIX.data()) == 0 && textureDefinition.mImageUri.find(EMBEDDED_DATA_IMAGE_MEDIA_TYPE.data(), EMBEDDED_DATA_PREFIX.length()) == EMBEDDED_DATA_PREFIX.length())
90 uint32_t position = textureDefinition.mImageUri.find(EMBEDDED_DATA_BASE64_ENCODING_TYPE.data(), EMBEDDED_DATA_PREFIX.length() + EMBEDDED_DATA_IMAGE_MEDIA_TYPE.length());
91 if(position != std::string::npos)
93 position += EMBEDDED_DATA_BASE64_ENCODING_TYPE.length();
94 std::string data = textureDefinition.mImageUri.substr(position);
95 std::vector<uint8_t> buffer;
96 Dali::Toolkit::DecodeBase64PropertyData(data, buffer);
97 uint32_t bufferSize = buffer.size();
99 Dali::Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromBuffer(reinterpret_cast<uint8_t*>(buffer.data()), bufferSize, textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
102 pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
108 pixelData = SyncImageLoader::Load(resourcePath + textureDefinition.mImageUri, textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
114 SamplerFlags::Type SamplerFlags::Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT)
116 return FILTER_MODES_FROM_DALI[minFilter] | ((FILTER_MODES_FROM_DALI[magFilter] & FILTER_MAG_BITS) << FILTER_MAG_SHIFT) |
117 (WRAP_MODES_FROM_DALI[wrapS] << WRAP_S_SHIFT) | (WRAP_MODES_FROM_DALI[wrapT] << WRAP_T_SHIFT);
120 FilterMode::Type SamplerFlags::GetMinFilter(Type flags)
122 return FILTER_MODES_TO_DALI[flags & FILTER_MIN_MASK];
125 FilterMode::Type SamplerFlags::GetMagFilter(Type flags)
127 return FILTER_MODES_TO_DALI[(flags >> FILTER_MAG_SHIFT) & FILTER_MAG_MASK];
130 WrapMode::Type SamplerFlags::GetWrapS(Type flags)
132 return WRAP_MODES_TO_DALI[(flags >> WRAP_S_SHIFT) & WRAP_S_MASK];
135 WrapMode::Type SamplerFlags::GetWrapT(Type flags)
137 return WRAP_MODES_TO_DALI[(flags >> WRAP_T_SHIFT) & WRAP_T_MASK];
140 Sampler SamplerFlags::MakeSampler(Type flags)
142 auto sampler = Sampler::New();
143 sampler.SetFilterMode(GetMinFilter(flags), GetMagFilter(flags));
144 sampler.SetWrapMode(GetWrapS(flags), GetWrapT(flags));
148 TextureDefinition::TextureDefinition(const std::string& imageUri, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode)
149 : mImageUri(imageUri),
150 mSamplerFlags(samplerFlags),
151 mMinImageDimensions(minImageDimensions),
152 mSamplingMode(samplingMode)
156 TextureDefinition::TextureDefinition(std::string&& imageUri, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode)
157 : mImageUri(std::move(imageUri)),
158 mSamplerFlags(samplerFlags),
159 mMinImageDimensions(minImageDimensions),
160 mSamplingMode(samplingMode)
164 TextureDefinition::TextureDefinition(std::vector<uint8_t>&& textureBuffer, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode)
166 mSamplerFlags(samplerFlags),
167 mMinImageDimensions(minImageDimensions),
168 mSamplingMode(samplingMode),
169 mTextureBuffer(std::move(textureBuffer))
173 MaterialDefinition::RawData
174 MaterialDefinition::LoadRaw(const std::string& imagesPath)
178 const bool hasTransparency = MaskMatch(mFlags, TRANSPARENCY);
179 // Why we add additional count here?
180 uint32_t numBuffers = static_cast<uint32_t>(mTextureStages.size()) + (hasTransparency ? !CheckTextures(ALBEDO) + !CheckTextures(METALLIC | ROUGHNESS) + !CheckTextures(NORMAL)
181 : !CheckTextures(ALBEDO | METALLIC) + !CheckTextures(NORMAL | ROUGHNESS));
186 raw.mTextures.reserve(numBuffers);
189 auto iTexture = mTextureStages.begin();
190 auto checkStage = [&](uint32_t flags)
192 return iTexture != mTextureStages.end() && MaskMatch(iTexture->mSemantic, flags);
195 // Check for compulsory textures: Albedo, Metallic, Roughness, Normal
196 if(checkStage(ALBEDO | METALLIC))
198 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
201 if(checkStage(NORMAL | ROUGHNESS))
203 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
206 else // single value normal-roughness
208 const auto bufferSize = 4;
209 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1
210 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
215 if(checkStage(ALBEDO))
217 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
220 else if(mNeedAlbedoTexture) // single value albedo, albedo-alpha or albedo-metallic
222 uint32_t bufferSize = 4;
223 uint8_t* buffer = nullptr;
224 auto format = Pixel::Format::RGBA8888;
225 if(hasTransparency) // albedo-alpha
227 buffer = new uint8_t[bufferSize];
228 buffer[3] = static_cast<uint8_t>(mColor.a * 255.f);
230 else if(!checkStage(METALLIC | ROUGHNESS)) // albedo-metallic
232 buffer = new uint8_t[bufferSize];
233 buffer[3] = 0xff; // metallic of 1.0
238 buffer = new uint8_t[bufferSize];
239 format = Pixel::Format::RGB888;
241 buffer[0] = static_cast<uint8_t>(mColor.r * 255.f);
242 buffer[1] = static_cast<uint8_t>(mColor.g * 255.f);
243 buffer[2] = static_cast<uint8_t>(mColor.b * 255.f);
244 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, format, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
247 // If we have transparency, or an image based albedo map, we will have to continue with separate metallicRoughness + normal.
248 const bool createMetallicRoughnessAndNormal = hasTransparency || std::distance(mTextureStages.begin(), iTexture) > 0;
249 if(checkStage(METALLIC | ROUGHNESS))
251 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
254 else if(createMetallicRoughnessAndNormal && mNeedMetallicRoughnessTexture)
256 // NOTE: we want to set both metallic and roughness to 1.0; dli uses the R & A channels,
257 // glTF2 uses B & G, so we might as well just set all components to 1.0.
258 const auto bufferSize = 4;
259 uint8_t* buffer = new uint8_t[bufferSize]{0xff, 0xff, 0xff, 0xff};
260 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
263 if(checkStage(NORMAL))
265 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
268 else if(mNeedNormalTexture)
270 if(createMetallicRoughnessAndNormal)
272 const auto bufferSize = 3;
273 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff}; // normal of (0, 0, 1)
274 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
276 else // single-value normal-roughness
278 const auto bufferSize = 4;
279 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1.0
280 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
286 if(checkStage(SUBSURFACE))
288 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
292 if(checkStage(OCCLUSION))
294 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
298 if(checkStage(EMISSIVE))
300 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
304 if(checkStage(SPECULAR))
306 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
310 if(checkStage(SPECULAR_COLOR))
312 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
319 TextureSet MaterialDefinition::Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const
321 auto textureSet = TextureSet::New();
324 for(auto& tData : raw.mTextures)
326 auto& pixels = tData.mPixels;
330 texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
331 texture.Upload(tData.mPixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
332 if(tData.mSamplerFlags & SamplerFlags::MIPMAP_MASK)
334 texture.GenerateMipmaps();
338 textureSet.SetTexture(n, texture);
339 textureSet.SetSampler(n, SamplerFlags::MakeSampler(tData.mSamplerFlags));
344 // Assign textures to slots -- starting with 2D ones, then cubemaps, if any.
345 if(mEnvironmentIdx < static_cast<Index>(environments.size()))
347 auto& envTextures = environments[mEnvironmentIdx].second;
348 // If pre-computed brdf texture is defined, set the texture.
349 if(envTextures.mBrdf)
351 textureSet.SetTexture(n, envTextures.mBrdf);
355 if(envTextures.mDiffuse)
357 textureSet.SetTexture(n, envTextures.mDiffuse);
361 if(envTextures.mSpecular)
363 auto specularSampler = Sampler::New();
364 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
365 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
367 textureSet.SetTexture(n, envTextures.mSpecular);
368 textureSet.SetSampler(n, specularSampler);
374 ExceptionFlinger(ASSERT_LOCATION) << "Environment index (" << mEnvironmentIdx << ") out of bounds (" << environments.size() << ").";
380 bool MaterialDefinition::CheckTextures(uint32_t flags) const
382 return std::find_if(mTextureStages.begin(), mTextureStages.end(), [flags](const TextureStage& ts)
383 { return MaskMatch(ts.mSemantic, flags); }) != mTextureStages.end();
386 } // namespace Loader
387 } // namespace Scene3D