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%
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// Metal/MTLIndirectCommandBuffer.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"27#include "MTLResource.hpp"28#include "MTLTypes.hpp"29#include <cstdint>3031namespace MTL32{33class IndirectCommandBufferDescriptor;34class IndirectComputeCommand;35class IndirectRenderCommand;3637_MTL_OPTIONS(NS::UInteger, IndirectCommandType) {38 IndirectCommandTypeDraw = 1,39 IndirectCommandTypeDrawIndexed = 1 << 1,40 IndirectCommandTypeDrawPatches = 1 << 2,41 IndirectCommandTypeDrawIndexedPatches = 1 << 3,42 IndirectCommandTypeConcurrentDispatch = 1 << 5,43 IndirectCommandTypeConcurrentDispatchThreads = 1 << 6,44 IndirectCommandTypeDrawMeshThreadgroups = 1 << 7,45 IndirectCommandTypeDrawMeshThreads = 1 << 8,46};4748struct IndirectCommandBufferExecutionRange49{50 uint32_t location;51 uint32_t length;52} _MTL_PACKED;5354class IndirectCommandBufferDescriptor : public NS::Copying<IndirectCommandBufferDescriptor>55{56public:57 static IndirectCommandBufferDescriptor* alloc();5859 IndirectCommandType commandTypes() const;6061 bool inheritBuffers() const;6263 bool inheritCullMode() const;6465 bool inheritDepthBias() const;6667 bool inheritDepthClipMode() const;6869 bool inheritDepthStencilState() const;7071 bool inheritFrontFacingWinding() const;7273 bool inheritPipelineState() const;7475 bool inheritTriangleFillMode() const;7677 IndirectCommandBufferDescriptor* init();7879 NS::UInteger maxFragmentBufferBindCount() const;8081 NS::UInteger maxKernelBufferBindCount() const;8283 NS::UInteger maxKernelThreadgroupMemoryBindCount() const;8485 NS::UInteger maxMeshBufferBindCount() const;8687 NS::UInteger maxObjectBufferBindCount() const;8889 NS::UInteger maxObjectThreadgroupMemoryBindCount() const;9091 NS::UInteger maxVertexBufferBindCount() const;9293 void setCommandTypes(MTL::IndirectCommandType commandTypes);9495 void setInheritBuffers(bool inheritBuffers);9697 void setInheritCullMode(bool inheritCullMode);9899 void setInheritDepthBias(bool inheritDepthBias);100101 void setInheritDepthClipMode(bool inheritDepthClipMode);102103 void setInheritDepthStencilState(bool inheritDepthStencilState);104105 void setInheritFrontFacingWinding(bool inheritFrontFacingWinding);106107 void setInheritPipelineState(bool inheritPipelineState);108109 void setInheritTriangleFillMode(bool inheritTriangleFillMode);110111 void setMaxFragmentBufferBindCount(NS::UInteger maxFragmentBufferBindCount);112113 void setMaxKernelBufferBindCount(NS::UInteger maxKernelBufferBindCount);114115 void setMaxKernelThreadgroupMemoryBindCount(NS::UInteger maxKernelThreadgroupMemoryBindCount);116117 void setMaxMeshBufferBindCount(NS::UInteger maxMeshBufferBindCount);118119 void setMaxObjectBufferBindCount(NS::UInteger maxObjectBufferBindCount);120121 void setMaxObjectThreadgroupMemoryBindCount(NS::UInteger maxObjectThreadgroupMemoryBindCount);122123 void setMaxVertexBufferBindCount(NS::UInteger maxVertexBufferBindCount);124125 void setSupportColorAttachmentMapping(bool supportColorAttachmentMapping);126127 void setSupportDynamicAttributeStride(bool supportDynamicAttributeStride);128129 void setSupportRayTracing(bool supportRayTracing);130131 bool supportColorAttachmentMapping() const;132133 bool supportDynamicAttributeStride() const;134135 bool supportRayTracing() const;136};137class IndirectCommandBuffer : public NS::Referencing<IndirectCommandBuffer, Resource>138{139public:140 ResourceID gpuResourceID() const;141142 IndirectComputeCommand* indirectComputeCommand(NS::UInteger commandIndex);143144 IndirectRenderCommand* indirectRenderCommand(NS::UInteger commandIndex);145146 void reset(NS::Range range);147148 NS::UInteger size() const;149};150151}152153_MTL_INLINE MTL::IndirectCommandBufferDescriptor* MTL::IndirectCommandBufferDescriptor::alloc()154{155 return NS::Object::alloc<MTL::IndirectCommandBufferDescriptor>(_MTL_PRIVATE_CLS(MTLIndirectCommandBufferDescriptor));156}157158_MTL_INLINE MTL::IndirectCommandType MTL::IndirectCommandBufferDescriptor::commandTypes() const159{160 return Object::sendMessage<MTL::IndirectCommandType>(this, _MTL_PRIVATE_SEL(commandTypes));161}162163_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritBuffers() const164{165 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritBuffers));166}167168_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritCullMode() const169{170 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritCullMode));171}172173_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritDepthBias() const174{175 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritDepthBias));176}177178_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritDepthClipMode() const179{180 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritDepthClipMode));181}182183_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritDepthStencilState() const184{185 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritDepthStencilState));186}187188_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritFrontFacingWinding() const189{190 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritFrontFacingWinding));191}192193_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritPipelineState() const194{195 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritPipelineState));196}197198_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::inheritTriangleFillMode() const199{200 return Object::sendMessage<bool>(this, _MTL_PRIVATE_SEL(inheritTriangleFillMode));201}202203_MTL_INLINE MTL::IndirectCommandBufferDescriptor* MTL::IndirectCommandBufferDescriptor::init()204{205 return NS::Object::init<MTL::IndirectCommandBufferDescriptor>();206}207208_MTL_INLINE NS::UInteger MTL::IndirectCommandBufferDescriptor::maxFragmentBufferBindCount() const209{210 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxFragmentBufferBindCount));211}212213_MTL_INLINE NS::UInteger MTL::IndirectCommandBufferDescriptor::maxKernelBufferBindCount() const214{215 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxKernelBufferBindCount));216}217218_MTL_INLINE NS::UInteger MTL::IndirectCommandBufferDescriptor::maxKernelThreadgroupMemoryBindCount() const219{220 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxKernelThreadgroupMemoryBindCount));221}222223_MTL_INLINE NS::UInteger MTL::IndirectCommandBufferDescriptor::maxMeshBufferBindCount() const224{225 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxMeshBufferBindCount));226}227228_MTL_INLINE NS::UInteger MTL::IndirectCommandBufferDescriptor::maxObjectBufferBindCount() const229{230 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxObjectBufferBindCount));231}232233_MTL_INLINE NS::UInteger MTL::IndirectCommandBufferDescriptor::maxObjectThreadgroupMemoryBindCount() const234{235 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxObjectThreadgroupMemoryBindCount));236}237238_MTL_INLINE NS::UInteger MTL::IndirectCommandBufferDescriptor::maxVertexBufferBindCount() const239{240 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(maxVertexBufferBindCount));241}242243_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setCommandTypes(MTL::IndirectCommandType commandTypes)244{245 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setCommandTypes_), commandTypes);246}247248_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritBuffers(bool inheritBuffers)249{250 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritBuffers_), inheritBuffers);251}252253_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritCullMode(bool inheritCullMode)254{255 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritCullMode_), inheritCullMode);256}257258_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritDepthBias(bool inheritDepthBias)259{260 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritDepthBias_), inheritDepthBias);261}262263_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritDepthClipMode(bool inheritDepthClipMode)264{265 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritDepthClipMode_), inheritDepthClipMode);266}267268_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritDepthStencilState(bool inheritDepthStencilState)269{270 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritDepthStencilState_), inheritDepthStencilState);271}272273_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritFrontFacingWinding(bool inheritFrontFacingWinding)274{275 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritFrontFacingWinding_), inheritFrontFacingWinding);276}277278_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritPipelineState(bool inheritPipelineState)279{280 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritPipelineState_), inheritPipelineState);281}282283_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setInheritTriangleFillMode(bool inheritTriangleFillMode)284{285 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setInheritTriangleFillMode_), inheritTriangleFillMode);286}287288_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setMaxFragmentBufferBindCount(NS::UInteger maxFragmentBufferBindCount)289{290 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxFragmentBufferBindCount_), maxFragmentBufferBindCount);291}292293_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setMaxKernelBufferBindCount(NS::UInteger maxKernelBufferBindCount)294{295 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxKernelBufferBindCount_), maxKernelBufferBindCount);296}297298_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setMaxKernelThreadgroupMemoryBindCount(NS::UInteger maxKernelThreadgroupMemoryBindCount)299{300 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxKernelThreadgroupMemoryBindCount_), maxKernelThreadgroupMemoryBindCount);301}302303_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setMaxMeshBufferBindCount(NS::UInteger maxMeshBufferBindCount)304{305 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxMeshBufferBindCount_), maxMeshBufferBindCount);306}307308_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setMaxObjectBufferBindCount(NS::UInteger maxObjectBufferBindCount)309{310 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxObjectBufferBindCount_), maxObjectBufferBindCount);311}312313_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setMaxObjectThreadgroupMemoryBindCount(NS::UInteger maxObjectThreadgroupMemoryBindCount)314{315 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxObjectThreadgroupMemoryBindCount_), maxObjectThreadgroupMemoryBindCount);316}317318_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setMaxVertexBufferBindCount(NS::UInteger maxVertexBufferBindCount)319{320 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setMaxVertexBufferBindCount_), maxVertexBufferBindCount);321}322323_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setSupportColorAttachmentMapping(bool supportColorAttachmentMapping)324{325 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setSupportColorAttachmentMapping_), supportColorAttachmentMapping);326}327328_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setSupportDynamicAttributeStride(bool supportDynamicAttributeStride)329{330 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setSupportDynamicAttributeStride_), supportDynamicAttributeStride);331}332333_MTL_INLINE void MTL::IndirectCommandBufferDescriptor::setSupportRayTracing(bool supportRayTracing)334{335 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setSupportRayTracing_), supportRayTracing);336}337338_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::supportColorAttachmentMapping() const339{340 return Object::sendMessageSafe<bool>(this, _MTL_PRIVATE_SEL(supportColorAttachmentMapping));341}342343_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::supportDynamicAttributeStride() const344{345 return Object::sendMessageSafe<bool>(this, _MTL_PRIVATE_SEL(supportDynamicAttributeStride));346}347348_MTL_INLINE bool MTL::IndirectCommandBufferDescriptor::supportRayTracing() const349{350 return Object::sendMessageSafe<bool>(this, _MTL_PRIVATE_SEL(supportRayTracing));351}352353_MTL_INLINE MTL::ResourceID MTL::IndirectCommandBuffer::gpuResourceID() const354{355 return Object::sendMessage<MTL::ResourceID>(this, _MTL_PRIVATE_SEL(gpuResourceID));356}357358_MTL_INLINE MTL::IndirectComputeCommand* MTL::IndirectCommandBuffer::indirectComputeCommand(NS::UInteger commandIndex)359{360 return Object::sendMessage<MTL::IndirectComputeCommand*>(this, _MTL_PRIVATE_SEL(indirectComputeCommandAtIndex_), commandIndex);361}362363_MTL_INLINE MTL::IndirectRenderCommand* MTL::IndirectCommandBuffer::indirectRenderCommand(NS::UInteger commandIndex)364{365 return Object::sendMessage<MTL::IndirectRenderCommand*>(this, _MTL_PRIVATE_SEL(indirectRenderCommandAtIndex_), commandIndex);366}367368_MTL_INLINE void MTL::IndirectCommandBuffer::reset(NS::Range range)369{370 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(resetWithRange_), range);371}372373_MTL_INLINE NS::UInteger MTL::IndirectCommandBuffer::size() const374{375 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(size));376}377