[Impeller] Frustum culling (flutter/engine#34733)

This commit is contained in:
Brandon DeRosier
2022-07-18 19:11:30 -07:00
committed by GitHub
parent 309a65a183
commit 70e78ec2bb
7 changed files with 40 additions and 2 deletions

View File

@@ -33,9 +33,14 @@ void ClipContents::SetClipOperation(Entity::ClipOperation clip_op) {
}
std::optional<Rect> ClipContents::GetCoverage(const Entity& entity) const {
return path_.GetTransformedBoundingBox(entity.GetTransformation());
return std::nullopt;
};
bool ClipContents::ShouldRender(const Entity& entity,
const RenderPass& pass) const {
return true;
}
bool ClipContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
@@ -109,6 +114,11 @@ std::optional<Rect> ClipRestoreContents::GetCoverage(
return std::nullopt;
};
bool ClipRestoreContents::ShouldRender(const Entity& entity,
const RenderPass& pass) const {
return true;
}
bool ClipRestoreContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {

View File

@@ -27,6 +27,10 @@ class ClipContents final : public Contents {
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
bool ShouldRender(const Entity& entity,
const RenderPass& pass) const override;
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,
@@ -48,6 +52,10 @@ class ClipRestoreContents final : public Contents {
// |Contents|
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
bool ShouldRender(const Entity& entity,
const RenderPass& pass) const override;
// |Contents|
bool Render(const ContentContext& renderer,
const Entity& entity,

View File

@@ -57,4 +57,12 @@ std::optional<Snapshot> Contents::RenderToSnapshot(
.transform = Matrix::MakeTranslation(bounds->origin)};
}
bool Contents::ShouldRender(const Entity& entity,
const RenderPass& pass) const {
auto coverage = GetCoverage(entity);
return coverage.has_value() &&
Rect::MakeSize(Size(pass.GetRenderTargetSize()))
.IntersectsWithRect(coverage.value());
}
} // namespace impeller

View File

@@ -47,6 +47,8 @@ class Contents {
const ContentContext& renderer,
const Entity& entity) const;
virtual bool ShouldRender(const Entity& entity, const RenderPass& pass) const;
protected:
private:

View File

@@ -41,6 +41,10 @@ std::optional<Rect> Entity::GetCoverage() const {
return contents_->GetCoverage(*this);
}
bool Entity::ShouldRender(const RenderPass& pass) const {
return contents_->ShouldRender(*this, pass);
}
void Entity::SetContents(std::shared_ptr<Contents> contents) {
contents_ = std::move(contents);
}

View File

@@ -81,6 +81,8 @@ class Entity {
std::optional<Rect> GetCoverage() const;
bool ShouldRender(const RenderPass& pass) const;
void SetContents(std::shared_ptr<Contents> contents);
const std::shared_ptr<Contents>& GetContents() const;

View File

@@ -358,11 +358,15 @@ bool EntityPass::OnRender(ContentContext& renderer,
}
auto render_element = [&stencil_depth_floor, &pass_context, &pass_depth,
&renderer](Entity element_entity) {
&renderer](Entity& element_entity) {
element_entity.SetStencilDepth(element_entity.GetStencilDepth() -
stencil_depth_floor);
auto pass = pass_context.GetRenderPass(pass_depth);
if (!element_entity.ShouldRender(*pass)) {
return true; // Nothing to render.
}
if (!element_entity.Render(renderer, *pass)) {
return false;
}