I’m performing a convolution with a 3×3 kernel in an iPhone shader, GLSL ES 1.1. Currently I am just doing 9 texture lookups. Is there a faster way? Some ideas:
-
passing the input image as a buffer rather than a texture to avoid invoking texture interpolation.
-
Passing 9 varying vec2 coordinates from the vertex shader (rather than just one as I am currently doing) to encourage the processor to prefetch the texture efficiently.
-
Looking into various Apple extensions that might be appropriate for this.
-
(Added) investigate ES equivalents for the GLSL shaderOffset call (which is not available under ES but perhaps there is an equivalent)
In terms of hardware, I’m focussed in particular on the iPhone 4S.
Are you sure you don’t mean OpenGL ES 2.0? You can’t do shaders of any kind using OpenGL ES 1.1. I’ll assume the former.
In my experience, the fastest way I’ve found to do this is your second listed item. I do several types of 3×3 convolutions in my GPUImage framework (which you could just use instead of trying to roll your own) and for those I feed in the texture offset for the horizontal and vertical directions and calculate the nine texture coordinates needed within the vertex shader. From there, I pass those as varyings to the fragment shader.
This (for the most part) avoids dependent texture reads in the fragment shader, which are terribly expensive on the iOS PowerVR GPUs. I say “for the most part” because on older devices like the iPhone 4, only eight of those varyings are used to avoid a dependent texture read. As I learned this last week, the ninth triggers a dependent texture read on older devices, so that slows things down a bit. The iPhone 4S, however, doesn’t have this issue because it supports a greater number of varyings being used in this fashion.
I use the following for my vertex shader:
and fragment shader:
Even with the above caveats, this shader runs in ~2 ms for a 640×480 frame of video on an iPhone 4, and a 4S can handle 1080p video at 30 FPS easily with a shader like this.