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] Implement draw order optimization. #54067
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6790f13
[Impeller] Implement draw order optimization.
bdero 38e33e6
Use offsets instead of pointers
bdero 19754c0
Refactor into separate TU and add tests.
bdero 2679d8f
Fix use-after-move
bdero 0357f8b
Correct tests
bdero d918b47
Fix depth writing for TextureContents
bdero ae467b2
Disable depth writing for the MSAA backdrop case
bdero cffcd69
blank
bdero b641611
Correct doc strings
bdero 70d065e
Fix comments
bdero 2a3239a
Switch order for kSolid blur style in the 2-pass blur
bdero 4fa2da3
Handle backdrop restores and disable when FramebufferFetch is not ava…
bdero 68656ff
Fix crasher for backdrop restore
bdero d874c32
Respect the clear color optimization in the fallback
bdero 3e5d0ab
When resolving the final list, only apply the values to the root laye…
bdero 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,77 @@ | ||
| // 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/entity/draw_order_resolver.h" | ||
|
|
||
| #include "flutter/fml/logging.h" | ||
| #include "impeller/base/validation.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| DrawOrderResolver::DrawOrderResolver() : draw_order_layers_({{}}){}; | ||
|
|
||
| void DrawOrderResolver::AddElement(size_t element_index, bool is_opaque) { | ||
| DrawOrderLayer& layer = draw_order_layers_.back(); | ||
| if (is_opaque) { | ||
| layer.opaque_elements.push_back(element_index); | ||
| } else { | ||
| layer.dependent_elements.push_back(element_index); | ||
| } | ||
| } | ||
| void DrawOrderResolver::PushClip(size_t element_index) { | ||
| draw_order_layers_.back().dependent_elements.push_back(element_index); | ||
| draw_order_layers_.push_back({}); | ||
| }; | ||
|
|
||
| void DrawOrderResolver::PopClip() { | ||
| if (draw_order_layers_.size() == 1u) { | ||
| // This is likely recoverable, so don't assert. | ||
| VALIDATION_LOG | ||
| << "Attemped to pop the root draw order layer. This is a bug in " | ||
| "`EntityPass`."; | ||
| return; | ||
| } | ||
|
|
||
| DrawOrderLayer& layer = draw_order_layers_.back(); | ||
| DrawOrderLayer& parent_layer = | ||
| draw_order_layers_[draw_order_layers_.size() - 2]; | ||
|
|
||
| layer.WriteCombinedDraws(parent_layer.dependent_elements, 0, 0); | ||
|
|
||
| draw_order_layers_.pop_back(); | ||
| } | ||
|
|
||
| DrawOrderResolver::ElementRefs DrawOrderResolver::GetSortedDraws( | ||
| size_t opaque_skip_count, | ||
| size_t translucent_skip_count) const { | ||
| FML_DCHECK(draw_order_layers_.size() == 1u); | ||
|
|
||
| ElementRefs sorted_elements; | ||
| draw_order_layers_.back().WriteCombinedDraws( | ||
| sorted_elements, opaque_skip_count, translucent_skip_count); | ||
|
|
||
| return sorted_elements; | ||
| } | ||
|
|
||
| void DrawOrderResolver::DrawOrderLayer::WriteCombinedDraws( | ||
| ElementRefs& destination, | ||
| size_t opaque_skip_count, | ||
| size_t translucent_skip_count) const { | ||
| FML_DCHECK(opaque_skip_count <= opaque_elements.size()); | ||
| FML_DCHECK(translucent_skip_count <= dependent_elements.size()); | ||
|
|
||
| destination.reserve(destination.size() + // | ||
| opaque_elements.size() - opaque_skip_count + // | ||
| dependent_elements.size()); | ||
|
|
||
| // Draw backdrop-independent elements first. | ||
| destination.insert(destination.end(), opaque_elements.rbegin(), | ||
| opaque_elements.rend() - opaque_skip_count); | ||
| // Then, draw backdrop-dependent elements in their original order. | ||
| destination.insert(destination.end(), | ||
| dependent_elements.begin() + translucent_skip_count, | ||
| dependent_elements.end()); | ||
| } | ||
|
|
||
| } // 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,85 @@ | ||||||||
| // 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. | ||||||||
|
|
||||||||
| #ifndef FLUTTER_IMPELLER_ENTITY_DRAW_ORDER_RESOLVER_H_ | ||||||||
| #define FLUTTER_IMPELLER_ENTITY_DRAW_ORDER_RESOLVER_H_ | ||||||||
|
|
||||||||
| #include <vector> | ||||||||
|
|
||||||||
| namespace impeller { | ||||||||
|
|
||||||||
| /// Helper that records draw indices in painter's order and sorts the draws into | ||||||||
| /// an optimized order based on translucency and clips. | ||||||||
| class DrawOrderResolver { | ||||||||
| public: | ||||||||
| using ElementRefs = std::vector<size_t>; | ||||||||
|
|
||||||||
| DrawOrderResolver(); | ||||||||
|
|
||||||||
| void AddElement(size_t element_index, bool is_opaque); | ||||||||
|
|
||||||||
| void PushClip(size_t element_index); | ||||||||
|
|
||||||||
| void PopClip(); | ||||||||
|
|
||||||||
| //------------------------------------------------------------------------- | ||||||||
| /// @brief Returns the sorted draws for the current draw order layer. | ||||||||
| /// This should only be called after all recording has finished. | ||||||||
| /// | ||||||||
| /// @param[in] opaque_skip_count The number of opaque elements to skip | ||||||||
| /// when appending the combined elements. | ||||||||
| /// This is used for the "clear color" | ||||||||
| /// optimization. | ||||||||
| /// @param[in] translucent_skip_count The number of translucent elements to | ||||||||
| /// skip when appending the combined | ||||||||
| /// elements. This is used for the | ||||||||
| /// "clear color" optimization. | ||||||||
| /// | ||||||||
| ElementRefs GetSortedDraws(size_t opaque_skip_count, | ||||||||
| size_t translucent_skip_count) const; | ||||||||
|
|
||||||||
| private: | ||||||||
| /// A data structure for collecting sorted draws for a given "draw order | ||||||||
| /// layer". Currently these layers just correspond to the local clip stack. | ||||||||
| struct DrawOrderLayer { | ||||||||
| /// The list of backdrop-independent elements (always just opaque). These | ||||||||
| /// are order independent, and so we draw them optimally render these | ||||||||
| /// elements in reverse painter's order so that they cull one another | ||||||||
|
||||||||
| /// are order independent, and so we draw them optimally render these | |
| /// elements in reverse painter's order so that they cull one another | |
| /// are order independent, and so we draw them optimally in reverse painter's order so that they cull one another |
sentence seems weird
Member
Author
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.
Outdated
Contributor
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.
The elements of all child draw layers will be resolved to this list
What does this mean?
Member
Author
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.
Meant to say child clips. Removed, it's redundant anyway.
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,115 @@ | ||
| // 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 "flutter/testing/testing.h" | ||
| #include "impeller/entity/draw_order_resolver.h" | ||
| #include "third_party/googletest/googletest/include/gtest/gtest.h" | ||
|
|
||
| namespace impeller { | ||
| namespace testing { | ||
|
|
||
| TEST(DrawOrderResolverTest, GetSortedDrawsReturnsCorrectOrderWithNoClips) { | ||
| DrawOrderResolver resolver; | ||
|
|
||
| // Opaque items. | ||
| resolver.AddElement(0, true); | ||
| resolver.AddElement(1, true); | ||
| // Translucent items. | ||
| resolver.AddElement(2, false); | ||
| resolver.AddElement(3, false); | ||
|
|
||
| auto sorted_elements = resolver.GetSortedDraws(0, 0); | ||
|
|
||
| EXPECT_EQ(sorted_elements.size(), 4u); | ||
| // First, the opaque items are drawn in reverse order. | ||
| EXPECT_EQ(sorted_elements[0], 1u); | ||
| EXPECT_EQ(sorted_elements[1], 0u); | ||
| // Then the translucent items are drawn. | ||
| EXPECT_EQ(sorted_elements[2], 2u); | ||
| EXPECT_EQ(sorted_elements[3], 3u); | ||
| } | ||
|
|
||
| TEST(DrawOrderResolverTest, GetSortedDrawsReturnsCorrectOrderWithClips) { | ||
| DrawOrderResolver resolver; | ||
|
|
||
| // Items before clip. | ||
| resolver.AddElement(0, false); | ||
| resolver.AddElement(1, true); | ||
| resolver.AddElement(2, false); | ||
| resolver.AddElement(3, true); | ||
|
|
||
| // Clip. | ||
| resolver.PushClip(4); | ||
| { | ||
| // Clipped items. | ||
| resolver.AddElement(5, false); | ||
| resolver.AddElement(6, false); | ||
| // Clipped translucent items. | ||
| resolver.AddElement(7, true); | ||
| resolver.AddElement(8, true); | ||
| } | ||
| resolver.PopClip(); | ||
|
|
||
| // Items after clip. | ||
| resolver.AddElement(9, true); | ||
| resolver.AddElement(10, false); | ||
| resolver.AddElement(11, true); | ||
| resolver.AddElement(12, false); | ||
|
|
||
| auto sorted_elements = resolver.GetSortedDraws(0, 0); | ||
|
|
||
| EXPECT_EQ(sorted_elements.size(), 13u); | ||
| // First, all the non-clipped opaque items are drawn in reverse order. | ||
| EXPECT_EQ(sorted_elements[0], 11u); | ||
| EXPECT_EQ(sorted_elements[1], 9u); | ||
| EXPECT_EQ(sorted_elements[2], 3u); | ||
| EXPECT_EQ(sorted_elements[3], 1u); | ||
| // Then, non-clipped translucent items that came before the clip are drawn in | ||
| // their original order. | ||
| EXPECT_EQ(sorted_elements[4], 0u); | ||
| EXPECT_EQ(sorted_elements[5], 2u); | ||
|
|
||
| // Then, the clip and its sorted child items are drawn. | ||
| EXPECT_EQ(sorted_elements[6], 4u); | ||
| { | ||
| // Opaque clipped items are drawn in reverse order. | ||
| EXPECT_EQ(sorted_elements[7], 8u); | ||
| EXPECT_EQ(sorted_elements[8], 7u); | ||
| // Translucent clipped items are drawn. | ||
| EXPECT_EQ(sorted_elements[9], 5u); | ||
| EXPECT_EQ(sorted_elements[10], 6u); | ||
| } | ||
| // Finally, the non-clipped translucent items which came after the clip are | ||
| // drawn in their original order. | ||
| EXPECT_EQ(sorted_elements[11], 10u); | ||
| EXPECT_EQ(sorted_elements[12], 12u); | ||
| } | ||
|
|
||
| TEST(DrawOrderResolverTest, GetSortedDrawsRespectsSkipCounts) { | ||
| DrawOrderResolver resolver; | ||
|
|
||
| // These items will be skipped. | ||
| resolver.AddElement(0, false); | ||
| resolver.AddElement(1, true); | ||
| resolver.AddElement(2, false); | ||
| // These ones will be included in the final draw list. | ||
| resolver.AddElement(3, false); | ||
| resolver.AddElement(4, true); | ||
| resolver.AddElement(5, true); | ||
|
|
||
| // Form the draw list, skipping elements 0, 1, and 2. | ||
| // This emulates what happens when entitypass applies the clear color | ||
| // optimization. | ||
| auto sorted_elements = resolver.GetSortedDraws(1, 2); | ||
|
|
||
| EXPECT_EQ(sorted_elements.size(), 3u); | ||
| // First, opaque items are drawn in reverse order. | ||
| EXPECT_EQ(sorted_elements[0], 5u); | ||
| EXPECT_EQ(sorted_elements[1], 4u); | ||
| // Then, translucent items are drawn. | ||
| EXPECT_EQ(sorted_elements[2], 3u); | ||
| } | ||
|
|
||
| } // namespace testing | ||
| } // namespace impeller |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Here's where depth writing gets turned on for all opaque draws.