iOS using OpenGL shaders in CIKernel

638 Views Asked by At

Is it possible to use OpenGL shaders in iOS using CIKernel ? If not, is there a way to convert between the two ?

Example OpenGL Shader

 #extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
void main() {
    vec4 textureColor = texture2D(sTexture, vTextureCoord);
    vec4 outputColor;
    outputColor.r = (textureColor.r * 0.393) + (textureColor.g * 0.769) + (textureColor.b * 0.189);
    outputColor.g = (textureColor.r * 0.349) + (textureColor.g * 0.686) + (textureColor.b * 0.168);
    outputColor.b = (textureColor.r * 0.272) + (textureColor.g * 0.534) + (textureColor.b * 0.131);
    outputColor.a = 1.0;
    gl_FragColor = outputColor;

I am trying to use the same filters for iOS and android. Android already uses OpenGL shaders, so I would like to use the same shaders in my iOS app.

1

There are 1 best solutions below

1
On BEST ANSWER

You can, but with a few tweaks.

  • vTextureCoord and samplerExternalOES would be passed into a kernel function as arguments.
  • The sTexture type would be __sample (assuming you're using a CIColorKernel which means the kernel can only access the pixel currently being computed)
  • For a color kernel, the function declaration would be kernel vec4 xyzzy(__sample pixel) - you can't name it main.
  • You don't need texture2D in a color kernel. In the example above pixel holds the color of the current pixel.
  • Rather than setting the value of gl_FragColor, your kernel function needs to return the color as a vec4.

If you want to use your code verbatim, you could (at a stretch) consider a Sprite Kit SKShader to generate a SKTexture which can be rendered as a CGImage.

simon