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: 6 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -5231,6 +5231,9 @@ ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.gls
ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.vert + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noalpha_decal.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noalpha_nodecal.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/kernel.glsl + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/kernel_decal.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/gaussian_blur/kernel_nodecal.frag + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/geometry/points.comp + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/geometry/uv.comp + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/entity/shaders/glyph_atlas.frag + ../../../flutter/LICENSE
Expand Down Expand Up @@ -8041,6 +8044,9 @@ FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.glsl
FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur.vert
FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noalpha_decal.frag
FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/gaussian_blur_noalpha_nodecal.frag
FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/kernel.glsl
FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/kernel_decal.frag
FILE: ../../../flutter/impeller/entity/shaders/gaussian_blur/kernel_nodecal.frag
FILE: ../../../flutter/impeller/entity/shaders/geometry/points.comp
FILE: ../../../flutter/impeller/entity/shaders/geometry/uv.comp
FILE: ../../../flutter/impeller/entity/shaders/glyph_atlas.frag
Expand Down
3 changes: 3 additions & 0 deletions impeller/entity/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ impeller_shaders("entity_shaders") {
"shaders/gaussian_blur/gaussian_blur.vert",
"shaders/gaussian_blur/gaussian_blur_noalpha_decal.frag",
"shaders/gaussian_blur/gaussian_blur_noalpha_nodecal.frag",
"shaders/gaussian_blur/kernel_decal.frag",
"shaders/gaussian_blur/kernel_nodecal.frag",
"shaders/glyph_atlas.frag",
"shaders/glyph_atlas_color.frag",
"shaders/glyph_atlas.vert",
Expand Down Expand Up @@ -245,6 +247,7 @@ impeller_component("entity") {
}

deps = [ "//flutter/fml" ]
defines = [ "_USE_MATH_DEFINES" ]
}

