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%
11.1 KB · 327 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLVertexDescriptor.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 VertexAttributeDescriptor;31class VertexAttributeDescriptorArray;32class VertexBufferLayoutDescriptor;33class VertexBufferLayoutDescriptorArray;34class VertexDescriptor;35_MTL_ENUM(NS::UInteger, VertexFormat) {36    VertexFormatInvalid = 0,37    VertexFormatUChar2 = 1,38    VertexFormatUChar3 = 2,39    VertexFormatUChar4 = 3,40    VertexFormatChar2 = 4,41    VertexFormatChar3 = 5,42    VertexFormatChar4 = 6,43    VertexFormatUChar2Normalized = 7,44    VertexFormatUChar3Normalized = 8,45    VertexFormatUChar4Normalized = 9,46    VertexFormatChar2Normalized = 10,47    VertexFormatChar3Normalized = 11,48    VertexFormatChar4Normalized = 12,49    VertexFormatUShort2 = 13,50    VertexFormatUShort3 = 14,51    VertexFormatUShort4 = 15,52    VertexFormatShort2 = 16,53    VertexFormatShort3 = 17,54    VertexFormatShort4 = 18,55    VertexFormatUShort2Normalized = 19,56    VertexFormatUShort3Normalized = 20,57    VertexFormatUShort4Normalized = 21,58    VertexFormatShort2Normalized = 22,59    VertexFormatShort3Normalized = 23,60    VertexFormatShort4Normalized = 24,61    VertexFormatHalf2 = 25,62    VertexFormatHalf3 = 26,63    VertexFormatHalf4 = 27,64    VertexFormatFloat = 28,65    VertexFormatFloat2 = 29,66    VertexFormatFloat3 = 30,67    VertexFormatFloat4 = 31,68    VertexFormatInt = 32,69    VertexFormatInt2 = 33,70    VertexFormatInt3 = 34,71    VertexFormatInt4 = 35,72    VertexFormatUInt = 36,73    VertexFormatUInt2 = 37,74    VertexFormatUInt3 = 38,75    VertexFormatUInt4 = 39,76    VertexFormatInt1010102Normalized = 40,77    VertexFormatUInt1010102Normalized = 41,78    VertexFormatUChar4Normalized_BGRA = 42,79    VertexFormatUChar = 45,80    VertexFormatChar = 46,81    VertexFormatUCharNormalized = 47,82    VertexFormatCharNormalized = 48,83    VertexFormatUShort = 49,84    VertexFormatShort = 50,85    VertexFormatUShortNormalized = 51,86    VertexFormatShortNormalized = 52,87    VertexFormatHalf = 53,88    VertexFormatFloatRG11B10 = 54,89    VertexFormatFloatRGB9E5 = 55,90};9192_MTL_ENUM(NS::UInteger, VertexStepFunction) {93    VertexStepFunctionConstant = 0,94    VertexStepFunctionPerVertex = 1,95    VertexStepFunctionPerInstance = 2,96    VertexStepFunctionPerPatch = 3,97    VertexStepFunctionPerPatchControlPoint = 4,98};99100static const NS::UInteger BufferLayoutStrideDynamic = NS::UIntegerMax;101102class VertexBufferLayoutDescriptor : public NS::Copying<VertexBufferLayoutDescriptor>103{104public:105    static VertexBufferLayoutDescriptor* alloc();106107    VertexBufferLayoutDescriptor*        init();108109    void                                 setStepFunction(MTL::VertexStepFunction stepFunction);110111    void                                 setStepRate(NS::UInteger stepRate);112113    void                                 setStride(NS::UInteger stride);114115    VertexStepFunction                   stepFunction() const;116117    NS::UInteger                         stepRate() const;118119    NS::UInteger                         stride() const;120};121class VertexBufferLayoutDescriptorArray : public NS::Referencing<VertexBufferLayoutDescriptorArray>122{123public:124    static VertexBufferLayoutDescriptorArray* alloc();125126    VertexBufferLayoutDescriptorArray*        init();127128    VertexBufferLayoutDescriptor*             object(NS::UInteger index);129    void                                      setObject(const MTL::VertexBufferLayoutDescriptor* bufferDesc, NS::UInteger index);130};131class VertexAttributeDescriptor : public NS::Copying<VertexAttributeDescriptor>132{133public:134    static VertexAttributeDescriptor* alloc();135136    NS::UInteger                      bufferIndex() const;137138    VertexFormat                      format() const;139140    VertexAttributeDescriptor*        init();141142    NS::UInteger                      offset() const;143144    void                              setBufferIndex(NS::UInteger bufferIndex);145146    void                              setFormat(MTL::VertexFormat format);147148    void                              setOffset(NS::UInteger offset);149};150class VertexAttributeDescriptorArray : public NS::Referencing<VertexAttributeDescriptorArray>151{152public:153    static VertexAttributeDescriptorArray* alloc();154155    VertexAttributeDescriptorArray*        init();156157    VertexAttributeDescriptor*             object(NS::UInteger index);158    void                                   setObject(const MTL::VertexAttributeDescriptor* attributeDesc, NS::UInteger index);159};160class VertexDescriptor : public NS::Copying<VertexDescriptor>161{162public:163    static VertexDescriptor*           alloc();164165    VertexAttributeDescriptorArray*    attributes() const;166167    VertexDescriptor*                  init();168169    VertexBufferLayoutDescriptorArray* layouts() const;170171    void                               reset();172173    static VertexDescriptor*           vertexDescriptor();174};175176}177178_MTL_INLINE MTL::VertexBufferLayoutDescriptor* MTL::VertexBufferLayoutDescriptor::alloc()179{180    return NS::Object::alloc<MTL::VertexBufferLayoutDescriptor>(_MTL_PRIVATE_CLS(MTLVertexBufferLayoutDescriptor));181}182183_MTL_INLINE MTL::VertexBufferLayoutDescriptor* MTL::VertexBufferLayoutDescriptor::init()184{185    return NS::Object::init<MTL::VertexBufferLayoutDescriptor>();186}187188_MTL_INLINE void MTL::VertexBufferLayoutDescriptor::setStepFunction(MTL::VertexStepFunction stepFunction)189{190    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setStepFunction_), stepFunction);191}192193_MTL_INLINE void MTL::VertexBufferLayoutDescriptor::setStepRate(NS::UInteger stepRate)194{195    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setStepRate_), stepRate);196}197198_MTL_INLINE void MTL::VertexBufferLayoutDescriptor::setStride(NS::UInteger stride)199{200    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setStride_), stride);201}202203_MTL_INLINE MTL::VertexStepFunction MTL::VertexBufferLayoutDescriptor::stepFunction() const204{205    return Object::sendMessage<MTL::VertexStepFunction>(this, _MTL_PRIVATE_SEL(stepFunction));206}207208_MTL_INLINE NS::UInteger MTL::VertexBufferLayoutDescriptor::stepRate() const209{210    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(stepRate));211}212213_MTL_INLINE NS::UInteger MTL::VertexBufferLayoutDescriptor::stride() const214{215    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(stride));216}217218_MTL_INLINE MTL::VertexBufferLayoutDescriptorArray* MTL::VertexBufferLayoutDescriptorArray::alloc()219{220    return NS::Object::alloc<MTL::VertexBufferLayoutDescriptorArray>(_MTL_PRIVATE_CLS(MTLVertexBufferLayoutDescriptorArray));221}222223_MTL_INLINE MTL::VertexBufferLayoutDescriptorArray* MTL::VertexBufferLayoutDescriptorArray::init()224{225    return NS::Object::init<MTL::VertexBufferLayoutDescriptorArray>();226}227228_MTL_INLINE MTL::VertexBufferLayoutDescriptor* MTL::VertexBufferLayoutDescriptorArray::object(NS::UInteger index)229{230    return Object::sendMessage<MTL::VertexBufferLayoutDescriptor*>(this, _MTL_PRIVATE_SEL(objectAtIndexedSubscript_), index);231}232233_MTL_INLINE void MTL::VertexBufferLayoutDescriptorArray::setObject(const MTL::VertexBufferLayoutDescriptor* bufferDesc, NS::UInteger index)234{235    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setObject_atIndexedSubscript_), bufferDesc, index);236}237238_MTL_INLINE MTL::VertexAttributeDescriptor* MTL::VertexAttributeDescriptor::alloc()239{240    return NS::Object::alloc<MTL::VertexAttributeDescriptor>(_MTL_PRIVATE_CLS(MTLVertexAttributeDescriptor));241}242243_MTL_INLINE NS::UInteger MTL::VertexAttributeDescriptor::bufferIndex() const244{245    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(bufferIndex));246}247248_MTL_INLINE MTL::VertexFormat MTL::VertexAttributeDescriptor::format() const249{250    return Object::sendMessage<MTL::VertexFormat>(this, _MTL_PRIVATE_SEL(format));251}252253_MTL_INLINE MTL::VertexAttributeDescriptor* MTL::VertexAttributeDescriptor::init()254{255    return NS::Object::init<MTL::VertexAttributeDescriptor>();256}257258_MTL_INLINE NS::UInteger MTL::VertexAttributeDescriptor::offset() const259{260    return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(offset));261}262263_MTL_INLINE void MTL::VertexAttributeDescriptor::setBufferIndex(NS::UInteger bufferIndex)264{265    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setBufferIndex_), bufferIndex);266}267268_MTL_INLINE void MTL::VertexAttributeDescriptor::setFormat(MTL::VertexFormat format)269{270    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setFormat_), format);271}272273_MTL_INLINE void MTL::VertexAttributeDescriptor::setOffset(NS::UInteger offset)274{275    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setOffset_), offset);276}277278_MTL_INLINE MTL::VertexAttributeDescriptorArray* MTL::VertexAttributeDescriptorArray::alloc()279{280    return NS::Object::alloc<MTL::VertexAttributeDescriptorArray>(_MTL_PRIVATE_CLS(MTLVertexAttributeDescriptorArray));281}282283_MTL_INLINE MTL::VertexAttributeDescriptorArray* MTL::VertexAttributeDescriptorArray::init()284{285    return NS::Object::init<MTL::VertexAttributeDescriptorArray>();286}287288_MTL_INLINE MTL::VertexAttributeDescriptor* MTL::VertexAttributeDescriptorArray::object(NS::UInteger index)289{290    return Object::sendMessage<MTL::VertexAttributeDescriptor*>(this, _MTL_PRIVATE_SEL(objectAtIndexedSubscript_), index);291}292293_MTL_INLINE void MTL::VertexAttributeDescriptorArray::setObject(const MTL::VertexAttributeDescriptor* attributeDesc, NS::UInteger index)294{295    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setObject_atIndexedSubscript_), attributeDesc, index);296}297298_MTL_INLINE MTL::VertexDescriptor* MTL::VertexDescriptor::alloc()299{300    return NS::Object::alloc<MTL::VertexDescriptor>(_MTL_PRIVATE_CLS(MTLVertexDescriptor));301}302303_MTL_INLINE MTL::VertexAttributeDescriptorArray* MTL::VertexDescriptor::attributes() const304{305    return Object::sendMessage<MTL::VertexAttributeDescriptorArray*>(this, _MTL_PRIVATE_SEL(attributes));306}307308_MTL_INLINE MTL::VertexDescriptor* MTL::VertexDescriptor::init()309{310    return NS::Object::init<MTL::VertexDescriptor>();311}312313_MTL_INLINE MTL::VertexBufferLayoutDescriptorArray* MTL::VertexDescriptor::layouts() const314{315    return Object::sendMessage<MTL::VertexBufferLayoutDescriptorArray*>(this, _MTL_PRIVATE_SEL(layouts));316}317318_MTL_INLINE void MTL::VertexDescriptor::reset()319{320    Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(reset));321}322323_MTL_INLINE MTL::VertexDescriptor* MTL::VertexDescriptor::vertexDescriptor()324{325    return Object::sendMessage<MTL::VertexDescriptor*>(_MTL_PRIVATE_CLS(MTLVertexDescriptor), _MTL_PRIVATE_SEL(vertexDescriptor));326}327