Reverts "[Impeller] immediately encode blit commands for Metal. (#52795)" (flutter/engine#52816)

Reverts: flutter/engine#52795
Initiated by: jonahwilliams
Reason for reverting: failing postsubmit testing -[MTLIGAccelCommandBuffer renderCommandEncoderWithDescriptor:]:438: failed assertion `Already have uncommitted encoder'
Original PR Author: jonahwilliams

Reviewed By: {chinmaygarde}

This change reverts the following previous change:
Metal equivalent of https://github.com/flutter/engine/pull/52794

No need to defer encoding of blit commands when the command encoder already defers execution until we submit the command buffer. Removes extra indirection and heap allocation.
This commit is contained in:
auto-submit[bot]
2024-05-14 19:39:47 +00:00
committed by GitHub
parent fe8dfc3e19
commit c7a5f42051
7 changed files with 297 additions and 154 deletions

View File

@@ -42656,6 +42656,8 @@ ORIGIN: ../../../flutter/impeller/renderer/backend/gles/texture_gles.cc + ../../
ORIGIN: ../../../flutter/impeller/renderer/backend/gles/texture_gles.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/impeller/renderer/backend/metal/command_buffer_mtl.h + ../../../flutter/LICENSE
@@ -45529,6 +45531,8 @@ FILE: ../../../flutter/impeller/renderer/backend/gles/texture_gles.cc
FILE: ../../../flutter/impeller/renderer/backend/gles/texture_gles.h
FILE: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.h
FILE: ../../../flutter/impeller/renderer/backend/metal/allocator_mtl.mm
FILE: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.h
FILE: ../../../flutter/impeller/renderer/backend/metal/blit_command_mtl.mm
FILE: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.h
FILE: ../../../flutter/impeller/renderer/backend/metal/blit_pass_mtl.mm
FILE: ../../../flutter/impeller/renderer/backend/metal/command_buffer_mtl.h

View File

@@ -8,6 +8,8 @@ impeller_component("metal") {
sources = [
"allocator_mtl.h",
"allocator_mtl.mm",
"blit_command_mtl.h",
"blit_command_mtl.mm",
"blit_pass_mtl.h",
"blit_pass_mtl.mm",
"command_buffer_mtl.h",

View File

@@ -0,0 +1,66 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_BLIT_COMMAND_MTL_H_
#define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_BLIT_COMMAND_MTL_H_
#include <Metal/Metal.h>
#include "impeller/base/backend_cast.h"
#include "impeller/renderer/blit_command.h"
namespace impeller {
/// Mixin for dispatching Metal commands.
struct BlitEncodeMTL : BackendCast<BlitEncodeMTL, BlitCommand> {
virtual ~BlitEncodeMTL();
virtual std::string GetLabel() const = 0;
[[nodiscard]] virtual bool Encode(
id<MTLBlitCommandEncoder> encoder) const = 0;
};
struct BlitCopyTextureToTextureCommandMTL
: public BlitCopyTextureToTextureCommand,
public BlitEncodeMTL {
~BlitCopyTextureToTextureCommandMTL() override;
std::string GetLabel() const override;
[[nodiscard]] bool Encode(id<MTLBlitCommandEncoder> encoder) const override;
};
struct BlitCopyTextureToBufferCommandMTL
: public BlitCopyTextureToBufferCommand,
public BlitEncodeMTL {
~BlitCopyTextureToBufferCommandMTL() override;
std::string GetLabel() const override;
[[nodiscard]] bool Encode(id<MTLBlitCommandEncoder> encoder) const override;
};
struct BlitGenerateMipmapCommandMTL : public BlitGenerateMipmapCommand,
public BlitEncodeMTL {
~BlitGenerateMipmapCommandMTL() override;
std::string GetLabel() const override;
[[nodiscard]] bool Encode(id<MTLBlitCommandEncoder> encoder) const override;
};
struct BlitCopyBufferToTextureCommandMTL
: public BlitCopyBufferToTextureCommand,
public BlitEncodeMTL {
~BlitCopyBufferToTextureCommandMTL() override;
std::string GetLabel() const override;
[[nodiscard]] bool Encode(id<MTLBlitCommandEncoder> encoder) const override;
};
} // namespace impeller
#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_BLIT_COMMAND_MTL_H_

View File

@@ -0,0 +1,152 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/renderer/backend/metal/blit_command_mtl.h"
#include "impeller/renderer/backend/metal/device_buffer_mtl.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
namespace impeller {
BlitEncodeMTL::~BlitEncodeMTL() = default;
BlitCopyTextureToTextureCommandMTL::~BlitCopyTextureToTextureCommandMTL() =
default;
std::string BlitCopyTextureToTextureCommandMTL::GetLabel() const {
return label;
}
bool BlitCopyTextureToTextureCommandMTL::Encode(
id<MTLBlitCommandEncoder> encoder) const {
auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture();
if (!source_mtl) {
return false;
}
auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture();
if (!destination_mtl) {
return false;
}
auto source_origin_mtl =
MTLOriginMake(source_region.GetX(), source_region.GetY(), 0);
auto source_size_mtl =
MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1);
auto destination_origin_mtl =
MTLOriginMake(destination_origin.x, destination_origin.y, 0);
[encoder copyFromTexture:source_mtl
sourceSlice:0
sourceLevel:0
sourceOrigin:source_origin_mtl
sourceSize:source_size_mtl
toTexture:destination_mtl
destinationSlice:0
destinationLevel:0
destinationOrigin:destination_origin_mtl];
return true;
};
BlitCopyTextureToBufferCommandMTL::~BlitCopyTextureToBufferCommandMTL() =
default;
std::string BlitCopyTextureToBufferCommandMTL::GetLabel() const {
return label;
}
bool BlitCopyTextureToBufferCommandMTL::Encode(
id<MTLBlitCommandEncoder> encoder) const {
auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture();
if (!source_mtl) {
return false;
}
auto destination_mtl = DeviceBufferMTL::Cast(*destination).GetMTLBuffer();
if (!destination_mtl) {
return false;
}
auto source_origin_mtl =
MTLOriginMake(source_region.GetX(), source_region.GetY(), 0);
auto source_size_mtl =
MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1);
auto destination_bytes_per_pixel =
BytesPerPixelForPixelFormat(source->GetTextureDescriptor().format);
auto destination_bytes_per_row =
source_size_mtl.width * destination_bytes_per_pixel;
auto destination_bytes_per_image =
source_size_mtl.height * destination_bytes_per_row;
[encoder copyFromTexture:source_mtl
sourceSlice:0
sourceLevel:0
sourceOrigin:source_origin_mtl
sourceSize:source_size_mtl
toBuffer:destination_mtl
destinationOffset:destination_offset
destinationBytesPerRow:destination_bytes_per_row
destinationBytesPerImage:destination_bytes_per_image];
return true;
};
BlitCopyBufferToTextureCommandMTL::~BlitCopyBufferToTextureCommandMTL() =
default;
std::string BlitCopyBufferToTextureCommandMTL::GetLabel() const {
return label;
}
bool BlitCopyBufferToTextureCommandMTL::Encode(
id<MTLBlitCommandEncoder> encoder) const {
auto source_mtl = DeviceBufferMTL::Cast(*source.buffer).GetMTLBuffer();
if (!source_mtl) {
return false;
}
auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture();
if (!destination_mtl) {
return false;
}
auto destination_origin_mtl =
MTLOriginMake(destination_region.GetX(), destination_region.GetY(), 0);
auto source_size_mtl = MTLSizeMake(destination_region.GetWidth(),
destination_region.GetHeight(), 1);
auto destination_bytes_per_pixel =
BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format);
auto source_bytes_per_row =
destination_region.GetWidth() * destination_bytes_per_pixel;
[encoder copyFromBuffer:source_mtl
sourceOffset:source.range.offset
sourceBytesPerRow:source_bytes_per_row
sourceBytesPerImage:
0 // 0 for 2D textures according to
// https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400752-copyfrombuffer
sourceSize:source_size_mtl
toTexture:destination_mtl
destinationSlice:slice
destinationLevel:0
destinationOrigin:destination_origin_mtl];
return true;
};
BlitGenerateMipmapCommandMTL::~BlitGenerateMipmapCommandMTL() = default;
std::string BlitGenerateMipmapCommandMTL::GetLabel() const {
return label;
}
bool BlitGenerateMipmapCommandMTL::Encode(
id<MTLBlitCommandEncoder> encoder) const {
return TextureMTL::Cast(*texture).GenerateMipmap(encoder);
};
} // namespace impeller

View File

@@ -7,6 +7,7 @@
#include <Metal/Metal.h>
#include "impeller/renderer/backend/metal/blit_command_mtl.h"
#include "impeller/renderer/blit_pass.h"
namespace impeller {
@@ -19,14 +20,10 @@ class BlitPassMTL final : public BlitPass {
private:
friend class CommandBufferMTL;
id<MTLBlitCommandEncoder> encoder_ = nil;
std::vector<std::unique_ptr<BlitEncodeMTL>> commands_;
id<MTLCommandBuffer> buffer_ = nil;
std::string label_;
bool is_valid_ = false;
bool is_metal_trace_active_ = false;
// Many parts of the codebase will start writing to a render pass but
// never submit them. This boolean is used to track if a submit happened
// so that in the dtor we can always ensure the render pass is finished.
mutable bool did_finish_encoding_ = false;
explicit BlitPassMTL(id<MTLCommandBuffer> buffer);
@@ -40,6 +37,8 @@ class BlitPassMTL final : public BlitPass {
bool EncodeCommands(
const std::shared_ptr<Allocator>& transients_allocator) const override;
bool EncodeCommands(id<MTLBlitCommandEncoder> pass) const;
// |BlitPass|
bool OnCopyTextureToTextureCommand(std::shared_ptr<Texture> source,
std::shared_ptr<Texture> destination,

View File

@@ -15,12 +15,12 @@
#include "impeller/core/formats.h"
#include "impeller/core/host_buffer.h"
#include "impeller/core/shader_types.h"
#include "impeller/renderer/backend/metal/blit_command_mtl.h"
#include "impeller/renderer/backend/metal/device_buffer_mtl.h"
#include "impeller/renderer/backend/metal/formats_mtl.h"
#include "impeller/renderer/backend/metal/pipeline_mtl.h"
#include "impeller/renderer/backend/metal/sampler_mtl.h"
#include "impeller/renderer/backend/metal/texture_mtl.h"
#include "impeller/renderer/blit_command.h"
namespace impeller {
@@ -29,19 +29,10 @@ BlitPassMTL::BlitPassMTL(id<MTLCommandBuffer> buffer) : buffer_(buffer) {
if (!buffer_) {
return;
}
encoder_ = [buffer_ blitCommandEncoder];
#ifdef IMPELLER_DEBUG
is_metal_trace_active_ =
[[MTLCaptureManager sharedCaptureManager] isCapturing];
#endif // IMPELLER_DEBUG
is_valid_ = true;
}
BlitPassMTL::~BlitPassMTL() {
if (!did_finish_encoding_) {
[encoder_ endEncoding];
}
}
BlitPassMTL::~BlitPassMTL() = default;
bool BlitPassMTL::IsValid() const {
return is_valid_;
@@ -51,13 +42,49 @@ void BlitPassMTL::OnSetLabel(std::string label) {
if (label.empty()) {
return;
}
[encoder_ setLabel:@(label.c_str())];
label_ = std::move(label);
}
bool BlitPassMTL::EncodeCommands(
const std::shared_ptr<Allocator>& transients_allocator) const {
[encoder_ endEncoding];
did_finish_encoding_ = true;
TRACE_EVENT0("impeller", "BlitPassMTL::EncodeCommands");
if (!IsValid()) {
return false;
}
auto blit_command_encoder = [buffer_ blitCommandEncoder];
if (!blit_command_encoder) {
return false;
}
if (!label_.empty()) {
[blit_command_encoder setLabel:@(label_.c_str())];
}
// Success or failure, the pass must end. The buffer can only process one pass
// at a time.
fml::ScopedCleanupClosure auto_end(
[blit_command_encoder]() { [blit_command_encoder endEncoding]; });
return EncodeCommands(blit_command_encoder);
}
bool BlitPassMTL::EncodeCommands(id<MTLBlitCommandEncoder> encoder) const {
fml::closure pop_debug_marker = [encoder]() { [encoder popDebugGroup]; };
for (const auto& command : commands_) {
fml::ScopedCleanupClosure auto_pop_debug_marker(pop_debug_marker);
auto label = command->GetLabel();
if (!label.empty()) {
[encoder pushDebugGroup:@(label.c_str())];
} else {
auto_pop_debug_marker.Release();
}
if (!command->Encode(encoder)) {
return false;
}
}
return true;
}
@@ -68,44 +95,14 @@ bool BlitPassMTL::OnCopyTextureToTextureCommand(
IRect source_region,
IPoint destination_origin,
std::string label) {
auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture();
if (!source_mtl) {
return false;
}
auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture();
if (!destination_mtl) {
return false;
}
auto source_origin_mtl =
MTLOriginMake(source_region.GetX(), source_region.GetY(), 0);
auto source_size_mtl =
MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1);
auto destination_origin_mtl =
MTLOriginMake(destination_origin.x, destination_origin.y, 0);
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ pushDebugGroup:@(label.c_str())];
}
#endif // IMPELLER_DEBUG
[encoder_ copyFromTexture:source_mtl
sourceSlice:0
sourceLevel:0
sourceOrigin:source_origin_mtl
sourceSize:source_size_mtl
toTexture:destination_mtl
destinationSlice:0
destinationLevel:0
destinationOrigin:destination_origin_mtl];
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ popDebugGroup];
}
#endif // IMPELLER_DEBUG
auto command = std::make_unique<BlitCopyTextureToTextureCommandMTL>();
command->label = label;
command->source = std::move(source);
command->destination = std::move(destination);
command->source_region = source_region;
command->destination_origin = destination_origin;
commands_.emplace_back(std::move(command));
return true;
}
@@ -116,48 +113,14 @@ bool BlitPassMTL::OnCopyTextureToBufferCommand(
IRect source_region,
size_t destination_offset,
std::string label) {
auto source_mtl = TextureMTL::Cast(*source).GetMTLTexture();
if (!source_mtl) {
return false;
}
auto command = std::make_unique<BlitCopyTextureToBufferCommandMTL>();
command->label = label;
command->source = std::move(source);
command->destination = std::move(destination);
command->source_region = source_region;
command->destination_offset = destination_offset;
auto destination_mtl = DeviceBufferMTL::Cast(*destination).GetMTLBuffer();
if (!destination_mtl) {
return false;
}
auto source_origin_mtl =
MTLOriginMake(source_region.GetX(), source_region.GetY(), 0);
auto source_size_mtl =
MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1);
auto destination_bytes_per_pixel =
BytesPerPixelForPixelFormat(source->GetTextureDescriptor().format);
auto destination_bytes_per_row =
source_size_mtl.width * destination_bytes_per_pixel;
auto destination_bytes_per_image =
source_size_mtl.height * destination_bytes_per_row;
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ pushDebugGroup:@(label.c_str())];
}
#endif // IMPELLER_DEBUG
[encoder_ copyFromTexture:source_mtl
sourceSlice:0
sourceLevel:0
sourceOrigin:source_origin_mtl
sourceSize:source_size_mtl
toBuffer:destination_mtl
destinationOffset:destination_offset
destinationBytesPerRow:destination_bytes_per_row
destinationBytesPerImage:destination_bytes_per_image];
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ popDebugGroup];
}
#endif // IMPELLER_DEBUG
commands_.emplace_back(std::move(command));
return true;
}
@@ -167,67 +130,26 @@ bool BlitPassMTL::OnCopyBufferToTextureCommand(
IRect destination_region,
std::string label,
uint32_t slice) {
auto source_mtl = DeviceBufferMTL::Cast(*source.buffer).GetMTLBuffer();
if (!source_mtl) {
return false;
}
auto command = std::make_unique<BlitCopyBufferToTextureCommandMTL>();
command->label = std::move(label);
command->source = std::move(source);
command->destination = std::move(destination);
command->destination_region = destination_region;
command->slice = slice;
auto destination_mtl = TextureMTL::Cast(*destination).GetMTLTexture();
if (!destination_mtl) {
return false;
}
auto destination_origin_mtl =
MTLOriginMake(destination_region.GetX(), destination_region.GetY(), 0);
auto source_size_mtl = MTLSizeMake(destination_region.GetWidth(),
destination_region.GetHeight(), 1);
auto destination_bytes_per_pixel =
BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format);
auto source_bytes_per_row =
destination_region.GetWidth() * destination_bytes_per_pixel;
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ pushDebugGroup:@(label.c_str())];
}
#endif // IMPELLER_DEBUG
[encoder_
copyFromBuffer:source_mtl
sourceOffset:source.range.offset
sourceBytesPerRow:source_bytes_per_row
sourceBytesPerImage:
0 // 0 for 2D textures according to
// https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400752-copyfrombuffer
sourceSize:source_size_mtl
toTexture:destination_mtl
destinationSlice:slice
destinationLevel:0
destinationOrigin:destination_origin_mtl];
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ popDebugGroup];
}
#endif // IMPELLER_DEBUG
commands_.emplace_back(std::move(command));
return true;
}
// |BlitPass|
bool BlitPassMTL::OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
std::string label) {
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ pushDebugGroup:@(label.c_str())];
}
#endif // IMPELLER_DEBUG
auto result = TextureMTL::Cast(*texture).GenerateMipmap(encoder_);
#ifdef IMPELLER_DEBUG
if (is_metal_trace_active_) {
[encoder_ popDebugGroup];
}
#endif // IMPELLER_DEBUG
return result;
auto command = std::make_unique<BlitGenerateMipmapCommandMTL>();
command->label = label;
command->texture = std::move(texture);
commands_.emplace_back(std::move(command));
return true;
}
} // namespace impeller

View File

@@ -66,13 +66,11 @@ TEST_P(BlitPassTest, BlitPassesForMatchingFormats) {
TextureDescriptor src_desc;
src_desc.format = PixelFormat::kR8G8B8A8UNormInt;
src_desc.size = {100, 100};
src_desc.storage_mode = StorageMode::kHostVisible;
auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
TextureDescriptor dst_format;
dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
dst_format.size = {100, 100};
dst_format.storage_mode = StorageMode::kHostVisible;
auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
EXPECT_TRUE(blit_pass->AddCopy(src, dst));