ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
FilamentResourceManager.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <ecvMesh.h>
11 
12 #include <memory>
13 #include <unordered_map>
14 #include <unordered_set>
15 
18 
20 namespace filament {
21 class Engine;
22 class IndexBuffer;
23 class IndirectLight;
24 class Material;
25 class MaterialInstance;
26 class Skybox;
27 class Texture;
28 class RenderTarget;
29 class VertexBuffer;
30 } // namespace filament
32 
33 namespace cloudViewer {
34 
35 namespace t {
36 namespace geometry {
37 class Image;
38 }
39 } // namespace t
40 
41 namespace geometry {
42 class Image;
43 }
44 
45 namespace visualization {
46 namespace rendering {
47 
48 // Centralized storage of allocated resources.
49 // Used for convenient access from various components of render.
50 // Owns all added resources.
52 public:
74 
75  explicit FilamentResourceManager(filament::Engine& engine);
77 
78  // \param materialData must remain valid for the duration of the call to
79  // CreateMaterial(), and may be freed afterwards.
80  MaterialHandle CreateMaterial(const void* material_data, size_t data_size);
83 
84  TextureHandle CreateTexture(const char* path, bool srgb);
85  TextureHandle CreateTexture(const std::shared_ptr<geometry::Image>& image,
86  bool srgb);
87  // Slow, will make copy of image data and free it after.
90  // Creates texture of size 'dimension' filled with color 'color'
91  TextureHandle CreateTextureFilled(const Eigen::Vector3f& color,
92  size_t dimension);
93  // Creates a texture for use as a color attachment to a RenderTarget
95  // Creates a texture for use as a depth attachment to a RenderTarget
97 
99  TextureHandle depth);
100 
101  // Replaces the contents of the texture with the image. Returns false if
102  // the image is not the same size of the texture.
103  bool UpdateTexture(TextureHandle texture,
104  const std::shared_ptr<geometry::Image> image,
105  bool srgb);
106  bool UpdateTexture(TextureHandle texture,
107  const t::geometry::Image& image,
108  bool srgb);
109 
111  SkyboxHandle CreateColorSkybox(const Eigen::Vector3f& color);
113 
114  // Since rendering uses not all CloudViewer geometry/filament features, we
115  // don't know which arguments pass to CreateVB(...). Thus creation of VB is
116  // managed by FilamentGeometryBuffersBuilder class
117  VertexBufferHandle AddVertexBuffer(filament::VertexBuffer* vertex_buffer);
119  IndexBufferHandle CreateIndexBuffer(size_t indices_count,
120  size_t index_stride);
121 
122  std::weak_ptr<filament::Material> GetMaterial(const MaterialHandle& id);
123  std::weak_ptr<filament::MaterialInstance> GetMaterialInstance(
124  const MaterialInstanceHandle& id);
125  std::weak_ptr<filament::Texture> GetTexture(const TextureHandle& id);
126  std::weak_ptr<filament::RenderTarget> GetRenderTarget(
127  const RenderTargetHandle& id);
128  std::weak_ptr<filament::IndirectLight> GetIndirectLight(
129  const IndirectLightHandle& id);
130  std::weak_ptr<filament::Skybox> GetSkybox(const SkyboxHandle& id);
131  std::weak_ptr<filament::VertexBuffer> GetVertexBuffer(
132  const VertexBufferHandle& id);
133  std::weak_ptr<filament::IndexBuffer> GetIndexBuffer(
134  const IndexBufferHandle& id);
135 
136  void DestroyAll();
137  void Destroy(const REHandle_abstract& id);
138 
139 public:
140  // Only public so that .cpp file can use this
141  template <class ResourceType>
142  struct BoxedResource {
143  std::shared_ptr<ResourceType> ptr;
144  size_t use_count = 0;
145 
147  BoxedResource(std::shared_ptr<ResourceType> p) : ptr(p), use_count(1) {}
148 
149  std::shared_ptr<ResourceType> operator->() { return ptr; }
150  };
151 
152 private:
153  filament::Engine& engine_;
154 
155  template <class ResourceType>
156  using ResourcesContainer =
157  std::unordered_map<REHandle_abstract, BoxedResource<ResourceType>>;
158 
159  ResourcesContainer<filament::MaterialInstance> material_instances_;
160  ResourcesContainer<filament::Material> materials_;
161  ResourcesContainer<filament::Texture> textures_;
162  ResourcesContainer<filament::RenderTarget> render_targets_;
163  ResourcesContainer<filament::IndirectLight> ibls_;
164  ResourcesContainer<filament::Skybox> skyboxes_;
165  ResourcesContainer<filament::VertexBuffer> vertex_buffers_;
166  ResourcesContainer<filament::IndexBuffer> index_buffers_;
167 
168  // Stores dependent resources, which should be deallocated when
169  // resource referred by map key is deallocated.
170  // WARNING: Don't put in dependent list resources which are available
171  // publicly
172  std::unordered_map<REHandle_abstract, std::unordered_set<REHandle_abstract>>
173  dependencies_;
174 
175  filament::Texture* LoadTextureFromImage(
176  const std::shared_ptr<geometry::Image>& image, bool srgb);
177  filament::Texture* LoadTextureFromImage(const t::geometry::Image& image,
178  bool srgb);
179  filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
180  size_t dimension);
181 
182  void LoadDefaults();
183 };
184 
185 } // namespace rendering
186 } // namespace visualization
187 } // namespace cloudViewer
std::shared_ptr< core::Tensor > image
int width
int height
math::float4 color
std::vector< UVAtlasVertex > vb
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:33
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:29
IndexBufferHandle CreateIndexBuffer(size_t indices_count, size_t index_stride)
TextureHandle CreateTextureFilled(const Eigen::Vector3f &color, size_t dimension)
std::weak_ptr< filament::Material > GetMaterial(const MaterialHandle &id)
std::weak_ptr< filament::IndexBuffer > GetIndexBuffer(const IndexBufferHandle &id)
std::weak_ptr< filament::IndirectLight > GetIndirectLight(const IndirectLightHandle &id)
bool UpdateTexture(TextureHandle texture, const std::shared_ptr< geometry::Image > image, bool srgb)
std::weak_ptr< filament::RenderTarget > GetRenderTarget(const RenderTargetHandle &id)
std::weak_ptr< filament::Texture > GetTexture(const TextureHandle &id)
std::weak_ptr< filament::Skybox > GetSkybox(const SkyboxHandle &id)
RenderTargetHandle CreateRenderTarget(TextureHandle color, TextureHandle depth)
std::weak_ptr< filament::VertexBuffer > GetVertexBuffer(const VertexBufferHandle &id)
std::weak_ptr< filament::MaterialInstance > GetMaterialInstance(const MaterialInstanceHandle &id)
SkyboxHandle CreateSkybox(const ResourceLoadRequest &request)
MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle &id)
IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest &request)
MaterialHandle CreateMaterial(const void *material_data, size_t data_size)
VertexBufferHandle AddVertexBuffer(filament::VertexBuffer *vertex_buffer)
static const std::string path
Definition: PointCloud.cpp:59
Generic file read and write utility for python interface.