Search code examples
iosobjective-copengl-esgpuimage

GPUImage is slow the first blurs


I'm using the excellent GPUImage to blur my views (https://github.com/BradLarson/GPUImage)

However, it seems that the first couple of times I blur the views, it's really slow. Then after a few blurs, it's much faster.

Why is that, and is there some way to preload the GPUImage framework so it fast all the time?

Thanks

    GPUImageiOSBlurFilter *blurFilter = [GPUImageiOSBlurFilter new];
    blurFilter.blurRadiusInPixels = 1;
    blurFilter.saturation = 1.2;
    blurFilter.downsampling = 4.0f;
    blurredImage = [blurFilter imageByFilteringImage:blurredImage];

Solution

  • There will be a slight lag on the first usage of a given filter, because the framework has to compile the shaders used for that filter. These shaders are then cached for later reuse. Blurs in particular will exhibit this, since their shaders are generated for each blur radius that you use (for performance reasons when reused in video, etc.).

    That said, make sure that you absolutely need to work with UIImages in the above code. -imageByFilteringImage: isn't always the fastest approach, since it has to take in a UIImage, generate a GPUImagePicture behind the scenes, upload that image as a texture, process the image, then grab the bytes and recreate a UIImage from that.

    If you want to display a more realtime feed of adjusting an image and displaying a preview to the screen, you'd be better off manually creating the GPUImagePicture and sending the output to a GPUImageView, with updates every time you change the filter values.

    Also, don't do any performance tests in the Simulator. It exhibits weird behavior due to the software emulation of certain parts of the GPU, and I've seen strange initial lags when processing things there. This isn't present in the native Mac version of the framework, it's just a Simulator oddity.