Skip to content

Use uniforms in the Porter Duff blend shader instead of specialization constants - #195458

Merged
auto-submit[bot] merged 5 commits into
flutter:masterfrom
walley892:porter-duff-defrag
Oct 30, 2026
Merged

auto-submit[bot] merged 5 commits into
flutter:masterfrom
walley892:porter-duff-defrag

Conversation

@walley892

@walley892 walley892 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Changes the Porter Duff blend shader to use uniforms instead of specialization constants for the blending coefficients. As each specialized variant triggered a full shader compile, this removes 14 shader compiles (~23%) from the engine initialization path, it also removes overhead from swapping shaders.

This was how the shader was initially configured. It was modified to use specialization constants because of compilation issues on adreno chips using Vulkan (PR, issue). However, those chips are now blacklisted from Vulkan (PR) - so no compilation issues occur.

Pre-launch Checklist

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

@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 28, 2026
@walley892 walley892 added the CICD Run CI/CD label Oct 29, 2026
@walley892 walley892 changed the title porter duff defrag Use uniforms in the Porter Duff blend shader instead of specialization constants Oct 29, 2026
@walley892

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 consolidates the multiple specialized Porter-Duff blend pipelines in Impeller into a single pipeline, passing the blend coefficients as uniforms via the fragment shader's uniform block instead of using specialization constants. The feedback identifies a potential out-of-bounds read in the new 'SetPorterDuffCoefficients' function if an invalid or negative 'BlendMode' is cast, recommending a cast to 'size_t' prior to the bounds check to ensure safety.

Comment on lines +58 to +62
if (blend_mode > Entity::kLastPipelineBlendMode) {
VALIDATION_LOG << "Invalid porter duff blend mode "
<< BlendModeToString(blend_mode);
blend_mode = BlendMode::kClear;
}

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.

high

If blend_mode is negative (which can happen if an invalid integer is cast to BlendMode or due to uninitialized memory), the direct comparison blend_mode > Entity::kLastPipelineBlendMode will evaluate to false because BlendMode is typically a signed enum. Subsequently, static_cast<size_t>(blend_mode) will wrap around to a very large value, leading to an out-of-bounds read on kPorterDuffCoefficients and a potential crash.

To prevent this, cast blend_mode to size_t before performing the comparison. This handles both negative values and values exceeding the upper bound in a single check.

