2 * Copyright (c) 2021 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 uint32_t numBuffers = mTextureStages.size() + (hasTransparency ? !CheckTextures(ALBEDO) + !CheckTextures(METALLIC | ROUGHNESS) + !CheckTextures(NORMAL) : !CheckTextures(ALBEDO | METALLIC) + !CheckTextures(NORMAL | ROUGHNESS));
118 raw.mTextures.reserve(numBuffers);
121 auto iTexture = mTextureStages.cbegin();
122 auto checkStage = [&](uint32_t flags) {
123 return iTexture != mTextureStages.end() && MaskMatch(iTexture->mSemantic, flags);
126 // Check for compulsory textures: Albedo, Metallic, Roughness, Normal
127 if(checkStage(ALBEDO | METALLIC))
129 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
132 if(checkStage(NORMAL | ROUGHNESS))
134 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
137 else // single value normal-roughness
139 const auto bufferSize = 4;
140 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1
141 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
146 if(checkStage(ALBEDO))
148 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
151 else // single value albedo, albedo-alpha or albedo-metallic
153 uint32_t bufferSize = 4;
154 uint8_t* buffer = nullptr;
155 auto format = Pixel::Format::RGBA8888;
156 if(hasTransparency) // albedo-alpha
158 buffer = new uint8_t[bufferSize];
159 buffer[3] = static_cast<uint8_t>(mColor.a * 255.f);
161 else if(!checkStage(METALLIC | ROUGHNESS)) // albedo-metallic
163 buffer = new uint8_t[bufferSize];
164 buffer[3] = 0xff; // metallic of 1.0
169 buffer = new uint8_t[bufferSize];
170 format = Pixel::Format::RGB888;
172 buffer[0] = static_cast<uint8_t>(mColor.r * 255.f);
173 buffer[1] = static_cast<uint8_t>(mColor.g * 255.f);
174 buffer[2] = static_cast<uint8_t>(mColor.b * 255.f);
175 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, format, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
178 // If we have transparency, or an image based albedo map, we will have to continue with separate metallicRoughness + normal.
179 const bool createMetallicRoughnessAndNormal = hasTransparency || std::distance(mTextureStages.begin(), iTexture) > 0;
180 if(checkStage(METALLIC | ROUGHNESS))
182 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
185 else if(createMetallicRoughnessAndNormal)
187 // NOTE: we want to set both metallic and roughness to 1.0; dli uses the R & A channels,
188 // glTF2 uses B & G, so we might as well just set all components to 1.0.
189 const auto bufferSize = 4;
190 uint8_t* buffer = new uint8_t[bufferSize]{0xff, 0xff, 0xff, 0xff};
191 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
194 if(checkStage(NORMAL))
196 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
199 else if(createMetallicRoughnessAndNormal)
201 const auto bufferSize = 3;
202 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff}; // normal of (0, 0, 1)
203 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGB888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
205 else // single-value normal-roughness
207 const auto bufferSize = 4;
208 uint8_t* buffer = new uint8_t[bufferSize]{0x7f, 0x7f, 0xff, 0xff}; // normal of (0, 0, 1), roughness of 1.0
209 raw.mTextures.push_back({PixelData::New(buffer, bufferSize, 1, 1, Pixel::RGBA8888, PixelData::DELETE_ARRAY), SINGLE_VALUE_SAMPLER});
213 // Extra textures. TODO: emissive, occlusion etc.
214 if(checkStage(SUBSURFACE))
216 raw.mTextures.push_back({SyncImageLoader::Load(imagesPath + iTexture->mTexture.mImageUri), iTexture->mTexture.mSamplerFlags});
223 TextureSet MaterialDefinition::Load(const EnvironmentDefinition::Vector& environments, RawData&& raw) const
225 auto textureSet = TextureSet::New();
228 for(auto& tData : raw.mTextures)
230 auto& pixels = tData.mPixels;
231 auto texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
232 texture.Upload(tData.mPixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
233 if(tData.mSamplerFlags & SamplerFlags::MIPMAP_MASK)
235 texture.GenerateMipmaps();
238 textureSet.SetTexture(n, texture);
239 textureSet.SetSampler(n, SamplerFlags::MakeSampler(tData.mSamplerFlags));
244 // Assign textures to slots -- starting with 2D ones, then cubemaps, if any.
245 if(mEnvironmentIdx < environments.size())
247 auto& envTextures = environments[mEnvironmentIdx].second;
248 if(envTextures.mDiffuse)
250 textureSet.SetTexture(n, envTextures.mDiffuse);
254 if(envTextures.mSpecular)
256 auto specularSampler = Sampler::New();
257 specularSampler.SetWrapMode(WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE, WrapMode::CLAMP_TO_EDGE);
258 specularSampler.SetFilterMode(FilterMode::LINEAR_MIPMAP_LINEAR, FilterMode::LINEAR);
260 textureSet.SetTexture(n, envTextures.mSpecular);
261 textureSet.SetSampler(n, specularSampler);
266 ExceptionFlinger(ASSERT_LOCATION) << "Environment index (" << mEnvironmentIdx << ") out of bounds (" << environments.size() << ").";
272 bool MaterialDefinition::CheckTextures(uint32_t flags) const
274 return std::find_if(mTextureStages.begin(), mTextureStages.end(), [flags](const TextureStage& ts) {
275 return MaskMatch(ts.mSemantic, flags);
276 }) != mTextureStages.end();
279 } // namespace SceneLoader