2
0

MultiGPUExampleComponent.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #pragma once
  9. #include <AzCore/Component/Component.h>
  10. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  11. #include <Atom/RHI/FrameScheduler.h>
  12. #include <Atom/RHI/DrawItem.h>
  13. #include <Atom/RHI/Device.h>
  14. #include <Atom/RHI/Factory.h>
  15. #include <Atom/RHI/PipelineState.h>
  16. #include <Atom/RHI/BufferPool.h>
  17. #include <Atom/RHI/ImagePool.h>
  18. #include <Atom/RHI/Image.h>
  19. #include <Atom/RHI/ShaderResourceGroupPool.h>
  20. #include <Atom/RHI/CopyItem.h>
  21. #include <AzCore/Math/Matrix4x4.h>
  22. #include <RHI/BasicRHIComponent.h>
  23. namespace AtomSampleViewer
  24. {
  25. // MultiGPU RHI example.
  26. // Renders a rotating triangle to the screen similar to the TriangleExampleComponent, except, the left half of the screen is rendered by
  27. // GPU 0 and the right half by GPU 1, which is then copied to GPU 0 to composite and show the final output on GPU 0.
  28. // At least two devices need to be initialized (by passing "--device-count 2") to run this example.
  29. class MultiGPUExampleComponent final
  30. : public BasicRHIComponent
  31. {
  32. public:
  33. AZ_COMPONENT(MultiGPUExampleComponent, "{BBA75A38-F111-4F52-AD5E-334B6DD58827}", AZ::Component);
  34. AZ_DISABLE_COPY(MultiGPUExampleComponent);
  35. static void Reflect(AZ::ReflectContext* context);
  36. MultiGPUExampleComponent();
  37. ~MultiGPUExampleComponent() override = default;
  38. protected:
  39. // AZ::Component
  40. void Activate() override;
  41. void Deactivate() override;
  42. void FrameBeginInternal(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  43. // RHISystemNotificationBus::Handler
  44. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  45. private:
  46. /////////////////////////////////////////////////////////////////////////
  47. //! Shared Resources
  48. void CreateRenderScopeProducer();
  49. AZ::RHI::MultiDevice::DeviceMask m_deviceMask;
  50. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  51. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  52. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  53. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_shaderResourceGroupShared;
  54. AZ::RHI::ShaderInputConstantIndex m_objectMatrixConstantIndex;
  55. struct BufferDataTrianglePass
  56. {
  57. AZStd::array<VertexPosition, 3> m_positions;
  58. AZStd::array<VertexColor, 3> m_colors;
  59. AZStd::array<uint16_t, 3> m_indices;
  60. };
  61. AZ::RHI::GeometryView m_geometryView;
  62. AZ::RHI::Ptr<AZ::RHI::ImagePool> m_imagePool{};
  63. AZStd::array<AZ::RHI::Ptr<AZ::RHI::Image>, 2> m_images;
  64. AZStd::array<AZ::RHI::AttachmentId, 2> m_imageAttachmentIds = { { AZ::RHI::AttachmentId("MultiGPURenderTexture1"),
  65. AZ::RHI::AttachmentId("MultiGPURenderTexture2") } };
  66. AZStd::array<AZ::RHI::AttachmentId, 2> m_bufferAttachmentIds = { { AZ::RHI::AttachmentId("MultiGPUBufferToCPU"),
  67. AZ::RHI::AttachmentId("MultiGPUBufferToGPU") } };
  68. uint32_t m_imageWidth{0};
  69. uint32_t m_imageHeight{0};
  70. /////////////////////////////////////////////////////////////////////////
  71. //! First device methods and members
  72. void CreateCopyToGPUScopeProducer();
  73. void CreateCopyToCPUScopeProducer();
  74. void CreateCompositeScopeProducer();
  75. struct BufferDataCompositePass
  76. {
  77. AZStd::array<VertexPosition, 4> m_positions;
  78. AZStd::array<VertexUV, 4> m_uvs;
  79. AZStd::array<uint16_t, 6> m_indices;
  80. };
  81. AZStd::array<AZ::RHI::ShaderInputImageIndex, 2> m_textureInputIndices;
  82. AZ::RHI::Ptr<AZ::RHI::Device> m_device_1{};
  83. AZ::RHI::MultiDevice::DeviceMask m_deviceMask_1{};
  84. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_stagingBufferPool{};
  85. AZ::RHI::Ptr<AZ::RHI::Buffer> m_stagingBufferToGPU{};
  86. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBufferComposite{};
  87. AZ::RHI::GeometryView m_geometryViewComposite;
  88. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineStateComposite;
  89. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroupPool> m_shaderResourceGroupPoolComposite;
  90. AZ::RHI::Ptr<AZ::RHI::ShaderResourceGroup> m_shaderResourceGroupComposite;
  91. AZ::RHI::ShaderResourceGroupData m_shaderResourceGroupDataComposite;
  92. AZStd::array<AZ::RHI::Scissor, 2> m_scissors{};
  93. /////////////////////////////////////////////////////////////////////////
  94. //! Second device methods and members
  95. AZ::RHI::Ptr<AZ::RHI::Device> m_device_2{};
  96. AZ::RHI::MultiDevice::DeviceMask m_deviceMask_2{};
  97. AZ::RHI::Ptr<AZ::RHI::Buffer> m_stagingBufferToCPU{};
  98. AZStd::vector<AZStd::shared_ptr<AZ::RHI::ScopeProducer>> m_secondaryScopeProducers;
  99. };
  100. } // namespace AtomSampleViewer