5

I have the following function in Core Image Kernel Language and I need something equivalent in Metal Shading Language, but I have problem with destCoord , unpremultiply and premultiply functions.

kernel vec4 MyFunc(sampler src, __color color, float distance, float slope) {
  vec4 t;
  float d;

  d = destCoord().y * slope + distance;
  t = unpremultiply(sample(src, samplerCoord(src)));
  t = (t - d*color) / (1.0-d);

  return premultiply(t);
}

My function in MSL so far is:

float4 MyFunc(sample_t image, float3 color, float dist, float slope) {
    float4 t;
    float d;
    
    d = color[1] * slope + dist
    ...
        
    return t;
}

Any help would be appreciated!

Asteroid
  • 1,049
  • 2
  • 8
  • 16

1 Answers1

6

This should work:

float4 MyFunc(sampler src, float4 color, float dist, float slope, destination dest) {
     const float d = dest.coord().y * slope + dist;
     float4 t = unpremultiply(src.sample(src.coord()));
     t = (t - d * color) / (1.0 - d);

     return premultiply(t);
}

Note the destination parameter. This is an optional last parameter to a kernel, that gives you access to information about the render destination (like the coordinate in destination-space that you are rendering to). You don't need to pass anything for this when invoking the CIKernel, Core Image will fill it automatically.

Since you are only sampling the input src at the current location, you can also optimize the kernel to be a CIColorKernel. These are kernels that have a 1:1 mapping of input to output pixels. They can be concatenated by the Core Image runtime. The kernel code would look like this:

float4 MyFunc(sample_t src, float4 color, float dist, float slope, destination dest) {
    const float d = dest.coord().y * slope + dist;
    float4 t = unpremultiply(src);
    t = (t - d * color) / (1.0 - d);

    return premultiply(t);
}

Notice sample_t (which is basically a float4) vs. sampler.

Frank Rupprecht
  • 9,191
  • 31
  • 56
  • Where do I find more programming samples to learn what is and not possible using CoreImage Metal Shading Language? The videos of Apple as well as the reference documentation is not enough. https://developer.apple.com/metal/MetalCIKLReference6.pdf – Deepak Sharma Aug 30 '21 at 15:40
  • I can recommend you check out Simon "FlexMonkey" Gladman's free book "Core Image for Swift". Though the code samples still use the old CI Kernel Language, they can be easily translated into Metal. The important parts are the Core Image concepts and methods he describes in great detail, which didn't change much in recent years. https://books.apple.com/de/book/core-image-for-swift/id1073029980?l=en – Frank Rupprecht Aug 31 '21 at 05:37
  • That's fine for learning about CoreImage kernel, but my point is converting Metal/GLSL fragment/vertex shaders or compute shaders to Metal Core Image kernel. I understand Metal Core Image kernel has too few constructs and is not the same as a shader written in Metal. – Deepak Sharma Aug 31 '21 at 15:38
  • Hmm yeah, except for the WWDC talks I haven't seen any resources in this regard. If you post specific questions here or in the Developer Forum I might be able to help you out. – Frank Rupprecht Sep 03 '21 at 11:43