Repository navigation
Conversation
… 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.
There was a problem hiding this comment.
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.
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
left a comment
There was a problem hiding this comment.
Looking promising. The big thing from me is the absl hash request, gemini pointed out a potentially scary timing bug.
| 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; | ||
| } |
There was a problem hiding this comment.
Let's use absl hash instead of fml hash. The function is way better and work better with the absl containers.
| return hash; | ||
| } | ||
|
|
||
| impeller::PipelineDescriptor PipelineState::ToDescriptor() const { |
There was a problem hiding this comment.
Let's add a pipeline_state_unittests.cc to test things like this.
| EXPECT_NE(a.GetHash(), b.GetHash()); | ||
| } | ||
|
|
||
| TEST(FlutterGpuPipelineStateTest, ToDescriptorKeepsTheState) { |
There was a problem hiding this comment.
Things like this should go in pipeline_state_unittests.cc
|
|
||
| void RenderPass::SetPipeline(fml::RefPtr<RenderPipeline> pipeline) { | ||
| if (render_pipeline_.get() == pipeline.get()) { | ||
| void RenderPass::SetPipeline(RenderPipeline* pipeline) { |
There was a problem hiding this comment.
should this be RenderPipeline&?
|
|
||
| @Native<Void Function(Pointer<Void>, Pointer<Void>)>( | ||
| symbol: 'InternalFlutterGpu_RenderPass_BindPipeline', | ||
| isLeaf: true, |
There was a problem hiding this comment.
Gemini thinks there is a bug here, PTAL
SetPipelinenow runs as a leaf call, and itsrender_pipeline_ = fml::Ref(pipeline)releases the old pipeline. The Dart side doesn't keep the bound pipeline alive.bindPipelinejust 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 pipelineThat last release runs
~RenderPipeline, then~DartWrappable, then~DartWeakPersistentValue, thenClear(), which callsDart_CurrentIsolateGroup()andDart_DeleteWeakPersistentHandle()(dart_weak_persistent_value.cc:44-52).FinalizeDartWrapperonly 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 anyDart_*API.None of the
RenderPasscalls were leaf calls before this PR; all 21isLeaf: truelines are new. Of those,SetPipelineis the only one that can drop the last reference to a Dart-wrapped object.BindSetnever stores the set, and the buffer/texture binds keep impeller objects, not the wrappers.Fix: either keep
_bindPipelineas 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 itNeither the existing tests nor the new ones can catch this, because it depends on GC timing.
There was a problem hiding this comment.
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.
Related to #193745. Split out of #196621.
A render pass built an
impeller::PipelineDescriptoron 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
///).