• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何在 opengl es 中使用 Apples Core Image

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:06:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

所以我的 opengl es 游戏中有一个纹理(在帧缓冲区中),我一直想模糊它。我一直在尝试多种方法来查看哪一种方法可以获得最好的 FPS,因为这需要实时发生,因为纹理会不断变化。

我究竟如何获取一个 opengles 纹理并将其发送到 coreImage,然后返回到 opengles 进行显示?

这是一些基本上你放在 UIImage 中的代码,它返回一个模糊的。我卡住的地方是将纹理获取到 UIImage,我想知道它们是否是一种更好的方法,然后每次加载新的 UIImage。

- (UIImage *)blurredImageWithImageUIImage *)sourceImage{

    //  Create our blurred image
    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *inputImage = [CIImage imageWithCGImage:sourceImage.CGImage];

    //  Setting up Gaussian Blur
    CIFilter *filter = [CIFilter filterWithName"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:15.0f] forKey"inputRadius"];
    CIImage *result = [filter valueForKey:kCIOutputImageKey];

    /*  CIGaussianBlur has a tendency to shrink the image a little, this ensures it matches 
     *  up exactly to the bounds of our original image */
    CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];

    UIImage *retVal = [UIImage imageWithCGImage:cgImage];
    return retVal;
}

如图所示,核心图像可能比最有效的方法更好。 enter image description here



Best Answer-推荐答案


如果您还没有尝试并注意到,您引用的方法会杀死性能——要从 GL 纹理创建 UIImage,您需要阅读一堆像素离开 GPU 并进入 CPU 内存。然后 CoreImage 可能会将其发送到 GPU 以执行其效果,但是由于您从中获得了 UIImage ,因此您在将图像发送回 GPU 之前再次从 GPU 读取用作纹理。那是每帧四次数据传输,而且每一次都很昂贵。

相反,将 Core Image 保留在 GPU 上,并且不要使用中间的 UIImage

  1. 您需要保留一个 CIContext 以进行渲染,因此请尽早使用 contextWithEAGLContext: 从您的 GL 上下文中创建一个并坚持下去。

  2. 使用 imageWithTexture:size:flipped:colorSpace:从您的 GL 纹理创建 CIImage

  3. 当您想将过滤器链的结果渲染到 GL 纹理时,将其与 glBindFramebuffer 绑定(bind)(就像您制作自己的渲染到纹理传递一样),并使用 drawImage:inRect:fromRect: 在您的 CIContext 中绘制过滤器的输出 CIImage . (并且不要忘记在使用它执行其他 GL 渲染之前重新绑定(bind)该纹理。)

这在 2012 年的 WWDC 演讲中得到了很好的介绍:Core Image Techniques .讨论从 40:00 左右开始,代码和演示持续到 46:05 左右。但是在接下来的演讲中,您将获得更多的演示、灵感、代码和架构技巧,这些都是您可以在 GL 中使用 CoreImage 做的有趣事情,让游戏看起来很棒。

此外,如果您使用 Metal (

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap