Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ impeller_component("entity") {
"entity.h",
"entity_pass.cc",
"entity_pass.h",
"entity_pass_clip_stack.cc",
"entity_pass_clip_stack.h",
"entity_pass_delegate.cc",
"entity_pass_delegate.h",
"entity_pass_target.cc",
Expand Down
138 changes: 18 additions & 120 deletions impeller/entity/entity_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "impeller/base/strings.h"
#include "impeller/base/validation.h"
#include "impeller/core/formats.h"
#include "impeller/entity/contents/clip_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/filters/color_filter_contents.h"
#include "impeller/entity/contents/filters/inputs/filter_input.h"
Expand Down Expand Up @@ -409,9 +408,8 @@ bool EntityPass::Render(ContentContext& renderer,
return true;
});

ClipCoverageStack clip_coverage_stack = {ClipCoverageLayer{
.coverage = Rect::MakeSize(root_render_target.GetRenderTargetSize()),
.clip_depth = 0}};
clip_stack_->Initialize(
Rect::MakeSize(root_render_target.GetRenderTargetSize()));
Copy link
Member

@bdero bdero Mar 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch moves from per-pass tracking of replay clips to a single list of tracked replay clips reused across all passes. So when replaying clips we need to be careful that only clips which impact the current pass are replayed, otherwise there may be perf issues if StC is turned on and fidelity bugs if StC is turned off.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, taking a closer look, maybe you're already handling this correctly. I see the state is held in a per-pass vector.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this mistake initially but I believe thatI've fully corrected it. The clip stacks and record/replay entities are stored per-subpass, and for each subpass we push/pop a new set of data.


bool reads_from_onscreen_backdrop = GetTotalPassReads(renderer) > 0;
// In this branch path, we need to render everything to an offscreen texture
Expand All @@ -430,8 +428,7 @@ bool EntityPass::Render(ContentContext& renderer,
offscreen_target, // pass_target
Point(), // global_pass_position
Point(), // local_pass_position
0, // pass_depth
clip_coverage_stack // clip_coverage_stack
0 // pass_depth
)) {
// Validation error messages are triggered for all `OnRender()` failure
// cases.
Expand Down Expand Up @@ -536,8 +533,7 @@ bool EntityPass::Render(ContentContext& renderer,
pass_target, // pass_target
Point(), // global_pass_position
Point(), // local_pass_position
0, // pass_depth
clip_coverage_stack); // clip_coverage_stack
0); // pass_depth
}

EntityPass::EntityResult EntityPass::GetEntityForElement(
Expand All @@ -548,7 +544,6 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
ISize root_pass_size,
Point global_pass_position,
uint32_t pass_depth,
ClipCoverageStack& clip_coverage_stack,
size_t clip_depth_floor) const {
//--------------------------------------------------------------------------
/// Setup entity element.
Expand Down Expand Up @@ -590,7 +585,6 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
global_pass_position, // global_pass_position
Point(), // local_pass_position
pass_depth, // pass_depth
clip_coverage_stack, // clip_coverage_stack
clip_depth_, // clip_depth_floor
nullptr, // backdrop_filter_contents
pass_context.GetRenderPass(pass_depth) // collapsed_parent_pass
Expand Down Expand Up @@ -625,13 +619,13 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
pass_context.EndPass();
}

