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/public-api/image-loader/sync-image-loader.h"
26 using namespace Toolkit;
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,
45 constexpr SamplerFlags::Type WRAP_MODES_FROM_DALI[]{
46 SamplerFlags::WRAP_CLAMP,
47 SamplerFlags::WRAP_CLAMP,
48 SamplerFlags::WRAP_REPEAT,
49 SamplerFlags::WRAP_MIRROR,
52 constexpr FilterMode::Type FILTER_MODES_TO_DALI[]{
55 FilterMode::NEAREST_MIPMAP_NEAREST,
56 FilterMode::LINEAR_MIPMAP_NEAREST,
57 FilterMode::NEAREST_MIPMAP_LINEAR,
58 FilterMode::LINEAR_MIPMAP_LINEAR,
61 constexpr WrapMode::Type WRAP_MODES_TO_DALI[]{
63 WrapMode::CLAMP_TO_EDGE,
64 WrapMode::MIRRORED_REPEAT};
66 const SamplerFlags::Type SINGLE_VALUE_SAMPLER = SamplerFlags::Encode(FilterMode::NEAREST, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
69 SamplerFlags::Type SamplerFlags::Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT)
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);
75 FilterMode::Type SamplerFlags::GetMinFilter(Type flags)
77 return FILTER_MODES_TO_DALI[flags & FILTER_MIN_MASK];
80 FilterMode::Type SamplerFlags::GetMagFilter(Type flags)
82 return FILTER_MODES_TO_DALI[(flags >> FILTER_MAG_SHIFT) & FILTER_MAG_MASK];
85 WrapMode::Type SamplerFlags::GetWrapS(Type flags)
87 return WRAP_MODES_TO_DALI[(flags >> WRAP_S_SHIFT) & WRAP_S_MASK];
90 WrapMode::Type SamplerFlags::GetWrapT(Type flags)
92 return WRAP_MODES_TO_DALI[(flags >> WRAP_T_SHIFT) & WRAP_T_MASK];
95 Sampler SamplerFlags::MakeSampler(Type flags)
97 auto sampler = Sampler::New();
98 sampler.SetFilterMode(GetMinFilter(flags), GetMagFilter(flags));
99 sampler.SetWrapMode(GetWrapS(flags), GetWrapT(flags));
103 TextureDefinition::TextureDefinition(const std::string& imageUri, SamplerFlags::Type samplerFlags)
104 : mImageUri(imageUri),
105 mSamplerFlags(samplerFlags)
109 MaterialDefinition::RawData
110 MaterialDefinition::LoadRaw(const std::string& imagesPath) const
114 const bool hasTransparency = MaskMatch(mFlags, TRANSPARENCY);
115 // Why we add additional count here?
116 uint32_t numBuffers = mTextureStages.size() + (hasTransparency ? !CheckTextures(ALBEDO) + !CheckTextures(METALLIC | ROUGHNESS) + !CheckTextures(NORMAL)
117 : !CheckTextures(ALBEDO | METALLIC) + !CheckTextures(NORMAL | ROUGHNESS));
122 raw.mTextures.reserve(numBuffers);
125 auto iTexture = mTextureStages.cbegin();
126 auto checkStage = [&](uint32_t flags) {
127 return iTexture != mTextureStages.end() && MaskMatch(iTexture->mSemantic, flags);
130 // Check for compulsory textures: Albedo, Metallic, Roughness, Normal
131 if(checkStage(ALBEDO | METALLIC))
133 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
136 if(checkStage(NORMAL | ROUGHNESS))
138 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
141 else // single value normal-roughness
143 const auto bufferSize = 4;
144 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1
145 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
150 if(checkStage(ALBEDO))
152 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
155 else if(mNeedAlbedoTexture) // single value albedo, albedo-alpha or albedo-metallic
157 uint32_t bufferSize = 4;
158 uint8_t* buffer = nullptr;
159 auto format = Pixel::Format::RGBA8888;
160 if(hasTransparency) // albedo-alpha
162 buffer = new uint8_t[bufferSize];
163 buffer[3] = static_cast<uint8_t>(mColor.a * 255.f);
165 else if(!checkStage(METALLIC | ROUGHNESS)) // albedo-metallic
167 buffer = new uint8_t[bufferSize];
168 buffer[3] = 0xff; // metallic of 1.0
173 buffer = new uint8_t[bufferSize];
174 format = Pixel::Format::RGB888;
176 buffer[0] = static_cast<uint8_t>(mColor.r * 255.f);
177 buffer[1] = static_cast<uint8_t>(mColor.g * 255.f);
178 buffer[2] = static_cast<uint8_t>(mColor.b * 255.f);
179 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, format, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
182 // If we have transparency, or an image based albedo map, we will have to continue with separate metallicRoughness + normal.
183 const bool createMetallicRoughnessAndNormal = hasTransparency || std::distance(mTextureStages.begin(), iTexture) > 0;
184 if(checkStage(METALLIC | ROUGHNESS))
186 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
189 else if(createMetallicRoughnessAndNormal && mNeedMetallicRoughnessTexture)
191 // NOTE: we want to set both metallic and roughness to 1.0; dli uses the R & A channels,
192 // glTF2 uses B & G, so we might as well just set all components to 1.0.
193 const auto bufferSize = 4;
194 uint8_t* buffer = new uint8_t[bufferSize]{0xff, 0xff, 0xff, 0xff};
195 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
198 if(checkStage(NORMAL))
200 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
203 else if(mNeedNormalTexture)
205 if(createMetallicRoughnessAndNormal)
207 const auto bufferSize = 3;
208 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff}; // normal of (0, 0, 1)
209 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
211 else // single-value normal-roughness
213 const auto bufferSize = 4;
214 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1.0
215 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
221 if(checkStage(SUBSURFACE))
223 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
227 if(checkStage(OCCLUSION))
229 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
233 if(checkStage(EMISSIVE))
235 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
242 TextureSet MaterialDefinition::Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const
244 auto textureSet = TextureSet::New();
247 for(auto& tData : raw.mTextures)
249 auto& pixels = tData.mPixels;
250 auto texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
251 texture.Upload(tData.mPixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
252 if(tData.mSamplerFlags & SamplerFlags::MIPMAP_MASK)
254 texture.GenerateMipmaps();
257 textureSet.SetTexture(n, texture);
258 textureSet.SetSampler(n, SamplerFlags::MakeSampler(tData.mSamplerFlags));
263 // Assign textures to slots -- starting with 2D ones, then cubemaps, if any.
264 if(mEnvironmentIdx < environments.size())
266 auto& envTextures = environments[mEnvironmentIdx].second;
267 // If pre-computed brdf texture is defined, set the texture.
268 if(envTextures.mBrdf)
270 textureSet.SetTexture(n, envTextures.mBrdf);
274 if(envTextures.mDiffuse)
276 textureSet.SetTexture(n, envTextures.mDiffuse);
280 if(envTextures.mSpecular)
282 auto specularSampler = Sampler::New();
283 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
284 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
286 textureSet.SetTexture(n, envTextures.mSpecular);
287 textureSet.SetSampler(n, specularSampler);
293 ExceptionFlinger(ASSERT_LOCATION) << "Environment index (" << mEnvironmentIdx << ") out of bounds (" << environments.size() << ").";
299 bool MaterialDefinition::CheckTextures(uint32_t flags) const
301 return std::find_if(mTextureStages.begin(), mTextureStages.end(), [flags](const TextureStage& ts) {
302 return MaskMatch(ts.mSemantic, flags);
303 }) != mTextureStages.end();
306 } // namespace Loader
307 } // namespace Scene3D