BasicRHIComponent.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #pragma once
  13. #include <AzCore/Component/Component.h>
  14. #include <AzCore/std/smart_ptr/shared_ptr.h>
  15. #include <Atom/RPI.Public/Image/StreamingImage.h>
  16. #include <Atom/RHI/RHISystemInterface.h>
  17. #include <Atom/RPI.Public/Pass/RenderPass.h>
  18. #include <Atom/RPI.Public/Shader/Shader.h>
  19. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  20. #include <Atom/RPI.Public/WindowContext.h>
  21. #include <Atom/RHI/Factory.h>
  22. #include <Atom/RHI/FrameScheduler.h>
  23. #include <Atom/RHI/ScopeProducer.h>
  24. #include <Atom/RHI.Reflect/Format.h>
  25. #include <AtomCore/Instance/InstanceId.h>
  26. #include <AzCore/Math/Matrix4x4.h>
  27. #include <AzCore/Math/Vector3.h>
  28. #include <AzCore/Math/Vector4.h>
  29. namespace AZ
  30. {
  31. class AssetCollectionAsyncLoader;
  32. namespace RHI
  33. {
  34. class Device;
  35. }
  36. namespace RPI
  37. {
  38. class PipelineStateCache;
  39. }
  40. }
  41. namespace AtomSampleViewer
  42. {
  43. class BasicRHIComponent;
  44. // Using RenderPass instead of Pass so we can have a scope to clear RenderTarget
  45. class RHISamplePass
  46. : public AZ::RPI::RenderPass
  47. {
  48. using Base = AZ::RPI::RenderPass;
  49. AZ_RPI_PASS(RHISamplePass);
  50. public:
  51. AZ_RTTI(RHISamplePass, "{7F70BF4C-F5B3-453A-AEC4-3F5599BAFC16}", Pass);
  52. AZ_CLASS_ALLOCATOR(RHISamplePass, AZ::SystemAllocator, 0);
  53. virtual ~RHISamplePass();
  54. //! Creates a new pass without a PassTemplate
  55. static AZ::RPI::Ptr<RHISamplePass> Create(const AZ::RPI::PassDescriptor& descriptor);
  56. void SetRHISample(BasicRHIComponent* sample);
  57. // Pass overrides
  58. const AZ::RPI::PipelineViewTag& GetPipelineViewTag() const override;
  59. protected:
  60. explicit RHISamplePass(const AZ::RPI::PassDescriptor& descriptor);
  61. // --- Pass Behaviour Overrides ---
  62. void BuildInternal() override; // build attachment
  63. void FrameBeginInternal(FramePrepareParams params) override; // import scopes
  64. BasicRHIComponent* m_rhiSample = nullptr;
  65. AZ::RPI::Ptr<AZ::RPI::PassAttachment> m_outputAttachment;
  66. AZ::RPI::PipelineViewTag m_pipelineViewTag;
  67. };
  68. class BasicRHIComponent
  69. : public AZ::Component
  70. , public AZ::RHI::RHISystemNotificationBus::Handler
  71. {
  72. friend class RHISamplePass;
  73. public:
  74. AZ_RTTI(BasicRHIComponent, "{FAB340E4-2D91-48CD-A7BC-81ED25721415}", AZ::Component);
  75. BasicRHIComponent() = default;
  76. ~BasicRHIComponent() override = default;
  77. // Creates a 3D image from 2D images. All 2D images are required to have the same format and layout
  78. static void CreateImage3dData(AZStd::vector<uint8_t>& data, AZ::RHI::ImageSubresourceLayout& layout, AZ::RHI::Format& format, AZStd::vector<const char*>&& imageAssetPaths);
  79. void SetOutputInfo(uint32_t width, uint32_t height, AZ::RHI::Format format, AZ::RHI::AttachmentId attachmentId);
  80. bool IsSupportedRHISamplePipeline();
  81. float GetViewportWidth();
  82. float GetViewportHeight();
  83. protected:
  84. AZ_DISABLE_COPY(BasicRHIComponent);
  85. struct VertexPosition;
  86. struct VertexColor;
  87. struct VertexU;
  88. struct VertexUV;
  89. struct VertexUVW;
  90. struct VertexUVWX;
  91. struct VertexNormal;
  92. // Component
  93. virtual void Activate() override = 0;
  94. virtual void Deactivate() override = 0;
  95. virtual bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  96. // RHISystemNotificationBus::Handler
  97. virtual void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  98. // virtual function which is called by OnFramePrepare
  99. virtual void FrameBeginInternal([[maybe_unused]] AZ::RHI::FrameGraphBuilder& frameGraphBuilder) {};
  100. // Buffer Setting
  101. void SetVertexPosition(VertexPosition* positionBuffer, int bufferIndex, float x, float y, float z);
  102. void SetVertexPosition(VertexPosition* positionBuffer, int bufferIndex, const AZ::Vector3& position);
  103. void SetVertexPosition(VertexPosition* positionBuffer, int bufferIndex, const VertexPosition& position);
  104. void SetVertexColor(VertexColor* colorBuffer, int bufferIndex, float r, float g, float b, float a);
  105. void SetVertexColor(VertexColor* colorBuffer, int bufferIndex, const AZ::Vector4& color);
  106. void SetVertexColor(VertexColor* colorBuffer, int bufferIndex, const VertexColor& color);
  107. void SetVertexIndex(uint16_t* indexBuffer, int bufferIndex, const uint16_t index);
  108. void SetVertexU(VertexU* uBuffer, int bufferIndex, float u);
  109. void SetVertexUV(VertexUV* uvBuffer, int bufferIndex, float u, float v);
  110. void SetVertexUVW(VertexUVW* uvwBuffer, int bufferIndex, float u, float v, float w);
  111. void SetVertexUVW(VertexUVW* uvwBuffer, int bufferIndex, const AZ::Vector3& uvw);
  112. void SetVertexUVWX(VertexUVWX* uvwxBuffer, int bufferIndex, float u, float v, float w, float x);
  113. void SetVertexNormal(VertexNormal* normalBuffer, int bufferIndex, float nx, float ny, float nz);
  114. void SetVertexNormal(VertexNormal* normalBuffer, int bufferIndex, const AZ::Vector3& normal);
  115. void SetVertexIndexIncreasing(uint16_t* indexBuffer, size_t bufferSize);
  116. void SetFullScreenRect(VertexPosition* positionBuffer, VertexUV* uvBuffer, uint16_t* indexBuffer);
  117. void SetCube(VertexPosition* positionBuffer, VertexColor* colorBuffer, VertexNormal* normalBuffer, uint16_t* indexBuffer);
  118. AZ::Matrix4x4 CreateViewMatrix(AZ::Vector3 eye, AZ::Vector3 up, AZ::Vector3 lookAt);
  119. // Shader
  120. AZ::Data::Instance<AZ::RPI::Shader> LoadShader(const char* shaderFilePath, const char* sampleName);
  121. AZ::Data::Instance<AZ::RPI::Shader> LoadShader(const AZ::AssetCollectionAsyncLoader& assetLoadMgr, const char* shaderFilePath, const char* sampleName);
  122. static AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> CreateShaderResourceGroup(AZ::Data::Instance<AZ::RPI::Shader> shader, const char* shaderResourceGroupId, const char* sampleName);
  123. void FindShaderInputIndex(
  124. AZ::RHI::ShaderInputConstantIndex* shaderInputConstIndex,
  125. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> shaderResourceGroup,
  126. const AZ::Name& shaderInputName,
  127. const char* componentName
  128. );
  129. void FindShaderInputIndex(
  130. AZ::RHI::ShaderInputImageIndex* shaderInputImageIndex,
  131. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> shaderResourceGroup,
  132. const AZ::Name& shaderInputName,
  133. const char* componentName
  134. );
  135. void FindShaderInputIndex(
  136. AZ::RHI::ShaderInputBufferIndex* shaderInputBufferIndex,
  137. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> shaderResourceGroup,
  138. const AZ::Name& shaderInputName,
  139. const char* componentName
  140. );
  141. void FindShaderInputIndex(
  142. AZ::RHI::ShaderInputSamplerIndex* shaderInputSamplerIndex,
  143. AZ::Data::Instance<AZ::RPI::ShaderResourceGroup> shaderResourceGroup,
  144. const AZ::Name& shaderInputName,
  145. const char* componentName
  146. );
  147. // set output info from m_windowContext
  148. void SetOutputInfoFromWindowContext();
  149. // setup viewport and scissor from output width and height
  150. void UpdateViewportAndScissor();
  151. // Texture
  152. AZ::Data::Instance<AZ::RPI::StreamingImage> LoadStreamingImage(const char* textureFilePath, const char* sampleName);
  153. // Input Assembly Data
  154. struct VertexPosition
  155. {
  156. float m_position[3];
  157. };
  158. struct VertexColor
  159. {
  160. float m_color[4];
  161. };
  162. struct VertexNormal
  163. {
  164. float m_normal[3];
  165. };
  166. struct VertexU
  167. {
  168. float m_u;
  169. };
  170. struct VertexUV
  171. {
  172. float m_uv[2];
  173. };
  174. struct VertexUVW
  175. {
  176. float m_uvw[3];
  177. };
  178. struct VertexUVWX
  179. {
  180. float m_uvwx[4];
  181. };
  182. // Variables
  183. AZStd::shared_ptr<AZ::RPI::WindowContext> m_windowContext;
  184. AZStd::vector<AZStd::shared_ptr<AZ::RHI::ScopeProducer>> m_scopeProducers;
  185. // The output (render target) info
  186. uint32_t m_outputWidth = 1920;
  187. uint32_t m_outputHeight = 1080;
  188. AZ::RHI::Format m_outputFormat;
  189. AZ::RHI::AttachmentId m_outputAttachmentId;
  190. AZ::RHI::Viewport m_viewport;
  191. AZ::RHI::Scissor m_scissor;
  192. // whether this sample supports RHI sample render pipeline or not
  193. bool m_supportRHISamplePipeline = false;
  194. };
  195. } // namespace AtomSampleViewer