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