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] improve blur performance for Android and iPad Pro. #39291
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -183,13 +183,12 @@ std::optional<Snapshot> DirectionalGaussianBlurFilterContents::RenderFilter( | |
|
|
||
| VS::FrameInfo frame_info; | ||
| frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1)); | ||
|
|
||
| FS::FragInfo frag_info; | ||
| frag_info.texture_sampler_y_coord_scale = | ||
| frame_info.texture_sampler_y_coord_scale = | ||
| input_snapshot->texture->GetYCoordScale(); | ||
| frag_info.alpha_mask_sampler_y_coord_scale = | ||
| frame_info.alpha_mask_sampler_y_coord_scale = | ||
| source_snapshot->texture->GetYCoordScale(); | ||
|
|
||
| FS::FragInfo frag_info; | ||
| auto r = Radius{transformed_blur_radius_length}; | ||
| frag_info.blur_sigma = Sigma{r}.sigma; | ||
| frag_info.blur_radius = r.radius; | ||
|
|
@@ -198,7 +197,6 @@ std::optional<Snapshot> DirectionalGaussianBlurFilterContents::RenderFilter( | |
| frag_info.blur_direction = | ||
| pass_transform.Invert().TransformDirection(Vector2(1, 0)).Normalize(); | ||
|
|
||
| frag_info.tile_mode = static_cast<Scalar>(tile_mode_); | ||
| frag_info.src_factor = src_color_factor_; | ||
| frag_info.inner_blur_factor = inner_blur_factor_; | ||
| frag_info.outer_blur_factor = outer_blur_factor_; | ||
|
|
@@ -207,19 +205,48 @@ std::optional<Snapshot> DirectionalGaussianBlurFilterContents::RenderFilter( | |
| Command cmd; | ||
| cmd.label = SPrintF("Gaussian Blur Filter (Radius=%.2f)", | ||
| transformed_blur_radius_length); | ||
| cmd.BindVertices(vtx_buffer); | ||
|
|
||
| auto options = OptionsFromPass(pass); | ||
| options.blend_mode = BlendMode::kSource; | ||
| cmd.pipeline = renderer.GetGaussianBlurPipeline(options); | ||
| cmd.BindVertices(vtx_buffer); | ||
| auto input_descriptor = input_snapshot->sampler_descriptor; | ||
| auto source_descriptor = source_snapshot->sampler_descriptor; | ||
| switch (tile_mode_) { | ||
| case Entity::TileMode::kDecal: | ||
| cmd.pipeline = renderer.GetGaussianBlurDecalPipeline(options); | ||
| break; | ||
| case Entity::TileMode::kClamp: | ||
| cmd.pipeline = renderer.GetGaussianBlurPipeline(options); | ||
| input_descriptor.width_address_mode = SamplerAddressMode::kClampToEdge; | ||
| input_descriptor.height_address_mode = SamplerAddressMode::kClampToEdge; | ||
| source_descriptor.width_address_mode = SamplerAddressMode::kClampToEdge; | ||
| source_descriptor.height_address_mode = | ||
| SamplerAddressMode::kClampToEdge; | ||
| break; | ||
| case Entity::TileMode::kMirror: | ||
| cmd.pipeline = renderer.GetGaussianBlurPipeline(options); | ||
| input_descriptor.width_address_mode = SamplerAddressMode::kMirror; | ||
| input_descriptor.height_address_mode = SamplerAddressMode::kMirror; | ||
| source_descriptor.width_address_mode = SamplerAddressMode::kMirror; | ||
| source_descriptor.height_address_mode = SamplerAddressMode::kMirror; | ||
| break; | ||
| case Entity::TileMode::kRepeat: | ||
| cmd.pipeline = renderer.GetGaussianBlurPipeline(options); | ||
| input_descriptor.width_address_mode = SamplerAddressMode::kRepeat; | ||
| input_descriptor.height_address_mode = SamplerAddressMode::kRepeat; | ||
| source_descriptor.width_address_mode = SamplerAddressMode::kRepeat; | ||
| source_descriptor.height_address_mode = SamplerAddressMode::kRepeat; | ||
| break; | ||
| } | ||
|
|
||
| FS::BindTextureSampler( | ||
| cmd, input_snapshot->texture, | ||
| renderer.GetContext()->GetSamplerLibrary()->GetSampler( | ||
| input_snapshot->sampler_descriptor)); | ||
| input_descriptor)); | ||
| FS::BindAlphaMaskSampler( | ||
| cmd, source_snapshot->texture, | ||
| renderer.GetContext()->GetSamplerLibrary()->GetSampler( | ||
| source_snapshot->sampler_descriptor)); | ||
| source_descriptor)); | ||
| VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info)); | ||
| FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info)); | ||
|
|
||
|
|
@@ -281,7 +308,7 @@ std::optional<Rect> DirectionalGaussianBlurFilterContents::GetFilterCoverage( | |
|
|
||
| auto transform = inputs[0]->GetTransform(entity) * effect_transform; | ||
| auto transformed_blur_vector = | ||
| transform.TransformDirection(blur_direction_ * Radius{blur_sigma_}.radius) | ||
| transform.TransformDirection(blur_direction_* Radius{blur_sigma_}.radius) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Weird change. Is the formatter forcing this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup :( |
||
| .Abs(); | ||
| auto extent = coverage->size + transformed_blur_vector * 2; | ||
| return Rect(coverage->origin - transformed_blur_vector, | ||
|
|
||
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,70 @@ | ||
| // 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. | ||
|
|
||
| // 1D (directional) gaussian blur. | ||
| // | ||
| // Paths for future optimization: | ||
| // * Remove the uv bounds multiplier in SampleColor by adding optional | ||
| // support for SamplerAddressMode::ClampToBorder in the texture sampler. | ||
| // * Render both blur passes into a smaller texture than the source image | ||
| // (~1/radius size). | ||
| // * If doing the small texture render optimization, cache misses can be | ||
| // reduced in the first pass by sampling the source textures with a mip | ||
| // level of log2(min_radius). | ||
|
|
||
| #include <impeller/constants.glsl> | ||
| #include <impeller/gaussian.glsl> | ||
| #include <impeller/texture.glsl> | ||
| #include <impeller/types.glsl> | ||
|
|
||
| uniform sampler2D texture_sampler; | ||
| uniform sampler2D alpha_mask_sampler; | ||
|
|
||
| uniform FragInfo { | ||
| vec2 texture_size; | ||
| vec2 blur_direction; | ||
|
|
||
| // The blur sigma and radius have a linear relationship which is defined | ||
| // host-side, but both are useful controls here. Sigma (pixels per standard | ||
| // deviation) is used to define the gaussian function itself, whereas the | ||
| // radius is used to limit how much of the function is integrated. | ||
| float blur_sigma; | ||
| float blur_radius; | ||
|
|
||
| float src_factor; | ||
| float inner_blur_factor; | ||
| float outer_blur_factor; | ||
| } | ||
| frag_info; | ||
|
|
||
| in vec2 v_texture_coords; | ||
| in vec2 v_src_texture_coords; | ||
|
|
||
| out vec4 frag_color; | ||
|
|
||
| void main() { | ||
| vec4 total_color = vec4(0); | ||
| float gaussian_integral = 0; | ||
| vec2 blur_uv_offset = frag_info.blur_direction / frag_info.texture_size; | ||
|
|
||
| for (float i = -frag_info.blur_radius; i <= frag_info.blur_radius; i++) { | ||
| float gaussian = IPGaussian(i, frag_info.blur_sigma); | ||
| gaussian_integral += gaussian; | ||
| total_color += | ||
| gaussian * | ||
| Sample(texture_sampler, // sampler | ||
| v_texture_coords + blur_uv_offset * i // texture coordinates | ||
| ); | ||
| } | ||
|
|
||
| vec4 blur_color = total_color / gaussian_integral; | ||
|
|
||
| vec4 src_color = Sample(alpha_mask_sampler, // sampler | ||
| v_src_texture_coords // texture coordinates | ||
| ); | ||
| float blur_factor = frag_info.inner_blur_factor * float(src_color.a > 0) + | ||
| frag_info.outer_blur_factor * float(src_color.a == 0); | ||
|
|
||
| frag_color = blur_color * blur_factor + src_color * frag_info.src_factor; | ||
| } |
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,11 @@ | ||
| // 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/texture.glsl> | ||
|
|
||
| vec4 Sample(sampler2D tex, vec2 coords) { | ||
| return IPSampleDecal(tex, coords); | ||
| } | ||
|
|
||
| #include "gaussian_blur.glsl" |
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.
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.
Oh yeah, we should totally be doing this! This has come up a couple of times before when the blur was under pressure.