Filters: Replace Snapshot position with a full transform (flutter/engine#130)

This commit is contained in:
Brandon DeRosier
2022-04-14 02:04:42 -07:00
committed by Dan Field
parent 75634a6ab9
commit 5a342e838d
17 changed files with 247 additions and 202 deletions

View File

@@ -33,7 +33,7 @@ std::optional<Rect> Contents::GetCoverage(const Entity& entity) const {
return entity.GetPathCoverage();
}
std::optional<Snapshot> Contents::RenderToTexture(
std::optional<Snapshot> Contents::RenderToSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
auto bounds = GetCoverage(entity);
@@ -58,7 +58,8 @@ std::optional<Snapshot> Contents::RenderToTexture(
return std::nullopt;
}
return Snapshot{.texture = texture, .position = bounds->origin};
return Snapshot{.texture = texture,
.transform = Matrix::MakeTranslation(bounds->origin)};
}
} // namespace impeller

View File

@@ -39,11 +39,11 @@ class Contents {
/// @brief Get the screen space bounding rectangle that this contents affects.
virtual std::optional<Rect> GetCoverage(const Entity& entity) const;
/// @brief Render this contents to a texture, respecting the entity's
/// @brief Render this contents to a snapshot, respecting the entity's
/// transform, path, stencil depth, blend mode, etc.
/// The result texture size is always the size of
/// `GetCoverage(entity)`.
virtual std::optional<Snapshot> RenderToTexture(
virtual std::optional<Snapshot> RenderToSnapshot(
const ContentContext& renderer,
const Entity& entity) const;

View File

@@ -25,23 +25,43 @@ static bool AdvancedBlend(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& bounds,
const Rect& coverage,
PipelineProc pipeline_proc) {
if (inputs.size() < 2) {
return false;
}
auto dst_snapshot = inputs[1]->GetSnapshot(renderer, entity);
if (!dst_snapshot.has_value()) {
return true;
}
auto maybe_dst_uvs = dst_snapshot->GetCoverageUVs(coverage);
if (!maybe_dst_uvs.has_value()) {
return true;
}
auto dst_uvs = maybe_dst_uvs.value();
auto src_snapshot = inputs[0]->GetSnapshot(renderer, entity);
if (!src_snapshot.has_value()) {
return true;
}
auto maybe_src_uvs = src_snapshot->GetCoverageUVs(coverage);
if (!maybe_src_uvs.has_value()) {
return true;
}
auto src_uvs = maybe_src_uvs.value();
auto& host_buffer = pass.GetTransientsBuffer();
auto size = pass.GetRenderTargetSize();
VertexBufferBuilder<typename VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), Point(0, 0)},
{Point(size.width, 0), Point(1, 0)},
{Point(size.width, size.height), Point(1, 1)},
{Point(0, 0), Point(0, 0)},
{Point(size.width, size.height), Point(1, 1)},
{Point(0, size.height), Point(0, 1)},
{Point(0, 0), dst_uvs[0], src_uvs[0]},
{Point(size.width, 0), dst_uvs[1], src_uvs[1]},
{Point(size.width, size.height), dst_uvs[3], src_uvs[3]},
{Point(0, 0), dst_uvs[0], src_uvs[0]},
{Point(size.width, size.height), dst_uvs[3], src_uvs[3]},
{Point(0, size.height), dst_uvs[2], src_uvs[2]},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
@@ -56,25 +76,12 @@ static bool AdvancedBlend(const FilterInput::Vector& inputs,
cmd.pipeline = std::move(pipeline);
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, sampler);
FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, sampler);
typename VS::FrameInfo frame_info;
frame_info.mvp = Matrix::MakeOrthographic(size);
auto dst_snapshot = inputs[1]->GetSnapshot(renderer, entity);
FS::BindTextureSamplerSrc(cmd, dst_snapshot->texture, sampler);
frame_info.dst_uv_transform =
Matrix::MakeTranslation(-(dst_snapshot->position - bounds.origin) /
size) *
Matrix::MakeScale(
Vector3(Size(size) / Size(dst_snapshot->texture->GetSize())));
auto src_snapshot = inputs[0]->GetSnapshot(renderer, entity);
FS::BindTextureSamplerDst(cmd, src_snapshot->texture, sampler);
frame_info.src_uv_transform =
Matrix::MakeTranslation(-(src_snapshot->position - bounds.origin) /
size) *
Matrix::MakeScale(
Vector3(Size(size) / Size(src_snapshot->texture->GetSize())));
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
VS::BindFrameInfo(cmd, uniform_view);
pass.AddCommand(cmd);
@@ -99,11 +106,11 @@ void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
advanced_blend_proc_ = [](const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity, RenderPass& pass,
const Rect& bounds) {
const Rect& coverage) {
PipelineProc p = &ContentContext::GetTextureBlendScreenPipeline;
return AdvancedBlend<TextureBlendScreenPipeline::VertexShader,
TextureBlendScreenPipeline::FragmentShader>(
inputs, renderer, entity, pass, bounds, p);
inputs, renderer, entity, pass, coverage, p);
};
break;
default:
@@ -116,49 +123,62 @@ static bool BasicBlend(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& bounds,
const Rect& coverage,
Entity::BlendMode basic_blend) {
using VS = TextureBlendPipeline::VertexShader;
using FS = TextureBlendPipeline::FragmentShader;
auto& host_buffer = pass.GetTransientsBuffer();
auto size = pass.GetRenderTargetSize();
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), Point(0, 0)},
{Point(size.width, 0), Point(1, 0)},
{Point(size.width, size.height), Point(1, 1)},
{Point(0, 0), Point(0, 0)},
{Point(size.width, size.height), Point(1, 1)},
{Point(0, size.height), Point(0, 1)},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
// Draw the first texture using kSource.
Command cmd;
cmd.label = "Basic Blend Filter";
cmd.BindVertices(vtx_buffer);
auto options = OptionsFromPass(pass);
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetTextureBlendPipeline(options);
{
auto input = inputs[0]->GetSnapshot(renderer, entity);
auto add_blend_command = [&](std::optional<Snapshot> input) {
if (!input.has_value()) {
return false;
}
auto input_coverage = input->GetCoverage();
if (!input_coverage.has_value()) {
return false;
}
FS::BindTextureSamplerSrc(cmd, input->texture, sampler);
auto size = input->texture->GetSize();
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), Point(0, 0)},
{Point(size.width, 0), Point(1, 0)},
{Point(size.width, size.height), Point(1, 1)},
{Point(0, 0), Point(0, 0)},
{Point(size.width, size.height), Point(1, 1)},
{Point(0, size.height), Point(0, 1)},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
cmd.BindVertices(vtx_buffer);
VS::FrameInfo frame_info;
frame_info.mvp =
Matrix::MakeOrthographic(size) *
Matrix::MakeTranslation(input->position - bounds.origin) *
Matrix::MakeScale(Size(input->texture->GetSize()) / Size(size));
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
Matrix::MakeTranslation(-coverage.origin) *
input->transform;
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
VS::BindFrameInfo(cmd, uniform_view);
pass.AddCommand(cmd);
return true;
};
// Draw the first texture using kSource.
options.blend_mode = Entity::BlendMode::kSource;
cmd.pipeline = renderer.GetTextureBlendPipeline(options);
if (!add_blend_command(inputs[0]->GetSnapshot(renderer, entity))) {
return true;
}
pass.AddCommand(cmd);
if (inputs.size() < 2) {
return true;
@@ -172,17 +192,9 @@ static bool BasicBlend(const FilterInput::Vector& inputs,
for (auto texture_i = inputs.begin() + 1; texture_i < inputs.end();
texture_i++) {
auto input = texture_i->get()->GetSnapshot(renderer, entity);
FS::BindTextureSamplerSrc(cmd, input->texture, sampler);
VS::FrameInfo frame_info;
frame_info.mvp = frame_info.mvp =
Matrix::MakeOrthographic(size) *
Matrix::MakeTranslation(input->position - bounds.origin) *
Matrix::MakeScale(Size(input->texture->GetSize()) / Size(size));
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
VS::BindFrameInfo(cmd, uniform_view);
pass.AddCommand(cmd);
if (!add_blend_command(input)) {
return true;
}
}
return true;
@@ -192,23 +204,23 @@ bool BlendFilterContents::RenderFilter(const FilterInput::Vector& inputs,
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& bounds) const {
const Rect& coverage) const {
if (inputs.empty()) {
return true;
}
if (inputs.size() == 1) {
// Nothing to blend.
return BasicBlend(inputs, renderer, entity, pass, bounds,
return BasicBlend(inputs, renderer, entity, pass, coverage,
Entity::BlendMode::kSource);
}
if (blend_mode_ <= Entity::BlendMode::kLastPipelineBlendMode) {
return BasicBlend(inputs, renderer, entity, pass, bounds, blend_mode_);
return BasicBlend(inputs, renderer, entity, pass, coverage, blend_mode_);
}
if (blend_mode_ <= Entity::BlendMode::kLastAdvancedBlendMode) {
return advanced_blend_proc_(inputs, renderer, entity, pass, bounds);
return advanced_blend_proc_(inputs, renderer, entity, pass, coverage);
}
FML_UNREACHABLE();

View File

@@ -16,7 +16,7 @@ class BlendFilterContents : public FilterContents {
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& bounds)>;
const Rect& coverage)>;
BlendFilterContents();
@@ -30,7 +30,7 @@ class BlendFilterContents : public FilterContents {
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& bounds) const override;
const Rect& coverage) const override;
Entity::BlendMode blend_mode_;
AdvancedBlendProc advanced_blend_proc_;

View File

@@ -50,7 +50,9 @@ std::shared_ptr<FilterContents> FilterContents::MakeBlend(
new_blend = std::make_shared<BlendFilterContents>();
new_blend->SetInputs({blend_input, *in_i});
new_blend->SetBlendMode(blend_mode);
blend_input = FilterInput::Make(new_blend);
if (in_i < inputs.end() - 1) {
blend_input = FilterInput::Make(new_blend);
}
}
// new_blend will always be assigned because inputs.size() >= 2.
return new_blend;
@@ -104,7 +106,7 @@ bool FilterContents::Render(const ContentContext& renderer,
// Run the filter.
auto maybe_snapshot = RenderToTexture(renderer, entity);
auto maybe_snapshot = RenderToSnapshot(renderer, entity);
if (!maybe_snapshot.has_value()) {
return false;
}
@@ -147,7 +149,7 @@ std::optional<Rect> FilterContents::GetCoverage(const Entity& entity) const {
return result;
}
std::optional<Snapshot> FilterContents::RenderToTexture(
std::optional<Snapshot> FilterContents::RenderToSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
auto bounds = GetCoverage(entity);
@@ -166,7 +168,8 @@ std::optional<Snapshot> FilterContents::RenderToTexture(
return std::nullopt;
}
return Snapshot{.texture = texture, .position = bounds->origin};
return Snapshot{.texture = texture,
.transform = Matrix::MakeTranslation(bounds->origin)};
}
} // namespace impeller

View File

@@ -116,7 +116,7 @@ class FilterContents : public Contents {
std::optional<Rect> GetCoverage(const Entity& entity) const override;
// |Contents|
virtual std::optional<Snapshot> RenderToTexture(
virtual std::optional<Snapshot> RenderToSnapshot(
const ContentContext& renderer,
const Entity& entity) const override;

View File

@@ -7,6 +7,7 @@
#include <cstdarg>
#include <initializer_list>
#include <memory>
#include <optional>
#include "impeller/entity/contents/snapshot.h"
#include "impeller/entity/entity.h"
@@ -32,7 +33,7 @@ FilterInput::Variant FilterInput::GetInput() const {
std::optional<Rect> FilterInput::GetCoverage(const Entity& entity) const {
if (snapshot_) {
return Rect(snapshot_->position, Size(snapshot_->texture->GetSize()));
return snapshot_->GetCoverage();
}
if (auto contents = std::get_if<std::shared_ptr<Contents>>(&input_)) {
@@ -51,7 +52,7 @@ std::optional<Snapshot> FilterInput::GetSnapshot(const ContentContext& renderer,
if (snapshot_) {
return snapshot_;
}
snapshot_ = RenderToTexture(renderer, entity);
snapshot_ = MakeSnapshot(renderer, entity);
return snapshot_;
}
@@ -60,15 +61,26 @@ FilterInput::FilterInput(Variant input) : input_(input) {}
FilterInput::~FilterInput() = default;
std::optional<Snapshot> FilterInput::RenderToTexture(
std::optional<Snapshot> FilterInput::MakeSnapshot(
const ContentContext& renderer,
const Entity& entity) const {
if (auto contents = std::get_if<std::shared_ptr<Contents>>(&input_)) {
return contents->get()->RenderToTexture(renderer, entity);
return contents->get()->RenderToSnapshot(renderer, entity);
}
if (auto texture = std::get_if<std::shared_ptr<Texture>>(&input_)) {
return Snapshot::FromTransformedTexture(renderer, entity, *texture);
// Rendered textures stretch to fit the entity path coverage, so we
// incorporate this behavior by translating and scaling the snapshot
// transform.
auto path_bounds = entity.GetPath().GetBoundingBox();
if (!path_bounds.has_value()) {
return std::nullopt;
}
auto transform = entity.GetTransformation() *
Matrix::MakeTranslation(path_bounds->origin) *
Matrix::MakeScale(Vector2(path_bounds->size) /
texture->get()->GetSize());
return Snapshot{.texture = *texture, .transform = transform};
}
FML_UNREACHABLE();

View File

@@ -50,8 +50,10 @@ class FilterInput final {
private:
FilterInput(Variant input);
std::optional<Snapshot> RenderToTexture(const ContentContext& renderer,
const Entity& entity) const;
std::optional<Snapshot> MakeSnapshot(const ContentContext& renderer,
const Entity& entity) const;
std::optional<Snapshot> MakeSnapshotForTexture(const Entity& entity) const;
Variant input_;
mutable std::optional<Snapshot> snapshot_;

View File

@@ -11,7 +11,9 @@
#include "impeller/entity/contents/filters/filter_contents.h"
#include "impeller/geometry/rect.h"
#include "impeller/geometry/scalar.h"
#include "impeller/renderer/formats.h"
#include "impeller/renderer/render_pass.h"
#include "impeller/renderer/sampler_descriptor.h"
#include "impeller/renderer/sampler_library.h"
namespace impeller {
@@ -69,8 +71,8 @@ void DirectionalGaussianBlurFilterContents::SetBlurStyle(BlurStyle blur_style) {
}
void DirectionalGaussianBlurFilterContents::SetSourceOverride(
FilterInput::Ref alpha_mask) {
source_override_ = alpha_mask;
FilterInput::Ref source_override) {
source_override_ = source_override;
}
bool DirectionalGaussianBlurFilterContents::RenderFilter(
@@ -78,7 +80,7 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter(
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& bounds) const {
const Rect& coverage) const {
if (inputs.empty()) {
return true;
}
@@ -88,79 +90,61 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter(
auto& host_buffer = pass.GetTransientsBuffer();
auto input = inputs[0]->GetSnapshot(renderer, entity);
if (!input.has_value()) {
return true;
}
// Input 0 snapshot and UV mapping.
auto input_bounds = inputs[0]->GetCoverage(entity);
if (!input_bounds.has_value() || input_bounds->IsEmpty()) {
auto input_snapshot = inputs[0]->GetSnapshot(renderer, entity);
if (!input_snapshot.has_value()) {
return true;
}
auto filter_bounds = GetCoverage(entity);
if (!filter_bounds.has_value() || filter_bounds->IsEmpty()) {
FML_LOG(ERROR) << "The gaussian blur filter coverage is missing or empty "
"even though the filter's input has coverage.";
return false;
auto maybe_input_uvs = input_snapshot->GetCoverageUVs(coverage);
if (!maybe_input_uvs.has_value()) {
return true;
}
auto input_uvs = maybe_input_uvs.value();
// Source override snapshot and UV mapping.
auto source = source_override_ ? source_override_ : inputs[0];
auto source_snapshot = source->GetSnapshot(renderer, entity);
if (!source_snapshot.has_value()) {
return true;
}
auto maybe_source_uvs = source_snapshot->GetCoverageUVs(coverage);
if (!maybe_source_uvs.has_value()) {
return true;
}
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);
auto transformed_blur = entity.GetTransformation().TransformDirection(
blur_direction_ * blur_sigma_.sigma);
// LTRB
Scalar uv[4] = {
(filter_bounds->GetLeft() - input_bounds->GetLeft()) /
input_bounds->size.width,
(filter_bounds->GetTop() - input_bounds->GetTop()) /
input_bounds->size.height,
1 + (filter_bounds->GetRight() - input_bounds->GetRight()) /
input_bounds->size.width,
1 + (filter_bounds->GetBottom() - input_bounds->GetBottom()) /
input_bounds->size.height,
};
auto source = source_override_ ? source_override_ : inputs[0];
auto source_texture = source->GetSnapshot(renderer, entity);
auto source_bounds = source->GetCoverage(entity);
if (!source_texture.has_value() || !source_bounds.has_value() ||
source_bounds->IsEmpty()) {
VALIDATION_LOG << "The gaussian blur source override has no coverage.";
return false;
}
// LTRB
Scalar uv_src[4] = {
(filter_bounds->GetLeft() - source_bounds->GetLeft()) /
source_bounds->size.width,
(filter_bounds->GetTop() - source_bounds->GetTop()) /
source_bounds->size.height,
1 + (filter_bounds->GetRight() - source_bounds->GetRight()) /
source_bounds->size.width,
1 + (filter_bounds->GetBottom() - source_bounds->GetBottom()) /
source_bounds->size.height,
};
VertexBufferBuilder<VS::PerVertexData> vtx_builder;
vtx_builder.AddVertices({
{Point(0, 0), Point(uv[0], uv[1]), Point(uv_src[0], uv_src[1])},
{Point(1, 0), Point(uv[2], uv[1]), Point(uv_src[2], uv_src[1])},
{Point(1, 1), Point(uv[2], uv[3]), Point(uv_src[2], uv_src[3])},
{Point(0, 0), Point(uv[0], uv[1]), Point(uv_src[0], uv_src[1])},
{Point(1, 1), Point(uv[2], uv[3]), Point(uv_src[2], uv_src[3])},
{Point(0, 1), Point(uv[0], uv[3]), Point(uv_src[0], uv_src[3])},
});
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
VS::FrameInfo frame_info;
frame_info.texture_size = Point(input_bounds->size);
frame_info.texture_size = Point(input_snapshot->GetCoverage().value().size);
frame_info.blur_sigma = transformed_blur.GetLength();
frame_info.blur_radius = Radius{Sigma{frame_info.blur_sigma}}.radius;
frame_info.blur_direction = transformed_blur.Normalize();
frame_info.blur_direction = input_snapshot->transform.Invert()
.TransformDirection(transformed_blur)
.Normalize();
frame_info.src_factor = src_color_factor_;
frame_info.inner_blur_factor = inner_blur_factor_;
frame_info.outer_blur_factor = outer_blur_factor_;
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
SamplerDescriptor sampler_desc;
sampler_desc.min_filter = MinMagFilter::kLinear;
sampler_desc.mag_filter = MinMagFilter::kLinear;
auto sampler =
renderer.GetContext()->GetSamplerLibrary()->GetSampler(sampler_desc);
Command cmd;
cmd.label = "Gaussian Blur Filter";
@@ -169,8 +153,8 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter(
cmd.pipeline = renderer.GetGaussianBlurPipeline(options);
cmd.BindVertices(vtx_buffer);
FS::BindTextureSampler(cmd, input->texture, sampler);
FS::BindAlphaMaskSampler(cmd, source_texture->texture, sampler);
FS::BindTextureSampler(cmd, input_snapshot->texture, sampler);
FS::BindAlphaMaskSampler(cmd, source_snapshot->texture, sampler);
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
@@ -181,8 +165,8 @@ bool DirectionalGaussianBlurFilterContents::RenderFilter(
std::optional<Rect> DirectionalGaussianBlurFilterContents::GetCoverage(
const Entity& entity) const {
auto bounds = FilterContents::GetCoverage(entity);
if (!bounds.has_value()) {
auto coverage = FilterContents::GetCoverage(entity);
if (!coverage.has_value()) {
return std::nullopt;
}
@@ -191,8 +175,8 @@ std::optional<Rect> DirectionalGaussianBlurFilterContents::GetCoverage(
.TransformDirection(blur_direction_ *
ceil(Radius{blur_sigma_}.radius))
.Abs();
auto extent = bounds->size + transformed_blur_vector * 2;
return Rect(bounds->origin - transformed_blur_vector,
auto extent = coverage->size + transformed_blur_vector * 2;
return Rect(coverage->origin - transformed_blur_vector,
Size(extent.x, extent.y));
}

View File

@@ -35,7 +35,7 @@ class DirectionalGaussianBlurFilterContents final : public FilterContents {
const ContentContext& renderer,
const Entity& entity,
RenderPass& pass,
const Rect& bounds) const override;
const Rect& coverage) const override;
Sigma blur_sigma_;
Vector2 blur_direction_;
BlurStyle blur_style_ = BlurStyle::kNormal;

View File

@@ -11,35 +11,28 @@
namespace impeller {
std::optional<Snapshot> Snapshot::FromTransformedTexture(
const ContentContext& renderer,
const Entity& entity,
std::shared_ptr<Texture> texture) {
auto bounds = entity.GetPathCoverage();
if (!bounds.has_value()) {
std::optional<Rect> Snapshot::GetCoverage() const {
if (!texture) {
return std::nullopt;
}
return Rect(Size(texture->GetSize())).TransformBounds(transform);
}
auto result = renderer.MakeSubpass(
ISize(bounds->size),
[&texture, &entity, &bounds](const ContentContext& renderer,
RenderPass& pass) -> bool {
TextureContents contents;
contents.SetTexture(texture);
contents.SetSourceRect(Rect::MakeSize(Size(texture->GetSize())));
Entity sub_entity;
sub_entity.SetPath(entity.GetPath());
sub_entity.SetBlendMode(Entity::BlendMode::kSource);
sub_entity.SetTransformation(
Matrix::MakeTranslation(Vector3(-bounds->origin)) *
entity.GetTransformation());
return contents.Render(renderer, sub_entity, pass);
});
if (!result) {
std::optional<Matrix> Snapshot::GetUVTransform() const {
if (!texture || texture->GetSize().IsZero()) {
return std::nullopt;
}
return Matrix::MakeScale(1 / Vector2(texture->GetSize())) *
transform.Invert();
}
return Snapshot{.texture = result, .position = bounds->origin};
std::optional<std::array<Point, 4>> Snapshot::GetCoverageUVs(
const Rect& coverage) const {
auto uv_transform = GetUVTransform();
if (!uv_transform.has_value()) {
return std::nullopt;
}
return coverage.GetTransformedPoints(uv_transform.value());
}
} // namespace impeller

View File

@@ -21,16 +21,19 @@ class Entity;
/// Represents a texture and its intended draw position.
struct Snapshot {
std::shared_ptr<Texture> texture;
/// The offset from the origin where this texture is intended to be
/// rendered.
Vector2 position;
/// The transform that should be applied to this texture for rendering.
Matrix transform;
/// Transform a texture by the given `entity`'s transformation matrix to a new
/// texture.
static std::optional<Snapshot> FromTransformedTexture(
const ContentContext& renderer,
const Entity& entity,
std::shared_ptr<Texture> texture);
std::optional<Rect> GetCoverage() const;
/// @brief Get the transform that converts screen space coordinates to the UV
/// space of this snapshot.
std::optional<Matrix> GetUVTransform() const;
/// @brief Map a coverage rect to this filter input's UV space.
/// Result order: Top left, top right, bottom left, bottom right.
std::optional<std::array<Point, 4>> GetCoverageUVs(
const Rect& coverage) const;
};
} // namespace impeller

View File

@@ -733,8 +733,9 @@ TEST_F(EntityTest, GaussianBlurFilter) {
}
ImGui::End();
auto blend = FilterContents::MakeBlend(
Entity::BlendMode::kPlus, FilterInput::Make({boston, bridge, bridge}));
auto blend =
FilterContents::MakeBlend(Entity::BlendMode::kScreen,
FilterInput::Make({boston, bridge, kalimba}));
auto blur = FilterContents::MakeGaussianBlur(
FilterInput::Make(blend), FilterContents::Sigma{blur_amount[0]},
@@ -745,7 +746,7 @@ TEST_F(EntityTest, GaussianBlurFilter) {
auto rect = Rect(-Point(input_size) / 2, Size(input_size));
auto ctm = Matrix::MakeTranslation(Vector3(offset[0], offset[1])) *
Matrix::MakeRotationZ(Radians(rotation)) *
Matrix::MakeScale(Vector3(scale[0], scale[1])) *
Matrix::MakeScale(Vector2(scale[0], scale[1])) *
Matrix::MakeSkew(skew[0], skew[1]);
auto target_contents = blur;

View File

@@ -4,20 +4,18 @@
uniform FrameInfo {
mat4 mvp;
mat4 dst_uv_transform;
mat4 src_uv_transform;
} frame_info;
}
frame_info;
in vec2 vertices;
in vec2 texture_coords;
in vec2 dst_texture_coords;
in vec2 src_texture_coords;
out vec2 v_dst_texture_coords;
out vec2 v_src_texture_coords;
void main() {
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
v_dst_texture_coords =
(frame_info.dst_uv_transform * vec4(texture_coords, 1.0, 1.0)).xy;
v_src_texture_coords =
(frame_info.src_uv_transform * vec4(texture_coords, 1.0, 1.0)).xy;
v_dst_texture_coords = dst_texture_coords;
v_src_texture_coords = src_texture_coords;
}

View File

@@ -57,6 +57,19 @@ TEST(GeometryTest, InvertMultMatrix) {
}
}
TEST(GeometryTest, MatrixBasis) {
auto matrix = Matrix{1, 2, 3, 4, //
5, 6, 7, 8, //
9, 10, 11, 12, //
13, 14, 15, 16};
auto basis = matrix.Basis();
auto expect = Matrix{1, 2, 3, 0, //
5, 6, 7, 0, //
9, 10, 11, 0, //
0, 0, 0, 1};
ASSERT_MATRIX_NEAR(basis, expect);
}
TEST(GeometryTest, MutliplicationMatrix) {
auto rotation = Matrix::MakeRotationZ(Radians{M_PI_4});
auto invert = rotation.Invert();
@@ -862,6 +875,15 @@ TEST(GeometryTest, RectGetPoints) {
ASSERT_POINT_NEAR(points[3], Point(400, 600));
}
TEST(GeometryTest, RectGetTransformedPoints) {
Rect r(100, 200, 300, 400);
auto points = r.GetTransformedPoints(Matrix::MakeTranslation({10, 20}));
ASSERT_POINT_NEAR(points[0], Point(110, 220));
ASSERT_POINT_NEAR(points[1], Point(410, 220));
ASSERT_POINT_NEAR(points[2], Point(110, 620));
ASSERT_POINT_NEAR(points[3], Point(410, 620));
}
TEST(GeometryTest, RectMakePointBounds) {
{
Rect r =

View File

@@ -165,6 +165,17 @@ struct Matrix {
// clang-format on
}
constexpr Matrix Basis() const {
// clang-format off
return Matrix(
m[0], m[1], m[2], 0.0,
m[4], m[5], m[6], 0.0,
m[8], m[9], m[10], 0.0,
0.0, 0.0, 0.0, 1.0
);
// clang-format on
}
constexpr Matrix Translate(const Vector3& t) const {
// clang-format off
return Matrix(m[0], m[1], m[2], m[3],

View File

@@ -139,21 +139,24 @@ struct TRect {
}
constexpr std::array<TPoint<T>, 4> GetPoints() const {
const auto left = std::min(origin.x, origin.x + size.width);
const auto top = std::min(origin.y, origin.y + size.height);
const auto right = std::max(origin.x, origin.x + size.width);
const auto bottom = std::max(origin.y, origin.y + size.height);
auto [left, top, right, bottom] = GetLTRB();
return {TPoint(left, top), TPoint(right, top), TPoint(left, bottom),
TPoint(right, bottom)};
}
constexpr std::array<TPoint<T>, 4> GetTransformedPoints(
const Matrix& transform) const {
auto points = GetPoints();
for (uint i = 0; i < points.size(); i++) {
points[i] = transform * points[i];
}
return points;
}
/// @brief Creates a new bounding box that contains this transformed
/// rectangle.
constexpr TRect TransformBounds(const Matrix& transform) const {
auto points = GetPoints();
for (uint i = 0; i < points.size(); i++) {
points[i] = transform * points[i];
}
auto points = GetTransformedPoints(transform);
return TRect::MakePointBounds({points.begin(), points.end()}).value();
}