How to fix the delay in the first use of a shader?
I ran the OpenGL ES Analysis tool from Instruments in my app, and it stated that after compiling the shader, I should make a call to glDrawArrays in a prewarming pass. So I checked the time of开发者_如何学Python some shaders I wrote, and indeed the first time the program run it is much slower.
In my code I have a generic shader loader, which doesn't know the shader uniforms/attributes, it just compile it. And it seems that the best place to prewarm the shader is there (so I don't need to add glDrawArrays everywhere). I tryed adding this in my shader loader:
glUseProgram(prog);
glDrawArrays(GL_TRIANGLES, 0, 0);
It fixed the delay, but since I'm not setting any uniforms/attributes I'm not sure if it is safe. Also it somehow looks like a workaround. What is the best way to prewarm the program?
your code is (almost) perfectly safe. The uniforms are not a problem since they do contain some default values. As for the attributes, it is not a problem either since you are not emitting any vertices.
On the other hand, this may be the easiest way to prewarm the shaders, but may not be the best one. A similar techniques were used back at a time to prewarm textures (to fill the texture cache). It was usually done by rendering a few frames while not displaying the output. This was seen for example in Unreal Tournament, it displayed "Precaching" for a short while after loading the level. I'd suggest doing the same.
精彩评论