Open3D (C++ API)  0.11.0
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 
38 namespace filament {
39 class Engine;
40 class IndexBuffer;
41 class IndirectLight;
42 class Material;
43 class MaterialInstance;
44 class Skybox;
45 class Texture;
46 class VertexBuffer;
47 } // namespace filament
49 
50 namespace open3d {
51 
52 namespace geometry {
53 class Image;
54 }
55 
56 namespace visualization {
57 namespace rendering {
58 
59 // Centralized storage of allocated resources.
60 // Used for convenient access from various components of render.
61 // Owns all added resources.
63 public:
77 
78  explicit FilamentResourceManager(filament::Engine& engine);
80 
81  // \param materialData must remain valid for the duration of the call to
82  // CreateMaterial(), and may be freed afterwards.
83  MaterialHandle CreateMaterial(const void* material_data, size_t data_size);
84  MaterialHandle CreateMaterial(const ResourceLoadRequest& request);
85  MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle& id);
86 
87  TextureHandle CreateTexture(const char* path, bool srgb);
88  TextureHandle CreateTexture(const std::shared_ptr<geometry::Image>& image,
89  bool srgb);
90  // Slow, will make copy of image data and free it after.
91  TextureHandle CreateTexture(const geometry::Image& image, bool srgb);
92  // Creates texture of size 'dimension' filled with color 'color'
93  TextureHandle CreateTextureFilled(const Eigen::Vector3f& color,
94  size_t dimension);
95 
96  IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest& request);
97  SkyboxHandle CreateColorSkybox(const Eigen::Vector3f& color);
98  SkyboxHandle CreateSkybox(const ResourceLoadRequest& request);
99 
100  // Since rendering uses not all Open3D geometry/filament features, we don't
101  // know which arguments pass to CreateVB(...). Thus creation of VB is
102  // managed by FilamentGeometryBuffersBuilder class
103  VertexBufferHandle AddVertexBuffer(filament::VertexBuffer* vertex_buffer);
104  void ReuseVertexBuffer(VertexBufferHandle vb);
105  IndexBufferHandle CreateIndexBuffer(size_t indices_count,
106  size_t index_stride);
107 
108  std::weak_ptr<filament::Material> GetMaterial(const MaterialHandle& id);
109  std::weak_ptr<filament::MaterialInstance> GetMaterialInstance(
110  const MaterialInstanceHandle& id);
111  std::weak_ptr<filament::Texture> GetTexture(const TextureHandle& id);
112  std::weak_ptr<filament::IndirectLight> GetIndirectLight(
113  const IndirectLightHandle& id);
114  std::weak_ptr<filament::Skybox> GetSkybox(const SkyboxHandle& id);
115  std::weak_ptr<filament::VertexBuffer> GetVertexBuffer(
116  const VertexBufferHandle& id);
117  std::weak_ptr<filament::IndexBuffer> GetIndexBuffer(
118  const IndexBufferHandle& id);
119 
120  void DestroyAll();
121  void Destroy(const REHandle_abstract& id);
122 
123 public:
124  // Only public so that .cpp file can use this
125  template <class ResourceType>
126  struct BoxedResource {
127  std::shared_ptr<ResourceType> ptr;
128  size_t use_count = 0;
129 
131  BoxedResource(std::shared_ptr<ResourceType> p) : ptr(p), use_count(1) {}
132 
133  std::shared_ptr<ResourceType> operator->() { return ptr; }
134  };
135 
136 private:
137  filament::Engine& engine_;
138 
139  template <class ResourceType>
140  using ResourcesContainer =
141  std::unordered_map<REHandle_abstract, BoxedResource<ResourceType>>;
142 
143  ResourcesContainer<filament::MaterialInstance> material_instances_;
144  ResourcesContainer<filament::Material> materials_;
145  ResourcesContainer<filament::Texture> textures_;
146  ResourcesContainer<filament::IndirectLight> ibls_;
147  ResourcesContainer<filament::Skybox> skyboxes_;
148  ResourcesContainer<filament::VertexBuffer> vertex_buffers_;
149  ResourcesContainer<filament::IndexBuffer> index_buffers_;
150 
151  // Stores dependent resources, which should be deallocated when
152  // resource referred by map key is deallocated.
153  // WARNING: Don't put in dependent list resources which are available
154  // publicly
155  std::unordered_map<REHandle_abstract, std::unordered_set<REHandle_abstract>>
156  dependencies_;
157 
158  filament::Texture* LoadTextureFromImage(
159  const std::shared_ptr<geometry::Image>& image, bool srgb);
160  filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
161  size_t dimension);
162 
163  void LoadDefaults();
164 };
165 
166 } // namespace rendering
167 } // namespace visualization
168 } // namespace open3d
static const MaterialInstanceHandle kColorMapMaterial
Definition: FilamentResourceManager.h:73
BoxedResource(std::shared_ptr< ResourceType > p)
Definition: FilamentResourceManager.h:131
static const MaterialHandle kDefaultDepthShader
Definition: FilamentResourceManager.h:68
Definition: RendererHandle.h:115
static const MaterialHandle kDefaultUnlitSolidColorShader
Definition: FilamentResourceManager.h:70
Definition: FilamentResourceManager.h:62
static const TextureHandle kDefaultNormalMap
Definition: FilamentResourceManager.h:76
static const MaterialHandle kDefaultNormalShader
Definition: FilamentResourceManager.h:67
static const MaterialHandle kDefaultUnlit
Definition: FilamentResourceManager.h:66
math::float4 color
Definition: LineSetBuffers.cpp:63
static const MaterialInstanceHandle kDepthMaterial
Definition: FilamentResourceManager.h:71
std::shared_ptr< ResourceType > ptr
Definition: FilamentResourceManager.h:127
static const TextureHandle kDefaultColorMap
Definition: FilamentResourceManager.h:75
static const MaterialHandle kDefaultUnlitGradientShader
Definition: FilamentResourceManager.h:69
static const MaterialInstanceHandle kNormalsMaterial
Definition: FilamentResourceManager.h:72
Definition: PinholeCameraIntrinsic.cpp:35
static const MaterialHandle kDefaultLit
Definition: FilamentResourceManager.h:64
std::shared_ptr< ResourceType > operator->()
Definition: FilamentResourceManager.h:133
static const TextureHandle kDefaultTexture
Definition: FilamentResourceManager.h:74
static const MaterialHandle kDefaultLitWithTransparency
Definition: FilamentResourceManager.h:65
Definition: FilamentEngine.h:31
The Image class stores image with customizable width, height, num of channels and bytes per channel...
Definition: Image.h:53