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

[Impeller] read from framebuffer for advanced blends on iOS. #39567

Merged
merged 14 commits into from
Feb 14, 2023

Conversation

jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented Feb 12, 2023

Work towards flutter/flutter#120223 for iOS. This dramatically improves performance of the pull quote on wonderous by reducing the number of subpasses and removing the required full screen fill. from 8ms down to 2-3 ms, vs 1.5-2ms for Skia. So pretty fast, and with less memory too

Note that this excludes the shaders from non iOS platforms but does not exclude the normal advanced blend shaders from iOS. Since we're not doing this in filter contents I've decided to punt on whether or not we should refactor that to take advantage of this - speciifically for drawVertices/drawAtlas there would be more followup work

I also left out support for a foreground color, but we should probably add that back and find some way to detect offscreen solid color contents. Or just always convert SolidColorContents into a 1x1 texture of that solid color when calling RenderToSnapshot.

image (11)

Here is a fun test app for this change:

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: Scaffold(body: Example())));
}

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

  @override
  State<Example> createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  var hellos = [
    'Hello',
  ];

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
          Container(color: Colors.grey),
          GridView.count(crossAxisCount: 8, children: [
            for (int i = 0; i < 100; i++)
              Text('  ${hellos[i % hellos.length]}  ', style: TextStyle(fontSize: 40))
          ]),
          GridView.count(crossAxisCount: 8, children: [
            for (int i = 0; i < 200; i++)
              CustomPaint(
                painter: FooPainter(i),
                size: Size(50, 50),
              ),
          ]),
      ]
    );
  }
}

class FooPainter extends CustomPainter {
  FooPainter(this.i);

  final int i;

  var colors = [
    Colors.red,
    Colors.green,
    Colors.blue,
    Colors.yellow,
    Colors.purple,
    Colors.orange,
    Colors.lime,
  ];

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawRect(Rect.fromLTWH(0, 0, size.width, size.height), Paint()..color = colors[i % colors.length] .. blendMode = BlendMode.screen);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

@jonahwilliams
Copy link
Member Author

Here is the colordodge shader compiled (abbreviated):

#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct BlendInfo
{
    float dst_input_alpha;
    float src_y_coord_scale;
    float color_factor;
    float4 color;
};

struct framebuffer_blend_colordodge_fragment_main_out
{
    float4 frag_color [[color(0)]];
};

struct framebuffer_blend_colordodge_fragment_main_in
{
    float2 v_src_texture_coords [[user(locn0)]];
};

fragment framebuffer_blend_colordodge_fragment_main_out framebuffer_blend_colordodge_fragment_main(framebuffer_blend_colordodge_fragment_main_in in [[stage_in]], constant BlendInfo& blend_info [[buffer(0)]], float4 uSub [[color(0)]], texture2d<float> texture_sampler_src [[texture(0)]], sampler texture_sampler_srcSmplr [[sampler(0)]])
{
    framebuffer_blend_colordodge_fragment_main_out out = {};
    float4 _380 = uSub;
    ...
    return out;
}

Of course this is resulting in solid black output. note I did leave some deb

@jonahwilliams
Copy link
Member Author

This now works on simple examples but fails on wonderous. Probably some coverage/transform/stencil issue I haven't shaken out yet.

Also note that I made an effort to exclude the framebuffer shaders on non ios platforms but did not make an effort to remove the regular advanced blends. TBD if we want to do that.

@@ -83,6 +83,7 @@ def main():
'-ffast-math',
# Record symbols in a separate *.metallibsym file.
'-frecord-sources=flat',
'-g',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: remember to remove

@jonahwilliams jonahwilliams requested a review from bdero February 13, 2023 23:35
@jonahwilliams
Copy link
Member Author

@bdero WDUT about the approximate approach here?

(Also checking if you can spot the bug before I do 🦅 )

@jonahwilliams
Copy link
Member Author

frame debugger has the geometry in the right place, and it worked for simpler examples. i made the coverage a huge rect. 🤔

@bdero
Copy link
Member

bdero commented Feb 14, 2023

Overall approach LGTM! 👍 I glanced through and didn't spot the geometry bug. Is stuff getting partially cut off or getting culled away completely? Do the UV values reasonable in the frame debugger?

@jonahwilliams
Copy link
Member Author

The draw call itself isn't getting culled - I can see the geometry and uvs look reasonable. Actually changing it to output a solid color results in no differences in wonderous so I don't think its a UV issue anyway

image

Not sure what else to look at. Very simple examples seem to work fine.

@jonahwilliams
Copy link
Member Author

Wait, am I actually adding this correctly or am I actually just replacing the last entity.

@jonahwilliams
Copy link
Member Author

Well I guess yes, since I call renderToSnapshot on the contents I replace.

@jonahwilliams
Copy link
Member Author

ahh, just forgot to set the stencil 🤦

@jonahwilliams jonahwilliams marked this pull request as ready for review February 14, 2023 02:02
@jonahwilliams jonahwilliams changed the title [impeller] read from framebuffer for advanced blends [impeller] read from framebuffer for advanced blends on iOS Feb 14, 2023
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@jonahwilliams jonahwilliams changed the title [impeller] read from framebuffer for advanced blends on iOS [impeller] read from framebuffer for advanced blends on iOS. Feb 14, 2023
@jonahwilliams
Copy link
Member Author

Looks like I also need to disable this on iOS simulators.

@jonahwilliams jonahwilliams force-pushed the framebuffer_sample branch 2 times, most recently from b72f058 to 0c4c6d6 Compare February 14, 2023 04:26
@chinmaygarde chinmaygarde changed the title [impeller] read from framebuffer for advanced blends on iOS. [Impeller] read from framebuffer for advanced blends on iOS. Feb 14, 2023
Copy link
Member

@bdero bdero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -437,6 +506,83 @@ class ContentContext {
ContentContextOptions opts) const {
return GetPipeline(blend_softlight_pipelines_, opts);
}
#if FML_OS_PHYSICAL_IOS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So reading from the framebuffer wouldn't happen to work on Apple silicon macs, would it? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be, but from the errors I hit it sounds like it needs a substantially higher MSL version:

https://github.com/KhronosGroup/SPIRV-Cross/blob/4e2fdb25671c742a9fbe93a6034eb1542244c7e1/spirv_msl.cpp#L13082

@@ -35,6 +35,7 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
// Metal to AIR must be updated as well.
sl_options.msl_version =
spirv_cross::CompilerMSL::Options::make_msl_version(1, 2);
sl_options.use_framebuffer_fetch_subpasses = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, well that turned out simpler than I was expecting!

@jonahwilliams jonahwilliams added the autosubmit Merge PR when tree becomes green via auto submit App label Feb 14, 2023
@auto-submit auto-submit bot merged commit 9bd50b7 into flutter:main Feb 14, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Feb 15, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Feb 15, 2023
…120761)

