Skip to content

[Impeller] Minify GLSL shaders - #195240

Merged
auto-submit[bot] merged 6 commits into
flutter:masterfrom
zanderso:minify-shaders
Oct 30, 2026
Merged

auto-submit[bot] merged 6 commits into
flutter:masterfrom
zanderso:minify-shaders

Conversation

@zanderso

@zanderso zanderso commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

For the GLSL shaders that get baked into the engine as textual source, this PR:

  • Removes comments and whitespace,
  • Removes needless precision from float literals

This shrinks the GLSL shaders bundled into the engine by ~150KB, reducing the uncompressed size of libflutter.so by roughly that amount. This impacts the on-disk and in-memory size of the engine, but only reduces the in-transit apk by under 20KB since the text compresses well.

Later, we can decide whether to also shorten variable names, which would save an additional ~40KB.

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Oct 24, 2026
@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. e: impeller Impeller rendering backend issues and features requests labels Oct 24, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a GLSL minifier to optimize SPIRV-Cross GLSL output for OpenGL target platforms in Impeller, along with corresponding unit tests. Feedback on the implementation suggests several key improvements: handling the 'f' and 'F' float literal suffixes to ensure they are correctly shortened, replacing locale-dependent checks (std::isalnum and std::isdigit) with locale-independent ASCII checks to prevent parsing bugs, defensively checking the return status of std::to_chars, and preserving newlines between minified lines to maintain shader debuggability and prevent driver issues with extremely long lines.

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems like a liability Zach, are we sure it's worth the 20k in compressed size? Uncompressed isn't something we really care about. I wonder if there is something simpler that gets us the majority of the win? Like stripping comments?

We shouldn't be doing this for debug builds, that's for sure since it obfuscates peoples ability to debug shaders while they are being authored.

@zanderso

Copy link
Copy Markdown
Member Author

Yeah, that's a good question. Native .so's are only compressed in-transit. The in-memory and on-disk cost is the full 190KB, which is the motivation for doing this.

There was already code for stripping comments in the compiler, but not sure if it was effective or not. I think the best way to simplify this change would be to remove the local variable renaming. That would keep most of the benefit and removing whitespace and re-writing floats is more obviously correct.

@jtmcdole jtmcdole left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like the direction, but I yield to Aaron.