Suggested change
if (blend_mode > Entity::kLastPipelineBlendMode) {
VALIDATION_LOG << "Invalid porter duff blend mode "
<< BlendModeToString(blend_mode);
blend_mode = BlendMode::kClear;
}
if (static_cast<size_t>(blend_mode) > static_cast<size_t>(Entity::kLastPipelineBlendMode)) {
VALIDATION_LOG << "Invalid porter duff blend mode "
<< BlendModeToString(blend_mode);
blend_mode = BlendMode::kClear;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

BlendMode is an unsigned int8

enum class BlendMode : uint8_t {
. If an invalid integer were cast to it, it would wrap to the valid range.

@walley892
walley892 requested a review from gaaclarke October 29, 2026 23:30
@walley892
walley892 marked this pull request as ready for review October 29, 2026 23:30

@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 consolidates the individual Porter-Duff blend pipelines in Impeller into a single pipeline (porter_duff_blend) that selects the blend mode dynamically using uniforms. The porter_duff_blend.frag shader, ContentContext, AtlasContents, BlendFilterContents, and VerticesSimpleBlendContents have been updated to support this unified pipeline by passing blend coefficients as uniforms. Feedback points out a potential out-of-bounds memory access in SetPorterDuffCoefficients if BlendMode is a signed enum and a negative value is passed, which can bypass the bounds check. Casting blend_mode to size_t before comparison is recommended to prevent this issue.

Comment on lines +58 to +62
if (blend_mode > Entity::kLastPipelineBlendMode) {
VALIDATION_LOG << "Invalid porter duff blend mode "
<< BlendModeToString(blend_mode);
blend_mode = BlendMode::kClear;
}

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.

security-high high

If BlendMode is a signed enum, passing a negative value to SetPorterDuffCoefficients will bypass the blend_mode > Entity::kLastPipelineBlendMode check. When it is subsequently cast to size_t to index kPorterDuffCoefficients, it will wrap around to a very large value, causing an out-of-bounds memory access.\n\nCasting both operands to size_t before comparison ensures that any negative values are correctly caught as being out of bounds.

  if (static_cast<size_t>(blend_mode) >\n      static_cast<size_t>(Entity::kLastPipelineBlendMode)) {\n    VALIDATION_LOG << "Invalid porter duff blend mode "\n                   << BlendModeToString(blend_mode);\n    blend_mode = BlendMode::kClear;\n  }

@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

@walley892 walley892 added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
@auto-submit

auto-submit Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/195458, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label.

@walley892 walley892 added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
@auto-submit auto-submit Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Oct 30, 2026
@auto-submit

auto-submit Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

autosubmit label was removed for flutter/flutter/195458, because - The status or check suite Presubmit has failed. Please fix the issues identified (or deflake) before re-applying this label.

@walley892 walley892 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 5d7f53a Oct 30, 2026
40 of 41 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 31, 2026
flutter/flutter@5e805bf...ee8b482

2026-10-31 [email protected] Roll Skia from 9bab765df357 to 607de4b035dc (1 revision) (flutter/flutter#195728)
2026-10-31 [email protected] Roll Skia from 284054bec98a to 9bab765df357 (2 revisions) (flutter/flutter#195726)
2026-10-31 [email protected] Roll Skia from 3a3f56f7ddd4 to 284054bec98a (1 revision) (flutter/flutter#195724)
2026-10-31 [email protected] Roll Skia from 466994d27b1e to 3a3f56f7ddd4 (1 revision) (flutter/flutter#195719)
2026-10-30 [email protected] Fix XcodeProjectInterpreter dependency cycle in flutter_tools (flutter/flutter#195694)
2026-10-30 [email protected] Document that Positioned also works inside an Overlay (flutter/flutter#190607)
2026-10-30 [email protected] Roll Skia from ce0b85b46db6 to 466994d27b1e (3 revisions) (flutter/flutter#195711)
2026-10-30 [email protected] Adds Vulkan sync validation test run (flutter/flutter#195596)
2026-10-30 [email protected] [Android] Let the raster thread use every core of a chip without little cores (flutter/flutter#195071)
2026-10-30 [email protected] Roll Skia from 121d04c01e2d to ce0b85b46db6 (6 revisions) (flutter/flutter#195699)
2026-10-30 [email protected] [web] Anchor platform view slots to top-left to fix RTL document layout (flutter/flutter#194601)
2026-10-30 [email protected] Update golden file testing docs (flutter/flutter#195449)
2026-10-30 [email protected] [tool] Migrate Cache.flutterRoot static state to instance configuration (flutter/flutter#190795)
2026-10-30 [email protected] Apply view metrics overrides below the framework (flutter/flutter#194311)
2026-10-30 [email protected] Roll Skia from c0c46ee9f938 to 121d04c01e2d (9 revisions) (flutter/flutter#195687)
2026-10-30 [email protected] Use uniforms in the Porter Duff blend shader instead of specialization constants (flutter/flutter#195458)
2026-10-30 [email protected] [flutter_tools] Inject dependencies into iOS, macOS, and Windows application packages (flutter/flutter#194047)

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
gmackall pushed a commit to gmackall/flutter that referenced this pull request Nov 3, 2026
…n constants (flutter#195458)

Changes the Porter Duff blend shader to use uniforms instead of
specialization constants for the blending coefficients. As each
specialized variant triggered a full shader compile, this removes 14
shader compiles (~23%) from the engine initialization path, it also
removes overhead from swapping shaders.

This was how the shader was initially configured. It was modified to use
specialization constants because of compilation issues on adreno chips
using Vulkan ([PR](flutter#161273),
[issue](flutter#161209)). However,
those chips are now blacklisted from Vulkan
([PR](flutter#178833)) - so no
compilation issues occur.

## 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.
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants