Skip to content

Commit

Permalink
Wire up enhanced command buffer error reporting. (flutter#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored and dnfield committed Apr 27, 2022
1 parent f2bf47d commit 2dd3797
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
117 changes: 116 additions & 1 deletion impeller/renderer/backend/metal/command_buffer_mtl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@

namespace impeller {

id<MTLCommandBuffer> CreateCommandBuffer(id<MTLCommandQueue> queue) {
if (@available(iOS 14.0, macOS 11.0, *)) {
auto desc = [[MTLCommandBufferDescriptor alloc] init];
// Degrades CPU performance slightly but is well worth the cost for typical
// Impeller workloads.
desc.errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus;
return [queue commandBufferWithDescriptor:desc];
}
return [queue commandBuffer];
}

CommandBufferMTL::CommandBufferMTL(id<MTLCommandQueue> queue)
: buffer_([queue commandBuffer]) {
: buffer_(CreateCommandBuffer(queue)) {
if (!buffer_) {
return;
}
Expand Down Expand Up @@ -42,6 +53,109 @@
return CommandBufferMTL::Status::kError;
}

API_AVAILABLE(ios(14.0), macos(11.0))
NSString* MTLCommandEncoderErrorStateToString(
MTLCommandEncoderErrorState state) {
switch (state) {
case MTLCommandEncoderErrorStateUnknown:
return @"unknown";
case MTLCommandEncoderErrorStateCompleted:
return @"completed";
case MTLCommandEncoderErrorStateAffected:
return @"affected";
case MTLCommandEncoderErrorStatePending:
return @"pending";
case MTLCommandEncoderErrorStateFaulted:
return @"faulted";
}
return @"unknown";
}

static NSString* MTLCommandBufferErrorToString(MTLCommandBufferError code) {
switch (code) {
case MTLCommandBufferErrorNone:
return @"none";
case MTLCommandBufferErrorInternal:
return @"internal";
case MTLCommandBufferErrorTimeout:
return @"timeout";
case MTLCommandBufferErrorPageFault:
return @"page fault";
case MTLCommandBufferErrorAccessRevoked:
return @"access revoked / blacklisted";
case MTLCommandBufferErrorNotPermitted:
return @"not permitted";
case MTLCommandBufferErrorOutOfMemory:
return @"out of memory";
case MTLCommandBufferErrorInvalidResource:
return @"invalid resource";
case MTLCommandBufferErrorMemoryless:
return @"memory-less";
case MTLCommandBufferErrorStackOverflow:
return @"stack overflow";
default:
break;
}

return [NSString stringWithFormat:@"<unknown> %zu", code];
}

static void LogMTLCommandBufferErrorIfPresent(id<MTLCommandBuffer> buffer) {
if (!buffer) {
return;
}

if (buffer.status == MTLCommandBufferStatusCompleted) {
return;
}

std::stringstream stream;
stream << ">>>>>>>" << std::endl;
stream << "Impeller command buffer could not be committed!" << std::endl;

if (auto desc = buffer.error.localizedDescription) {
stream << desc.UTF8String << std::endl;
}

if (buffer.error) {
stream << "Domain: "
<< (buffer.error.domain.length > 0u ? buffer.error.domain.UTF8String
: "<unknown>")
<< " Code: "
<< MTLCommandBufferErrorToString(
static_cast<MTLCommandBufferError>(buffer.error.code))
.UTF8String
<< std::endl;
}

if (@available(iOS 14.0, macOS 11.0, *)) {
NSArray<id<MTLCommandBufferEncoderInfo>>* infos =
buffer.error.userInfo[MTLCommandBufferEncoderInfoErrorKey];
for (id<MTLCommandBufferEncoderInfo> info in infos) {
stream << (info.label.length > 0u ? info.label.UTF8String
: "<Unlabelled Render Pass>")
<< ": "
<< MTLCommandEncoderErrorStateToString(info.errorState).UTF8String
<< std::endl;

auto signposts = [info.debugSignposts componentsJoinedByString:@", "];
if (signposts.length > 0u) {
stream << signposts.UTF8String << std::endl;
}
}

for (id<MTLFunctionLog> log in buffer.logs) {
auto desc = log.description;
if (desc.length > 0u) {
stream << desc.UTF8String << std::endl;
}
}
}

stream << "<<<<<<<";
VALIDATION_LOG << stream.str();
}

bool CommandBufferMTL::SubmitCommands(CompletionCallback callback) {
if (!buffer_) {
// Already committed. This is caller error.
Expand All @@ -53,6 +167,7 @@

if (callback) {
[buffer_ addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
LogMTLCommandBufferErrorIfPresent(buffer);
callback(ToCommitResult(buffer.status));
}];
}
Expand Down
2 changes: 2 additions & 0 deletions impeller/renderer/renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ bool Renderer::Render(std::unique_ptr<Surface> surface,
return false;
}

render_pass->SetLabel("Onscreen Render Pass");

if (render_callback && !render_callback(*render_pass)) {
return false;
}
Expand Down

0 comments on commit 2dd3797

Please sign in to comment.