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] GPUTracer for Metal. #46846
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cf77065
[Impeller] GPUTracer for Metal.
78a4d15
++
e3ae43d
++
d7186e7
weak from this
172a87b
maybe unused.
bd37576
Update gpu_tracer_mtl.h
8e1c67b
++
0b46742
Merge branch 'main' of github.com:flutter/engine into gpu_tracing_metal
3caf282
move ifdef
0d3b630
Merge branch 'main' into gpu_tracing_metal
0d1da88
Merge branch 'main' into gpu_tracing_metal
076eeb7
Update surface_mtl.mm
212fe99
Update surface_mtl.mm
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,51 @@ | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <Metal/Metal.h> | ||
|
|
||
| #include <memory> | ||
| #include <optional> | ||
| #include "impeller/base/thread.h" | ||
| #include "impeller/base/thread_safety.h" | ||
| #include "impeller/geometry/scalar.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| class ContextMTL; | ||
|
|
||
| /// @brief Approximate the GPU frame time by computing a difference between the | ||
| /// smallest | ||
| /// GPUStartTime and largest GPUEndTime for all cmd buffers submitted in | ||
| /// a frame workload. | ||
| class GPUTracerMTL : public std::enable_shared_from_this<GPUTracerMTL> { | ||
| public: | ||
| GPUTracerMTL() = default; | ||
|
|
||
| ~GPUTracerMTL() = default; | ||
|
|
||
| /// @brief Record that the current frame has ended. Any additional cmd buffers | ||
| /// will be | ||
| /// attributed to the "next" frame. | ||
| void MarkFrameEnd(); | ||
|
|
||
| /// @brief Record the current cmd buffer GPU execution timestamps into an | ||
| /// aggregate | ||
| /// frame workload metric. | ||
| void RecordCmdBuffer(id<MTLCommandBuffer> buffer); | ||
|
|
||
| private: | ||
| struct GPUTraceState { | ||
| Scalar smallest_timestamp = std::numeric_limits<float>::max(); | ||
| Scalar largest_timestamp = 0; | ||
| size_t pending_buffers = 0; | ||
| }; | ||
|
|
||
| mutable Mutex trace_state_mutex_; | ||
| GPUTraceState trace_states_[16] IPLR_GUARDED_BY(trace_state_mutex_); | ||
| size_t current_state_ IPLR_GUARDED_BY(trace_state_mutex_) = 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,56 @@ | ||
| // 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 <Metal/Metal.h> | ||
| #include "fml/trace_event.h" | ||
| #include "impeller/renderer/backend/metal/context_mtl.h" | ||
| #include "impeller/renderer/backend/metal/formats_mtl.h" | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "impeller/renderer/backend/metal/gpu_tracer_mtl.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| void GPUTracerMTL::MarkFrameEnd() { | ||
| if (@available(ios 10.3, tvos 10.2, macos 10.15, macCatalyst 13.0, *)) { | ||
| Lock lock(trace_state_mutex_); | ||
| current_state_ = (current_state_ + 1) % 16; | ||
| } | ||
| } | ||
|
|
||
| void GPUTracerMTL::RecordCmdBuffer(id<MTLCommandBuffer> buffer) { | ||
| if (@available(ios 10.3, tvos 10.2, macos 10.15, macCatalyst 13.0, *)) { | ||
| Lock lock(trace_state_mutex_); | ||
| auto current_state = current_state_; | ||
| trace_states_[current_state].pending_buffers += 1; | ||
|
|
||
| auto weak_self = weak_from_this(); | ||
| [buffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) { | ||
| auto self = weak_self.lock(); | ||
| if (!self) { | ||
| return; | ||
| } | ||
| Lock lock(self->trace_state_mutex_); | ||
| auto& state = self->trace_states_[current_state]; | ||
| state.pending_buffers--; | ||
| state.smallest_timestamp = std::min( | ||
| state.smallest_timestamp, static_cast<Scalar>(buffer.GPUStartTime)); | ||
| state.largest_timestamp = std::max( | ||
| state.largest_timestamp, static_cast<Scalar>(buffer.GPUEndTime)); | ||
|
|
||
| if (state.pending_buffers == 0) { | ||
| auto gpu_ms = | ||
| (state.largest_timestamp - state.smallest_timestamp) * 1000; | ||
| state.smallest_timestamp = std::numeric_limits<float>::max(); | ||
| state.largest_timestamp = 0; | ||
| FML_TRACE_COUNTER("flutter", "GPUTracer", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it should be a constant somewhere so that MTL and VK use the same one always
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where would you want this? I can put it in core?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Im gonna punt on this - the trace event will probably change a few times |
||
| reinterpret_cast<int64_t>(this), // Trace Counter ID | ||
| "FrameTimeMS", gpu_ms); | ||
| } | ||
| }]; | ||
| } | ||
| } | ||
|
|
||
| } // 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.
I think this has some issues with GPUTracerMTL being deleted (or retained), but I'm not quite sure how to solve that with object-c / c++ interop.
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.
Fixed, thanks @gaaclarke !