Skip to content

[impeller] Inconsistent size and result on fragment shaders #132802

@ebraminio

Description

@ebraminio

Steps to reproduce

Run the code sample in iOS vs macOS, or a regular iOS run vs flutter run --no-enable-impeller

Expected results

Note the vertical red bar position, the shader generated gradient should have the same size in both impeller vs non-impeller.

Actual results

Different results in iOS impeller vs non-impeller (also couldn't run it at all in macOS/Android with flutter run --enable-impeller to test macOS version of impeller for this issue which is a separate bug also)

Code sample

Code sample
import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: const ShaderPage(),
    );
  }
}

class ShaderPage extends StatefulWidget {
  const ShaderPage({super.key});

  @override
  State<ShaderPage> createState() => _ShaderPageState();
}

class _ShaderPageState extends State<ShaderPage> {
  bool _initialized = false;
  FragmentProgram? _shader;

  void _initializeShader() async {
    if (_initialized == true) return;
    _initialized = true;
    final shader = await FragmentProgram.fromAsset('shaders/shader.frag');
    setState(() => _shader = shader);
  }

  @override
  Widget build(BuildContext context) {
    _initializeShader();
    final shader = _shader;
    return Scaffold(
      appBar: AppBar(title: const Text('Test')),
      body: SingleChildScrollView(
        child: Column(
          children: [
            if (shader != null)
              CustomPaint(
                painter: _ScenePainter(shader),
                child: Padding(
                  padding: const EdgeInsets.only(top: 8, bottom: 8),
                  child: Stack(
                    children: [
                      Container(height: 200),
                      Container(height: 200, width: 10, color: Colors.red),
                    ],
                  ),
                ),
              ),
            Container(height: 1800),
          ],
        ),
      ),
    );
  }
}

class _ScenePainter extends CustomPainter {
  final FragmentProgram program;

  _ScenePainter(this.program);

  @override
  void paint(Canvas canvas, Size size) {
    final shader = program.fragmentShader();
    shader.setFloat(0, size.width);
    shader.setFloat(1, size.height);
    canvas.drawPaint(Paint()..shader = shader);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}

And save the following in shaders/shader.frag

#include <flutter/runtime_effect.glsl>

uniform vec2 uSize;
out vec4 fragColor;

void main() {
    float v = FlutterFragCoord().y / uSize.y;
    fragColor = vec4(v, v, v, 1);
}

And add the following to pubspec.yaml,

flutter:
  [...]
  shaders:
    - shaders/shader.frag

Screenshots or Video

Screenshots / Video demonstration

regular macOS run vs regular [impeller] iOS run:
image

regular macOS run vs no-impeller iOS run (flutter run --no-enable-impeller):
image

Logs

No response

Flutter Doctor output

Doctor output
flutter doctor -v              
[✓] Flutter (Channel stable, 3.13.0, on macOS 13.4.1 22F770820d darwin-arm64,
    locale en-US)
    • Flutter version 3.13.0 on channel stable at /Users/ebrahim/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision efbf63d9c6 (2 days ago), 2023-08-15 21:05:06 -0500
    • Engine revision 1ac611c64e
    • Dart version 3.1.0
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/ebrahim/Library/Android/sdk
    • Platform android-34, build-tools 33.0.2
    • Java binary at: /Applications/Android
      Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build
      17.0.6+0-17.0.6b829.9-10027231)

[✓] VS Code (version 1.81.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.70.0

[✓] Connected device (3 available)
    • iPad Pro (12.9-inch) (6th generation) (mobile) •
      14D51279-7D96-4D91-B7FA-5F3BCA78F1C3 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)
    • macOS (desktop)                                • macos
      • darwin-arm64   • macOS 13.4.1 22F770820d darwin-arm64
    • Chrome (web)                                   • chrome
      • web-javascript • Google Chrome 114.0.5735.198

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work liste: impellerImpeller rendering backend issues and features requeststeam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions