From 0fd2436cd18444248617be8fb913a2b054d71c52 Mon Sep 17 00:00:00 2001 From: Powei Feng Date: Fri, 13 Sep 2024 15:22:55 -0700 Subject: [PATCH] sample: fix two IBL loading crashes - Both Scene and IBL are holding on to a skybox reference. We need to make sure the order they are destroyed in right order. - Reloading IBL should trigger resetting the indrect light in gltf_viewer. --- libs/filamentapp/src/FilamentApp.cpp | 3 +++ samples/gltf_viewer.cpp | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/libs/filamentapp/src/FilamentApp.cpp b/libs/filamentapp/src/FilamentApp.cpp index d4e4b44ac32..4b7bbd8c33d 100644 --- a/libs/filamentapp/src/FilamentApp.cpp +++ b/libs/filamentapp/src/FilamentApp.cpp @@ -552,6 +552,9 @@ void FilamentApp::loadIBL(std::string_view path) { return; } + // Note that IBL holds a skybox, and Scene also holds a reference. We cannot release IBL's + // skybox until after new skybox has been set in the scene. + std::unique_ptr oldIBL = std::move(mIBL); mIBL = std::make_unique(*mEngine); if (!iblPath.isDirectory()) { diff --git a/samples/gltf_viewer.cpp b/samples/gltf_viewer.cpp index e3dc4363a38..ee5e1362262 100644 --- a/samples/gltf_viewer.cpp +++ b/samples/gltf_viewer.cpp @@ -671,7 +671,15 @@ int main(int argc, char** argv) { buffer.shrink_to_fit(); }; - auto loadResources = [&app] (const utils::Path& filename) { + auto setupIBL = [&app]() { + auto ibl = FilamentApp::get().getIBL(); + if (ibl) { + app.viewer->setIndirectLight(ibl->getIndirectLight(), ibl->getSphericalHarmonics()); + app.viewer->getSettings().view.fogSettings.fogColorTexture = ibl->getFogTexture(); + } + }; + + auto loadResources = [&app, &setupIBL] (const utils::Path& filename) { // Load external textures and buffers. std::string const gltfPath = filename.getAbsolutePath(); ResourceConfiguration configuration = {}; @@ -708,12 +716,7 @@ int main(int argc, char** argv) { instances[mi]->setStencilWrite(true); instances[mi]->setStencilOpDepthStencilPass(MaterialInstance::StencilOperation::INCR); } - - auto ibl = FilamentApp::get().getIBL(); - if (ibl) { - app.viewer->setIndirectLight(ibl->getIndirectLight(), ibl->getSphericalHarmonics()); - app.viewer->getSettings().view.fogSettings.fogColorTexture = ibl->getFogTexture(); - } + setupIBL(); }; auto setup = [&](Engine* engine, View* view, Scene* scene) { @@ -1190,7 +1193,7 @@ int main(int argc, char** argv) { filename = getPathForIBLAsset(path); if (!filename.isEmpty()) { FilamentApp::get().loadIBL(path); - return; + setupIBL(); } });