Convert ci.metallib shader to Kernel Core Image Language

Hello everyone !In my task, I do not have the ability to connect сi.Metallib in the project. I have a ci.metallib shader, this kind

#define TRACKING_SEVERITY 0.025
#define TRACKING_SPEED 0.2
#define SHIMMER_SPEED 30.0
#define RGB_MASK_SIZE 2.0

#include <metal_stdlib>
#include <CoreImage/CoreImage.h>
using namespace metal;

extern "C" { namespace coreimage {
    
    float mod(float x, float y) {
        return float(x - y * floor(x/y));
    }

    float4 mainImage(sampler_h src, float time, float amount) {
        // const float magnitude = sin(time) * 0.1 * amount;

        float2 greenCoord = src.coord();

        greenCoord.x -= sin(greenCoord.y * 500.0 + time) * INTERLACING_SEVERITY * amount;

        float scan = mod(greenCoord.y, 3.0);

        float yOffset = floor(sin(time * SHIMMER_SPEED));
        float pix = (greenCoord.y+yOffset) * src.size().x + greenCoord.x;
        pix = floor(pix);

        float4 colMask = float4(mod(pix, RGB_MASK_SIZE), mod((pix+1.0), RGB_MASK_SIZE), mod((pix+2.0), RGB_MASK_SIZE), 1.0);
        colMask = colMask / (RGB_MASK_SIZE - 1.0) + 0.5;

        // Tracking
        float t = -time * TRACKING_SPEED;
        float fractionalTime = (t - floor(t)) * 1.3 - TRACKING_HEIGHT;
        if(fractionalTime + TRACKING_HEIGHT >= greenCoord.y && fractionalTime <= greenCoord.y)
        {
            greenCoord.x -= fractionalTime * TRACKING_SEVERITY;
        }

        return src.sample(greenCoord).b*colMask*scan;
    }
}}

How does this code welcome to this form?

let kernel = CIKernel(source: """
kernel vec4 mainImage(sampler image, float time, float amount) {
                
                float mod(float x, float y) {
                    return float(x - y * floor(x/y));
                }?
                
                vec2 greenCoord = destCoord();?

                .......????
            }
""")

How exactly the types change?

Answered by ForumsContributor in
Accepted Answer

Hi, I think you might want to use this function https://developer.apple.com/documentation/coreimage/cikernel/2880194-init to make a MTLFunction from your metallib into a CIKernel. Hope this helps.

Convert ci.metallib shader to Kernel Core Image Language
 
 
Q