Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Impeller] take advantage of native decal sampling, blend cleanups #40839

Merged
merged 2 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 17 additions & 8 deletions impeller/entity/contents/filters/blend_filter_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ static std::optional<Entity> AdvancedBlend(
typename FS::BlendInfo blend_info;
typename VS::FrameInfo frame_info;

auto dst_sampler_descriptor = dst_snapshot->sampler_descriptor;
if (renderer.GetDeviceCapabilities().SupportsDecalTileMode()) {
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
dst_snapshot->sampler_descriptor);
dst_sampler_descriptor);
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, dst_sampler);
frame_info.dst_y_coord_scale = dst_snapshot->texture->GetYCoordScale();
blend_info.dst_input_alpha = absorb_opacity ? dst_snapshot->opacity : 1.0;
Expand All @@ -132,8 +137,13 @@ static std::optional<Entity> AdvancedBlend(
// binding.
FS::BindTextureSamplerSrc(cmd, dst_snapshot->texture, dst_sampler);
} else {
auto src_sampler_descriptor = src_snapshot->sampler_descriptor;
if (renderer.GetDeviceCapabilities().SupportsDecalTileMode()) {
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
}
auto src_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
src_snapshot->sampler_descriptor);
src_sampler_descriptor);
blend_info.color_factor = 0;
FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, src_sampler);
frame_info.src_y_coord_scale = src_snapshot->texture->GetYCoordScale();
Expand Down Expand Up @@ -220,9 +230,10 @@ static std::optional<Entity> PipelineBlend(
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
Matrix::MakeTranslation(-coverage.origin) *
input->transform;
FS::FragInfo frag_info;
frag_info.texture_sampler_y_coord_scale =
frame_info.texture_sampler_y_coord_scale =
input->texture->GetYCoordScale();

FS::FragInfo frag_info;
frag_info.input_alpha = absorb_opacity ? input->opacity : 1.0;
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
Expand Down Expand Up @@ -257,10 +268,8 @@ static std::optional<Entity> PipelineBlend(

if (foreground_color.has_value()) {
auto contents = std::make_shared<SolidColorContents>();
contents->SetGeometry(Geometry::MakeFillPath(
PathBuilder{}
.AddRect(Rect::MakeSize(pass.GetRenderTargetSize()))
.TakePath()));
contents->SetGeometry(
Geometry::MakeRect(Rect::MakeSize(pass.GetRenderTargetSize())));
contents->SetColor(foreground_color.value());

Entity foreground_entity;
Expand Down
9 changes: 8 additions & 1 deletion impeller/entity/contents/framebuffer_blend_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,15 @@ bool FramebufferBlendContents::Render(const ContentContext& renderer,

VS::FrameInfo frame_info;

auto src_sampler_descriptor = src_snapshot->sampler_descriptor;
if (!renderer.GetDeviceCapabilities().SupportsDecalTileMode()) {
// No known devices that support framebuffer fetch but not decal tile mode.
return false;
}
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
auto src_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
src_snapshot->sampler_descriptor);
src_sampler_descriptor);
FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, src_sampler);

frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
Expand Down
27 changes: 18 additions & 9 deletions impeller/entity/shaders/blending/advanced_blend.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,28 @@ in vec2 v_src_texture_coords;

out vec4 frag_color;

vec4 Sample(sampler2D texture_sampler, vec2 texture_coords) {
// gles 2.0 is the only backend without native decal support.
#ifdef IMPELLER_TARGET_OPENGLES
return IPSampleDecal(texture_sampler, texture_coords);
#else
return texture(texture_sampler, texture_coords);
#endif
}

void main() {
vec4 dst_sample = IPSampleDecal(texture_sampler_dst, // sampler
v_dst_texture_coords // texture coordinates
) *
vec4 dst_sample = Sample(texture_sampler_dst, // sampler
v_dst_texture_coords // texture coordinates
) *
blend_info.dst_input_alpha;

vec4 dst = IPUnpremultiply(dst_sample);
vec4 src = blend_info.color_factor > 0
? blend_info.color
: IPUnpremultiply(IPSampleDecal(
texture_sampler_src, // sampler
v_src_texture_coords // texture coordinates
));
vec4 src =
blend_info.color_factor > 0
? blend_info.color
: IPUnpremultiply(Sample(texture_sampler_src, // sampler
v_src_texture_coords // texture coordinates
));

vec4 blended = vec4(Blend(dst.rgb, src.rgb), 1) * dst.a;

Expand Down
6 changes: 2 additions & 4 deletions impeller/entity/shaders/blending/blend.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
uniform sampler2D texture_sampler_src;

uniform FragInfo {
float texture_sampler_y_coord_scale;
float input_alpha;
}
frag_info;
Expand All @@ -18,7 +17,6 @@ in vec2 v_texture_coords;
out vec4 frag_color;

void main() {
frag_color = IPSample(texture_sampler_src, v_texture_coords,
frag_info.texture_sampler_y_coord_scale) *
frag_info.input_alpha;
frag_color =
texture(texture_sampler_src, v_texture_coords) * frag_info.input_alpha;
}
5 changes: 4 additions & 1 deletion impeller/entity/shaders/blending/blend.vert
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <impeller/texture.glsl>
#include <impeller/types.glsl>

uniform FrameInfo {
mat4 mvp;
float texture_sampler_y_coord_scale;
}
frame_info;

Expand All @@ -16,5 +18,6 @@ out vec2 v_texture_coords;

void main() {
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
v_texture_coords = texture_coords;
v_texture_coords =
IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale);
}
6 changes: 3 additions & 3 deletions impeller/entity/shaders/blending/ios/framebuffer_blend.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ void main() {
vec4 dst_sample = ReadDestination();
vec4 dst = IPUnpremultiply(dst_sample);
vec4 src =
IPUnpremultiply(IPSampleDecal(texture_sampler_src, // sampler
v_src_texture_coords // texture coordinates
));
IPUnpremultiply(texture(texture_sampler_src, // sampler
v_src_texture_coords // texture coordinates
));

vec4 blended = vec4(Blend(dst.rgb, src.rgb), 1) * dst.a;

Expand Down