2 * Copyright (c) 2024 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/devel-api/adaptor-framework/image-loading.h>
24 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
25 #include <dali/integration-api/pixel-data-integ.h>
28 #include <dali-scene3d/internal/common/image-resource-loader.h>
32 using namespace Toolkit;
38 const Matrix3 TextureDefinition::DEFAULT_TRANSFORM = Matrix3(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
42 constexpr SamplerFlags::Type FILTER_MODES_FROM_DALI[]{
43 SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_NEAREST,
44 SamplerFlags::FILTER_LINEAR,
45 SamplerFlags::FILTER_NEAREST,
46 SamplerFlags::FILTER_LINEAR,
47 SamplerFlags::FILTER_NEAREST | SamplerFlags::FILTER_MIPMAP_NEAREST,
48 SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_NEAREST,
49 SamplerFlags::FILTER_NEAREST | SamplerFlags::FILTER_MIPMAP_LINEAR,
50 SamplerFlags::FILTER_LINEAR | SamplerFlags::FILTER_MIPMAP_LINEAR,
53 constexpr SamplerFlags::Type WRAP_MODES_FROM_DALI[]{
54 SamplerFlags::WRAP_CLAMP,
55 SamplerFlags::WRAP_CLAMP,
56 SamplerFlags::WRAP_REPEAT,
57 SamplerFlags::WRAP_MIRROR,
60 constexpr FilterMode::Type FILTER_MODES_TO_DALI[]{
63 FilterMode::NEAREST_MIPMAP_NEAREST,
64 FilterMode::LINEAR_MIPMAP_NEAREST,
65 FilterMode::NEAREST_MIPMAP_LINEAR,
66 FilterMode::LINEAR_MIPMAP_LINEAR,
69 constexpr WrapMode::Type WRAP_MODES_TO_DALI[]{
71 WrapMode::CLAMP_TO_EDGE,
72 WrapMode::MIRRORED_REPEAT};
74 const SamplerFlags::Type GetSingleValueSampler()
76 static const SamplerFlags::Type SINGLE_VALUE_SAMPLER = SamplerFlags::Encode(FilterMode::NEAREST, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
77 return SINGLE_VALUE_SAMPLER;
80 static constexpr std::string_view EMBEDDED_DATA_PREFIX = "data:";
81 static constexpr std::string_view EMBEDDED_DATA_IMAGE_MEDIA_TYPE = "image/";
82 static constexpr std::string_view EMBEDDED_DATA_BASE64_ENCODING_TYPE = "base64,";
84 Dali::PixelData LoadImageResource(const std::string& resourcePath,
85 TextureDefinition& textureDefinition,
86 FittingMode::Type fittingMode,
87 bool orientationCorrection)
89 Dali::PixelData pixelData;
90 if(!textureDefinition.mTextureBuffer.empty())
92 Dali::Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromBuffer(textureDefinition.mTextureBuffer.data(), textureDefinition.mTextureBuffer.size(), textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
95 pixelData = Devel::PixelBuffer::Convert(pixelBuffer);
98 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())
100 uint32_t position = textureDefinition.mImageUri.find(EMBEDDED_DATA_BASE64_ENCODING_TYPE.data(), EMBEDDED_DATA_PREFIX.length() + EMBEDDED_DATA_IMAGE_MEDIA_TYPE.length());
101 if(position != std::string::npos)
103 position += EMBEDDED_DATA_BASE64_ENCODING_TYPE.length();
104 std::string_view data = std::string_view(textureDefinition.mImageUri).substr(position);
105 std::vector<uint8_t> buffer;
106 Dali::Toolkit::DecodeBase64FromString(data, buffer);
107 uint32_t bufferSize = buffer.size();
109 Dali::Devel::PixelBuffer pixelBuffer = Dali::LoadImageFromBuffer(reinterpret_cast<uint8_t*>(buffer.data()), bufferSize, textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
112 pixelData = Dali::Devel::PixelBuffer::Convert(pixelBuffer, true);
118 textureDefinition.mDirectoryPath = resourcePath;
119 pixelData = Internal::ImageResourceLoader::GetCachedPixelData(resourcePath + textureDefinition.mImageUri, textureDefinition.mMinImageDimensions, fittingMode, textureDefinition.mSamplingMode, orientationCorrection);
125 SamplerFlags::Type SamplerFlags::Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT)
127 return FILTER_MODES_FROM_DALI[minFilter] | ((FILTER_MODES_FROM_DALI[magFilter] & FILTER_MAG_BITS) << FILTER_MAG_SHIFT) |
128 (WRAP_MODES_FROM_DALI[wrapS] << WRAP_S_SHIFT) | (WRAP_MODES_FROM_DALI[wrapT] << WRAP_T_SHIFT);
131 FilterMode::Type SamplerFlags::GetMinFilter(Type flags)
133 return FILTER_MODES_TO_DALI[flags & FILTER_MIN_MASK];
136 FilterMode::Type SamplerFlags::GetMagFilter(Type flags)
138 return FILTER_MODES_TO_DALI[(flags >> FILTER_MAG_SHIFT) & FILTER_MAG_MASK];
141 WrapMode::Type SamplerFlags::GetWrapS(Type flags)
143 return WRAP_MODES_TO_DALI[(flags >> WRAP_S_SHIFT) & WRAP_S_MASK];
146 WrapMode::Type SamplerFlags::GetWrapT(Type flags)
148 return WRAP_MODES_TO_DALI[(flags >> WRAP_T_SHIFT) & WRAP_T_MASK];
151 Sampler SamplerFlags::MakeSampler(Type flags)
153 auto sampler = Sampler::New();
154 sampler.SetFilterMode(GetMinFilter(flags), GetMagFilter(flags));
155 sampler.SetWrapMode(GetWrapS(flags), GetWrapT(flags));
159 TextureDefinition::TextureDefinition(const std::string& imageUri, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode, Matrix3 transform)
160 : mImageUri(imageUri),
161 mSamplerFlags(samplerFlags),
162 mMinImageDimensions(minImageDimensions),
163 mSamplingMode(samplingMode),
164 mTransform(transform)
168 TextureDefinition::TextureDefinition(std::string&& imageUri, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode, Matrix3 transform)
169 : mImageUri(std::move(imageUri)),
170 mSamplerFlags(samplerFlags),
171 mMinImageDimensions(minImageDimensions),
172 mSamplingMode(samplingMode),
173 mTransform(transform)
177 TextureDefinition::TextureDefinition(std::vector<uint8_t>&& textureBuffer, SamplerFlags::Type samplerFlags, ImageDimensions minImageDimensions, SamplingMode::Type samplingMode, Matrix3 transform)
179 mSamplerFlags(samplerFlags),
180 mMinImageDimensions(minImageDimensions),
181 mSamplingMode(samplingMode),
182 mTransform(transform),
183 mTextureBuffer(std::move(textureBuffer))
187 MaterialDefinition::RawData
188 MaterialDefinition::LoadRaw(const std::string& imagesPath)
192 const bool hasTransparency = MaskMatch(mFlags, TRANSPARENCY);
193 // Why we add additional count here?
194 uint32_t numBuffers = static_cast<uint32_t>(mTextureStages.size()) + (hasTransparency ? !CheckTextures(ALBEDO) + !CheckTextures(METALLIC | ROUGHNESS) + !CheckTextures(NORMAL)
195 : !CheckTextures(ALBEDO | METALLIC) + !CheckTextures(NORMAL | ROUGHNESS));
200 raw.mTextures.reserve(numBuffers);
203 auto iTexture = mTextureStages.begin();
204 auto checkStage = [&](uint32_t flags) {
205 return iTexture != mTextureStages.end() && MaskMatch(iTexture->mSemantic, flags);
208 // Check for compulsory textures: Albedo, Metallic, Roughness, Normal
209 if(checkStage(ALBEDO | METALLIC))
211 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
214 if(checkStage(NORMAL | ROUGHNESS))
216 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
219 else // single value normal-roughness
221 raw.mTextures.push_back({Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataZAxisAndAlphaRGBA(), GetSingleValueSampler()});
226 if(checkStage(ALBEDO))
228 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
231 else if(mNeedAlbedoTexture) // single value albedo, albedo-alpha or albedo-metallic
233 uint32_t bufferSize = 4;
234 uint8_t* buffer = nullptr;
235 auto format = Pixel::Format::RGBA8888;
236 if(hasTransparency) // albedo-alpha
238 buffer = new uint8_t[bufferSize];
239 buffer[3] = static_cast<uint8_t>(mColor.a * 255.f);
241 else if(!checkStage(METALLIC | ROUGHNESS)) // albedo-metallic
243 buffer = new uint8_t[bufferSize];
244 buffer[3] = 0xff; // metallic of 1.0
249 buffer = new uint8_t[bufferSize];
250 format = Pixel::Format::RGB888;
252 buffer[0] = static_cast<uint8_t>(mColor.r * 255.f);
253 buffer[1] = static_cast<uint8_t>(mColor.g * 255.f);
254 buffer[2] = static_cast<uint8_t>(mColor.b * 255.f);
255 raw.mTextures.push_back({Dali::Integration::NewPixelDataWithReleaseAfterUpload(buffer, bufferSize, 1, 1, 0, format, PixelData::DELETE_ARRAY), GetSingleValueSampler()});
258 // If we have transparency, or an image based albedo map, we will have to continue with separate metallicRoughness + normal.
259 const bool createMetallicRoughnessAndNormal = hasTransparency || std::distance(mTextureStages.begin(), iTexture) > 0;
260 if(checkStage(METALLIC | ROUGHNESS))
262 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
265 else if(createMetallicRoughnessAndNormal && mNeedMetallicRoughnessTexture)
267 // NOTE: we want to set both metallic and roughness to 1.0; dli uses the R & A channels,
268 // glTF2 uses B & G, so we might as well just set all components to 1.0.
269 raw.mTextures.push_back({Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataWhiteRGBA(), GetSingleValueSampler()});
272 if(checkStage(NORMAL))
274 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
277 else if(mNeedNormalTexture)
279 if(createMetallicRoughnessAndNormal)
281 raw.mTextures.push_back({Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataZAxisRGB(), GetSingleValueSampler()});
283 else // single-value normal-roughness
285 raw.mTextures.push_back({Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyPixelDataZAxisAndAlphaRGBA(), GetSingleValueSampler()});
291 if(checkStage(SUBSURFACE))
293 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
297 if(checkStage(OCCLUSION))
299 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
303 if(checkStage(EMISSIVE))
305 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
309 if(checkStage(SPECULAR))
311 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
315 if(checkStage(SPECULAR_COLOR))
317 raw.mTextures.push_back({LoadImageResource(imagesPath, iTexture->mTexture, FittingMode::DEFAULT, true), iTexture->mTexture.mSamplerFlags});
324 TextureSet MaterialDefinition::Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const
326 auto textureSet = TextureSet::New();
329 for(auto& tData : raw.mTextures)
331 auto& pixels = tData.mPixels;
335 texture = Dali::Scene3D::Internal::ImageResourceLoader::GetCachedTexture(pixels, tData.mSamplerFlags & SamplerFlags::MIPMAP_MASK);
338 textureSet.SetTexture(n, texture);
339 textureSet.SetSampler(n, SamplerFlags::MakeSampler(tData.mSamplerFlags));
346 textureSet.SetTexture(n++, Dali::Scene3D::Internal::ImageResourceLoader::GetEmptyTextureWhiteRGB());
349 // Assign textures to slots -- starting with 2D ones, then cubemaps, if any.
350 if(mEnvironmentIdx < static_cast<Index>(environments.size()))
352 auto& envTextures = environments[mEnvironmentIdx].second;
353 // If pre-computed brdf texture is defined, set the texture.
354 if(envTextures.mBrdf)
356 textureSet.SetTexture(n, envTextures.mBrdf);
360 if(envTextures.mDiffuse)
362 textureSet.SetTexture(n, envTextures.mDiffuse);
366 if(envTextures.mSpecular)
368 auto specularSampler = Sampler::New();
369 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
370 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
372 textureSet.SetTexture(n, envTextures.mSpecular);
373 textureSet.SetSampler(n, specularSampler);
379 ExceptionFlinger(ASSERT_LOCATION) << "Environment index (" << mEnvironmentIdx << ") out of bounds (" << environments.size() << ").";
385 bool MaterialDefinition::CheckTextures(uint32_t flags) const
387 return std::find_if(mTextureStages.begin(), mTextureStages.end(), [flags](const TextureStage& ts) { return MaskMatch(ts.mSemantic, flags); }) != mTextureStages.end();
390 } // namespace Loader
391 } // namespace Scene3D