<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MetalPerformanceShaders",
  "identifier" : "/documentation/MetalPerformanceShaders/MPSKernel",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Metal Performance Shaders"
    ],
    "preciseIdentifier" : "c:objc(cs)MPSKernel"
  },
  "title" : "MPSKernel"
}
-->

# MPSKernel

A standard interface for Metal Performance Shaders kernels.

```
class MPSKernel
```

## Overview

You should not use the [`MPSKernel`](/documentation/MetalPerformanceShaders/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:

```swift
let sobel = MPSImageSobel(device: mtlDevice)
```

1. Encode the kernel into a command buffer.

```swift
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 <doc://com.apple.documentation/documentation/Metal/MTLCommandEncoder/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:)`](/documentation/MetalPerformanceShaders/MPSUnaryImageKernel/encode(commandBuffer:inPlaceTexture:fallbackCopyAllocator:)) method. If the operation cannot be completed in place, then <doc://com.apple.documentation/documentation/Swift/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`](/documentation/MetalPerformanceShaders/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:

```swift
commandBuffer.commit()
```

> Note:
> It should be self evident that step 2 may not be thread safe. That is, you can not have multiple threads manipulating the same properties on the same kernel object at the same time and achieve coherent output. In common usage, the kernel properties don’t often need to be changed from their default values, but if you need to apply the same filter to multiple images on multiple threads with cropping/tiling, make additional kernel objects per thread (they are cheap). You can use multiple kernel objects on multiple threads, as long as only one thread is operating on any particular kernel object at a time.

## Topics

### Initializers

[`-  initWithCoder:`](/documentation/MetalPerformanceShaders/MPSKernel/init(coder:))

[`-  initWithCoder:device:`](/documentation/MetalPerformanceShaders/MPSKernel/init(coder:device:))

### Methods

[`-  initWithDevice:`](/documentation/MetalPerformanceShaders/MPSKernel/init(device:))

Initializes a new kernel object.

[`-  copyWithZone:device:`](/documentation/MetalPerformanceShaders/MPSKernel/copy(with:device:))

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

### Properties

[`options`](/documentation/MetalPerformanceShaders/MPSKernel/options)

The set of options used to run the kernel.

[`MPSKernelOptions`](/documentation/MetalPerformanceShaders/MPSKernelOptions)

The options used when creating a kernel.

[`device`](/documentation/MetalPerformanceShaders/MPSKernel/device)

The device on which the kernel will be used.

[`label`](/documentation/MetalPerformanceShaders/MPSKernel/label)

The string that identifies the kernel.

## Relationships

### Inherited By

[`MPSImageHistogram`](/documentation/MetalPerformanceShaders/MPSImageHistogram)

[`MPSMatrixUnaryKernel`](/documentation/MetalPerformanceShaders/MPSMatrixUnaryKernel)

[`MPSBinaryImageKernel`](/documentation/MetalPerformanceShaders/MPSBinaryImageKernel)

[`MPSMatrixRandom`](/documentation/MetalPerformanceShaders/MPSMatrixRandom)

[`MPSTemporalAA`](/documentation/MetalPerformanceShaders/MPSTemporalAA)

[`MPSNNGraph`](/documentation/MetalPerformanceShaders/MPSNNGraph)

[`MPSImageCopyToMatrix`](/documentation/MetalPerformanceShaders/MPSImageCopyToMatrix)

[`MPSCNNMultiaryKernel`](/documentation/MetalPerformanceShaders/MPSCNNMultiaryKernel)

[`MPSNNOptimizer`](/documentation/MetalPerformanceShaders/MPSNNOptimizer)

[`MPSImageEDLines`](/documentation/MetalPerformanceShaders/MPSImageEDLines)

[`MPSMatrixSum`](/documentation/MetalPerformanceShaders/MPSMatrixSum)

[`MPSAccelerationStructure`](/documentation/MetalPerformanceShaders/MPSAccelerationStructure)

[`MPSMatrixBinaryKernel`](/documentation/MetalPerformanceShaders/MPSMatrixBinaryKernel)

[`MPSImageFindKeypoints`](/documentation/MetalPerformanceShaders/MPSImageFindKeypoints)

[`MPSMatrixCopyToImage`](/documentation/MetalPerformanceShaders/MPSMatrixCopyToImage)

[`MPSRayIntersector`](/documentation/MetalPerformanceShaders/MPSRayIntersector)

[`MPSMatrixCopy`](/documentation/MetalPerformanceShaders/MPSMatrixCopy)

[`MPSImageNormalizedHistogram`](/documentation/MetalPerformanceShaders/MPSImageNormalizedHistogram)

[`MPSCNNKernel`](/documentation/MetalPerformanceShaders/MPSCNNKernel)

[`MPSRNNMatrixTrainingLayer`](/documentation/MetalPerformanceShaders/MPSRNNMatrixTrainingLayer)

[`MPSMatrixMultiplication`](/documentation/MetalPerformanceShaders/MPSMatrixMultiplication)

[`MPSCNNBinaryKernel`](/documentation/MetalPerformanceShaders/MPSCNNBinaryKernel)

[`MPSSVGF`](/documentation/MetalPerformanceShaders/MPSSVGF)

[`MPSUnaryImageKernel`](/documentation/MetalPerformanceShaders/MPSUnaryImageKernel)

[`MPSImageGuidedFilter`](/documentation/MetalPerformanceShaders/MPSImageGuidedFilter)

[`MPSRNNMatrixInferenceLayer`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer)

[`MPSNDArrayMultiaryBase`](/documentation/MetalPerformanceShaders/MPSNDArrayMultiaryBase)

### Conforms To

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Equatable`](/documentation/Swift/Equatable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Hashable`](/documentation/Swift/Hashable)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)