Add blend filter support for advanced blends (flutter/engine#81)
This commit is contained in:
committed by
Dan Field
parent
a1ff981559
commit
4cc86af054
@@ -14,6 +14,10 @@ impeller_shaders("entity_shaders") {
|
||||
"shaders/solid_fill.vert",
|
||||
"shaders/solid_stroke.frag",
|
||||
"shaders/solid_stroke.vert",
|
||||
"shaders/texture_blend.frag",
|
||||
"shaders/texture_blend.vert",
|
||||
"shaders/texture_blend_screen.frag",
|
||||
"shaders/texture_blend_screen.vert",
|
||||
"shaders/texture_fill.frag",
|
||||
"shaders/texture_fill.vert",
|
||||
"shaders/glyph_atlas.frag",
|
||||
|
||||
@@ -18,6 +18,10 @@ ContentContext::ContentContext(std::shared_ptr<Context> context)
|
||||
gradient_fill_pipelines_[{}] =
|
||||
std::make_unique<GradientFillPipeline>(*context_);
|
||||
solid_fill_pipelines_[{}] = std::make_unique<SolidFillPipeline>(*context_);
|
||||
texture_blend_pipelines_[{}] =
|
||||
std::make_unique<TextureBlendPipeline>(*context_);
|
||||
texture_blend_screen_pipelines_[{}] =
|
||||
std::make_unique<TextureBlendScreenPipeline>(*context_);
|
||||
texture_pipelines_[{}] = std::make_unique<TexturePipeline>(*context_);
|
||||
solid_stroke_pipelines_[{}] =
|
||||
std::make_unique<SolidStrokePipeline>(*context_);
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "flutter/fml/hash_combine.h"
|
||||
#include "flutter/fml/macros.h"
|
||||
#include "fml/logging.h"
|
||||
#include "impeller/base/validation.h"
|
||||
#include "impeller/entity/entity.h"
|
||||
#include "impeller/entity/glyph_atlas.frag.h"
|
||||
#include "impeller/entity/glyph_atlas.vert.h"
|
||||
@@ -21,6 +23,10 @@
|
||||
#include "impeller/entity/texture_fill.frag.h"
|
||||
#include "impeller/entity/texture_fill.vert.h"
|
||||
#include "impeller/renderer/formats.h"
|
||||
#include "texture_blend.frag.h"
|
||||
#include "texture_blend.vert.h"
|
||||
#include "texture_blend_screen.frag.h"
|
||||
#include "texture_blend_screen.vert.h"
|
||||
|
||||
namespace impeller {
|
||||
|
||||
@@ -28,6 +34,10 @@ using GradientFillPipeline =
|
||||
PipelineT<GradientFillVertexShader, GradientFillFragmentShader>;
|
||||
using SolidFillPipeline =
|
||||
PipelineT<SolidFillVertexShader, SolidFillFragmentShader>;
|
||||
using TextureBlendPipeline =
|
||||
PipelineT<TextureBlendVertexShader, TextureBlendFragmentShader>;
|
||||
using TextureBlendScreenPipeline =
|
||||
PipelineT<TextureBlendScreenVertexShader, TextureBlendScreenFragmentShader>;
|
||||
using TexturePipeline =
|
||||
PipelineT<TextureFillVertexShader, TextureFillFragmentShader>;
|
||||
using SolidStrokePipeline =
|
||||
@@ -75,6 +85,16 @@ class ContentContext {
|
||||
return GetPipeline(solid_fill_pipelines_, opts);
|
||||
}
|
||||
|
||||
std::shared_ptr<Pipeline> GetTextureBlendPipeline(
|
||||
ContentContextOptions opts) const {
|
||||
return GetPipeline(texture_blend_pipelines_, opts);
|
||||
}
|
||||
|
||||
std::shared_ptr<Pipeline> GetTextureBlendScreenPipeline(
|
||||
ContentContextOptions opts) const {
|
||||
return GetPipeline(texture_blend_screen_pipelines_, opts);
|
||||
}
|
||||
|
||||
std::shared_ptr<Pipeline> GetTexturePipeline(
|
||||
ContentContextOptions opts) const {
|
||||
return GetPipeline(texture_pipelines_, opts);
|
||||
@@ -115,6 +135,8 @@ class ContentContext {
|
||||
// map.
|
||||
mutable Variants<GradientFillPipeline> gradient_fill_pipelines_;
|
||||
mutable Variants<SolidFillPipeline> solid_fill_pipelines_;
|
||||
mutable Variants<TextureBlendPipeline> texture_blend_pipelines_;
|
||||
mutable Variants<TextureBlendScreenPipeline> texture_blend_screen_pipelines_;
|
||||
mutable Variants<TexturePipeline> texture_pipelines_;
|
||||
mutable Variants<SolidStrokePipeline> solid_stroke_pipelines_;
|
||||
mutable Variants<ClipPipeline> clip_pipelines_;
|
||||
@@ -123,12 +145,24 @@ class ContentContext {
|
||||
|
||||
static void ApplyOptionsToDescriptor(PipelineDescriptor& desc,
|
||||
const ContentContextOptions& options) {
|
||||
auto blend_mode = options.blend_mode;
|
||||
if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
VALIDATION_LOG << "Cannot use blend mode "
|
||||
<< static_cast<int>(options.blend_mode)
|
||||
<< " as a pipeline blend.";
|
||||
blend_mode = Entity::BlendMode::kSourceOver;
|
||||
}
|
||||
|
||||
desc.SetSampleCount(options.sample_count);
|
||||
|
||||
ColorAttachmentDescriptor color0 = *desc.GetColorAttachmentDescriptor(0u);
|
||||
color0.alpha_blend_op = BlendOperation::kAdd;
|
||||
color0.color_blend_op = BlendOperation::kAdd;
|
||||
switch (options.blend_mode) {
|
||||
|
||||
static_assert(Entity::BlendMode::kLastPipelineBlendMode ==
|
||||
Entity::BlendMode::kModulate);
|
||||
|
||||
switch (blend_mode) {
|
||||
case Entity::BlendMode::kClear:
|
||||
color0.dst_alpha_blend_factor = BlendFactor::kZero;
|
||||
color0.dst_color_blend_factor = BlendFactor::kZero;
|
||||
@@ -214,6 +248,8 @@ class ContentContext {
|
||||
color0.src_alpha_blend_factor = BlendFactor::kZero;
|
||||
color0.src_color_blend_factor = BlendFactor::kZero;
|
||||
break;
|
||||
default:
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
desc.SetColorAttachmentDescriptor(0u, std::move(color0));
|
||||
}
|
||||
|
||||
@@ -28,10 +28,33 @@ namespace impeller {
|
||||
std::shared_ptr<FilterContents> FilterContents::MakeBlend(
|
||||
Entity::BlendMode blend_mode,
|
||||
InputTextures input_textures) {
|
||||
auto blend = std::make_shared<BlendFilterContents>();
|
||||
blend->SetInputTextures(input_textures);
|
||||
blend->SetBlendMode(blend_mode);
|
||||
return blend;
|
||||
if (blend_mode > Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
VALIDATION_LOG << "Invalid blend mode " << static_cast<int>(blend_mode)
|
||||
<< " passed to FilterContents::MakeBlend.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (input_textures.size() < 2 ||
|
||||
blend_mode <= Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
auto blend = std::make_shared<BlendFilterContents>();
|
||||
blend->SetInputTextures(input_textures);
|
||||
blend->SetBlendMode(blend_mode);
|
||||
return blend;
|
||||
}
|
||||
|
||||
if (blend_mode <= Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
InputVariant blend = input_textures[0];
|
||||
for (auto in_i = input_textures.begin() + 1; in_i < input_textures.end();
|
||||
in_i++) {
|
||||
auto new_blend = std::make_shared<BlendFilterContents>();
|
||||
new_blend->SetInputTextures({blend, *in_i});
|
||||
new_blend->SetBlendMode(blend_mode);
|
||||
blend = new_blend;
|
||||
}
|
||||
return std::get<std::shared_ptr<FilterContents>>(blend);
|
||||
}
|
||||
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
|
||||
FilterContents::FilterContents() = default;
|
||||
@@ -150,20 +173,106 @@ ISize FilterContents::GetOutputSize() const {
|
||||
******* BlendFilterContents
|
||||
******************************************************************************/
|
||||
|
||||
BlendFilterContents::BlendFilterContents() = default;
|
||||
BlendFilterContents::BlendFilterContents() {
|
||||
SetBlendMode(Entity::BlendMode::kSourceOver);
|
||||
}
|
||||
|
||||
BlendFilterContents::~BlendFilterContents() = default;
|
||||
|
||||
void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
|
||||
blend_mode_ = blend_mode;
|
||||
}
|
||||
using PipelineProc =
|
||||
std::shared_ptr<Pipeline> (ContentContext::*)(ContentContextOptions) const;
|
||||
|
||||
bool BlendFilterContents::RenderFilter(
|
||||
template <typename VS, typename FS>
|
||||
static void AdvancedBlendPass(std::shared_ptr<Texture> input_d,
|
||||
std::shared_ptr<Texture> input_s,
|
||||
std::shared_ptr<const Sampler> sampler,
|
||||
const ContentContext& renderer,
|
||||
RenderPass& pass,
|
||||
Command& cmd) {}
|
||||
|
||||
template <typename VS, typename FS>
|
||||
static bool AdvancedBlend(
|
||||
const std::vector<std::shared_ptr<Texture>>& input_textures,
|
||||
const ContentContext& renderer,
|
||||
RenderPass& pass) const {
|
||||
using VS = TexturePipeline::VertexShader;
|
||||
using FS = TexturePipeline::FragmentShader;
|
||||
RenderPass& pass,
|
||||
PipelineProc pipeline_proc) {
|
||||
if (input_textures.size() < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& host_buffer = pass.GetTransientsBuffer();
|
||||
|
||||
VertexBufferBuilder<typename VS::PerVertexData> vtx_builder;
|
||||
vtx_builder.AddVertices({
|
||||
{Point(0, 0), Point(0, 0)},
|
||||
{Point(1, 0), Point(1, 0)},
|
||||
{Point(1, 1), Point(1, 1)},
|
||||
{Point(0, 0), Point(0, 0)},
|
||||
{Point(1, 1), Point(1, 1)},
|
||||
{Point(0, 1), Point(0, 1)},
|
||||
});
|
||||
auto vtx_buffer = vtx_builder.CreateVertexBuffer(host_buffer);
|
||||
|
||||
typename VS::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
|
||||
|
||||
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
|
||||
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
|
||||
|
||||
auto options = OptionsFromPass(pass);
|
||||
options.blend_mode = Entity::BlendMode::kSource;
|
||||
std::shared_ptr<Pipeline> pipeline =
|
||||
std::invoke(pipeline_proc, renderer, options);
|
||||
|
||||
Command cmd;
|
||||
cmd.label = "Advanced Blend Filter";
|
||||
cmd.BindVertices(vtx_buffer);
|
||||
cmd.pipeline = std::move(pipeline);
|
||||
VS::BindFrameInfo(cmd, uniform_view);
|
||||
|
||||
FS::BindTextureSamplerDst(cmd, input_textures[0], sampler);
|
||||
FS::BindTextureSamplerSrc(cmd, input_textures[1], sampler);
|
||||
pass.AddCommand(cmd);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void BlendFilterContents::SetBlendMode(Entity::BlendMode blend_mode) {
|
||||
if (blend_mode > Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
VALIDATION_LOG << "Invalid blend mode " << static_cast<int>(blend_mode)
|
||||
<< " assigned to BlendFilterContents.";
|
||||
}
|
||||
|
||||
blend_mode_ = blend_mode;
|
||||
|
||||
if (blend_mode > Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
static_assert(Entity::BlendMode::kLastAdvancedBlendMode ==
|
||||
Entity::BlendMode::kScreen);
|
||||
|
||||
switch (blend_mode) {
|
||||
case Entity::BlendMode::kScreen:
|
||||
advanced_blend_proc_ =
|
||||
[](const std::vector<std::shared_ptr<Texture>>& input_textures,
|
||||
const ContentContext& renderer, RenderPass& pass) {
|
||||
PipelineProc p = &ContentContext::GetTextureBlendScreenPipeline;
|
||||
return AdvancedBlend<TextureBlendScreenPipeline::VertexShader,
|
||||
TextureBlendScreenPipeline::FragmentShader>(
|
||||
input_textures, renderer, pass, p);
|
||||
};
|
||||
break;
|
||||
default:
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool BasicBlend(
|
||||
const std::vector<std::shared_ptr<Texture>>& input_textures,
|
||||
const ContentContext& renderer,
|
||||
RenderPass& pass,
|
||||
Entity::BlendMode basic_blend) {
|
||||
using VS = TextureBlendPipeline::VertexShader;
|
||||
using FS = TextureBlendPipeline::FragmentShader;
|
||||
|
||||
auto& host_buffer = pass.GetTransientsBuffer();
|
||||
|
||||
@@ -180,24 +289,63 @@ bool BlendFilterContents::RenderFilter(
|
||||
|
||||
VS::FrameInfo frame_info;
|
||||
frame_info.mvp = Matrix::MakeOrthographic(ISize(1, 1));
|
||||
frame_info.alpha = 1;
|
||||
|
||||
auto uniform_view = host_buffer.EmplaceUniform(frame_info);
|
||||
auto sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler({});
|
||||
|
||||
// Draw the first texture using kSource.
|
||||
|
||||
Command cmd;
|
||||
cmd.label = "Blend Filter";
|
||||
auto options = OptionsFromPass(pass);
|
||||
options.blend_mode = blend_mode_;
|
||||
cmd.pipeline = renderer.GetTexturePipeline(options);
|
||||
cmd.label = "Basic Blend Filter";
|
||||
cmd.BindVertices(vtx_buffer);
|
||||
auto options = OptionsFromPass(pass);
|
||||
options.blend_mode = Entity::BlendMode::kSource;
|
||||
cmd.pipeline = renderer.GetTextureBlendPipeline(options);
|
||||
FS::BindTextureSamplerSrc(cmd, input_textures[0], sampler);
|
||||
VS::BindFrameInfo(cmd, uniform_view);
|
||||
for (const auto& texture : input_textures) {
|
||||
FS::BindTextureSampler(cmd, texture, sampler);
|
||||
pass.AddCommand(cmd);
|
||||
|
||||
if (input_textures.size() < 2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Write subsequent textures using the selected blend mode.
|
||||
|
||||
options.blend_mode = basic_blend;
|
||||
cmd.pipeline = renderer.GetTextureBlendPipeline(options);
|
||||
|
||||
for (auto texture_i = input_textures.begin() + 1;
|
||||
texture_i < input_textures.end(); texture_i++) {
|
||||
FS::BindTextureSamplerSrc(cmd, *texture_i, sampler);
|
||||
pass.AddCommand(cmd);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlendFilterContents::RenderFilter(
|
||||
const std::vector<std::shared_ptr<Texture>>& input_textures,
|
||||
const ContentContext& renderer,
|
||||
RenderPass& pass) const {
|
||||
if (input_textures.empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (input_textures.size() == 1) {
|
||||
// Nothing to blend.
|
||||
return BasicBlend(input_textures, renderer, pass,
|
||||
Entity::BlendMode::kSource);
|
||||
}
|
||||
|
||||
if (blend_mode_ <= Entity::BlendMode::kLastPipelineBlendMode) {
|
||||
return BasicBlend(input_textures, renderer, pass, blend_mode_);
|
||||
}
|
||||
|
||||
if (blend_mode_ <= Entity::BlendMode::kLastAdvancedBlendMode) {
|
||||
return advanced_blend_proc_(input_textures, renderer, pass);
|
||||
}
|
||||
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
namespace impeller {
|
||||
|
||||
class Pipeline;
|
||||
|
||||
/*******************************************************************************
|
||||
******* FilterContents
|
||||
******************************************************************************/
|
||||
@@ -75,6 +77,11 @@ class FilterContents : public Contents {
|
||||
|
||||
class BlendFilterContents : public FilterContents {
|
||||
public:
|
||||
using AdvancedBlendProc = std::function<bool(
|
||||
const std::vector<std::shared_ptr<Texture>>& input_textures,
|
||||
const ContentContext& renderer,
|
||||
RenderPass& pass)>;
|
||||
|
||||
BlendFilterContents();
|
||||
|
||||
~BlendFilterContents() override;
|
||||
@@ -86,7 +93,8 @@ class BlendFilterContents : public FilterContents {
|
||||
const ContentContext& renderer,
|
||||
RenderPass& pass) const override;
|
||||
|
||||
Entity::BlendMode blend_mode_ = Entity::BlendMode::kSourceOver;
|
||||
Entity::BlendMode blend_mode_;
|
||||
AdvancedBlendProc advanced_blend_proc_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(BlendFilterContents);
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <variant>
|
||||
#include "impeller/entity/contents/contents.h"
|
||||
#include "impeller/geometry/color.h"
|
||||
#include "impeller/geometry/matrix.h"
|
||||
@@ -18,10 +19,11 @@ class RenderPass;
|
||||
|
||||
class Entity {
|
||||
public:
|
||||
/// All pipeline blend mode presets assume that both the source (fragment
|
||||
/// output) and destination (first color attachment) have colors with
|
||||
/// premultiplied alpha.
|
||||
/// All blend modes assume that both the source (fragment output) and
|
||||
/// destination (first color attachment) have colors with premultiplied alpha.
|
||||
enum class BlendMode {
|
||||
// The following blend modes are able to be used as pipeline blend modes or
|
||||
// via `BlendFilterContents`.
|
||||
kClear,
|
||||
kSource,
|
||||
kDestination,
|
||||
@@ -36,6 +38,14 @@ class Entity {
|
||||
kXor,
|
||||
kPlus,
|
||||
kModulate,
|
||||
|
||||
// The following blend modes use equations that are not available for
|
||||
// pipelines on most graphics devices without extensions, and so they are
|
||||
// only able to be used via `BlendFilterContents`.
|
||||
kScreen,
|
||||
|
||||
kLastPipelineBlendMode = kModulate,
|
||||
kLastAdvancedBlendMode = kScreen,
|
||||
};
|
||||
|
||||
Entity();
|
||||
|
||||
@@ -657,13 +657,11 @@ TEST_F(EntityTest, Filters) {
|
||||
ASSERT_TRUE(bridge && boston && kalimba);
|
||||
|
||||
auto callback = [&](ContentContext& context, RenderPass& pass) -> bool {
|
||||
// Draws kalimba and overwrites it with boston.
|
||||
auto blend0 = FilterContents::MakeBlend(
|
||||
Entity::BlendMode::kSourceOver, {kalimba, boston});
|
||||
auto blend0 = FilterContents::MakeBlend(Entity::BlendMode::kModulate,
|
||||
{kalimba, boston});
|
||||
|
||||
// Adds bridge*3 to boston.
|
||||
auto blend1 = FilterContents::MakeBlend(
|
||||
Entity::BlendMode::kPlus, {bridge, bridge, blend0, bridge});
|
||||
auto blend1 = FilterContents::MakeBlend(Entity::BlendMode::kScreen,
|
||||
{bridge, blend0, bridge, bridge});
|
||||
|
||||
Entity entity;
|
||||
entity.SetPath(PathBuilder{}.AddRect({100, 100, 300, 300}).TakePath());
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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.
|
||||
|
||||
uniform sampler2D texture_sampler_src;
|
||||
|
||||
in vec2 v_texture_coords;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
frag_color = texture(texture_sampler_src, v_texture_coords);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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.
|
||||
|
||||
uniform FrameInfo {
|
||||
mat4 mvp;
|
||||
} frame_info;
|
||||
|
||||
in vec2 vertices;
|
||||
in vec2 texture_coords;
|
||||
|
||||
out vec2 v_texture_coords;
|
||||
|
||||
void main() {
|
||||
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
|
||||
v_texture_coords = texture_coords;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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.
|
||||
|
||||
uniform sampler2D texture_sampler_dst;
|
||||
uniform sampler2D texture_sampler_src;
|
||||
|
||||
in vec2 v_texture_coords;
|
||||
|
||||
out vec4 frag_color;
|
||||
|
||||
void main() {
|
||||
vec4 dst = texture(texture_sampler_dst, v_texture_coords);
|
||||
vec4 src = texture(texture_sampler_src, v_texture_coords);
|
||||
frag_color = src + dst - src * dst;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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.
|
||||
|
||||
uniform FrameInfo {
|
||||
mat4 mvp;
|
||||
} frame_info;
|
||||
|
||||
in vec2 vertices;
|
||||
in vec2 texture_coords;
|
||||
|
||||
out vec2 v_texture_coords;
|
||||
|
||||
void main() {
|
||||
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
|
||||
v_texture_coords = texture_coords;
|
||||
}
|
||||
Reference in New Issue
Block a user