SPB Git

spb/forge Public MIT

Forge — LLM training from scratch in pure C++20 + Metal on Apple Silicon.

C++ 61.2% C 23% Python 7.6% TeX 7.2% CMake 1.1%
7.2 KB · 218 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLCaptureManager.hpp4//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 at10//11//     http://www.apache.org/licenses/LICENSE-2.012//13// Unless required by applicable law or agreed to in writing, software14// 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 and17// limitations under the License.18//19//-------------------------------------------------------------------------------------------------------------------------------------------------------------2021#pragma once2223#include "../Foundation/Foundation.hpp"24#include "MTLDefines.hpp"25#include "MTLHeaderBridge.hpp"26#include "MTLPrivate.hpp"2728namespace MTL29{30class CaptureDescriptor;31class CaptureManager;32class CaptureScope;33class CommandQueue;34class Device;35}3637namespace MTL438{39class CommandQueue;40}4142namespace MTL43{44_MTL_ENUM(NS::Integer, CaptureError) {45    CaptureErrorNotSupported = 1,46    CaptureErrorAlreadyCapturing = 2,47    CaptureErrorInvalidDescriptor = 3,48};4950_MTL_ENUM(NS::Integer, CaptureDestination) {51    CaptureDestinationDeveloperTools = 1,52    CaptureDestinationGPUTraceDocument = 2,53};5455class CaptureDescriptor : public NS::Copying<CaptureDescriptor>56{57public:58    static CaptureDescriptor* alloc();5960    NS::Object*               captureObject() const;6162    CaptureDestination        destination() const;6364    CaptureDescriptor*        init();6566    NS::URL*                  outputURL() const;6768    void                      setCaptureObject(NS::Object* captureObject);6970    void                      setDestination(MTL::CaptureDestination destination);7172    void                      setOutputURL(const NS::URL* outputURL);73};74class CaptureManager : public NS::Referencing<CaptureManager>75{76public:77    static CaptureManager* alloc();7879    CaptureScope*          defaultCaptureScope() const;8081    CaptureManager*        init();8283    bool                   isCapturing() const;8485    CaptureScope*          newCaptureScope(const MTL::Device* device);86    CaptureScope*          newCaptureScope(const MTL::CommandQueue* commandQueue);87    CaptureScope*          newCaptureScope(const MTL4::CommandQueue* commandQueue);8889    void                   setDefaultCaptureScope(const MTL::CaptureScope* defaultCaptureScope);9091    static CaptureManager* sharedCaptureManager();9293    bool                   startCapture(const MTL::CaptureDescriptor* descriptor, NS::Error** error);94    void                   startCapture(const MTL::Device* device);95    void                   startCapture(const MTL::CommandQueue* commandQueue);96    void                   startCapture(const MTL::CaptureScope* captureScope);9798    void                   stopCapture();99100    bool                   supportsDestination(MTL::CaptureDestination destination);101};102103}104_MTL_INLINE MTL::CaptureDescriptor* MTL::CaptureDescriptor::alloc()105{106    return NS::Object::alloc<MTL::CaptureDescriptor>(_MTL_PRIVATE_CLS(MTLCaptureDescriptor));107}108109_MTL_INLINE NS::Object* MTL::CaptureDescriptor::captureObject() const110{111    return Object::sendMessage<NS::Object*>(this, _MTL_PRIVATE_SEL(captureObject));112}113114_MTL_INLINE MTL::CaptureDestination MTL::CaptureDescriptor::destination() const115{116    return Object::sendMessage<MTL::CaptureDestination>(this, _MTL_PRIVATE_SEL(destination));117}118119_MTL_INLINE MTL::CaptureDescriptor* MTL::CaptureDescriptor::init()120{121    return NS::Object::init<MTL::CaptureDescriptor>();122}123124_MTL_INLINE NS::URL* MTL::CaptureDescriptor::outputURL() const125{126    return Object::sendMessage<NS::URL*>(this, _MTL_PRIVATE_SEL(outputURL));127}128129_MTL_INLINE void MTL::CaptureDescriptor::setCaptureObject(NS::Object* captureObject)130{131    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setCaptureObject_), captureObject);132}133134_MTL_INLINE void MTL::CaptureDescriptor::setDestination(MTL::CaptureDestination destination)135{136    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setDestination_), destination);137}138139_MTL_INLINE void MTL::CaptureDescriptor::setOutputURL(const NS::URL* outputURL)140{141    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setOutputURL_), outputURL);142}143144_MTL_INLINE MTL::CaptureManager* MTL::CaptureManager::alloc()145{146    return NS::Object::alloc<MTL::CaptureManager>(_MTL_PRIVATE_CLS(MTLCaptureManager));147}148149_MTL_INLINE MTL::CaptureScope* MTL::CaptureManager::defaultCaptureScope() const150{151    return Object::sendMessage<MTL::CaptureScope*>(this, _MTL_PRIVATE_SEL(defaultCaptureScope));152}153154_MTL_INLINE MTL::CaptureManager* MTL::CaptureManager::init()155{156    return NS::Object::init<MTL::CaptureManager>();157}158159_MTL_INLINE bool MTL::CaptureManager::isCapturing() const160{161    return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(isCapturing));162}163164_MTL_INLINE MTL::CaptureScope* MTL::CaptureManager::newCaptureScope(const MTL::Device* device)165{166    return Object::sendMessage<MTL::CaptureScope*>(this, _MTL_PRIVATE_SEL(newCaptureScopeWithDevice_), device);167}168169_MTL_INLINE MTL::CaptureScope* MTL::CaptureManager::newCaptureScope(const MTL::CommandQueue* commandQueue)170{171    return Object::sendMessage<MTL::CaptureScope*>(this, _MTL_PRIVATE_SEL(newCaptureScopeWithCommandQueue_), commandQueue);172}173174_MTL_INLINE MTL::CaptureScope* MTL::CaptureManager::newCaptureScope(const MTL4::CommandQueue* commandQueue)175{176    return Object::sendMessage<MTL::CaptureScope*>(this, _MTL_PRIVATE_SEL(newCaptureScopeWithMTL4CommandQueue_), commandQueue);177}178179_MTL_INLINE void MTL::CaptureManager::setDefaultCaptureScope(const MTL::CaptureScope* defaultCaptureScope)180{181    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setDefaultCaptureScope_), defaultCaptureScope);182}183184_MTL_INLINE MTL::CaptureManager* MTL::CaptureManager::sharedCaptureManager()185{186    return Object::sendMessage<MTL::CaptureManager*>(_MTL_PRIVATE_CLS(MTLCaptureManager), _MTL_PRIVATE_SEL(sharedCaptureManager));187}188189_MTL_INLINE bool MTL::CaptureManager::startCapture(const MTL::CaptureDescriptor* descriptor, NS::Error** error)190{191    return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(startCaptureWithDescriptor_error_), descriptor, error);192}193194_MTL_INLINE void MTL::CaptureManager::startCapture(const MTL::Device* device)195{196    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(startCaptureWithDevice_), device);197}198199_MTL_INLINE void MTL::CaptureManager::startCapture(const MTL::CommandQueue* commandQueue)200{201    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(startCaptureWithCommandQueue_), commandQueue);202}203204_MTL_INLINE void MTL::CaptureManager::startCapture(const MTL::CaptureScope* captureScope)205{206    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(startCaptureWithScope_), captureScope);207}208209_MTL_INLINE void MTL::CaptureManager::stopCapture()210{211    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(stopCapture));212}213214_MTL_INLINE bool MTL::CaptureManager::supportsDestination(MTL::CaptureDestination destination)215{216    return Object::sendMessageSafe<bool>(this, _MTL_PRIVATE_SEL(supportsDestination_), destination);217}218