带采样器的金属芯图像内核



我试图使用带有采样器参数的CIColorKernel或CIBlendKernel,但程序崩溃。这是我成功编译的着色器代码。

extern "C" float4 wipeLinear(coreimage::sampler t1, coreimage::sampler t2, float time) {
float2 coord1 = t1.coord();
float2 coord2 = t2.coord();
float4 innerRect = t2.extent();
float minX = innerRect.x + time*innerRect.z;
float minY = innerRect.y + time*innerRect.w;
float cropWidth = (1 - time) * innerRect.w;
float cropHeight = (1 - time) * innerRect.z;
float4 s1 = t1.sample(coord1);
float4 s2 = t2.sample(coord2);
if ( coord1.x > minX && coord1.x < minX + cropWidth && coord1.y > minY && coord1.y <= minY + cropHeight) {
return s1;
} else {
return s2;
}
}

它在初始化时崩溃。

class CIWipeRenderer: CIFilter {
var backgroundImage:CIImage?
var foregroundImage:CIImage?
var  inputTime: Float = 0.0
static var kernel:CIColorKernel = { () -> CIColorKernel in
let url = Bundle.main.url(forResource: "AppCIKernels", withExtension: "ci.metallib")!
let data = try! Data(contentsOf: url)
return try! CIColorKernel(functionName: "wipeLinear", fromMetalLibraryData: data) //Crashes here!!!!

}()
override var outputImage: CIImage? {
guard let backgroundImage = backgroundImage else {
return nil
}

guard let foregroundImage = foregroundImage else {
return nil
}

return CIWipeRenderer.kernel.apply(extent: backgroundImage.extent, arguments: [backgroundImage, foregroundImage, inputTime])
}

}

它在尝试行中崩溃,并出现以下错误:

Fatal error: 'try!' expression unexpectedly raised an error: Foundation._GenericObjCError.nilError

如果我用以下代码替换内核代码,它就像一个魅力:

extern "C" float4 wipeLinear(coreimage::sample_t s1, coreimage::sample_t s2, float time)
{
return mix(s1, s2, time);
}

因此,代码中没有明显的错误,例如传递了错误的函数名等等

对于您的用例,实际上可以使用CIColorKernel。你只需要把渲染目的地的范围也传递给内核,然后你就不需要采样器来访问它了

内核看起来是这样的:

extern "C" float4 wipeLinear(coreimage::sample_t t1, coreimage::sample_t t2, float4 destinationExtent, float time, coreimage::destination destination) {
float minX = destinationExtent.x + time * destinationExtent.z;
float minY = destinationExtent.y + time * destinationExtent.w;
float cropWidth = (1.0 - time) * destinationExtent.w;
float cropHeight = (1.0 - time) * destinationExtent.z;
float2 destCoord = destination.coord();
if ( destCoord.x > minX && destCoord.x < minX + cropWidth && destCoord.y > minY && destCoord.y <= minY + cropHeight) {
return t1;
} else {
return t2;
}
}

你这样称呼它:

let destinationExtent = CIVector(cgRect: backgroundImage.extent)
return CIWipeRenderer.kernel.apply(extent: backgroundImage.extent, arguments: [backgroundImage, foregroundImage, destinationExtent, inputTime])

请注意,内核中的最后一个destination参数是由Core Image自动传递的。你不需要通过arguments

是的,不能在CIColorKernelCIBlendKernel中使用采样器。这些内核针对从输入像素到输出像素的1:1映射的用例进行了优化。这允许Core Image在一个命令缓冲区中执行多个内核,因为它们不需要任何中间缓冲区写入
sampler允许您在任意坐标下对输入进行采样,这在本例中是不允许的。

您可以简单地使用CIKernel。当您需要更自由地对输入进行采样时,可以使用它。

要初始化内核,您需要调整如下代码:

static var kernel: CIKernel = {
let url = Bundle.main.url(forResource: "AppCIKernels", withExtension: "ci.metallib")!
let data = try! Data(contentsOf: URL)
return try! CIKernel(functionName: "wipeLinear", fromMetalLibraryData: data)
}()

在调用内核时,您现在还需要提供ROI回调,如下所示:

let roiCallback: CIKernelROICallback = { index, rect -> CGRect in
return rect // you need the same region from the input as the output
}
// or even shorter
let roiCallback: CIKernelROICallback = { $1 }
return CIWipeRenderer.kernel.apply(extent: backgroundImage.extent, roiCallback: roiCallback, arguments: [backgroundImage, foregroundImage, inputTime])

奖励答案:

对于这种混合效果,实际上根本不需要任何内核。你可以通过简单的裁剪和合成来实现所有这些:

class CIWipeRenderer: CIFilter {
var backgroundImage:CIImage?
var foregroundImage:CIImage?
var inputTime: CGFloat = 0.0
override var outputImage: CIImage? {
guard let backgroundImage = backgroundImage else { return nil }
guard let foregroundImage = foregroundImage else { return nil }
// crop the foreground based on time
var foregroundCrop = foregroundImage.extent
foregroundCrop.size.width *= inputTime
foregroundCrop.size.height *= inputTime
return foregroundImage.cropped(to: foregroundCrop).composited(over: backgroundImage)
}
}

相关内容

  • 没有找到相关文章

最新更新