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%
19.3 KB · 398 lines cpp
Raw Blame History
1//-------------------------------------------------------------------------------------------------------------------------------------------------------------2//3// MetalFX/MTLFXSpatialScaler.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//-------------------------------------------------------------------------------------------------------------------------------------------------------------2425#include "MTLFXDefines.hpp"26#include "MTLFXPrivate.hpp"2728#include "../Metal/Metal.hpp"2930//-------------------------------------------------------------------------------------------------------------------------------------------------------------3132namespace MTL4FX33{34    class SpatialScaler;35}3637namespace MTLFX38{39    _MTLFX_ENUM( NS::Integer, SpatialScalerColorProcessingMode )40    {41        SpatialScalerColorProcessingModePerceptual  = 0,42        SpatialScalerColorProcessingModeLinear      = 1,43        SpatialScalerColorProcessingModeHDR         = 244    };4546    class SpatialScalerDescriptor : public NS::Copying< SpatialScalerDescriptor >47    {48    public:49        static class SpatialScalerDescriptor*       alloc();50        class SpatialScalerDescriptor*              init();5152        MTL::PixelFormat                            colorTextureFormat() const;53        void                                        setColorTextureFormat( MTL::PixelFormat format );5455        MTL::PixelFormat                            outputTextureFormat() const;56        void                                        setOutputTextureFormat( MTL::PixelFormat format );5758        NS::UInteger                                inputWidth() const;59        void                                        setInputWidth( NS::UInteger width );6061        NS::UInteger                                inputHeight() const;62        void                                        setInputHeight( NS::UInteger height );6364        NS::UInteger                                outputWidth() const;65        void                                        setOutputWidth( NS::UInteger width );6667        NS::UInteger                                outputHeight() const;68        void                                        setOutputHeight( NS::UInteger height );6970        SpatialScalerColorProcessingMode            colorProcessingMode() const;71        void                                        setColorProcessingMode( SpatialScalerColorProcessingMode mode );7273        class SpatialScaler*                        newSpatialScaler( const MTL::Device* pDevice ) const;74        MTL4FX::SpatialScaler*                      newSpatialScaler( const MTL::Device* pDevice, const MTL4::Compiler* pCompiler ) const;7576        static bool                                 supportsDevice( const MTL::Device* pDevice);77        static bool                                 supportsMetal4FX( const MTL::Device* pDevice );78    };7980    class SpatialScalerBase : public NS::Referencing< SpatialScaler >81    {82    public:83        MTL::TextureUsage                           colorTextureUsage() const;84        MTL::TextureUsage                           outputTextureUsage() const;8586        NS::UInteger                                inputContentWidth() const;87        void                                        setInputContentWidth( NS::UInteger width );8889        NS::UInteger                                inputContentHeight() const;90        void                                        setInputContentHeight( NS::UInteger height );9192        MTL::Texture*                               colorTexture() const;93        void                                        setColorTexture( MTL::Texture* pTexture );9495        MTL::Texture*                               outputTexture() const;96        void                                        setOutputTexture( MTL::Texture* pTexture );9798        MTL::PixelFormat                            colorTextureFormat() const;99        MTL::PixelFormat                            outputTextureFormat() const;100        NS::UInteger                                inputWidth() const;101        NS::UInteger                                inputHeight() const;102        NS::UInteger                                outputWidth() const;103        NS::UInteger                                outputHeight() const;104        SpatialScalerColorProcessingMode            colorProcessingMode() const;105106        MTL::Fence*                                 fence() const;107        void                                        setFence( MTL::Fence* pFence );108    };109110    class SpatialScaler : public NS::Referencing< SpatialScaler, SpatialScalerBase >111    {112    public:113        void                                        encodeToCommandBuffer( MTL::CommandBuffer* pCommandBuffer );114    };115}116117//-------------------------------------------------------------------------------------------------------------------------------------------------------------118119_MTLFX_INLINE MTLFX::SpatialScalerDescriptor* MTLFX::SpatialScalerDescriptor::alloc()120{121    return NS::Object::alloc< SpatialScalerDescriptor >( _MTLFX_PRIVATE_CLS( MTLFXSpatialScalerDescriptor ) );122}123124//-------------------------------------------------------------------------------------------------------------------------------------------------------------125126_MTLFX_INLINE MTLFX::SpatialScalerDescriptor* MTLFX::SpatialScalerDescriptor::init()127{128    return NS::Object::init< SpatialScalerDescriptor >();129}130131//-------------------------------------------------------------------------------------------------------------------------------------------------------------132133_MTLFX_INLINE MTL::PixelFormat MTLFX::SpatialScalerDescriptor::colorTextureFormat() const134{135    return Object::sendMessage< MTL::PixelFormat >( this, _MTLFX_PRIVATE_SEL( colorTextureFormat ) );136}137138//-------------------------------------------------------------------------------------------------------------------------------------------------------------139140_MTLFX_INLINE void MTLFX::SpatialScalerDescriptor::setColorTextureFormat( MTL::PixelFormat format )141{142    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setColorTextureFormat_ ), format );143}144145//-------------------------------------------------------------------------------------------------------------------------------------------------------------146147_MTLFX_INLINE MTL::PixelFormat MTLFX::SpatialScalerDescriptor::outputTextureFormat() const148{149    return Object::sendMessage< MTL::PixelFormat >( this, _MTLFX_PRIVATE_SEL( outputTextureFormat ) );150}151152//-------------------------------------------------------------------------------------------------------------------------------------------------------------153154_MTLFX_INLINE void MTLFX::SpatialScalerDescriptor::setOutputTextureFormat( MTL::PixelFormat format )155{156    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setOutputTextureFormat_ ), format );157}158159//-------------------------------------------------------------------------------------------------------------------------------------------------------------160161_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerDescriptor::inputWidth() const162{163    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( inputWidth ) );164}165166//-------------------------------------------------------------------------------------------------------------------------------------------------------------167168_MTLFX_INLINE void MTLFX::SpatialScalerDescriptor::setInputWidth( NS::UInteger width )169{170    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setInputWidth_ ), width );171}172173//-------------------------------------------------------------------------------------------------------------------------------------------------------------174175_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerDescriptor::inputHeight() const176{177    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( inputHeight ) );178}179180//-------------------------------------------------------------------------------------------------------------------------------------------------------------181182_MTLFX_INLINE void MTLFX::SpatialScalerDescriptor::setInputHeight( NS::UInteger height )183{184    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setInputHeight_ ), height );185}186187//-------------------------------------------------------------------------------------------------------------------------------------------------------------188189_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerDescriptor::outputWidth() const190{191    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( outputWidth ) );192}193194//-------------------------------------------------------------------------------------------------------------------------------------------------------------195196_MTLFX_INLINE void MTLFX::SpatialScalerDescriptor::setOutputWidth( NS::UInteger width )197{198    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setOutputWidth_ ), width );199}200201//-------------------------------------------------------------------------------------------------------------------------------------------------------------202203_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerDescriptor::outputHeight() const204{205    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( outputHeight ) );206}207208//-------------------------------------------------------------------------------------------------------------------------------------------------------------209210_MTLFX_INLINE void MTLFX::SpatialScalerDescriptor::setOutputHeight( NS::UInteger height )211{212    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setOutputHeight_ ), height );213}214215//-------------------------------------------------------------------------------------------------------------------------------------------------------------216217_MTLFX_INLINE MTLFX::SpatialScalerColorProcessingMode MTLFX::SpatialScalerDescriptor::colorProcessingMode() const218{219    return Object::sendMessage< SpatialScalerColorProcessingMode >( this, _MTLFX_PRIVATE_SEL( colorProcessingMode ) );220}221222//-------------------------------------------------------------------------------------------------------------------------------------------------------------223224_MTLFX_INLINE void MTLFX::SpatialScalerDescriptor::setColorProcessingMode( SpatialScalerColorProcessingMode mode )225{226    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setColorProcessingMode_ ), mode );227}228229//-------------------------------------------------------------------------------------------------------------------------------------------------------------230231_MTLFX_INLINE MTLFX::SpatialScaler* MTLFX::SpatialScalerDescriptor::newSpatialScaler( const MTL::Device* pDevice ) const232{233    return Object::sendMessage< SpatialScaler* >( this, _MTLFX_PRIVATE_SEL( newSpatialScalerWithDevice_ ), pDevice );234}235236//-------------------------------------------------------------------------------------------------------------------------------------------------------------237238_MTLFX_INLINE MTL4FX::SpatialScaler* MTLFX::SpatialScalerDescriptor::newSpatialScaler( const MTL::Device* pDevice, const MTL4::Compiler* pCompiler ) const239{240    return Object::sendMessage< MTL4FX::SpatialScaler* >( this, _MTLFX_PRIVATE_SEL( newSpatialScalerWithDevice_compiler_ ), pDevice, pCompiler );241}242243//-------------------------------------------------------------------------------------------------------------------------------------------------------------244245_MTLFX_INLINE bool MTLFX::SpatialScalerDescriptor::supportsDevice( const MTL::Device* pDevice )246{247    return Object::sendMessageSafe< bool >( _NS_PRIVATE_CLS( MTLFXSpatialScalerDescriptor ), _MTLFX_PRIVATE_SEL( supportsDevice_ ), pDevice );248}249250//-------------------------------------------------------------------------------------------------------------------------------------------------------------251252_MTLFX_INLINE bool MTLFX::SpatialScalerDescriptor::supportsMetal4FX( const MTL::Device* pDevice )253{254    return Object::sendMessageSafe< bool >( _NS_PRIVATE_CLS( MTLFXSpatialScalerDescriptor ), _MTLFX_PRIVATE_SEL( supportsMetal4FX_ ), pDevice );255}256257//-------------------------------------------------------------------------------------------------------------------------------------------------------------258259_MTLFX_INLINE MTL::TextureUsage MTLFX::SpatialScalerBase::colorTextureUsage() const260{261    return Object::sendMessage< MTL::TextureUsage >( this, _MTLFX_PRIVATE_SEL( colorTextureUsage ) );262}263264//-------------------------------------------------------------------------------------------------------------------------------------------------------------265266_MTLFX_INLINE MTL::TextureUsage MTLFX::SpatialScalerBase::outputTextureUsage() const267{268    return Object::sendMessage< MTL::TextureUsage >( this, _MTLFX_PRIVATE_SEL( outputTextureUsage ) );269}270271//-------------------------------------------------------------------------------------------------------------------------------------------------------------272273_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerBase::inputContentWidth() const274{275    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( inputContentWidth ) );276}277278//-------------------------------------------------------------------------------------------------------------------------------------------------------------279280_MTLFX_INLINE void MTLFX::SpatialScalerBase::setInputContentWidth( NS::UInteger width )281{282    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setInputContentWidth_ ), width );283}284285//-------------------------------------------------------------------------------------------------------------------------------------------------------------286287_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerBase::inputContentHeight() const288{289    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( inputContentHeight ) );290}291292//-------------------------------------------------------------------------------------------------------------------------------------------------------------293294_MTLFX_INLINE void MTLFX::SpatialScalerBase::setInputContentHeight( NS::UInteger height )295{296    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setInputContentHeight_ ), height );297}298299//-------------------------------------------------------------------------------------------------------------------------------------------------------------300301_MTLFX_INLINE MTL::Texture* MTLFX::SpatialScalerBase::colorTexture() const302{303    return Object::sendMessage< MTL::Texture* >( this, _MTLFX_PRIVATE_SEL( colorTexture ) );304}305306//-------------------------------------------------------------------------------------------------------------------------------------------------------------307308_MTLFX_INLINE void MTLFX::SpatialScalerBase::setColorTexture( MTL::Texture* pTexture )309{310    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setColorTexture_ ), pTexture );311}312313//-------------------------------------------------------------------------------------------------------------------------------------------------------------314315_MTLFX_INLINE MTL::Texture* MTLFX::SpatialScalerBase::outputTexture() const316{317    return Object::sendMessage< MTL::Texture* >( this, _MTLFX_PRIVATE_SEL( outputTexture ) );318}319320//-------------------------------------------------------------------------------------------------------------------------------------------------------------321322_MTLFX_INLINE void MTLFX::SpatialScalerBase::setOutputTexture( MTL::Texture* pTexture )323{324    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setOutputTexture_ ), pTexture );325}326327//-------------------------------------------------------------------------------------------------------------------------------------------------------------328329_MTLFX_INLINE MTL::PixelFormat MTLFX::SpatialScalerBase::colorTextureFormat() const330{331    return Object::sendMessage< MTL::PixelFormat >( this, _MTLFX_PRIVATE_SEL( colorTextureFormat ) );332}333334//-------------------------------------------------------------------------------------------------------------------------------------------------------------335336_MTLFX_INLINE MTL::PixelFormat MTLFX::SpatialScalerBase::outputTextureFormat() const337{338    return Object::sendMessage< MTL::PixelFormat >( this, _MTLFX_PRIVATE_SEL( outputTextureFormat ) );339}340341//-------------------------------------------------------------------------------------------------------------------------------------------------------------342343_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerBase::inputWidth() const344{345    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( inputWidth ) );346}347348//-------------------------------------------------------------------------------------------------------------------------------------------------------------349350_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerBase::inputHeight() const351{352    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( inputHeight ) );353}354355//-------------------------------------------------------------------------------------------------------------------------------------------------------------356357_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerBase::outputWidth() const358{359    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( outputWidth ) );360}361362//-------------------------------------------------------------------------------------------------------------------------------------------------------------363364_MTLFX_INLINE NS::UInteger MTLFX::SpatialScalerBase::outputHeight() const365{366    return Object::sendMessage< NS::UInteger >( this, _MTLFX_PRIVATE_SEL( outputHeight ) );367}368369//-------------------------------------------------------------------------------------------------------------------------------------------------------------370371_MTLFX_INLINE MTLFX::SpatialScalerColorProcessingMode MTLFX::SpatialScalerBase::colorProcessingMode() const372{373    return Object::sendMessage< SpatialScalerColorProcessingMode >( this, _MTLFX_PRIVATE_SEL( colorProcessingMode ) );374}375376//-------------------------------------------------------------------------------------------------------------------------------------------------------------377378_MTLFX_INLINE MTL::Fence* MTLFX::SpatialScalerBase::fence() const379{380    return Object::sendMessage< MTL::Fence* >( this, _MTLFX_PRIVATE_SEL( fence ) );381}382383//-------------------------------------------------------------------------------------------------------------------------------------------------------------384385_MTLFX_INLINE void MTLFX::SpatialScalerBase::setFence( MTL::Fence* pFence )386{387    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( setFence_ ), pFence );388}389390//-------------------------------------------------------------------------------------------------------------------------------------------------------------391392_MTLFX_INLINE void MTLFX::SpatialScaler::encodeToCommandBuffer( MTL::CommandBuffer* pCommandBuffer )393{394    Object::sendMessage< void >( this, _MTLFX_PRIVATE_SEL( encodeToCommandBuffer_ ), pCommandBuffer );395}396397//-------------------------------------------------------------------------------------------------------------------------------------------------------------398