A standard interface for Metal Performance Shaders kernels.


class MPSKernel : NSObject


You should not use the MPSKernel class directly. Instead, a number of subclasses are available that define specific high-performance data-parallel operations.

The basic sequence for applying a kernel to an image is as follows:

  1. Initialize a kernel corresponding to the operation you wish to perform:

    let sobel = MPSImageSobel(device: mtlDevice)
  2. Encode the kernel into a command buffer.

    sobel.offset = ...
    sobel.clipRect = ...
    sobel.options = ...
    sobel.encode(commandBuffer: commandBuffer,
                 sourceTexture: inputImage,
                 destinationTexture: resultImage)

    Encoding the kernel merely encodes the operation into a command buffer. It does not modify any pixels, yet. All kernel state has been copied to the command buffer. Kernels may be reused. If the texture was previously operated on by another command encoder (e.g. a render command encoder), you should call the endEncoding() method on the other encoder before encoding the filter.

    Some kernels work in place, even in situations where Metal might not normally allow in-place operation on textures. If in-place operation is desired, you may attempt to call the encode(commandBuffer:inPlaceTexture:fallbackCopyAllocator:) method. If the operation cannot be completed in place, then false will be returned and you will have to create a new result texture and try again. To make an in-place image filter reliable, pass a fallback MPSCopyAllocator block to the method to create a new texture to write to in the event that a filter cannot operate in place.

    You may repeat step 2 to encode more kernels, as desired.

  3. After encoding any additional work to the command buffer using other encoders, submit the command buffer to your command queue, using:




init(device: MTLDevice)

Initializes a new kernel object.

func copy(with: NSZone?, device: MTLDevice?) -> Self

Makes a copy of this kernel object for a new device.


var options: MPSKernelOptions

The set of options used to run the kernel.

struct MPSKernelOptions

The options used when creating a kernel.

var device: MTLDevice

The device on which the kernel will be used.

var label: String?

The string that identifies the kernel.


Inherits From