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