Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b449bab

Browse files
[Impeller] take advantage of native decal sampling, blend cleanups (#40839)
[Impeller] take advantage of native decal sampling, blend cleanups
1 parent b6756c5 commit b449bab

File tree

7 files changed

+293
-297
lines changed

7 files changed

+293
-297
lines changed

impeller/entity/contents/filters/blend_filter_contents.cc

+17-8
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,13 @@ static std::optional<Entity> AdvancedBlend(
118118
typename FS::BlendInfo blend_info;
119119
typename VS::FrameInfo frame_info;
120120

121+
auto dst_sampler_descriptor = dst_snapshot->sampler_descriptor;
122+
if (renderer.GetDeviceCapabilities().SupportsDecalTileMode()) {
123+
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
124+
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
125+
}
121126
auto dst_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
122-
dst_snapshot->sampler_descriptor);
127+
dst_sampler_descriptor);
123128
FS::BindTextureSamplerDst(cmd, dst_snapshot->texture, dst_sampler);
124129
frame_info.dst_y_coord_scale = dst_snapshot->texture->GetYCoordScale();
125130
blend_info.dst_input_alpha = absorb_opacity ? dst_snapshot->opacity : 1.0;
@@ -132,8 +137,13 @@ static std::optional<Entity> AdvancedBlend(
132137
// binding.
133138
FS::BindTextureSamplerSrc(cmd, dst_snapshot->texture, dst_sampler);
134139
} else {
140+
auto src_sampler_descriptor = src_snapshot->sampler_descriptor;
141+
if (renderer.GetDeviceCapabilities().SupportsDecalTileMode()) {
142+
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
143+
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
144+
}
135145
auto src_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
136-
src_snapshot->sampler_descriptor);
146+
src_sampler_descriptor);
137147
blend_info.color_factor = 0;
138148
FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, src_sampler);
139149
frame_info.src_y_coord_scale = src_snapshot->texture->GetYCoordScale();
@@ -220,9 +230,10 @@ static std::optional<Entity> PipelineBlend(
220230
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *
221231
Matrix::MakeTranslation(-coverage.origin) *
222232
input->transform;
223-
FS::FragInfo frag_info;
224-
frag_info.texture_sampler_y_coord_scale =
233+
frame_info.texture_sampler_y_coord_scale =
225234
input->texture->GetYCoordScale();
235+
236+
FS::FragInfo frag_info;
226237
frag_info.input_alpha = absorb_opacity ? input->opacity : 1.0;
227238
FS::BindFragInfo(cmd, host_buffer.EmplaceUniform(frag_info));
228239
VS::BindFrameInfo(cmd, host_buffer.EmplaceUniform(frame_info));
@@ -257,10 +268,8 @@ static std::optional<Entity> PipelineBlend(
257268

258269
if (foreground_color.has_value()) {
259270
auto contents = std::make_shared<SolidColorContents>();
260-
contents->SetGeometry(Geometry::MakeFillPath(
261-
PathBuilder{}
262-
.AddRect(Rect::MakeSize(pass.GetRenderTargetSize()))
263-
.TakePath()));
271+
contents->SetGeometry(
272+
Geometry::MakeRect(Rect::MakeSize(pass.GetRenderTargetSize())));
264273
contents->SetColor(foreground_color.value());
265274

266275
Entity foreground_entity;

impeller/entity/contents/framebuffer_blend_contents.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,15 @@ bool FramebufferBlendContents::Render(const ContentContext& renderer,
128128

129129
VS::FrameInfo frame_info;
130130

131+
auto src_sampler_descriptor = src_snapshot->sampler_descriptor;
132+
if (!renderer.GetDeviceCapabilities().SupportsDecalTileMode()) {
133+
// No known devices that support framebuffer fetch but not decal tile mode.
134+
return false;
135+
}
136+
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
137+
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
131138
auto src_sampler = renderer.GetContext()->GetSamplerLibrary()->GetSampler(
132-
src_snapshot->sampler_descriptor);
139+
src_sampler_descriptor);
133140
FS::BindTextureSamplerSrc(cmd, src_snapshot->texture, src_sampler);
134141

135142
frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) *

impeller/entity/shaders/blending/advanced_blend.glsl

+18-9
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,28 @@ in vec2 v_src_texture_coords;
2222

2323
out vec4 frag_color;
2424

25+
vec4 Sample(sampler2D texture_sampler, vec2 texture_coords) {
26+
// gles 2.0 is the only backend without native decal support.
27+
#ifdef IMPELLER_TARGET_OPENGLES
28+
return IPSampleDecal(texture_sampler, texture_coords);
29+
#else
30+
return texture(texture_sampler, texture_coords);
31+
#endif
32+
}
33+
2534
void main() {
26-
vec4 dst_sample = IPSampleDecal(texture_sampler_dst, // sampler
27-
v_dst_texture_coords // texture coordinates
28-
) *
35+
vec4 dst_sample = Sample(texture_sampler_dst, // sampler
36+
v_dst_texture_coords // texture coordinates
37+
) *
2938
blend_info.dst_input_alpha;
3039

3140
vec4 dst = IPUnpremultiply(dst_sample);
32-
vec4 src = blend_info.color_factor > 0
33-
? blend_info.color
34-
: IPUnpremultiply(IPSampleDecal(
35-
texture_sampler_src, // sampler
36-
v_src_texture_coords // texture coordinates
37-
));
41+
vec4 src =
42+
blend_info.color_factor > 0
43+
? blend_info.color
44+
: IPUnpremultiply(Sample(texture_sampler_src, // sampler
45+
v_src_texture_coords // texture coordinates
46+
));
3847

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

impeller/entity/shaders/blending/blend.frag

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
uniform sampler2D texture_sampler_src;
99

1010
uniform FragInfo {
11-
float texture_sampler_y_coord_scale;
1211
float input_alpha;
1312
}
1413
frag_info;
@@ -18,7 +17,6 @@ in vec2 v_texture_coords;
1817
out vec4 frag_color;
1918

2019
void main() {
21-
frag_color = IPSample(texture_sampler_src, v_texture_coords,
22-
frag_info.texture_sampler_y_coord_scale) *
23-
frag_info.input_alpha;
20+
frag_color =
21+
texture(texture_sampler_src, v_texture_coords) * frag_info.input_alpha;
2422
}

impeller/entity/shaders/blending/blend.vert

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <impeller/texture.glsl>
56
#include <impeller/types.glsl>
67

78
uniform FrameInfo {
89
mat4 mvp;
10+
float texture_sampler_y_coord_scale;
911
}
1012
frame_info;
1113

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

1719
void main() {
1820
gl_Position = frame_info.mvp * vec4(vertices, 0.0, 1.0);
19-
v_texture_coords = texture_coords;
21+
v_texture_coords =
22+
IPRemapCoords(texture_coords, frame_info.texture_sampler_y_coord_scale);
2023
}

impeller/entity/shaders/blending/ios/framebuffer_blend.glsl

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ void main() {
3434
vec4 dst_sample = ReadDestination();
3535
vec4 dst = IPUnpremultiply(dst_sample);
3636
vec4 src =
37-
IPUnpremultiply(IPSampleDecal(texture_sampler_src, // sampler
38-
v_src_texture_coords // texture coordinates
39-
));
37+
IPUnpremultiply(texture(texture_sampler_src, // sampler
38+
v_src_texture_coords // texture coordinates
39+
));
4040

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

0 commit comments

Comments
 (0)