Use the M1 Mac GPU with Go

submited by
Style Pass
2022-05-13 16:30:12

I bought one of the new M1 Macbok pro machines when they came out. I’m really happy with it, everything works great, and they got rid of the Touchbar (which I hated) and put Magsafe, HDMI, and the SD card slot back.

I wanted to be able to carry out file operations, network operations, and data processing or file handling in Go, and still be able to run specific data processing tasks on the GPU.

I’ve made the Go side as simple as I could. Compile the shader, create the input matrix and populate it, create the output matrix, run gpu.Run(input, output) and collect the results.

Here, the Metal Shader Language is expecting an input image consisting of a 2D matrix of uint8 values with each 4 group of 4 values representing a single pixel’s red, green, blue and alpha values.

The shader code calculates the average of red, green and blue, and uses that as the value for all 3, to create a grayscale image.

Leave a Comment