Search code examples
iosswiftmetalmetalkit

Reading pixel Data in Fragment Shader


I am doing a photo Editing application in Metal iOS. I am having a texture of image. I want to have a tool when user taps the texture I want to make the tapped point(a square area around tapped point) I want to read that specific area and I want to read the color and I want to make it grayscale.

I know we can read the pixel data of texture in Kernel function. Is it possible to read the pixel data in Fragment Shader and do the above scenario.


Solution

  • What you are describing is the HelloCompute Metal example provided by Apple. Just download it and take a look at how a texture is rendered and how a shader can be used to convert color pixels to grayscale. The BasicTexturing example also shows how to do a plain texture render on its own.