forked from flutter-team-archive/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_buffer_mtl.mm
More file actions
249 lines (212 loc) · 6.96 KB
/
Copy pathcommand_buffer_mtl.mm
File metadata and controls
249 lines (212 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// 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/command_buffer_mtl.h"
#include "flutter/fml/make_copyable.h"
#include "flutter/fml/synchronization/semaphore.h"
#include "impeller/renderer/backend/metal/blit_pass_mtl.h"
#include "impeller/renderer/backend/metal/compute_pass_mtl.h"
#include "impeller/renderer/backend/metal/context_mtl.h"
#include "impeller/renderer/backend/metal/render_pass_mtl.h"
namespace impeller {
// NOLINTEND(readability-identifier-naming)
#if !(defined(TARGET_OS_TV) && TARGET_OS_TV)
API_AVAILABLE(ios(14.0), macos(11.0))
static 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";
}
#endif
// NOLINTEND(readability-identifier-naming)
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 MTLCommandBufferErrorNotPermitted:
return @"not permitted";
case MTLCommandBufferErrorOutOfMemory:
return @"out of memory";
case MTLCommandBufferErrorInvalidResource:
return @"invalid resource";
case MTLCommandBufferErrorMemoryless:
return @"memory-less";
default:
break;
}
return [NSString stringWithFormat:@"<unknown> %zu", code];
}
static bool LogMTLCommandBufferErrorIfPresent(id<MTLCommandBuffer> buffer) {
if (!buffer) {
return true;
}
if (buffer.status == MTLCommandBufferStatusCompleted) {
return true;
}
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 !(defined(TARGET_OS_TV) && TARGET_OS_TV)
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;
}
}
}
#endif
stream << "<<<<<<<";
VALIDATION_LOG << stream.str();
return false;
}
static id<MTLCommandBuffer> CreateCommandBuffer(id<MTLCommandQueue> queue) {
#if !defined(FLUTTER_RELEASE) && !(defined(TARGET_OS_TV) && TARGET_OS_TV)
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];
}
#endif // FLUTTER_RELEASE
return [queue commandBuffer];
}
CommandBufferMTL::CommandBufferMTL(const std::weak_ptr<const Context>& context,
id<MTLDevice> device,
id<MTLCommandQueue> queue)
: CommandBuffer(context),
buffer_(CreateCommandBuffer(queue)),
device_(device) {}
CommandBufferMTL::~CommandBufferMTL() = default;
bool CommandBufferMTL::IsValid() const {
return buffer_ != nil;
}
void CommandBufferMTL::SetLabel(std::string_view label) const {
#ifdef IMPELLER_DEBUG
if (label.empty()) {
return;
}
[buffer_ setLabel:@(label.data())];
#endif // IMPELLER_DEBUG
}
static CommandBuffer::Status ToCommitResult(MTLCommandBufferStatus status) {
switch (status) {
case MTLCommandBufferStatusCompleted:
return CommandBufferMTL::Status::kCompleted;
case MTLCommandBufferStatusEnqueued:
return CommandBufferMTL::Status::kPending;
default:
break;
}
return CommandBufferMTL::Status::kError;
}
bool CommandBufferMTL::OnSubmitCommands(CompletionCallback callback) {
auto context = context_.lock();
if (!context) {
return false;
}
#ifdef IMPELLER_DEBUG
ContextMTL::Cast(*context).GetGPUTracer()->RecordCmdBuffer(buffer_);
#endif // IMPELLER_DEBUG
if (callback) {
[buffer_
addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
[[maybe_unused]] auto result =
LogMTLCommandBufferErrorIfPresent(buffer);
FML_DCHECK(result)
<< "Must not have errors during command buffer submission.";
callback(ToCommitResult(buffer.status));
}];
}
[buffer_ commit];
buffer_ = nil;
return true;
}
void CommandBufferMTL::OnWaitUntilCompleted() {}
void CommandBufferMTL::OnWaitUntilScheduled() {}
std::shared_ptr<RenderPass> CommandBufferMTL::OnCreateRenderPass(
RenderTarget target) {
if (!buffer_) {
return nullptr;
}
auto context = context_.lock();
if (!context) {
return nullptr;
}
auto pass = std::shared_ptr<RenderPassMTL>(
new RenderPassMTL(context, target, buffer_));
if (!pass->IsValid()) {
return nullptr;
}
return pass;
}
std::shared_ptr<BlitPass> CommandBufferMTL::OnCreateBlitPass() {
if (!buffer_) {
return nullptr;
}
auto pass = std::shared_ptr<BlitPassMTL>(new BlitPassMTL(buffer_, device_));
if (!pass->IsValid()) {
return nullptr;
}
return pass;
}
std::shared_ptr<ComputePass> CommandBufferMTL::OnCreateComputePass() {
if (!buffer_) {
return nullptr;
}
auto context = context_.lock();
if (!context) {
return nullptr;
}
auto pass =
std::shared_ptr<ComputePassMTL>(new ComputePassMTL(context, buffer_));
if (!pass->IsValid()) {
return nullptr;
}
return pass;
}
} // namespace impeller