<!--
{
  "availability" : [
    "iOS: 10.0.0 -",
    "iPadOS: 10.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 10.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MetalPerformanceShaders",
  "identifier" : "/documentation/MetalPerformanceShaders/MPSMatrixMultiplication/encode(commandBuffer:leftMatrix:rightMatrix:resultMatrix:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal Performance Shaders"
    ],
    "preciseIdentifier" : "c:objc(cs)MPSMatrixMultiplication(im)encodeToCommandBuffer:leftMatrix:rightMatrix:resultMatrix:"
  },
  "title" : "encode(commandBuffer:leftMatrix:rightMatrix:resultMatrix:)"
}
-->

# encode(commandBuffer:leftMatrix:rightMatrix:resultMatrix:)

Encodes a matrix multiplication kernel to a command buffer.

```
func encode(commandBuffer: any MTLCommandBuffer, leftMatrix: MPSMatrix, rightMatrix: MPSMatrix, resultMatrix: MPSMatrix)
```

## Parameters

`commandBuffer`

The command buffer that will receive the encoded kernel.

`leftMatrix`

The left input matrix.

`rightMatrix`

The right input matrix.

`resultMatrix`

The addend matrix which will also be overwritten by the operation result.

## Discussion

The following constraints apply to the sizes of the matrices depending on the transposition operations and the sizes requested at initialization time, as well as the origins at the time this method is called:

- The left input matrix must be large enough to hold an array of size `resultRows x interiorColumns` elements, beginning at the value of the [`leftMatrixOrigin`](/documentation/MetalPerformanceShaders/MPSMatrixMultiplication/leftMatrixOrigin) property.
- The right input matrix must be large enough to hold an array of size `interiorColumns x resultColumns` elements, beginning at the value of the [`rightMatrixOrigin`](/documentation/MetalPerformanceShaders/MPSMatrixMultiplication/rightMatrixOrigin) property.
- The result matrix must be large enough to hold an array of size `resultRows x resultColumns` elements, beginning at the value of the [`resultMatrixOrigin`](/documentation/MetalPerformanceShaders/MPSMatrixMultiplication/resultMatrixOrigin) property.

---

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)