Debian Codegraph @

libs/gltfio/src/UbershaderLoader.cpp

raw
1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 #include <gltfio/MaterialProvider.h>
18
19 #include <filament/MaterialInstance.h>
20 #include <filament/Texture.h>
21 #include <filament/TextureSampler.h>
22
23 #include <math/mat4.h>
24
25 #include <utils/Log.h>
26
27 #if GLTFIO_LITE
28 #include "gltfresources_lite.h"
29 #else
30 #include "gltfresources.h"
31 #endif
32
33 using namespace filament;
34 using namespace filament::math;
35 using namespace gltfio;
36 using namespace utils;
37
38 namespace {
39
40 using CullingMode = MaterialInstance::CullingMode;
41
42 class UbershaderLoader : public MaterialProvider {
43 public:
44 UbershaderLoader(filament::Engine* engine);
45 ~UbershaderLoader() {}
46
47 MaterialSource getSource() const noexcept override { return LOAD_UBERSHADERS; }
48
49 filament::MaterialInstance* createMaterialInstance(MaterialKey* config, UvMap* uvmap,
50 const char* label) override;
51
52 size_t getMaterialsCount() const noexcept override;
53 const filament::Material* const* getMaterials() const noexcept override;
54 void destroyMaterials() override;
55
56 Material* getMaterial(const MaterialKey& config) const;
57
58 enum ShadingMode {
59 UNLIT = 0,
60 LIT = 1,
61 SPECULAR_GLOSSINESS = 2,
62 };
63
64 mutable Material* mMaterials[11] = {};
65 Texture* mDummyTexture = nullptr;
66
67 filament::Engine* mEngine;
68 };
69
70 #if GLTFIO_LITE
71
72 #define CREATE_MATERIAL(name) Material::Builder() \
73 .package(GLTFRESOURCES_LITE_ ## name ## _DATA, GLTFRESOURCES_LITE_ ## name ## _SIZE) \
74 .build(*mEngine);
75
76 #else
77
78 #define CREATE_MATERIAL(name) Material::Builder() \
79 .package(GLTFRESOURCES_ ## name ## _DATA, GLTFRESOURCES_ ## name ## _SIZE) \
80 .build(*mEngine);
81
82 #endif
83
84 #define MATINDEX(shading, alpha, sheen, transmit) (transmit ? 10 : (sheen ? 9 : (int(shading) + 3 * int(alpha))))
85
86 UbershaderLoader::UbershaderLoader(Engine* engine) : mEngine(engine) {
87 unsigned char texels[4] = {};
88 mDummyTexture = Texture::Builder()
89 .width(1).height(1)
90 .format(Texture::InternalFormat::RGBA8)
91 .build(*mEngine);
92 Texture::PixelBufferDescriptor pbd(texels, sizeof(texels), Texture::Format::RGBA,
93 Texture::Type::UBYTE);
94 mDummyTexture->setImage(*mEngine, 0, std::move(pbd));
95 }
96
97 size_t UbershaderLoader::getMaterialsCount() const noexcept {
98 return sizeof(mMaterials) / sizeof(mMaterials[0]);
99 }
100
101 const Material* const* UbershaderLoader::getMaterials() const noexcept {
102 return &mMaterials[0];
103 }
104
105 void UbershaderLoader::destroyMaterials() {
106 for (auto& material : mMaterials) {
107 mEngine->destroy(material);
108 material = nullptr;
109 }
110 mEngine->destroy(mDummyTexture);
111 }
112
113 Material* UbershaderLoader::getMaterial(const MaterialKey& config) const {
114 const ShadingMode shading = config.unlit ? UNLIT :
115 (config.useSpecularGlossiness ? SPECULAR_GLOSSINESS : LIT);
116 const int matindex = MATINDEX(shading, config.alphaMode, config.hasSheen, config.hasTransmission);
117 if (mMaterials[matindex] != nullptr) {
118 return mMaterials[matindex];
119 }
120 switch (matindex) {
121
122 #if !GLTFIO_LITE || defined(GLTFRESOURCES_LITE_LIT_OPAQUE_DATA)
123 case MATINDEX(LIT, AlphaMode::OPAQUE, false, false): mMaterials[matindex] = CREATE_MATERIAL(LIT_OPAQUE); break;
124 #endif
125
126 #if !GLTFIO_LITE || defined(GLTFRESOURCES_LITE_LIT_BLEND_DATA)
127 case MATINDEX(LIT, AlphaMode::BLEND, false, false): mMaterials[matindex] = CREATE_MATERIAL(LIT_FADE); break;
128 #endif
129
130 #if !GLTFIO_LITE
131 case MATINDEX(LIT, AlphaMode::MASK, false, false): mMaterials[matindex] = CREATE_MATERIAL(LIT_MASKED); break;
132 case MATINDEX(UNLIT, AlphaMode::OPAQUE, false, false): mMaterials[matindex] = CREATE_MATERIAL(UNLIT_OPAQUE); break;
133 case MATINDEX(UNLIT, AlphaMode::MASK, false, false): mMaterials[matindex] = CREATE_MATERIAL(UNLIT_MASKED); break;
134 case MATINDEX(UNLIT, AlphaMode::BLEND, false, false): mMaterials[matindex] = CREATE_MATERIAL(UNLIT_FADE); break;
135 case MATINDEX(SPECULAR_GLOSSINESS, AlphaMode::OPAQUE, false, false): mMaterials[matindex] = CREATE_MATERIAL(SPECULARGLOSSINESS_OPAQUE); break;
136 case MATINDEX(SPECULAR_GLOSSINESS, AlphaMode::MASK, false, false): mMaterials[matindex] = CREATE_MATERIAL(SPECULARGLOSSINESS_MASKED); break;
137 case MATINDEX(SPECULAR_GLOSSINESS, AlphaMode::BLEND, false, false): mMaterials[matindex] = CREATE_MATERIAL(SPECULARGLOSSINESS_FADE); break;
138 case MATINDEX(0, 0, false, true): mMaterials[matindex] = CREATE_MATERIAL(LIT_TRANSMISSION); break;
139 case MATINDEX(0, 0, true, false): mMaterials[matindex] = CREATE_MATERIAL(LIT_SHEEN); break;
140 #endif
141 }
142 if (mMaterials[matindex] == nullptr) {
143 slog.w << "Unsupported glTF material configuration; falling back to LIT_OPAQUE." << io::endl;
144 MaterialKey litOpaque = config;
145 litOpaque.alphaMode = AlphaMode::OPAQUE;
146 litOpaque.hasTransmission = false;
147 litOpaque.useSpecularGlossiness = false;
148 litOpaque.unlit = false;
149 return getMaterial(litOpaque);
150 }
151 return mMaterials[matindex];
152 }
153
154 MaterialInstance* UbershaderLoader::createMaterialInstance(MaterialKey* config, UvMap* uvmap,
155 const char* label) {
156 // Diagnostics are not supported with LOAD_UBERSHADERS, please use GENERATE_SHADERS instead.
157 if (config->enableDiagnostics) {
158 return nullptr;
159 }
160
161 if (config->hasTransmission && config->hasSheen) {
162 slog.w << "Transmission and sheen are not supported together in ubershader mode,"
163 " removing sheen (" << label << ")." << io::endl;
164 config->hasSheen = false;
165 }
166
167 const bool clearCoatConflict = config->hasTransmission || config->hasSheen;
168
169 // Due to sampler overload, disable transmission if necessary and print a friendly warning.
170 if (config->hasClearCoat && clearCoatConflict) {
171 slog.w << "Transmission and sheen are not supported in ubershader mode for clearcoat"
172 " materials (" << label << ")." << io::endl;
173 config->hasTransmission = false;
174 config->hasSheen = false;
175 }
176
177 constrainMaterial(config, uvmap);
178 auto getUvIndex = [uvmap](uint8_t srcIndex, bool hasTexture) -> int {
179 return hasTexture ? int(uvmap->at(srcIndex)) - 1 : -1;
180 };
181 Material* material = getMaterial(*config);
182 MaterialInstance* mi = material->createInstance(label);
183 mi->setParameter("baseColorIndex",
184 getUvIndex(config->baseColorUV, config->hasBaseColorTexture));
185 mi->setParameter("normalIndex", getUvIndex(config->normalUV, config->hasNormalTexture));
186 mi->setParameter("metallicRoughnessIndex",
187 getUvIndex(config->metallicRoughnessUV, config->hasMetallicRoughnessTexture));
188 mi->setParameter("aoIndex", getUvIndex(config->aoUV, config->hasOcclusionTexture));
189 mi->setParameter("emissiveIndex", getUvIndex(config->emissiveUV, config->hasEmissiveTexture));
190
191 mi->setDoubleSided(config->doubleSided);
192 mi->setCullingMode(config->doubleSided ? CullingMode::NONE : CullingMode::BACK);
193
194 bool clearCoatNeedsTexture = true;
195
196 #if !GLTFIO_LITE
197 mat3f identity;
198 mi->setParameter("baseColorUvMatrix", identity);
199 mi->setParameter("metallicRoughnessUvMatrix", identity);
200 mi->setParameter("normalUvMatrix", identity);
201 mi->setParameter("occlusionUvMatrix", identity);
202 mi->setParameter("emissiveUvMatrix", identity);
203
204 if (config->hasClearCoat) {
205 mi->setParameter("clearCoatIndex",
206 getUvIndex(config->clearCoatUV, config->hasClearCoatTexture));
207 mi->setParameter("clearCoatRoughnessIndex",
208 getUvIndex(config->clearCoatRoughnessUV, config->hasClearCoatRoughnessTexture));
209 mi->setParameter("clearCoatNormalIndex",
210 getUvIndex(config->clearCoatNormalUV, config->hasClearCoatNormalTexture));
211 mi->setParameter("clearCoatUvMatrix", identity);
212 mi->setParameter("clearCoatRoughnessUvMatrix", identity);
213 mi->setParameter("clearCoatNormalUvMatrix", identity);
214 } else {
215 if (config->hasSheen) {
216 clearCoatNeedsTexture = false;
217 mi->setParameter("sheenColorIndex",
218 getUvIndex(config->sheenColorUV, config->hasSheenColorTexture));
219 mi->setParameter("sheenRoughnessIndex",
220 getUvIndex(config->sheenRoughnessUV, config->hasSheenRoughnessTexture));
221 mi->setParameter("sheenColorUvMatrix", identity);
222 mi->setParameter("sheenRoughnessUvMatrix", identity);
223
224 }
225 if (config->hasTransmission) {
226 clearCoatNeedsTexture = false;
227 mi->setParameter("transmissionUvMatrix", identity);
228 mi->setParameter("transmissionIndex",
229 getUvIndex(config->transmissionUV, config->hasTransmissionTexture));
230 }
231 }
232 #endif
233
234 TextureSampler sampler;
235 mi->setParameter("normalMap", mDummyTexture, sampler);
236 mi->setParameter("baseColorMap", mDummyTexture, sampler);
237 mi->setParameter("metallicRoughnessMap", mDummyTexture, sampler);
238 mi->setParameter("occlusionMap", mDummyTexture, sampler);
239 mi->setParameter("emissiveMap", mDummyTexture, sampler);
240 if (clearCoatNeedsTexture) {
241 mi->setParameter("clearCoatMap", mDummyTexture, sampler);
242 mi->setParameter("clearCoatRoughnessMap", mDummyTexture, sampler);
243 mi->setParameter("clearCoatNormalMap", mDummyTexture, sampler);
244 }
245 if (!config->hasClearCoat) {
246 if (config->hasTransmission) {
247 mi->setParameter("transmissionMap", mDummyTexture, sampler);
248 }
249 if (config->hasSheen) {
250 mi->setParameter("sheenColorMap", mDummyTexture, sampler);
251 mi->setParameter("sheenRoughnessMap", mDummyTexture, sampler);
252 }
253 }
254
255 return mi;
256 }
257
258 } // anonymous namespace
259
260 namespace gltfio {
261
262 MaterialProvider* createUbershaderLoader(filament::Engine* engine) {
263 return new UbershaderLoader(engine);
264 }
265
266 } // namespace gltfio