Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: GL ES: Fragment shader optimization

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2013
    Posts
    4

    Question GL ES: Fragment shader optimization

    Summary:
    I get FPS slowdown as soon as I try to tint the sprites (i.e: multiply texture with color in the fragment shader)

    Details:

    Hardware: iPod touch 4

    Using 64x64 png texture containing alpha channel, rendering with glEnable(GL_BLEND). (A smiley face with a drop shadow)

    I am drawing 700 sprites on the screen using glDrawArrays. And yes I am batching all of these in a single draw call. Following shows Vertex data structure:

    Code :
        struct Vertex {
            float Position[2];
            float Color[4];
            float Texture[2];
        };
    Yes I am sending colour with each vertex because I selectively need to tint some sprites but not others. Following is the fragment shader I am using:

    Code :
        varying lowp vec2 TexCoord;
        uniform sampler2D TextureSampler;
     
        void main(void)
        {
            gl_FragColor = texture2D( TextureSampler, TexCoord );
        }

    Till now it is working GREAT, giving me full 60 FPS !!!

    BUT

    As soon as I change the fragment shader to the following (to allowing tinting):

    Code :
        varying lowp vec4 DestinationColor;
        varying lowp vec2 TexCoord;
        uniform sampler2D TextureSampler;
     
        void main(void)
        {
            gl_FragColor = texture2D( TextureSampler, TexCoord ) * DestinationColor;
        }

    The performance drops to 47 FPS only due to this single change {just by multiplication with ONE vector} (FPS measured using xcode instruments and OpenGL detective). Any ideas what is going on ?

    Thanks.
    Last edited by fakhir; 05-23-2013 at 07:50 PM.

  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,269
    Try using glDrawElements with an appropriate indexlist.
    Try increasing the complexity of the fragment shader, (e.g by multiplying with one more uniform vec4) to see if the performance will continue scaling down.

  3. #3
    Member Regular Contributor malexander's Avatar
    Join Date
    Aug 2009
    Location
    Ontario
    Posts
    302
    My guess is that because the Color vertex attribute is not being used, the compiler is optimizing it out and it's not being fetched. Try multiplying by a constant instead of a varying; I expect you'll see performance remain roughly the same (60fps). The extra attribute may be causing you to run into memory bandwidth limitations.

    It would also be helpful to see the vertex shader.

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    25
    The makers of powerVR hardware recommend against the use of alpha testing. ); It interferes with hardware optimizations that take place behind the scenes. Search for the PowerVR Performance Recommendations document.

    I assume that they are referring to glEnable(GL_BLEND); If you use it you are going to see a dramatic drop in performance. Everything you add will make matters much worse than you'd normally expect.

    Also, in the powerVR SDK shaders I've noticed that they use a precision qualifier for the texture samplers.

    uniform vec4 sampler2D TextureSampler; instead of
    uniform sampler2D TextureSampler;

    I assume that if you leave the lowp out of the statement then the drivers will set a default value, whatever it may be. If that default value is mediump or highp then maybe your dramatic drop in fps is due to the driver casting from highp to lowp.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    25
    47 fps seems to be the number that the iPhone 4's will drop down to when under load.

  6. #6
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    22
    Why are you passing color as an attribute instead of a uniform?

    Can you post your vertex shader?

  7. #7
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by marcClintDion View Post
    The makers of powerVR hardware recommend against the use of alpha testing. ); It interferes with hardware optimizations that take place behind the scenes. Search for the PowerVR Performance Recommendations document.

    I assume that they are referring to glEnable(GL_BLEND);
    I assume that they were referring to alpha testing, i.e. glEnable(GL_ALPHA_TEST) and glAlphaFunc().

    With blending, fragments always update the depth buffer even if they leave the colour buffer unchanged (when alpha is zero). Alpha-testing causes fragments to be conditionally discarded based upon the alpha value, and discarded fragments don't update the depth buffer.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •