Path: blob/master/thirdparty/metal-cpp/Metal/MTLCommandBuffer.hpp
21345 views
//-------------------------------------------------------------------------------------------------------------------------------------------------------------1//2// Metal/MTLCommandBuffer.hpp3//4// Copyright 2020-2025 Apple Inc.5//6// Licensed under the Apache License, Version 2.0 (the "License");7// you may not use this file except in compliance with the License.8// You may obtain a copy of the License at9//10// http://www.apache.org/licenses/LICENSE-2.011//12// Unless required by applicable law or agreed to in writing, software13// distributed under the License is distributed on an "AS IS" BASIS,14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15// See the License for the specific language governing permissions and16// limitations under the License.17//18//-------------------------------------------------------------------------------------------------------------------------------------------------------------1920#pragma once2122#include "../Foundation/Foundation.hpp"23#include "MTLDefines.hpp"24#include "MTLHeaderBridge.hpp"25#include "MTLPrivate.hpp"26#include <CoreFoundation/CoreFoundation.h>27#include <cstdint>2829#include <functional>3031namespace MTL32{33class AccelerationStructureCommandEncoder;34class AccelerationStructurePassDescriptor;35class BlitCommandEncoder;36class BlitPassDescriptor;37class CommandBuffer;38class CommandBufferDescriptor;39class CommandQueue;40class ComputeCommandEncoder;41class ComputePassDescriptor;42class Device;43class Drawable;44class Event;45class LogContainer;46class LogState;47class ParallelRenderCommandEncoder;48class RenderCommandEncoder;49class RenderPassDescriptor;50class ResidencySet;51class ResourceStateCommandEncoder;52class ResourceStatePassDescriptor;53_MTL_ENUM(NS::UInteger, CommandBufferStatus) {54CommandBufferStatusNotEnqueued = 0,55CommandBufferStatusEnqueued = 1,56CommandBufferStatusCommitted = 2,57CommandBufferStatusScheduled = 3,58CommandBufferStatusCompleted = 4,59CommandBufferStatusError = 5,60};6162_MTL_ENUM(NS::UInteger, CommandBufferError) {63CommandBufferErrorNone = 0,64CommandBufferErrorInternal = 1,65CommandBufferErrorTimeout = 2,66CommandBufferErrorPageFault = 3,67CommandBufferErrorBlacklisted = 4,68CommandBufferErrorAccessRevoked = 4,69CommandBufferErrorNotPermitted = 7,70CommandBufferErrorOutOfMemory = 8,71CommandBufferErrorInvalidResource = 9,72CommandBufferErrorMemoryless = 10,73CommandBufferErrorDeviceRemoved = 11,74CommandBufferErrorStackOverflow = 12,75};7677_MTL_ENUM(NS::Integer, CommandEncoderErrorState) {78CommandEncoderErrorStateUnknown = 0,79CommandEncoderErrorStateCompleted = 1,80CommandEncoderErrorStateAffected = 2,81CommandEncoderErrorStatePending = 3,82CommandEncoderErrorStateFaulted = 4,83};8485_MTL_ENUM(NS::UInteger, DispatchType) {86DispatchTypeSerial = 0,87DispatchTypeConcurrent = 1,88};8990_MTL_OPTIONS(NS::UInteger, CommandBufferErrorOption) {91CommandBufferErrorOptionNone = 0,92CommandBufferErrorOptionEncoderExecutionStatus = 1,93};9495using CommandBufferHandler = void (^)(CommandBuffer*);96using HandlerFunction = std::function<void(CommandBuffer*)>;9798class CommandBufferDescriptor : public NS::Copying<CommandBufferDescriptor>99{100public:101static CommandBufferDescriptor* alloc();102103CommandBufferErrorOption errorOptions() const;104105CommandBufferDescriptor* init();106107LogState* logState() const;108109bool retainedReferences() const;110111void setErrorOptions(MTL::CommandBufferErrorOption errorOptions);112113void setLogState(const MTL::LogState* logState);114115void setRetainedReferences(bool retainedReferences);116};117class CommandBufferEncoderInfo : public NS::Referencing<CommandBufferEncoderInfo>118{119public:120NS::Array* debugSignposts() const;121122CommandEncoderErrorState errorState() const;123124NS::String* label() const;125};126class CommandBuffer : public NS::Referencing<CommandBuffer>127{128public:129CFTimeInterval GPUEndTime() const;130131CFTimeInterval GPUStartTime() const;132133AccelerationStructureCommandEncoder* accelerationStructureCommandEncoder();134AccelerationStructureCommandEncoder* accelerationStructureCommandEncoder(const MTL::AccelerationStructurePassDescriptor* descriptor);135136void addCompletedHandler(const MTL::CommandBufferHandler block);137void addCompletedHandler(const MTL::HandlerFunction& function);138139void addScheduledHandler(const MTL::CommandBufferHandler block);140void addScheduledHandler(const MTL::HandlerFunction& function);141142BlitCommandEncoder* blitCommandEncoder();143BlitCommandEncoder* blitCommandEncoder(const MTL::BlitPassDescriptor* blitPassDescriptor);144145CommandQueue* commandQueue() const;146147void commit();148149ComputeCommandEncoder* computeCommandEncoder(const MTL::ComputePassDescriptor* computePassDescriptor);150ComputeCommandEncoder* computeCommandEncoder();151ComputeCommandEncoder* computeCommandEncoder(MTL::DispatchType dispatchType);152153Device* device() const;154155void encodeSignalEvent(const MTL::Event* event, uint64_t value);156157void encodeWait(const MTL::Event* event, uint64_t value);158159void enqueue();160161NS::Error* error() const;162CommandBufferErrorOption errorOptions() const;163164CFTimeInterval kernelEndTime() const;165166CFTimeInterval kernelStartTime() const;167168NS::String* label() const;169170LogContainer* logs() const;171172ParallelRenderCommandEncoder* parallelRenderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor);173174void popDebugGroup();175176void presentDrawable(const MTL::Drawable* drawable);177void presentDrawableAfterMinimumDuration(const MTL::Drawable* drawable, CFTimeInterval duration);178179void presentDrawableAtTime(const MTL::Drawable* drawable, CFTimeInterval presentationTime);180181void pushDebugGroup(const NS::String* string);182183RenderCommandEncoder* renderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor);184185ResourceStateCommandEncoder* resourceStateCommandEncoder();186ResourceStateCommandEncoder* resourceStateCommandEncoder(const MTL::ResourceStatePassDescriptor* resourceStatePassDescriptor);187188bool retainedReferences() const;189190void setLabel(const NS::String* label);191192CommandBufferStatus status() const;193194void useResidencySet(const MTL::ResidencySet* residencySet);195void useResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count);196197void waitUntilCompleted();198199void waitUntilScheduled();200};201202}203_MTL_INLINE MTL::CommandBufferDescriptor* MTL::CommandBufferDescriptor::alloc()204{205return NS::Object::alloc<MTL::CommandBufferDescriptor>(_MTL_PRIVATE_CLS(MTLCommandBufferDescriptor));206}207208_MTL_INLINE MTL::CommandBufferErrorOption MTL::CommandBufferDescriptor::errorOptions() const209{210return Object::sendMessage<MTL::CommandBufferErrorOption>(this, _MTL_PRIVATE_SEL(errorOptions));211}212213_MTL_INLINE MTL::CommandBufferDescriptor* MTL::CommandBufferDescriptor::init()214{215return NS::Object::init<MTL::CommandBufferDescriptor>();216}217218_MTL_INLINE MTL::LogState* MTL::CommandBufferDescriptor::logState() const219{220return Object::sendMessage<MTL::LogState*>(this, _MTL_PRIVATE_SEL(logState));221}222223_MTL_INLINE bool MTL::CommandBufferDescriptor::retainedReferences() const224{225return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(retainedReferences));226}227228_MTL_INLINE void MTL::CommandBufferDescriptor::setErrorOptions(MTL::CommandBufferErrorOption errorOptions)229{230Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setErrorOptions_), errorOptions);231}232233_MTL_INLINE void MTL::CommandBufferDescriptor::setLogState(const MTL::LogState* logState)234{235Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLogState_), logState);236}237238_MTL_INLINE void MTL::CommandBufferDescriptor::setRetainedReferences(bool retainedReferences)239{240Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setRetainedReferences_), retainedReferences);241}242243_MTL_INLINE NS::Array* MTL::CommandBufferEncoderInfo::debugSignposts() const244{245return Object::sendMessage<NS::Array*>(this, _MTL_PRIVATE_SEL(debugSignposts));246}247248_MTL_INLINE MTL::CommandEncoderErrorState MTL::CommandBufferEncoderInfo::errorState() const249{250return Object::sendMessage<MTL::CommandEncoderErrorState>(this, _MTL_PRIVATE_SEL(errorState));251}252253_MTL_INLINE NS::String* MTL::CommandBufferEncoderInfo::label() const254{255return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));256}257258_MTL_INLINE CFTimeInterval MTL::CommandBuffer::GPUEndTime() const259{260return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(GPUEndTime));261}262263_MTL_INLINE CFTimeInterval MTL::CommandBuffer::GPUStartTime() const264{265return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(GPUStartTime));266}267268_MTL_INLINE MTL::AccelerationStructureCommandEncoder* MTL::CommandBuffer::accelerationStructureCommandEncoder()269{270return Object::sendMessage<MTL::AccelerationStructureCommandEncoder*>(this, _MTL_PRIVATE_SEL(accelerationStructureCommandEncoder));271}272273_MTL_INLINE MTL::AccelerationStructureCommandEncoder* MTL::CommandBuffer::accelerationStructureCommandEncoder(const MTL::AccelerationStructurePassDescriptor* descriptor)274{275return Object::sendMessage<MTL::AccelerationStructureCommandEncoder*>(this, _MTL_PRIVATE_SEL(accelerationStructureCommandEncoderWithDescriptor_), descriptor);276}277278_MTL_INLINE void MTL::CommandBuffer::addCompletedHandler(const MTL::CommandBufferHandler block)279{280Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addCompletedHandler_), block);281}282283_MTL_INLINE void MTL::CommandBuffer::addCompletedHandler(const MTL::HandlerFunction& function)284{285__block HandlerFunction blockFunction = function;286addCompletedHandler(^(MTL::CommandBuffer* pCommandBuffer) { blockFunction(pCommandBuffer); });287}288289_MTL_INLINE void MTL::CommandBuffer::addScheduledHandler(const MTL::CommandBufferHandler block)290{291Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(addScheduledHandler_), block);292}293294_MTL_INLINE void MTL::CommandBuffer::addScheduledHandler(const MTL::HandlerFunction& function)295{296__block HandlerFunction blockFunction = function;297addScheduledHandler(^(MTL::CommandBuffer* pCommandBuffer) { blockFunction(pCommandBuffer); });298}299300_MTL_INLINE MTL::BlitCommandEncoder* MTL::CommandBuffer::blitCommandEncoder()301{302return Object::sendMessage<MTL::BlitCommandEncoder*>(this, _MTL_PRIVATE_SEL(blitCommandEncoder));303}304305_MTL_INLINE MTL::BlitCommandEncoder* MTL::CommandBuffer::blitCommandEncoder(const MTL::BlitPassDescriptor* blitPassDescriptor)306{307return Object::sendMessage<MTL::BlitCommandEncoder*>(this, _MTL_PRIVATE_SEL(blitCommandEncoderWithDescriptor_), blitPassDescriptor);308}309310_MTL_INLINE MTL::CommandQueue* MTL::CommandBuffer::commandQueue() const311{312return Object::sendMessage<MTL::CommandQueue*>(this, _MTL_PRIVATE_SEL(commandQueue));313}314315_MTL_INLINE void MTL::CommandBuffer::commit()316{317Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(commit));318}319320_MTL_INLINE MTL::ComputeCommandEncoder* MTL::CommandBuffer::computeCommandEncoder(const MTL::ComputePassDescriptor* computePassDescriptor)321{322return Object::sendMessage<MTL::ComputeCommandEncoder*>(this, _MTL_PRIVATE_SEL(computeCommandEncoderWithDescriptor_), computePassDescriptor);323}324325_MTL_INLINE MTL::ComputeCommandEncoder* MTL::CommandBuffer::computeCommandEncoder()326{327return Object::sendMessage<MTL::ComputeCommandEncoder*>(this, _MTL_PRIVATE_SEL(computeCommandEncoder));328}329330_MTL_INLINE MTL::ComputeCommandEncoder* MTL::CommandBuffer::computeCommandEncoder(MTL::DispatchType dispatchType)331{332return Object::sendMessage<MTL::ComputeCommandEncoder*>(this, _MTL_PRIVATE_SEL(computeCommandEncoderWithDispatchType_), dispatchType);333}334335_MTL_INLINE MTL::Device* MTL::CommandBuffer::device() const336{337return Object::sendMessage<MTL::Device*>(this, _MTL_PRIVATE_SEL(device));338}339340_MTL_INLINE void MTL::CommandBuffer::encodeSignalEvent(const MTL::Event* event, uint64_t value)341{342Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(encodeSignalEvent_value_), event, value);343}344345_MTL_INLINE void MTL::CommandBuffer::encodeWait(const MTL::Event* event, uint64_t value)346{347Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(encodeWaitForEvent_value_), event, value);348}349350_MTL_INLINE void MTL::CommandBuffer::enqueue()351{352Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(enqueue));353}354355_MTL_INLINE NS::Error* MTL::CommandBuffer::error() const356{357return Object::sendMessage<NS::Error*>(this, _MTL_PRIVATE_SEL(error));358}359360_MTL_INLINE MTL::CommandBufferErrorOption MTL::CommandBuffer::errorOptions() const361{362return Object::sendMessage<MTL::CommandBufferErrorOption>(this, _MTL_PRIVATE_SEL(errorOptions));363}364365_MTL_INLINE CFTimeInterval MTL::CommandBuffer::kernelEndTime() const366{367return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(kernelEndTime));368}369370_MTL_INLINE CFTimeInterval MTL::CommandBuffer::kernelStartTime() const371{372return Object::sendMessage<CFTimeInterval>(this, _MTL_PRIVATE_SEL(kernelStartTime));373}374375_MTL_INLINE NS::String* MTL::CommandBuffer::label() const376{377return Object::sendMessage<NS::String*>(this, _MTL_PRIVATE_SEL(label));378}379380_MTL_INLINE MTL::LogContainer* MTL::CommandBuffer::logs() const381{382return Object::sendMessage<MTL::LogContainer*>(this, _MTL_PRIVATE_SEL(logs));383}384385_MTL_INLINE MTL::ParallelRenderCommandEncoder* MTL::CommandBuffer::parallelRenderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor)386{387return Object::sendMessage<MTL::ParallelRenderCommandEncoder*>(this, _MTL_PRIVATE_SEL(parallelRenderCommandEncoderWithDescriptor_), renderPassDescriptor);388}389390_MTL_INLINE void MTL::CommandBuffer::popDebugGroup()391{392Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(popDebugGroup));393}394395_MTL_INLINE void MTL::CommandBuffer::presentDrawable(const MTL::Drawable* drawable)396{397Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(presentDrawable_), drawable);398}399400_MTL_INLINE void MTL::CommandBuffer::presentDrawableAfterMinimumDuration(const MTL::Drawable* drawable, CFTimeInterval duration)401{402Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(presentDrawable_afterMinimumDuration_), drawable, duration);403}404405_MTL_INLINE void MTL::CommandBuffer::presentDrawableAtTime(const MTL::Drawable* drawable, CFTimeInterval presentationTime)406{407Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(presentDrawable_atTime_), drawable, presentationTime);408}409410_MTL_INLINE void MTL::CommandBuffer::pushDebugGroup(const NS::String* string)411{412Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(pushDebugGroup_), string);413}414415_MTL_INLINE MTL::RenderCommandEncoder* MTL::CommandBuffer::renderCommandEncoder(const MTL::RenderPassDescriptor* renderPassDescriptor)416{417return Object::sendMessage<MTL::RenderCommandEncoder*>(this, _MTL_PRIVATE_SEL(renderCommandEncoderWithDescriptor_), renderPassDescriptor);418}419420_MTL_INLINE MTL::ResourceStateCommandEncoder* MTL::CommandBuffer::resourceStateCommandEncoder()421{422return Object::sendMessage<MTL::ResourceStateCommandEncoder*>(this, _MTL_PRIVATE_SEL(resourceStateCommandEncoder));423}424425_MTL_INLINE MTL::ResourceStateCommandEncoder* MTL::CommandBuffer::resourceStateCommandEncoder(const MTL::ResourceStatePassDescriptor* resourceStatePassDescriptor)426{427return Object::sendMessage<MTL::ResourceStateCommandEncoder*>(this, _MTL_PRIVATE_SEL(resourceStateCommandEncoderWithDescriptor_), resourceStatePassDescriptor);428}429430_MTL_INLINE bool MTL::CommandBuffer::retainedReferences() const431{432return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(retainedReferences));433}434435_MTL_INLINE void MTL::CommandBuffer::setLabel(const NS::String* label)436{437Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setLabel_), label);438}439440_MTL_INLINE MTL::CommandBufferStatus MTL::CommandBuffer::status() const441{442return Object::sendMessage<MTL::CommandBufferStatus>(this, _MTL_PRIVATE_SEL(status));443}444445_MTL_INLINE void MTL::CommandBuffer::useResidencySet(const MTL::ResidencySet* residencySet)446{447Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(useResidencySet_), residencySet);448}449450_MTL_INLINE void MTL::CommandBuffer::useResidencySets(const MTL::ResidencySet* const residencySets[], NS::UInteger count)451{452Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(useResidencySets_count_), residencySets, count);453}454455_MTL_INLINE void MTL::CommandBuffer::waitUntilCompleted()456{457Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitUntilCompleted));458}459460_MTL_INLINE void MTL::CommandBuffer::waitUntilScheduled()461{462Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(waitUntilScheduled));463}464465466