Give filters full responsibility over commands (flutter/engine#35389)

This commit is contained in:
Brandon DeRosier
2022-08-15 10:22:25 -07:00
committed by GitHub
parent d9db0d0b47
commit 8bbe783558
10 changed files with 252 additions and 195 deletions

View File

@@ -268,32 +268,43 @@ void BlendFilterContents::SetForegroundColor(std::optional<Color> color) {
foreground_color_ = color;
}
bool BlendFilterContents::RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const {
std::optional<Snapshot> BlendFilterContents::RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Rect& coverage) const {
if (inputs.empty()) {
return true;
return std::nullopt;
}
if (inputs.size() == 1 && !foreground_color_.has_value()) {
// Nothing to blend.
return PipelineBlend(inputs, renderer, entity, pass, coverage,
Entity::BlendMode::kSource, std::nullopt);
}
ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
if (inputs.size() == 1 && !foreground_color_.has_value()) {
// Nothing to blend.
return PipelineBlend(inputs, renderer, entity, pass, coverage,
Entity::BlendMode::kSource, std::nullopt);
}
if (blend_mode_ <= Entity::BlendMode::kLastPipelineBlendMode) {
return PipelineBlend(inputs, renderer, entity, pass, coverage, blend_mode_,
foreground_color_);
}
if (blend_mode_ <= Entity::BlendMode::kLastPipelineBlendMode) {
return PipelineBlend(inputs, renderer, entity, pass, coverage,
blend_mode_, foreground_color_);
}
if (blend_mode_ <= Entity::BlendMode::kLastAdvancedBlendMode) {
return advanced_blend_proc_(inputs, renderer, entity, pass, coverage,
foreground_color_);
}
if (blend_mode_ <= Entity::BlendMode::kLastAdvancedBlendMode) {
return advanced_blend_proc_(inputs, renderer, entity, pass, coverage,
foreground_color_);
}
FML_UNREACHABLE();
};
FML_UNREACHABLE();
auto out_texture = renderer.MakeSubpass(ISize(coverage.size), callback);
if (!out_texture) {
return std::nullopt;
}
out_texture->SetLabel("BlendFilter Texture");
return Snapshot{.texture = out_texture,
.transform = Matrix::MakeTranslation(coverage.origin)};
}
} // namespace impeller

View File

@@ -31,11 +31,10 @@ class BlendFilterContents : public FilterContents {
private:
// |FilterContents|
bool RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const override;
std::optional<Snapshot> RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Rect& coverage) const override;
Entity::BlendMode blend_mode_ = Entity::BlendMode::kSourceOver;
AdvancedBlendProc advanced_blend_proc_;

View File

