This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] Add GPU frame time to Vulkan backend using timestamp queries. #46796
Merged
auto-submit
merged 13 commits into
flutter:main
from
jonahwilliams:add_start_end_trace_events
Oct 13, 2023
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4df93fc
[Impeller] add GPU start/end trace events.
995bb5e
++
4060e8b
timeline events.
dd6e71d
++
096c7e6
++
915652f
++
12870c3
dnfield review
4910e58
++
ccb89a5
Merge branch 'main' of github.com:flutter/engine into add_start_end_t…
e35c2ef
Update gpu_tracer_vk.cc
fc83a0c
Merge branch 'add_start_end_trace_events' of github.com:jonahwilliams…
f02f609
++
635ebb0
++
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "impeller/renderer/backend/vulkan/gpu_tracer_vk.h" | ||
|
|
||
| #include <utility> | ||
| #include "fml/trace_event.h" | ||
| #include "impeller/base/validation.h" | ||
| #include "impeller/renderer/backend/vulkan/command_buffer_vk.h" | ||
| #include "impeller/renderer/backend/vulkan/command_encoder_vk.h" | ||
| #include "impeller/renderer/backend/vulkan/context_vk.h" | ||
| #include "impeller/renderer/command_buffer.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| static constexpr uint32_t kPoolSize = 128u; | ||
|
|
||
| GPUTracerVK::GPUTracerVK(const std::weak_ptr<ContextVK>& context) | ||
| : context_(context) { | ||
| auto strong_context = context_.lock(); | ||
| if (!strong_context) { | ||
| return; | ||
| } | ||
| timestamp_period_ = strong_context->GetPhysicalDevice() | ||
| .getProperties() | ||
| .limits.timestampPeriod; | ||
| if (timestamp_period_ <= 0) { | ||
| // The device does not support timestamp queries. | ||
| return; | ||
| } | ||
| vk::QueryPoolCreateInfo info; | ||
| info.queryCount = kPoolSize; | ||
| info.queryType = vk::QueryType::eTimestamp; | ||
|
|
||
| auto [status, pool] = strong_context->GetDevice().createQueryPoolUnique(info); | ||
| if (status != vk::Result::eSuccess) { | ||
| VALIDATION_LOG << "Failed to create query pool."; | ||
| return; | ||
| } | ||
| query_pool_ = std::move(pool); | ||
| // Disable tracing in release mode. | ||
| #ifdef IMPELLER_DEBUG | ||
| valid_ = true; | ||
| #endif | ||
| } | ||
|
|
||
| void GPUTracerVK::RecordStartFrameTime() { | ||
| if (!valid_) { | ||
| return; | ||
| } | ||
| auto strong_context = context_.lock(); | ||
| if (!strong_context) { | ||
| return; | ||
| } | ||
| auto buffer = strong_context->CreateCommandBuffer(); | ||
| auto vk_trace_cmd_buffer = | ||
| CommandBufferVK::Cast(*buffer).GetEncoder()->GetCommandBuffer(); | ||
| // The two commands below are executed in order, such that writeTimeStamp is | ||
| // guaranteed to occur after resetQueryPool has finished. The validation | ||
| // layer seem particularly strict, and efforts to reset the entire pool | ||
| // were met with validation errors (though seemingly correct measurements). | ||
| // To work around this, the tracer only resets the query that will be | ||
| // used next. | ||
| vk_trace_cmd_buffer.resetQueryPool(query_pool_.get(), current_index_, 1); | ||
| vk_trace_cmd_buffer.writeTimestamp(vk::PipelineStageFlagBits::eTopOfPipe, | ||
| query_pool_.get(), current_index_); | ||
|
|
||
| if (!buffer->SubmitCommands()) { | ||
| VALIDATION_LOG << "GPUTracerVK: Failed to record start time."; | ||
| } | ||
|
|
||
| // The logic in RecordEndFrameTime requires us to have recorded a pair of | ||
| // tracing events. If this method failed for any reason we need to be sure we | ||
| // don't attempt to record and read back a second value, or we will get values | ||
| // that span multiple frames. | ||
| started_frame_ = true; | ||
| } | ||
|
|
||
| void GPUTracerVK::RecordEndFrameTime() { | ||
| if (!valid_ || !started_frame_) { | ||
| return; | ||
| } | ||
| auto strong_context = context_.lock(); | ||
| if (!strong_context) { | ||
| return; | ||
| } | ||
|
|
||
| started_frame_ = false; | ||
| auto last_query = current_index_; | ||
| current_index_ += 1; | ||
|
|
||
| auto buffer = strong_context->CreateCommandBuffer(); | ||
| auto vk_trace_cmd_buffer = | ||
| CommandBufferVK::Cast(*buffer).GetEncoder()->GetCommandBuffer(); | ||
| vk_trace_cmd_buffer.resetQueryPool(query_pool_.get(), current_index_, 1); | ||
| vk_trace_cmd_buffer.writeTimestamp(vk::PipelineStageFlagBits::eBottomOfPipe, | ||
| query_pool_.get(), current_index_); | ||
|
|
||
| // On completion of the second time stamp recording, we read back this value | ||
| // and the previous value. The difference is approximately the frame time. | ||
| const auto device_holder = strong_context->GetDeviceHolder(); | ||
| if (!buffer->SubmitCommands([&, last_query, | ||
| device_holder](CommandBuffer::Status status) { | ||
| auto strong_context = context_.lock(); | ||
| if (!strong_context) { | ||
| return; | ||
| } | ||
| uint64_t bits[2] = {0, 0}; | ||
| auto result = device_holder->GetDevice().getQueryPoolResults( | ||
| query_pool_.get(), last_query, 2, sizeof(bits), &bits, | ||
| sizeof(int64_t), vk::QueryResultFlagBits::e64); | ||
|
|
||
| if (result == vk::Result::eSuccess) { | ||
| // This value should probably be available in some form besides a | ||
| // timeline event but that is a job for a future Jonah. | ||
| auto gpu_ms = (((bits[1] - bits[0]) * timestamp_period_) / 1000000); | ||
| FML_TRACE_COUNTER("flutter", "GPUTracer", | ||
| 1234, // Trace Counter ID | ||
| "FrameTimeMS", gpu_ms); | ||
| } | ||
| })) { | ||
| if (!buffer->SubmitCommands()) { | ||
| VALIDATION_LOG << "GPUTracerVK failed to record frame end time."; | ||
| } | ||
| } | ||
|
|
||
| if (current_index_ == kPoolSize - 1) { | ||
| current_index_ = 0u; | ||
| } | ||
| } | ||
|
|
||
| } // namespace impeller | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "impeller/renderer/backend/vulkan/context_vk.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| /// @brief A class that uses timestamp queries to record the approximate GPU | ||
| /// execution time. | ||
| class GPUTracerVK { | ||
| public: | ||
| explicit GPUTracerVK(const std::weak_ptr<ContextVK>& context); | ||
|
|
||
| ~GPUTracerVK() = default; | ||
|
|
||
| /// @brief Record the approximate start time of the GPU workload for the | ||
| /// current frame. | ||
| void RecordStartFrameTime(); | ||
|
|
||
| /// @brief Record the approximate end time of the GPU workload for the current | ||
| /// frame. | ||
| void RecordEndFrameTime(); | ||
|
|
||
| private: | ||
| void ResetQueryPool(size_t pool); | ||
|
|
||
| const std::weak_ptr<ContextVK> context_; | ||
| vk::UniqueQueryPool query_pool_ = {}; | ||
|
|
||
| size_t current_index_ = 0u; | ||
| // The number of nanoseconds for each timestamp unit. | ||
| float timestamp_period_ = 1; | ||
| bool started_frame_ = false; | ||
| bool valid_ = false; | ||
| }; | ||
|
|
||
| } // namespace impeller |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably just be
reinterpret_cast<int64_t>(this), or at least a named constant somewhere.