BasicRHIComponent.h 9.1 KB

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