Skip to content

Commit a0378ad

Browse files
[CP-beta]Eliminate some early returns in uber_sdf.frag to fix broken UberSDF AA on Windows (#190378)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: What is the link to the issue this cherry-pick is addressing? #189827 ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping of production apps (the app crashes on launch). This information is for domain experts and release engineers to understand the consequences of saying yes or no to the cherry pick. This PR fixes anti-aliasing on some draw calls in Impeller Windows, specifically all draws using UberSDF (almost all draws except for paths and some corner case shapes). ### Changelog Description: Explain this cherry pick: * In one line that is accessible to most Flutter developers. * That describes the state prior to the fix. * That includes which platforms are impacted. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples. < Replace with changelog description here > [flutter/189827] When running an app on Windows with Impeller, most raw shape draws had jagged edges. ### Workaround: Is there a workaround for this issue? Yes, downgrading to Skia. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: What are the steps to validate that this fix works? Verified by running a Flutter app on Windows before and after this fix. See the PR description at #190260.
1 parent c7926ac commit a0378ad

2 files changed

Lines changed: 58 additions & 44 deletions

File tree

‎engine/src/flutter/impeller/entity/shaders/uber_sdf.frag‎

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,16 @@ float roundRectPixelSize(vec2 p) {
176176
vec2 corner_center = frag_info.size - radius;
177177
vec2 q = abs(p) - corner_center;
178178

179+
float pixel_size;
179180
// If in the rounded corner arc, blend X and Y pixel sizes along the normal.
180181
if (q.x > 0.0 && q.y > 0.0) {
181-
return length(normalize(q) * device_pixel_size);
182+
pixel_size = length(normalize(q) * device_pixel_size);
183+
} else {
184+
// Otherwise, we are closer to a straight edge. Get pixel size in the
185+
// direction perpendicular to the closer edge.
186+
pixel_size = (q.x > q.y) ? device_pixel_size.x : device_pixel_size.y;
182187
}
183-
184-
// Otherwise, we are closer to a straight edge. Get pixel size in the
185-
// direction perpendicular to the closer edge.
186-
return (q.x > q.y) ? device_pixel_size.x : device_pixel_size.y;
188+
return pixel_size;
187189
}
188190

189191
float pixelSize(float sdf) {
@@ -195,26 +197,30 @@ float pixelSize(float sdf) {
195197
// Returns vec2(sdf, pixel_size).
196198
vec2 filledSDF(vec2 p) {
197199
float sdf;
200+
float pixel_size;
198201
if (frag_info.type < 0.5) { // Circle
199202
sdf = distanceFromCircle(p, frag_info.size.x);
203+
pixel_size = pixelSize(sdf);
200204
} else if (frag_info.type < 1.5) { // Rect
201205
sdf = distanceFromRect(p, frag_info.size);
202206
// Rect has its own separate logic for calculating pixel size.
203-
return vec2(sdf, rectPixelSize(p));
207+
pixel_size = rectPixelSize(p);
204208
} else if (frag_info.type < 2.5) { // Oval
205209
sdf = distanceFromOval(p, frag_info.size);
210+
pixel_size = pixelSize(sdf);
206211
} else if (frag_info.type < 3.5) { // Rounded Rect
207-
// RoundRect has its own separate logic for calculating pixel size.
208212
sdf = distanceFromRoundedRect(p, frag_info.size, frag_info.radii);
209-
return vec2(sdf, roundRectPixelSize(p));
213+
// RoundRect has its own separate logic for calculating pixel size.
214+
pixel_size = roundRectPixelSize(p);
210215
} else { // Symmetric Rounded Superellipse
211216
sdf = distanceFromRoundedSuperellipse(
212217
p, frag_info.superellipse_degree, frag_info.superellipse_semi_axis,
213218
frag_info.radii.xy, frag_info.angle_span, frag_info.circle_center_top,
214219
frag_info.circle_center_right, frag_info.octant_offset_c,
215220
frag_info.superellipse_scale);
221+
pixel_size = pixelSize(sdf);
216222
}
217-
return vec2(sdf, pixelSize(sdf));
223+
return vec2(sdf, pixel_size);
218224
}
219225

220226
// Evaluates the stroked SDF for the shape selected by frag_info.type.
@@ -226,23 +232,31 @@ vec2 strokedSDF(vec2 p) {
226232

227233
float half_stroke = max(frag_info.stroke_width, base_pixel_size) * 0.5;
228234

229-
if (frag_info.type >= 0.5 && frag_info.type < 1.5) { // Rect
230-
231-
if (frag_info.stroke_join < 0.5) { // Miter
232-
float outer = distanceFromRect(p, frag_info.size + half_stroke);
233-
float inner = base_sdf + half_stroke;
234-
float sdf = max(outer, -inner);
235-
return vec2(sdf, pixelSize(sdf));
236-
} else if (frag_info.stroke_join < 1.5) { // Bevel
237-
float outer =
238-
distanceFromChamferRect(p, frag_info.size + half_stroke, half_stroke);
239-
float inner = base_sdf + half_stroke;
240-
float sdf = max(outer, -inner);
241-
return vec2(sdf, pixelSize(sdf));
242-
}
235+
float sdf;
236+
float pixel_size;
237+
if (frag_info.type >= 0.5 && frag_info.type < 1.5 &&
238+
frag_info.stroke_join < 0.5) {
239+
// Rect with Miter join
240+
float outer = distanceFromRect(p, frag_info.size + half_stroke);
241+
float inner = base_sdf + half_stroke;
242+
sdf = max(outer, -inner);
243+
pixel_size = pixelSize(sdf);
244+
} else if (frag_info.type >= 0.5 && frag_info.type < 1.5 &&
245+
frag_info.stroke_join >= 0.5 && frag_info.stroke_join < 1.5) {
246+
// Rect with Bevel join
247+
float outer =
248+
distanceFromChamferRect(p, frag_info.size + half_stroke, half_stroke);
249+
float inner = base_sdf + half_stroke;
250+
sdf = max(outer, -inner);
251+
pixel_size = pixelSize(sdf);
252+
} else {
253+
// All other shapes
254+
vec2 sdf_and_pixel_size =
255+
SDFStroke(base_sdf, base_pixel_size, frag_info.stroke_width);
256+
sdf = sdf_and_pixel_size.x;
257+
pixel_size = sdf_and_pixel_size.y;
243258
}
244-
245-
return SDFStroke(base_sdf, base_pixel_size, frag_info.stroke_width);
259+
return vec2(sdf, pixel_size);
246260
}
247261

248262
// Converts linear coverage alpha to perceptual alpha.

‎engine/src/flutter/impeller/tools/malioc.json‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8901,7 +8901,7 @@
89018901
"shortest_path_cycles": [
89028902
0.53125,
89038903
0.53125,
8904-
0.140625,
8904+
0.15625,
89058905
0.5,
89068906
0.0,
89078907
0.25,
@@ -8912,26 +8912,26 @@
89128912
"arith_fma"
89138913
],
89148914
"total_cycles": [
8915-
13.375,
8916-
13.375,
8917-
4.65625,
8918-
12.625,
8915+
13.625,
8916+
13.625,
8917+
4.53125,
8918+
13.4375,
89198919
0.0,
89208920
0.25,
89218921
0.0
89228922
]
89238923
},
89248924
"stack_spill_bytes": 0,
89258925
"thread_occupancy": 100,
8926-
"uniform_registers_used": 44,
8926+
"uniform_registers_used": 46,
89278927
"work_registers_used": 32
89288928
}
89298929
}
89308930
},
89318931
"Mali-T880": {
89328932
"core": "Mali-T880",
89338933
"filename": "flutter/impeller/entity/gles/uber_sdf.frag.gles",
8934-
"has_uniform_computation": false,
8934+
"has_uniform_computation": true,
89358935
"type": "Fragment",
89368936
"variants": {
89378937
"Main": {
@@ -8941,7 +8941,7 @@
89418941
"arithmetic"
89428942
],
89438943
"longest_path_cycles": [
8944-
45.209999084472656,
8944+
43.88999938964844,
89458945
3.0,
89468946
4.0
89478947
],
@@ -8962,13 +8962,13 @@
89628962
"arithmetic"
89638963
],
89648964
"total_cycles": [
8965-
102.33333587646484,
8965+
105.66666412353516,
89668966
3.0,
8967-
16.0
8967+
24.0
89688968
]
89698969
},
89708970
"thread_occupancy": 100,
8971-
"uniform_registers_used": 5,
8971+
"uniform_registers_used": 7,
89728972
"work_registers_used": 4
89738973
}
89748974
}
@@ -12277,7 +12277,7 @@
1227712277
"uses_late_zs_update": false,
1227812278
"variants": {
1227912279
"Main": {
12280-
"fp16_arithmetic": 36,
12280+
"fp16_arithmetic": 35,
1228112281
"has_stack_spilling": false,
1228212282
"performance": {
1228312283
"longest_path_bound_pipelines": [
@@ -12287,7 +12287,7 @@
1228712287
"longest_path_cycles": [
1228812288
4.0,
1228912289
3.387500047683716,
12290-
1.9249999523162842,
12290+
1.9375,
1229112291
4.0,
1229212292
0.0,
1229312293
0.25,
@@ -12309,7 +12309,7 @@
1230912309
"shortest_path_cycles": [
1231012310
0.5,
1231112311
0.453125,
12312-
0.15625,
12312+
0.171875,
1231312313
0.5,
1231412314
0.0,
1231512315
0.25,
@@ -12320,18 +12320,18 @@
1232012320
"arith_sfu"
1232112321
],
1232212322
"total_cycles": [
12323-
12.625,
12324-
12.125,
12325-
4.800000190734863,
12326-
12.625,
12323+
13.4375,
12324+
12.4375,
12325+
4.875,
12326+
13.4375,
1232712327
0.0,
1232812328
0.25,
1232912329
0.0
1233012330
]
1233112331
},
1233212332
"stack_spill_bytes": 0,
1233312333
"thread_occupancy": 100,
12334-
"uniform_registers_used": 50,
12334+
"uniform_registers_used": 48,
1233512335
"work_registers_used": 32
1233612336
}
1233712337
}

0 commit comments

Comments
 (0)