QueryExampleComponent.h 5.0 KB

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