TrianglesConstantBufferExampleComponent.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <AzCore/Component/TickBus.h>
  11. #include <AzCore/Math/Color.h>
  12. #include <Atom/RHI/RHISystemInterface.h>
  13. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  14. #include <Atom/RPI.Public/WindowContext.h>
  15. #include <Atom/RHI/ScopeProducer.h>
  16. #include <Atom/RHI/FrameScheduler.h>
  17. #include <Atom/RHI/DrawItem.h>
  18. #include <Atom/RHI/Device.h>
  19. #include <Atom/RHI/Factory.h>
  20. #include <Atom/RHI/PipelineState.h>
  21. #include <Atom/RHI/BufferPool.h>
  22. #include <AzCore/Math/Matrix4x4.h>
  23. #include <RHI/BasicRHIComponent.h>
  24. namespace AtomSampleViewer
  25. {
  26. /**
  27. * This example is similar to Triangle example, but it uses instancing to render
  28. * 30 triangles in 1 draw call. The main purpose of this example is show how to use a
  29. * Constant Buffer to pass data to the shader instead of using SRG Constants.
  30. * The data of all instances is uploaded with a single Constant Buffer
  31. * once per frame, then the vertex shader uses the SV_InstanceID to access to the
  32. * right instance information from the Constant Buffer.
  33. */
  34. class TrianglesConstantBufferExampleComponent final
  35. : public BasicRHIComponent
  36. , public AZ::TickBus::Handler
  37. {
  38. struct InstanceInfo
  39. {
  40. AZStd::array<float, 16> m_matrix;
  41. AZStd::array<float, 4> m_colorMultiplier;
  42. };
  43. public:
  44. AZ_COMPONENT(TrianglesConstantBufferExampleComponent, "{EF33DA37-65F8-44E5-BD09-B1C948229374}", AZ::Component);
  45. AZ_DISABLE_COPY(TrianglesConstantBufferExampleComponent);
  46. static void Reflect(AZ::ReflectContext* context);
  47. TrianglesConstantBufferExampleComponent();
  48. ~TrianglesConstantBufferExampleComponent() override = default;
  49. private:
  50. static const char* s_trianglesConstantBufferExampleName;
  51. static const uint32_t s_numberOfTrianglesTotal;
  52. // AZ::Component
  53. void Activate() override;
  54. void Deactivate() override;
  55. // TickBus::Handler
  56. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  57. // RHISystemNotificationBus::Handler
  58. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  59. // Updates a single constant buffer with multiple triangle instances
  60. void UploadDataToConstantBuffer(InstanceInfo* data, uint32_t elementSize, uint32_t elementCount);
  61. void CreateConstantBufferView();
  62. AZ::RHI::DrawItem m_drawItem;
  63. float m_time = 0.0f;
  64. // -------------------------------------------------
  65. // Input Assembly buffer and its Streams/Index Views
  66. // -------------------------------------------------
  67. struct IABufferData
  68. {
  69. AZStd::array<VertexPosition, 3> m_positions;
  70. AZStd::array<VertexColor, 3> m_colors;
  71. AZStd::array<uint16_t, 3> m_indices;
  72. };
  73. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  74. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  75. AZStd::array<AZ::RHI::StreamBufferView, 2> m_streamBufferViews;
  76. AZ::RHI::IndexBufferView m_indexBufferView;
  77. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_constantBufferPool;
  78. AZ::RHI::Ptr<AZ::RHI::Buffer> m_constantBuffer;
  79. AZ::RHI::Ptr<AZ::RHI::BufferView> m_constantBufferView;
  80. // Cached Constant Buffer alignment queued from the device
  81. uint32_t m_constantBufferAlighment = 0u;
  82. // --------------------------------------------------------
  83. // Pipeline state and SRG to be constructed from the shader
  84. // --------------------------------------------------------
  85. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  86. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_shaderResourceGroup;
  87. };
  88. } // namespace AtomSampleViewer