objective-c - 在 Metal 中升级和模糊纹理的有效方法
问题描述
我在 Metal (macOS) 中进行了一些渲染,价格相当昂贵,但如果结果稍微模糊,这是可以接受的。因此,为了优化事物,我以视图分辨率的一半渲染到纹理,然后放大结果并应用模糊滤镜 (MPS)。
为了放大纹理,我首先将其渲染到另一个纹理,然后应用模糊过滤器,将结果写入可绘制对象。我正在寻找一种技术来避免这种中间纹理(_texture2),以便模糊过滤器从原始纹理(_texture1)获取其输入。也许我应该编写自己的模糊着色器?
id <MTLTexture> _texture1; // half the size of the drawable
id <MTLTexture> _texture2; // same size as drawable
id<MTLCommandBuffer> commandBuffer = [_commandQueue commandBuffer];
id<MTLRenderCommandEncoder> renderEncoder =
[commandBuffer renderCommandEncoderWithDescriptor:...];
// this encoder renders to _texture1
if (renderEncoder) {
// do rendering here
[renderEncoder endEncoding];
MPSImageBilinearScale* scaleFilter =
[[MPSImageBilinearScale alloc] initWithDevice:_device];
scaleFilter.scaleTransform = ...; // enlarge by factor 2
[_scaleFilter encodeToCommandBuffer:commandBuffer sourceTexture:_texture1
destinationTexture:_texture2];
MPSImageGaussianBlur* blurFilter =
[[MPSImageGaussianBlur alloc] initWithDevice:_device sigma:1.0];
[_blurFilter encodeToCommandBuffer:commandBuffer sourceTexture:_texture2
destinationTexture:_view.currentDrawable.texture];
[commandBuffer presentDrawable:_view.currentDrawable];
}
[commandBuffer commit];
解决方案
推荐阅读
- c# - 正则表达式应该只匹配两种类型的带引号的字符串之一
- node.js - Node.Js 从 POST 请求中获取数据
- session - Firefox 68.0 更新导致 API 调用在 CSP 报告 uri POST 请求后返回 403
- java - 如何在 500 返回码导致异常后从 URL.openStream() 读取 HTML?
- excel - 更改后单元格不会变绿
- swift - swift UICollectionView 类似预览和拖动项
- azure-devops - 从父项继承字段值的子工作类型
- apache-spark - 如何从 apache spark 的外部文本文件中读取 structType 模式?
- c++ - Ray-AABB 交集函数的类隶属关系
- ios - 将 CMSampleBuffer 转换为 AVAudioPCMBuffer 以获取实时音频频率