Open3D (C++ API)
FilamentResourceManager.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2019 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <memory>
30 #include <unordered_map>
31 #include <unordered_set>
32 
36 
37 namespace filament {
38 class Engine;
39 class IndexBuffer;
40 class IndirectLight;
41 class Material;
42 class MaterialInstance;
43 class Skybox;
44 class Texture;
45 class VertexBuffer;
46 } // namespace filament
47 
48 namespace open3d {
49 
50 namespace geometry {
51 class Image;
52 }
53 
54 namespace visualization {
55 
56 // Centralized storage of allocated resources.
57 // Used for convenient access from various components of render.
58 // Owns all added resources.
60 public:
69 
70  explicit FilamentResourceManager(filament::Engine& engine);
72 
73  // \param materialData must remain valid for the duration of the call to
74  // CreateMaterial(), and may be freed afterwards.
75  MaterialHandle CreateMaterial(const void* material_data, size_t data_size);
76  MaterialHandle CreateMaterial(const ResourceLoadRequest& request);
77  MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle& id);
78  MaterialInstanceHandle CreateFromDescriptor(
79  const geometry::TriangleMesh::Material& material_attributes);
80 
81  TextureHandle CreateTexture(const char* path);
82  TextureHandle CreateTexture(const std::shared_ptr<geometry::Image>& image);
83  // Slow, will make copy of image data and free it after.
84  TextureHandle CreateTexture(const geometry::Image& image);
85  // Creates texture of size 'dimension' filled with color 'color'
86  TextureHandle CreateTextureFilled(const Eigen::Vector3f& color,
87  size_t dimension);
88 
89  IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest& request);
90  SkyboxHandle CreateSkybox(const ResourceLoadRequest& request);
91 
92  // Since rendering uses not all Open3D geometry/filament features, we don't
93  // know which arguments pass to CreateVB(...). Thus creation of VB is
94  // managed by FilamentGeometryBuffersBuilder class
95  VertexBufferHandle AddVertexBuffer(filament::VertexBuffer* vertex_buffer);
96  IndexBufferHandle CreateIndexBuffer(size_t indices_count,
97  size_t index_stride);
98 
99  std::weak_ptr<filament::Material> GetMaterial(const MaterialHandle& id);
100  std::weak_ptr<filament::MaterialInstance> GetMaterialInstance(
101  const MaterialInstanceHandle& id);
102  std::weak_ptr<filament::Texture> GetTexture(const TextureHandle& id);
103  std::weak_ptr<filament::IndirectLight> GetIndirectLight(
104  const IndirectLightHandle& id);
105  std::weak_ptr<filament::Skybox> GetSkybox(const SkyboxHandle& id);
106  std::weak_ptr<filament::VertexBuffer> GetVertexBuffer(
107  const VertexBufferHandle& id);
108  std::weak_ptr<filament::IndexBuffer> GetIndexBuffer(
109  const IndexBufferHandle& id);
110 
111  void DestroyAll();
112  void Destroy(const REHandle_abstract& id);
113 
114 private:
115  filament::Engine& engine_;
116 
117  template <class ResourceType>
118  using ResourcesContainer =
119  std::unordered_map<REHandle_abstract,
120  std::shared_ptr<ResourceType>>;
121 
122  ResourcesContainer<filament::MaterialInstance> material_instances_;
123  ResourcesContainer<filament::Material> materials_;
124  ResourcesContainer<filament::Texture> textures_;
125  ResourcesContainer<filament::IndirectLight> ibls_;
126  ResourcesContainer<filament::Skybox> skyboxes_;
127  ResourcesContainer<filament::VertexBuffer> vertex_buffers_;
128  ResourcesContainer<filament::IndexBuffer> index_buffers_;
129 
130  // Stores dependent resources, which should be deallocated when
131  // resource referred by map key is deallocated.
132  // WARNING: Don't put in dependent list resources which are available
133  // publicly
134  std::unordered_map<REHandle_abstract, std::unordered_set<REHandle_abstract>>
135  dependencies_;
136 
137  filament::Texture* LoadTextureFromImage(
138  const std::shared_ptr<geometry::Image>& image);
139  filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
140  size_t dimension);
141 
142  void LoadDefaults();
143 };
144 
145 } // namespace visualization
146 } // namespace open3d
Definition: FilamentResourceManager.h:59
static const MaterialInstanceHandle kColorMapMaterial
Definition: FilamentResourceManager.h:65
Definition: TriangleMesh.h:746
math::float4 color
Definition: LineSetBuffers.cpp:46
static const TextureHandle kDefaultNormalMap
Definition: FilamentResourceManager.h:68
static const TextureHandle kDefaultColorMap
Definition: FilamentResourceManager.h:67
Definition: Open3DViewer.h:29
static const MaterialInstanceHandle kDepthMaterial
Definition: FilamentResourceManager.h:63
static const MaterialInstanceHandle kNormalsMaterial
Definition: FilamentResourceManager.h:64
static const TextureHandle kDefaultTexture
Definition: FilamentResourceManager.h:66
static const MaterialHandle kDefaultUnlit
Definition: FilamentResourceManager.h:62
Definition: FilamentCamera.h:31
Definition: RendererHandle.h:71
The Image class stores image with customizable width, height, num of channels and bytes per channel...
Definition: Image.h:53
static const MaterialHandle kDefaultLit
Definition: FilamentResourceManager.h:61