Skip to content

Commit

Permalink
[flutter] Batch by color as well, to avoid Impeller slow path
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed Jun 2, 2023
1 parent ea1f802 commit bee4d40
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion spine-flutter/lib/spine_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4071,7 +4071,21 @@ class RenderCommand {
// is copied, so it doesn't matter that we free up the underlying memory on the next
// render call. See the implementation of Vertices.raw() here:
// https://github.com/flutter/engine/blob/5c60785b802ad2c8b8899608d949342d5c624952/lib/ui/painting/vertices.cc#L21
vertices = Vertices.raw(VertexMode.triangles, positions, textureCoordinates: uvs, colors: colors, indices: indices);
//
// Impeller is currently using a slow path when using vertex colors.
// See https://github.com/flutter/flutter/issues/127486
//
// We thus batch all meshes not only by atlas page and blend mode, but also vertex color.
// See spine_flutter.cpp, batch_commands().
//
// If the vertex color equals (1, 1, 1, 1), we do not store
// colors, which will trigger the fast path in Impeller. Otherwise we have to go the slow path, which
// has to render to an offscreen surface.
if (colors.isNotEmpty && colors[0] == -1) {
vertices = Vertices.raw(VertexMode.triangles, positions, textureCoordinates: uvs, indices: indices);
} else {
vertices = Vertices.raw(VertexMode.triangles, positions, textureCoordinates: uvs, colors: colors, indices: indices);
}
} else {
// On the web, rendering is done through CanvasKit, which requires copies of the native data.
final positionsCopy = Float32List.fromList(positions);
Expand Down
1 change: 1 addition & 0 deletions spine-flutter/src/spine_flutter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ static _spine_render_command *batch_commands(BlockAllocator &allocator, Vector<_
_spine_render_command *cmd = i < commands.size() ? commands[i] : nullptr;
if (cmd != nullptr && cmd->atlasPage == first->atlasPage &&
cmd->blendMode == first->blendMode &&
cmd->colors[0] == first->colors[0] &&
numIndices + cmd->numIndices < 0xffff) {
numVertices += cmd->numVertices;
numIndices += cmd->numIndices;
Expand Down

0 comments on commit bee4d40

Please sign in to comment.