Open3D (C++ API)  0.19.0
Loading...
Searching...
No Matches
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
19namespace filament {
20class Engine;
21class IndexBuffer;
22class IndirectLight;
23class Material;
25class Skybox;
26class Texture;
27class RenderTarget;
28class VertexBuffer;
29} // namespace filament
31
32namespace open3d {
33
34namespace t {
35namespace geometry {
36class Image;
37}
38} // namespace t
39
40namespace geometry {
41class Image;
42}
43
44namespace visualization {
45namespace rendering {
46
47// Centralized storage of allocated resources.
48// Used for convenient access from various components of render.
49// Owns all added resources.
51public:
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
96 // Creates a Filament texture that wraps an externally-owned GL texture.
97 // The caller retains ownership of the GL texture — Filament will not
98 // delete it. `format` is a Filament InternalFormat, `usage` is a
99 // combination of Filament TextureUsage flags.
100 TextureHandle CreateImportedTexture(std::uint32_t gl_handle,
101 int width,
102 int height,
103 int format,
104 int usage);
105
106#if defined(__APPLE__)
108 TextureHandle CreateImportedMTLTexture(std::uintptr_t mtl_texture,
109 int width,
110 int height,
111 int format,
112 int usage);
113#endif
114
116 TextureHandle depth);
120
121 // Replaces the contents of the texture with the image. Returns false if
122 // the image is not the same size of the texture.
123 bool UpdateTexture(TextureHandle texture,
124 const std::shared_ptr<geometry::Image> image,
125 bool srgb);
126 bool UpdateTexture(TextureHandle texture,
128 bool srgb);
129
131 SkyboxHandle CreateColorSkybox(const Eigen::Vector3f& color);
133
134 // Since rendering uses not all Open3D geometry/filament features, we don't
135 // know which arguments pass to CreateVB(...). Thus creation of VB is
136 // managed by FilamentGeometryBuffersBuilder class
137 VertexBufferHandle AddVertexBuffer(filament::VertexBuffer* vertex_buffer);
139 IndexBufferHandle CreateIndexBuffer(size_t indices_count,
140 size_t index_stride);
141
142 std::weak_ptr<filament::Material> GetMaterial(const MaterialHandle& id);
143 std::weak_ptr<filament::MaterialInstance> GetMaterialInstance(
144 const MaterialInstanceHandle& id);
145 std::weak_ptr<filament::Texture> GetTexture(const TextureHandle& id);
146 std::weak_ptr<filament::RenderTarget> GetRenderTarget(
147 const RenderTargetHandle& id);
148 std::weak_ptr<filament::IndirectLight> GetIndirectLight(
149 const IndirectLightHandle& id);
150 std::weak_ptr<filament::Skybox> GetSkybox(const SkyboxHandle& id);
151 std::weak_ptr<filament::VertexBuffer> GetVertexBuffer(
152 const VertexBufferHandle& id);
153 std::weak_ptr<filament::IndexBuffer> GetIndexBuffer(
154 const IndexBufferHandle& id);
155
156 void DestroyAll();
157 void Destroy(const REHandle_abstract& id);
158
159public:
160 // Only public so that .cpp file can use this
161 template <class ResourceType>
163 std::shared_ptr<ResourceType> ptr;
164 size_t use_count = 0;
165
167 BoxedResource(std::shared_ptr<ResourceType> p) : ptr(p), use_count(1) {}
168
169 std::shared_ptr<ResourceType> operator->() { return ptr; }
170 };
171
172private:
173 filament::Engine& engine_;
174
175 template <class ResourceType>
176 using ResourcesContainer =
177 std::unordered_map<REHandle_abstract, BoxedResource<ResourceType>>;
178
179 ResourcesContainer<filament::MaterialInstance> material_instances_;
180 ResourcesContainer<filament::Material> materials_;
181 ResourcesContainer<filament::Texture> textures_;
182 ResourcesContainer<filament::RenderTarget> render_targets_;
183 ResourcesContainer<filament::IndirectLight> ibls_;
184 ResourcesContainer<filament::Skybox> skyboxes_;
185 ResourcesContainer<filament::VertexBuffer> vertex_buffers_;
186 ResourcesContainer<filament::IndexBuffer> index_buffers_;
187
188 // Stores dependent resources, which should be deallocated when
189 // resource referred by map key is deallocated.
190 // WARNING: Don't put in dependent list resources which are available
191 // publicly
192 std::unordered_map<REHandle_abstract, std::unordered_set<REHandle_abstract>>
193 dependencies_;
194
195 // Cache for GPU
196 std::unordered_map<uint64_t, TextureHandle> texture_cache_;
197
198 filament::Texture* LoadTextureFromImage(
199 const std::shared_ptr<geometry::Image>& image, bool srgb);
200 filament::Texture* LoadTextureFromImage(const t::geometry::Image& image,
201 bool srgb);
202 filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
203 size_t dimension);
204
205 void LoadDefaults();
206};
207
208} // namespace rendering
209} // namespace visualization
210} // namespace open3d
std::shared_ptr< core::Tensor > image
Definition FilamentRenderer.cpp:328
filament::Texture::InternalFormat format
Definition FilamentResourceManager.cpp:202
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:819
static const MaterialInstanceHandle kNormalsMaterial
Definition FilamentResourceManager.h:67
void DestroyAll()
Definition FilamentResourceManager.cpp:844
RenderTargetHandle CreateColorOnlyRenderTarget(TextureHandle color)
Definition FilamentResourceManager.cpp:633
static const MaterialHandle kInfinitePlaneShader
Definition FilamentResourceManager.h:63
std::weak_ptr< filament::IndexBuffer > GetIndexBuffer(const IndexBufferHandle &id)
Definition FilamentResourceManager.cpp:839
static const TextureHandle kDefaultColorMap
Definition FilamentResourceManager.h:70
VertexBufferHandle AddVertexBuffer(filament::VertexBuffer *vertex_buffer)
Definition FilamentResourceManager.cpp:768
SkyboxHandle CreateColorSkybox(const Eigen::Vector3f &color)
Definition FilamentResourceManager.cpp:709
static const TextureHandle kDefaultNormalMap
Definition FilamentResourceManager.h:71
RenderTargetHandle CreateRenderTarget(TextureHandle color, TextureHandle depth)
Definition FilamentResourceManager.cpp:609
~FilamentResourceManager()
Definition FilamentResourceManager.cpp:390
std::weak_ptr< filament::Material > GetMaterial(const MaterialHandle &id)
Definition FilamentResourceManager.cpp:804
IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest &request)
Definition FilamentResourceManager.cpp:654
TextureHandle CreateImportedTexture(std::uint32_t gl_handle, int width, int height, int format, int usage)
Definition FilamentResourceManager.cpp:593
std::weak_ptr< filament::Skybox > GetSkybox(const SkyboxHandle &id)
Definition FilamentResourceManager.cpp:829
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:561
static const MaterialHandle kDefaultUnlitPolygonOffsetShader
Definition FilamentResourceManager.h:65
IndexBufferHandle CreateIndexBuffer(size_t indices_count, size_t index_stride)
Definition FilamentResourceManager.cpp:783
void ReuseVertexBuffer(VertexBufferHandle vb)
Definition FilamentResourceManager.cpp:774
TextureHandle CreateDepthAttachmentTexture(int width, int height)
Definition FilamentResourceManager.cpp:578
std::weak_ptr< filament::MaterialInstance > GetMaterialInstance(const MaterialInstanceHandle &id)
Definition FilamentResourceManager.cpp:810
static const MaterialHandle kDefaultDepthValueShader
Definition FilamentResourceManager.h:59
MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle &id)
Definition FilamentResourceManager.cpp:435
static const MaterialHandle kDefaultUnlitWithTransparency
Definition FilamentResourceManager.h:56
TextureHandle CreateTexture(const char *path, bool srgb)
Definition FilamentResourceManager.cpp:448
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:723
std::weak_ptr< filament::Texture > GetTexture(const TextureHandle &id)
Definition FilamentResourceManager.cpp:814
MaterialHandle CreateMaterial(const void *material_data, size_t data_size)
Definition FilamentResourceManager.cpp:392
static const TextureHandle kDefaultTexture
Definition FilamentResourceManager.h:69
TextureHandle CreateTextureFilled(const Eigen::Vector3f &color, size_t dimension)
Definition FilamentResourceManager.cpp:511
bool UpdateTexture(TextureHandle texture, const std::shared_ptr< geometry::Image > image, bool srgb)
Definition FilamentResourceManager.cpp:520
void Destroy(const REHandle_abstract &id)
Definition FilamentResourceManager.cpp:862
static const MaterialHandle kDefaultLitWithTransparency
Definition FilamentResourceManager.h:53
std::weak_ptr< filament::VertexBuffer > GetVertexBuffer(const VertexBufferHandle &id)
Definition FilamentResourceManager.cpp:834
std::weak_ptr< filament::IndirectLight > GetIndirectLight(const IndirectLightHandle &id)
Definition FilamentResourceManager.cpp:825
static const MaterialHandle kDefaultLit
Definition FilamentResourceManager.h:52
int width
Definition FilePCD.cpp:53
int height
Definition FilePCD.cpp:54
Definition FilamentEngine.h:15
Definition PinholeCameraIntrinsic.cpp:16
size_t use_count
Definition FilamentResourceManager.h:164
std::shared_ptr< ResourceType > operator->()
Definition FilamentResourceManager.h:169
std::shared_ptr< ResourceType > ptr
Definition FilamentResourceManager.h:163
BoxedResource(std::shared_ptr< ResourceType > p)
Definition FilamentResourceManager.h:167
Definition RendererHandle.h:90