SwapchainExampleComponent.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <RHI/SwapchainExampleComponent.h>
  9. #include <Utils/Utils.h>
  10. #include <SampleComponentManager.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. namespace AtomSampleViewer
  13. {
  14. void SwapchainExampleComponent::Reflect(AZ::ReflectContext* context)
  15. {
  16. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  17. {
  18. serializeContext->Class<SwapchainExampleComponent, AZ::Component>()
  19. ->Version(0)
  20. ;
  21. }
  22. }
  23. SwapchainExampleComponent::SwapchainExampleComponent()
  24. {
  25. m_supportRHISamplePipeline = true;
  26. }
  27. void SwapchainExampleComponent::Activate()
  28. {
  29. using namespace AZ;
  30. const RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
  31. // Create a scope for the swapchain.
  32. {
  33. struct ScopeData
  34. {
  35. };
  36. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, ScopeData&)
  37. {
  38. const u32 colorIndex = m_timeInSeconds % 3;
  39. RHI::ClearValue clearValue;
  40. switch (colorIndex)
  41. {
  42. case 0:
  43. clearValue = RHI::ClearValue::CreateVector4Float(1.0f, 0.0f, 0.0f, 1.0f);
  44. break;
  45. case 1:
  46. clearValue = RHI::ClearValue::CreateVector4Float(0.0f, 1.0f, 0.0f, 1.0f);
  47. break;
  48. case 2:
  49. clearValue = RHI::ClearValue::CreateVector4Float(0.0f, 0.0f, 1.0f, 1.0f);
  50. break;
  51. }
  52. // Binds the swapchain as a color attachment.
  53. {
  54. RHI::ImageScopeAttachmentDescriptor descriptor;
  55. descriptor.m_attachmentId = m_outputAttachmentId;
  56. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear;
  57. descriptor.m_loadStoreAction.m_clearValue = clearValue;
  58. frameGraph.UseColorAttachment(descriptor);
  59. }
  60. };
  61. RHI::EmptyCompileFunction<ScopeData> compileFunction;
  62. RHI::EmptyExecuteFunction<ScopeData> executeFunction;
  63. m_scopeProducers.emplace_back(
  64. aznew RHI::ScopeProducerFunction<
  65. ScopeData,
  66. decltype(prepareFunction),
  67. decltype(compileFunction),
  68. decltype(executeFunction)>(
  69. RHI::ScopeId{"Swapchain"},
  70. ScopeData{},
  71. prepareFunction,
  72. compileFunction,
  73. executeFunction));
  74. }
  75. AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
  76. AZ::TickBus::Handler::BusConnect();
  77. }
  78. void SwapchainExampleComponent::Deactivate()
  79. {
  80. AZ::TickBus::Handler::BusDisconnect();
  81. AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  82. m_windowContext = nullptr;
  83. m_scopeProducers.clear();
  84. }
  85. void SwapchainExampleComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
  86. {
  87. AZ_UNUSED(deltaTime);
  88. m_timeInSeconds = static_cast<uint32_t>(time.GetSeconds());
  89. }
  90. } // namespace AtomSampleViewer