RayTracingAccelerationStructurePass.h 4.1 KB

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