@@ -47,63 +47,82 @@ void BorderMaskBlurFilterContents::SetBlurStyle(BlurStyle blur_style) {
}
}
bool BorderMaskBlurFilterContents::RenderFilter(
std::optional<Snapshot> BorderMaskBlurFilterContents::RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const {
if (inputs.empty()) {
return true;
}
using VS = BorderMaskBlurPipeline::VertexShader;
using FS = BorderMaskBlurPipeline::FragmentShader;
auto& host_buffer = pass.GetTransientsBuffer();
//----------------------------------------------------------------------------
/// Handle inputs.
///
if (inputs.empty()) {
return std::nullopt;
}
auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity);
if (!input_snapshot.has_value()) {
return true;
return std::nullopt;
}
auto maybe_input_uvs = input_snapshot->GetCoverageUVs(coverage);
if (!maybe_input_uvs.has_value()) {
return true;
return std::nullopt;
}
auto input_uvs = maybe_input_uvs.value();
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), input_uvs[0]},
{Point(1, 0), input_uvs[1]},
{Point(1, 1), input_uvs[3]},
{Point(0, 0), input_uvs[0]},
{Point(1, 1), input_uvs[3]},
{Point(0, 1), input_uvs[2]},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
//----------------------------------------------------------------------------
/// Render to texture.
///
Command cmd;
cmd.label = "Border Mask Blur Filter";
auto options = OptionsFromPass(pass);
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetBorderMaskBlurPipeline(options);
cmd.BindVertices(vtx_buffer);
ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
auto& host_buffer = pass.GetTransientsBuffer();
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.sigma_uv = Vector2(sigma_x_.sigma, sigma_y_.sigma).Abs() /
input_snapshot->texture->GetSize();
frame_info.src_factor = src_color_factor_;
frame_info.inner_blur_factor = inner_blur_factor_;
frame_info.outer_blur_factor = outer_blur_factor_;
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
VS::BindFrameInfo(cmd, uniform_view);
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), input_uvs[0]},
{Point(1, 0), input_uvs[1]},
{Point(1, 1), input_uvs[3]},
{Point(0, 0), input_uvs[0]},
{Point(1, 1), input_uvs[3]},
{Point(0, 1), input_uvs[2]},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindTextureSampler(cmd, input_snapshot->texture, sampler);
Command cmd;
cmd.label = "Border Mask Blur Filter";
auto options = OptionsFromPass(pass);
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetBorderMaskBlurPipeline(options);
cmd.BindVertices(vtx_buffer);
return pass.AddCommand(std::move(cmd));
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.sigma_uv = Vector2(sigma_x_.sigma, sigma_y_.sigma).Abs() /
input_snapshot->texture->GetSize();
frame_info.src_factor = src_color_factor_;
frame_info.inner_blur_factor = inner_blur_factor_;
frame_info.outer_blur_factor = outer_blur_factor_;
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
VS::BindFrameInfo(cmd, uniform_view);
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindTextureSampler(cmd, input_snapshot->texture, sampler);
return pass.AddCommand(std::move(cmd));
};
auto out_texture = renderer.MakeSubpass(ISize(coverage.size), callback);
if (!out_texture) {
return std::nullopt;
}
out_texture->SetLabel("BorderMaskBlurFilter Texture");
return Snapshot{.texture = out_texture,
.transform = Matrix::MakeTranslation(coverage.origin)};
}
std::optional<Rect> BorderMaskBlurFilterContents::GetFilterCoverage(

View File

@@ -27,11 +27,11 @@ class BorderMaskBlurFilterContents final : public FilterContents {
private:
// |FilterContents|
bool RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const override;
std::optional<Snapshot> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Rect& coverage) const override;
Sigma sigma_x_;
Sigma sigma_y_;

View File

@@ -23,50 +23,60 @@ void ColorMatrixFilterContents::SetMatrix(const ColorMatrix& matrix) {
matrix_ = matrix;
}
bool ColorMatrixFilterContents::RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const {
if (inputs.empty()) {
return true;
}
std::optional<Snapshot> ColorMatrixFilterContents::RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Rect& coverage) const {
using VS = ColorMatrixColorFilterPipeline::VertexShader;
using FS = ColorMatrixColorFilterPipeline::FragmentShader;
auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity);
if (!input_snapshot.has_value()) {
return true;
//----------------------------------------------------------------------------
/// Handle inputs.
///
if (inputs.empty()) {
return std::nullopt;
}
Command cmd;
cmd.label = "Color Matrix Filter";
auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity);
if (!input_snapshot.has_value()) {
return std::nullopt;
}
auto options = OptionsFromPass(pass);
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetColorMatrixColorFilterPipeline(options);
//----------------------------------------------------------------------------
/// Render to texture.
///
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0)},
{Point(1, 0)},
{Point(1, 1)},
{Point(0, 0)},
{Point(1, 1)},
{Point(0, 1)},
});
auto& host_buffer = pass.GetTransientsBuffer();
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
cmd.BindVertices(vtx_buffer);
ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
Command cmd;
cmd.label = "Color Matrix Filter";
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
auto options = OptionsFromPass(pass);
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetColorMatrixColorFilterPipeline(options);
FS::FragInfo frag_info;
const float* matrix = matrix_.array;
frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
// clang-format off
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0)},
{Point(1, 0)},
{Point(1, 1)},
{Point(0, 0)},
{Point(1, 1)},
{Point(0, 1)},
});
auto& host_buffer = pass.GetTransientsBuffer();
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
cmd.BindVertices(vtx_buffer);
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
FS::FragInfo frag_info;
const float* matrix = matrix_.array;
frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
// clang-format off
frag_info.color_m =
Matrix(
matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
@@ -74,15 +84,25 @@ bool ColorMatrixFilterContents::RenderFilter(const FilterInput::Vector& inputs,
matrix[10], matrix[11], matrix[12], matrix[13],
matrix[15], matrix[16], matrix[17], matrix[18]
);
// clang-format on
// clang-format on
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindInputTexture(cmd, input_snapshot->texture, sampler);
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindInputTexture(cmd, input_snapshot->texture, sampler);
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
return pass.AddCommand(std::move(cmd));
return pass.AddCommand(std::move(cmd));
};
auto out_texture = renderer.MakeSubpass(ISize(coverage.size), callback);
if (!out_texture) {
return std::nullopt;
}
out_texture->SetLabel("ColorMatrixFilter Texture");
return Snapshot{.texture = out_texture,
.transform = Matrix::MakeTranslation(coverage.origin)};
}
} // namespace impeller

