mtl-rs 0.1.12

Rust bindings for Apple's Metal API
use objc2::{
    encode::{Encode, Encoding, RefEncode},
    extern_class, extern_conformance, extern_methods, msg_send,
    rc::{Allocated, Retained},
};
use objc2_foundation::{CopyingHelper, NSCopying, NSObject, NSObjectProtocol, NSString};

use crate::*;

/// Option mask for requesting reflection information at pipeline build time.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4shaderreflection?language=objc)
// NS_OPTIONS
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MTL4ShaderReflection(pub usize);
bitflags::bitflags! {
    impl MTL4ShaderReflection: usize {
/// Requests no information.
        #[doc(alias = "MTL4ShaderReflectionNone")]
        const None = 0;
/// Requests reflection information for bindings.
        #[doc(alias = "MTL4ShaderReflectionBindingInfo")]
        const BindingInfo = 1<<0;
/// Requests reflection information for buffer types.
        #[doc(alias = "MTL4ShaderReflectionBufferTypeInfo")]
        const BufferTypeInfo = 1<<1;
    }
}

unsafe impl Encode for MTL4ShaderReflection {
    const ENCODING: Encoding = usize::ENCODING;
}

unsafe impl RefEncode for MTL4ShaderReflection {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

/// Enumeration for controlling alpha-to-one state of a pipeline state object.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4alphatoonestate?language=objc)
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MTL4AlphaToOneState(pub isize);
impl MTL4AlphaToOneState {
    /// Disables alpha-to-one.
    #[doc(alias = "MTL4AlphaToOneStateDisabled")]
    pub const DISABLED: Self = Self(0);
    /// Enables alpha-to-one.
    #[doc(alias = "MTL4AlphaToOneStateEnabled")]
    pub const ENABLED: Self = Self(1);
}

unsafe impl Encode for MTL4AlphaToOneState {
    const ENCODING: Encoding = isize::ENCODING;
}

unsafe impl RefEncode for MTL4AlphaToOneState {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

/// Enumeration for controlling alpha-to-coverage state of a pipeline state object.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4alphatocoveragestate?language=objc)
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MTL4AlphaToCoverageState(pub isize);
impl MTL4AlphaToCoverageState {
    /// Disables alpha-to-coverage.
    #[doc(alias = "MTL4AlphaToCoverageStateDisabled")]
    pub const DISABLED: Self = Self(0);
    /// Enables alpha-to-coverage.
    #[doc(alias = "MTL4AlphaToCoverageStateEnabled")]
    pub const ENABLED: Self = Self(1);
}

unsafe impl Encode for MTL4AlphaToCoverageState {
    const ENCODING: Encoding = isize::ENCODING;
}

unsafe impl RefEncode for MTL4AlphaToCoverageState {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

/// Enumeration for controlling the blend state of a pipeline state object.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4blendstate?language=objc)
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MTL4BlendState(pub isize);
impl MTL4BlendState {
    /// Disables blending.
    #[doc(alias = "MTL4BlendStateDisabled")]
    pub const DISABLED: Self = Self(0);
    /// Enables blending.
    #[doc(alias = "MTL4BlendStateEnabled")]
    pub const ENABLED: Self = Self(1);
    /// Defers determining the blending stage.
    ///
    /// Behaves as ``MTL4BlendStateDisabled`` until you specialize this pipeline value.
    #[doc(alias = "MTL4BlendStateUnspecialized")]
    pub const UNSPECIALIZED: Self = Self(2);
}

unsafe impl Encode for MTL4BlendState {
    const ENCODING: Encoding = isize::ENCODING;
}

unsafe impl RefEncode for MTL4BlendState {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

/// Enumeration for controlling support for ``MTLIndirectCommandBuffer``.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4indirectcommandbuffersupportstate?language=objc)
// NS_ENUM
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MTL4IndirectCommandBufferSupportState(pub isize);
impl MTL4IndirectCommandBufferSupportState {
    /// Disables support for indirect command buffers.
    #[doc(alias = "MTL4IndirectCommandBufferSupportStateDisabled")]
    pub const DISABLED: Self = Self(0);
    /// Enables support for indirect command buffers.
    #[doc(alias = "MTL4IndirectCommandBufferSupportStateEnabled")]
    pub const ENABLED: Self = Self(1);
}

unsafe impl Encode for MTL4IndirectCommandBufferSupportState {
    const ENCODING: Encoding = isize::ENCODING;
}

unsafe impl RefEncode for MTL4IndirectCommandBufferSupportState {
    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

extern_class!(
    /// Provides options controlling how to compile a pipeline state.
    ///
    /// You provide these options through the ``MTL4PipelineDescriptor`` class at compilation time.
    ///
    /// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4pipelineoptions?language=objc)
    #[unsafe(super(NSObject))]
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct MTL4PipelineOptions;
);

extern_conformance!(
    unsafe impl NSCopying for MTL4PipelineOptions {}
);

unsafe impl CopyingHelper for MTL4PipelineOptions {
    type Result = Self;
}

extern_conformance!(
    unsafe impl NSObjectProtocol for MTL4PipelineOptions {}
);

impl MTL4PipelineOptions {
    extern_methods!(
        /// Controls whether to enable or disable Metal Shader Validation for the pipeline.
        #[unsafe(method(shaderValidation))]
        #[unsafe(method_family = none)]
        pub fn shader_validation(&self) -> MTLShaderValidation;

        /// Setter for [`shaderValidation`][Self::shaderValidation].
        #[unsafe(method(setShaderValidation:))]
        #[unsafe(method_family = none)]
        pub fn set_shader_validation(
            &self,
            shader_validation: MTLShaderValidation,
        );

        /// Controls whether to include Metal shader reflection in this pipeline.
        #[unsafe(method(shaderReflection))]
        #[unsafe(method_family = none)]
        pub fn shader_reflection(&self) -> MTL4ShaderReflection;

        /// Setter for [`shaderReflection`][Self::shaderReflection].
        #[unsafe(method(setShaderReflection:))]
        #[unsafe(method_family = none)]
        pub fn set_shader_reflection(
            &self,
            shader_reflection: MTL4ShaderReflection,
        );
    );
}

/// Methods declared on superclass `NSObject`.
impl MTL4PipelineOptions {
    extern_methods!(
        #[unsafe(method(init))]
        #[unsafe(method_family = init)]
        pub fn init(this: Allocated<Self>) -> Retained<Self>;

        #[unsafe(method(new))]
        #[unsafe(method_family = new)]
        pub fn new() -> Retained<Self>;
    );
}

extern_class!(
    /// Base type for descriptors you use for building pipeline state objects.
    ///
    /// See also [Apple's documentation](https://developer.apple.com/documentation/metal/mtl4pipelinedescriptor?language=objc)
    #[unsafe(super(NSObject))]
    #[derive(Debug, PartialEq, Eq, Hash)]
    pub struct MTL4PipelineDescriptor;
);

extern_conformance!(
    unsafe impl NSCopying for MTL4PipelineDescriptor {}
);

unsafe impl CopyingHelper for MTL4PipelineDescriptor {
    type Result = Self;
}

extern_conformance!(
    unsafe impl NSObjectProtocol for MTL4PipelineDescriptor {}
);

impl MTL4PipelineDescriptor {
    extern_methods!(
        /// Provides compile-time options when you build the pipeline.
        #[unsafe(method(options))]
        #[unsafe(method_family = none)]
        pub fn options(&self) -> Option<Retained<MTL4PipelineOptions>>;

        /// Setter for [`options`][Self::options].
        #[unsafe(method(setOptions:))]
        #[unsafe(method_family = none)]
        pub fn set_options(
            &self,
            options: Option<&MTL4PipelineOptions>,
        );
    );
}

impl MTL4PipelineDescriptor {
    /// Assigns an optional string that uniquely identifies a pipeline descriptor.
    ///
    /// After you provide this label, you can use it to look up a pipeline state object by name in a binary archive.
    pub fn label(&self) -> Option<String> {
        let s: Option<Retained<NSString>> = unsafe { msg_send![self, label] };
        s.map(|v| v.to_string())
    }

    /// Setter for [`label`][Self::label].
    pub fn set_label(
        &self,
        label: Option<&str>,
    ) {
        unsafe {
            let _: () = msg_send![self, setLabel: label.map(NSString::from_str).as_deref()];
        }
    }
}

/// Methods declared on superclass `NSObject`.
impl MTL4PipelineDescriptor {
    extern_methods!(
        #[unsafe(method(init))]
        #[unsafe(method_family = init)]
        pub fn init(this: Allocated<Self>) -> Retained<Self>;

        #[unsafe(method(new))]
        #[unsafe(method_family = new)]
        pub fn new() -> Retained<Self>;
    );
}