MatrixAlignmentTestExampleComponent.h 7.2 KB

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