RayTracingClusterExampleComponent.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <Atom/RHI/BufferPool.h>
  10. #include <Atom/RHI/Device.h>
  11. #include <Atom/RHI/DeviceDispatchRaysItem.h>
  12. #include <Atom/RHI/Factory.h>
  13. #include <Atom/RHI/FrameScheduler.h>
  14. #include <Atom/RHI/PipelineState.h>
  15. #include <Atom/RHI/RayTracingAccelerationStructure.h>
  16. #include <Atom/RHI/RayTracingBufferPools.h>
  17. #include <Atom/RHI/RayTracingPipelineState.h>
  18. #include <Atom/RHI/RayTracingShaderTable.h>
  19. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  20. #include <AzCore/Component/Component.h>
  21. #include <AzCore/Math/Matrix4x4.h>
  22. #include <AzCore/Math/PackedVector3.h>
  23. #include <RHI/BasicRHIComponent.h>
  24. namespace AZ::RHI
  25. {
  26. using uint = uint32_t;
  27. #include "../../Assets/ShaderLib/Atom/Features/IndirectRayTracing.azsli"
  28. } // namespace AZ::RHI::CLAS
  29. namespace AtomSampleViewer
  30. {
  31. using namespace AZ;
  32. // This sample demonstrates the use of Atom Ray Tracing through the RHI abstraction layer.
  33. // It creates three triangles and one rectangle in a scene, and ray traces that scene to
  34. // an output image and displays it.
  35. class RayTracingClusterExampleComponent final
  36. : public BasicRHIComponent
  37. {
  38. public:
  39. AZ_COMPONENT(RayTracingClusterExampleComponent, "{85D46D8F-84C6-4178-ABE5-CADBBC6E6E9C}", AZ::Component);
  40. AZ_DISABLE_COPY(RayTracingClusterExampleComponent);
  41. static void Reflect(AZ::ReflectContext* context);
  42. RayTracingClusterExampleComponent();
  43. ~RayTracingClusterExampleComponent() override {}
  44. protected:
  45. // AZ::Component
  46. void Activate() override;
  47. void Deactivate() override;
  48. private:
  49. void CreateResourcePools();
  50. void CreateGeometry();
  51. void CreateFullScreenBuffer();
  52. void CreateOutputTexture();
  53. void CreateRasterShader();
  54. void CreateRayTracingAccelerationStructureObjects();
  55. void CreateRayTracingPipelineState();
  56. void CreateRayTracingShaderTable();
  57. void CreateRayTracingAccelerationTableScope();
  58. void CreateRayTracingDispatchScope();
  59. void CreateRasterScope();
  60. static const uint32_t m_imageWidth = 1920;
  61. static const uint32_t m_imageHeight = 1080;
  62. // resource pools
  63. RHI::Ptr<RHI::BufferPool> m_inputAssemblyBufferPool;
  64. RHI::Ptr<RHI::ImagePool> m_imagePool;
  65. RHI::Ptr<RHI::RayTracingBufferPools> m_rayTracingBufferPools;
  66. // triangle vertex/index buffers
  67. AZStd::array<VertexPosition, 3> m_triangleVertices;
  68. AZStd::array<uint16_t, 3> m_triangleIndices;
  69. RHI::Ptr<RHI::Buffer> m_triangleVB;
  70. RHI::Ptr<RHI::Buffer> m_triangleIB;
  71. // rectangle vertex/index buffers
  72. AZStd::array<VertexPosition, 4> m_rectangleVertices;
  73. AZStd::array<uint16_t, 6> m_rectangleIndices;
  74. RHI::Ptr<RHI::Buffer> m_rectangleVB;
  75. RHI::Ptr<RHI::Buffer> m_rectangleIB;
  76. // CLAS data
  77. using ClusterVertexType = PackedVector3f;
  78. using ClusterIndexType = PackedVector3<uint32_t>;
  79. AZStd::vector<RHI::RayTracingClasBuildTriangleClusterInfoExpanded> m_clusterSourceInfosExpanded;
  80. RHI::Ptr<RHI::Buffer> m_clusterVertexBuffer;
  81. RHI::Ptr<RHI::Buffer> m_clusterIndexBuffer;
  82. RHI::Ptr<RHI::Buffer> m_srcInfosArrayBuffer;
  83. uint32_t m_maxClusterTriangleCount = 0;
  84. uint32_t m_maxClusterVertexCount = 0;
  85. uint32_t m_maxGeometryIndex = 0;
  86. uint32_t m_maxTotalTriangleCount = 0;
  87. uint32_t m_maxTotalVertexCount = 0;
  88. // ray tracing acceleration structures
  89. RHI::Ptr<RHI::RayTracingBlas> m_triangleRayTracingBlas;
  90. RHI::Ptr<RHI::RayTracingBlas> m_rectangleRayTracingBlas;
  91. RHI::Ptr<RHI::RayTracingClusterBlas> m_clusterRayTracingBlas;
  92. bool m_clusterRayTracingBlasInitialized = false;
  93. RHI::Ptr<RHI::RayTracingTlas> m_rayTracingTlas;
  94. RHI::BufferViewDescriptor m_tlasBufferViewDescriptor;
  95. RHI::AttachmentId m_tlasBufferAttachmentId = RHI::AttachmentId("tlasBufferAttachmentId");
  96. // ray tracing shaders
  97. Data::Instance<RPI::Shader> m_rayGenerationShader;
  98. Data::Instance<RPI::Shader> m_missShader;
  99. Data::Instance<RPI::Shader> m_closestHitGradientShader;
  100. Data::Instance<RPI::Shader> m_closestHitSolidShader;
  101. // ray tracing pipeline state
  102. RHI::Ptr<RHI::RayTracingPipelineState> m_rayTracingPipelineState;
  103. // ray tracing shader table
  104. RHI::Ptr<RHI::RayTracingShaderTable> m_rayTracingShaderTable;
  105. // ray tracing global shader resource group and pipeline state
  106. Data::Instance<RPI::ShaderResourceGroup> m_globalSrg;
  107. RHI::ConstPtr<RHI::PipelineState> m_globalPipelineState;
  108. // ray tracing local shader resource groups, one for each object in the scene
  109. enum LocalSrgs
  110. {
  111. Triangle1,
  112. Triangle2,
  113. Triangle3,
  114. Rectangle,
  115. Count
  116. };
  117. AZStd::array<Data::Instance<RPI::ShaderResourceGroup>, LocalSrgs::Count> m_localSrgs;
  118. bool m_buildLocalSrgs = true;
  119. // output image, written to by the ray tracing shader and displayed in the fullscreen draw shader
  120. RHI::Ptr<RHI::Image> m_outputImage;
  121. RHI::Ptr<RHI::ImageView> m_outputImageView;
  122. RHI::ImageViewDescriptor m_outputImageViewDescriptor;
  123. RHI::AttachmentId m_outputImageAttachmentId = RHI::AttachmentId("outputImageAttachmentId");
  124. // fullscreen buffer for the raster pass to display the output image
  125. struct FullScreenBufferData
  126. {
  127. AZStd::array<VertexPosition, 4> m_positions;
  128. AZStd::array<VertexUV, 4> m_uvs;
  129. AZStd::array<uint16_t, 6> m_indices;
  130. };
  131. RHI::Ptr<RHI::Buffer> m_fullScreenInputAssemblyBuffer;
  132. RHI::GeometryView m_geometryView{ AZ::RHI::MultiDevice::AllDevices };
  133. RHI::InputStreamLayout m_fullScreenInputStreamLayout;
  134. RHI::ConstPtr<RHI::PipelineState> m_drawPipelineState;
  135. Data::Instance<RPI::ShaderResourceGroup> m_drawSRG;
  136. RHI::ShaderInputConstantIndex m_drawDimensionConstantIndex;
  137. // time variable for moving the triangles and rectangle each frame
  138. float m_time = 0.0f;
  139. };
  140. } // namespace AtomSampleViewer