Skip to content

[Impeller] Count pipeline uses once per pass and skip a pipeline that is already bound - #197189

Open
bdero wants to merge 5 commits into
flutter:masterfrom
bdero:bdero/impeller-pipeline-use-counts
Open

bdero wants to merge 5 commits into
flutter:masterfrom
bdero:bdero/impeller-pipeline-use-counts

Conversation

@bdero

@bdero bdero commented Nov 23, 2026

Copy link
Copy Markdown
Member

Related to #193745. Split out of #196621.

Profile and debug builds counted each draw's pipeline by hashing its descriptor under a library-wide writer lock, which was about a fifth of Flutter GPU's per-draw encode time on Metal and serialized threads encoding at the same time. Passes now count uses by pointer and report each distinct pipeline once when encoded.

Metal and Vulkan also skip a pipeline that is already bound for the pass, and OpenGL ES reuses one scratch buffer when unpadding uniform arrays.

Pre-launch Checklist

Profile and debug builds counted each draw's pipeline by hashing its
descriptor under a library-wide writer lock, which cost about a fifth of
Flutter GPU's per-draw encode time on Metal and serialized threads encoding
at the same time. Passes now count uses by pointer and report each distinct
pipeline once when encoded.
Metal reapplied a pipeline's whole state to the encoder on every draw, and
Vulkan rebound it on every draw, even when it had not changed. Both now
remember what is bound for the pass.

OpenGL ES reuses one buffer when unpadding uniform arrays instead of
allocating one per draw.
@bdero bdero added engine flutter/engine related. See also e: labels. e: impeller Impeller rendering backend issues and features requests CICD Run CI/CD labels 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 PipelineUseCounter to batch and defer pipeline usage logging in Impeller, moving the overhead of hashing descriptors out of individual draw calls. It updates RenderPass and its backend implementations (GLES, Metal, Vulkan) to count pipeline uses and flush them to the PipelineLibrary when encoding commands. Additionally, it optimizes GLES uniform buffer binding by reusing a scratch vector. The review feedback suggests adding a defensive null check for pipeline in RenderPassMTL::SetPipeline to prevent potential null pointer dereferences, and marking unused parameters with [[maybe_unused]] in PipelineUseCounter::Count, PipelineLibrary::LogPipelineUsage, and PipelineUseCounter::Flush to avoid compiler warnings in release builds.

A null pipeline on a pass with nothing bound matched the remembered pipeline
and left the pass marked as having a valid one.
@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.

This is looking good, there's just a few places i'd like cleaned up with conditional compilations. Here's roughly my goals with the pipeline usage counter code:

  1. minimize the amount of conditional compilation
  2. put conditional compilation in .cc files outside of standard impeller classes
  3. minimize code in standard impeller classes for it (prefer to put logic in the counter compilation units)

bool has_valid_pipeline_ = false;
// The pipeline whose state is set on the encoder, which persists across
// draws.
const Pipeline<PipelineDescriptor>* bound_pipeline_ = nullptr;

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.

Raw pointers are verboten. Bare minimum we should be documenting the lifetime. Maybe use a weak_ptr on debug builds?

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 36c8215. It is a PipelineRef now, and the comment covers the lifetime.

Comment on lines +80 to +85
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG || \
FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE
if (auto library = context_->GetPipelineLibrary()) {
pipeline_uses_.Flush(*library);
}
#endif

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.

The preprocessor macro is superfluous here.

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 36c8215.

///
class PipelineUseCounter {
public:
void Count(const Pipeline<PipelineDescriptor>* 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.

can we put this in the .cc file please?

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 36c8215.

Comment on lines +53 to +54
#if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG || \
FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE

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 not optimize this away to keep the code simpler, its basically nothing if it's never used anyways.

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 36c8215.


// |RenderPass|
void RenderPassMTL::SetPipeline(PipelineRef pipeline) {
CountPipelineUse(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.

Do we want this after the !pipeline check?

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 36c8215.

Counting moves out of the header, the counter's members exist in every build
mode, and a pass no longer guards the flush. Metal remembers the bound
pipeline as a PipelineRef and counts a use only after rejecting a null one.
@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 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