3
0

CubeMapCaptureFeatureProcessor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <CubeMapCapture/CubeMapCaptureFeatureProcessor.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <Atom/RPI.Public/Scene.h>
  11. namespace AZ
  12. {
  13. namespace Render
  14. {
  15. void CubeMapCaptureFeatureProcessor::Reflect(ReflectContext* context)
  16. {
  17. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  18. {
  19. serializeContext
  20. ->Class<CubeMapCaptureFeatureProcessor, FeatureProcessor>()
  21. ->Version(1);
  22. }
  23. }
  24. void CubeMapCaptureFeatureProcessor::Activate()
  25. {
  26. m_cubeMapCaptures.reserve(InitialProbeAllocationSize);
  27. EnableSceneNotification();
  28. }
  29. void CubeMapCaptureFeatureProcessor::Deactivate()
  30. {
  31. AZ_Warning("CubeMapCaptureFeatureProcessor", m_cubeMapCaptures.size() == 0,
  32. "Deactivating the CubeMapCaptureFeatureProcessor but there are still outstanding CubeMapCaptures. Components\n"
  33. "using CubeMapCaptureHandles should free them before the CubeMapCaptureFeatureProcessor is deactivated.\n"
  34. );
  35. DisableSceneNotification();
  36. }
  37. void CubeMapCaptureFeatureProcessor::Simulate([[maybe_unused]] const FeatureProcessor::SimulatePacket& packet)
  38. {
  39. AZ_PROFILE_SCOPE(AzRender, "CubeMapCaptureFeatureProcessor: Simulate");
  40. // call Simulate on all cubeMapCaptures
  41. for (auto& cubeMapCapture : m_cubeMapCaptures)
  42. {
  43. AZ_Assert(cubeMapCapture.use_count() > 1, "CubeMapCapture found with no corresponding owner, ensure that RemoveCubeMapCapture() is called before releasing CubeMapCapture handles");
  44. cubeMapCapture->Simulate();
  45. }
  46. }
  47. void CubeMapCaptureFeatureProcessor::OnRenderEnd()
  48. {
  49. // call OnRenderEnd on all cubeMapCaptures
  50. for (auto& cubeMapCapture : m_cubeMapCaptures)
  51. {
  52. AZ_Assert(cubeMapCapture.use_count() > 1, "CubeMapCapture found with no corresponding owner, ensure that RemoveCubeMapCapture() is called before releasing CubeMapCapture handles");
  53. cubeMapCapture->OnRenderEnd();
  54. }
  55. }
  56. CubeMapCaptureHandle CubeMapCaptureFeatureProcessor::AddCubeMapCapture(const AZ::Transform& transform)
  57. {
  58. AZStd::shared_ptr<CubeMapCapture> cubeMapCapture = AZStd::make_shared<CubeMapCapture>();
  59. cubeMapCapture->Init(GetParentScene());
  60. cubeMapCapture->SetTransform(transform);
  61. m_cubeMapCaptures.push_back(cubeMapCapture);
  62. return cubeMapCapture;
  63. }
  64. void CubeMapCaptureFeatureProcessor::RemoveCubeMapCapture(CubeMapCaptureHandle& cubeMapCapture)
  65. {
  66. AZ_Assert(cubeMapCapture.get(), "RemoveCubeMapCapture called with an invalid handle");
  67. auto itEntry = AZStd::find_if(m_cubeMapCaptures.begin(), m_cubeMapCaptures.end(), [&](AZStd::shared_ptr<CubeMapCapture> const& entry)
  68. {
  69. return (entry == cubeMapCapture);
  70. });
  71. AZ_Assert(itEntry != m_cubeMapCaptures.end(), "RemoveCubeMapCapture called with a CubeMapCapture that is not in the CubeMapCapture list");
  72. m_cubeMapCaptures.erase(itEntry);
  73. }
  74. void CubeMapCaptureFeatureProcessor::SetTransform(const CubeMapCaptureHandle& cubeMapCapture, const AZ::Transform& transform)
  75. {
  76. AZ_Assert(cubeMapCapture.get(), "SetTransform called with an invalid handle");
  77. cubeMapCapture->SetTransform(transform);
  78. }
  79. void CubeMapCaptureFeatureProcessor::RenderCubeMap(const CubeMapCaptureHandle& cubeMapCapture, RenderCubeMapCallback callback, const AZStd::string& relativePath)
  80. {
  81. AZ_Assert(cubeMapCapture.get(), "RenderCubeMap called with an invalid handle");
  82. cubeMapCapture->RenderCubeMap(callback, relativePath);
  83. }
  84. bool CubeMapCaptureFeatureProcessor::IsCubeMapReferenced(const AZStd::string& relativePath)
  85. {
  86. for (auto& cubeMapCapture : m_cubeMapCaptures)
  87. {
  88. if (cubeMapCapture->GetRelativePath() == relativePath)
  89. {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. void CubeMapCaptureFeatureProcessor::SetExposure(const CubeMapCaptureHandle& cubeMapCapture, float exposure)
  96. {
  97. AZ_Assert(cubeMapCapture.get(), "SetExposure called with an invalid handle");
  98. cubeMapCapture->SetExposure(exposure);
  99. }
  100. void CubeMapCaptureFeatureProcessor::SetRelativePath(const CubeMapCaptureHandle& cubeMapCapture, const AZStd::string& relativePath)
  101. {
  102. AZ_Assert(cubeMapCapture.get(), "SetRelativePath called with an invalid handle");
  103. cubeMapCapture->SetRelativePath(relativePath);
  104. }
  105. void CubeMapCaptureFeatureProcessor::OnRenderPipelineChanged(RPI::RenderPipeline* renderPipeline,
  106. RPI::SceneNotification::RenderPipelineChangeType changeType)
  107. {
  108. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::PassChanged)
  109. {
  110. for (auto& cubeMapCapture : m_cubeMapCaptures)
  111. {
  112. cubeMapCapture->OnRenderPipelinePassesChanged(renderPipeline);
  113. }
  114. }
  115. }
  116. } // namespace Render
  117. } // namespace AZ