if (clip_coverage_stack.empty()) {
if (!clip_stack_->HasCoverage()) {
// The current clip is empty. This means the pass texture won't be
// visible, so skip it.
capture.CreateChild("Subpass Entity (Skipped: Empty clip A)");
return EntityPass::EntityResult::Skip();
}
auto clip_coverage_back = clip_coverage_stack.back().coverage;
auto clip_coverage_back = clip_stack_->CurrentClipCoverage();
if (!clip_coverage_back.has_value()) {
capture.CreateChild("Subpass Entity (Skipped: Empty clip B)");
return EntityPass::EntityResult::Skip();
Expand Down Expand Up @@ -690,8 +684,7 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
// save layers may transform the subpass texture after it's rendered,
// causing parent clip coverage to get misaligned with the actual area that
// the subpass will affect in the parent pass.
ClipCoverageStack subpass_clip_coverage_stack = {ClipCoverageLayer{
.coverage = subpass_coverage, .clip_depth = subpass->clip_depth_}};
clip_stack_->PushSubpass(subpass_coverage, subpass->clip_depth_);

// Stencil textures aren't shared between EntityPasses (as much of the
// time they are transient).
Expand All @@ -704,14 +697,14 @@ EntityPass::EntityResult EntityPass::GetEntityForElement(
subpass_coverage->GetOrigin() -
global_pass_position, // local_pass_position
++pass_depth, // pass_depth
subpass_clip_coverage_stack, // clip_coverage_stack
subpass->clip_depth_, // clip_depth_floor
subpass_backdrop_filter_contents // backdrop_filter_contents
)) {
// Validation error messages are triggered for all `OnRender()` failure
// cases.
return EntityPass::EntityResult::Failure();
}
clip_stack_->PopSubpass();

// The subpass target's texture may have changed during OnRender.
auto subpass_texture =
Expand Down Expand Up @@ -757,7 +750,6 @@ bool EntityPass::RenderElement(Entity& element_entity,
InlinePassContext& pass_context,
int32_t pass_depth,
ContentContext& renderer,
ClipCoverageStack& clip_coverage_stack,
Point global_pass_position) const {
auto result = pass_context.GetRenderPass(pass_depth);
if (!result.pass) {
Expand All @@ -770,7 +762,7 @@ bool EntityPass::RenderElement(Entity& element_entity,
if (result.just_created) {
// Restore any clips that were recorded before the backdrop filter was
// applied.
auto& replay_entities = clip_replay_->GetReplayEntities();
auto& replay_entities = clip_stack_->GetReplayEntities();
for (const auto& entity : replay_entities) {
if (!entity.Render(renderer, *result.pass)) {
VALIDATION_LOG << "Failed to render entity for clip restore.";
Expand Down Expand Up @@ -801,7 +793,7 @@ bool EntityPass::RenderElement(Entity& element_entity,
}
}

auto current_clip_coverage = clip_coverage_stack.back().coverage;
auto current_clip_coverage = clip_stack_->CurrentClipCoverage();
if (current_clip_coverage.has_value()) {
// Entity transforms are relative to the current pass position, so we need
// to check clip coverage in the same space.
Expand All @@ -826,81 +818,12 @@ bool EntityPass::RenderElement(Entity& element_entity,
element_entity.GetContents()->SetCoverageHint(
Rect::Intersection(element_coverage_hint, current_clip_coverage));

switch (clip_coverage.type) {
case Contents::ClipCoverage::Type::kNoChange:
break;
case Contents::ClipCoverage::Type::kAppend: {
auto op = clip_coverage_stack.back().coverage;
clip_coverage_stack.push_back(
ClipCoverageLayer{.coverage = clip_coverage.coverage,
.clip_depth = element_entity.GetClipDepth() + 1});
FML_DCHECK(clip_coverage_stack.back().clip_depth ==
clip_coverage_stack.front().clip_depth +
clip_coverage_stack.size() - 1);

if (!op.has_value()) {
// Running this append op won't impact the clip buffer because the
// whole screen is already being clipped, so skip it.
return true;
}
} break;
case Contents::ClipCoverage::Type::kRestore: {
if (clip_coverage_stack.back().clip_depth <=
element_entity.GetClipDepth()) {
// Drop clip restores that will do nothing.
return true;
}

auto restoration_index = element_entity.GetClipDepth() -
clip_coverage_stack.front().clip_depth;
FML_DCHECK(restoration_index < clip_coverage_stack.size());

// We only need to restore the area that covers the coverage of the
// clip rect at target depth + 1.
std::optional<Rect> restore_coverage =
(restoration_index + 1 < clip_coverage_stack.size())
? clip_coverage_stack[restoration_index + 1].coverage
: std::nullopt;
if (restore_coverage.has_value()) {
// Make the coverage rectangle relative to the current pass.
restore_coverage = restore_coverage->Shift(-global_pass_position);
}
clip_coverage_stack.resize(restoration_index + 1);

if constexpr (ContentContext::kEnableStencilThenCover) {
// Skip all clip restores when stencil-then-cover is enabled.
if (clip_coverage_stack.back().coverage.has_value()) {
clip_replay_->RecordEntity(element_entity, clip_coverage.type);
}
return true;
}

if (!clip_coverage_stack.back().coverage.has_value()) {
// Running this restore op won't make anything renderable, so skip it.
return true;
}

auto restore_contents =
static_cast<ClipRestoreContents*>(element_entity.GetContents().get());
restore_contents->SetRestoreCoverage(restore_coverage);

} break;
}

#ifdef IMPELLER_ENABLE_CAPTURE
{
auto element_entity_coverage = element_entity.GetCoverage();
if (element_entity_coverage.has_value()) {
element_entity_coverage =
element_entity_coverage->Shift(global_pass_position);
element_entity.GetCapture().AddRect("Coverage", *element_entity_coverage,
{.readonly = true});
}
if (!clip_stack_->AppendClipCoverage(clip_coverage, element_entity,
clip_depth_floor,
global_pass_position)) {
return true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe store the 1-line comment explaining why this state is terminal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
#endif

element_entity.SetClipDepth(element_entity.GetClipDepth() - clip_depth_floor);
clip_replay_->RecordEntity(element_entity, clip_coverage.type);
if (!element_entity.Render(renderer, *result.pass)) {
VALIDATION_LOG << "Failed to render entity.";
return false;
Expand All @@ -916,7 +839,6 @@ bool EntityPass::OnRender(
Point global_pass_position,
Point local_pass_position,
uint32_t pass_depth,
ClipCoverageStack& clip_coverage_stack,
size_t clip_depth_floor,
std::shared_ptr<Contents> backdrop_filter_contents,
const std::optional<InlinePassContext::RenderPassResult>&
Expand Down Expand Up @@ -964,7 +886,7 @@ bool EntityPass::OnRender(
backdrop_entity.SetNewClipDepth(std::numeric_limits<uint32_t>::max());

RenderElement(backdrop_entity, clip_depth_floor, pass_context, pass_depth,
renderer, clip_coverage_stack, global_pass_position);
renderer, global_pass_position);
}

bool is_collapsing_clear_colors = !collapsed_parent_pass &&
Expand All @@ -990,7 +912,6 @@ bool EntityPass::OnRender(
root_pass_size, // root_pass_size
global_pass_position, // global_pass_position
pass_depth, // pass_depth
clip_coverage_stack, // clip_coverage_stack
clip_depth_floor); // clip_depth_floor

switch (result.status) {
Expand Down Expand Up @@ -1058,8 +979,7 @@ bool EntityPass::OnRender(
/// Render the Element.
///
if (!RenderElement(result.entity, clip_depth_floor, pass_context,
pass_depth, renderer, clip_coverage_stack,
global_pass_position)) {
pass_depth, renderer, global_pass_position)) {
// Specific validation logs are handled in `render_element()`.
return false;
}
Expand Down Expand Up @@ -1256,30 +1176,8 @@ void EntityPass::SetEnableOffscreenCheckerboard(bool enabled) {
enable_offscreen_debug_checkerboard_ = enabled;
}

const EntityPassClipRecorder& EntityPass::GetEntityPassClipRecorder() const {
return *clip_replay_;
}

EntityPassClipRecorder::EntityPassClipRecorder() {}

void EntityPassClipRecorder::RecordEntity(const Entity& entity,
Contents::ClipCoverage::Type type) {
switch (type) {
case Contents::ClipCoverage::Type::kNoChange:
return;
case Contents::ClipCoverage::Type::kAppend:
rendered_clip_entities_.push_back(entity.Clone());
break;
case Contents::ClipCoverage::Type::kRestore:
if (!rendered_clip_entities_.empty()) {
rendered_clip_entities_.pop_back();
}
break;
}
}

const std::vector<Entity>& EntityPassClipRecorder::GetReplayEntities() const {
return rendered_clip_entities_;
const EntityPassClipStack& EntityPass::GetEntityPassClipRecorder() const {
return *clip_stack_;
}

} // namespace impeller
Loading