InputAssemblyExampleComponent.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/DrawItem.h>
  10. #include <Atom/RHI/ScopeProducer.h>
  11. #include <Atom/RHI/RHISystemInterface.h>
  12. #include <Atom/RPI.Public/Shader/Shader.h>
  13. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  14. #include <AzCore/Component/Component.h>
  15. #include <AzCore/Component/TickBus.h>
  16. #include <RHI/BasicRHIComponent.h>
  17. namespace AtomSampleViewer
  18. {
  19. //! This samples shows how to handle vertex buffer generation on a compute shader and how
  20. //! to properly declare shader inputs.
  21. //! There's 2 scopes in this sample, one runs a compute shader that is in charge of generating and animating the
  22. //! vertices of a 2D hexagon. The second scope is in charge of drawing the previously generated hexagon vertex buffer.
  23. class InputAssemblyExampleComponent final
  24. : public BasicRHIComponent
  25. , public AZ::TickBus::Handler
  26. {
  27. public:
  28. AZ_COMPONENT(InputAssemblyExampleComponent, "{1F061564-DB4A-4B68-B361-0B427B3CA5B5}", AZ::Component);
  29. AZ_DISABLE_COPY(InputAssemblyExampleComponent);
  30. static void Reflect(AZ::ReflectContext* context);
  31. InputAssemblyExampleComponent();
  32. ~InputAssemblyExampleComponent() = default;
  33. protected:
  34. // We have only float4 vertex positions as data.
  35. using BufferData = AZStd::array<AZStd::array<float, 4>, 6>;
  36. int m_numThreadsX = 1;
  37. int m_numThreadsY = 1;
  38. int m_numThreadsZ = 1;
  39. // AZ::Component
  40. void Activate() override;
  41. void Deactivate() override;
  42. void FrameBeginInternal(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  43. // TickBus::Handler
  44. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  45. void CreateInputAssemblyLayout();
  46. void CreateBuffers();
  47. void LoadComputeShader();
  48. void LoadRasterShader();
  49. void CreateComputeScope();
  50. void CreateRasterScope();
  51. // -------------------------------------------------
  52. // Input Assembly buffer and its Streams/Index Views
  53. // -------------------------------------------------
  54. AZ::RHI::GeometryView m_geometryView[2];
  55. AZ::RHI::InputStreamLayout m_inputStreamLayout;
  56. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  57. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  58. // ----------------------
  59. // Pipeline state and SRG
  60. // ----------------------
  61. // Dispatch pipeline
  62. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_dispatchPipelineState;
  63. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_dispatchSRG[2];
  64. AZ::RHI::ShaderInputConstantIndex m_dispatchTimeConstantIndex;
  65. AZ::RHI::ShaderInputBufferIndex m_dispatchIABufferIndex;
  66. // Draw pipeline
  67. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_drawPipelineState;
  68. AZ::RHI::ShaderInputConstantIndex m_drawMatrixIndex;
  69. AZ::RHI::ShaderInputConstantIndex m_drawColorIndex;
  70. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_drawSRG[2];
  71. // This is used to animate the hexagon.
  72. float m_time = 0.0f;
  73. };
  74. }