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 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "impeller/entity/contents/filters/border_mask_blur_filter_contents.h"
#include "impeller/entity/contents/content_context.h"

#include "impeller/entity/contents/anonymous_contents.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_library.h"
Expand Down Expand Up @@ -68,50 +69,61 @@ std::optional<Entity> BorderMaskBlurFilterContents::RenderFilter(
if (!input_snapshot.has_value()) {
return std::nullopt;
}

auto maybe_input_uvs = input_snapshot->GetCoverageUVs(coverage);
if (!maybe_input_uvs.has_value()) {
return std::nullopt;
}
auto input_uvs = maybe_input_uvs.value();

//----------------------------------------------------------------------------
/// Render to texture.
/// Create AnonymousContents for rendering.
///

ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
auto sigma = effect_transform * Vector2(sigma_x_.sigma, sigma_y_.sigma);
RenderProc render_proc = [coverage, input_snapshot, input_uvs = input_uvs,
src_color_factor = src_color_factor_,
inner_blur_factor = inner_blur_factor_,
outer_blur_factor = outer_blur_factor_, sigma](
const ContentContext& renderer,
const Entity& entity, RenderPass& pass) -> bool {
auto& host_buffer = pass.GetTransientsBuffer();

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]},
{coverage.origin, input_uvs[0]},
{{coverage.origin.x + coverage.size.width, coverage.origin.y},
input_uvs[1]},
{{coverage.origin.x + coverage.size.width,
coverage.origin.y + coverage.size.height},
input_uvs[3]},
{coverage.origin, input_uvs[0]},
{{coverage.origin.x + coverage.size.width,
coverage.origin.y + coverage.size.height},
input_uvs[3]},
{{coverage.origin.x, coverage.origin.y + coverage.size.height},
input_uvs[2]},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);

Command cmd;
cmd.label = "Border Mask Blur Filter";
auto options = OptionsFromPass(pass);
options.blend_mode = BlendMode::kSource;
auto options = OptionsFromPassAndEntity(pass, entity);

cmd.pipeline = renderer.GetBorderMaskBlurPipeline(options);
cmd.BindVertices(vtx_buffer);
cmd.stencil_reference = entity.GetStencilDepth();

VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();

auto sigma = effect_transform * Vector2(sigma_x_.sigma, sigma_y_.sigma);

FS::FragInfo frag_info;
frag_info.sigma_uv = sigma.Abs() / input_snapshot->texture->GetSize();
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.src_factor = src_color_factor;
frag_info.inner_blur_factor = inner_blur_factor;
frag_info.outer_blur_factor = outer_blur_factor;

FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
Expand All @@ -122,17 +134,19 @@ std::optional<Entity> BorderMaskBlurFilterContents::RenderFilter(
return pass.AddCommand(std::move(cmd));
};

auto out_texture = renderer.MakeSubpass("Border Mask Blur Filter",
ISize(coverage.size), callback);
if (!out_texture) {
return std::nullopt;
}
CoverageProc coverage_proc =
[coverage](const Entity& entity) -> std::optional<Rect> {
return coverage;
};

auto contents = AnonymousContents::Make(render_proc, coverage_proc);

return Entity::FromSnapshot(
Snapshot{.texture = out_texture,
.transform = Matrix::MakeTranslation(coverage.origin),
.opacity = input_snapshot->opacity},
entity.GetBlendMode(), entity.GetStencilDepth());
Entity sub_entity;
sub_entity.SetContents(std::move(contents));
sub_entity.SetStencilDepth(entity.GetStencilDepth());
sub_entity.SetTransformation(entity.GetTransformation());
sub_entity.SetBlendMode(entity.GetBlendMode());
return sub_entity;
}

std::optional<Rect> BorderMaskBlurFilterContents::GetFilterCoverage(
Expand Down
62 changes: 36 additions & 26 deletions impeller/entity/contents/filters/color_matrix_filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <optional>

#include "impeller/entity/contents/anonymous_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/geometry/point.h"
Expand Down Expand Up @@ -46,38 +47,45 @@ std::optional<Entity> ColorMatrixFilterContents::RenderFilter(
}

//----------------------------------------------------------------------------
/// Render to texture.
/// Create AnonymousContents for rendering.
///

ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
RenderProc render_proc = [input_snapshot, color_matrix = matrix_, coverage,
absorb_opacity = GetAbsorbOpacity()](
const ContentContext& renderer,
const Entity& entity, RenderPass& pass) -> bool {
Command cmd;
cmd.label = "Color Matrix Filter";
cmd.stencil_reference = entity.GetStencilDepth();

auto options = OptionsFromPass(pass);
options.blend_mode = BlendMode::kSource;
auto options = OptionsFromPassAndEntity(pass, entity);
cmd.pipeline = renderer.GetColorMatrixColorFilterPipeline(options);

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)},
{coverage.origin, Point(0, 0)},
{{coverage.origin.x + coverage.size.width, coverage.origin.y},
Point(1, 0)},
{{coverage.origin.x + coverage.size.width,
coverage.origin.y + coverage.size.height},
Point(1, 1)},
{coverage.origin, Point(0, 0)},
{{coverage.origin.x + coverage.size.width,
coverage.origin.y + coverage.size.height},
Point(1, 1)},
{{coverage.origin.x, coverage.origin.y + coverage.size.height},
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));
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();