* ab2dec516 Roll Skia from 8de7f68a3661 to 67a8177742e6 (4 revisions) (flutter/engine#39623)

* a7ec1e50f Remove libpng dependency in the APNG decoder (flutter/engine#39622)

* cb5393336 Add more info to malioc_diff.py output (flutter/engine#39625)

* 60c199501 Roll Dart SDK from ea59504416a8 to 22ad11262460 (2 revisions) (flutter/engine#39624)

* b1f76fbb0 [Impeller] Avoid stringstream usage in CreateUniformMemberKey (flutter/engine#39606)

* a85220d2f Fix a flake in EmbedderTest.CompositorRenderTargetsNotRecycledWhenAvoidsCacheSet (flutter/engine#39596)

* 3141b116d [impeller] Refactored backend specific feature checks to capabilities (flutter/engine#39598)

* bf667254f [impellerc] Generate GLSL output for the runtime-stage-gles target (flutter/engine#39602)

* 57e83aa16 [Impeller] Special case color filter in tiled texture. (flutter/engine#39445)

* e1325a9de Roll Skia from 67a8177742e6 to a3fe5233eb10 (9 revisions) (flutter/engine#39627)

* 9f2c4db50 Add app anatomy diagram (flutter/engine#39628)

* e8c14e436 Roll Fuchsia Linux SDK from mWwKhmxRlXNJATVmu... to A15Lg2MzGSkbj33mo... (flutter/engine#39631)

* 9bd50b706 [Impeller] read from framebuffer for advanced blends on iOS. (flutter/engine#39567)

* 66715c928 Fix build output path of the malioc core list (flutter/engine#39629)

* 727500b12 Roll Skia from a3fe5233eb10 to b7508e2f2577 (3 revisions) (flutter/engine#39634)

* f193de9cc [Impeller] Make RenderTarget::CreateOffscreen utilities have a stencil by default (flutter/engine#39636)

* 76b51e33a Roll Skia from b7508e2f2577 to 5974e36ea190 (4 revisions) (flutter/engine#39639)

* aba44ff0b Roll Dart SDK from 22ad11262460 to 1dc26efa3c4c (1 revision) (flutter/engine#39640)

* 657102f87 Roll Fuchsia Mac SDK from EFcCpAxOuQllDqP0F... to jsnm3dngrd9MveHrB... (flutter/engine#39641)

* 44e36c9c0 Roll Skia from 5974e36ea190 to 2a7644cf4bd7 (1 revision) (flutter/engine#39642)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App e: impeller needs tests
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants