ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
FilamentEngine.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 // 4068: Filament has some clang-specific vectorizing pragma's that MSVC flags
11 #ifdef _MSC_VER
12 #pragma warning(push)
13 #pragma warning(disable : 4068)
14 #endif // _MSC_VER
15 
16 #include <filament/Engine.h>
17 
18 #ifdef _MSC_VER
19 #pragma warning(pop)
20 #endif // _MSC_VER
21 
22 #include <FileSystem.h>
23 
24 #include <cstddef> // <filament/Engine> recursive includes needs this, std::size_t especially
25 
27 
28 namespace cloudViewer {
29 namespace visualization {
30 namespace rendering {
31 
32 namespace {
33 static std::shared_ptr<EngineInstance> g_instance = nullptr;
34 } // namespace
35 
36 EngineInstance::RenderingType EngineInstance::type_ = RenderingType::kDefault;
37 bool EngineInstance::is_headless_ = false;
38 std::string EngineInstance::resource_path_ = "";
39 
41 
42 void EngineInstance::EnableHeadless() { is_headless_ = true; }
43 
44 void EngineInstance::SetResourcePath(const std::string& resource_path) {
45  resource_path_ = resource_path;
48  ("Can't find resource directory: " + resource_path_).c_str());
49  }
50 }
51 
52 const std::string& EngineInstance::GetResourcePath() { return resource_path_; }
53 
54 filament::Engine& EngineInstance::GetInstance() { return *Get().engine_; }
55 
57  return *Get().resource_manager_;
58 }
59 
61  resource_manager_->DestroyAll();
62  delete resource_manager_;
63  resource_manager_ = nullptr;
64 
65  filament::Engine::destroy(engine_);
66  engine_ = nullptr;
67 }
68 
69 EngineInstance& EngineInstance::Get() {
70  if (!g_instance) {
71  g_instance = std::shared_ptr<EngineInstance>(new EngineInstance());
72  }
73  return *g_instance;
74 }
75 
76 void EngineInstance::DestroyInstance() { g_instance.reset(); }
77 
80 extern "C" filament::backend::Platform* CreateEGLHeadlessPlatform();
81 
82 EngineInstance::EngineInstance() {
83  filament::backend::Backend backend = filament::backend::Backend::DEFAULT;
84  switch (type_) {
86  backend = filament::backend::Backend::DEFAULT;
87  break;
89  backend = filament::backend::Backend::OPENGL;
90  break;
92  backend = filament::backend::Backend::VULKAN;
93  break;
95  backend = filament::backend::Backend::METAL;
96  break;
97  }
98 
99  filament::backend::Platform* custom_platform = nullptr;
100  if (is_headless_) {
101 #ifdef __linux__
102  utility::LogInfo("EGL headless mode enabled.");
103  custom_platform = CreateEGLHeadlessPlatform();
104 #else
105  utility::LogError("EGL Headless is not supported on this platform.");
106 #endif
107  }
108 
109  engine_ = filament::Engine::create(backend, custom_platform);
110  resource_manager_ = new FilamentResourceManager(*engine_);
111 }
112 
113 } // namespace rendering
114 } // namespace visualization
115 } // namespace cloudViewer
char type
static FilamentResourceManager & GetResourceManager()
static void SetResourcePath(const std::string &resource_path)
#define LogInfo(...)
Definition: Logging.h:81
#define LogError(...)
Definition: Logging.h:60
bool DirectoryExists(const std::string &directory)
Definition: FileSystem.cpp:473
filament::backend::Platform * CreateEGLHeadlessPlatform()
Generic file read and write utility for python interface.