View File

@@ -24,11 +24,11 @@ class ColorMatrixFilterContents final : public FilterContents {
private:
// |FilterContents|
bool RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const override;
std::optional<Snapshot> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Rect& coverage) const override;
ColorMatrix matrix_;

View File

@@ -220,20 +220,8 @@ std::optional<Snapshot> FilterContents::RenderToSnapshot(
return std::nullopt;
}
// Render the filter into a new texture.
auto texture = renderer.MakeSubpass(
ISize(coverage->size),
[=](const ContentContext& renderer, RenderPass& pass) -> bool {
return RenderFilter(inputs_, renderer, entity_with_local_transform,
pass, coverage.value());
});
if (!texture) {
return std::nullopt;
}
return Snapshot{.texture = texture,
.transform = Matrix::MakeTranslation(coverage->origin)};
return RenderFilter(inputs_, renderer, entity_with_local_transform,
coverage.value());
}
Matrix FilterContents::GetLocalTransform() const {

View File

@@ -101,13 +101,12 @@ class FilterContents : public Contents {
const FilterInput::Vector& inputs,
const Entity& entity) const;
/// @brief Takes a set of zero or more input textures and writes to an output
/// texture.
virtual bool RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const = 0;
/// @brief Converts zero or more filter inputs into a new texture.
virtual std::optional<Snapshot> RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
const Rect& coverage) const = 0;
std::optional<Rect> GetLocalCoverage(const Entity& local_entity) const;

View File

