Files
o3de/Gems/Atom/RPI/Code/Source/RPI.Public/Shader/ShaderReloadDebugTracker.cpp
T
Chris Burel 0e885b8267 [Linux] Fix crash from accessing an invalid AZ::EnvironmentVariable
During asset processing, no `RPISystem` component is created, so nothing
so nothing calls `ShaderSystem::Init()`, so nothing calls
`ShaderReloadDebugTracker::Init()`. Consequently, the
AZ::EnvironmentVariables that are used during
`ShaderReloadDebugTracker::IsEnabled()` never got created, causing a read
from a nullptr at runtime. This fixes that issue by making `IsEnabled()`
call `CreateVariable()` on the variables it needs if they are not valid.

In addition, it changes the call to `CreateVariable()` to initialize the
variable's values directly, to ensure they are only initialized once. It
also switches to use `AZ::Crc32` so that the variable's id is computed at
compile time.

Signed-off-by: Chris Burel <burelc@amazon.com>
2021-11-05 11:38:41 -07:00

83 lines
2.6 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <Atom/RPI.Public/Shader/ShaderReloadDebugTracker.h>
#include <AzCore/Module/Environment.h>
namespace AZ
{
namespace RPI
{
namespace ShaderReloadDebugTrackerInternal
{
static constexpr char EnabledVariableName[] = "ShaderReloadDebugTracker enabled";
static constexpr char IndentVariableName[] = "ShaderReloadDebugTracker indent";
static EnvironmentVariable<bool> s_enabled;
static EnvironmentVariable<int> s_indent;
}
void ShaderReloadDebugTracker::Init()
{
MakeReady();
}
void ShaderReloadDebugTracker::Shutdown()
{
ShaderReloadDebugTrackerInternal::s_enabled.Reset();
ShaderReloadDebugTrackerInternal::s_indent.Reset();
}
void ShaderReloadDebugTracker::MakeReady()
{
if (!ShaderReloadDebugTrackerInternal::s_enabled.IsValid())
{
ShaderReloadDebugTrackerInternal::s_enabled = AZ::Environment::CreateVariable<bool>(AZ::Crc32(ShaderReloadDebugTrackerInternal::EnabledVariableName), false);
ShaderReloadDebugTrackerInternal::s_indent = AZ::Environment::CreateVariable<int>(AZ::Crc32(ShaderReloadDebugTrackerInternal::IndentVariableName), 0);
}
}
bool ShaderReloadDebugTracker::IsEnabled()
{
#ifdef AZ_ENABLE_SHADER_RELOAD_DEBUG_TRACKER
MakeReady();
// Set this to true in the debugger to turn on hot reload tracing.
// If needed, we could hook this up to a CVar.
return ShaderReloadDebugTrackerInternal::s_enabled.Get();
#else
return false;
#endif
}
void ShaderReloadDebugTracker::AddIndent()
{
MakeReady();
ShaderReloadDebugTrackerInternal::s_indent.Get() += IndentSpaces;
}
void ShaderReloadDebugTracker::RemoveIndent()
{
MakeReady();
ShaderReloadDebugTrackerInternal::s_indent.Get() -= IndentSpaces;
}
int ShaderReloadDebugTracker::GetIndent()
{
MakeReady();
return ShaderReloadDebugTrackerInternal::s_indent.Get();
}
ShaderReloadDebugTracker::ScopedSection::~ScopedSection()
{
ShaderReloadDebugTracker::EndSection("%s", m_sectionName.c_str());
}
} // namespace RPI
} // namespace AZ