FS::FragInfo frag_info;
const float* matrix = matrix_.array;
const float* matrix = color_matrix.array;
frag_info.color_v = Vector4(matrix[4], matrix[9], matrix[14], matrix[19]);
// clang-format off
frag_info.color_m = Matrix(
Expand All @@ -87,7 +95,7 @@ std::optional<Entity> ColorMatrixFilterContents::RenderFilter(
matrix[3], matrix[8], matrix[13], matrix[18]
);
// clang-format on
frag_info.input_alpha = GetAbsorbOpacity() ? input_snapshot->opacity : 1.0f;
frag_info.input_alpha = absorb_opacity ? input_snapshot->opacity : 1.0f;
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindInputTexture(cmd, input_snapshot->texture, sampler);
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
Expand All @@ -97,18 +105,20 @@ std::optional<Entity> ColorMatrixFilterContents::RenderFilter(
return pass.AddCommand(std::move(cmd));
};

auto out_texture = renderer.MakeSubpass(
"Color Matrix Filter", input_snapshot->texture->GetSize(), callback);
if (!out_texture) {
return std::nullopt;
}
CoverageProc coverage_proc =
[coverage](const Entity& entity) -> std::optional<Rect> {
return coverage;
};

auto contents = AnonymousContents::Make(render_proc, coverage_proc);

return Entity::FromSnapshot(
Snapshot{.texture = out_texture,
.transform = input_snapshot->transform,
.sampler_descriptor = input_snapshot->sampler_descriptor,
.opacity = GetAbsorbOpacity() ? 1.0f : input_snapshot->opacity},
entity.GetBlendMode(), entity.GetStencilDepth());
Entity sub_entity;
sub_entity.SetContents(std::move(contents));
sub_entity.SetStencilDepth(entity.GetStencilDepth());
sub_entity.SetTransformation(Matrix::MakeTranslation(coverage.origin) *
entity.GetTransformation());
sub_entity.SetBlendMode(entity.GetBlendMode());
return sub_entity;
}

} // namespace impeller
65 changes: 42 additions & 23 deletions impeller/entity/contents/filters/linear_to_srgb_filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

#include "impeller/entity/contents/anonymous_contents.h"
#include "impeller/entity/contents/content_context.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/geometry/point.h"
Expand Down Expand Up @@ -35,36 +36,53 @@ std::optional<Entity> LinearToSrgbFilterContents::RenderFilter(
return std::nullopt;
}

ContentContext::SubpassCallback callback = [&](const ContentContext& renderer,
RenderPass& pass) {
auto maybe_input_uvs = input_snapshot->GetCoverageUVs(coverage);
if (!maybe_input_uvs.has_value()) {
return std::nullopt;
}
auto input_uvs = maybe_input_uvs.value();

//----------------------------------------------------------------------------
/// Create AnonymousContents for rendering.
///
RenderProc render_proc = [input_snapshot, coverage, input_uvs,
absorb_opacity = GetAbsorbOpacity()](
const ContentContext& renderer,
const Entity& entity, RenderPass& pass) -> bool {
Command cmd;
cmd.label = "Linear to sRGB Filter";
cmd.stencil_reference = entity.GetStencilDepth();

auto options = OptionsFromPass(pass);
options.blend_mode = BlendMode::kSource;
auto options = OptionsFromPassAndEntity(pass, entity);
cmd.pipeline = renderer.GetLinearToSrgbFilterPipeline(options);

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)},
{coverage.origin, input_uvs[0]},
{{coverage.origin.x + coverage.size.width, coverage.origin.y},
input_uvs[1]},
{{coverage.origin.x + coverage.size.width,
coverage.origin.y + coverage.size.height},
input_uvs[3]},
{coverage.origin, input_uvs[0]},
{{coverage.origin.x + coverage.size.width,
coverage.origin.y + coverage.size.height},
input_uvs[3]},
{{coverage.origin.x, coverage.origin.y + coverage.size.height},
input_uvs[2]},
});

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));
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize());
frame_info.texture_sampler_y_coord_scale =
input_snapshot->texture->GetYCoordScale();

FS::FragInfo frag_info;
frag_info.input_alpha = GetAbsorbOpacity() ? input_snapshot->opacity : 1.0f;
frag_info.input_alpha = absorb_opacity ? input_snapshot->opacity : 1.0f;

auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindInputTexture(cmd, input_snapshot->texture, sampler);
Expand All @@ -74,18 +92,19 @@ std::optional<Entity> LinearToSrgbFilterContents::RenderFilter(
return pass.AddCommand(std::move(cmd));
};

auto out_texture = renderer.MakeSubpass(
"Linear to sRGB Filter", input_snapshot->texture->GetSize(), callback);
if (!out_texture) {
return std::nullopt;
}
CoverageProc coverage_proc =
[coverage](const Entity& entity) -> std::optional<Rect> {
return coverage;
};

auto contents = AnonymousContents::Make(render_proc, coverage_proc);

return Entity::FromSnapshot(
Snapshot{.texture = out_texture,
.transform = input_snapshot->transform,
.sampler_descriptor = input_snapshot->sampler_descriptor,
.opacity = GetAbsorbOpacity() ? 1.0f : input_snapshot->opacity},
entity.GetBlendMode(), entity.GetStencilDepth());
Entity sub_entity;
sub_entity.SetContents(std::move(contents));
sub_entity.SetStencilDepth(entity.GetStencilDepth());
sub_entity.SetTransformation(entity.GetTransformation());
sub_entity.SetBlendMode(entity.GetBlendMode());
return sub_entity;
}

} // namespace impeller
Loading