namespace impeller {
namespace compiler {

std::string StripGLSLComments(std::string_view source) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sometimes, I wish the compiler was written in dart...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, FFI would have to get to the point where a Dart class can inherit from a C++ class because the spriv_cross code is all C++, and some of the compiler is implemented by subclassing its classes.

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You know what, this glsl is the output of shaderc so there probably isn't a lot of comments already.

For the sake of giving you a clear path forward, let's remove the name mangling for now and we can measure the benefit after this lands and decide if we want to do that as a followup. Let's also only do this for release engines.

@gaaclarke

Copy link
Copy Markdown
Member

Oh that reminds me, we are using shaderc or spirv-cross to generate the glsl code. I can't remember which. But they may already have a flag that does this minification for us already if it is generating glsl. We should pull on that thread.

@zanderso

Copy link
Copy Markdown
Member Author

Oh that reminds me, we are using shaderc or spirv-cross to generate the glsl code. I can't remember which. But they may already have a flag that does this minification for us already if it is generating glsl. We should pull on that thread.

I'll take a look.

@zanderso zanderso left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There's no 'minification' option/flag in spriv_cross, but I found a simpler way to implement the float minification by subclassing spirv_cross::CompilerGLSL. The whitespace removal is simpler as-is in this PR.

I removed the variable name shortening and that added back ~30KB or so.

/// Registers a custom `spirv_cross::FloatFormatter` backed by `std::to_chars`
/// so floating-point literals are emitted in their shortest round-tripping
/// decimal representation instead of SPIRV-Cross's default `"%.32g"` format.
class MinifyingCompilerGLSL final : public spirv_cross::CompilerGLSL {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Subclassing CompilerGLSL allows overriding the methods that emit floats so that they're just emitted using the shortest representation in the first place.

To get another ~30KB of reduction, it's also possible to override methods in here to get shorter variable names.

@gaaclarke

Copy link
Copy Markdown
Member

@zanderso friendly reminder, when you want another review make sure to press the "re-request review" button. looks like you may be close to that point

@zanderso

Copy link
Copy Markdown
Member Author

Thanks for the reminder! I was waiting for the presubs to be all green before re-requesting =)

@flutter-dashboard

Copy link
Copy Markdown

Golden file changes have been found for this pull request. Click here to view and triage (e.g. because this is an intentional change).

If you are still iterating on this change and are not ready to resolve the images on the Flutter Gold dashboard, consider marking this PR as a draft pull request above. You will still be able to view image results on the dashboard, commenting will be silenced, and the check will not try to resolve itself until marked ready for review.

For more guidance, visit Writing a golden file test for package:flutter.

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

Changes reported for pull request #195240 at sha f8eab92

@flutter-dashboard flutter-dashboard Bot added the will affect goldens Changes to golden files label Oct 27, 2026
@zanderso
zanderso requested a review from gaaclarke October 28, 2026 04:14

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mostly looking good. There is one potential gotcha in the float transformation logic. We should do a followup to apply this to user defined shaders too.

@zanderso
zanderso requested a review from jtmcdole October 30, 2026 12:54

@gaaclarke gaaclarke left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm, thanks

@zanderso zanderso added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Oct 30, 2026
Merged via the queue into flutter:master with commit cf912e3 Oct 30, 2026
23 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Oct 30, 2026
flutter/flutter@c091098...5e805bf

2026-10-30 [email protected] Fix/shared thread merger expired lease (flutter/flutter#194200)
2026-10-30 [email protected] [FGP] Report the real error when applying kotlin-android fails (flutter/flutter#195080)
2026-10-30 [email protected] [Impeller] Minify GLSL shaders (flutter/flutter#195240)
2026-10-30 [email protected] Roll Packages from 0b72e92 to 3742a4e (12 revisions) (flutter/flutter#195686)
2026-10-30 [email protected] Roll Dart SDK from 52c908c00267 to fe609902984b (2 revisions) (flutter/flutter#195679)
2026-10-30 [email protected] Roll Skia from 0789f2ab93b8 to c0c46ee9f938 (1 revision) (flutter/flutter#195671)
2026-10-30 [email protected] Roll Fuchsia Linux SDK from SORPfa9tgFn83M1CR... to x2SqGbVUkOm4d6vk_... (flutter/flutter#195670)
2026-10-30 [email protected] Roll Skia from 4470b8abaad6 to 0789f2ab93b8 (2 revisions) (flutter/flutter#195665)
2026-10-30 [email protected] Roll Dart SDK from 89f7a35480d6 to 52c908c00267 (1 revision) (flutter/flutter#195659)
2026-10-30 [email protected] Roll Skia from 9f4e3117465e to 4470b8abaad6 (4 revisions) (flutter/flutter#195658)
2026-10-30 [email protected] Roll Skia from ec13cd547e98 to 9f4e3117465e (1 revision) (flutter/flutter#195650)
2026-10-30 [email protected] [web] Keep semantics editing state while the text input connection is open (flutter/flutter#195141)
2026-10-30 [email protected] [web_paragraph] Fix multi-span BiDi span order and region direction (flutter/flutter#194980)
2026-10-30 [email protected] [web] Unskip web tests that pass on CanvasKit and Skwasm: accessibility_inspector_test, color_filter_test (flutter/flutter#195541)
2026-10-30 [email protected] Roll Skia from 4ecd22a4a1d0 to ec13cd547e98 (10 revisions) (flutter/flutter#195647)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
@zanderso
zanderso deleted the minify-shaders branch November 2, 2026 15:20
gmackall pushed a commit to gmackall/flutter that referenced this pull request Nov 3, 2026
For the GLSL shaders that get baked into the engine as textual source,
this PR:
- Removes comments and whitespace,
- Removes needless precision from float literals

This shrinks the GLSL shaders bundled into the engine by ~150KB,
reducing the uncompressed size of libflutter.so by roughly that amount.
This impacts the on-disk and in-memory size of the engine, but only
reduces the in-transit apk by under 20KB since the text compresses well.

Later, we can decide whether to also shorten variable names, which would
save an additional ~40KB.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant in-code documentation (doc comments with
`///`).
- [x] If this PR introduces a new feature or capability, I created and
linked a website documentation issue or PR in [flutter/website] (or
verified none is needed).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

If this change needs to override an active code freeze, provide a
comment explaining why. The code freeze workflow can be overridden by
code reviewers. See pinned issues for any active code freezes with
guidance.

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[flutter/website]: https://github.com/flutter/website
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD e: impeller Impeller rendering backend issues and features requests engine flutter/engine related. See also e: labels. will affect goldens Changes to golden files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants