ComputeExampleComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <Atom/RHI/BufferPool.h>
  10. #include <Atom/RHI/DrawItem.h>
  11. #include <Atom/RHI/ScopeProducer.h>
  12. #include <Atom/RHI/RHISystemInterface.h>
  13. #include <Atom/RPI.Public/Shader/Shader.h>
  14. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  15. #include <Atom/RPI.Reflect/ResourcePoolAssetCreator.h>
  16. #include <AzCore/Component/Component.h>
  17. #include <AzCore/Component/TickBus.h>
  18. #include <RHI/BasicRHIComponent.h>
  19. namespace AtomSampleViewer
  20. {
  21. //! Compute shader testing
  22. //! Testing the functionality of dispatch pipeline
  23. //! Compute scope do the computation, raster scope render the result to screen
  24. //! Should show Julia Fractals on success
  25. class ComputeExampleComponent final
  26. : public BasicRHIComponent
  27. , public AZ::TickBus::Handler
  28. {
  29. public:
  30. AZ_COMPONENT(ComputeExampleComponent, "{AA5EB226-A6DC-451F-A771-376A4B35C000}", AZ::Component);
  31. AZ_DISABLE_COPY(ComputeExampleComponent);
  32. static void Reflect(AZ::ReflectContext* context);
  33. ComputeExampleComponent();
  34. ~ComputeExampleComponent() = default;
  35. protected:
  36. static const char* s_computeExampleName;
  37. static const int s_numberOfSRGs = 2;
  38. int m_numThreadsX = 8;
  39. int m_numThreadsY = 8;
  40. int m_numThreadsZ = 1;
  41. struct ConstantData;
  42. // AZ::Component
  43. void Activate() override;
  44. void Deactivate() override;
  45. // RHISystemNotificationBus::Handler
  46. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  47. // TickBus::Handler
  48. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  49. void CreateInputAssemblyBuffersAndViews();
  50. void CreateComputeBuffer();
  51. void LoadComputeShader();
  52. void LoadRasterShader();
  53. void CreateComputeScope();
  54. void CreateRasterScope();
  55. // -------------------------------------------------
  56. // Input Assembly buffer and its Streams/Index Views
  57. // -------------------------------------------------
  58. struct BufferData
  59. {
  60. AZStd::array<VertexPosition, 4> m_positions;
  61. AZStd::array<VertexUV, 4> m_uvs;
  62. AZStd::array<uint16_t, 6> m_indices;
  63. };
  64. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  65. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  66. AZ::RHI::GeometryView m_geometryView;
  67. AZ::RHI::InputStreamLayout m_inputStreamLayout;
  68. // ----------------------------
  69. // Compute Buffer
  70. // ----------------------------
  71. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_computeBufferPool;
  72. AZ::RHI::Ptr<AZ::RHI::Buffer> m_computeBuffer;
  73. AZ::RHI::Ptr<AZ::RHI::BufferView> m_computeBufferView;
  74. AZ::RHI::BufferViewDescriptor m_bufferViewDescriptor;
  75. AZ::RHI::AttachmentId m_bufferAttachmentId = AZ::RHI::AttachmentId("bufferAttachmentId");
  76. // ----------------------
  77. // Pipeline state and SRG
  78. // ----------------------
  79. // Dispatch pipeline
  80. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_dispatchPipelineState;
  81. AZStd::array< AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, s_numberOfSRGs> m_dispatchSRGs;
  82. AZ::RHI::ShaderInputConstantIndex m_dispatchDimensionConstantIndex;
  83. AZ::RHI::ShaderInputConstantIndex m_dispatchSeedConstantIndex;
  84. // Draw pipeline
  85. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_drawPipelineState;
  86. AZStd::array< AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, s_numberOfSRGs> m_drawSRGs;
  87. AZ::RHI::ShaderInputConstantIndex m_drawDimensionConstantIndex;
  88. // ------
  89. // Others
  90. // ------
  91. static const uint32_t m_bufferWidth = 1920;
  92. static const uint32_t m_bufferHeight = 1080;
  93. float m_time = 0.0f;
  94. };
  95. }