From 5b8a37a75b809b34cc217be27e6162c3db94167d Mon Sep 17 00:00:00 2001 From: Guthrie Adams Date: Mon, 18 Oct 2021 18:21:50 -0500 Subject: [PATCH] Additional preview renderer checks Preventing multiple preview renderers from being created with multiple catalog loaded notifications Purging material preview images reaching a maximum number of stored images Purging material previews if entity gets destroyed Signed-off-by: Guthrie Adams --- .../PreviewRendererSystemComponent.cpp | 7 ++++-- .../EditorCommonFeaturesSystemComponent.cpp | 16 ++++++++---- .../EditorCommonFeaturesSystemComponent.h | 2 +- .../EditorMaterialSystemComponent.cpp | 25 ++++++++++++++++++- .../Material/EditorMaterialSystemComponent.h | 11 +++++++- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Gems/Atom/Tools/AtomToolsFramework/Code/Source/PreviewRenderer/PreviewRendererSystemComponent.cpp b/Gems/Atom/Tools/AtomToolsFramework/Code/Source/PreviewRenderer/PreviewRendererSystemComponent.cpp index f88adfc65d..fc5b31297a 100644 --- a/Gems/Atom/Tools/AtomToolsFramework/Code/Source/PreviewRenderer/PreviewRendererSystemComponent.cpp +++ b/Gems/Atom/Tools/AtomToolsFramework/Code/Source/PreviewRenderer/PreviewRendererSystemComponent.cpp @@ -63,8 +63,11 @@ namespace AtomToolsFramework void PreviewRendererSystemComponent::OnCatalogLoaded([[maybe_unused]] const char* catalogFile) { AZ::TickBus::QueueFunction([this](){ - m_previewRenderer.reset(aznew AtomToolsFramework::PreviewRenderer( - "PreviewRendererSystemComponent Preview Scene", "PreviewRendererSystemComponent Preview Pipeline")); + if (!m_previewRenderer) + { + m_previewRenderer.reset(aznew AtomToolsFramework::PreviewRenderer( + "PreviewRendererSystemComponent Preview Scene", "PreviewRendererSystemComponent Preview Pipeline")); + } }); } diff --git a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.cpp b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.cpp index 1718dde5d4..2bf428bd2d 100644 --- a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.cpp +++ b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.cpp @@ -216,11 +216,17 @@ namespace AZ using namespace LyIntegration; ThumbnailerRequestsBus::Broadcast( - &ThumbnailerRequests::RegisterThumbnailProvider, MAKE_TCACHE(SharedThumbnailCache), - ThumbnailContext::DefaultContext); + &ThumbnailerRequests::RegisterThumbnailProvider, MAKE_TCACHE(SharedThumbnailCache), ThumbnailContext::DefaultContext); - m_renderer = AZStd::make_unique(); - m_previewerFactory = AZStd::make_unique(); + if (!m_thumbnailRenderer) + { + m_thumbnailRenderer = AZStd::make_unique(); + } + + if (!m_previewerFactory) + { + m_previewerFactory = AZStd::make_unique(); + } } void EditorCommonFeaturesSystemComponent::TeardownThumbnails() @@ -232,7 +238,7 @@ namespace AZ &ThumbnailerRequests::UnregisterThumbnailProvider, SharedThumbnailCache::ProviderName, ThumbnailContext::DefaultContext); - m_renderer.reset(); + m_thumbnailRenderer.reset(); m_previewerFactory.reset(); } } // namespace Render diff --git a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.h b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.h index 8021770873..82bb93a808 100644 --- a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.h +++ b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/EditorCommonFeaturesSystemComponent.h @@ -78,7 +78,7 @@ namespace AZ AZStd::string m_atomLevelDefaultAssetPath{ "LevelAssets/default.slice" }; float m_envProbeHeight{ 200.0f }; - AZStd::unique_ptr m_renderer; + AZStd::unique_ptr m_thumbnailRenderer; AZStd::unique_ptr m_previewerFactory; }; } // namespace Render diff --git a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.cpp b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.cpp index 5df17a5478..ea59c20ab3 100644 --- a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.cpp +++ b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.cpp @@ -185,7 +185,7 @@ namespace AZ materialAssignmentId); previewRenderer->AddCaptureRequest( - { 128, + { m_materialPreviewResolution, AZStd::make_shared( previewRenderer->GetScene(), previewRenderer->GetView(), previewRenderer->GetEntityContextId(), AZ::RPI::AssetUtils::GetAssetIdForProductPath(DefaultModelPath), materialAssetId, @@ -220,11 +220,17 @@ namespace AZ return QPixmap(); } + void EditorMaterialSystemComponent::OnEntityDestroyed(const AZ::EntityId& entityId) + { + m_materialPreviews.erase(entityId); + } + void EditorMaterialSystemComponent::OnRenderMaterialPreviewComplete( [[maybe_unused]] const AZ::EntityId& entityId, [[maybe_unused]] const AZ::Render::MaterialAssignmentId& materialAssignmentId, [[maybe_unused]] const QPixmap& pixmap) { + PurgePreviews(); m_materialPreviews[entityId][materialAssignmentId] = pixmap; } @@ -281,5 +287,22 @@ namespace AZ } return AzToolsFramework::AssetBrowser::SourceFileDetails(); } + + void EditorMaterialSystemComponent::PurgePreviews() + { + size_t materialPreviewCount = 0; + for (const auto& [entityId, previews] : m_materialPreviews) + { + for (const auto& [assignmentId, images] : previews) + { + materialPreviewCount += previews.size(); + } + } + + if (materialPreviewCount > m_materialPreviewLimit) + { + m_materialPreviews.clear(); + } + } } // namespace Render } // namespace AZ diff --git a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.h b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.h index 6fce538ead..e5778b1b9c 100644 --- a/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.h +++ b/Gems/AtomLyIntegration/CommonFeatures/Code/Source/Material/EditorMaterialSystemComponent.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,7 @@ namespace AZ //! System component that manages launching and maintaining connections with the material editor. class EditorMaterialSystemComponent final : public AZ::Component + , public AZ::EntitySystemBus::Handler , public EditorMaterialSystemComponentNotificationBus::Handler , public EditorMaterialSystemComponentRequestBus::Handler , public AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotificationBus::Handler @@ -54,9 +56,12 @@ namespace AZ QPixmap GetRenderedMaterialPreview( const AZ::EntityId& entityId, const AZ::Render::MaterialAssignmentId& materialAssignmentId) const override; + // AZ::EntitySystemBus::Handler overrides... + void OnEntityDestroyed(const AZ::EntityId& entityId) override; + //! EditorMaterialSystemComponentNotificationBus::Handler overrides... void OnRenderMaterialPreviewComplete( - const AZ::EntityId& entityId, const AZ::Render::MaterialAssignmentId& materialAssignmentId, const QPixmap& pixmap)override; + const AZ::EntityId& entityId, const AZ::Render::MaterialAssignmentId& materialAssignmentId, const QPixmap& pixmap) override; //! AssetBrowserInteractionNotificationBus::Handler overrides... AzToolsFramework::AssetBrowser::SourceFileDetails GetSourceFileDetails(const char* fullSourceFileName) override; @@ -68,9 +73,13 @@ namespace AZ // AztoolsFramework::EditorEvents::Bus::Handler overrides... void NotifyRegisterViews() override; + void PurgePreviews(); + QAction* m_openMaterialEditorAction = nullptr; AZStd::unique_ptr m_materialBrowserInteractions; AZStd::unordered_map> m_materialPreviews; + static constexpr const size_t m_materialPreviewLimit = 100; + static constexpr const int m_materialPreviewResolution = 128; }; } // namespace Render } // namespace AZ