QueryExampleComponent.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <AzCore/Component/Component.h>
  10. #include <AzCore/Component/TickBus.h>
  11. #include <AzCore/std/containers/array.h>
  12. #include <AzCore/std/chrono/clocks.h>
  13. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  14. #include <Atom/RHI.Reflect/ShaderResourceGroupLayoutDescriptor.h>
  15. #include <Atom/RHI.Reflect/ScopeId.h>
  16. #include <Atom/RHI/Buffer.h>
  17. #include <Atom/RHI/BufferPool.h>
  18. #include <Atom/RHI/DrawItem.h>
  19. #include <Atom/RHI/PipelineState.h>
  20. #include <Atom/RHI/QueryPool.h>
  21. #include <Atom/RHI/Query.h>
  22. #include <Atom/RHI/StreamBufferView.h>
  23. #include <RHI/BasicRHIComponent.h>
  24. #include <ExampleComponentBus.h>
  25. #include <Utils/ImGuiSidebar.h>
  26. namespace AZ
  27. {
  28. namespace RHI
  29. {
  30. class FrameGraphBuilder;
  31. }
  32. }
  33. namespace AtomSampleViewer
  34. {
  35. /*
  36. * QueryPool RHI example.
  37. * Test use of QueryPools for occlusion queries, predication, timestamp information and pipeline
  38. * statistics.
  39. * Three quads are rendered in the following order:
  40. * First: a quad in the back that may be occluded.
  41. * Second: a quad in the front that may occlude the one in the back.
  42. * Third: a bounding box quad that represent the first quad. This quad doesn't write to the rendertargets
  43. * and is used to write to the current occlusion query.
  44. *
  45. * Predication is available for platforms that support it. A separate scope is in charge of copying the data
  46. * from the QueryPool to a buffer that is used later for predication.
  47. * A sidebar is used for enable/disable the different options.
  48. */
  49. class QueryExampleComponent final
  50. : public BasicRHIComponent
  51. , public AZ::TickBus::Handler
  52. {
  53. public:
  54. AZ_COMPONENT(QueryExampleComponent, "{5ECAF824-8D1A-49CB-86D6-42511865638D}", AZ::Component);
  55. AZ_DISABLE_COPY(QueryExampleComponent);
  56. static void Reflect(AZ::ReflectContext* context);
  57. QueryExampleComponent();
  58. ~QueryExampleComponent() override = default;
  59. protected:
  60. enum class QueryType : uint32_t
  61. {
  62. Occlusion,
  63. Predication
  64. };
  65. // AZ::Component overrides ...
  66. void Activate() override;
  67. void Deactivate() override;
  68. // RHISystemNotificationBus::Handler ...
  69. void OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder) override;
  70. // AZ::TickBus::Handler ...
  71. void OnTick(float deltaTime, AZ::ScriptTimePoint time);
  72. void CreateGeometryResources();
  73. void CreateShaderResources();
  74. void CreateCopyBufferScopeProducer();
  75. void CreateScopeProducer();
  76. void CreateQueryResources();
  77. void CreatePredicationResources();
  78. void SetQueryType(QueryType type);
  79. void DrawSampleSettings();
  80. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_inputAssemblyBufferPool;
  81. AZ::RHI::Ptr<AZ::RHI::Buffer> m_quadInputAssemblyBuffer;
  82. struct BufferData
  83. {
  84. AZStd::array<VertexPosition, 4> m_positions;
  85. AZStd::array<uint16_t, 6> m_indices;
  86. };
  87. AZ::RHI::StreamBufferView m_quadStreamBufferView;
  88. AZ::RHI::InputStreamLayout m_quadInputStreamLayout;
  89. AZStd::array<AZ::Data::Instance<AZ::RPI::ShaderResourceGroup>, 3> m_shaderResourceGroups;
  90. AZ::RHI::ShaderInputConstantIndex m_objectMatrixConstantIndex;
  91. AZ::RHI::ShaderInputConstantIndex m_colorConstantIndex;
  92. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_quadPipelineState;
  93. AZ::RHI::ConstPtr<AZ::RHI::PipelineState> m_boudingBoxPipelineState;
  94. AZ::RHI::Ptr<AZ::RHI::QueryPool> m_occlusionQueryPool;
  95. AZ::RHI::Ptr<AZ::RHI::QueryPool> m_timeStampQueryPool;
  96. AZ::RHI::Ptr<AZ::RHI::QueryPool> m_statisticsQueryPool;
  97. struct QueryEntry
  98. {
  99. AZ::RHI::Ptr<AZ::RHI::Query> m_query;
  100. bool m_isValid = false; // Indicates if the query has been written at least once.
  101. };
  102. AZStd::array<QueryEntry, AZ::RHI::Limits::Device::FrameCountMax> m_occlusionQueries;
  103. AZStd::array<QueryEntry, AZ::RHI::Limits::Device::FrameCountMax * 2> m_timestampQueries;
  104. AZStd::array<QueryEntry, AZ::RHI::Limits::Device::FrameCountMax> m_statisticsQueries;
  105. uint32_t m_currentOcclusionQueryIndex = 0;
  106. uint32_t m_currentTimestampQueryIndex = 0;
  107. uint32_t m_currentStatisticsQueryIndex = 0;
  108. QueryType m_currentType = QueryType::Occlusion;
  109. AZ::RHI::Ptr<AZ::RHI::BufferPool> m_predicationBufferPool;
  110. AZ::RHI::Ptr<AZ::RHI::Buffer> m_predicationBuffer;
  111. AZ::RHI::BufferScopeAttachmentDescriptor m_predicationBufferAttachmentDescriptor;
  112. bool m_timestampEnabled = false;
  113. bool m_pipelineStatisticsEnabled = false;
  114. bool m_precisionOcclusionEnabled = false;
  115. float m_elapsedTime = 0.0f;
  116. ImGuiSidebar m_imguiSidebar;
  117. };
  118. } // namespace AtomSampleViewer