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.