forked from flutter/flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Impeller] Add rounded rect SDF blur (flutter#35084)
- Loading branch information
Showing
9 changed files
with
293 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// 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/entity/contents/rrect_shadow_contents.h" | ||
#include <optional> | ||
|
||
#include "impeller/entity/contents/content_context.h" | ||
#include "impeller/entity/entity.h" | ||
#include "impeller/geometry/path.h" | ||
#include "impeller/geometry/path_builder.h" | ||
#include "impeller/renderer/render_pass.h" | ||
#include "impeller/renderer/vertex_buffer_builder.h" | ||
#include "impeller/tessellator/tessellator.h" | ||
|
||
namespace impeller { | ||
|
||
RRectShadowContents::RRectShadowContents() = default; | ||
|
||
RRectShadowContents::~RRectShadowContents() = default; | ||
|
||
void RRectShadowContents::SetRRect(std::optional<Rect> rect, | ||
Scalar corner_radius) { | ||
rect_ = rect; | ||
corner_radius_ = corner_radius; | ||
} | ||
|
||
void RRectShadowContents::SetSigma(Sigma sigma) { | ||
sigma_ = sigma; | ||
} | ||
|
||
void RRectShadowContents::SetColor(Color color) { | ||
color_ = color.Premultiply(); | ||
} | ||
|
||
std::optional<Rect> RRectShadowContents::GetCoverage( | ||
const Entity& entity) const { | ||
if (!rect_.has_value()) { | ||
return std::nullopt; | ||
} | ||
|
||
Scalar radius = Radius{sigma_}.radius; | ||
|
||
auto ltrb = rect_->GetLTRB(); | ||
Rect bounds = Rect::MakeLTRB(ltrb[0] - radius, ltrb[1] - radius, | ||
ltrb[2] + radius, ltrb[3] + radius); | ||
return bounds.TransformBounds(entity.GetTransformation()); | ||
}; | ||
|
||
bool RRectShadowContents::Render(const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const { | ||
if (!rect_.has_value()) { | ||
return true; | ||
} | ||
|
||
using VS = RRectBlurPipeline::VertexShader; | ||
using FS = RRectBlurPipeline::FragmentShader; | ||
|
||
VertexBufferBuilder<VS::PerVertexData> vtx_builder; | ||
|
||
auto blur_radius = Radius{sigma_}.radius; | ||
auto positive_rect = rect_->GetPositive(); | ||
{ | ||
auto left = -blur_radius; | ||
auto top = -blur_radius; | ||
auto right = positive_rect.size.width + blur_radius; | ||
auto bottom = positive_rect.size.height + blur_radius; | ||
|
||
vtx_builder.AddVertices({ | ||
{Point(left, top)}, | ||
{Point(right, top)}, | ||
{Point(left, bottom)}, | ||
{Point(left, bottom)}, | ||
{Point(right, top)}, | ||
{Point(right, bottom)}, | ||
}); | ||
} | ||
|
||
Command cmd; | ||
cmd.label = "RRect Shadow"; | ||
cmd.pipeline = | ||
renderer.GetRRectBlurPipeline(OptionsFromPassAndEntity(pass, entity)); | ||
cmd.stencil_reference = entity.GetStencilDepth(); | ||
|
||
cmd.primitive_type = PrimitiveType::kTriangle; | ||
cmd.BindVertices(vtx_builder.CreateVertexBuffer(pass.GetTransientsBuffer())); | ||
|
||
VS::VertInfo vert_info; | ||
vert_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * | ||
entity.GetTransformation() * | ||
Matrix::MakeTranslation({positive_rect.origin}); | ||
VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(vert_info)); | ||
|
||
FS::FragInfo frag_info; | ||
frag_info.color = color_; | ||
frag_info.blur_radius = blur_radius; | ||
frag_info.rect_size = Point(positive_rect.size); | ||
frag_info.corner_radius = | ||
std::min(corner_radius_, std::min(positive_rect.size.width / 2.0f, | ||
positive_rect.size.height / 2.0f)); | ||
FS::BindFragInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frag_info)); | ||
|
||
if (!pass.AddCommand(std::move(cmd))) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "impeller/entity/contents/contents.h" | ||
#include "impeller/entity/contents/filters/filter_contents.h" | ||
#include "impeller/geometry/color.h" | ||
|
||
namespace impeller { | ||
|
||
class Path; | ||
class HostBuffer; | ||
struct VertexBuffer; | ||
|
||
class RRectShadowContents final : public Contents { | ||
public: | ||
RRectShadowContents(); | ||
|
||
~RRectShadowContents() override; | ||
|
||
void SetRRect(std::optional<Rect> rect, Scalar corner_radius = 0); | ||
|
||
void SetSigma(Sigma sigma); | ||
|
||
void SetColor(Color color); | ||
|
||
// |Contents| | ||
std::optional<Rect> GetCoverage(const Entity& entity) const override; | ||
|
||
// |Contents| | ||
bool Render(const ContentContext& renderer, | ||
const Entity& entity, | ||
RenderPass& pass) const override; | ||
|
||
private: | ||
std::optional<Rect> rect_; | ||
Scalar corner_radius_; | ||
Sigma sigma_; | ||
|
||
Color color_; | ||
|
||
FML_DISALLOW_COPY_AND_ASSIGN(RRectShadowContents); | ||
}; | ||
|
||
} // namespace impeller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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 FragInfo { | ||
vec4 color; | ||
float blur_radius; | ||
vec2 rect_size; | ||
float corner_radius; | ||
} | ||
frag_info; | ||
|
||
in vec2 v_position; | ||
|
||
out vec4 frag_color; | ||
|
||
// Simple logistic sigmoid with a domain of [-1, 1] and range of [0, 1]. | ||
float Sigmoid(float x) { | ||
return 1.03731472073 / (1 + exp(-4 * x)) - 0.0186573603638; | ||
} | ||
|
||
float RRectDistance(vec2 sample_position, vec2 rect_size, float corner_radius) { | ||
vec2 space = abs(sample_position) - rect_size + corner_radius; | ||
return length(max(space, 0.0)) + min(max(space.x, space.y), 0.0) - | ||
corner_radius; | ||
} | ||
|
||
void main() { | ||
vec2 center = v_position - frag_info.rect_size / 2.0; | ||
float dist = | ||
RRectDistance(center, frag_info.rect_size / 2.0, frag_info.corner_radius); | ||
float shadow_mask = Sigmoid(-dist / frag_info.blur_radius); | ||
frag_color = frag_info.color * shadow_mask; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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 VertInfo { | ||
mat4 mvp; | ||
} | ||
vert_info; | ||
|
||
in vec2 position; | ||
|
||
out vec2 v_position; | ||
|
||
void main() { | ||
gl_Position = vert_info.mvp * vec4(position, 0.0, 1.0); | ||
// The fragment stage uses local coordinates to compute the blur. | ||
v_position = position; | ||
} |