Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 RenderTarget;
47 class VertexBuffer;
48 } // namespace filament
50 
51 namespace open3d {
52 
53 namespace t {
54 namespace geometry {
55 class Image;
56 }
57 } // namespace t
58 
59 namespace geometry {
60 class Image;
61 }
62 
63 namespace visualization {
64 namespace rendering {
65 
66 // Centralized storage of allocated resources.
67 // Used for convenient access from various components of render.
68 // Owns all added resources.
70 public:
91 
92  explicit FilamentResourceManager(filament::Engine& engine);
94 
95  // \param materialData must remain valid for the duration of the call to
96  // CreateMaterial(), and may be freed afterwards.
97  MaterialHandle CreateMaterial(const void* material_data, size_t data_size);
98  MaterialHandle CreateMaterial(const ResourceLoadRequest& request);
99  MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle& id);
100 
101  TextureHandle CreateTexture(const char* path, bool srgb);
102  TextureHandle CreateTexture(const std::shared_ptr<geometry::Image>& image,
103  bool srgb);
104  // Slow, will make copy of image data and free it after.
105  TextureHandle CreateTexture(const geometry::Image& image, bool srgb);
106  TextureHandle CreateTexture(const t::geometry::Image& image, bool srgb);
107  // Creates texture of size 'dimension' filled with color 'color'
108  TextureHandle CreateTextureFilled(const Eigen::Vector3f& color,
109  size_t dimension);
110  // Creates a texture for use as a color attachment to a RenderTarget
111  TextureHandle CreateColorAttachmentTexture(int width, int height);
112  // Creates a texture for use as a depth attachment to a RenderTarget
113  TextureHandle CreateDepthAttachmentTexture(int width, int height);
114 
115  RenderTargetHandle CreateRenderTarget(TextureHandle color,
116  TextureHandle depth);
117 
118  // Replaces the contents of the texture with the image. Returns false if
119  // the image is not the same size of the texture.
120  bool UpdateTexture(TextureHandle texture,
121  const std::shared_ptr<geometry::Image> image,
122  bool srgb);
123  bool UpdateTexture(TextureHandle texture,
124  const t::geometry::Image& image,
125  bool srgb);
126 
127  IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest& request);
128  SkyboxHandle CreateColorSkybox(const Eigen::Vector3f& color);
129  SkyboxHandle CreateSkybox(const ResourceLoadRequest& request);
130 
131  // Since rendering uses not all Open3D geometry/filament features, we don't
132  // know which arguments pass to CreateVB(...). Thus creation of VB is
133  // managed by FilamentGeometryBuffersBuilder class
134  VertexBufferHandle AddVertexBuffer(filament::VertexBuffer* vertex_buffer);
135  void ReuseVertexBuffer(VertexBufferHandle vb);
136  IndexBufferHandle CreateIndexBuffer(size_t indices_count,
137  size_t index_stride);
138 
139  std::weak_ptr<filament::Material> GetMaterial(const MaterialHandle& id);
140  std::weak_ptr<filament::MaterialInstance> GetMaterialInstance(
141  const MaterialInstanceHandle& id);
142  std::weak_ptr<filament::Texture> GetTexture(const TextureHandle& id);
143  std::weak_ptr<filament::RenderTarget> GetRenderTarget(
144  const RenderTargetHandle& id);
145  std::weak_ptr<filament::IndirectLight> GetIndirectLight(
146  const IndirectLightHandle& id);
147  std::weak_ptr<filament::Skybox> GetSkybox(const SkyboxHandle& id);
148  std::weak_ptr<filament::VertexBuffer> GetVertexBuffer(
149  const VertexBufferHandle& id);
150  std::weak_ptr<filament::IndexBuffer> GetIndexBuffer(
151  const IndexBufferHandle& id);
152 
153  void DestroyAll();
154  void Destroy(const REHandle_abstract& id);
155 
156 public:
157  // Only public so that .cpp file can use this
158  template <class ResourceType>
159  struct BoxedResource {
160  std::shared_ptr<ResourceType> ptr;
161  size_t use_count = 0;
162 
164  BoxedResource(std::shared_ptr<ResourceType> p) : ptr(p), use_count(1) {}
165 
166  std::shared_ptr<ResourceType> operator->() { return ptr; }
167  };
168 
169 private:
170  filament::Engine& engine_;
171 
172  template <class ResourceType>
173  using ResourcesContainer =
174  std::unordered_map<REHandle_abstract, BoxedResource<ResourceType>>;
175 
176  ResourcesContainer<filament::MaterialInstance> material_instances_;
177  ResourcesContainer<filament::Material> materials_;
178  ResourcesContainer<filament::Texture> textures_;
179  ResourcesContainer<filament::RenderTarget> render_targets_;
180  ResourcesContainer<filament::IndirectLight> ibls_;
181  ResourcesContainer<filament::Skybox> skyboxes_;
182  ResourcesContainer<filament::VertexBuffer> vertex_buffers_;
183  ResourcesContainer<filament::IndexBuffer> index_buffers_;
184 
185  // Stores dependent resources, which should be deallocated when
186  // resource referred by map key is deallocated.
187  // WARNING: Don't put in dependent list resources which are available
188  // publicly
189  std::unordered_map<REHandle_abstract, std::unordered_set<REHandle_abstract>>
190  dependencies_;
191 
192  filament::Texture* LoadTextureFromImage(
193  const std::shared_ptr<geometry::Image>& image, bool srgb);
194  filament::Texture* LoadTextureFromImage(const t::geometry::Image& image,
195  bool srgb);
196  filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
197  size_t dimension);
198 
199  void LoadDefaults();
200 };
201 
202 } // namespace rendering
203 } // namespace visualization
204 } // namespace open3d
static const MaterialHandle kInfinitePlaneShader
Definition: FilamentResourceManager.h:82
static const MaterialInstanceHandle kColorMapMaterial
Definition: FilamentResourceManager.h:87
static const MaterialHandle kDefaultDepthValueShader
Definition: FilamentResourceManager.h:78
BoxedResource(std::shared_ptr< ResourceType > p)
Definition: FilamentResourceManager.h:164
static const MaterialHandle kDefaultUnlitWithTransparency
Definition: FilamentResourceManager.h:75
static const MaterialHandle kDefaultDepthShader
Definition: FilamentResourceManager.h:77
Definition: RendererHandle.h:116
static const MaterialHandle kDefaultUnlitSolidColorShader
Definition: FilamentResourceManager.h:80
Definition: FilamentResourceManager.h:69
static const MaterialHandle kDefaultUnlitBackgroundShader
Definition: FilamentResourceManager.h:81
static const TextureHandle kDefaultNormalMap
Definition: FilamentResourceManager.h:90
static const MaterialHandle kDefaultLineShader
Definition: FilamentResourceManager.h:83
static const MaterialHandle kDefaultNormalShader
Definition: FilamentResourceManager.h:76
static const MaterialHandle kDefaultUnlit
Definition: FilamentResourceManager.h:74
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
math::float4 color
Definition: LineSetBuffers.cpp:64
static const MaterialHandle kDefaultLitSSR
Definition: FilamentResourceManager.h:73
static const MaterialInstanceHandle kDepthMaterial
Definition: FilamentResourceManager.h:85
std::shared_ptr< ResourceType > ptr
Definition: FilamentResourceManager.h:160
static const TextureHandle kDefaultColorMap
Definition: FilamentResourceManager.h:89
static const MaterialHandle kDefaultUnlitGradientShader
Definition: FilamentResourceManager.h:79
static const MaterialInstanceHandle kNormalsMaterial
Definition: FilamentResourceManager.h:86
Definition: PinholeCameraIntrinsic.cpp:35
static const MaterialHandle kDefaultLit
Definition: FilamentResourceManager.h:71
std::shared_ptr< ResourceType > operator->()
Definition: FilamentResourceManager.h:166
int height
Definition: FilePCD.cpp:72
static const TextureHandle kDefaultTexture
Definition: FilamentResourceManager.h:88
static const MaterialHandle kDefaultLitWithTransparency
Definition: FilamentResourceManager.h:72
Definition: FilamentEngine.h:31
The Image class stores image with customizable width, height, num of channels and bytes per channel...
Definition: Image.h:53
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:228
static const MaterialHandle kDefaultUnlitPolygonOffsetShader
Definition: FilamentResourceManager.h:84
int width
Definition: FilePCD.cpp:71