RayTracingVertexAnimationExampleComponent.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Feature/Debug/RayTracingDebugFeatureProcessorInterface.h>
  10. #include <Atom/Feature/RayTracing/RayTracingFeatureProcessorInterface.h>
  11. #include <Atom/RPI.Public/Buffer/BufferPool.h>
  12. #include <AzCore/Math/PackedVector3.h>
  13. #include <CommonSampleComponentBase.h>
  14. #include <Passes/VertexAnimationPass.h>
  15. #include <Utils/ImGuiHistogramQueue.h>
  16. #include <Utils/ImGuiSidebar.h>
  17. namespace AtomSampleViewer
  18. {
  19. //! This test adds many instances of a basic shape to the ray-tracing scene and animates the shapes' vertices each frame. The
  20. //! visualization is done with the DebugRayTracingPass. The goal of this test is to benchmark the ray tracing acceleration structure
  21. //! build/update time for animated meshes.
  22. class RayTracingVertexAnimationExampleComponent
  23. : public CommonSampleComponentBase
  24. , public AZ::RPI::SceneNotificationBus::Handler
  25. , public AZ::TickBus::Handler
  26. {
  27. using Base = CommonSampleComponentBase;
  28. enum class AccelerationStructureType : int
  29. {
  30. TriangleBLAS,
  31. CLAS_ClusterBLAS,
  32. };
  33. struct PerformanceConfiguration
  34. {
  35. int m_geometryCount;
  36. AccelerationStructureType m_accelerationStructureType;
  37. };
  38. struct PerformanceResult
  39. {
  40. float m_buildTime;
  41. float m_traceRayTime;
  42. };
  43. public:
  44. AZ_COMPONENT(RayTracingVertexAnimationExampleComponent, "{9B43FED7-8BD0-4159-BDEC-BBF7EA39C1EC}", Base);
  45. static void Reflect(AZ::ReflectContext* context);
  46. RayTracingVertexAnimationExampleComponent();
  47. void Activate() override;
  48. void Deactivate() override;
  49. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) override;
  50. void OnRenderPipelineChanged(AZ::RPI::RenderPipeline* renderPipeline, RenderPipelineChangeType changeType) override;
  51. private:
  52. AZ_DISABLE_COPY_MOVE(RayTracingVertexAnimationExampleComponent);
  53. struct RayTracingMesh
  54. {
  55. AZ::Uuid m_uuid;
  56. AZ::Render::RayTracingFeatureProcessorInterface::Mesh m_rtMesh;
  57. AZ::Render::RayTracingFeatureProcessorInterface::SubMeshVector m_rtSubMeshes;
  58. };
  59. struct BasicGeometry
  60. {
  61. AZStd::vector<AZ::PackedVector3f> m_positions;
  62. AZStd::vector<AZ::PackedVector3f> m_normals;
  63. AZStd::vector<AZ::u32> m_indices;
  64. int m_verticesPerCluster{ 0 };
  65. int m_trianglesPerCluster{ 0 };
  66. int GetVertexCount() const;
  67. int GetVertexCountPerCluster() const;
  68. int GetIndexCount() const;
  69. int GetIndexCountPerCluster() const;
  70. int GetTriangleCount() const;
  71. int GetTriangleCountPerCluster() const;
  72. int GetClusterCount() const;
  73. };
  74. void SaveVSyncStateAndDisableVsync();
  75. void RestoreVSyncState();
  76. void GeneratePerformanceConfigurations();
  77. BasicGeometry GenerateBasicGeometry();
  78. void CreateBufferPools();
  79. void CreateRayTracingGeometry();
  80. void AddVertexAnimationPass(AZ::RPI::RenderPipeline* renderPipeline);
  81. void SetVertexAnimationPassData();
  82. void DrawSidebar();
  83. void UpdatePerformanceData();
  84. void PrintPerformanceResults();
  85. AZ::Render::MeshFeatureProcessorInterface& GetMeshFeatureProcessor();
  86. AZ::Render::RayTracingFeatureProcessorInterface& GetRayTracingFeatureProcessor();
  87. AZ::Render::RayTracingDebugFeatureProcessorInterface& GetRayTracingDebugFeatureProcessor();
  88. AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor{ nullptr };
  89. AZ::Render::RayTracingFeatureProcessorInterface* m_rayTracingFeatureProcessor{ nullptr };
  90. AZ::Render::RayTracingDebugFeatureProcessorInterface* m_rayTracingDebugFeatureProcessor{ nullptr };
  91. uint32_t m_preActivateVSyncInterval{ 0 };
  92. AZ::RHI::BufferBindFlags m_geometryDataBufferBindFlags{ AZ::RHI::BufferBindFlags::ShaderReadWrite |
  93. AZ::RHI::BufferBindFlags::DynamicInputAssembly };
  94. AZ::Data::Asset<AZ::RPI::ResourcePoolAsset> m_geometryDataBufferPoolAsset;
  95. AZ::Data::Instance<AZ::RPI::Buffer> m_sourceGeometryBuffer;
  96. AZ::Data::Instance<AZ::RPI::Buffer> m_targetGeometryBuffer;
  97. AZ::Data::Instance<AZ::RPI::Buffer> m_instanceOffsetDataBuffer;
  98. AZ::RHI::Ptr<AZ::RHI::Buffer> m_srcInfosArrayBuffer;
  99. AZ::RHI::Ptr<AZ::RHI::Buffer> m_clusterStreamOffsets;
  100. AZStd::vector<RayTracingMesh> m_rayTracingData;
  101. int m_geometryCount{ 100 };
  102. bool m_separateClusterBlasForEachInstance{ true };
  103. AZ::u32 m_vertexCountPerInstance;
  104. AZ::u32 m_targetVertexStridePerInstance;
  105. AZ::RPI::Ptr<AZ::Render::VertexAnimationPass> m_vertexAnimationPass;
  106. ImGuiSidebar m_imguiSidebar;
  107. AccelerationStructureType m_accelerationStructureType{ AccelerationStructureType::TriangleBLAS };
  108. AZ::Render::RayTracingDebugViewMode m_rayTracingDebugViewMode{ AZ::Render::RayTracingDebugViewMode::Barycentrics };
  109. unsigned m_histogramSampleCount{ 60 };
  110. ImGuiHistogramQueue m_imGuiFrameTimer{ m_histogramSampleCount, m_histogramSampleCount };
  111. ImGuiHistogramQueue m_accelerationStructurePassTimer{ m_histogramSampleCount, m_histogramSampleCount };
  112. ImGuiHistogramQueue m_rayTracingPassTimer{ m_histogramSampleCount, m_histogramSampleCount };
  113. AZ::RHI::Ptr<AZ::RPI::Pass> m_rayTracingAccelerationStructurePass;
  114. AZ::RHI::Ptr<AZ::RPI::Pass> m_debugRayTracingPass;
  115. AZStd::vector<PerformanceConfiguration> m_performanceConfigurations;
  116. AZStd::vector<PerformanceResult> m_performanceResults;
  117. int m_measureTicks{ 0 };
  118. int m_currentPerformanceConfiguration{ -1 };
  119. };
  120. } // namespace AtomSampleViewer