android - 将 Android OpenGL 过滤器重写为 Metal(用于 CIFilter)

标签 android ios opengl-es metal core-image

我们的应用程序的 Android 版本使用 GLSL (ES) 编写了数十个图像过滤器。从 iOS 12 开始,OpenGL 已被弃用,CIFilter 内核必须用 Metal 编写。

我以前有一些 OpenGL 的背景,但是在 Metal 中编写 CIFilter 内核对我来说是新的。

这是其中一个过滤器。你能帮我把它翻译成 Metal 作为 CIFilter 内核吗?这将为我提供一个很好的例子,以便我可以翻译其他人。

#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
uniform float texelWidth;
uniform float texelHeight;
uniform float intensivity;
void main() {
    float SIZE = 1.25 + (intensivity / 100.0)*2.0;
    vec4 color;
    float min = 1.0;
    float max = 0.0;
    float val = 0.0;
    for (float x = -SIZE; x < SIZE; x++) {
        for (float y = -SIZE; y < SIZE; y++) {
            color = texture2D(sTexture, vTextureCoord + vec2(x * texelWidth, y * texelHeight));
            val = (color.r + color.g + color.b) / 3.;
            if (val > max) { max = val; } else if (val < min) { min = val; }
        }
    }
    float range = 5. * (max - min);
    gl_FragColor = vec4(pow(1. - range, SIZE * 1.5));
    gl_FragColor = vec4((gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3. > 0.75 ? vec3(1.) : gl_FragColor.rgb, 1.);
}

最佳答案

这是试图复制您描述的过滤器的内核的 Metal 源代码:

#include <metal_stdlib>
#include <CoreImage/CoreImage.h>

using namespace metal;

extern "C" {
namespace coreimage {

float4 sketch(sampler src, float texelWidth, float texelHeight, float intensity40) {
    float size = 1.25f + (intensity40 / 100.0f) * 2.0f;

    float minVal = 1.0f;
    float maxVal = 0.0f;
    for (float x = -size; x < size; ++x) {
        for (float y = -size; y < size; ++y) {
            float4 color = src.sample(src.coord() + float2(x * texelWidth, y * texelHeight));
            float val = (color.r + color.g + color.b) / 3.0f;
            if (val > maxVal) {
                maxVal = val;
            } else if (val < minVal) {
                minVal = val;
            }
        }
    }

    float range = 5.0f * (maxVal - minVal);

    float4 outColor(pow(1.0f - range, size * 1.5f));
    outColor = float4((outColor.r + outColor.g + outColor.b) / 3.0f > 0.75f ? float3(1.0f) : outColor.rgb, 1.0f);
    return outColor;
}

}
}

我假设您已经熟悉 basics如何正确地将 Metal 着色器构建到可以由 Core Image 加载的库中。

您可以在运行时通过加载默认的 Metal 库并请求“sketch”函数来实例化您的内核(名称是任意的,只要它与内核源代码匹配):

NSURL *libraryURL = [NSBundle.mainBundle URLForResource:@"default" withExtension:@"metallib"];
NSData *libraryData = [NSData dataWithContentsOfURL:libraryURL];

NSError *error;
CIKernel *kernel = [CIKernel kernelWithFunctionName:@"sketch" fromMetalLibraryData:libraryData error:&error];

然后,您可以通过将此内核包装在您自己的 CIFilter 子类中,将其应用于图像,或者直接调用它:

CIImage *outputImage = [kernel applyWithExtent:CGRectMake(0, 0, width, height)
                                   roiCallback:^CGRect(int index, CGRect destRect)
                        { return destRect; }
                                     arguments:@[inputImage, @(1.0f/width), @(1.0f/height), @(60.0f)]];

我已尝试为每个参数选择合理的默认值(第一个参数应该是 CIImage 的实例),但当然可以根据需要调整这些值。

关于android - 将 Android OpenGL 过滤器重写为 Metal(用于 CIFilter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54403939/

相关文章:

在 Android 中打开资源时出现 java.io.FileNotFoundException

java - Appium-如何使用Java从iOS设备读取日志

iphone - OpenGL 从另一个 View 返回后绘制黑屏

ios - 在 iOS 和 OpenGLES 2.0 中使用 sampler3D 纹理的 texture3D 查找是否可用

opengl-es - 在 RGBA 纹理中编码 float 据

android - 为什么我在 Android 中得到 "Cannot remove Fragment attached to a different FragmentManager"?

android - 无法执行 dex : Multiple dex files define Lorg/apache/maven/artifact/Artifact

ios - Swift:if语句检查节点纹理是否与另一个节点纹理相同

ios - 单击左右按钮时将 CollectionView 的单元格向左和向右滚动

javascript - 如何使用 IP 网络摄像头应用程序中的 jpeg 流作为 html5 视频标签中的 src?