AuxGeomFeatureProcessor.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 <AuxGeom/AuxGeomFeatureProcessor.h>
  9. #include "AuxGeomDrawQueue.h"
  10. #include "DynamicPrimitiveProcessor.h"
  11. #include "FixedShapeProcessor.h"
  12. #include <Atom/RHI/RHISystemInterface.h>
  13. #include <Atom/RPI.Public/View.h>
  14. namespace AZ
  15. {
  16. namespace Render
  17. {
  18. const char* AuxGeomFeatureProcessor::s_featureProcessorName = "AuxGeomFeatureProcessor";
  19. void AuxGeomFeatureProcessor::Reflect(ReflectContext* context)
  20. {
  21. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  22. {
  23. serializeContext
  24. ->Class<AuxGeomFeatureProcessor, FeatureProcessor>()
  25. ->Version(1);
  26. }
  27. }
  28. void AuxGeomFeatureProcessor::Activate()
  29. {
  30. AZ::RPI::Scene* scene = GetParentScene();
  31. // create an AuxGeomDrawQueue object for this scene and register it with the AuxGeomSystemComponent
  32. m_sceneDrawQueue = RPI::AuxGeomDrawPtr(aznew AuxGeomDrawQueue);
  33. // initialize the dynamic primitive processor
  34. m_dynamicPrimitiveProcessor = AZStd::make_unique<DynamicPrimitiveProcessor>();
  35. if (!m_dynamicPrimitiveProcessor->Initialize(scene))
  36. {
  37. AZ_Error(s_featureProcessorName, false, "Failed to init AuxGeom DynamicPrimitiveProcessor");
  38. return;
  39. }
  40. // initialize the fixed shape processor
  41. m_fixedShapeProcessor = AZStd::make_unique<FixedShapeProcessor>();
  42. if (!m_fixedShapeProcessor->Initialize(RHI::MultiDevice::AllDevices, scene))
  43. {
  44. AZ_Error(s_featureProcessorName, false, "Failed to init AuxGeom FixedShapeProcessor");
  45. return;
  46. }
  47. EnableSceneNotification();
  48. }
  49. void AuxGeomFeatureProcessor::Deactivate()
  50. {
  51. DisableSceneNotification();
  52. m_viewDrawDataMap.clear();
  53. m_dynamicPrimitiveProcessor->Release();
  54. m_dynamicPrimitiveProcessor = nullptr;
  55. m_fixedShapeProcessor->Release();
  56. m_fixedShapeProcessor = nullptr;
  57. // release the AuxGeomDrawQueue object for this scene
  58. m_sceneDrawQueue = nullptr;
  59. }
  60. void AuxGeomFeatureProcessor::Render(const FeatureProcessor::RenderPacket& fpPacket)
  61. {
  62. AZ_PROFILE_SCOPE(AzRender, "AuxGeomFeatureProcessor: Render");
  63. // Get the scene data and switch buffers so that other threads can continue to queue requests
  64. AuxGeomBufferData* bufferData = static_cast<AuxGeomDrawQueue*>(m_sceneDrawQueue.get())->Commit();
  65. // Process the dynamic primitives
  66. m_dynamicPrimitiveProcessor->PrepareFrame();
  67. m_dynamicPrimitiveProcessor->ProcessDynamicPrimitives(bufferData, fpPacket);
  68. // Process the objects (draw requests using fixed shape buffers)
  69. m_fixedShapeProcessor->PrepareFrame();
  70. m_fixedShapeProcessor->ProcessObjects(bufferData, fpPacket);
  71. if (m_viewDrawDataMap.size())
  72. {
  73. FeatureProcessor::RenderPacket perViewRP;
  74. perViewRP.m_drawListMask = fpPacket.m_drawListMask;
  75. for (auto& view : fpPacket.m_views)
  76. {
  77. auto it = m_viewDrawDataMap.find(view.get());
  78. if (it != m_viewDrawDataMap.end())
  79. {
  80. bufferData = static_cast<AuxGeomDrawQueue*>(it->second.get())->Commit();
  81. perViewRP.m_views.push_back(view);
  82. // Process the dynamic primitives
  83. m_dynamicPrimitiveProcessor->ProcessDynamicPrimitives(bufferData, perViewRP);
  84. // Process the objects (draw requests using fixed shape buffers)
  85. m_fixedShapeProcessor->ProcessObjects(bufferData, perViewRP);
  86. perViewRP.m_views.clear();
  87. }
  88. }
  89. }
  90. }
  91. void AuxGeomFeatureProcessor::OnRenderEnd()
  92. {
  93. m_dynamicPrimitiveProcessor->FrameEnd();
  94. m_fixedShapeProcessor->FrameEnd();
  95. }
  96. RPI::AuxGeomDrawPtr AuxGeomFeatureProcessor::GetDrawQueueForView(const RPI::View* view)
  97. {
  98. if (view)
  99. {
  100. auto drawDataIterator = m_viewDrawDataMap.find(view);
  101. if (drawDataIterator != m_viewDrawDataMap.end())
  102. {
  103. return drawDataIterator->second;
  104. }
  105. }
  106. AZ_Warning("AuxGeomFeatureProcessor", false, "Draw Queue requested for unknown view");
  107. return RPI::AuxGeomDrawPtr(nullptr);
  108. }
  109. RPI::AuxGeomDrawPtr AuxGeomFeatureProcessor::GetOrCreateDrawQueueForView(const RPI::View* view)
  110. {
  111. if (!view)
  112. {
  113. return RPI::AuxGeomDrawPtr(nullptr);
  114. }
  115. auto drawQueueIterator = m_viewDrawDataMap.find(view);
  116. if (drawQueueIterator == m_viewDrawDataMap.end())
  117. {
  118. RPI::AuxGeomDrawPtr drawQueue = RPI::AuxGeomDrawPtr(aznew AuxGeomDrawQueue());
  119. m_viewDrawDataMap.emplace(view, AZStd::move(drawQueue));
  120. return m_viewDrawDataMap[view];
  121. }
  122. return drawQueueIterator->second;
  123. }
  124. void AuxGeomFeatureProcessor::ReleaseDrawQueueForView(const RPI::View* view)
  125. {
  126. m_viewDrawDataMap.erase(view);
  127. }
  128. void AuxGeomFeatureProcessor::OnSceneRenderPipelinesChanged()
  129. {
  130. m_dynamicPrimitiveProcessor->SetUpdatePipelineStates();
  131. m_fixedShapeProcessor->SetUpdatePipelineStates();
  132. }
  133. void AuxGeomFeatureProcessor::OnRenderPipelineChanged([[maybe_unused]] RPI::RenderPipeline* pipeline,
  134. [[maybe_unused]] RPI::SceneNotification::RenderPipelineChangeType changeType)
  135. {
  136. OnSceneRenderPipelinesChanged();
  137. }
  138. } // namespace Render
  139. } // namespace AZ