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-scene-loader/public-api/material-definition.h"
22 #include "dali-toolkit/public-api/image-loader/sync-image-loader.h"
26 using namespace Toolkit;
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,
43 constexpr SamplerFlags::Type WRAP_MODES_FROM_DALI[]{
44 SamplerFlags::WRAP_CLAMP,
45 SamplerFlags::WRAP_CLAMP,
46 SamplerFlags::WRAP_REPEAT,
47 SamplerFlags::WRAP_MIRROR,
50 constexpr FilterMode::Type FILTER_MODES_TO_DALI[]{
53 FilterMode::NEAREST_MIPMAP_NEAREST,
54 FilterMode::LINEAR_MIPMAP_NEAREST,
55 FilterMode::NEAREST_MIPMAP_LINEAR,
56 FilterMode::LINEAR_MIPMAP_LINEAR,
59 constexpr WrapMode::Type WRAP_MODES_TO_DALI[]{
61 WrapMode::CLAMP_TO_EDGE,
62 WrapMode::MIRRORED_REPEAT};
64 const SamplerFlags::Type SINGLE_VALUE_SAMPLER = SamplerFlags::Encode(FilterMode::NEAREST, FilterMode::NEAREST, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
67 SamplerFlags::Type SamplerFlags::Encode(FilterMode::Type minFilter, FilterMode::Type magFilter, WrapMode::Type wrapS, WrapMode::Type wrapT)
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);
73 FilterMode::Type SamplerFlags::GetMinFilter(Type flags)
75 return FILTER_MODES_TO_DALI[flags & FILTER_MIN_MASK];
78 FilterMode::Type SamplerFlags::GetMagFilter(Type flags)
80 return FILTER_MODES_TO_DALI[(flags >> FILTER_MAG_SHIFT) & FILTER_MAG_MASK];
83 WrapMode::Type SamplerFlags::GetWrapS(Type flags)
85 return WRAP_MODES_TO_DALI[(flags >> WRAP_S_SHIFT) & WRAP_S_MASK];
88 WrapMode::Type SamplerFlags::GetWrapT(Type flags)
90 return WRAP_MODES_TO_DALI[(flags >> WRAP_T_SHIFT) & WRAP_T_MASK];
93 Sampler SamplerFlags::MakeSampler(Type flags)
95 auto sampler = Sampler::New();
96 sampler.SetFilterMode(GetMinFilter(flags), GetMagFilter(flags));
97 sampler.SetWrapMode(GetWrapS(flags), GetWrapT(flags));
101 TextureDefinition::TextureDefinition(const std::string& imageUri, SamplerFlags::Type samplerFlags)
102 : mImageUri(imageUri),
103 mSamplerFlags(samplerFlags)
107 MaterialDefinition::RawData
108 MaterialDefinition::LoadRaw(const std::string& imagesPath) const
112 const bool hasTransparency = MaskMatch(mFlags, TRANSPARENCY);
113 // Why we add additional count here?
114 uint32_t numBuffers = mTextureStages.size() + (hasTransparency ? !CheckTextures(ALBEDO) + !CheckTextures(METALLIC | ROUGHNESS) + !CheckTextures(NORMAL)
115 : !CheckTextures(ALBEDO | METALLIC) + !CheckTextures(NORMAL | ROUGHNESS));
120 raw.mTextures.reserve(numBuffers);
123 auto iTexture = mTextureStages.cbegin();
124 auto checkStage = [&](uint32_t flags) {
125 return iTexture != mTextureStages.end() && MaskMatch(iTexture->mSemantic, flags);
128 // Check for compulsory textures: Albedo, Metallic, Roughness, Normal
129 if(checkStage(ALBEDO | METALLIC))
131 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
134 if(checkStage(NORMAL | ROUGHNESS))
136 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
139 else // single value normal-roughness
141 const auto bufferSize = 4;
142 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1
143 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
148 if(checkStage(ALBEDO))
150 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
153 else if(mNeedAlbedoTexture) // single value albedo, albedo-alpha or albedo-metallic
155 uint32_t bufferSize = 4;
156 uint8_t* buffer = nullptr;
157 auto format = Pixel::Format::RGBA8888;
158 if(hasTransparency) // albedo-alpha
160 buffer = new uint8_t[bufferSize];
161 buffer[3] = static_cast<uint8_t>(mColor.a * 255.f);
163 else if(!checkStage(METALLIC | ROUGHNESS)) // albedo-metallic
165 buffer = new uint8_t[bufferSize];
166 buffer[3] = 0xff; // metallic of 1.0
171 buffer = new uint8_t[bufferSize];
172 format = Pixel::Format::RGB888;
174 buffer[0] = static_cast<uint8_t>(mColor.r * 255.f);
175 buffer[1] = static_cast<uint8_t>(mColor.g * 255.f);
176 buffer[2] = static_cast<uint8_t>(mColor.b * 255.f);
177 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, format, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
180 // If we have transparency, or an image based albedo map, we will have to continue with separate metallicRoughness + normal.
181 const bool createMetallicRoughnessAndNormal = hasTransparency || std::distance(mTextureStages.begin(), iTexture) > 0;
182 if(checkStage(METALLIC | ROUGHNESS))
184 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
187 else if(createMetallicRoughnessAndNormal && mNeedMetallicRoughnessTexture)
189 // NOTE: we want to set both metallic and roughness to 1.0; dli uses the R & A channels,
190 // glTF2 uses B & G, so we might as well just set all components to 1.0.
191 const auto bufferSize = 4;
192 uint8_t* buffer = new uint8_t[bufferSize]{0xff, 0xff, 0xff, 0xff};
193 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
196 if(checkStage(NORMAL))
198 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
201 else if(mNeedNormalTexture)
203 if(createMetallicRoughnessAndNormal)
205 const auto bufferSize = 3;
206 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff}; // normal of (0, 0, 1)
207 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
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.0
213 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
219 if(checkStage(SUBSURFACE))
221 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
225 if(checkStage(OCCLUSION))
227 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
231 if(checkStage(EMISSIVE))
233 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
240 TextureSet MaterialDefinition::Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const
242 auto textureSet = TextureSet::New();
245 for(auto& tData : raw.mTextures)
247 auto& pixels = tData.mPixels;
248 auto texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
249 texture.Upload(tData.mPixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
250 if(tData.mSamplerFlags & SamplerFlags::MIPMAP_MASK)
252 texture.GenerateMipmaps();
255 textureSet.SetTexture(n, texture);
256 textureSet.SetSampler(n, SamplerFlags::MakeSampler(tData.mSamplerFlags));
261 // Assign textures to slots -- starting with 2D ones, then cubemaps, if any.
262 if(mEnvironmentIdx < environments.size())
264 auto& envTextures = environments[mEnvironmentIdx].second;
265 if(envTextures.mDiffuse)
267 textureSet.SetTexture(n, envTextures.mDiffuse);
271 if(envTextures.mSpecular)
273 auto specularSampler = Sampler::New();
274 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
275 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
277 textureSet.SetTexture(n, envTextures.mSpecular);
278 textureSet.SetSampler(n, specularSampler);
282 // If pre-computed brdf texture is defined, set the texture.
283 if(envTextures.mBrdf)
285 textureSet.SetTexture(n, envTextures.mBrdf);
290 ExceptionFlinger(ASSERT_LOCATION) << "Environment index (" << mEnvironmentIdx << ") out of bounds (" << environments.size() << ").";
296 bool MaterialDefinition::CheckTextures(uint32_t flags) const
298 return std::find_if(mTextureStages.begin(), mTextureStages.end(), [flags](const TextureStage& ts) {
299 return MaskMatch(ts.mSemantic, flags);
300 }) != mTextureStages.end();
303 } // namespace SceneLoader