Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.19.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 // Copyright (c) 2018-2024 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:
73 
74  explicit FilamentResourceManager(filament::Engine& engine);
76 
77  // \param materialData must remain valid for the duration of the call to
78  // CreateMaterial(), and may be freed afterwards.
79  MaterialHandle CreateMaterial(const void* material_data, size_t data_size);
82 
83  TextureHandle CreateTexture(const char* path, bool srgb);
84  TextureHandle CreateTexture(const std::shared_ptr<geometry::Image>& image,
85  bool srgb);
86  // Slow, will make copy of image data and free it after.
89  // Creates texture of size 'dimension' filled with color 'color'
90  TextureHandle CreateTextureFilled(const Eigen::Vector3f& color,
91  size_t dimension);
92  // Creates a texture for use as a color attachment to a RenderTarget
94  // Creates a texture for use as a depth attachment to a RenderTarget
96 
98  TextureHandle depth);
99 
100  // Replaces the contents of the texture with the image. Returns false if
101  // the image is not the same size of the texture.
102  bool UpdateTexture(TextureHandle texture,
103  const std::shared_ptr<geometry::Image> image,
104  bool srgb);
105  bool UpdateTexture(TextureHandle texture,
106  const t::geometry::Image& image,
107  bool srgb);
108 
110  SkyboxHandle CreateColorSkybox(const Eigen::Vector3f& color);
112 
113  // Since rendering uses not all Open3D geometry/filament features, we don't
114  // know which arguments pass to CreateVB(...). Thus creation of VB is
115  // managed by FilamentGeometryBuffersBuilder class
116  VertexBufferHandle AddVertexBuffer(filament::VertexBuffer* vertex_buffer);
118  IndexBufferHandle CreateIndexBuffer(size_t indices_count,
119  size_t index_stride);
120 
121  std::weak_ptr<filament::Material> GetMaterial(const MaterialHandle& id);
122  std::weak_ptr<filament::MaterialInstance> GetMaterialInstance(
123  const MaterialInstanceHandle& id);
124  std::weak_ptr<filament::Texture> GetTexture(const TextureHandle& id);
125  std::weak_ptr<filament::RenderTarget> GetRenderTarget(
126  const RenderTargetHandle& id);
127  std::weak_ptr<filament::IndirectLight> GetIndirectLight(
128  const IndirectLightHandle& id);
129  std::weak_ptr<filament::Skybox> GetSkybox(const SkyboxHandle& id);
130  std::weak_ptr<filament::VertexBuffer> GetVertexBuffer(
131  const VertexBufferHandle& id);
132  std::weak_ptr<filament::IndexBuffer> GetIndexBuffer(
133  const IndexBufferHandle& id);
134 
135  void DestroyAll();
136  void Destroy(const REHandle_abstract& id);
137 
138 public:
139  // Only public so that .cpp file can use this
140  template <class ResourceType>
141  struct BoxedResource {
142  std::shared_ptr<ResourceType> ptr;
143  size_t use_count = 0;
144 
146  BoxedResource(std::shared_ptr<ResourceType> p) : ptr(p), use_count(1) {}
147 
148  std::shared_ptr<ResourceType> operator->() { return ptr; }
149  };
150 
151 private:
152  filament::Engine& engine_;
153 
154  template <class ResourceType>
155  using ResourcesContainer =
156  std::unordered_map<REHandle_abstract, BoxedResource<ResourceType>>;
157 
158  ResourcesContainer<filament::MaterialInstance> material_instances_;
159  ResourcesContainer<filament::Material> materials_;
160  ResourcesContainer<filament::Texture> textures_;
161  ResourcesContainer<filament::RenderTarget> render_targets_;
162  ResourcesContainer<filament::IndirectLight> ibls_;
163  ResourcesContainer<filament::Skybox> skyboxes_;
164  ResourcesContainer<filament::VertexBuffer> vertex_buffers_;
165  ResourcesContainer<filament::IndexBuffer> index_buffers_;
166 
167  // Stores dependent resources, which should be deallocated when
168  // resource referred by map key is deallocated.
169  // WARNING: Don't put in dependent list resources which are available
170  // publicly
171  std::unordered_map<REHandle_abstract, std::unordered_set<REHandle_abstract>>
172  dependencies_;
173 
174  // Cache for GPU
175  std::unordered_map<uint64_t, TextureHandle> texture_cache_;
176 
177  filament::Texture* LoadTextureFromImage(
178  const std::shared_ptr<geometry::Image>& image, bool srgb);
179  filament::Texture* LoadTextureFromImage(const t::geometry::Image& image,
180  bool srgb);
181  filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
182  size_t dimension);
183 
184  void LoadDefaults();
185 };
186 
187 } // namespace rendering
188 } // namespace visualization
189 } // namespace open3d
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:183
math::float4 color
Definition: LineSetBuffers.cpp:45
double t
Definition: SurfaceReconstructionPoisson.cpp:172
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:771
static const MaterialInstanceHandle kNormalsMaterial
Definition: FilamentResourceManager.h:68
void DestroyAll()
Definition: FilamentResourceManager.cpp:796
static const MaterialHandle kInfinitePlaneShader
Definition: FilamentResourceManager.h:64
std::weak_ptr< filament::IndexBuffer > GetIndexBuffer(const IndexBufferHandle &id)
Definition: FilamentResourceManager.cpp:791
static const TextureHandle kDefaultColorMap
Definition: FilamentResourceManager.h:71
VertexBufferHandle AddVertexBuffer(filament::VertexBuffer *vertex_buffer)
Definition: FilamentResourceManager.cpp:720
SkyboxHandle CreateColorSkybox(const Eigen::Vector3f &color)
Definition: FilamentResourceManager.cpp:661
static const TextureHandle kDefaultNormalMap
Definition: FilamentResourceManager.h:72
static const MaterialHandle kGaussianSplatShader
Definition: FilamentResourceManager.h:53
RenderTargetHandle CreateRenderTarget(TextureHandle color, TextureHandle depth)
Definition: FilamentResourceManager.cpp:584
~FilamentResourceManager()
Definition: FilamentResourceManager.cpp:385
std::weak_ptr< filament::Material > GetMaterial(const MaterialHandle &id)
Definition: FilamentResourceManager.cpp:756
IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest &request)
Definition: FilamentResourceManager.cpp:606
std::weak_ptr< filament::Skybox > GetSkybox(const SkyboxHandle &id)
Definition: FilamentResourceManager.cpp:781
FilamentResourceManager(filament::Engine &engine)
Definition: FilamentResourceManager.cpp:380
static const MaterialHandle kDefaultLineShader
Definition: FilamentResourceManager.h:65
static const MaterialHandle kDefaultUnlitBackgroundShader
Definition: FilamentResourceManager.h:63
static const MaterialHandle kDefaultDepthShader
Definition: FilamentResourceManager.h:59
static const MaterialHandle kDefaultUnlit
Definition: FilamentResourceManager.h:56
static const MaterialHandle kDefaultUnlitSolidColorShader
Definition: FilamentResourceManager.h:62
static const MaterialHandle kDefaultUnlitGradientShader
Definition: FilamentResourceManager.h:61
TextureHandle CreateColorAttachmentTexture(int width, int height)
Definition: FilamentResourceManager.cpp:553
static const MaterialHandle kDefaultUnlitPolygonOffsetShader
Definition: FilamentResourceManager.h:66
IndexBufferHandle CreateIndexBuffer(size_t indices_count, size_t index_stride)
Definition: FilamentResourceManager.cpp:735
void ReuseVertexBuffer(VertexBufferHandle vb)
Definition: FilamentResourceManager.cpp:726
TextureHandle CreateDepthAttachmentTexture(int width, int height)
Definition: FilamentResourceManager.cpp:569
std::weak_ptr< filament::MaterialInstance > GetMaterialInstance(const MaterialInstanceHandle &id)
Definition: FilamentResourceManager.cpp:762
static const MaterialHandle kDefaultDepthValueShader
Definition: FilamentResourceManager.h:60
MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle &id)
Definition: FilamentResourceManager.cpp:430
static const MaterialHandle kDefaultUnlitWithTransparency
Definition: FilamentResourceManager.h:57
TextureHandle CreateTexture(const char *path, bool srgb)
Definition: FilamentResourceManager.cpp:443
static const MaterialHandle kDefaultNormalShader
Definition: FilamentResourceManager.h:58
static const MaterialHandle kDefaultLitSSR
Definition: FilamentResourceManager.h:55
static const MaterialInstanceHandle kColorMapMaterial
Definition: FilamentResourceManager.h:69
static const MaterialInstanceHandle kDepthMaterial
Definition: FilamentResourceManager.h:67
SkyboxHandle CreateSkybox(const ResourceLoadRequest &request)
Definition: FilamentResourceManager.cpp:675
std::weak_ptr< filament::Texture > GetTexture(const TextureHandle &id)
Definition: FilamentResourceManager.cpp:766
MaterialHandle CreateMaterial(const void *material_data, size_t data_size)
Definition: FilamentResourceManager.cpp:387
static const TextureHandle kDefaultTexture
Definition: FilamentResourceManager.h:70
TextureHandle CreateTextureFilled(const Eigen::Vector3f &color, size_t dimension)
Definition: FilamentResourceManager.cpp:503
bool UpdateTexture(TextureHandle texture, const std::shared_ptr< geometry::Image > image, bool srgb)
Definition: FilamentResourceManager.cpp:512
void Destroy(const REHandle_abstract &id)
Definition: FilamentResourceManager.cpp:808
static const MaterialHandle kDefaultLitWithTransparency
Definition: FilamentResourceManager.h:54
std::weak_ptr< filament::VertexBuffer > GetVertexBuffer(const VertexBufferHandle &id)
Definition: FilamentResourceManager.cpp:786
std::weak_ptr< filament::IndirectLight > GetIndirectLight(const IndirectLightHandle &id)
Definition: FilamentResourceManager.cpp:777
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:148
size_t use_count
Definition: FilamentResourceManager.h:143
std::shared_ptr< ResourceType > ptr
Definition: FilamentResourceManager.h:142
BoxedResource(std::shared_ptr< ResourceType > p)
Definition: FilamentResourceManager.h:146
Definition: RendererHandle.h:90