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/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 GetSingleValueSampler()
70 static const SamplerFlags::Type SINGLE_VALUE_SAMPLER = SamplerFlags::Encode(FilterMode::NEAREST, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
71 return SINGLE_VALUE_SAMPLER;
74 static constexpr std::string_view EMBEDDED_DATA_PREFIX = "data:";
75 static constexpr std::string_view EMBEDDED_DATA_IMAGE_MEDIA_TYPE = "image/";
76 static constexpr std::string_view EMBEDDED_DATA_BASE64_ENCODING_TYPE = "base64,";
78 Dali::PixelData LoadImageResource(const std::string& resourcePath,
79 TextureDefinition& textureDefinition,
80 FittingMode::Type fittingMode,
81 bool orientationCorrection)
83 Dali::PixelData pixelData;
84 if(!textureDefinition.mTextureBuffer.empty())
86 Dali::Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromBuffer(textureDefinition.mTextureBuffer.data(), textureDefinition.mTextureBuffer.size(), textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
89 pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
92 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())
94 uint32_t position = textureDefinition.mImageUri.find(EMBEDDED_DATA_BASE64_ENCODING_TYPE.data(), EMBEDDED_DATA_PREFIX.length() + EMBEDDED_DATA_IMAGE_MEDIA_TYPE.length());
95 if(position != std::string::npos)
97 position += EMBEDDED_DATA_BASE64_ENCODING_TYPE.length();
98 std::string_view data = std::string_view(textureDefinition.mImageUri).substr(position);
99 std::vector<uint8_t> buffer;
100 Dali::Toolkit::DecodeBase64FromString(data, buffer);
101 uint32_t bufferSize = buffer.size();
103 Dali::Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromBuffer(reinterpret_cast<uint8_t*>(buffer.data()), bufferSize, textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
106 pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
112 pixelData = SyncImageLoader::Load(resourcePath + textureDefinition.mImageUri, textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
118 SamplerFlags::Type SamplerFlags::Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT)
120 return FILTER_MODES_FROM_DALI[minFilter] | ((FILTER_MODES_FROM_DALI[magFilter] & FILTER_MAG_BITS) << FILTER_MAG_SHIFT) |
121 (WRAP_MODES_FROM_DALI[wrapS] << WRAP_S_SHIFT) | (WRAP_MODES_FROM_DALI[wrapT] << WRAP_T_SHIFT);
124 FilterMode::Type SamplerFlags::GetMinFilter(Type flags)
126 return FILTER_MODES_TO_DALI[flags & FILTER_MIN_MASK];
129 FilterMode::Type SamplerFlags::GetMagFilter(Type flags)
131 return FILTER_MODES_TO_DALI[(flags >> FILTER_MAG_SHIFT) & FILTER_MAG_MASK];
134 WrapMode::Type SamplerFlags::GetWrapS(Type flags)
136 return WRAP_MODES_TO_DALI[(flags >> WRAP_S_SHIFT) & WRAP_S_MASK];
139 WrapMode::Type SamplerFlags::GetWrapT(Type flags)
141 return WRAP_MODES_TO_DALI[(flags >> WRAP_T_SHIFT) & WRAP_T_MASK];
144 Sampler SamplerFlags::MakeSampler(Type flags)
146 auto sampler = Sampler::New();
147 sampler.SetFilterMode(GetMinFilter(flags), GetMagFilter(flags));
148 sampler.SetWrapMode(GetWrapS(flags), GetWrapT(flags));
152 TextureDefinition::TextureDefinition(const std::string& imageUri, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode)
153 : mImageUri(imageUri),
154 mSamplerFlags(samplerFlags),
155 mMinImageDimensions(minImageDimensions),
156 mSamplingMode(samplingMode)
160 TextureDefinition::TextureDefinition(std::string&& imageUri, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode)
161 : mImageUri(std::move(imageUri)),
162 mSamplerFlags(samplerFlags),
163 mMinImageDimensions(minImageDimensions),
164 mSamplingMode(samplingMode)
168 TextureDefinition::TextureDefinition(std::vector<uint8_t>&& textureBuffer, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode)
170 mSamplerFlags(samplerFlags),
171 mMinImageDimensions(minImageDimensions),
172 mSamplingMode(samplingMode),
173 mTextureBuffer(std::move(textureBuffer))
177 MaterialDefinition::RawData
178 MaterialDefinition::LoadRaw(const std::string& imagesPath)
182 const bool hasTransparency = MaskMatch(mFlags, TRANSPARENCY);
183 // Why we add additional count here?
184 uint32_t numBuffers = static_cast<uint32_t>(mTextureStages.size()) + (hasTransparency ? !CheckTextures(ALBEDO) + !CheckTextures(METALLIC | ROUGHNESS) + !CheckTextures(NORMAL)
185 : !CheckTextures(ALBEDO | METALLIC) + !CheckTextures(NORMAL | ROUGHNESS));
190 raw.mTextures.reserve(numBuffers);
193 auto iTexture = mTextureStages.begin();
194 auto checkStage = [&](uint32_t flags) {
195 return iTexture != mTextureStages.end() && MaskMatch(iTexture->mSemantic, flags);
198 // Check for compulsory textures: Albedo, Metallic, Roughness, Normal
199 if(checkStage(ALBEDO | METALLIC))
201 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
204 if(checkStage(NORMAL | ROUGHNESS))
206 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
209 else // single value normal-roughness
211 const auto bufferSize = 4;
212 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1
213 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), GetSingleValueSampler()});
218 if(checkStage(ALBEDO))
220 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
223 else if(mNeedAlbedoTexture) // single value albedo, albedo-alpha or albedo-metallic
225 uint32_t bufferSize = 4;
226 uint8_t* buffer = nullptr;
227 auto format = Pixel::Format::RGBA8888;
228 if(hasTransparency) // albedo-alpha
230 buffer = new uint8_t[bufferSize];
231 buffer[3] = static_cast<uint8_t>(mColor.a * 255.f);
233 else if(!checkStage(METALLIC | ROUGHNESS)) // albedo-metallic
235 buffer = new uint8_t[bufferSize];
236 buffer[3] = 0xff; // metallic of 1.0
241 buffer = new uint8_t[bufferSize];
242 format = Pixel::Format::RGB888;
244 buffer[0] = static_cast<uint8_t>(mColor.r * 255.f);
245 buffer[1] = static_cast<uint8_t>(mColor.g * 255.f);
246 buffer[2] = static_cast<uint8_t>(mColor.b * 255.f);
247 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, format, PixelData::DELETE_ARRAY), GetSingleValueSampler()});
250 // If we have transparency, or an image based albedo map, we will have to continue with separate metallicRoughness + normal.
251 const bool createMetallicRoughnessAndNormal = hasTransparency || std::distance(mTextureStages.begin(), iTexture) > 0;
252 if(checkStage(METALLIC | ROUGHNESS))
254 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
257 else if(createMetallicRoughnessAndNormal && mNeedMetallicRoughnessTexture)
259 // NOTE: we want to set both metallic and roughness to 1.0; dli uses the R & A channels,
260 // glTF2 uses B & G, so we might as well just set all components to 1.0.
261 const auto bufferSize = 4;
262 uint8_t* buffer = new uint8_t[bufferSize]{0xff, 0xff, 0xff, 0xff};
263 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), GetSingleValueSampler()});
266 if(checkStage(NORMAL))
268 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
271 else if(mNeedNormalTexture)
273 if(createMetallicRoughnessAndNormal)
275 const auto bufferSize = 3;
276 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff}; // normal of (0, 0, 1)
277 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY), GetSingleValueSampler()});
279 else // single-value normal-roughness
281 const auto bufferSize = 4;
282 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1.0
283 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), GetSingleValueSampler()});
289 if(checkStage(SUBSURFACE))
291 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
295 if(checkStage(OCCLUSION))
297 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
301 if(checkStage(EMISSIVE))
303 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
307 if(checkStage(SPECULAR))
309 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
313 if(checkStage(SPECULAR_COLOR))
315 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
322 TextureSet MaterialDefinition::Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const
324 auto textureSet = TextureSet::New();
327 for(auto& tData : raw.mTextures)
329 auto& pixels = tData.mPixels;
333 texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
334 texture.Upload(tData.mPixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
335 if(tData.mSamplerFlags & SamplerFlags::MIPMAP_MASK)
337 texture.GenerateMipmaps();
341 textureSet.SetTexture(n, texture);
342 textureSet.SetSampler(n, SamplerFlags::MakeSampler(tData.mSamplerFlags));
347 // Assign textures to slots -- starting with 2D ones, then cubemaps, if any.
348 if(mEnvironmentIdx < static_cast<Index>(environments.size()))
350 auto& envTextures = environments[mEnvironmentIdx].second;
351 // If pre-computed brdf texture is defined, set the texture.
352 if(envTextures.mBrdf)
354 textureSet.SetTexture(n, envTextures.mBrdf);
358 if(envTextures.mDiffuse)
360 textureSet.SetTexture(n, envTextures.mDiffuse);
364 if(envTextures.mSpecular)
366 auto specularSampler = Sampler::New();
367 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
368 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
370 textureSet.SetTexture(n, envTextures.mSpecular);
371 textureSet.SetSampler(n, specularSampler);
377 ExceptionFlinger(ASSERT_LOCATION) << "Environment index (" << mEnvironmentIdx << ") out of bounds (" << environments.size() << ").";
383 bool MaterialDefinition::CheckTextures(uint32_t flags) const
385 return std::find_if(mTextureStages.begin(), mTextureStages.end(), [flags](const TextureStage& ts) { return MaskMatch(ts.mSemantic, flags); }) != mTextureStages.end();
388 } // namespace Loader
389 } // namespace Scene3D