Repository: google/filament
File: libs/iblprefilter/src/IBLPrefilterContext.cpp (C++)

Code before:
    { -1.0f, -1.0f, 1.0f, 1.0f },
    { 3.0f,  -1.0f, 1.0f, 1.0f },
    { -1.0f, 3.0f,  1.0f, 1.0f }
};

constexpr uint16_t sFullScreenTriangleIndices[3] = { 0, 1, 2 };

float lodToPerceptualRoughness(float lod) noexcept {
    // Inverse perceptualRoughness-to-LOD mapping:
    // The LOD-to-perceptualRoughness mapping is a quadratic fit for
    // log2(perceptualRoughness)+iblMaxMipLevel when iblMaxMipLevel is 4.
    // We found empirically that this mapping works very well for a 256 cubemap with 5 levels used,
    // but also scales well for other iblMaxMipLevel values.
    const float a = 2.0f;
    const float b = -1.0f;
    return (lod != 0)
           ? saturate((std::sqrt(a * a + 4.0f * b * lod) - a) / (2.0f * b))
           : 0.0f;
}

template<typename T>
constexpr T log4(T x) {
    return std::log2(x) * T(0.5);
}

void cleanupMaterialInstance(MaterialInstance const* mi, Engine& engine, RenderableManager& rcm,
    utils::EntityInstance<RenderableManager> const& ci) {
    // mi is already nullptr, there is no need to clean up again.
    if (mi == nullptr)
        return;

    rcm.clearMaterialInstanceAt(ci, 0);
    engine.destroy(mi);
}

} // anonymous


IBLPrefilterContext::IBLPrefilterContext(Engine& engine)
        : mEngine(engine) {
    utils::EntityManager& em = utils::EntityManager::get();
    mCameraEntity = em.create();
    mFullScreenQuadEntity = em.create();

    mIntegrationMaterial = Material::Builder().package(
            IBLPREFILTER_MATERIALS_IBLPREFILTER_DATA,
            IBLPREFILTER_MATERIALS_IBLPREFILTER_SIZE).build(engine);

    mIrradianceIntegrationMaterial = Material::Builder().package(
            IBLPREFILTER_MATERIALS_IBLPREFILTER_DATA,
            IBLPREFILTER_MATERIALS_IBLPREFILTER_SIZE)

Review comment (suggestion):
```suggestion
static void cleanupMaterialInstance(MaterialInstance const* mi, Engine& engine, RenderableManager& rcm,
```

Code after:
    { -1.0f, -1.0f, 1.0f, 1.0f },
    { 3.0f,  -1.0f, 1.0f, 1.0f },
    { -1.0f, 3.0f,  1.0f, 1.0f }
};

constexpr uint16_t sFullScreenTriangleIndices[3] = { 0, 1, 2 };

static float lodToPerceptualRoughness(float lod) noexcept {
    // Inverse perceptualRoughness-to-LOD mapping:
    // The LOD-to-perceptualRoughness mapping is a quadratic fit for
    // log2(perceptualRoughness)+iblMaxMipLevel when iblMaxMipLevel is 4.
    // We found empirically that this mapping works very well for a 256 cubemap with 5 levels used,
    // but also scales well for other iblMaxMipLevel values.
    const float a = 2.0f;
    const float b = -1.0f;
    return (lod != 0)
           ? saturate((std::sqrt(a * a + 4.0f * b * lod) - a) / (2.0f * b))
           : 0.0f;
}

template<typename T>
constexpr T log4(T x) {
    return std::log2(x) * T(0.5);
}

static void cleanupMaterialInstance(MaterialInstance const* mi, Engine& engine, RenderableManager& rcm,
    RenderableManager::Instance const& ci) {
    // mi is already nullptr, there is no need to clean up again.
    if (mi == nullptr)
        return;

    rcm.clearMaterialInstanceAt(ci, 0);
    engine.destroy(mi);
}

} // anonymous


IBLPrefilterContext::IBLPrefilterContext(Engine& engine)
        : mEngine(engine) {
    utils::EntityManager& em = utils::EntityManager::get();
    mCameraEntity = em.create();
    mFullScreenQuadEntity = em.create();

    mIntegrationMaterial = Material::Builder().package(
            IBLPREFILTER_MATERIALS_IBLPREFILTER_DATA,
            IBLPREFILTER_MATERIALS_IBLPREFILTER_SIZE).build(engine);

    mIrradianceIntegrationMaterial = Material::Builder().package(
            IBLPREFILTER_MATERIALS_IBLPREFILTER_DATA,
            IBLPREFILTER_MATERIALS_IBLPREFILTER_SIZE)