[Impeller] Implements gaussian blur that scales down before applying the blur (flutter/engine#47576)
A partial implementation of the new gaussian blur effect. This should perform enough of the code to start getting some performance numbers. Known outstanding problems: 1) The edges of the blur are clipped. I have notes on how I plan on expanding render space in the PR. 1) Animating the sigma causes some "jumping around artifacts" resulting from the downsampling (maybe the discrete nature of texture pixel size?) 1) Coverage hints are ignored. I think depth tests will make that not much of an issue. 1) We aren't ping ponging textures yet 1) The snapshot's transform is ignored. issue: https://github.com/flutter/flutter/issues/131580 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -140,6 +140,7 @@
|
||||
../../../flutter/impeller/docs
|
||||
../../../flutter/impeller/entity/contents/checkerboard_contents_unittests.cc
|
||||
../../../flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents_unittests.cc
|
||||
../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents_unittests.cc
|
||||
../../../flutter/impeller/entity/contents/filters/inputs/filter_input_unittests.cc
|
||||
../../../flutter/impeller/entity/contents/test
|
||||
../../../flutter/impeller/entity/contents/vertices_contents_unittests.cc
|
||||
|
||||
@@ -3062,6 +3062,8 @@ ORIGIN: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_b
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/filter_contents.cc + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/filter_contents.h + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h + ../../../flutter/LICENSE
|
||||
ORIGIN: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc + ../../../flutter/LICENSE
|
||||
@@ -5829,6 +5831,8 @@ FILE: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_blu
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.cc
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/filter_contents.h
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/gaussian_blur_filter_contents.h
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.cc
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/contents_filter_input.h
|
||||
FILE: ../../../flutter/impeller/entity/contents/filters/inputs/filter_contents_filter_input.cc
|
||||
|
||||
@@ -134,6 +134,8 @@ impeller_component("entity") {
|
||||
"contents/filters/directional_gaussian_blur_filter_contents.h",
|
||||
"contents/filters/filter_contents.cc",
|
||||
"contents/filters/filter_contents.h",
|
||||
"contents/filters/gaussian_blur_filter_contents.cc",
|
||||
"contents/filters/gaussian_blur_filter_contents.h",
|
||||
"contents/filters/inputs/contents_filter_input.cc",
|
||||
"contents/filters/inputs/contents_filter_input.h",
|
||||
"contents/filters/inputs/filter_contents_filter_input.cc",
|
||||
@@ -255,6 +257,7 @@ impeller_component("entity_unittests") {
|
||||
sources = [
|
||||
"contents/checkerboard_contents_unittests.cc",
|
||||
"contents/filters/directional_gaussian_blur_filter_contents_unittests.cc",
|
||||
"contents/filters/gaussian_blur_filter_contents_unittests.cc",
|
||||
"contents/filters/inputs/filter_input_unittests.cc",
|
||||
"contents/vertices_contents_unittests.cc",
|
||||
"entity_pass_target_unittests.cc",
|
||||
|
||||
@@ -185,6 +185,7 @@ std::optional<Entity> DirectionalGaussianBlurFilterContents::RenderFilter(
|
||||
auto r = Radius{transformed_blur_radius_length};
|
||||
frag_info.blur_sigma = Sigma{r}.sigma;
|
||||
frag_info.blur_radius = std::round(r.radius);
|
||||
frag_info.step_size = 2.0;
|
||||
|
||||
// The blur direction is in input UV space.
|
||||
frag_info.blur_uv_offset =
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "impeller/entity/contents/content_context.h"
|
||||
#include "impeller/entity/contents/filters/border_mask_blur_filter_contents.h"
|
||||
#include "impeller/entity/contents/filters/directional_gaussian_blur_filter_contents.h"
|
||||
#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h"
|
||||
#include "impeller/entity/contents/filters/inputs/filter_input.h"
|
||||
#include "impeller/entity/contents/filters/local_matrix_filter_contents.h"
|
||||
#include "impeller/entity/contents/filters/matrix_filter_contents.h"
|
||||
@@ -55,6 +56,20 @@ std::shared_ptr<FilterContents> FilterContents::MakeGaussianBlur(
|
||||
Sigma sigma_y,
|
||||
BlurStyle blur_style,
|
||||
Entity::TileMode tile_mode) {
|
||||
constexpr bool use_new_filter =
|
||||
#ifdef IMPELLER_ENABLE_NEW_GAUSSIAN_FILTER
|
||||
true;
|
||||
#else
|
||||
false;
|
||||
#endif
|
||||
|
||||
// TODO(https://github.com/flutter/flutter/issues/131580): Remove once the new
|
||||
// blur handles all cases.
|
||||
if (use_new_filter) {
|
||||
auto blur = std::make_shared<GaussianBlurFilterContents>(sigma_x.sigma);
|
||||
blur->SetInputs({input});
|
||||
return blur;
|
||||
}
|
||||
std::shared_ptr<FilterContents> x_blur = MakeDirectionalGaussianBlur(
|
||||
/*input=*/input,
|
||||
/*sigma=*/sigma_x,
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
// 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/contents/filters/gaussian_blur_filter_contents.h"
|
||||
|
||||
#include "impeller/entity/contents/content_context.h"
|
||||
#include "impeller/entity/texture_fill.frag.h"
|
||||
#include "impeller/entity/texture_fill.vert.h"
|
||||
#include "impeller/renderer/command.h"
|
||||
#include "impeller/renderer/render_pass.h"
|
||||
#include "impeller/renderer/sampler_library.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
using GaussianBlurVertexShader = GaussianBlurPipeline::VertexShader;
|
||||
using GaussianBlurFragmentShader = GaussianBlurPipeline::FragmentShader;
|
||||
|
||||
namespace {
|
||||
SamplerDescriptor MakeSamplerDescriptor(MinMagFilter filter,
|
||||
SamplerAddressMode address_mode) {
|
||||
SamplerDescriptor sampler_desc;
|
||||
sampler_desc.min_filter = filter;
|
||||
sampler_desc.mag_filter = filter;
|
||||
sampler_desc.width_address_mode = address_mode;
|
||||
sampler_desc.height_address_mode = address_mode;
|
||||
return sampler_desc;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void BindVertices(Command& cmd,
|
||||
HostBuffer& host_buffer,
|
||||
std::initializer_list<typename T::PerVertexData>&& vertices) {
|
||||
VertexBufferBuilder<typename T::PerVertexData> vtx_builder;
|
||||
vtx_builder.AddVertices(vertices);
|
||||
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
|
||||
cmd.BindVertices(vtx_buffer);
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> MakeDownsampleSubpass(
|
||||
const ContentContext& renderer,
|
||||
std::shared_ptr<Texture> input_texture,
|
||||
const SamplerDescriptor& sampler_descriptor,
|
||||
const Quad& uvs,
|
||||
const ISize& subpass_size) {
|
||||
ContentContext::SubpassCallback subpass_callback =
|
||||
[&](const ContentContext& renderer, RenderPass& pass) {
|
||||
HostBuffer& host_buffer = pass.GetTransientsBuffer();
|
||||
|
||||
Command cmd;
|
||||
DEBUG_COMMAND_INFO(cmd, "Gaussian blur downsample");
|
||||
auto pipeline_options = OptionsFromPass(pass);
|
||||
pipeline_options.primitive_type = PrimitiveType::kTriangleStrip;
|
||||
cmd.pipeline = renderer.GetTexturePipeline(pipeline_options);
|
||||
|
||||
TextureFillVertexShader::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
|
||||
frame_info.texture_sampler_y_coord_scale = 1.0;
|
||||
frame_info.alpha = 1.0;
|
||||
|
||||
BindVertices<TextureFillVertexShader>(cmd, host_buffer,
|
||||
{
|
||||
{Point(0, 0), uvs[0]},
|
||||
{Point(1, 0), uvs[1]},
|
||||
{Point(0, 1), uvs[2]},
|
||||
{Point(1, 1), uvs[3]},
|
||||
});
|
||||
|
||||
TextureFillVertexShader::BindFrameInfo(
|
||||
cmd, host_buffer.EmplaceUniform(frame_info));
|
||||
TextureFillFragmentShader::BindTextureSampler(
|
||||
cmd, input_texture,
|
||||
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
|
||||
sampler_descriptor));
|
||||
|
||||
pass.AddCommand(std::move(cmd));
|
||||
|
||||
return true;
|
||||
};
|
||||
std::shared_ptr<Texture> out_texture = renderer.MakeSubpass(
|
||||
"Gaussian Blur Filter", subpass_size, subpass_callback);
|
||||
return out_texture;
|
||||
}
|
||||
|
||||
std::shared_ptr<Texture> MakeBlurSubpass(
|
||||
const ContentContext& renderer,
|
||||
std::shared_ptr<Texture> input_texture,
|
||||
const SamplerDescriptor& sampler_descriptor,
|
||||
const GaussianBlurFragmentShader::BlurInfo& blur_info) {
|
||||
ISize subpass_size = input_texture->GetSize();
|
||||
ContentContext::SubpassCallback subpass_callback =
|
||||
[&](const ContentContext& renderer, RenderPass& pass) {
|
||||
GaussianBlurVertexShader::FrameInfo frame_info{
|
||||
.mvp = Matrix::MakeOrthographic(ISize(1, 1)),
|
||||
.texture_sampler_y_coord_scale = 1.0};
|
||||
|
||||
HostBuffer& host_buffer = pass.GetTransientsBuffer();
|
||||
|
||||
Command cmd;
|
||||
ContentContextOptions options = OptionsFromPass(pass);
|
||||
options.primitive_type = PrimitiveType::kTriangleStrip;
|
||||
cmd.pipeline = renderer.GetGaussianBlurPipeline(options);
|
||||
BindVertices<GaussianBlurVertexShader>(cmd, host_buffer,
|
||||
{
|
||||
{Point(0, 0), Point(0, 0)},
|
||||
{Point(1, 0), Point(1, 0)},
|
||||
{Point(0, 1), Point(0, 1)},
|
||||
{Point(1, 1), Point(1, 1)},
|
||||
});
|
||||
|
||||
GaussianBlurFragmentShader::BindTextureSampler(
|
||||
cmd, input_texture,
|
||||
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
|
||||
sampler_descriptor));
|
||||
GaussianBlurVertexShader::BindFrameInfo(
|
||||
cmd, host_buffer.EmplaceUniform(frame_info));
|
||||
GaussianBlurFragmentShader::BindBlurInfo(
|
||||
cmd, host_buffer.EmplaceUniform(blur_info));
|
||||
pass.AddCommand(std::move(cmd));
|
||||
|
||||
return true;
|
||||
};
|
||||
std::shared_ptr<Texture> out_texture = renderer.MakeSubpass(
|
||||
"Gaussian Blur Filter", subpass_size, subpass_callback);
|
||||
return out_texture;
|
||||
}
|
||||
|
||||
/// Given a desired |scalar|, will return the scalar that gets close but leaves
|
||||
/// |size| in integer sizes.
|
||||
Vector2 CalculateIntegerScale(Scalar scalar, ISize size) {
|
||||
ISize new_size(size.width / scalar, size.height / scalar);
|
||||
return Vector2(size.width / static_cast<Scalar>(new_size.width),
|
||||
size.height / static_cast<Scalar>(new_size.height));
|
||||
}
|
||||
|
||||
/// Calculate how much to scale down the texture depending on the blur radius.
|
||||
/// This curve was taken from |DirectionalGaussianBlurFilterContents|.
|
||||
Scalar CalculateScale(Scalar radius) {
|
||||
constexpr Scalar decay = 4.0; // Larger is more gradual.
|
||||
constexpr Scalar limit = 0.95; // The maximum percentage of the scaledown.
|
||||
const Scalar curve =
|
||||
std::min(1.0, decay / (std::max(1.0f, radius) + decay - 1.0));
|
||||
return (curve - 1) * limit + 1;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
GaussianBlurFilterContents::GaussianBlurFilterContents(Scalar sigma)
|
||||
: sigma_(sigma) {}
|
||||
|
||||
std::optional<Rect> GaussianBlurFilterContents::GetFilterSourceCoverage(
|
||||
const Matrix& effect_transform,
|
||||
const Rect& output_limit) const {
|
||||
Scalar blur_radius = CalculateBlurRadius(sigma_);
|
||||
Vector3 blur_radii =
|
||||
effect_transform.Basis() * Vector3{blur_radius, blur_radius, 0.0};
|
||||
return output_limit.Expand(Point(blur_radii.x, blur_radii.y));
|
||||
}
|
||||
|
||||
std::optional<Rect> GaussianBlurFilterContents::GetFilterCoverage(
|
||||
const FilterInput::Vector& inputs,
|
||||
const Entity& entity,
|
||||
const Matrix& effect_transform) const {
|
||||
if (inputs.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<Rect> input_coverage = inputs[0]->GetCoverage(entity);
|
||||
if (!input_coverage.has_value()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
Scalar blur_radius = CalculateBlurRadius(sigma_);
|
||||
Vector3 blur_radii =
|
||||
effect_transform.Basis() * Vector3{blur_radius, blur_radius, 0.0};
|
||||
return input_coverage.value().Expand(Point(blur_radii.x, blur_radii.y));
|
||||
}
|
||||
|
||||
std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
|
||||
const FilterInput::Vector& inputs,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity,
|
||||
const Matrix& effect_transform,
|
||||
const Rect& coverage,
|
||||
const std::optional<Rect>& coverage_hint) const {
|
||||
if (inputs.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<Snapshot> input_snapshot =
|
||||
inputs[0]->GetSnapshot("GaussianBlur", renderer, entity,
|
||||
/*coverage_limit=*/coverage_hint);
|
||||
if (!input_snapshot.has_value()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (sigma_ < kEhCloseEnough) {
|
||||
return Entity::FromSnapshot(input_snapshot.value(), entity.GetBlendMode(),
|
||||
entity.GetClipDepth()); // No blur to render.
|
||||
}
|
||||
|
||||
Scalar blur_radius = CalculateBlurRadius(sigma_);
|
||||
Scalar desired_scale = 1.0 / CalculateScale(blur_radius);
|
||||
Vector2 downsample =
|
||||
CalculateIntegerScale(desired_scale, input_snapshot->texture->GetSize());
|
||||
|
||||
// TODO(gaaclarke): This isn't taking into account the blur radius to expand
|
||||
// the rendered size, so blurred objects are clipped. In
|
||||
// order for that to be implemented correctly we'll need to
|
||||
// start adjusting the geometry coordinates in the downsample
|
||||
// step so that there is a border of transparency around it
|
||||
// before the blur steps.
|
||||
ISize subpass_size =
|
||||
ISize(input_snapshot->texture->GetSize().width / downsample.x,
|
||||
input_snapshot->texture->GetSize().height / downsample.y);
|
||||
|
||||
Quad uvs =
|
||||
CalculateUVs(inputs[0], entity, input_snapshot->texture->GetSize());
|
||||
|
||||
std::shared_ptr<Texture> pass1_out_texture = MakeDownsampleSubpass(
|
||||
renderer, input_snapshot->texture, input_snapshot->sampler_descriptor,
|
||||
uvs, subpass_size);
|
||||
|
||||
Size pass1_pixel_size(1.0 / pass1_out_texture->GetSize().width,
|
||||
1.0 / pass1_out_texture->GetSize().height);
|
||||
|
||||
std::shared_ptr<Texture> pass2_out_texture = MakeBlurSubpass(
|
||||
renderer, pass1_out_texture, input_snapshot->sampler_descriptor,
|
||||
GaussianBlurFragmentShader::BlurInfo{
|
||||
.blur_uv_offset = Point(0.0, pass1_pixel_size.height),
|
||||
.blur_sigma = sigma_ / downsample.y,
|
||||
.blur_radius = blur_radius / downsample.y,
|
||||
.step_size = 1.0,
|
||||
});
|
||||
|
||||
// TODO(gaaclarke): Make this pass reuse the texture from pass1.
|
||||
std::shared_ptr<Texture> pass3_out_texture = MakeBlurSubpass(
|
||||
renderer, pass2_out_texture, input_snapshot->sampler_descriptor,
|
||||
GaussianBlurFragmentShader::BlurInfo{
|
||||
.blur_uv_offset = Point(pass1_pixel_size.width, 0.0),
|
||||
.blur_sigma = sigma_ / downsample.x,
|
||||
.blur_radius = blur_radius / downsample.x,
|
||||
.step_size = 1.0,
|
||||
});
|
||||
|
||||
SamplerDescriptor sampler_desc = MakeSamplerDescriptor(
|
||||
MinMagFilter::kLinear, SamplerAddressMode::kClampToEdge);
|
||||
|
||||
return Entity::FromSnapshot(
|
||||
Snapshot{
|
||||
.texture = pass3_out_texture,
|
||||
.transform =
|
||||
entity.GetTransformation() *
|
||||
Matrix::MakeScale(
|
||||
{input_snapshot->texture->GetSize().width /
|
||||
static_cast<Scalar>(pass1_out_texture->GetSize().width),
|
||||
input_snapshot->texture->GetSize().height /
|
||||
static_cast<Scalar>(pass1_out_texture->GetSize().height),
|
||||
1.0}),
|
||||
.sampler_descriptor = sampler_desc,
|
||||
.opacity = input_snapshot->opacity},
|
||||
entity.GetBlendMode(), entity.GetClipDepth());
|
||||
}
|
||||
|
||||
Scalar GaussianBlurFilterContents::CalculateBlurRadius(Scalar sigma) {
|
||||
return static_cast<Radius>(Sigma(sigma)).radius;
|
||||
}
|
||||
|
||||
Quad GaussianBlurFilterContents::CalculateUVs(
|
||||
const std::shared_ptr<FilterInput>& filter_input,
|
||||
const Entity& entity,
|
||||
const ISize& texture_size) {
|
||||
Matrix input_transform = filter_input->GetLocalTransform(entity);
|
||||
Rect snapshot_rect =
|
||||
Rect::MakeXYWH(0, 0, texture_size.width, texture_size.height);
|
||||
Quad coverage_quad = snapshot_rect.GetTransformedPoints(input_transform);
|
||||
|
||||
Matrix uv_transform = Matrix::MakeScale(
|
||||
{1.0f / texture_size.width, 1.0f / texture_size.height, 1.0f});
|
||||
return uv_transform.Transform(coverage_quad);
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
@@ -0,0 +1,58 @@
|
||||
// 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include "impeller/entity/contents/filters/filter_contents.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
/// Performs a bidirectional Gaussian blur.
|
||||
///
|
||||
/// This is accomplished by rendering multiple passes in multiple directions.
|
||||
/// Note: This will replace `DirectionalGaussianBlurFilterContents`.
|
||||
class GaussianBlurFilterContents final : public FilterContents {
|
||||
public:
|
||||
explicit GaussianBlurFilterContents(Scalar sigma = 0.0f);
|
||||
|
||||
Scalar GetSigma() const { return sigma_; }
|
||||
|
||||
// |FilterContents|
|
||||
std::optional<Rect> GetFilterSourceCoverage(
|
||||
const Matrix& effect_transform,
|
||||
const Rect& output_limit) const override;
|
||||
|
||||
// |FilterContents|
|
||||
std::optional<Rect> GetFilterCoverage(
|
||||
const FilterInput::Vector& inputs,
|
||||
const Entity& entity,
|
||||
const Matrix& effect_transform) const override;
|
||||
|
||||
/// Given a sigma (standard deviation) calculate the blur radius (1/2 the
|
||||
/// kernel size).
|
||||
static Scalar CalculateBlurRadius(Scalar sigma);
|
||||
|
||||
/// Calculate the UV coordinates for rendering the filter_input.
|
||||
/// @param filter_input The FilterInput that should be rendered.
|
||||
/// @param entity The associated entity for the filter_input.
|
||||
/// @param texture_size The size of the texture_size the uvs will be used for.
|
||||
static Quad CalculateUVs(const std::shared_ptr<FilterInput>& filter_input,
|
||||
const Entity& entity,
|
||||
const ISize& pass_size);
|
||||
|
||||
private:
|
||||
// |FilterContents|
|
||||
std::optional<Entity> RenderFilter(
|
||||
const FilterInput::Vector& input_textures,
|
||||
const ContentContext& renderer,
|
||||
const Entity& entity,
|
||||
const Matrix& effect_transform,
|
||||
const Rect& coverage,
|
||||
const std::optional<Rect>& coverage_hint) const override;
|
||||
|
||||
const Scalar sigma_ = 0.0;
|
||||
};
|
||||
|
||||
} // namespace impeller
|
||||
@@ -0,0 +1,242 @@
|
||||
// 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 "flutter/testing/testing.h"
|
||||
#include "fml/status_or.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h"
|
||||
#include "impeller/entity/entity_playground.h"
|
||||
#include "impeller/geometry/geometry_asserts.h"
|
||||
#include "impeller/renderer/testing/mocks.h"
|
||||
|
||||
namespace impeller {
|
||||
namespace testing {
|
||||
|
||||
namespace {
|
||||
|
||||
Scalar CalculateSigmaForBlurRadius(Scalar blur_radius) {
|
||||
// See Sigma.h
|
||||
return (blur_radius / kKernelRadiusPerSigma) + 0.5;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
class GaussianBlurFilterContentsTest : public EntityPlayground {
|
||||
public:
|
||||
std::shared_ptr<Texture> MakeTexture(const TextureDescriptor& desc) {
|
||||
return GetContentContext()
|
||||
->GetContext()
|
||||
->GetResourceAllocator()
|
||||
->CreateTexture(desc);
|
||||
}
|
||||
};
|
||||
INSTANTIATE_PLAYGROUND_SUITE(GaussianBlurFilterContentsTest);
|
||||
|
||||
TEST(GaussianBlurFilterContentsTest, Create) {
|
||||
GaussianBlurFilterContents contents;
|
||||
ASSERT_EQ(contents.GetSigma(), 0.0);
|
||||
}
|
||||
|
||||
TEST(GaussianBlurFilterContentsTest, CoverageEmpty) {
|
||||
GaussianBlurFilterContents contents;
|
||||
FilterInput::Vector inputs = {};
|
||||
Entity entity;
|
||||
std::optional<Rect> coverage =
|
||||
contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix());
|
||||
ASSERT_FALSE(coverage.has_value());
|
||||
}
|
||||
|
||||
TEST(GaussianBlurFilterContentsTest, CoverageSimple) {
|
||||
GaussianBlurFilterContents contents;
|
||||
FilterInput::Vector inputs = {
|
||||
FilterInput::Make(Rect::MakeLTRB(10, 10, 110, 110))};
|
||||
Entity entity;
|
||||
std::optional<Rect> coverage =
|
||||
contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix());
|
||||
ASSERT_EQ(coverage, Rect::MakeLTRB(10, 10, 110, 110));
|
||||
}
|
||||
|
||||
TEST(GaussianBlurFilterContentsTest, CoverageWithSigma) {
|
||||
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
|
||||
GaussianBlurFilterContents contents(/*sigma=*/sigma_radius_1);
|
||||
FilterInput::Vector inputs = {
|
||||
FilterInput::Make(Rect::MakeLTRB(100, 100, 200, 200))};
|
||||
Entity entity;
|
||||
std::optional<Rect> coverage =
|
||||
contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix());
|
||||
ASSERT_EQ(coverage, Rect::MakeLTRB(99, 99, 201, 201));
|
||||
}
|
||||
|
||||
TEST_P(GaussianBlurFilterContentsTest, CoverageWithTexture) {
|
||||
TextureDescriptor desc = {
|
||||
.format = PixelFormat::kB8G8R8A8UNormInt,
|
||||
.size = ISize(100, 100),
|
||||
};
|
||||
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
|
||||
GaussianBlurFilterContents contents(/*sigma=*/sigma_radius_1);
|
||||
std::shared_ptr<Texture> texture =
|
||||
GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture(
|
||||
desc);
|
||||
FilterInput::Vector inputs = {FilterInput::Make(texture)};
|
||||
Entity entity;
|
||||
entity.SetTransformation(Matrix::MakeTranslation({100, 100, 0}));
|
||||
std::optional<Rect> coverage =
|
||||
contents.GetFilterCoverage(inputs, entity, /*effect_transform=*/Matrix());
|
||||
ASSERT_EQ(coverage, Rect::MakeLTRB(99, 99, 201, 201));
|
||||
}
|
||||
|
||||
TEST_P(GaussianBlurFilterContentsTest, CoverageWithEffectTransform) {
|
||||
TextureDescriptor desc = {
|
||||
.format = PixelFormat::kB8G8R8A8UNormInt,
|
||||
.size = ISize(100, 100),
|
||||
};
|
||||
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
|
||||
GaussianBlurFilterContents contents(/*sigma=*/sigma_radius_1);
|
||||
std::shared_ptr<Texture> texture =
|
||||
GetContentContext()->GetContext()->GetResourceAllocator()->CreateTexture(
|
||||
desc);
|
||||
FilterInput::Vector inputs = {FilterInput::Make(texture)};
|
||||
Entity entity;
|
||||
entity.SetTransformation(Matrix::MakeTranslation({100, 100, 0}));
|
||||
std::optional<Rect> coverage = contents.GetFilterCoverage(
|
||||
inputs, entity, /*effect_transform=*/Matrix::MakeScale({2.0, 2.0, 1.0}));
|
||||
ASSERT_EQ(coverage, Rect::MakeLTRB(100 - 2, 100 - 2, 200 + 2, 200 + 2));
|
||||
}
|
||||
|
||||
TEST(GaussianBlurFilterContentsTest, FilterSourceCoverage) {
|
||||
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
|
||||
auto contents = std::make_unique<GaussianBlurFilterContents>(sigma_radius_1);
|
||||
std::optional<Rect> coverage = contents->GetFilterSourceCoverage(
|
||||
/*effect_transform=*/Matrix::MakeScale({2.0, 2.0, 1.0}),
|
||||
/*output_limit=*/Rect::MakeLTRB(100, 100, 200, 200));
|
||||
ASSERT_EQ(coverage, Rect::MakeLTRB(100 - 2, 100 - 2, 200 + 2, 200 + 2));
|
||||
}
|
||||
|
||||
TEST_P(GaussianBlurFilterContentsTest, RenderCoverageMatchesGetCoverage) {
|
||||
TextureDescriptor desc = {
|
||||
.format = PixelFormat::kB8G8R8A8UNormInt,
|
||||
.size = ISize(100, 100),
|
||||
};
|
||||
std::shared_ptr<Texture> texture = MakeTexture(desc);
|
||||
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
|
||||
auto contents = std::make_unique<GaussianBlurFilterContents>(sigma_radius_1);
|
||||
contents->SetInputs({FilterInput::Make(texture)});
|
||||
std::shared_ptr<ContentContext> renderer = GetContentContext();
|
||||
|
||||
Entity entity;
|
||||
std::optional<Entity> result =
|
||||
contents->GetEntity(*renderer, entity, /*coverage_hint=*/{});
|
||||
EXPECT_TRUE(result.has_value());
|
||||
if (result.has_value()) {
|
||||
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
|
||||
std::optional<Rect> result_coverage = result.value().GetCoverage();
|
||||
std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
|
||||
EXPECT_TRUE(result_coverage.has_value());
|
||||
EXPECT_TRUE(contents_coverage.has_value());
|
||||
if (result_coverage.has_value() && contents_coverage.has_value()) {
|
||||
// TODO(gaaclarke): This test won't pass until the blur_radius is used to
|
||||
// expand the coverage. See note inside of
|
||||
// gaussian_blur_filter_contents.cc.
|
||||
// EXPECT_TRUE(RectNear(result_coverage.value(),
|
||||
// contents_coverage.value()));
|
||||
EXPECT_TRUE(
|
||||
RectNear(result_coverage.value(), Rect::MakeLTRB(0, 0, 100, 100)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(GaussianBlurFilterContentsTest,
|
||||
RenderCoverageMatchesGetCoverageTranslate) {
|
||||
TextureDescriptor desc = {
|
||||
.format = PixelFormat::kB8G8R8A8UNormInt,
|
||||
.size = ISize(100, 100),
|
||||
};
|
||||
std::shared_ptr<Texture> texture = MakeTexture(desc);
|
||||
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
|
||||
auto contents = std::make_unique<GaussianBlurFilterContents>(sigma_radius_1);
|
||||
contents->SetInputs({FilterInput::Make(texture)});
|
||||
std::shared_ptr<ContentContext> renderer = GetContentContext();
|
||||
|
||||
Entity entity;
|
||||
entity.SetTransformation(Matrix::MakeTranslation({100, 200, 0}));
|
||||
std::optional<Entity> result =
|
||||
contents->GetEntity(*renderer, entity, /*coverage_hint=*/{});
|
||||
EXPECT_TRUE(result.has_value());
|
||||
if (result.has_value()) {
|
||||
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
|
||||
std::optional<Rect> result_coverage = result.value().GetCoverage();
|
||||
std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
|
||||
EXPECT_TRUE(result_coverage.has_value());
|
||||
EXPECT_TRUE(contents_coverage.has_value());
|
||||
if (result_coverage.has_value() && contents_coverage.has_value()) {
|
||||
// TODO(gaaclarke): This test won't pass until the blur_radius is used to
|
||||
// expand the coverage. See note inside of
|
||||
// gaussian_blur_filter_contents.cc.
|
||||
// EXPECT_TRUE(RectNear(result_coverage.value(),
|
||||
// contents_coverage.value()));
|
||||
EXPECT_TRUE(RectNear(contents_coverage.value(),
|
||||
Rect::MakeLTRB(99, 199, 201, 301)));
|
||||
EXPECT_TRUE(RectNear(result_coverage.value(),
|
||||
Rect::MakeLTRB(100, 200, 200, 300)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(GaussianBlurFilterContentsTest,
|
||||
RenderCoverageMatchesGetCoverageRotated) {
|
||||
TextureDescriptor desc = {
|
||||
.format = PixelFormat::kB8G8R8A8UNormInt,
|
||||
.size = ISize(400, 300),
|
||||
};
|
||||
std::shared_ptr<Texture> texture = MakeTexture(desc);
|
||||
Scalar sigma_radius_1 = CalculateSigmaForBlurRadius(1.0);
|
||||
auto contents = std::make_unique<GaussianBlurFilterContents>(sigma_radius_1);
|
||||
contents->SetInputs({FilterInput::Make(texture)});
|
||||
std::shared_ptr<ContentContext> renderer = GetContentContext();
|
||||
|
||||
Entity entity;
|
||||
entity.SetTransformation(Matrix::MakeTranslation({400, 100, 0}) *
|
||||
Matrix::MakeRotationZ(Degrees(90.0)));
|
||||
std::optional<Entity> result =
|
||||
contents->GetEntity(*renderer, entity, /*coverage_hint=*/{});
|
||||
EXPECT_TRUE(result.has_value());
|
||||
if (result.has_value()) {
|
||||
EXPECT_EQ(result.value().GetBlendMode(), BlendMode::kSourceOver);
|
||||
std::optional<Rect> result_coverage = result.value().GetCoverage();
|
||||
std::optional<Rect> contents_coverage = contents->GetCoverage(entity);
|
||||
EXPECT_TRUE(result_coverage.has_value());
|
||||
EXPECT_TRUE(contents_coverage.has_value());
|
||||
if (result_coverage.has_value() && contents_coverage.has_value()) {
|
||||
// TODO(gaaclarke): This test won't pass until the blur_radius is used to
|
||||
// expand the coverage. See note inside of
|
||||
// gaussian_blur_filter_contents.cc.
|
||||
// EXPECT_TRUE(RectNear(result_coverage.value(),
|
||||
// contents_coverage.value()));
|
||||
EXPECT_TRUE(RectNear(contents_coverage.value(),
|
||||
Rect::MakeLTRB(99, 99, 401, 501)));
|
||||
EXPECT_TRUE(RectNear(result_coverage.value(),
|
||||
Rect::MakeLTRB(100, 100, 400, 500)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_P(GaussianBlurFilterContentsTest, CalculateUVsSimple) {
|
||||
TextureDescriptor desc = {
|
||||
.format = PixelFormat::kB8G8R8A8UNormInt,
|
||||
.size = ISize(100, 100),
|
||||
};
|
||||
std::shared_ptr<Texture> texture = MakeTexture(desc);
|
||||
auto filter_input = FilterInput::Make(texture);
|
||||
Entity entity;
|
||||
Quad uvs = GaussianBlurFilterContents::CalculateUVs(filter_input, entity,
|
||||
ISize(100, 100));
|
||||
std::optional<Rect> uvs_bounds = Rect::MakePointBounds(uvs);
|
||||
EXPECT_TRUE(uvs_bounds.has_value());
|
||||
if (uvs_bounds.has_value()) {
|
||||
EXPECT_TRUE(RectNear(uvs_bounds.value(), Rect::MakeXYWH(0, 0, 1, 1)));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
@@ -1120,14 +1120,18 @@ TEST_P(EntityTest, GaussianBlurFilter) {
|
||||
|
||||
// Renders a green bounding rect of the target filter.
|
||||
Entity bounds_entity;
|
||||
bounds_entity.SetContents(SolidColorContents::Make(
|
||||
PathBuilder{}
|
||||
.AddRect(target_contents->GetCoverage(entity).value())
|
||||
.TakePath(),
|
||||
bounds_color));
|
||||
bounds_entity.SetTransformation(Matrix());
|
||||
std::optional<Rect> target_contents_coverage =
|
||||
target_contents->GetCoverage(entity);
|
||||
if (target_contents_coverage.has_value()) {
|
||||
bounds_entity.SetContents(SolidColorContents::Make(
|
||||
PathBuilder{}
|
||||
.AddRect(target_contents->GetCoverage(entity).value())
|
||||
.TakePath(),
|
||||
bounds_color));
|
||||
bounds_entity.SetTransformation(Matrix());
|
||||
|
||||
bounds_entity.Render(context, pass);
|
||||
bounds_entity.Render(context, pass);
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
@@ -29,6 +29,7 @@ uniform BlurInfo {
|
||||
// radius is used to limit how much of the function is integrated.
|
||||
float blur_sigma;
|
||||
float16_t blur_radius;
|
||||
float16_t step_size;
|
||||
}
|
||||
blur_info;
|
||||
|
||||
@@ -54,7 +55,7 @@ void main() {
|
||||
// just uses 0.5 as an optimization until the gaussian coefficients are
|
||||
// calculated and passed in from the cpu.
|
||||
for (float16_t i = -blur_info.blur_radius; i <= blur_info.blur_radius;
|
||||
i += 2.0hf) {
|
||||
i += blur_info.step_size) {
|
||||
// Use the 32 bit Gaussian function because the 16 bit variation results in
|
||||
// quality loss/visible banding. Also, 16 bit variation internally breaks
|
||||
// down at a moderately high (but still reasonable) blur sigma of >255 when
|
||||
|
||||
@@ -59,7 +59,8 @@ inline ::testing::AssertionResult RectNear(impeller::Rect a, impeller::Rect b) {
|
||||
NumberNear(a.GetSize().height, b.GetSize().height);
|
||||
|
||||
return equal ? ::testing::AssertionSuccess()
|
||||
: ::testing::AssertionFailure() << "Rects are not equal.";
|
||||
: ::testing::AssertionFailure()
|
||||
<< "Rects are not equal (" << a << " " << b << ")";
|
||||
}
|
||||
|
||||
inline ::testing::AssertionResult ColorNear(impeller::Color a,
|
||||
@@ -76,7 +77,8 @@ inline ::testing::AssertionResult PointNear(impeller::Point a,
|
||||
auto equal = NumberNear(a.x, b.x) && NumberNear(a.y, b.y);
|
||||
|
||||
return equal ? ::testing::AssertionSuccess()
|
||||
: ::testing::AssertionFailure() << "Points are not equal.";
|
||||
: ::testing::AssertionFailure()
|
||||
<< "Points are not equal (" << a << " " << b << ").";
|
||||
}
|
||||
|
||||
inline ::testing::AssertionResult Vector3Near(impeller::Vector3 a,
|
||||
|
||||
@@ -445,6 +445,15 @@ struct Matrix {
|
||||
return Vector2(v.x * m[0] + v.y * m[4], v.x * m[1] + v.y * m[5]);
|
||||
}
|
||||
|
||||
constexpr Quad Transform(const Quad& quad) const {
|
||||
return {
|
||||
*this * quad[0],
|
||||
*this * quad[1],
|
||||
*this * quad[2],
|
||||
*this * quad[3],
|
||||
};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
static constexpr Matrix MakeOrthographic(TSize<T> size) {
|
||||
// Per assumptions about NDC documented above.
|
||||
|
||||
@@ -309,6 +309,7 @@ using IPoint = TPoint<int64_t>;
|
||||
using IPoint32 = TPoint<int32_t>;
|
||||
using UintPoint32 = TPoint<uint32_t>;
|
||||
using Vector2 = Point;
|
||||
using Quad = std::array<Point, 4>;
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
|
||||
@@ -48,6 +48,11 @@ struct TRect {
|
||||
return TRect(0.0, 0.0, size.width, size.height);
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
constexpr static std::optional<TRect> MakePointBounds(const U& value) {
|
||||
return MakePointBounds(value.begin(), value.end());
|
||||
}
|
||||
|
||||
template <typename PointIter>
|
||||
constexpr static std::optional<TRect> MakePointBounds(const PointIter first,
|
||||
const PointIter last) {
|
||||
|
||||
@@ -234,5 +234,19 @@ TEST(SizeTest, IRectIsEmpty) {
|
||||
EXPECT_TRUE(IRect::MakeXYWH(1, 2, -1, 7).IsEmpty());
|
||||
}
|
||||
|
||||
TEST(RectTest, MakePointBoundsQuad) {
|
||||
Quad quad = {
|
||||
Point(10, 10),
|
||||
Point(20, 10),
|
||||
Point(10, 20),
|
||||
Point(20, 20),
|
||||
};
|
||||
std::optional<Rect> bounds = Rect::MakePointBounds(quad);
|
||||
EXPECT_TRUE(bounds.has_value());
|
||||
if (bounds.has_value()) {
|
||||
EXPECT_TRUE(RectNear(bounds.value(), Rect::MakeLTRB(10, 10, 20, 20)));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
|
||||
@@ -3338,7 +3338,7 @@
|
||||
"arithmetic"
|
||||
],
|
||||
"shortest_path_cycles": [
|
||||
1.649999976158142,
|
||||
1.3200000524520874,
|
||||
1.0,
|
||||
0.0
|
||||
],
|
||||
@@ -3346,14 +3346,14 @@
|
||||
"arithmetic"
|
||||
],
|
||||
"total_cycles": [
|
||||
5.0,
|
||||
4.666666507720947,
|
||||
1.0,
|
||||
1.0
|
||||
]
|
||||
},
|
||||
"thread_occupancy": 100,
|
||||
"uniform_registers_used": 1,
|
||||
"work_registers_used": 4
|
||||
"work_registers_used": 3
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3456,7 +3456,7 @@
|
||||
"arithmetic"
|
||||
],
|
||||
"shortest_path_cycles": [
|
||||
1.649999976158142,
|
||||
1.3200000524520874,
|
||||
1.0,
|
||||
0.0
|
||||
],
|
||||
@@ -3464,7 +3464,7 @@
|
||||
"arithmetic"
|
||||
],
|
||||
"total_cycles": [
|
||||
3.6666667461395264,
|
||||
3.3333332538604736,
|
||||
1.0,
|
||||
1.0
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user