FeatureProcessorFactory.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/FeatureProcessorFactory.h>
  9. #include <Atom/RPI.Public/FeatureProcessor.h>
  10. #include <Atom/RPI.Public/Scene.h>
  11. #include <AzCore/Interface/Interface.h>
  12. #include <AzCore/Component/ComponentApplicationBus.h>
  13. namespace AZ
  14. {
  15. namespace RPI
  16. {
  17. FeatureProcessorFactory* FeatureProcessorFactory::Get()
  18. {
  19. return Interface<FeatureProcessorFactory>::Get();
  20. }
  21. FeatureProcessorFactory::FeatureProcessorFactory()
  22. {
  23. }
  24. void FeatureProcessorFactory::Init()
  25. {
  26. Interface<FeatureProcessorFactory>::Register(this);
  27. }
  28. void FeatureProcessorFactory::Shutdown()
  29. {
  30. Interface<FeatureProcessorFactory>::Unregister(this);
  31. }
  32. FeatureProcessorPtr FeatureProcessorFactory::CreateFeatureProcessor(FeatureProcessorId featureProcessorId)
  33. {
  34. AZ::SerializeContext* serializeContext = nullptr;
  35. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  36. if (!serializeContext)
  37. {
  38. AZ_Warning("FeatureProcessorFactory", false, "Provided type '%s' could not be created since the serialize context could not be retrieved.", featureProcessorId.GetCStr());
  39. return nullptr;
  40. }
  41. AZ::Uuid typeId{};
  42. // First check the registry for the feature processor id, otherwise fall back on the serialize context.
  43. auto foundIt = GetEntry(featureProcessorId);
  44. if (foundIt != AZStd::end(m_registry))
  45. {
  46. typeId = foundIt->m_typeId;
  47. }
  48. else
  49. {
  50. auto foundUuids = serializeContext->FindClassId(AZ::Crc32(featureProcessorId.GetStringView()));
  51. if (foundUuids.empty())
  52. {
  53. AZ_Warning("FeatureProcessorFactory", false, "Provided type %s is either an invalid TypeId or does not match any class names", featureProcessorId.GetCStr());
  54. }
  55. else
  56. {
  57. typeId = foundUuids[0];
  58. }
  59. }
  60. // Create the class from the type id.
  61. auto* classData = serializeContext->FindClassData(typeId);
  62. if (!classData)
  63. {
  64. AZ_Warning("FeatureProcessorFactory", false, "Provided type '%s' could not be created since failed to get class data. Make sure it was reflected to the serialize context.", featureProcessorId.GetCStr());
  65. return nullptr;
  66. }
  67. else if (!(classData->m_azRtti && classData->m_azRtti->IsTypeOf(FeatureProcessor::RTTI_Type())))
  68. {
  69. AZ_Warning("FeatureProcessorFactory", false, "Provided type %s is not a Feature Processor, or could not find RTTI information.", featureProcessorId.GetCStr());
  70. return nullptr;
  71. }
  72. return FeatureProcessorPtr(reinterpret_cast<FeatureProcessor*>(classData->m_factory->Create("FeatureProcessor")));
  73. }
  74. AZ::TypeId FeatureProcessorFactory::GetFeatureProcessorTypeId(FeatureProcessorId featureProcessorId)
  75. {
  76. auto foundIt = GetEntry(featureProcessorId);
  77. return foundIt == AZStd::end(m_registry) ? AZ::TypeId{} : foundIt->m_typeId;
  78. }
  79. AZ::TypeId FeatureProcessorFactory::GetFeatureProcessorInterfaceTypeId(FeatureProcessorId featureProcessorId)
  80. {
  81. auto foundIt = GetEntry(featureProcessorId);
  82. return foundIt == AZStd::end(m_registry) ? AZ::TypeId{} : foundIt->m_interfaceTypeId;
  83. }
  84. FeatureProcessorFactory::FeatureProcessorRegistry::const_iterator FeatureProcessorFactory::GetEntry(FeatureProcessorId featureProcessorId)
  85. {
  86. auto findFn = [featureProcessorId](const FeatureProcessorEntry& entry)
  87. {
  88. return entry.m_featureProcessorId == featureProcessorId;
  89. };
  90. return AZStd::find_if(AZStd::begin(m_registry), AZStd::end(m_registry), findFn);
  91. }
  92. void FeatureProcessorFactory::EnableAllForScene(Scene* scene)
  93. {
  94. AZ::SerializeContext* serializeContext = nullptr;
  95. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  96. if (!serializeContext)
  97. {
  98. AZ_Warning("FeatureProcessorFactory", false, "Enable feature processors requires a valid SerializeContext");
  99. return;
  100. }
  101. for (auto& entry : m_registry)
  102. {
  103. auto* classData = serializeContext->FindClassData(entry.m_typeId);
  104. if (!classData)
  105. {
  106. AZ_Warning("FeatureProcessorFactory", false, "Can't create feature processor [%s] since we failed to get class data ", entry.m_featureProcessorId.GetCStr());
  107. continue;
  108. }
  109. FeatureProcessorPtr fp = FeatureProcessorPtr(reinterpret_cast<FeatureProcessor*>(classData->m_factory->Create("FeatureProcessor")));
  110. scene->AddFeatureProcessor(AZStd::move(fp));
  111. }
  112. }
  113. }
  114. }