我在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];