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 8 of 8

Thread: Switching GLSL vertex shaders -- REALLY slow?

  1. #1
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    2,704

    Switching GLSL vertex shaders -- REALLY slow?

    I have a case with four separate GLSL vertex shaders, where 1/4 of the scene is drawn with each of the shaders. This scene, running at a fixed 60 Hz, takes 100% of the CPU! Meanwhile, if I set the vertex shader once, and just draw the same scene (just not switching shaders), the same scene takes 5% of the CPU.

    I can understand that the first bind of a specific shader would cause compilation and validation, but this CPU usage is each and every frame; not just the first time the shader is bound.

    What can I do to prevent this problem? Spending all that CPU on just switching shaders is not an option for us; we'll have to go back to fixed-function processing.
    "If you can't afford to do something right,
    you'd better make sure you can afford to do it wrong!"

  2. #2
    Senior Member OpenGL Guru Relic's Avatar
    Join Date
    Apr 2000
    Posts
    2,527

    Re: Switching GLSL vertex shaders -- REALLY slow?

    You mean this
    Code :
    for (i = 0; i < 4; i++)
    {
    glUseProgramObject(program[i]);
    Render(scenepart[i])
    }
    versus this
    Code :
    glUseProgramObject(program[n]);
    for (i = 0; i < 4; i++)
    {
    Render(scenepart[i])
    }
    Did you test all four shaders for n above?
    Are they equally fast?
    Is one falling back to SW (well, at 60 Hz that'd be surprising).

    What hardware and drivers?

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    2,704

    Re: Switching GLSL vertex shaders -- REALLY slow?

    Yes, that's what I mean. Yes, I tested all four shaders; no difference.

    The surprising thing is that this happens both on NVIDIA hardware and ATI hardware, Anything from a GF4200Ti, through a Radeon 9600, to a GF6800; all using modern drivers.

    The fact that both hardware lines see the same problem lead me to believe there's some systemic reason for this CPU usage, but I just can't figure out what it would be, or how to work around it.
    "If you can't afford to do something right,
    you'd better make sure you can afford to do it wrong!"

  4. #4
    Junior Member Regular Contributor
    Join Date
    May 2005
    Location
    boulder, co
    Posts
    228

    Re: Switching GLSL vertex shaders -- REALLY slow?

    I don't know what's causing the problem you're having, but I have more than a dozen shaders (pairs of fragment/vertex shaders), and I switch between them many times during one frame (I never got around to sort by shaders), but my CPU is still below 15%..

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    2,704

    Re: Switching GLSL vertex shaders -- REALLY slow?

    Thanks; that's a good data point.
    "If you can't afford to do something right,
    you'd better make sure you can afford to do it wrong!"

  6. #6
    Junior Member Regular Contributor
    Join Date
    Dec 2002
    Location
    Silicon Valley
    Posts
    217

    Re: Switching GLSL vertex shaders -- REALLY slow?

    Are you absolutely sure you are only calling glUseProgramObject 4 times per frame? Shader swapping is a performance killer...

  7. #7
    Junior Member Regular Contributor Zulfiqar Malik's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    247

    Re: Switching GLSL vertex shaders -- REALLY slow?

    I have certainly done many shader changes per-frame on a variety of hardware (both ATI and nVidia) and never encountered the aforementioned scenario. However, i have never used a lone vertex or fragment shader. You can try attaching a fragment shader (that simply outputs 1, 1, 1, 1 in gl_FragColor), with each of the vertex shaders, simply for testing, and see whether the problem persists or not.
    But then again, you might have tried that already, but i am guessing anyways.
    Zulfiqar Inayat Malik.
    Senior Developer, The Foundry.

  8. #8
    Senior Member OpenGL Guru zed's Avatar
    Join Date
    Jul 2000
    Location
    S41.16.25 E173.16.21
    Posts
    2,609

    Re: Switching GLSL vertex shaders -- REALLY slow?

    The surprising thing is that this happens both on NVIDIA hardware and ATI hardware, Anything from a GF4200Ti, through a Radeon 9600, to a GF6800; all using modern drivers.
    gf4200 is not a gf4 i take it as they dont support glsl

    sorry ive no answers but can also tesstify to using great numbers of shaders ave >30 changes a frame (actually im forgetting lighting theres prolly greater than100 changes a frame) + im getting no great cpu hit.

    one thing u can try (its stopid but i get occasional error if i dont do the following) but after youve set the program reset it with the following
    glGetIntegerv( GL_CURRENT_PROGRAM, &active_programID );
    glUseProgram( GLuint(active_programID) );

    also one thing i just noticed u mention just vertex shaders (i always think of them as unified ie a shader is vs+fs combined)
    im a lazy ass so each fs gets its own vs (prolly a lot of duplication with vs's) perhaps its something to do with this

Posting Permissions

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