// ---------------------------------------------------------------------------- // - Open3D: www.open3d.org - // ---------------------------------------------------------------------------- // Copyright (c) 2018-2023 www.open3d.org // SPDX-License-Identifier: MIT // ---------------------------------------------------------------------------- #pragma once #include #include #include #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 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& 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 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 GetMaterial(const MaterialHandle& id); std::weak_ptr GetMaterialInstance( const MaterialInstanceHandle& id); std::weak_ptr GetTexture(const TextureHandle& id); std::weak_ptr GetRenderTarget( const RenderTargetHandle& id); std::weak_ptr GetIndirectLight( const IndirectLightHandle& id); std::weak_ptr GetSkybox(const SkyboxHandle& id); std::weak_ptr GetVertexBuffer( const VertexBufferHandle& id); std::weak_ptr GetIndexBuffer( const IndexBufferHandle& id); void DestroyAll(); void Destroy(const REHandle_abstract& id); public: // Only public so that .cpp file can use this template struct BoxedResource { std::shared_ptr ptr; size_t use_count = 0; BoxedResource() {} BoxedResource(std::shared_ptr p) : ptr(p), use_count(1) {} std::shared_ptr operator->() { return ptr; } }; private: filament::Engine& engine_; template using ResourcesContainer = std::unordered_map>; ResourcesContainer material_instances_; ResourcesContainer materials_; ResourcesContainer textures_; ResourcesContainer render_targets_; ResourcesContainer ibls_; ResourcesContainer skyboxes_; ResourcesContainer vertex_buffers_; ResourcesContainer 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> dependencies_; // Cache for GPU std::unordered_map texture_cache_; filament::Texture* LoadTextureFromImage( const std::shared_ptr& 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