impeller_component("entity_test_helpers") {
Expand Down
2 changes: 2 additions & 0 deletions impeller/entity/contents/content_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ ContentContext::ContentContext(
options_trianglestrip);
gaussian_blur_noalpha_nodecal_pipelines_.CreateDefault(*context_,
options_trianglestrip);
kernel_decal_pipelines_.CreateDefault(*context_, options_trianglestrip);
kernel_nodecal_pipelines_.CreateDefault(*context_, options_trianglestrip);
border_mask_blur_pipelines_.CreateDefault(*context_, options_trianglestrip);
morphology_filter_pipelines_.CreateDefault(*context_, options_trianglestrip,
{supports_decal});
Expand Down
18 changes: 18 additions & 0 deletions impeller/entity/contents/content_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
#include "impeller/entity/gaussian_blur.vert.h"
#include "impeller/entity/gaussian_blur_noalpha_decal.frag.h"
#include "impeller/entity/gaussian_blur_noalpha_nodecal.frag.h"
#include "impeller/entity/kernel_decal.frag.h"
#include "impeller/entity/kernel_nodecal.frag.h"

#include "impeller/entity/position_color.vert.h"

Expand Down Expand Up @@ -137,6 +139,10 @@ using GaussianBlurDecalPipeline =
using GaussianBlurPipeline =
RenderPipelineT<GaussianBlurVertexShader,
GaussianBlurNoalphaNodecalFragmentShader>;
using KernelDecalPipeline =
RenderPipelineT<GaussianBlurVertexShader, KernelDecalFragmentShader>;
using KernelPipeline =
RenderPipelineT<GaussianBlurVertexShader, KernelNodecalFragmentShader>;
using BorderMaskBlurPipeline =
RenderPipelineT<BorderMaskBlurVertexShader, BorderMaskBlurFragmentShader>;
using MorphologyFilterPipeline =
Expand Down Expand Up @@ -447,6 +453,16 @@ class ContentContext {
return GetPipeline(gaussian_blur_noalpha_nodecal_pipelines_, opts);
}

std::shared_ptr<Pipeline<PipelineDescriptor>> GetKernelDecalPipeline(
ContentContextOptions opts) const {
return GetPipeline(kernel_decal_pipelines_, opts);
}

std::shared_ptr<Pipeline<PipelineDescriptor>> GetKernelPipeline(
ContentContextOptions opts) const {
return GetPipeline(kernel_nodecal_pipelines_, opts);
}

std::shared_ptr<Pipeline<PipelineDescriptor>> GetBorderMaskBlurPipeline(
ContentContextOptions opts) const {
return GetPipeline(border_mask_blur_pipelines_, opts);
Expand Down Expand Up @@ -813,6 +829,8 @@ class ContentContext {
gaussian_blur_noalpha_decal_pipelines_;
mutable Variants<GaussianBlurPipeline>
gaussian_blur_noalpha_nodecal_pipelines_;
mutable Variants<KernelDecalPipeline> kernel_decal_pipelines_;
mutable Variants<KernelPipeline> kernel_nodecal_pipelines_;
mutable Variants<BorderMaskBlurPipeline> border_mask_blur_pipelines_;
mutable Variants<MorphologyFilterPipeline> morphology_filter_pipelines_;
mutable Variants<ColorMatrixColorFilterPipeline>
Expand Down
85 changes: 58 additions & 27 deletions impeller/entity/contents/filters/gaussian_blur_filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "impeller/entity/contents/filters/gaussian_blur_filter_contents.h"

#include <cmath>

#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/texture_fill.frag.h"
#include "impeller/entity/texture_fill.vert.h"
Expand All @@ -14,8 +16,8 @@

namespace impeller {

using GaussianBlurVertexShader = GaussianBlurPipeline::VertexShader;
using GaussianBlurFragmentShader = GaussianBlurPipeline::FragmentShader;
using GaussianBlurVertexShader = KernelPipeline::VertexShader;
using GaussianBlurFragmentShader = KernelPipeline::FragmentShader;

namespace {

Expand Down Expand Up @@ -120,7 +122,7 @@ fml::StatusOr<RenderTarget> MakeBlurSubpass(
const RenderTarget& input_pass,
const SamplerDescriptor& sampler_descriptor,
Entity::TileMode tile_mode,
const GaussianBlurFragmentShader::BlurInfo& blur_info,
const BlurParameters& blur_info,
std::optional<RenderTarget> destination_target,
const Quad& blur_uvs) {
if (blur_info.blur_sigma < kEhCloseEnough) {
Expand All @@ -147,9 +149,9 @@ fml::StatusOr<RenderTarget> MakeBlurSubpass(
if (tile_mode == Entity::TileMode::kDecal &&
!renderer.GetDeviceCapabilities()
.SupportsDecalSamplerAddressMode()) {
cmd.pipeline = renderer.GetGaussianBlurDecalPipeline(options);
cmd.pipeline = renderer.GetKernelDecalPipeline(options);
} else {
cmd.pipeline = renderer.GetGaussianBlurPipeline(options);
cmd.pipeline = renderer.GetKernelPipeline(options);
}

BindVertices<GaussianBlurVertexShader>(cmd, host_buffer,
Expand All @@ -169,8 +171,8 @@ fml::StatusOr<RenderTarget> MakeBlurSubpass(
linear_sampler_descriptor));
GaussianBlurVertexShader::BindFrameInfo(
cmd, host_buffer.EmplaceUniform(frame_info));
GaussianBlurFragmentShader::BindBlurInfo(
cmd, host_buffer.EmplaceUniform(blur_info));
GaussianBlurFragmentShader::BindKernelSamples(
cmd, host_buffer.EmplaceUniform(GenerateBlurInfo(blur_info)));
pass.AddCommand(std::move(cmd));

return true;
Expand Down Expand Up @@ -343,16 +345,17 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
}
}

fml::StatusOr<RenderTarget> pass2_out =
MakeBlurSubpass(renderer, /*input_pass=*/pass1_out.value(),
input_snapshot->sampler_descriptor, tile_mode_,
GaussianBlurFragmentShader::BlurInfo{
.blur_uv_offset = Point(0.0, pass1_pixel_size.y),
.blur_sigma = scaled_sigma.y * effective_scalar.y,
.blur_radius = blur_radius.y * effective_scalar.y,
.step_size = 1.0,
},
/*destination_target=*/std::nullopt, blur_uvs);
fml::StatusOr<RenderTarget> pass2_out = MakeBlurSubpass(
renderer, /*input_pass=*/pass1_out.value(),
input_snapshot->sampler_descriptor, tile_mode_,
BlurParameters{
.blur_uv_offset = Point(0.0, pass1_pixel_size.y),
.blur_sigma = scaled_sigma.y * effective_scalar.y,
.blur_radius =
static_cast<int>(std::round(blur_radius.y * effective_scalar.y)),
.step_size = 1,
},
/*destination_target=*/std::nullopt, blur_uvs);

if (!pass2_out.ok()) {
return std::nullopt;
Expand All @@ -364,16 +367,17 @@ std::optional<Entity> GaussianBlurFilterContents::RenderFilter(
? std::optional<RenderTarget>(pass1_out.value())
: std::optional<RenderTarget>(std::nullopt);

fml::StatusOr<RenderTarget> pass3_out =
MakeBlurSubpass(renderer, /*input_pass=*/pass2_out.value(),
input_snapshot->sampler_descriptor, tile_mode_,
GaussianBlurFragmentShader::BlurInfo{
.blur_uv_offset = Point(pass1_pixel_size.x, 0.0),
.blur_sigma = scaled_sigma.x * effective_scalar.x,
.blur_radius = blur_radius.x * effective_scalar.x,
.step_size = 1.0,
},
pass3_destination, blur_uvs);
fml::StatusOr<RenderTarget> pass3_out = MakeBlurSubpass(
renderer, /*input_pass=*/pass2_out.value(),
input_snapshot->sampler_descriptor, tile_mode_,
BlurParameters{
.blur_uv_offset = Point(pass1_pixel_size.x, 0.0),
.blur_sigma = scaled_sigma.x * effective_scalar.x,
.blur_radius =
static_cast<int>(std::round(blur_radius.x * effective_scalar.x)),
.step_size = 1,
},
pass3_destination, blur_uvs);

if (!pass3_out.ok()) {
return std::nullopt;
Expand Down Expand Up @@ -429,4 +433,31 @@ Scalar GaussianBlurFilterContents::ScaleSigma(Scalar sigma) {
return clamped * scalar;
}

KernelPipeline::FragmentShader::KernelSamples GenerateBlurInfo(
BlurParameters parameters) {
KernelPipeline::FragmentShader::KernelSamples result;
result.sample_count =
((2 * parameters.blur_radius) / parameters.step_size) + 1;
FML_CHECK(result.sample_count < 24);

Scalar tally = 0.0f;
for (int i = 0; i < result.sample_count; ++i) {
int x = (i * parameters.step_size) - parameters.blur_radius;
result.samples[i] = KernelPipeline::FragmentShader::KernelSample{
.uv_offset = parameters.blur_uv_offset * x,
.coefficient = expf(-0.5f * (x * x) /
(parameters.blur_sigma * parameters.blur_sigma)) /
(sqrtf(2.0f * M_PI) * parameters.blur_sigma),
};
tally += result.samples[i].coefficient;
}

// Make sure everything adds up to 1.
for (auto& sample : result.samples) {
sample.coefficient /= tally;
}

return result;
}

} // namespace impeller
11 changes: 11 additions & 0 deletions impeller/entity/contents/filters/gaussian_blur_filter_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
#define FLUTTER_IMPELLER_ENTITY_CONTENTS_FILTERS_GAUSSIAN_BLUR_FILTER_CONTENTS_H_

#include <optional>
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/filters/filter_contents.h"

namespace impeller {

struct BlurParameters {
Point blur_uv_offset;
Scalar blur_sigma;
int blur_radius;
int step_size;
};

KernelPipeline::FragmentShader::KernelSamples GenerateBlurInfo(
BlurParameters parameters);

/// Performs a bidirectional Gaussian blur.
///
/// This is accomplished by rendering multiple passes in multiple directions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,30 @@ TEST(GaussianBlurFilterContentsTest, CalculateSigmaForBlurRadius) {
EXPECT_NEAR(sigma, derived_sigma, 0.01f);
}

TEST(GaussianBlurFilterContentsTest, Coefficients) {
BlurParameters parameters = {.blur_uv_offset = Point(1, 0),
.blur_sigma = 1,
.blur_radius = 5,
.step_size = 1};
KernelPipeline::FragmentShader::KernelSamples samples =
GenerateBlurInfo(parameters);
EXPECT_EQ(samples.sample_count, 11);

// Coefficients should add up to 1.
Scalar tally = 0;
for (int i = 0; i < samples.sample_count; ++i) {
tally += samples.samples[i].coefficient;
}
EXPECT_FLOAT_EQ(tally, 1.0f);

// Verify the shape of the curve.
for (int i = 0; i < 5; ++i) {
EXPECT_FLOAT_EQ(samples.samples[i].coefficient,
samples.samples[10 - i].coefficient);
EXPECT_TRUE(samples.samples[i + 1].coefficient >
samples.samples[i].coefficient);
}
}

} // namespace testing
} // namespace impeller
46 changes: 46 additions & 0 deletions impeller/entity/shaders/gaussian_blur/kernel.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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/constants.glsl>
#include <impeller/gaussian.glsl>
#include <impeller/texture.glsl>
#include <impeller/types.glsl>

uniform f16sampler2D texture_sampler;

struct KernelSample {
vec2 uv_offset;
float coefficient;
};

uniform KernelSamples {
int sample_count;
KernelSample samples[24];
}
blur_info;

f16vec4 Sample(f16sampler2D tex, vec2 coords) {
#if ENABLE_DECAL_SPECIALIZATION
return IPHalfSampleDecal(tex, coords);
#else
return texture(tex, coords);
#endif
}

in vec2 v_texture_coords;

out f16vec4 frag_color;

void main() {
f16vec4 total_color = f16vec4(0.0hf);

for (int i = 0; i < blur_info.sample_count; ++i) {
float16_t coefficient = float16_t(blur_info.samples[i].coefficient);
total_color +=
coefficient * Sample(texture_sampler,
v_texture_coords + blur_info.samples[i].uv_offset);
}

frag_color = total_color;
}
9 changes: 9 additions & 0 deletions impeller/entity/shaders/gaussian_blur/kernel_decal.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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.

precision mediump float;

#define ENABLE_DECAL_SPECIALIZATION 1

#include "kernel.glsl"
9 changes: 9 additions & 0 deletions impeller/entity/shaders/gaussian_blur/kernel_nodecal.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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.

precision mediump float;

#define ENABLE_DECAL_SPECIALIZATION 0

#include "kernel.glsl"
Loading