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/MTLIntersectionFunctionTable.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 Buffer;34class FunctionHandle;35class IntersectionFunctionTableDescriptor;36class VisibleFunctionTable;3738_MTL_OPTIONS(NS::UInteger, IntersectionFunctionSignature) {39 IntersectionFunctionSignatureNone = 0,40 IntersectionFunctionSignatureInstancing = 1,41 IntersectionFunctionSignatureTriangleData = 1 << 1,42 IntersectionFunctionSignatureWorldSpaceData = 1 << 2,43 IntersectionFunctionSignatureInstanceMotion = 1 << 3,44 IntersectionFunctionSignaturePrimitiveMotion = 1 << 4,45 IntersectionFunctionSignatureExtendedLimits = 1 << 5,46 IntersectionFunctionSignatureMaxLevels = 1 << 6,47 IntersectionFunctionSignatureCurveData = 1 << 7,48 IntersectionFunctionSignatureIntersectionFunctionBuffer = 1 << 8,49 IntersectionFunctionSignatureUserData = 1 << 9,50};5152struct IntersectionFunctionBufferArguments53{54 uint64_t intersectionFunctionBuffer;55 uint64_t intersectionFunctionBufferSize;56 uint64_t intersectionFunctionStride;57} _MTL_PACKED;5859class IntersectionFunctionTableDescriptor : public NS::Copying<IntersectionFunctionTableDescriptor>60{61public:62 static IntersectionFunctionTableDescriptor* alloc();6364 NS::UInteger functionCount() const;6566 IntersectionFunctionTableDescriptor* init();6768 static IntersectionFunctionTableDescriptor* intersectionFunctionTableDescriptor();6970 void setFunctionCount(NS::UInteger functionCount);71};72class IntersectionFunctionTable : public NS::Referencing<IntersectionFunctionTable, Resource>73{74public:75 ResourceID gpuResourceID() const;7677 void setBuffer(const MTL::Buffer* buffer, NS::UInteger offset, NS::UInteger index);78 void setBuffers(const MTL::Buffer* const buffers[], const NS::UInteger offsets[], NS::Range range);7980 void setFunction(const MTL::FunctionHandle* function, NS::UInteger index);81 void setFunctions(const MTL::FunctionHandle* const functions[], NS::Range range);8283 void setOpaqueCurveIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::UInteger index);84 void setOpaqueCurveIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::Range range);8586 void setOpaqueTriangleIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::UInteger index);87 void setOpaqueTriangleIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::Range range);8889 void setVisibleFunctionTable(const MTL::VisibleFunctionTable* functionTable, NS::UInteger bufferIndex);90 void setVisibleFunctionTables(const MTL::VisibleFunctionTable* const functionTables[], NS::Range bufferRange);91};9293}9495_MTL_INLINE MTL::IntersectionFunctionTableDescriptor* MTL::IntersectionFunctionTableDescriptor::alloc()96{97 return NS::Object::alloc<MTL::IntersectionFunctionTableDescriptor>(_MTL_PRIVATE_CLS(MTLIntersectionFunctionTableDescriptor));98}99100_MTL_INLINE NS::UInteger MTL::IntersectionFunctionTableDescriptor::functionCount() const101{102 return Object::sendMessage<NS::UInteger>(this, _MTL_PRIVATE_SEL(functionCount));103}104105_MTL_INLINE MTL::IntersectionFunctionTableDescriptor* MTL::IntersectionFunctionTableDescriptor::init()106{107 return NS::Object::init<MTL::IntersectionFunctionTableDescriptor>();108}109110_MTL_INLINE MTL::IntersectionFunctionTableDescriptor* MTL::IntersectionFunctionTableDescriptor::intersectionFunctionTableDescriptor()111{112 return Object::sendMessage<MTL::IntersectionFunctionTableDescriptor*>(_MTL_PRIVATE_CLS(MTLIntersectionFunctionTableDescriptor), _MTL_PRIVATE_SEL(intersectionFunctionTableDescriptor));113}114115_MTL_INLINE void MTL::IntersectionFunctionTableDescriptor::setFunctionCount(NS::UInteger functionCount)116{117 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setFunctionCount_), functionCount);118}119120_MTL_INLINE MTL::ResourceID MTL::IntersectionFunctionTable::gpuResourceID() const121{122 return Object::sendMessage<MTL::ResourceID>(this, _MTL_PRIVATE_SEL(gpuResourceID));123}124125_MTL_INLINE void MTL::IntersectionFunctionTable::setBuffer(const MTL::Buffer* buffer, NS::UInteger offset, NS::UInteger index)126{127 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setBuffer_offset_atIndex_), buffer, offset, index);128}129130_MTL_INLINE void MTL::IntersectionFunctionTable::setBuffers(const MTL::Buffer* const buffers[], const NS::UInteger offsets[], NS::Range range)131{132 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setBuffers_offsets_withRange_), buffers, offsets, range);133}134135_MTL_INLINE void MTL::IntersectionFunctionTable::setFunction(const MTL::FunctionHandle* function, NS::UInteger index)136{137 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setFunction_atIndex_), function, index);138}139140_MTL_INLINE void MTL::IntersectionFunctionTable::setFunctions(const MTL::FunctionHandle* const functions[], NS::Range range)141{142 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setFunctions_withRange_), functions, range);143}144145_MTL_INLINE void MTL::IntersectionFunctionTable::setOpaqueCurveIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::UInteger index)146{147 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setOpaqueCurveIntersectionFunctionWithSignature_atIndex_), signature, index);148}149150_MTL_INLINE void MTL::IntersectionFunctionTable::setOpaqueCurveIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::Range range)151{152 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setOpaqueCurveIntersectionFunctionWithSignature_withRange_), signature, range);153}154155_MTL_INLINE void MTL::IntersectionFunctionTable::setOpaqueTriangleIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::UInteger index)156{157 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setOpaqueTriangleIntersectionFunctionWithSignature_atIndex_), signature, index);158}159160_MTL_INLINE void MTL::IntersectionFunctionTable::setOpaqueTriangleIntersectionFunction(MTL::IntersectionFunctionSignature signature, NS::Range range)161{162 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setOpaqueTriangleIntersectionFunctionWithSignature_withRange_), signature, range);163}164165_MTL_INLINE void MTL::IntersectionFunctionTable::setVisibleFunctionTable(const MTL::VisibleFunctionTable* functionTable, NS::UInteger bufferIndex)166{167 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setVisibleFunctionTable_atBufferIndex_), functionTable, bufferIndex);168}169170_MTL_INLINE void MTL::IntersectionFunctionTable::setVisibleFunctionTables(const MTL::VisibleFunctionTable* const functionTables[], NS::Range bufferRange)171{172 Object::sendMessage<void>(this, _MTL_PRIVATE_SEL(setVisibleFunctionTables_withBufferRange_), functionTables, bufferRange);173}174