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?

Replies

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.

  • Hello! Thank you for giving me the answer! I changed the Build Settings how was in WWDC2020 session https://developer.apple.com/videos/play/wwdc2020/10021/ ,and made Build Rules flags "-cikernel" and "-fcikernel" With my attempts to show a filter from Kernel "ci.metallib" also how .metal i constantly comes to a black screen in real time preview, then I made metal MTKView, camera began to be displayed in real time, but the filter disappeared completely.

  • Since my ci.metallib shader is working on a project with static image, but in but in real time no.

Add a Comment