MatrixAlignmentTestExampleComponent.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  12. #include <Atom/RHI/FrameScheduler.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 <RHI/BasicRHIComponent.h>
  18. #include <Utils/ImGuiSidebar.h>
  19. namespace AtomSampleViewer
  20. {
  21. /*
  22. * An example that renders the content a chosen RxC matrix followed by float or float2 scalar.
  23. * It renders those values as a grid of size (R+1)x(C+1) when Coordinates 0..R-1, 0..C-1 contain the
  24. * a Gray value R,G,B where R==G==B==m_matrix<R><C>[r][c]. m_matrix<R><C> is a variable in the SRG Constant Buffer.
  25. * The cell coordinate [R][C] contains the color of another SRG constant called m_fAfter<R><C> which can be a "float"
  26. * or a "float2".
  27. * The rendered grid shows an intuitive image that can be used to catch if a given RHI has data offsets or alignment issues
  28. * in the SRG Constant Buffer.
  29. */
  30. class MatrixAlignmentTestExampleComponent final
  31. : public BasicRHIComponent
  32. , public AZ::TickBus::Handler
  33. {
  34. public:
  35. AZ_COMPONENT(MatrixAlignmentTestExampleComponent, "{194CEF1E-5F35-4179-AB8F-0A4D3831377A}", AZ::Component);
  36. AZ_DISABLE_COPY(MatrixAlignmentTestExampleComponent);
  37. static void Reflect(AZ::ReflectContext* context);
  38. MatrixAlignmentTestExampleComponent();
  39. ~MatrixAlignmentTestExampleComponent() override = default;
  40. private:
  41. static constexpr char LogName[] = "MatrixAlignmentTest";
  42. protected:
  43. //! Releases render pipeline data. Should be used before calling InitializeRenderPipeline();
  44. void ReleaseRhiData();
  45. void InitializeRenderPipeline();
  46. // BasicRHIComponent
  47. void Activate() override;
  48. void Deactivate() override;
  49. void FrameBeginInternal(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  50. // TickBus::Handler
  51. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  52. // RHISystemNotificationBus::Handler
  53. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  54. // Helpers
  55. //! An ImGui function that renders a table of checkboxes based on the values
  56. //! stored in @m_checkedMatrixValues.
  57. void DrawMatrixValuesTable();
  58. //! Sets the correct SRG matrix & float (or float2) with data
  59. //! based on the selected number of rows, @numRows, and columns, @numColumns.
  60. bool SetSrgMatrixData(int numRows, int numColumns,
  61. const AZ::RHI::ShaderInputConstantIndex& dataAfterMatrixConstantId,
  62. const AZ::RHI::ShaderInputConstantIndex& matrixConstantId);
  63. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  64. AZ::RHI::Ptr<AZ::RHI::Buffer> m_inputAssemblyBuffer;
  65. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_pipelineState;
  66. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> m_shaderResourceGroup;
  67. AZ::RHI::ShaderInputConstantIndex m_resolutionConstantIndex;
  68. AZ::RHI::ShaderInputConstantIndex m_numRowsConstantIndex;
  69. int m_numRows;
  70. AZ::RHI::ShaderInputConstantIndex m_numColumnsConstantIndex;
  71. int m_numColumns;
  72. bool m_checkedMatrixValues[4][4];
  73. float m_rawMatrix[4][4];
  74. float m_floatAfterMatrix; // The value is controlled by an ImGui slider. [0.0f .. 1.0f]
  75. // Controlled by a Radio Button. Takes value 1 or 2.
  76. // Value 1 means to pick the Supervariant where the data after the matrix is of type float.
  77. // Value 2 means to pick the Supervariant where the data after the matrix is of type float2.
  78. int m_numFloatsAfterMatrix;
  79. bool m_needPipelineReload; // Only true when @m_numFloatsAfterMatrix changes.
  80. // A value in the range [0.0..1.0]
  81. // This variable gets the value of an ImGui Slider.
  82. // When the user enables the checkbox at any given matrix location
  83. // in the ImGui UI, then
  84. // @m_rawMatrix[R][C] = @m_checkedMatrixLocationValue.
  85. // When the user un-checks the checkbox then
  86. // @m_rawMatrix[R][C] = 1.0 - m_checkedMatrixLocationValue.
  87. float m_matrixLocationValue;
  88. AZ::RHI::ShaderInputConstantIndex m_matrix11ConstantIndex;
  89. AZ::RHI::ShaderInputConstantIndex m_fAfter11ConstantIndex;
  90. AZ::RHI::ShaderInputConstantIndex m_matrix12ConstantIndex;
  91. AZ::RHI::ShaderInputConstantIndex m_fAfter12ConstantIndex;
  92. AZ::RHI::ShaderInputConstantIndex m_matrix13ConstantIndex;
  93. AZ::RHI::ShaderInputConstantIndex m_fAfter13ConstantIndex;
  94. AZ::RHI::ShaderInputConstantIndex m_matrix14ConstantIndex;
  95. AZ::RHI::ShaderInputConstantIndex m_fAfter14ConstantIndex;
  96. // Not supported by AZSLc
  97. //AZ::RHI::ShaderInputConstantIndex m_matrix21ConstantIndex;
  98. //AZ::RHI::ShaderInputConstantIndex m_fAfter21ConstantIndex;
  99. AZ::RHI::ShaderInputConstantIndex m_matrix22ConstantIndex;
  100. AZ::RHI::ShaderInputConstantIndex m_fAfter22ConstantIndex;
  101. AZ::RHI::ShaderInputConstantIndex m_matrix23ConstantIndex;
  102. AZ::RHI::ShaderInputConstantIndex m_fAfter23ConstantIndex;
  103. AZ::RHI::ShaderInputConstantIndex m_matrix24ConstantIndex;
  104. AZ::RHI::ShaderInputConstantIndex m_fAfter24ConstantIndex;
  105. // Not supported by AZSLc
  106. //AZ::RHI::ShaderInputConstantIndex m_matrix31ConstantIndex;
  107. //AZ::RHI::ShaderInputConstantIndex m_fAfter31ConstantIndex;
  108. AZ::RHI::ShaderInputConstantIndex m_matrix32ConstantIndex;
  109. AZ::RHI::ShaderInputConstantIndex m_fAfter32ConstantIndex;
  110. AZ::RHI::ShaderInputConstantIndex m_matrix33ConstantIndex;
  111. AZ::RHI::ShaderInputConstantIndex m_fAfter33ConstantIndex;
  112. AZ::RHI::ShaderInputConstantIndex m_matrix34ConstantIndex;
  113. AZ::RHI::ShaderInputConstantIndex m_fAfter34ConstantIndex;
  114. // Not supported by AZSLc
  115. //AZ::RHI::ShaderInputConstantIndex m_matrix41ConstantIndex;
  116. //AZ::RHI::ShaderInputConstantIndex m_fAfter41ConstantIndex;
  117. AZ::RHI::ShaderInputConstantIndex m_matrix42ConstantIndex;
  118. AZ::RHI::ShaderInputConstantIndex m_fAfter42ConstantIndex;
  119. AZ::RHI::ShaderInputConstantIndex m_matrix43ConstantIndex;
  120. AZ::RHI::ShaderInputConstantIndex m_fAfter43ConstantIndex;
  121. AZ::RHI::ShaderInputConstantIndex m_matrix44ConstantIndex;
  122. AZ::RHI::ShaderInputConstantIndex m_fAfter44ConstantIndex;
  123. // Required data to render two triangles that cover the whole viewport.
  124. struct BufferData
  125. {
  126. AZStd::array<VertexPosition, 4> m_positions;
  127. AZStd::array<VertexColor, 4> m_colors;
  128. AZStd::array<uint16_t, 6> m_indices;
  129. };
  130. AZ::RHI::GeometryView m_geometryView;
  131. // ImGui stuff.
  132. ImGuiSidebar m_imguiSidebar;
  133. };
  134. } // namespace AtomSampleViewer