首页 > 解决方案 > 在 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];

标签: objective-cmacosmetalmetal-performance-shaders

解决方案


推荐阅读