Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/metal-cpp/Metal/MTLCommandBuffer.hpp
21345 views
1
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
2
//
3
// Metal/MTLCommandBuffer.hpp
4
//
5
// Copyright 2020-2025 Apple Inc.
6
//
7
// Licensed under the Apache License, Version 2.0 (the "License");
8
// you may not use this file except in compliance with the License.
9
// You may obtain a copy of the License at
10
//
11
// http://www.apache.org/licenses/LICENSE-2.0
12
//
13
// Unless required by applicable law or agreed to in writing, software
14
// distributed under the License is distributed on an "AS IS" BASIS,
15
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
// See the License for the specific language governing permissions and
17
// limitations under the License.
18
//
19
//-------------------------------------------------------------------------------------------------------------------------------------------------------------
20
21
#pragma once
22
23
#include "../Foundation/Foundation.hpp"
24
#include "MTLDefines.hpp"
25
#include "MTLHeaderBridge.hpp"
26
#include "MTLPrivate.hpp"
27
#include <CoreFoundation/CoreFoundation.h>
28
#include <cstdint>
29
30
#include <functional>
31
32
namespace MTL
33
{
34
class AccelerationStructureCommandEncoder;
35
class AccelerationStructurePassDescriptor;
36
class BlitCommandEncoder;
37
class BlitPassDescriptor;
38
class CommandBuffer;
39
class CommandBufferDescriptor;
40
class CommandQueue;
41
class ComputeCommandEncoder;
42
class ComputePassDescriptor;
43
class Device;
44
class Drawable;
45
class Event;
46
class LogContainer;
47
class LogState;
48
class ParallelRenderCommandEncoder;
49
class RenderCommandEncoder;
50
class RenderPassDescriptor;
51
class ResidencySet;
52
class ResourceStateCommandEncoder;
53
class ResourceStatePassDescriptor;
54
_MTL_ENUM(NS::UInteger, CommandBufferStatus) {
55
CommandBufferStatusNotEnqueued = 0,
56
CommandBufferStatusEnqueued = 1,
57
CommandBufferStatusCommitted = 2,
58
CommandBufferStatusScheduled = 3,
59
CommandBufferStatusCompleted = 4,
60
CommandBufferStatusError = 5,
61
};
62
63
_MTL_ENUM(NS::UInteger, CommandBufferError) {
64
CommandBufferErrorNone = 0,
65
CommandBufferErrorInternal = 1,
66
CommandBufferErrorTimeout = 2,
67
CommandBufferErrorPageFault = 3,
68
CommandBufferErrorBlacklisted = 4,
69
CommandBufferErrorAccessRevoked = 4,
70
CommandBufferErrorNotPermitted = 7,
71
CommandBufferErrorOutOfMemory = 8,
72
CommandBufferErrorInvalidResource = 9,
73
CommandBufferErrorMemoryless = 10,
74
CommandBufferErrorDeviceRemoved = 11,
75
CommandBufferErrorStackOverflow = 12,
76
};
77
78
_MTL_ENUM(NS::Integer, CommandEncoderErrorState) {
79
CommandEncoderErrorStateUnknown = 0,
80
CommandEncoderErrorStateCompleted = 1,
81
CommandEncoderErrorStateAffected = 2,
82
CommandEncoderErrorStatePending = 3,
83
CommandEncoderErrorStateFaulted = 4,
84
};
85
86
_MTL_ENUM(NS::UInteger, DispatchType) {
87
DispatchTypeSerial = 0,
88
DispatchTypeConcurrent = 1,
89
};
90
91
_MTL_OPTIONS(NS::UInteger, CommandBufferErrorOption) {
92
CommandBufferErrorOptionNone = 0,
93
CommandBufferErrorOptionEncoderExecutionStatus = 1,
94
};
95
96
using CommandBufferHandler = void (^)(CommandBuffer*);
97
using HandlerFunction = std::function<void(CommandBuffer*)>;
98
99
class CommandBufferDescriptor : public NS::Copying<CommandBufferDescriptor>
100
{
101
public:
102
static CommandBufferDescriptor* alloc();
103
104
CommandBufferErrorOption errorOptions() const;
105
106
CommandBufferDescriptor* init();
107
108
LogState* logState() const;
109
110
bool retainedReferences() const;
111
112
void setErrorOptions(MTL::CommandBufferErrorOption errorOptions);
113
114
void setLogState(const MTL::LogState* logState);
115
116
void setRetainedReferences(bool retainedReferences);
117
};
118
class CommandBufferEncoderInfo : public NS::Referencing<CommandBufferEncoderInfo>
119
{
120
public:
121
NS::Array* debugSignposts() const;
122
123
CommandEncoderErrorState errorState() const;
124
125
NS::String* label() const;
126
};
127
class CommandBuffer : public NS::Referencing<CommandBuffer>
128
{
129
public:
130
CFTimeInterval GPUEndTime() const;
131
132
CFTimeInterval GPUStartTime() const;
133
134
AccelerationStructureCommandEncoder* accelerationStructureCommandEncoder();
135
AccelerationStructureCommandEncoder* accelerationStructureCommandEncoder(const MTL::AccelerationStructurePassDescriptor* descriptor);
136
137
void addCompletedHandler(const MTL::CommandBufferHandler block);
138
void addCompletedHandler(const MTL::HandlerFunction& function);
139
140
void addScheduledHandler(const MTL::CommandBufferHandler block);
141
void addScheduledHandler(const MTL::HandlerFunction& function);
142
143
BlitCommandEncoder* blitCommandEncoder();
144
BlitCommandEncoder* blitCommandEncoder(const MTL::BlitPassDescriptor* blitPassDescriptor);
145
146
CommandQueue* commandQueue() const;
147
148
void commit();
149
150
ComputeCommandEncoder* computeCommandEncoder(const MTL::ComputePassDescriptor* computePassDescriptor);
151
ComputeCommandEncoder* computeCommandEncoder();
152
ComputeCommandEncoder* computeCommandEncoder(MTL::DispatchType dispatchType);
153
154
Device* device() const;
155
156
void encodeSignalEvent(const MTL::Event* event, uint64_t value);
157
158
void encodeWait(const MTL::Event* event, uint64_t value);
159
160
void enqueue();
161
162
NS::Error* error() const;
163
CommandBufferErrorOption errorOptions() const;
164
165
CFTimeInterval kernelEndTime() const;
166
167
CFTimeInterval kernelStartTime() const;
168
169
NS::String* label() const;
170
171
LogContainer* logs() const;
172
173
ParallelRenderCommandEncoder* parallelRenderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor);
174
175
void popDebugGroup();
176
177
void presentDrawable(const MTL::Drawable* drawable);
178
void presentDrawableAfterMinimumDuration(const MTL::Drawable* drawable, CFTimeInterval duration);
179
180
void presentDrawableAtTime(const MTL::Drawable* drawable, CFTimeInterval presentationTime);
181
182
void pushDebugGroup(const NS::String* string);
183
184
RenderCommandEncoder* renderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor);
185
186
ResourceStateCommandEncoder* resourceStateCommandEncoder();
187
ResourceStateCommandEncoder* resourceStateCommandEncoder(const MTL::ResourceStatePassDescriptor* resourceStatePassDescriptor);
188
189
bool retainedReferences() const;
190
191
void setLabel(const NS::String* label);
192
193
CommandBufferStatus status() const;
194
195
void useResidencySet(const MTL::ResidencySet* residencySet);
196
void useResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count);
197
198
void waitUntilCompleted();
199
200
void waitUntilScheduled();
201
};
202
203
}
204
_MTL_INLINE MTL::CommandBufferDescriptor* MTL::CommandBufferDescriptor::alloc()
205
{
206
return NS::Object::alloc<MTL::CommandBufferDescriptor>(_MTL_PRIVATE_CLS(MTLCommandBufferDescriptor));
207
}
208
209
_MTL_INLINE MTL::CommandBufferErrorOption MTL::CommandBufferDescriptor::errorOptions() const
210
{
211
return Object::sendMessage<MTL::CommandBufferErrorOption>(this, _MTL_PRIVATE_SEL(errorOptions));
212
}
213
214
_MTL_INLINE MTL::CommandBufferDescriptor* MTL::CommandBufferDescriptor::init()
215
{
216
return NS::Object::init<MTL::CommandBufferDescriptor>();
217
}
218
219
_MTL_INLINE MTL::LogState* MTL::CommandBufferDescriptor::logState() const
220
{
221
return Object::sendMessage<MTL::LogState*>(this, _MTL_PRIVATE_SEL(logState));
222
}
223
224
_MTL_INLINE bool MTL::CommandBufferDescriptor::retainedReferences() const
225
{
226
return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(retainedReferences));
227
}
228
229
_MTL_INLINE void MTL::CommandBufferDescriptor::setErrorOptions(MTL::CommandBufferErrorOption errorOptions)
230
{
231
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setErrorOptions_), errorOptions);
232
}
233
234
_MTL_INLINE void MTL::CommandBufferDescriptor::setLogState(const MTL::LogState* logState)
235
{
236
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLogState_), logState);
237
}
238
239
_MTL_INLINE void MTL::CommandBufferDescriptor::setRetainedReferences(bool retainedReferences)
240
{
241
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setRetainedReferences_), retainedReferences);
242
}
243
244
_MTL_INLINE NS::Array* MTL::CommandBufferEncoderInfo::debugSignposts() const
245
{
246
return Object::sendMessage<NS::Array*>(this, _MTL_PRIVATE_SEL(debugSignposts));
247
}
248
249
_MTL_INLINE MTL::CommandEncoderErrorState MTL::CommandBufferEncoderInfo::errorState() const
250
{
251
return Object::sendMessage<MTL::CommandEncoderErrorState>(this, _MTL_PRIVATE_SEL(errorState));
252
}
253
254
_MTL_INLINE NS::String* MTL::CommandBufferEncoderInfo::label() const
255
{
256
return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));
257
}
258
259
_MTL_INLINE CFTimeInterval MTL::CommandBuffer::GPUEndTime() const
260
{
261
return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(GPUEndTime));
262
}
263
264
_MTL_INLINE CFTimeInterval MTL::CommandBuffer::GPUStartTime() const
265
{
266
return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(GPUStartTime));
267
}
268
269
_MTL_INLINE MTL::AccelerationStructureCommandEncoder* MTL::CommandBuffer::accelerationStructureCommandEncoder()
270
{
271
return Object::sendMessage<MTL::AccelerationStructureCommandEncoder*>(this, _MTL_PRIVATE_SEL(accelerationStructureCommandEncoder));
272
}
273
274
_MTL_INLINE MTL::AccelerationStructureCommandEncoder* MTL::CommandBuffer::accelerationStructureCommandEncoder(const MTL::AccelerationStructurePassDescriptor* descriptor)
275
{
276
return Object::sendMessage<MTL::AccelerationStructureCommandEncoder*>(this, _MTL_PRIVATE_SEL(accelerationStructureCommandEncoderWithDescriptor_), descriptor);
277
}
278
279
_MTL_INLINE void MTL::CommandBuffer::addCompletedHandler(const MTL::CommandBufferHandler block)
280
{
281
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addCompletedHandler_), block);
282
}
283
284
_MTL_INLINE void MTL::CommandBuffer::addCompletedHandler(const MTL::HandlerFunction& function)
285
{
286
__block HandlerFunction blockFunction = function;
287
addCompletedHandler(^(MTL::CommandBuffer* pCommandBuffer) { blockFunction(pCommandBuffer); });
288
}
289
290
_MTL_INLINE void MTL::CommandBuffer::addScheduledHandler(const MTL::CommandBufferHandler block)
291
{
292
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addScheduledHandler_), block);
293
}
294
295
_MTL_INLINE void MTL::CommandBuffer::addScheduledHandler(const MTL::HandlerFunction& function)
296
{
297
__block HandlerFunction blockFunction = function;
298
addScheduledHandler(^(MTL::CommandBuffer* pCommandBuffer) { blockFunction(pCommandBuffer); });
299
}
300
301
_MTL_INLINE MTL::BlitCommandEncoder* MTL::CommandBuffer::blitCommandEncoder()
302
{
303
return Object::sendMessage<MTL::BlitCommandEncoder*>(this, _MTL_PRIVATE_SEL(blitCommandEncoder));
304
}
305
306
_MTL_INLINE MTL::BlitCommandEncoder* MTL::CommandBuffer::blitCommandEncoder(const MTL::BlitPassDescriptor* blitPassDescriptor)
307
{
308
return Object::sendMessage<MTL::BlitCommandEncoder*>(this, _MTL_PRIVATE_SEL(blitCommandEncoderWithDescriptor_), blitPassDescriptor);
309
}
310
311
_MTL_INLINE MTL::CommandQueue* MTL::CommandBuffer::commandQueue() const
312
{
313
return Object::sendMessage<MTL::CommandQueue*>(this, _MTL_PRIVATE_SEL(commandQueue));
314
}
315
316
_MTL_INLINE void MTL::CommandBuffer::commit()
317
{
318
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(commit));
319
}
320
321
_MTL_INLINE MTL::ComputeCommandEncoder* MTL::CommandBuffer::computeCommandEncoder(const MTL::ComputePassDescriptor* computePassDescriptor)
322
{
323
return Object::sendMessage<MTL::ComputeCommandEncoder*>(this, _MTL_PRIVATE_SEL(computeCommandEncoderWithDescriptor_), computePassDescriptor);
324
}
325
326
_MTL_INLINE MTL::ComputeCommandEncoder* MTL::CommandBuffer::computeCommandEncoder()
327
{
328
return Object::sendMessage<MTL::ComputeCommandEncoder*>(this, _MTL_PRIVATE_SEL(computeCommandEncoder));
329
}
330
331
_MTL_INLINE MTL::ComputeCommandEncoder* MTL::CommandBuffer::computeCommandEncoder(MTL::DispatchType dispatchType)
332
{
333
return Object::sendMessage<MTL::ComputeCommandEncoder*>(this, _MTL_PRIVATE_SEL(computeCommandEncoderWithDispatchType_), dispatchType);
334
}
335
336
_MTL_INLINE MTL::Device* MTL::CommandBuffer::device() const
337
{
338
return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));
339
}
340
341
_MTL_INLINE void MTL::CommandBuffer::encodeSignalEvent(const MTL::Event* event, uint64_t value)
342
{
343
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(encodeSignalEvent_value_), event, value);
344
}
345
346
_MTL_INLINE void MTL::CommandBuffer::encodeWait(const MTL::Event* event, uint64_t value)
347
{
348
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(encodeWaitForEvent_value_), event, value);
349
}
350
351
_MTL_INLINE void MTL::CommandBuffer::enqueue()
352
{
353
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(enqueue));
354
}
355
356
_MTL_INLINE NS::Error* MTL::CommandBuffer::error() const
357
{
358
return Object::sendMessage<NS::Error*>(this, _MTL_PRIVATE_SEL(error));
359
}
360
361
_MTL_INLINE MTL::CommandBufferErrorOption MTL::CommandBuffer::errorOptions() const
362
{
363
return Object::sendMessage<MTL::CommandBufferErrorOption>(this, _MTL_PRIVATE_SEL(errorOptions));
364
}
365
366
_MTL_INLINE CFTimeInterval MTL::CommandBuffer::kernelEndTime() const
367
{
368
return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(kernelEndTime));
369
}
370
371
_MTL_INLINE CFTimeInterval MTL::CommandBuffer::kernelStartTime() const
372
{
373
return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(kernelStartTime));
374
}
375
376
_MTL_INLINE NS::String* MTL::CommandBuffer::label() const
377
{
378
return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));
379
}
380
381
_MTL_INLINE MTL::LogContainer* MTL::CommandBuffer::logs() const
382
{
383
return Object::sendMessage<MTL::LogContainer*>(this, _MTL_PRIVATE_SEL(logs));
384
}
385
386
_MTL_INLINE MTL::ParallelRenderCommandEncoder* MTL::CommandBuffer::parallelRenderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor)
387
{
388
return Object::sendMessage<MTL::ParallelRenderCommandEncoder*>(this, _MTL_PRIVATE_SEL(parallelRenderCommandEncoderWithDescriptor_), renderPassDescriptor);
389
}
390
391
_MTL_INLINE void MTL::CommandBuffer::popDebugGroup()
392
{
393
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(popDebugGroup));
394
}
395
396
_MTL_INLINE void MTL::CommandBuffer::presentDrawable(const MTL::Drawable* drawable)
397
{
398
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(presentDrawable_), drawable);
399
}
400
401
_MTL_INLINE void MTL::CommandBuffer::presentDrawableAfterMinimumDuration(const MTL::Drawable* drawable, CFTimeInterval duration)
402
{
403
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(presentDrawable_afterMinimumDuration_), drawable, duration);
404
}
405
406
_MTL_INLINE void MTL::CommandBuffer::presentDrawableAtTime(const MTL::Drawable* drawable, CFTimeInterval presentationTime)
407
{
408
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(presentDrawable_atTime_), drawable, presentationTime);
409
}
410
411
_MTL_INLINE void MTL::CommandBuffer::pushDebugGroup(const NS::String* string)
412
{
413
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(pushDebugGroup_), string);
414
}
415
416
_MTL_INLINE MTL::RenderCommandEncoder* MTL::CommandBuffer::renderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor)
417
{
418
return Object::sendMessage<MTL::RenderCommandEncoder*>(this, _MTL_PRIVATE_SEL(renderCommandEncoderWithDescriptor_), renderPassDescriptor);
419
}
420
421
_MTL_INLINE MTL::ResourceStateCommandEncoder* MTL::CommandBuffer::resourceStateCommandEncoder()
422
{
423
return Object::sendMessage<MTL::ResourceStateCommandEncoder*>(this, _MTL_PRIVATE_SEL(resourceStateCommandEncoder));
424
}
425
426
_MTL_INLINE MTL::ResourceStateCommandEncoder* MTL::CommandBuffer::resourceStateCommandEncoder(const MTL::ResourceStatePassDescriptor* resourceStatePassDescriptor)
427
{
428
return Object::sendMessage<MTL::ResourceStateCommandEncoder*>(this, _MTL_PRIVATE_SEL(resourceStateCommandEncoderWithDescriptor_), resourceStatePassDescriptor);
429
}
430
431
_MTL_INLINE bool MTL::CommandBuffer::retainedReferences() const
432
{
433
return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(retainedReferences));
434
}
435
436
_MTL_INLINE void MTL::CommandBuffer::setLabel(const NS::String* label)
437
{
438
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);
439
}
440
441
_MTL_INLINE MTL::CommandBufferStatus MTL::CommandBuffer::status() const
442
{
443
return Object::sendMessage<MTL::CommandBufferStatus>(this, _MTL_PRIVATE_SEL(status));
444
}
445
446
_MTL_INLINE void MTL::CommandBuffer::useResidencySet(const MTL::ResidencySet* residencySet)
447
{
448
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(useResidencySet_), residencySet);
449
}
450
451
_MTL_INLINE void MTL::CommandBuffer::useResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count)
452
{
453
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(useResidencySets_count_), residencySets, count);
454
}
455
456
_MTL_INLINE void MTL::CommandBuffer::waitUntilCompleted()
457
{
458
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitUntilCompleted));
459
}
460
461
_MTL_INLINE void MTL::CommandBuffer::waitUntilScheduled()
462
{
463
Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitUntilScheduled));
464
}
465
466