-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Impeller] Reland 2: Implement draw order optimization. (#54268)
This time for sure! For each clip scope, draw opaque items in reverse order and translucent/backdrop-independent items in their original order afterwards. Clips are treated as translucent by the parent scope. Respects clips, subpass collapse, and the clear color optimization. Attempt 1: #54136 Revert 1: #54067 Attempt 2: #54215 Revert 2: #54261
- Loading branch information
Showing
15 changed files
with
635 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,124 @@ | ||
// 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 first draw order clip 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(); | ||
} | ||
|
||
void DrawOrderResolver::Flush() { | ||
FML_DCHECK(draw_order_layers_.size() >= 1u); | ||
|
||
size_t layer_count = draw_order_layers_.size(); | ||
|
||
// Pop all clip layers. | ||
while (draw_order_layers_.size() > 1u) { | ||
PopClip(); | ||
} | ||
|
||
// Move the root layer items into the sorted list. | ||
DrawOrderLayer& layer = draw_order_layers_.back(); | ||
if (!first_root_flush_.has_value()) { | ||
// Record the first flush. | ||
first_root_flush_ = std::move(layer); | ||
layer = {}; | ||
} else { | ||
// Write subsequent flushes into the sorted root list. | ||
layer.WriteCombinedDraws(sorted_elements_, 0, 0); | ||
layer.opaque_elements.clear(); | ||
layer.dependent_elements.clear(); | ||
} | ||
|
||
// Refill with empty layers. | ||
draw_order_layers_.resize(layer_count); | ||
} | ||
|
||
DrawOrderResolver::ElementRefs DrawOrderResolver::GetSortedDraws( | ||
size_t opaque_skip_count, | ||
size_t translucent_skip_count) const { | ||
FML_DCHECK(draw_order_layers_.size() == 1u) | ||
<< "Attempted to get sorted draws before all clips were popped."; | ||
|
||
ElementRefs sorted_elements; | ||
sorted_elements.reserve( | ||
(first_root_flush_.has_value() | ||
? first_root_flush_->opaque_elements.size() + | ||
first_root_flush_->dependent_elements.size() | ||
: 0u) + | ||
sorted_elements_.size() + | ||
draw_order_layers_.back().opaque_elements.size() + | ||
draw_order_layers_.back().dependent_elements.size()); | ||
|
||
// Write all flushed items. | ||
if (first_root_flush_.has_value()) { | ||
first_root_flush_->WriteCombinedDraws(sorted_elements, opaque_skip_count, | ||
translucent_skip_count); | ||
} | ||
sorted_elements.insert(sorted_elements.end(), sorted_elements_.begin(), | ||
sorted_elements_.end()); | ||
|
||
// Write any remaining non-flushed items. | ||
draw_order_layers_.back().WriteCombinedDraws( | ||
sorted_elements, first_root_flush_.has_value() ? 0 : opaque_skip_count, | ||
first_root_flush_.has_value() ? 0 : 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() - translucent_skip_count); | ||
|
||
// Draw backdrop-independent elements in reverse order 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 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,94 @@ | ||
// 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 <optional> | ||
#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(); | ||
|
||
void Flush(); | ||
|
||
//------------------------------------------------------------------------- | ||
/// @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 render these elements in reverse | ||
/// painter's order so that they cull one another. | ||
ElementRefs opaque_elements; | ||
|
||
/// The list of backdrop-dependent elements with respect to this draw | ||
/// order layer. These elements are drawn after all of the independent | ||
/// elements. | ||
ElementRefs dependent_elements; | ||
|
||
//----------------------------------------------------------------------- | ||
/// @brief Appends the combined opaque and transparent elements into | ||
/// a final destination buffer. | ||
/// | ||
/// @param[in] destination The buffer to append the combined | ||
/// elements to. | ||
/// @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. | ||
/// | ||
void WriteCombinedDraws(ElementRefs& destination, | ||
size_t opaque_skip_count, | ||
size_t translucent_skip_count) const; | ||
}; | ||
std::vector<DrawOrderLayer> draw_order_layers_; | ||
|
||
// The first time the root layer is flushed, the layer contents are stored | ||
// here. This is done to enable element skipping for the clear color | ||
// optimization. | ||
std::optional<DrawOrderLayer> first_root_flush_; | ||
// All subsequent root flushes are stored here. | ||
ElementRefs sorted_elements_; | ||
|
||
DrawOrderResolver(const DrawOrderResolver&) = delete; | ||
|
||
DrawOrderResolver& operator=(const DrawOrderResolver&) = delete; | ||
}; | ||
|
||
} // namespace impeller | ||
|
||
#endif // FLUTTER_IMPELLER_ENTITY_DRAW_ORDER_RESOLVER_H_ |
Oops, something went wrong.