Skip to content

[Impeller] Bind Metal resources through a flat cache - #197188

Open
bdero wants to merge 4 commits into
flutter:masterfrom
bdero:bdero/impeller-metal-binding-cache
Open

bdero wants to merge 4 commits into
flutter:masterfrom
bdero:bdero/impeller-metal-binding-cache

Conversation

@bdero

@bdero bdero commented Nov 23, 2026

Copy link
Copy Markdown
Member

Related to #193745. Split out of #196621.

Metal's pass binding cache walked nested std::maps on every bind and allocated nodes in each pass. It now uses flat arrays and compares unretained pointers, so a bind no longer touches reference counts. The buffer and sampler getters return references, and a texture resolves its Metal object once when it is created instead of on every bind.

Pre-launch Checklist

Metal's binding cache walked nested std::maps for every bind and allocated
nodes in each pass, so it now uses flat arrays. Binding also stops touching
reference counts. The cache compares unretained pointers, the buffer and
sampler getters return references, and a texture resolves its Metal object
once when it is created instead of on every bind.
@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 optimizes the PassBindingsCacheMTL in Impeller's Metal backend by replacing std::map lookups with fixed-size arrays and utilizing __unsafe_unretained pointers to eliminate reference counting overhead during draw calls. It also updates DeviceBufferMTL and SamplerMTL to return references to their underlying Metal objects and adds unit tests for the bindings cache. The review feedback highlights a performance regression in render_pass_mtl.mm where a ternary operator triggers ARC retain/release overhead, and identifies misleading validation log messages in SetTexture and SetSampler that refer to buffers instead of textures or samplers.

SetTexture and SetSampler both reported a buffer.
@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. Happy with the data structure changes. There are some annotations that are superfluous for optimized builds. (We should keep the definitions of those getters in the headers though so they can be inlined easily)

~DeviceBufferMTL() override;

id<MTLBuffer> GetMTLBuffer() const;
const id<MTLBuffer>& GetMTLBuffer() const { return buffer_; }

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 inefficient, an id should just be a pointer. I tested this code and with -O2 it's equivalent, so no reason for the ref.

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

uint64_t index,
uint64_t offset,
id<MTLBuffer> buffer);
__unsafe_unretained id<MTLBuffer> buffer);

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.

These directives are unnecessary too in O2, can we avoid them?

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.

These do change the output. Without them each setter retains its argument on entry and releases it on every exit at -O2, cache hits included.

/// The texture, without acquiring it or touching its reference count, for
/// use once per bind. Nil for a drawable, which only `GetMTLTexture` can
/// acquire.
const id<MTLTexture>& GetResolvedMTLTexture() 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.

Same 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 7fb9eb4.

~SamplerMTL() override;

id<MTLSamplerState> GetMTLSamplerState() const;
const id<MTLSamplerState>& GetMTLSamplerState() const { return state_; }

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.

Same here, having the definition in the header lets it be inlined which is sufficient.

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

The getters are inline, so returning by value compiles to the same code as
returning a reference.
@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