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 all 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
6 changes: 4 additions & 2 deletions impeller/entity/contents/filters/filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ std::shared_ptr<FilterContents> FilterContents::MakeDirectionalGaussianBlur(
BlurStyle blur_style,
Entity::TileMode tile_mode,
FilterInput::Ref source_override,
Sigma secondary_sigma,
const Matrix& effect_transform) {
auto blur = std::make_shared<DirectionalGaussianBlurFilterContents>();
blur->SetInputs({input});
Expand All @@ -87,6 +88,7 @@ std::shared_ptr<FilterContents> FilterContents::MakeDirectionalGaussianBlur(
blur->SetBlurStyle(blur_style);
blur->SetTileMode(tile_mode);
blur->SetSourceOverride(source_override);
blur->SetSecondarySigma(secondary_sigma);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: considerjust having a method that takes both sigmas so this doesn't get forgotten in a refactor.

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, I merged too quickly and missed this comment. I forgot to mark it private, but this param should be hidden as an implementation detail and never be used when making a 1D blur directly (only the factory for the 2D blur should ever use it), whereas the regular sigma param should remain as part of the API.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could have the second parameter be optional defaulted to zero right?

This is a nitpick though. It's ok to ignore if you think it should be.

blur->SetEffectTransform(effect_transform);
return blur;
}
Expand All @@ -100,10 +102,10 @@ std::shared_ptr<FilterContents> FilterContents::MakeGaussianBlur(
const Matrix& effect_transform) {
auto x_blur = MakeDirectionalGaussianBlur(input, sigma_x, Point(1, 0),
BlurStyle::kNormal, tile_mode,
nullptr, effect_transform);
nullptr, {}, effect_transform);
auto y_blur = MakeDirectionalGaussianBlur(FilterInput::Make(x_blur), sigma_y,
Point(0, 1), blur_style, tile_mode,
input, effect_transform);
input, sigma_x, effect_transform);
return y_blur;
}

Expand Down
1 change: 1 addition & 0 deletions impeller/entity/contents/filters/filter_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class FilterContents : public Contents {
BlurStyle blur_style = BlurStyle::kNormal,
Entity::TileMode tile_mode = Entity::TileMode::kDecal,
FilterInput::Ref alpha_mask = nullptr,
Sigma secondary_sigma = {},
const Matrix& effect_transform = Matrix());

static std::shared_ptr<FilterContents> MakeGaussianBlur(
Expand Down
36 changes: 24 additions & 12 deletions impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void DirectionalGaussianBlurFilterContents::SetSigma(Sigma sigma) {
blur_sigma_ = sigma;
}

void DirectionalGaussianBlurFilterContents::SetSecondarySigma(Sigma sigma) {
secondary_blur_sigma_ = sigma;
}

void DirectionalGaussianBlurFilterContents::SetDirection(Vector2 direction) {
blur_direction_ = direction.Normalize();
if (blur_direction_.IsZero()) {
Expand Down Expand Up @@ -221,12 +225,22 @@ std::optional<Snapshot> DirectionalGaussianBlurFilterContents::RenderFilter(
return pass.AddCommand(cmd);
};

Scalar x_scale =
1.0 /
std::ceil(std::log2(std::max(2.0f, transformed_blur_radius_length)));
auto scaled_texture_width = pass_texture_rect.size.width * x_scale;
auto out_texture = renderer.MakeSubpass(
ISize(scaled_texture_width, pass_texture_rect.size.height), callback);
Vector2 scale;
{
scale.x =
1.0 /
std::ceil(std::log2(std::max(2.0f, transformed_blur_radius_length)));

Scalar y_radius = std::abs(pass_transform.GetDirectionScale(Vector2(
0, source_override_ ? Radius{secondary_blur_sigma_}.radius : 1)));
scale.y = 1.0 / std::ceil(std::log2(std::max(2.0f, y_radius)));
}

Vector2 scaled_size = pass_texture_rect.size * scale;
ISize floored_size = ISize(scaled_size.x, scaled_size.y);

auto out_texture = renderer.MakeSubpass(floored_size, callback);

if (!out_texture) {
return std::nullopt;
}
Expand All @@ -238,12 +252,10 @@ std::optional<Snapshot> DirectionalGaussianBlurFilterContents::RenderFilter(

return Snapshot{
.texture = out_texture,
.transform = texture_rotate.Invert() *
Matrix::MakeTranslation(pass_texture_rect.origin) *
Matrix::MakeScale(Vector2(
(1 / x_scale) * (scaled_texture_width /
std::floor(scaled_texture_width)),
1)),
.transform =
texture_rotate.Invert() *
Matrix::MakeTranslation(pass_texture_rect.origin) *
Matrix::MakeScale((1 / scale) * (scaled_size / floored_size)),
.sampler_descriptor = sampler_desc};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class DirectionalGaussianBlurFilterContents final : public FilterContents {

void SetSigma(Sigma sigma);

void SetSecondarySigma(Sigma sigma);

void SetDirection(Vector2 direction);

void SetBlurStyle(BlurStyle blur_style);
Expand All @@ -42,6 +44,7 @@ class DirectionalGaussianBlurFilterContents final : public FilterContents {
const Matrix& effect_transform,
const Rect& coverage) const override;
Sigma blur_sigma_;
Sigma secondary_blur_sigma_;
Vector2 blur_direction_;
BlurStyle blur_style_ = BlurStyle::kNormal;
Entity::TileMode tile_mode_ = Entity::TileMode::kDecal;
Expand Down