Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Created September 29, 2026 12:06
Show Gist options
  • Select an option

  • Save PlugFox/d62e54600d89b31e83e3af3597eb0de0 to your computer and use it in GitHub Desktop.

Select an option

Save PlugFox/d62e54600d89b31e83e3af3597eb0de0 to your computer and use it in GitHub Desktop.
flutter/flutter#193388 and #193389: Impeller MetalSDF isAntiAlias and FlutterFragCoord shift reproductions

flutter/flutter#193388 and #193389: reproductions

  • coord.frag encodes FlutterFragCoord() in red and green (one period per 8 px), so every pixel's expected value is known.
  • repro_193388.dart is the reproduction from #193388, verbatim.
  • sweep.dart sweeps the fractional part of the rect's origin and prints the mean offset, in px, between the coordinate the shader saw and the pixel centre. It covers pixels at least 2 px inside a 40×16 rect. The SDF arm is the default isAntiAlias = true. The direct arm is isAntiAlias = false with the #193388 primer. The last column counts pixels where the two arms differ.

Setup: flutter create --platforms=macos --empty repro, put coord.frag in shaders/, and add to pubspec.yaml:

flutter:
  shaders:
    - shaders/coord.frag

Run:

flutter build macos --release -t lib/sweep.dart
build/macos/Build/Products/Release/repro.app/Contents/MacOS/repro

Output

macOS 27.0, Apple M3 Max, Using the Impeller rendering backend (MetalSDF). Identical on stable 3.47.5 (6a19cca564, engine ab59836859) and on master fab991537b (engine f059d397f9). Each was run twice with the same result.

sweep.dart:

origin          SDF path dx, dy   direct path dx, dy   pixels differing
(8.000, 8.000)  +0.007, +0.005    +0.000, +0.000       326
(8.125, 8.000)  +0.132, +0.005    -0.000, +0.000       640
(8.250, 8.000)  +0.257, +0.005    +0.003, +0.000       656
(8.375, 8.000)  +0.382, +0.005    +0.003, +0.000       656
(8.500, 8.000)  -0.155, +0.004    +0.004, +0.000       654
(8.625, 8.000)  -0.364, +0.004    +0.004, +0.000       656
(8.750, 8.000)  -0.243, +0.004    +0.009, +0.002       652
(8.875, 8.000)  -0.121, +0.004    +0.009, +0.002       652
(8.000, 8.250)  +0.007, +0.250    +0.003, +0.002       680
(8.000, 8.500)  +0.007, -0.404    +0.003, +0.002       675
(8.000, 8.750)  +0.008, -0.250    +0.003, +0.002       670
(8.370, 8.610)  +0.374, -0.389    +0.005, +0.002       695

repro_193388.dart, master fab991537b:

rect origin (8.0, 8.0), 40x16
  interior error vs expected: aa=true 8, aa=false 8, aa=false primed 0
  pixels differing: aa=false vs aa=false primed 326, aa=false vs aa=true 0, control aa=true vs aa=true primed 0
rect origin (8.37, 8.61), 40x16
  interior error vs expected: aa=true 16, aa=false 16, aa=false primed 4
  pixels differing: aa=false vs aa=false primed 696, aa=false vs aa=true 0, control aa=true vs aa=true primed 0
us per draw (64 rects in one picture): aa=true 112.8, aa=false 113.0, aa=false primed 7.3

On 3.47.5 the pixel lines are the same. The timing line reads 124.4 / 120.1 / 8.0.

