Skip to content

[Flutter GPU] Record pipeline state as plain data and cache pipelines per state - #197190

Open
bdero wants to merge 4 commits into
flutter:masterfrom
bdero:bdero/flutter-gpu-pipeline-state
Open

bdero wants to merge 4 commits into
flutter:masterfrom
bdero:bdero/flutter-gpu-pipeline-state

Conversation

@bdero

@bdero bdero commented Nov 23, 2026

Copy link
Copy Markdown
Member

Related to #193745. Split out of #196621.

A render pass built an impeller::PipelineDescriptor on the heap each time its state changed, and replay looked the result up in the pipeline library. The state is now a plain struct copied into the recording, and each render pipeline keeps the backend pipelines it has resolved, so a state change costs one hash. Registering a shader again drops what was resolved.

The per-draw calls from Dart are now leaf calls.

Pre-launch Checklist

… per state

A render pass built an impeller::PipelineDescriptor on the heap each time its
state changed, and replay looked the result up in the pipeline library. The
state is now a plain struct copied into the recording, and each pipeline keeps
the backend pipelines it has resolved, so a state change costs one hash.

The per-draw calls from Dart are now leaf calls.
@bdero bdero added engine flutter/engine related. See also e: labels. flutter-gpu CICD Run CI/CD team-fluttergpu Owned by Flutter GPU team labels Nov 23, 2026
@github-project-automation github-project-automation Bot moved this to 🤔 Needs Triage in Flutter GPU Nov 23, 2026
@bdero
bdero marked this pull request as ready for review November 23, 2026 01:08

@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 PipelineState struct to represent the backend pipeline state as plain data, optimizing state changes and pipeline lookups in RenderPass and RenderPipeline. It also marks several Dart native bindings as leaf calls and adds corresponding unit tests. The review feedback advises against truncating PipelineState within the recording stream (SetPipelineStateOp), as passing a truncated object by reference can trigger undefined behavior and segmentation faults from compiler-optimized wide reads. Retaining the full struct simplifies the codebase by eliminating manual std::memcpy operations, allowing direct map lookups, and enabling compile-time size optimizations in the stream's Append method.

Comment thread engine/src/flutter/lib/gpu/recording.h Resolved
Comment thread engine/src/flutter/lib/gpu/recording.h Resolved
A recorded op held only the used leading bytes of its PipelineState, which
left a reference to it pointing at less storage than the type. The op now
holds the whole struct.
@gaaclarke
gaaclarke self-requested a review November 23, 2026 18:21

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

Looking promising. The big thing from me is the absl hash request, gemini pointed out a potentially scary timing bug.

Comment on lines +38 to +47
size_t PipelineState::GetHash() const {
size_t hash = fml::HashCombine(
cull_mode, winding_order, primitive_type, polygon_mode, sample_count,
depth_format, stencil_format, color_attachment_count, depth.GetHash(),
stencil_front.GetHash(), stencil_back.GetHash());
for (size_t i = 0; i < color_attachment_count; i++) {
hash = fml::HashCombine(hash, colors[i].Hash());
}
return hash;
}

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.

Let's use absl hash instead of fml hash. The function is way better and work better with the absl containers.

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.

Done in 1bd8a60.

return hash;
}

impeller::PipelineDescriptor PipelineState::ToDescriptor() const {

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.

Let's add a pipeline_state_unittests.cc to test things like this.

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.

Done in 1bd8a60.

EXPECT_NE(a.GetHash(), b.GetHash());
}

TEST(FlutterGpuPipelineStateTest, ToDescriptorKeepsTheState) {

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.

Things like this should go in pipeline_state_unittests.cc

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.

Done in 1bd8a60.


void RenderPass::SetPipeline(fml::RefPtr<RenderPipeline> pipeline) {
if (render_pipeline_.get() == pipeline.get()) {
void RenderPass::SetPipeline(RenderPipeline* pipeline) {

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.

should this be RenderPipeline&?

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.

Done in 1bd8a60.


@Native<Void Function(Pointer<Void>, Pointer<Void>)>(
symbol: 'InternalFlutterGpu_RenderPass_BindPipeline',
isLeaf: true,

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.

Gemini thinks there is a bug here, PTAL

SetPipeline now runs as a leaf call, and its render_pipeline_ = fml::Ref(pipeline) releases the old pipeline. The Dart side doesn't keep the bound pipeline alive. bindPipeline just calls _bindPipeline(pipeline) and stores nothing. So this sequence is possible:

pass.bindPipeline(makePipeline());   // temporary; only the C++ RenderPass holds it after GC
pass.draw(3);
// GC: the old pipeline's Dart wrapper is finalized, so its ref is released
pass.bindPipeline(other);            // leaf: drops the LAST ref to the old pipeline

That last release runs ~RenderPipeline, then ~DartWrappable, then ~DartWeakPersistentValue, then Clear(), which calls Dart_CurrentIsolateGroup() and Dart_DeleteWeakPersistentHandle() (dart_weak_persistent_value.cc:44-52). FinalizeDartWrapper only drops the reference and never clears the handle, so the handle is still non-null at that point. Leaf calls aren't allowed to use any Dart_* API.

None of the RenderPass calls were leaf calls before this PR; all 21 isLeaf: true lines are new. Of those, SetPipeline is the only one that can drop the last reference to a Dart-wrapped object. BindSet never stores the set, and the buffer/texture binds keep impeller objects, not the wrappers.

Fix: either keep _bindPipeline as a non-leaf call (it only runs when the pipeline changes), or have the Dart side keep the bound pipeline alive across the call:

_bindPipeline(pipeline);
_pipeline = pipeline;  // assign after the call, so the old wrapper stays alive during it

Neither the existing tests nor the new ones can catch this, because it depends on GC timing.

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.

That was real. The pass now holds the pipeline's core, which has no Dart wrapper, so the leaf call can no longer release a wrapped object (1bd8a60).

…line state with absl

Rebinding a pipeline is a leaf call, and a pass that held the previous
pipeline could release the last reference to its Dart wrapper there, which
runs the Dart API. A pass now holds the pipeline's core, which has no
wrapper.

PipelineState hashes through AbslHashValue, and its tests move to their own
file.
@bdero
bdero requested a review from gaaclarke November 26, 2026 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. flutter-gpu team-fluttergpu Owned by Flutter GPU team

Projects

Status: 🤔 Needs Triage

Development

Successfully merging this pull request may close these issues.

2 participants