SplashScreenPass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Atom/RPI.Public/Image/StreamingImage.h>
  9. #include <Atom/RPI.Public/RPIUtils.h>
  10. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  11. #include <AzCore/Settings/SettingsRegistryImpl.h>
  12. #include <AzCore/Time/TimeSystem.h>
  13. #include <SplashScreen/SplashScreenPass.h>
  14. namespace AZ::Render
  15. {
  16. AZ::RPI::Ptr<SplashScreenPass> SplashScreenPass::Create(const AZ::RPI::PassDescriptor& descriptor)
  17. {
  18. AZ::RPI::Ptr<SplashScreenPass> pass = aznew SplashScreenPass(descriptor);
  19. return pass;
  20. }
  21. SplashScreenPass::SplashScreenPass(const AZ::RPI::PassDescriptor& descriptor)
  22. : AZ::RPI::FullscreenTrianglePass(descriptor)
  23. {
  24. }
  25. SplashScreenPass::~SplashScreenPass()
  26. {
  27. Clear();
  28. }
  29. void SplashScreenPass::InitializeInternal()
  30. {
  31. AZ::RPI::FullscreenTrianglePass::InitializeInternal();
  32. auto settingsRegistry = AZ::SettingsRegistry::Get();
  33. static const AZStd::string setregPath = "/O3DE/Atom/Feature/SplashScreen";
  34. if (!(settingsRegistry && settingsRegistry->GetObject(&m_settings, azrtti_typeid(m_settings), setregPath.c_str())))
  35. {
  36. return;
  37. }
  38. m_splashScreenImage = AZ::RPI::LoadStreamingTexture(m_settings.m_imagePath);
  39. m_durationSeconds = m_settings.m_durationSeconds;
  40. if (!m_splashScreenImage)
  41. {
  42. // Could not find an image based on the setreg path provided
  43. AZ_Error("SplashScreen", false, "Image path '%s' not found. Please update the /O3DE/Atom/Feature/SplashScreen/ImagePath setreg to a valid asset cache image file.", m_settings.m_imagePath.c_str());
  44. SetEnabled(false);
  45. return;
  46. }
  47. m_splashScreenImageIndex.Reset();
  48. m_splashScreenParamsIndex.Reset();
  49. const TimeUs currentTimeUs = static_cast<TimeUs>(AZStd::GetTimeNowMicroSecond());
  50. m_lastRealTimeStamp = aznumeric_cast<float>(currentTimeUs) / 1000000.0f;
  51. AZ::TickBus::Handler::BusConnect();
  52. }
  53. void SplashScreenPass::Clear()
  54. {
  55. m_splashScreenImage = nullptr;
  56. m_splashScreenImageIndex.Reset();
  57. m_splashScreenParamsIndex.Reset();
  58. }
  59. void SplashScreenPass::FrameBeginInternal([[maybe_unused]] FramePrepareParams params)
  60. {
  61. FullscreenTrianglePass::FrameBeginInternal(params);
  62. }
  63. void SplashScreenPass::FrameEndInternal()
  64. {
  65. FullscreenTrianglePass::FrameEndInternal();
  66. if (m_durationSeconds < 0.0f)
  67. {
  68. m_beginTimer = false;
  69. // Disable the pass after life time passes.
  70. SetEnabled(false);
  71. }
  72. }
  73. void SplashScreenPass::CompileResources(const AZ::RHI::FrameGraphCompileContext& context)
  74. {
  75. m_shaderResourceGroup->SetImage(m_splashScreenImageIndex, m_splashScreenImage);
  76. m_shaderResourceGroup->SetConstant(m_splashScreenParamsIndex, m_splashScreenParams);
  77. AZ::RPI::FullscreenTrianglePass::CompileResources(context);
  78. }
  79. void SplashScreenPass::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  80. {
  81. // Not using the delta time from Tick bus because it could be scaled.
  82. const TimeUs currentTimeUs = static_cast<TimeUs>(AZStd::GetTimeNowMicroSecond());
  83. float currentRealTimeStamp = aznumeric_cast<float>(currentTimeUs) / 1000000.0f;
  84. float realDeltaTime = currentRealTimeStamp - m_lastRealTimeStamp;
  85. m_lastRealTimeStamp = currentRealTimeStamp;
  86. if (m_beginTimer)
  87. {
  88. m_durationSeconds -= realDeltaTime;
  89. }
  90. if (m_settings.m_fading)
  91. {
  92. float leftTimeRatio = m_durationSeconds / m_settings.m_durationSeconds;
  93. m_splashScreenParams.m_fadingFactor = m_durationSeconds < 0.0f ? 0.0f : leftTimeRatio * leftTimeRatio * leftTimeRatio;
  94. }
  95. else
  96. {
  97. m_splashScreenParams.m_fadingFactor = 1.0f;
  98. }
  99. // Skipping the first frame for engine initialization time.
  100. m_beginTimer = true;
  101. if (m_durationSeconds < 0.0f)
  102. {
  103. AZ::TickBus::Handler::BusDisconnect();
  104. }
  105. }
  106. } // namespace AZ::Render