From 8fc69113c9d91a7fd64948787488e8f742962b3a Mon Sep 17 00:00:00 2001 From: nvsickle Date: Mon, 19 Apr 2021 15:08:06 -0700 Subject: [PATCH 01/10] Get the default viewport context on demand in FFont, as it may change --- .../AtomLyIntegration/AtomFont/FFont.h | 31 ++--------------- .../AtomFont/Code/Source/FFont.cpp | 33 +++++++++++++------ 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h b/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h index 96f5e09fc3..a6066984c7 100644 --- a/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h +++ b/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h @@ -277,11 +277,11 @@ namespace AZ void ScaleCoord(const RHI::Viewport& viewport, float& x, float& y) const; - void InitDefaultWindowContext(); - void InitDefaultViewportContext(); - void OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene) override; + RPI::WindowContextSharedPtr GetDefaultWindowContext() const; + RPI::ViewportContextPtr GetDefaultViewportContext() const; + private: static constexpr uint32_t NumBuffers = 2; static constexpr float WindowScaleWidth = 800.0f; @@ -294,9 +294,6 @@ namespace AZ size_t m_fontBufferSize = 0; unsigned char* m_fontBuffer = nullptr; - AZStd::shared_ptr m_defaultWindowContext; - AZStd::shared_ptr m_defaultViewportContext; - AZ::Data::Instance m_fontStreamingImage; AZ::RHI::Ptr m_fontImage; uint32_t m_fontImageVersion = 0; @@ -345,26 +342,4 @@ namespace AZ } } -inline void AZ::FFont::InitDefaultWindowContext() -{ - if (!m_defaultWindowContext) - { - // font is created before window & viewport in the editor so need to do late init - // TODO need to deal with multiple windows, such as the editor - AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_defaultWindowContext, &AZ::Render::Bootstrap::DefaultWindowInterface::GetDefaultWindowContext); - AZ_Assert(m_defaultWindowContext, "Unable to get the main window context"); - } -} - -inline void AZ::FFont::InitDefaultViewportContext() -{ - if (!m_defaultViewportContext) - { - // font is created before window & viewport in the editor so need to do late init - auto viewContextManager = AZ::Interface::Get(); - m_defaultViewportContext = viewContextManager->GetViewportContextByName(viewContextManager->GetDefaultViewportContextName()); - AZ_Assert(m_defaultViewportContext, "Unable to get the viewport context"); - } -} - #endif diff --git a/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp b/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp index d32302a07b..38024393fa 100644 --- a/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp +++ b/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp @@ -84,6 +84,20 @@ AZ::FFont::FFont(AtomFont* atomFont, const char* fontName) AZ::Render::Bootstrap::NotificationBus::Handler::BusConnect(); } +AZ::RPI::ViewportContextPtr AZ::FFont::GetDefaultViewportContext() const +{ + auto viewContextManager = AZ::Interface::Get(); + return viewContextManager->GetDefaultViewportContext(); +} + +AZ::RPI::WindowContextSharedPtr AZ::FFont::GetDefaultWindowContext() const +{ + if (auto defaultViewportContext = GetDefaultViewportContext()) + { + return defaultViewportContext->GetWindowContext(); + } + return {}; +} bool AZ::FFont::InitFont() { @@ -92,11 +106,8 @@ bool AZ::FFont::InitFont() return true; } - InitDefaultWindowContext(); - InitDefaultViewportContext(); - // Create and initialize DynamicDrawContext for font draw - AZ::RPI::Ptr dynamicDraw = m_atomFont->GetOrCreateDynamicDrawForScene(m_defaultViewportContext->GetRenderScene().get()); + AZ::RPI::Ptr dynamicDraw = m_atomFont->GetOrCreateDynamicDrawForScene(GetDefaultViewportContext()->GetRenderScene().get()); // Save draw srg input indices for later use Data::Instance drawSrg = dynamicDraw->NewDrawSrg(); @@ -259,7 +270,7 @@ void AZ::FFont::DrawString(float x, float y, const char* str, const bool asciiMu return; } - DrawStringUInternal(m_defaultWindowContext->GetViewport(), m_defaultViewportContext.get(), x, y, 1.0f, str, asciiMultiLine, ctx); + DrawStringUInternal(GetDefaultWindowContext()->GetViewport(), GetDefaultViewportContext().get(), x, y, 1.0f, str, asciiMultiLine, ctx); } void AZ::FFont::DrawString(float x, float y, float z, const char* str, const bool asciiMultiLine, const TextDrawContext& ctx) @@ -269,7 +280,7 @@ void AZ::FFont::DrawString(float x, float y, float z, const char* str, const boo return; } - DrawStringUInternal(m_defaultWindowContext->GetViewport(), m_defaultViewportContext.get(), x, y, z, str, asciiMultiLine, ctx); + DrawStringUInternal(GetDefaultWindowContext()->GetViewport(), GetDefaultViewportContext().get(), x, y, z, str, asciiMultiLine, ctx); } void AZ::FFont::DrawStringUInternal( @@ -282,6 +293,8 @@ void AZ::FFont::DrawStringUInternal( const bool asciiMultiLine, const TextDrawContext& ctx) { + InitFont(); + if (!str || !m_vertexBuffer // vertex buffer isn't created until BootstrapScene is ready, Editor tries to render text before that. || !m_fontTexture @@ -400,7 +413,7 @@ Vec2 AZ::FFont::GetTextSize(const char* str, const bool asciiMultiLine, const Te return Vec2(0.0f, 0.0f); } - return GetTextSizeUInternal(m_defaultWindowContext->GetViewport(), str, asciiMultiLine, ctx); + return GetTextSizeUInternal(GetDefaultWindowContext()->GetViewport(), str, asciiMultiLine, ctx); } Vec2 AZ::FFont::GetTextSizeUInternal( @@ -746,7 +759,7 @@ uint32_t AZ::FFont::WriteTextQuadsToBuffers(SVF_P2F_C4B_T2F_F4B* verts, uint16_t return true; }; - CreateQuadsForText(m_defaultWindowContext->GetViewport(), x, y, z, str, asciiMultiLine, ctx, AddQuad); + CreateQuadsForText(GetDefaultWindowContext()->GetViewport(), x, y, z, str, asciiMultiLine, ctx, AddQuad); return numQuadsWritten; } @@ -1438,7 +1451,7 @@ void AZ::FFont::AddCharsToFontTexture(const char* chars, int glyphSizeX, int gly Vec2 AZ::FFont::GetKerning(uint32_t leftGlyph, uint32_t rightGlyph, const TextDrawContext& ctx) const { - return GetKerningInternal(m_defaultWindowContext->GetViewport(), leftGlyph, rightGlyph, ctx); + return GetKerningInternal(GetDefaultWindowContext()->GetViewport(), leftGlyph, rightGlyph, ctx); } Vec2 AZ::FFont::GetKerningInternal(const RHI::Viewport& viewport, uint32_t leftGlyph, uint32_t rightGlyph, const TextDrawContext& ctx) const @@ -1454,7 +1467,7 @@ float AZ::FFont::GetAscender(const TextDrawContext& ctx) const float AZ::FFont::GetBaseline(const TextDrawContext& ctx) const { - return GetBaselineInternal(m_defaultWindowContext->GetViewport(), ctx); + return GetBaselineInternal(GetDefaultWindowContext()->GetViewport(), ctx); } float AZ::FFont::GetBaselineInternal(const RHI::Viewport& viewport, const TextDrawContext& ctx) const From c05d4b44e4789eafdad6168bba28e46870d257cd Mon Sep 17 00:00:00 2001 From: nvsickle Date: Mon, 19 Apr 2021 15:08:40 -0700 Subject: [PATCH 02/10] Move supplemental EditorViewportWidget rendering to OnBeginPrepareRender to avoid sync issues --- Code/Sandbox/Editor/EditorViewportWidget.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Code/Sandbox/Editor/EditorViewportWidget.cpp b/Code/Sandbox/Editor/EditorViewportWidget.cpp index 695a0fa5f1..897b3c569d 100644 --- a/Code/Sandbox/Editor/EditorViewportWidget.cpp +++ b/Code/Sandbox/Editor/EditorViewportWidget.cpp @@ -276,9 +276,6 @@ void EditorViewportWidget::paintEvent([[maybe_unused]] QPaintEvent* event) if ((ge && ge->IsLevelLoaded()) || (GetType() != ET_ViewportCamera)) { setRenderOverlayVisible(true); - m_isOnPaint = true; - Update(); - m_isOnPaint = false; } else { @@ -809,6 +806,10 @@ void EditorViewportWidget::OnBeginPrepareRender() return; } + m_isOnPaint = true; + Update(); + m_isOnPaint = false; + float fNearZ = GetIEditor()->GetConsoleVar("cl_DefaultNearPlane"); float fFarZ = m_Camera.GetFarPlane(); From 482e423ec9294a416697a37f0f08054814963b55 Mon Sep 17 00:00:00 2001 From: nvsickle Date: Fri, 16 Apr 2021 12:00:13 -0700 Subject: [PATCH 03/10] Fix crash on default layout restore --- Code/Sandbox/Editor/LayoutWnd.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Code/Sandbox/Editor/LayoutWnd.cpp b/Code/Sandbox/Editor/LayoutWnd.cpp index 56c0bcb849..dc3ae14d90 100644 --- a/Code/Sandbox/Editor/LayoutWnd.cpp +++ b/Code/Sandbox/Editor/LayoutWnd.cpp @@ -418,8 +418,9 @@ void CLayoutWnd::CreateLayout(EViewLayout layout, bool bBindViewports, EViewport QRect rcView = rect(); rcView.setBottom(rcView.bottom() - m_infoBar->height()); + // Ensure we delete our old view immediately so it can relinquish its backing ViewportContext if (m_maximizedView) - m_maximizedView->deleteLater(); + delete m_maximizedView; m_maximizedView = new CLayoutViewPane(this); m_maximizedView->SetId(0); From d2fadcb0e37bc78b035e9b0cb3c37fa960a7b190 Mon Sep 17 00:00:00 2001 From: nvsickle Date: Fri, 16 Apr 2021 12:02:55 -0700 Subject: [PATCH 04/10] Fix context menu handling in multi-viewport scenarios (the logic bugs here were many and nuanced, but we're narrowing in on something robust). Specifically this: -Ensures key/mouse up event propagation works across multiple viewports -Ensures that mouse up events for manipulators only get delivered if there's a corresponding mouse down event -Also tidies up the "are we done processing events this tick?" logic in ViewportManipulatorController --- .../Editor/ViewportManipulatorController.cpp | 26 +++++++++++++++---- .../Source/Viewport/RenderViewportWidget.cpp | 19 +++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Code/Sandbox/Editor/ViewportManipulatorController.cpp b/Code/Sandbox/Editor/ViewportManipulatorController.cpp index 910d037670..8ce2ea1cd9 100644 --- a/Code/Sandbox/Editor/ViewportManipulatorController.cpp +++ b/Code/Sandbox/Editor/ViewportManipulatorController.cpp @@ -95,6 +95,11 @@ bool ViewportManipulatorControllerInstance::HandleInputChannelEvent(const AzFram AZStd::optional overrideButton; AZStd::optional eventType; + // Because we receive events multiple times at separate priorities for manipulator events and + // viewport interaction events, we want to avoid updating our "last tick state" until we're on our last event, + // which currently is the low priority Interaction processor. + const bool finishedProcessingEvents = event.m_priority == InteractionPriority; + if (IsMouseMove(event.m_inputChannel)) { // Cache the ray trace results when doing manipulator interaction checks, no need to recalculate after @@ -120,10 +125,11 @@ bool ViewportManipulatorControllerInstance::HandleInputChannelEvent(const AzFram } else if (auto mouseButton = GetMouseButton(event.m_inputChannel); mouseButton != MouseButton::None) { + const AZ::u32 mouseButtonValue = static_cast(mouseButton); overrideButton = mouseButton; if (event.m_inputChannel.GetState() == InputChannel::State::Began) { - m_state.m_mouseButtons.m_mouseButtons |= static_cast(mouseButton); + m_state.m_mouseButtons.m_mouseButtons |= mouseButtonValue; if (IsDoubleClick(mouseButton)) { // Only remove the double click flag once we're done processing both Manipulator and Interaction events @@ -135,8 +141,8 @@ bool ViewportManipulatorControllerInstance::HandleInputChannelEvent(const AzFram } else { - // Only insert the double click timing once we're done processing both Manipulator and Interaction events, to avoid a false IsDoubleClick positive - if (event.m_priority == InteractionPriority) + // Only insert the double click timing once we're done processing events, to avoid a false IsDoubleClick positive + if (finishedProcessingEvents) { m_pendingDoubleClicks[mouseButton] = m_curTime; } @@ -145,8 +151,18 @@ bool ViewportManipulatorControllerInstance::HandleInputChannelEvent(const AzFram } else if (event.m_inputChannel.GetState() == InputChannel::State::Ended) { - m_state.m_mouseButtons.m_mouseButtons &= ~static_cast(mouseButton); - eventType = MouseEvent::Up; + // If we've actually logged a mouse down event, forward a mouse up event. + // This prevents corner cases like the context menu thinking it should be opened even though no one clicked in this viewport, + // due to RenderViewportWidget ensuring all controllers get InputChannel::State::Ended events. + if (m_state.m_mouseButtons.m_mouseButtons & mouseButtonValue) + { + // Erase the button from our state if we're done processing events. + if (event.m_priority == InteractionPriority) + { + m_state.m_mouseButtons.m_mouseButtons &= ~mouseButtonValue; + } + eventType = MouseEvent::Up; + } } } else if (auto keyboardModifier = GetKeyboardModifier(event.m_inputChannel); keyboardModifier != KeyboardModifier::None) diff --git a/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp b/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp index e08a5a045c..9ea2144289 100644 --- a/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp +++ b/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp @@ -164,6 +164,8 @@ namespace AtomToolsFramework bool RenderViewportWidget::OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) { + bool shouldConsumeEvent = true; + // Grab keyboard focus if we've been clicked on. // Qt normally handles this for us, but we're filtering native events before they get // synthesized into QMouseEvents. @@ -175,9 +177,18 @@ namespace AtomToolsFramework // Don't consume new input events if we don't currently have focus. // We do forward Ended events, as they may be relevant to our current state // (e.g. a key gets released after we lose focus, it shouldn't remain "stuck"). - if (!hasFocus() && inputChannel.GetState() != AzFramework::InputChannel::State::Ended) + if (!hasFocus()) { - return false; + if (inputChannel.GetState() == AzFramework::InputChannel::State::Ended) + { + // Forward the input ended event to our controllers, but don't prevent other viewports from receiving it. + shouldConsumeEvent = false; + } + else + { + // Not an event we should listen to, abort + return false; + } } // If we receive a mouse button event from outside of our viewport, ignore it even if we have focus. @@ -196,7 +207,9 @@ namespace AtomToolsFramework } AzFramework::NativeWindowHandle windowId = reinterpret_cast(winId()); - return m_controllerList->HandleInputChannelEvent({GetId(), windowId, inputChannel}); + const bool eventHandled = m_controllerList->HandleInputChannelEvent({GetId(), windowId, inputChannel}); + // If our controllers handled the event and it's one we can safely consume (i.e. it's not an Ended event that other viewports might need), consume it. + return eventHandled && shouldConsumeEvent; } void RenderViewportWidget::OnTick([[maybe_unused]]float deltaTime, AZ::ScriptTimePoint time) From dd7334471f9920b6312738c5abcf9d5fc37e0b1d Mon Sep 17 00:00:00 2001 From: nvsickle Date: Fri, 16 Apr 2021 12:17:35 -0700 Subject: [PATCH 05/10] Fix comment punctuation --- .../Code/Source/Viewport/RenderViewportWidget.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp b/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp index 9ea2144289..bf46ece27b 100644 --- a/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp +++ b/Gems/Atom/Tools/AtomToolsFramework/Code/Source/Viewport/RenderViewportWidget.cpp @@ -186,7 +186,7 @@ namespace AtomToolsFramework } else { - // Not an event we should listen to, abort + // Not an event we should listen to, abort. return false; } } From ebf41d2bdf58dcc3f7899bf94cf84e380c0f8428 Mon Sep 17 00:00:00 2001 From: nvsickle Date: Fri, 16 Apr 2021 14:26:58 -0700 Subject: [PATCH 06/10] Update code style --- Code/Sandbox/Editor/LayoutWnd.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Code/Sandbox/Editor/LayoutWnd.cpp b/Code/Sandbox/Editor/LayoutWnd.cpp index dc3ae14d90..34a96bca53 100644 --- a/Code/Sandbox/Editor/LayoutWnd.cpp +++ b/Code/Sandbox/Editor/LayoutWnd.cpp @@ -420,7 +420,9 @@ void CLayoutWnd::CreateLayout(EViewLayout layout, bool bBindViewports, EViewport // Ensure we delete our old view immediately so it can relinquish its backing ViewportContext if (m_maximizedView) + { delete m_maximizedView; + } m_maximizedView = new CLayoutViewPane(this); m_maximizedView->SetId(0); From e2a76299938d5e7a2f942917e434f9f4f2f8cb3f Mon Sep 17 00:00:00 2001 From: nvsickle Date: Mon, 19 Apr 2021 15:24:08 -0700 Subject: [PATCH 07/10] Remove statistics rendering from EditorViewportWidget - it's wrong at the moment, and needs to be moved to a controller --- Code/Sandbox/Editor/EditorViewportWidget.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Code/Sandbox/Editor/EditorViewportWidget.cpp b/Code/Sandbox/Editor/EditorViewportWidget.cpp index 897b3c569d..3c714b1d4c 100644 --- a/Code/Sandbox/Editor/EditorViewportWidget.cpp +++ b/Code/Sandbox/Editor/EditorViewportWidget.cpp @@ -906,11 +906,6 @@ void EditorViewportWidget::OnBeginPrepareRender() m_debugDisplay->DepthTestOn(); PostWidgetRendering(); - - if (!m_renderer->IsStereoEnabled()) - { - GetIEditor()->GetSystem()->RenderStatistics(); - } } ////////////////////////////////////////////////////////////////////////// From 6aabf2ee3db06bc56725b7cecea2e45f4d508621 Mon Sep 17 00:00:00 2001 From: nvsickle Date: Mon, 19 Apr 2021 15:36:25 -0700 Subject: [PATCH 08/10] Don't attempt to render manipulators in game mode --- Code/Sandbox/Editor/EditorViewportWidget.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Code/Sandbox/Editor/EditorViewportWidget.cpp b/Code/Sandbox/Editor/EditorViewportWidget.cpp index 3c714b1d4c..91c70b720f 100644 --- a/Code/Sandbox/Editor/EditorViewportWidget.cpp +++ b/Code/Sandbox/Editor/EditorViewportWidget.cpp @@ -881,6 +881,11 @@ void EditorViewportWidget::OnBeginPrepareRender() GetIEditor()->GetSystem()->SetViewCamera(m_Camera); + if (GetIEditor()->IsInGameMode()) + { + return; + } + PreWidgetRendering(); RenderAll(); From dee0f8470448c5ce4d60944f0db6df091e17c3f0 Mon Sep 17 00:00:00 2001 From: nvsickle Date: Mon, 19 Apr 2021 17:00:36 -0700 Subject: [PATCH 09/10] Clarified lazy initialization and added some thread sanity logic after discussion with @rgba16f --- .../Include/AtomLyIntegration/AtomFont/FFont.h | 1 + .../AtomFont/Code/Source/FFont.cpp | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h b/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h index a6066984c7..91fc0834ec 100644 --- a/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h +++ b/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h @@ -302,6 +302,7 @@ namespace AZ bool m_fontTexDirty = false; bool m_fontInitialized = false; + AZStd::atomic_bool m_fontInitializing = false; FontEffects m_effects; diff --git a/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp b/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp index 38024393fa..edbf148940 100644 --- a/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp +++ b/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp @@ -106,6 +106,14 @@ bool AZ::FFont::InitFont() return true; } + // If we're being initialized in another thread, abort. + if (m_fontInitializing) + { + return false; + } + + m_fontInitializing = true; + // Create and initialize DynamicDrawContext for font draw AZ::RPI::Ptr dynamicDraw = m_atomFont->GetOrCreateDynamicDrawForScene(GetDefaultViewportContext()->GetRenderScene().get()); @@ -129,6 +137,7 @@ bool AZ::FFont::InitFont() m_indexCount = 0; m_fontInitialized = true; + m_fontInitializing = false; return true; } @@ -293,7 +302,11 @@ void AZ::FFont::DrawStringUInternal( const bool asciiMultiLine, const TextDrawContext& ctx) { - InitFont(); + // Lazily ensure we're initialized before attempting to render. + if (!InitFont()) + { + return; + } if (!str || !m_vertexBuffer // vertex buffer isn't created until BootstrapScene is ready, Editor tries to render text before that. From 33b485767f584bef006049ed3beb792325190830 Mon Sep 17 00:00:00 2001 From: nvsickle Date: Tue, 20 Apr 2021 11:49:59 -0700 Subject: [PATCH 10/10] Address reviewer feedback, make FFont initialization state an atomic state machine --- .../AtomLyIntegration/AtomFont/FFont.h | 9 +++++-- .../AtomFont/Code/Source/FFont.cpp | 24 ++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h b/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h index 91fc0834ec..8e60cc2055 100644 --- a/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h +++ b/Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/FFont.h @@ -301,8 +301,13 @@ namespace AZ AtomFont* m_atomFont = nullptr; bool m_fontTexDirty = false; - bool m_fontInitialized = false; - AZStd::atomic_bool m_fontInitializing = false; + enum class InitializationState : AZ::u8 + { + Uninitialized, + Initializing, + Initialized + }; + AZStd::atomic m_fontInitializationState = InitializationState::Uninitialized; FontEffects m_effects; diff --git a/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp b/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp index edbf148940..20879876e7 100644 --- a/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp +++ b/Gems/AtomLyIntegration/AtomFont/Code/Source/FFont.cpp @@ -101,19 +101,16 @@ AZ::RPI::WindowContextSharedPtr AZ::FFont::GetDefaultWindowContext() const bool AZ::FFont::InitFont() { - if (m_fontInitialized) + auto initializationState = InitializationState::Uninitialized; + // Do an atomic transition to Initializing if we're in the Uninitialized state. + // Otherwise, check the current state. + // If we're Initialized, there's no more work to be done, return true to indicate we're good to go. + // If we're Initializing (on another thread), return false to let the consumer know it's not safe for us to be used yet. + if (!m_fontInitializationState.compare_exchange_strong(initializationState, InitializationState::Initializing)) { - return true; + return initializationState == InitializationState::Initialized; } - // If we're being initialized in another thread, abort. - if (m_fontInitializing) - { - return false; - } - - m_fontInitializing = true; - // Create and initialize DynamicDrawContext for font draw AZ::RPI::Ptr dynamicDraw = m_atomFont->GetOrCreateDynamicDrawForScene(GetDefaultViewportContext()->GetRenderScene().get()); @@ -136,8 +133,7 @@ bool AZ::FFont::InitFont() m_vertexCount = 0; m_indexCount = 0; - m_fontInitialized = true; - m_fontInitializing = false; + m_fontInitializationState = InitializationState::Initialized; return true; } @@ -1522,7 +1518,7 @@ bool AZ::FFont::UpdateTexture() { using namespace AZ; - if (!m_fontInitialized || !m_fontImage) + if (m_fontInitializationState != InitializationState::Initialized || !m_fontImage) { return false; } @@ -1590,7 +1586,7 @@ void AZ::FFont::Prepare(const char* str, bool updateTexture, const AtomFont::Gly const bool rerenderGlyphs = m_sizeBehavior == SizeBehavior::Rerender; const AtomFont::GlyphSize usedGlyphSize = rerenderGlyphs ? glyphSize : AtomFont::defaultGlyphSize; bool texUpdateNeeded = m_fontTexture->PreCacheString(str, nullptr, m_sizeRatio, usedGlyphSize, m_fontHintParams) == 1 || m_fontTexDirty; - if (m_fontInitialized && updateTexture && texUpdateNeeded && m_fontImage) + if (m_fontInitializationState == InitializationState::Initialized && updateTexture && texUpdateNeeded && m_fontImage) { UpdateTexture(); m_fontTexDirty = false;