Lags when switching shaders on nVidia using Cg


We have a problem with sudden switch of vertex and pixel shaders.

Suppose, we have a static geometry, which may be somehow lit by some number of point lights. In the common situation, the number of point lights is zero and I got simple material with 1 distant light (sun) reacting on geometry. The other shaders (for 1,2,…,N point lights) are loaded, but haven’t been executed yet even single time.

When first point light appears, all the objects in attenuated sphere have to switch it’s material. They do it, but the very first call to glDrawRangedElements() takes incredible time of about 800 MTicks (30000 vertices) compared to 1.5 MTicks in common situation.
After that lag everything goes just fine till the next point light appearing ((
Switched once, after that no lags with theses switched material, but every new material gives such 0.3…1.5 seconds stalls.

Looks like driver begins to do some work and stalls GPU pipeline.

Tested on 6800 and 7800/7900, 93.71 drivers and Cg 1.3 and 1.5.

P.S.: May be, we should not only load this extra-materials, but draw something dummy in dummy FBO in order to help driver do this job on loading, and not on first effective bind?

are you using cgCompileProgram, else it might be their cgSetAutoCompile thingie which can do deferred compiling. not sure what the defaults are.


nVidia answered, that actually they have a problem, when you begin to use some shader for the first time. They need to do some optimizations, and that make take pretty long time, so, they advice to draw something dummy on loading for that situations, like mentioned above.