| 1 | /* |
| 2 | * Copyright (C) 2016 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 <filamentapp/IBL.h> |
| 18 | |
| 19 | #include <filament/Engine.h> |
| 20 | #include <filament/IndirectLight.h> |
| 21 | #include <filament/Material.h> |
| 22 | #include <filament/MaterialInstance.h> |
| 23 | #include <filament/Skybox.h> |
| 24 | #include <filament/Texture.h> |
| 25 | |
| 26 | #include <image/KtxBundle.h> |
| 27 | #include <image/KtxUtility.h> |
| 28 | |
| 29 | #include <filament-iblprefilter/IBLPrefilterContext.h> |
| 30 | |
| 31 | #include <stb_image.h> |
| 32 | |
| 33 | #include <utils/Path.h> |
| 34 | |
| 35 | #include <fstream> |
| 36 | #include <iostream> |
| 37 | #include <string> |
| 38 | |
| 39 | #include <string.h> |
| 40 | |
| 41 | using namespace filament; |
| 42 | using namespace image; |
| 43 | using namespace filament::math; |
| 44 | using namespace utils; |
| 45 | |
| 46 | static constexpr float IBL_INTENSITY = 30000.0f; |
| 47 | |
| 48 | IBL::IBL(Engine& engine) : mEngine(engine) { |
| 49 | } |
| 50 | |
| 51 | IBL::~IBL() { |
| 52 | mEngine.destroy(mIndirectLight); |
| 53 | mEngine.destroy(mTexture); |
| 54 | mEngine.destroy(mSkybox); |
| 55 | mEngine.destroy(mSkyboxTexture); |
| 56 | } |
| 57 | |
| 58 | bool IBL::loadFromEquirect(Path const& path) { |
| 59 | if (!path.exists()) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | int w, h; |
| 64 | stbi_info(path.getAbsolutePath().c_str(), &w, &h, nullptr); |
| 65 | if (w != h * 2) { |
| 66 | std::cerr << "not an equirectangular image!" << std::endl; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // load image as float |
| 71 | int n; |
| 72 | const size_t size = w * h * sizeof(float3); |
| 73 | float3* const data = (float3*)stbi_loadf(path.getAbsolutePath().c_str(), &w, &h, &n, 3); |
| 74 | if (data == nullptr || n != 3) { |
| 75 | std::cerr << "Could not decode image " << std::endl; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | // now load texture |
| 80 | Texture::PixelBufferDescriptor buffer( |
| 81 | data, size,Texture::Format::RGB, Texture::Type::FLOAT, |
| 82 | [](void* buffer, size_t size, void* user) { stbi_image_free(buffer); }); |
| 83 | |
| 84 | Texture* const equirect = Texture::Builder() |
| 85 | .width((uint32_t)w) |
| 86 | .height((uint32_t)h) |
| 87 | .levels(0xff) |
| 88 | .format(Texture::InternalFormat::R11F_G11F_B10F) |
| 89 | .sampler(Texture::Sampler::SAMPLER_2D) |
| 90 | .build(mEngine); |
| 91 | |
| 92 | equirect->setImage(mEngine, 0, std::move(buffer)); |
| 93 | |
| 94 | IBLPrefilterContext context(mEngine); |
| 95 | IBLPrefilterContext::EquirectangularToCubemap equirectangularToCubemap(context); |
| 96 | IBLPrefilterContext::SpecularFilter specularFilter(context); |
| 97 | |
| 98 | mSkyboxTexture = equirectangularToCubemap(equirect); |
| 99 | |
| 100 | mEngine.destroy(equirect); |
| 101 | |
| 102 | mTexture = specularFilter(mSkyboxTexture); |
| 103 | |
| 104 | mIndirectLight = IndirectLight::Builder() |
| 105 | .reflections(mTexture) |
| 106 | .intensity(IBL_INTENSITY) |
| 107 | .build(mEngine); |
| 108 | |
| 109 | mSkybox = Skybox::Builder() |
| 110 | .environment(mSkyboxTexture) |
| 111 | .showSun(true) |
| 112 | .build(mEngine); |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool IBL::loadFromKtx(const std::string& prefix) { |
| 118 | Path iblPath(prefix + "_ibl.ktx"); |
| 119 | if (!iblPath.exists()) { |
| 120 | return false; |
| 121 | } |
| 122 | Path skyPath(prefix + "_skybox.ktx"); |
| 123 | if (!skyPath.exists()) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | auto createKtx = [] (Path path) { |
| 128 | using namespace std; |
| 129 | ifstream file(path.getPath(), ios::binary); |
| 130 | vector<uint8_t> contents((istreambuf_iterator<char>(file)), {}); |
| 131 | return new image::KtxBundle(contents.data(), contents.size()); |
| 132 | }; |
| 133 | |
| 134 | KtxBundle* iblKtx = createKtx(iblPath); |
| 135 | KtxBundle* skyKtx = createKtx(skyPath); |
| 136 | |
| 137 | mSkyboxTexture = ktx::createTexture(&mEngine, skyKtx, false); |
| 138 | mTexture = ktx::createTexture(&mEngine, iblKtx, false); |
| 139 | |
| 140 | if (!iblKtx->getSphericalHarmonics(mBands)) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | mIndirectLight = IndirectLight::Builder() |
| 145 | .reflections(mTexture) |
| 146 | .intensity(IBL_INTENSITY) |
| 147 | .build(mEngine); |
| 148 | |
| 149 | mSkybox = Skybox::Builder().environment(mSkyboxTexture).showSun(true).build(mEngine); |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool IBL::loadFromDirectory(const utils::Path& path) { |
| 155 | // First check if KTX files are available. |
| 156 | if (loadFromKtx(Path::concat(path, path.getName()))) { |
| 157 | return true; |
| 158 | } |
| 159 | // Read spherical harmonics |
| 160 | Path sh(Path::concat(path, "sh.txt")); |
| 161 | if (sh.exists()) { |
| 162 | std::ifstream shReader(sh); |
| 163 | shReader >> std::skipws; |
| 164 | |
| 165 | std::string line; |
| 166 | for (float3& band : mBands) { |
| 167 | std::getline(shReader, line); |
| 168 | int n = sscanf(line.c_str(), "(%f,%f,%f)", &band.r, &band.g, &band.b); // NOLINT(cert-err34-c) |
| 169 | if (n != 3) return false; |
| 170 | } |
| 171 | } else { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // Read mip-mapped cubemap |
| 176 | const std::string prefix = "m"; |
| 177 | if (!loadCubemapLevel(&mTexture, path, 0, prefix + "0_")) return false; |
| 178 | |
| 179 | size_t numLevels = mTexture->getLevels(); |
| 180 | for (size_t i = 1; i<numLevels; i++) { |
| 181 | const std::string levelPrefix = prefix + std::to_string(i) + "_"; |
| 182 | loadCubemapLevel(&mTexture, path, i, levelPrefix); |
| 183 | } |
| 184 | |
| 185 | if (!loadCubemapLevel(&mSkyboxTexture, path)) return false; |
| 186 | |
| 187 | mIndirectLight = IndirectLight::Builder() |
| 188 | .reflections(mTexture) |
| 189 | .irradiance(3, mBands) |
| 190 | .intensity(IBL_INTENSITY) |
| 191 | .build(mEngine); |
| 192 | |
| 193 | mSkybox = Skybox::Builder().environment(mSkyboxTexture).showSun(true).build(mEngine); |
| 194 | |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | bool IBL::loadCubemapLevel(filament::Texture** texture, const utils::Path& path, size_t level, |
| 199 | std::string const& levelPrefix) const { |
| 200 | Texture::FaceOffsets offsets; |
| 201 | Texture::PixelBufferDescriptor buffer; |
| 202 | if (loadCubemapLevel(texture, &buffer, &offsets, path, level, levelPrefix)) { |
| 203 | (*texture)->setImage(mEngine, level, std::move(buffer), offsets); |
| 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | bool IBL::loadCubemapLevel( |
| 210 | filament::Texture** texture, |
| 211 | Texture::PixelBufferDescriptor* outBuffer, |
| 212 | Texture::FaceOffsets* outOffsets, |
| 213 | const utils::Path& path, size_t level, std::string const& levelPrefix) const { |
| 214 | static const char* faceSuffix[6] = { "px", "nx", "py", "ny", "pz", "nz" }; |
| 215 | |
| 216 | size_t size = 0; |
| 217 | size_t numLevels = 1; |
| 218 | |
| 219 | { // this is just a scope to avoid variable name hiding below |
| 220 | int w, h; |
| 221 | std::string faceName = levelPrefix + faceSuffix[0] + ".rgb32f"; |
| 222 | Path facePath(Path::concat(path, faceName)); |
| 223 | if (!facePath.exists()) { |
| 224 | std::cerr << "The face " << faceName << " does not exist" << std::endl; |
| 225 | return false; |
| 226 | } |
| 227 | stbi_info(facePath.getAbsolutePath().c_str(), &w, &h, nullptr); |
| 228 | if (w != h) { |
| 229 | std::cerr << "width != height" << std::endl; |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | size = (size_t)w; |
| 234 | |
| 235 | if (!levelPrefix.empty()) { |
| 236 | numLevels = (size_t)std::log2(size) + 1; |
| 237 | } |
| 238 | |
| 239 | if (level == 0) { |
| 240 | *texture = Texture::Builder() |
| 241 | .width((uint32_t)size) |
| 242 | .height((uint32_t)size) |
| 243 | .levels((uint8_t)numLevels) |
| 244 | .format(Texture::InternalFormat::R11F_G11F_B10F) |
| 245 | .sampler(Texture::Sampler::SAMPLER_CUBEMAP) |
| 246 | .build(mEngine); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // RGB_10_11_11_REV encoding: 4 bytes per pixel |
| 251 | const size_t faceSize = size * size * sizeof(uint32_t); |
| 252 | |
| 253 | Texture::FaceOffsets offsets; |
| 254 | Texture::PixelBufferDescriptor buffer( |
| 255 | malloc(faceSize * 6), faceSize * 6, |
| 256 | Texture::Format::RGB, Texture::Type::UINT_10F_11F_11F_REV, |
| 257 | (Texture::PixelBufferDescriptor::Callback) &free); |
| 258 | |
| 259 | bool success = true; |
| 260 | uint8_t* p = static_cast<uint8_t*>(buffer.buffer); |
| 261 | |
| 262 | for (size_t j = 0; j < 6; j++) { |
| 263 | offsets[j] = faceSize * j; |
| 264 | |
| 265 | std::string faceName = levelPrefix + faceSuffix[j] + ".rgb32f"; |
| 266 | Path facePath(Path::concat(path, faceName)); |
| 267 | if (!facePath.exists()) { |
| 268 | std::cerr << "The face " << faceName << " does not exist" << std::endl; |
| 269 | success = false; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | int w, h, n; |
| 274 | unsigned char* data = stbi_load(facePath.getAbsolutePath().c_str(), &w, &h, &n, 4); |
| 275 | if (w != h || w != size) { |
| 276 | std::cerr << "Face " << faceName << "has a wrong size " << w << " x " << h << |
| 277 | ", instead of " << size << " x " << size << std::endl; |
| 278 | success = false; |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | if (data == nullptr || n != 4) { |
| 283 | std::cerr << "Could not decode face " << faceName << std::endl; |
| 284 | success = false; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | memcpy(p + offsets[j], data, w * h * sizeof(uint32_t)); |
| 289 | |
| 290 | stbi_image_free(data); |
| 291 | } |
| 292 | |
| 293 | if (!success) return false; |
| 294 | |
| 295 | *outBuffer = std::move(buffer); |
| 296 | *outOffsets = offsets; |
| 297 | |
| 298 | return true; |
| 299 | } |