Open3D (C++ API)  0.18.0+afb23f8
FilamentResourceManager.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <memory>
11 #include <unordered_map>
12 #include <unordered_set>
13 
17 
19 namespace filament {
20 class Engine;
21 class IndexBuffer;
22 class IndirectLight;
23 class Material;
24 class MaterialInstance;
25 class Skybox;
26 class Texture;
27 class RenderTarget;
28 class VertexBuffer;
29 } // namespace filament
31 
32 namespace open3d {
33 
34 namespace t {
35 namespace geometry {
36 class Image;
37 }
38 } // namespace t
39 
40 namespace geometry {
41 class Image;
42 }
43 
44 namespace visualization {
45 namespace rendering {
46 
47 // Centralized storage of allocated resources.
48 // Used for convenient access from various components of render.
49 // Owns all added resources.
51 public:
72 
73  explicit FilamentResourceManager(filament::Engine& engine);
75 
76  // \param materialData must remain valid for the duration of the call to
77  // CreateMaterial(), and may be freed afterwards.
78  MaterialHandle CreateMaterial(const void* material_data, size_t data_size);
81 
82  TextureHandle CreateTexture(const char* path, bool srgb);
83  TextureHandle CreateTexture(const std::shared_ptr<geometry::Image>& image,
84  bool srgb);
85  // Slow, will make copy of image data and free it after.
88  // Creates texture of size 'dimension' filled with color 'color'
89  TextureHandle CreateTextureFilled(const Eigen::Vector3f& color,
90  size_t dimension);
91  // Creates a texture for use as a color attachment to a RenderTarget
93  // Creates a texture for use as a depth attachment to a RenderTarget
95 
97  TextureHandle depth);
98 
99  // Replaces the contents of the texture with the image. Returns false if
100  // the image is not the same size of the texture.
101  bool UpdateTexture(TextureHandle texture,
102  const std::shared_ptr<geometry::Image> image,
103  bool srgb);
104  bool UpdateTexture(TextureHandle texture,
105  const t::geometry::Image& image,
106  bool srgb);
107 
109  SkyboxHandle CreateColorSkybox(const Eigen::Vector3f& color);
111 
112  // Since rendering uses not all Open3D geometry/filament features, we don't
113  // know which arguments pass to CreateVB(...). Thus creation of VB is
114  // managed by FilamentGeometryBuffersBuilder class
115  VertexBufferHandle AddVertexBuffer(filament::VertexBuffer* vertex_buffer);
117  IndexBufferHandle CreateIndexBuffer(size_t indices_count,
118  size_t index_stride);
119 
120  std::weak_ptr<filament::Material> GetMaterial(const MaterialHandle& id);
121  std::weak_ptr<filament::MaterialInstance> GetMaterialInstance(
122  const MaterialInstanceHandle& id);
123  std::weak_ptr<filament::Texture> GetTexture(const TextureHandle& id);
124  std::weak_ptr<filament::RenderTarget> GetRenderTarget(
125  const RenderTargetHandle& id);
126  std::weak_ptr<filament::IndirectLight> GetIndirectLight(
127  const IndirectLightHandle& id);
128  std::weak_ptr<filament::Skybox> GetSkybox(const SkyboxHandle& id);
129  std::weak_ptr<filament::VertexBuffer> GetVertexBuffer(
130  const VertexBufferHandle& id);
131  std::weak_ptr<filament::IndexBuffer> GetIndexBuffer(
132  const IndexBufferHandle& id);
133 
134  void DestroyAll();
135  void Destroy(const REHandle_abstract& id);
136 
137 public:
138  // Only public so that .cpp file can use this
139  template <class ResourceType>
140  struct BoxedResource {
141  std::shared_ptr<ResourceType> ptr;
142  size_t use_count = 0;
143 
145  BoxedResource(std::shared_ptr<ResourceType> p) : ptr(p), use_count(1) {}
146 
147  std::shared_ptr<ResourceType> operator->() { return ptr; }
148  };
149 
150 private:
151  filament::Engine& engine_;
152 
153  template <class ResourceType>
154  using ResourcesContainer =
155  std::unordered_map<REHandle_abstract, BoxedResource<ResourceType>>;
156 
157  ResourcesContainer<filament::MaterialInstance> material_instances_;
158  ResourcesContainer<filament::Material> materials_;
159  ResourcesContainer<filament::Texture> textures_;
160  ResourcesContainer<filament::RenderTarget> render_targets_;
161  ResourcesContainer<filament::IndirectLight> ibls_;
162  ResourcesContainer<filament::Skybox> skyboxes_;
163  ResourcesContainer<filament::VertexBuffer> vertex_buffers_;
164  ResourcesContainer<filament::IndexBuffer> index_buffers_;
165 
166  // Stores dependent resources, which should be deallocated when
167  // resource referred by map key is deallocated.
168  // WARNING: Don't put in dependent list resources which are available
169  // publicly
170  std::unordered_map<REHandle_abstract, std::unordered_set<REHandle_abstract>>
171  dependencies_;
172 
173  // Cache for GPU
174  std::unordered_map<uint64_t, TextureHandle> texture_cache_;
175 
176  filament::Texture* LoadTextureFromImage(
177  const std::shared_ptr<geometry::Image>& image, bool srgb);
178  filament::Texture* LoadTextureFromImage(const t::geometry::Image& image,
179  bool srgb);
180  filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
181  size_t dimension);
182 
183  void LoadDefaults();
184 };
185 
186 } // namespace rendering
187 } // namespace visualization
188 } // namespace open3d
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:183
math::float4 color
Definition: LineSetBuffers.cpp:45
std::vector< UVAtlasVertex > vb
Definition: UVUnwrapping.cpp:27
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:34
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:29
Definition: FilamentResourceManager.h:50
std::weak_ptr< filament::RenderTarget > GetRenderTarget(const RenderTargetHandle &id)
Definition: FilamentResourceManager.cpp:769
static const MaterialInstanceHandle kNormalsMaterial
Definition: FilamentResourceManager.h:67
void DestroyAll()
Definition: FilamentResourceManager.cpp:794
static const MaterialHandle kInfinitePlaneShader
Definition: FilamentResourceManager.h:63
std::weak_ptr< filament::IndexBuffer > GetIndexBuffer(const IndexBufferHandle &id)
Definition: FilamentResourceManager.cpp:789
static const TextureHandle kDefaultColorMap
Definition: FilamentResourceManager.h:70
VertexBufferHandle AddVertexBuffer(filament::VertexBuffer *vertex_buffer)
Definition: FilamentResourceManager.cpp:718
SkyboxHandle CreateColorSkybox(const Eigen::Vector3f &color)
Definition: FilamentResourceManager.cpp:659
static const TextureHandle kDefaultNormalMap
Definition: FilamentResourceManager.h:71
RenderTargetHandle CreateRenderTarget(TextureHandle color, TextureHandle depth)
Definition: FilamentResourceManager.cpp:582
~FilamentResourceManager()
Definition: FilamentResourceManager.cpp:383
std::weak_ptr< filament::Material > GetMaterial(const MaterialHandle &id)
Definition: FilamentResourceManager.cpp:754
IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest &request)
Definition: FilamentResourceManager.cpp:604
std::weak_ptr< filament::Skybox > GetSkybox(const SkyboxHandle &id)
Definition: FilamentResourceManager.cpp:779
FilamentResourceManager(filament::Engine &engine)
Definition: FilamentResourceManager.cpp:378
static const MaterialHandle kDefaultLineShader
Definition: FilamentResourceManager.h:64
static const MaterialHandle kDefaultUnlitBackgroundShader
Definition: FilamentResourceManager.h:62
static const MaterialHandle kDefaultDepthShader
Definition: FilamentResourceManager.h:58
static const MaterialHandle kDefaultUnlit
Definition: FilamentResourceManager.h:55
static const MaterialHandle kDefaultUnlitSolidColorShader
Definition: FilamentResourceManager.h:61
static const MaterialHandle kDefaultUnlitGradientShader
Definition: FilamentResourceManager.h:60
TextureHandle CreateColorAttachmentTexture(int width, int height)
Definition: FilamentResourceManager.cpp:551
static const MaterialHandle kDefaultUnlitPolygonOffsetShader
Definition: FilamentResourceManager.h:65
IndexBufferHandle CreateIndexBuffer(size_t indices_count, size_t index_stride)
Definition: FilamentResourceManager.cpp:733
void ReuseVertexBuffer(VertexBufferHandle vb)
Definition: FilamentResourceManager.cpp:724
TextureHandle CreateDepthAttachmentTexture(int width, int height)
Definition: FilamentResourceManager.cpp:567
std::weak_ptr< filament::MaterialInstance > GetMaterialInstance(const MaterialInstanceHandle &id)
Definition: FilamentResourceManager.cpp:760
static const MaterialHandle kDefaultDepthValueShader
Definition: FilamentResourceManager.h:59
MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle &id)
Definition: FilamentResourceManager.cpp:428
static const MaterialHandle kDefaultUnlitWithTransparency
Definition: FilamentResourceManager.h:56
TextureHandle CreateTexture(const char *path, bool srgb)
Definition: FilamentResourceManager.cpp:441
static const MaterialHandle kDefaultNormalShader
Definition: FilamentResourceManager.h:57
static const MaterialHandle kDefaultLitSSR
Definition: FilamentResourceManager.h:54
static const MaterialInstanceHandle kColorMapMaterial
Definition: FilamentResourceManager.h:68
static const MaterialInstanceHandle kDepthMaterial
Definition: FilamentResourceManager.h:66
SkyboxHandle CreateSkybox(const ResourceLoadRequest &request)
Definition: FilamentResourceManager.cpp:673
std::weak_ptr< filament::Texture > GetTexture(const TextureHandle &id)
Definition: FilamentResourceManager.cpp:764
MaterialHandle CreateMaterial(const void *material_data, size_t data_size)
Definition: FilamentResourceManager.cpp:385
static const TextureHandle kDefaultTexture
Definition: FilamentResourceManager.h:69
TextureHandle CreateTextureFilled(const Eigen::Vector3f &color, size_t dimension)
Definition: FilamentResourceManager.cpp:501
bool UpdateTexture(TextureHandle texture, const std::shared_ptr< geometry::Image > image, bool srgb)
Definition: FilamentResourceManager.cpp:510
void Destroy(const REHandle_abstract &id)
Definition: FilamentResourceManager.cpp:806
static const MaterialHandle kDefaultLitWithTransparency
Definition: FilamentResourceManager.h:53
std::weak_ptr< filament::VertexBuffer > GetVertexBuffer(const VertexBufferHandle &id)
Definition: FilamentResourceManager.cpp:784
std::weak_ptr< filament::IndirectLight > GetIndirectLight(const IndirectLightHandle &id)
Definition: FilamentResourceManager.cpp:775
static const MaterialHandle kDefaultLit
Definition: FilamentResourceManager.h:52
int width
Definition: FilePCD.cpp:52
int height
Definition: FilePCD.cpp:53
Definition: FilamentEngine.h:12
Definition: PinholeCameraIntrinsic.cpp:16
std::shared_ptr< ResourceType > operator->()
Definition: FilamentResourceManager.h:147
size_t use_count
Definition: FilamentResourceManager.h:142
std::shared_ptr< ResourceType > ptr
Definition: FilamentResourceManager.h:141
BoxedResource(std::shared_ptr< ResourceType > p)
Definition: FilamentResourceManager.h:145
Definition: RendererHandle.h:90