@@ -11,8 +11,10 @@
#include "impeller/entity/contents/filters/filter_contents.h"
#include "impeller/geometry/rect.h"
#include "impeller/geometry/scalar.h"
#include "impeller/renderer/command_buffer.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/render_target.h"
#include "impeller/renderer/sampler_descriptor.h"
#include "impeller/renderer/sampler_library.h"
@@ -80,30 +82,31 @@ void DirectionalGaussianBlurFilterContents::SetSourceOverride(
source_override_ = source_override;
}
bool DirectionalGaussianBlurFilterContents::RenderFilter(
std::optional<Snapshot> DirectionalGaussianBlurFilterContents::RenderFilter(
const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const {
if (inputs.empty()) {
return true;
}
using VS = GaussianBlurPipeline::VertexShader;
using FS = GaussianBlurPipeline::FragmentShader;
auto& host_buffer = pass.GetTransientsBuffer();
//----------------------------------------------------------------------------
/// Handle inputs.
///
if (inputs.empty()) {
return std::nullopt;
}
// Input 0 snapshot and UV mapping.
auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity);
if (!input_snapshot.has_value()) {
return true;
return std::nullopt;
}
auto maybe_input_uvs = input_snapshot->GetCoverageUVs(coverage);
if (!maybe_input_uvs.has_value()) {
return true;
return std::nullopt;
}
auto input_uvs = maybe_input_uvs.value();
@@ -112,66 +115,84 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter(
auto source = source_override_ ? source_override_ : inputs[0];
auto source_snapshot = source->GetSnapshot(renderer, entity);
if (!source_snapshot.has_value()) {
return true;
return std::nullopt;
}
auto maybe_source_uvs = source_snapshot->GetCoverageUVs(coverage);
if (!maybe_source_uvs.has_value()) {
return true;
return std::nullopt;
}
auto source_uvs = maybe_source_uvs.value();
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), input_uvs[0], source_uvs[0]},
{Point(1, 0), input_uvs[1], source_uvs[1]},
{Point(1, 1), input_uvs[3], source_uvs[3]},
{Point(0, 0), input_uvs[0], source_uvs[0]},
{Point(1, 1), input_uvs[3], source_uvs[3]},
{Point(0, 1), input_uvs[2], source_uvs[2]},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
//----------------------------------------------------------------------------
/// Render to texture.
///
auto transformed_blur = entity.GetTransformation().TransformDirection(
blur_direction_ * blur_sigma_.sigma);
ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
auto& host_buffer = pass.GetTransientsBuffer();
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), input_uvs[0], source_uvs[0]},
{Point(1, 0), input_uvs[1], source_uvs[1]},
{Point(1, 1), input_uvs[3], source_uvs[3]},
{Point(0, 0), input_uvs[0], source_uvs[0]},
{Point(1, 1), input_uvs[3], source_uvs[3]},
{Point(0, 1), input_uvs[2], source_uvs[2]},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
frag_info.alpha_mask_sampler_y_coord_scale =
source_snapshot->texture->GetYCoordScale();
frag_info.blur_sigma = transformed_blur.GetLength();
frag_info.blur_radius = Radius{Sigma{frag_info.blur_sigma}}.radius;
frag_info.blur_direction = input_snapshot->transform.Invert()
.TransformDirection(transformed_blur)
.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_;
frag_info.texture_size = Point(input_snapshot->GetCoverage().value().size);
auto transformed_blur = entity.GetTransformation().TransformDirection(
blur_direction_ * blur_sigma_.sigma);
SamplerDescriptor sampler_desc;
sampler_desc.min_filter = MinMagFilter::kLinear;
sampler_desc.mag_filter = MinMagFilter::kLinear;
auto sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc);
VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
Command cmd;
cmd.label = "Gaussian Blur Filter";
auto options = OptionsFromPass(pass);
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetGaussianBlurPipeline(options);
cmd.BindVertices(vtx_buffer);
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();
frag_info.alpha_mask_sampler_y_coord_scale =
source_snapshot->texture->GetYCoordScale();
frag_info.blur_sigma = transformed_blur.GetLength();
frag_info.blur_radius = Radius{Sigma{frag_info.blur_sigma}}.radius;
frag_info.blur_direction = input_snapshot->transform.Invert()
.TransformDirection(transformed_blur)
.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_;
frag_info.texture_size = Point(input_snapshot->GetCoverage().value().size);
FS::BindTextureSampler(cmd, input_snapshot->texture, sampler);
FS::BindAlphaMaskSampler(cmd, source_snapshot->texture, sampler);
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
SamplerDescriptor sampler_desc;
sampler_desc.min_filter = MinMagFilter::kLinear;
sampler_desc.mag_filter = MinMagFilter::kLinear;
auto sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc);
return pass.AddCommand(cmd);
Command cmd;
cmd.label = "Gaussian Blur Filter";
auto options = OptionsFromPass(pass);
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetGaussianBlurPipeline(options);
cmd.BindVertices(vtx_buffer);
FS::BindTextureSampler(cmd, input_snapshot->texture, sampler);
FS::BindAlphaMaskSampler(cmd, source_snapshot->texture, sampler);
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
return pass.AddCommand(cmd);
};
auto out_texture = renderer.MakeSubpass(ISize(coverage.size), callback);
if (!out_texture) {
return std::nullopt;
}
out_texture->SetLabel("DirectionalGaussianBlurFilter Texture");
return Snapshot{.texture = out_texture,
.transform = Matrix::MakeTranslation(coverage.origin)};
}
std::optional<Rect> DirectionalGaussianBlurFilterContents::GetFilterCoverage(

View File

@@ -33,11 +33,11 @@ class DirectionalGaussianBlurFilterContents final : public FilterContents {
private:
// |FilterContents|
bool RenderFilter(const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& coverage) const override;
std::optional<Snapshot> RenderFilter(
const FilterInput::Vector& input_textures,
const ContentContext& renderer,
const Entity& entity,
const Rect& coverage) const override;
Sigma blur_sigma_;
Vector2 blur_direction_;
BlurStyle blur_style_ = BlurStyle::kNormal;