<!--
{
  "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/MPSBinaryImageKernel/encode(commandBuffer:inPlacePrimaryTexture:secondaryTexture:fallbackCopyAllocator:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal Performance Shaders"
    ],
    "preciseIdentifier" : "c:objc(cs)MPSBinaryImageKernel(im)encodeToCommandBuffer:inPlacePrimaryTexture:secondaryTexture:fallbackCopyAllocator:"
  },
  "title" : "encode(commandBuffer:inPlacePrimaryTexture:secondaryTexture:fallbackCopyAllocator:)"
}
-->

# encode(commandBuffer:inPlacePrimaryTexture:secondaryTexture:fallbackCopyAllocator:)

This method attempts to apply a kernel in place on a texture.

```
func encode(commandBuffer: any MTLCommandBuffer, inPlacePrimaryTexture: UnsafeMutablePointer<any MTLTexture>, secondaryTexture: any MTLTexture, fallbackCopyAllocator copyAllocator: MPSCopyAllocator? = nil) -> Bool
```

## Parameters

`commandBuffer`

A valid command buffer to receive the encoded kernel.

`inPlacePrimaryTexture`

A pointer to a valid texture containing the primary image. On success, the image contents and possibly the texture itself will be replaced with the result image.

`secondaryTexture`

A pointer to a valid texture containing the secondary source image. It will not be overwritten.

`copyAllocator`

An optional block to allocate a new texture to hold the operation results, in case in-place operation is not possible. The allocator may use a different pixel format or size than the original texture. You may enqueue operations on the provided command buffer using the provided compute command encoder to initialize the texture contents.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the operation succeeded (the texture may have been replaced with a new texture if a copy allocator was provided). <doc://com.apple.documentation/documentation/Swift/false> if the operation failed (the texture is unmodified).

## Discussion

This method attempts to apply the kernel in place on a texture. In-place operation means that the same texture is used both to hold the input image and the results. Operating in-place can be an excellent way to reduce resource utilization, and save time and energy. While simple Metal kernels can not operate in place because textures can not be readable and writable at the same time, some Metal Performance Shaders kernels can operate in place because they use multi-pass algorithms. Whether a kernel can operate in-place can depend on current hardware, OS version, and the parameters and properties passed to it. You should never assume that a kernel will continue to work in place, even if you have observed it doing so before.

If the in-place operation succeeds, this method returns <doc://com.apple.documentation/documentation/Swift/true>. If the in-place operation fails and no copy allocator is provided, then this method returns <doc://com.apple.documentation/documentation/Swift/false>. Without a fallback copy allocator, in neither case is the pointer held at `texture` modified.

Failure during in-place operation is common. You may find it simplifies your code to provide a copy allocator. When an in-place operation fails, your copy allocator will be invoked to create a new texture in which to write the results, allowing the kernel to proceed reliably out-of-place. The original texture will be released, replaced with a pointer to the new texture and <doc://com.apple.documentation/documentation/Swift/true> will be returned. If the copy allocator returns an invalid texture, it is released, `texture` remains unmodified, and <doc://com.apple.documentation/documentation/Swift/false> is returned.

## See Also

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

A block to make a copy of a source texture for filters that can only execute out of place.



---

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)