#include <flutter/runtime_effect.glsl>
precision highp float;
out vec4 fragColor;
// Encodes the fragment's own position, so every pixel's expected value is known.
void main() {
vec2 p = FlutterFragCoord().xy;
fragColor = vec4(fract(p.x / 8.0), fract(p.y / 8.0), 0.5, 1.0);
}
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/widgets.dart';
const int kW = 64, kH = 32;
late ui.FragmentProgram program;
Rect rect = Rect.zero;
/// One shader rect. With [prime], a draw that renders nothing (non-empty rect,
/// BlendMode.dst, default anti-alias) is recorded first.
ui.Picture record({required bool antiAlias, bool prime = false, int count = 1}) {
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder);
if (prime) {
canvas.drawRect(const Rect.fromLTWH(0, 0, 1, 1), Paint()..blendMode = BlendMode.dst);
}
for (var i = 0; i < count; i++) {
canvas.drawRect(
rect,
Paint()
..shader = program.fragmentShader()
..isAntiAlias = antiAlias,
);
}
return recorder.endRecording();
}
Future<Uint8List> render(ui.Picture picture) async {
final ui.Image image = await picture.toImage(kW, kH);
final ByteData data = (await image.toByteData())!;
image.dispose();
return data.buffer.asUint8List();
}
/// The shader's value at a pixel centre.
int expected(double coord) => (((coord / 8.0) % 1.0) * 255).round();
/// Worst error against [expected] over pixels at least one pixel inside [rect].
int interiorError(Uint8List px) {
int wrapped(int d) => d < 128 ? d : 256 - d;
var worst = 0;
for (var y = (rect.top + 1).ceil(); y < (rect.bottom - 1).floor(); y++) {
for (var x = (rect.left + 1).ceil(); x < (rect.right - 1).floor(); x++) {
final i = (y * kW + x) * 4;
final int e = [
wrapped((px[i] - expected(x + 0.5)).abs()),
wrapped((px[i + 1] - expected(y + 0.5)).abs()),
].reduce((a, b) => a > b ? a : b);
if (e > worst) worst = e;
}
}
return worst;
}
int differing(Uint8List a, Uint8List b) {
var n = 0;
for (var i = 0; i < a.length; i += 4) {
if (a[i] != b[i] || a[i + 1] != b[i + 1] || a[i + 2] != b[i + 2] || a[i + 3] != b[i + 3]) {
n++;
}
}
return n;
}
Future<double> microsPerDraw({required bool antiAlias, bool prime = false}) async {
const int count = 64;
final times = <int>[];
for (var r = 0; r < 25; r++) {
final ui.Picture p = record(antiAlias: antiAlias, prime: prime, count: count);
final sw = Stopwatch()..start();
final ui.Image image = await p.toImage(kW, kH);
await image.toByteData();
sw.stop();
image.dispose();
p.dispose();
times.add(sw.elapsedMicroseconds);
}
times.sort();
return times[times.length ~/ 2] / count;
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
program = await ui.FragmentProgram.fromAsset('shaders/coord.frag');
for (final Rect r in const <Rect>[
Rect.fromLTWH(8, 8, 40, 16),
Rect.fromLTWH(8.37, 8.61, 40, 16),
]) {
rect = r;
final on = await render(record(antiAlias: true));
final off = await render(record(antiAlias: false));
final offPrimed = await render(record(antiAlias: false, prime: true));
final onPrimed = await render(record(antiAlias: true, prime: true));
print('rect origin (${r.left}, ${r.top}), 40x16');
print(' interior error vs expected: aa=true ${interiorError(on)}, '
'aa=false ${interiorError(off)}, aa=false primed ${interiorError(offPrimed)}');
print(' pixels differing: aa=false vs aa=false primed ${differing(off, offPrimed)}, '
'aa=false vs aa=true ${differing(off, on)}, '
'control aa=true vs aa=true primed ${differing(on, onPrimed)}');
}
rect = const Rect.fromLTWH(8, 8, 40, 16);
print('us per draw (64 rects in one picture): '
'aa=true ${(await microsPerDraw(antiAlias: true)).toStringAsFixed(1)}, '
'aa=false ${(await microsPerDraw(antiAlias: false)).toStringAsFixed(1)}, '
'aa=false primed ${(await microsPerDraw(antiAlias: false, prime: true)).toStringAsFixed(1)}');
exit(0);
}
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/widgets.dart';
const int kW = 64, kH = 32;
late ui.FragmentProgram program;
/// [sdf]: isAntiAlias = true (the default), which takes the UberSDF path on
/// MetalSDF. Otherwise the direct path: isAntiAlias = false, primed.
ui.Picture record(Rect rect, {required bool sdf}) {
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder);
if (!sdf) {
// Primer from #193388, so that isAntiAlias = false reaches Impeller.
canvas.drawRect(const Rect.fromLTWH(0, 0, 1, 1), Paint()..blendMode = BlendMode.dst);
}
canvas.drawRect(
rect,
Paint()
..shader = program.fragmentShader()
..isAntiAlias = sdf,
);
return recorder.endRecording();
}
Future<Uint8List> render(ui.Picture picture) async {
final ui.Image image = await picture.toImage(kW, kH);
final ByteData data = (await image.toByteData())!;
image.dispose();
return data.buffer.asUint8List();
}
/// Mean signed offset, in pixels, of the coordinate the shader saw against the
/// pixel centre, per axis, over pixels at least two pixels inside [rect].
/// One code value is 1/32 px.
(double, double, int) offset(Uint8List px, Rect rect) {
double signed(int got, double coord) {
final double want = ((coord / 8.0) % 1.0) * 255;
var d = got - want;
if (d > 127.5) d -= 255;
if (d < -127.5) d += 255;
return d / 32.0;
}
var sx = 0.0, sy = 0.0, n = 0;
for (var y = (rect.top + 2).ceil(); y < (rect.bottom - 2).floor(); y++) {
for (var x = (rect.left + 2).ceil(); x < (rect.right - 2).floor(); x++) {
final i = (y * kW + x) * 4;
sx += signed(px[i], x + 0.5);
sy += signed(px[i + 1], y + 0.5);
n++;
}
}
return (sx / n, sy / n, n);
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
program = await ui.FragmentProgram.fromAsset('shaders/coord.frag');
String f(double v) => (v >= 0 ? '+' : '') + v.toStringAsFixed(3);
print('origin SDF path dx, dy direct path dx, dy pixels differing');
for (final (double fx, double fy) in const <(double, double)>[
(0, 0), (0.125, 0), (0.25, 0), (0.375, 0), (0.5, 0), (0.625, 0), (0.75, 0), (0.875, 0),
(0, 0.25), (0, 0.5), (0, 0.75), (0.37, 0.61),
]) {
final rect = Rect.fromLTWH(8 + fx, 8 + fy, 40, 16);
final Uint8List a = await render(record(rect, sdf: true));
final Uint8List b = await render(record(rect, sdf: false));
var diff = 0;
for (var i = 0; i < a.length; i += 4) {
if (a[i] != b[i] || a[i + 1] != b[i + 1]) diff++;
}
final (sx, sy, _) = offset(a, rect);
final (dx, dy, _) = offset(b, rect);
print('(${(8 + fx).toStringAsFixed(3)}, ${(8 + fy).toStringAsFixed(3)}) '
'${f(sx)}, ${f(sy)} ${f(dx)}, ${f(dy)} $diff');
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment