Simple GLSL convolution shader is atrociously slow

后端 未结 2 471
南笙
南笙 2021-01-30 17:57

I\'m trying to implement a 2D outline shader in OpenGL ES2.0 for iOS. It is insanely slow. As in 5fps slow. I\'ve tracked it down to the texture2D() calls. However, without thos

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-30 18:56

    The only way I know of reducing time taken in this shader is by reducing the number of texture fetches. Since your shader samples textures from equally spaced points about the center pixels and linearly combines them, you could reduce the number of fetches by making use of the GL_LINEAR mode availbale for texture sampling.

    Basically instead of sampling at every texel, sample in between a pair of texels to directly get a linearly weighted sum.

    Let us call the sampling at offset (-stepw,-steph) and (-stepw,0) as x0 and x1 respectively. Then your sum is

    sum = x0*k0 + x1*k1

    Now instead if you sample in between these two texels, at a distance of k0/(k0+k1) from x0 and therefore k1/(k0+k1) from x1, then the GPU will perform the linear weighting during the fetch and give you,

    y = x1*k1/(k0+k1) + x0*k0/(k1+k0)

    Thus sum can be calculated as

    sum = y*(k0 + k1) from just one fetch!

    If you repeat this for the other adjacent pixels, you will end up doing 4 texture fetches for each of the adjacent offsets, and one extra texture fetch for the center pixel.

    The link explains this much better

提交回复
热议问题