RayTracingAccelerationStructurePass.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/ScopeProducer.h>
  10. #include <Atom/RPI.Public/GpuQuery/Query.h>
  11. #include <Atom/RPI.Public/Pass/Pass.h>
  12. #include <Atom/RPI.Public/Buffer/Buffer.h>
  13. namespace AZ
  14. {
  15. namespace Render
  16. {
  17. //! This pass builds the RayTracing acceleration structures for a scene
  18. class RayTracingAccelerationStructurePass final
  19. : public RPI::Pass
  20. , public RHI::ScopeProducer
  21. {
  22. public:
  23. AZ_RPI_PASS(RayTracingAccelerationStructurePass);
  24. using ScopeQuery = AZStd::array<AZ::RHI::Ptr<AZ::RPI::Query>, static_cast<size_t>(AZ::RPI::ScopeQueryType::Count)>;
  25. AZ_RTTI(RayTracingAccelerationStructurePass, "{6BAA1755-D7D2-497F-BCDB-CA28B42728DC}", Pass);
  26. AZ_CLASS_ALLOCATOR(RayTracingAccelerationStructurePass, SystemAllocator);
  27. //! Creates a RayTracingAccelerationStructurePass
  28. static RPI::Ptr<RayTracingAccelerationStructurePass> Create(const RPI::PassDescriptor& descriptor);
  29. ~RayTracingAccelerationStructurePass() = default;
  30. void AddScopeQueryToFrameGraph(AZ::RHI::FrameGraphInterface frameGraph);
  31. private:
  32. explicit RayTracingAccelerationStructurePass(const RPI::PassDescriptor& descriptor);
  33. // Scope producer functions
  34. void SetupFrameGraphDependencies(RHI::FrameGraphInterface frameGraph) override;
  35. void BuildCommandList(const RHI::FrameGraphExecuteContext& context) override;
  36. // Pass overrides
  37. void BuildInternal() override;
  38. void FrameBeginInternal(FramePrepareParams params) override;
  39. // Helper function to get the query by the scope index and query type
  40. AZ::RHI::Ptr<AZ::RPI::Query> GetQuery(AZ::RPI::ScopeQueryType queryType);
  41. // Executes a lambda depending on the passed ScopeQuery types
  42. template<typename Func>
  43. void ExecuteOnTimestampQuery(Func&& func);
  44. template<typename Func>
  45. void ExecuteOnPipelineStatisticsQuery(Func&& func);
  46. RPI::TimestampResult GetTimestampResultInternal() const override;
  47. RPI::PipelineStatisticsResult GetPipelineStatisticsResultInternal() const override;
  48. // Begin recording commands for the ScopeQueries
  49. void BeginScopeQuery(const AZ::RHI::FrameGraphExecuteContext& context);
  50. // End recording commands for the ScopeQueries
  51. void EndScopeQuery(const AZ::RHI::FrameGraphExecuteContext& context);
  52. // Readback the results from the ScopeQueries
  53. void ReadbackScopeQueryResults();
  54. // Used to set some build options for the TLASes
  55. static AZ::RHI::RayTracingAccelerationStructureBuildFlags CreateRayTracingAccelerationStructureBuildFlags(bool isSkinnedMesh);
  56. // buffer view descriptor for the TLAS
  57. RHI::BufferViewDescriptor m_tlasBufferViewDescriptor;
  58. // revision number of the ray tracing data when the TLAS was built
  59. uint32_t m_rayTracingRevision = 0;
  60. // revision out of date
  61. bool m_rayTracingRevisionOutDated{ false };
  62. // keeps track of the current frame to determine updates or rebuilds of the skinned BLASes
  63. uint64_t m_frameCount = 0;
  64. // the bits of this constant are used to check if a skinned BLAS is going to be rebuilt in any given frame
  65. static constexpr uint32_t SKINNED_BLAS_REBUILD_FRAME_INTERVAL = 8;
  66. // Readback results from the Timestamp queries
  67. AZ::RPI::TimestampResult m_timestampResult{};
  68. // Readback results from the PipelineStatistics queries
  69. AZ::RPI::PipelineStatisticsResult m_statisticsResult{};
  70. // The device index the pass ran on during the last frame, necessary to read the queries.
  71. int m_lastDeviceIndex = RHI::MultiDevice::DefaultDeviceIndex;
  72. // For each ScopeProducer an instance of the ScopeQuery is created, which consists
  73. // of a Timestamp and PipelineStatistic query.
  74. ScopeQuery m_scopeQueries;
  75. };
  76. } // namespace RPI
  77. } // namespace AZ