TrianglesConstantBufferExampleComponent.h 3.8 KB

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