Open3D (C++ API)  0.16.0
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) 2018-2021 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
38namespace filament {
39class Engine;
40class IndexBuffer;
41class IndirectLight;
42class Material;
44class Skybox;
45class Texture;
46class RenderTarget;
47class VertexBuffer;
48} // namespace filament
50
51namespace open3d {
52
53namespace t {
54namespace geometry {
55class Image;
56}
57} // namespace t
58
59namespace geometry {
60class Image;
61}
62
63namespace visualization {
64namespace rendering {
65
66// Centralized storage of allocated resources.
67// Used for convenient access from various components of render.
68// Owns all added resources.
70public:
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);
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.
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
112 // Creates a texture for use as a depth attachment to a RenderTarget
114
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,
125 bool srgb);
126
128 SkyboxHandle CreateColorSkybox(const Eigen::Vector3f& color);
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);
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
156public:
157 // Only public so that .cpp file can use this
158 template <class ResourceType>
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
169private:
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 // Cache for GPU
193 std::unordered_map<uint64_t, TextureHandle> texture_cache_;
194
195 filament::Texture* LoadTextureFromImage(
196 const std::shared_ptr<geometry::Image>& image, bool srgb);
197 filament::Texture* LoadTextureFromImage(const t::geometry::Image& image,
198 bool srgb);
199 filament::Texture* LoadFilledTexture(const Eigen::Vector3f& color,
200 size_t dimension);
201
202 void LoadDefaults();
203};
204
205} // namespace rendering
206} // namespace visualization
207} // namespace open3d
std::shared_ptr< core::Tensor > image
Definition: FilamentRenderer.cpp:202
math::float4 color
Definition: LineSetBuffers.cpp:64
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:53
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
Definition: FilamentResourceManager.h:69
std::weak_ptr< filament::RenderTarget > GetRenderTarget(const RenderTargetHandle &id)
Definition: FilamentResourceManager.cpp:788
static const MaterialInstanceHandle kNormalsMaterial
Definition: FilamentResourceManager.h:86
void DestroyAll()
Definition: FilamentResourceManager.cpp:813
static const MaterialHandle kInfinitePlaneShader
Definition: FilamentResourceManager.h:82
std::weak_ptr< filament::IndexBuffer > GetIndexBuffer(const IndexBufferHandle &id)
Definition: FilamentResourceManager.cpp:808
static const TextureHandle kDefaultColorMap
Definition: FilamentResourceManager.h:89
VertexBufferHandle AddVertexBuffer(filament::VertexBuffer *vertex_buffer)
Definition: FilamentResourceManager.cpp:737
SkyboxHandle CreateColorSkybox(const Eigen::Vector3f &color)
Definition: FilamentResourceManager.cpp:678
static const TextureHandle kDefaultNormalMap
Definition: FilamentResourceManager.h:90
RenderTargetHandle CreateRenderTarget(TextureHandle color, TextureHandle depth)
Definition: FilamentResourceManager.cpp:601
~FilamentResourceManager()
Definition: FilamentResourceManager.cpp:402
std::weak_ptr< filament::Material > GetMaterial(const MaterialHandle &id)
Definition: FilamentResourceManager.cpp:773
IndirectLightHandle CreateIndirectLight(const ResourceLoadRequest &request)
Definition: FilamentResourceManager.cpp:623
std::weak_ptr< filament::Skybox > GetSkybox(const SkyboxHandle &id)
Definition: FilamentResourceManager.cpp:798
FilamentResourceManager(filament::Engine &engine)
Definition: FilamentResourceManager.cpp:397
static const MaterialHandle kDefaultLineShader
Definition: FilamentResourceManager.h:83
static const MaterialHandle kDefaultUnlitBackgroundShader
Definition: FilamentResourceManager.h:81
static const MaterialHandle kDefaultDepthShader
Definition: FilamentResourceManager.h:77
static const MaterialHandle kDefaultUnlit
Definition: FilamentResourceManager.h:74
static const MaterialHandle kDefaultUnlitSolidColorShader
Definition: FilamentResourceManager.h:80
static const MaterialHandle kDefaultUnlitGradientShader
Definition: FilamentResourceManager.h:79
TextureHandle CreateColorAttachmentTexture(int width, int height)
Definition: FilamentResourceManager.cpp:570
static const MaterialHandle kDefaultUnlitPolygonOffsetShader
Definition: FilamentResourceManager.h:84
IndexBufferHandle CreateIndexBuffer(size_t indices_count, size_t index_stride)
Definition: FilamentResourceManager.cpp:752
void ReuseVertexBuffer(VertexBufferHandle vb)
Definition: FilamentResourceManager.cpp:743
TextureHandle CreateDepthAttachmentTexture(int width, int height)
Definition: FilamentResourceManager.cpp:586
std::weak_ptr< filament::MaterialInstance > GetMaterialInstance(const MaterialInstanceHandle &id)
Definition: FilamentResourceManager.cpp:779
static const MaterialHandle kDefaultDepthValueShader
Definition: FilamentResourceManager.h:78
MaterialInstanceHandle CreateMaterialInstance(const MaterialHandle &id)
Definition: FilamentResourceManager.cpp:447
static const MaterialHandle kDefaultUnlitWithTransparency
Definition: FilamentResourceManager.h:75
TextureHandle CreateTexture(const char *path, bool srgb)
Definition: FilamentResourceManager.cpp:460
static const MaterialHandle kDefaultNormalShader
Definition: FilamentResourceManager.h:76
static const MaterialHandle kDefaultLitSSR
Definition: FilamentResourceManager.h:73
static const MaterialInstanceHandle kColorMapMaterial
Definition: FilamentResourceManager.h:87
static const MaterialInstanceHandle kDepthMaterial
Definition: FilamentResourceManager.h:85
SkyboxHandle CreateSkybox(const ResourceLoadRequest &request)
Definition: FilamentResourceManager.cpp:692
std::weak_ptr< filament::Texture > GetTexture(const TextureHandle &id)
Definition: FilamentResourceManager.cpp:783
MaterialHandle CreateMaterial(const void *material_data, size_t data_size)
Definition: FilamentResourceManager.cpp:404
static const TextureHandle kDefaultTexture
Definition: FilamentResourceManager.h:88
TextureHandle CreateTextureFilled(const Eigen::Vector3f &color, size_t dimension)
Definition: FilamentResourceManager.cpp:520
bool UpdateTexture(TextureHandle texture, const std::shared_ptr< geometry::Image > image, bool srgb)
Definition: FilamentResourceManager.cpp:529
void Destroy(const REHandle_abstract &id)
Definition: FilamentResourceManager.cpp:825
static const MaterialHandle kDefaultLitWithTransparency
Definition: FilamentResourceManager.h:72
std::weak_ptr< filament::VertexBuffer > GetVertexBuffer(const VertexBufferHandle &id)
Definition: FilamentResourceManager.cpp:803
std::weak_ptr< filament::IndirectLight > GetIndirectLight(const IndirectLightHandle &id)
Definition: FilamentResourceManager.cpp:794
static const MaterialHandle kDefaultLit
Definition: FilamentResourceManager.h:71
int width
Definition: FilePCD.cpp:71
int height
Definition: FilePCD.cpp:72
Definition: FilamentEngine.h:31
Definition: PinholeCameraIntrinsic.cpp:35
size_t use_count
Definition: FilamentResourceManager.h:161
std::shared_ptr< ResourceType > operator->()
Definition: FilamentResourceManager.h:166
std::shared_ptr< ResourceType > ptr
Definition: FilamentResourceManager.h:160
BoxedResource(std::shared_ptr< ResourceType > p)
Definition: FilamentResourceManager.h:164
Definition: RendererHandle.h:109