2

Which is faster, a single call to glUseProgram, or sending e.g. 6 or so floats via glUniform (batched or separately), and by approximately how much?

Navigateur
  • 1,852
  • 3
  • 23
  • 39
  • 1
    possible duplicate of [GLSL multiple shaderprogram VS uniforms switches](http://stackoverflow.com/questions/6539774/glsl-multiple-shaderprogram-vs-uniforms-switches) – Ani Apr 25 '13 at 17:54
  • None of that question is asked here. Although it is related to the same subject matter, the other question asks: "Does initial uniforms setting offset the glUseProgram call overhead". This question specifically asks for a direct head-to-head speed comparison between a `glUseProgram` call and a `glUniform` call, which the other question does not ask for, and in this case, specifically (a) `glUniform` call/calls with 6 floats. – Navigateur Apr 25 '13 at 21:20
  • I beg to differ. Despite the question being slightly different, I believe the answer(s) you receive will be along the same lines. The "duplicate" tag was to direct you to a set of answers that have already provided - not to reflect on the quality of your question. If there was a "answers will be the same as" tag, I would gladly have used it. – Ani Apr 29 '13 at 15:03

1 Answers1

1

Can you describe in more detail the scenario where you think this affects the performance of the rendering pipeline? They offer completely different functionalities and I don't see why you would care about the performance of glUseProgram vs glUniform.

Now let's analyze what happens when you use this functions to get an idea of their cost.

When you call glUseProgram it changes several OpenGL rendering states because we are going to use new shaders attached to the program object. The specification says that vertex and fragment programs are installed in the processors when you invoke this function. That alone seems costly enough to overshadow the cost of glUniform. Also, when you install new vertex and fragment programs, additional states of the rendering pipeline are changed to accomodate the number of texture units and data layout used by the programs.

glUniform copies data from one location of memory to another to specify the value of an uniform variable. The worst case would be copying matrices which seems less complex than glUseProgram.

But in the end, it all depends of the amount of data you are transferring with glUniform and the underlying implementation of glUseProgram (it could be super optimized by the driver and have a very small cost) and if your engine is smart enough to group the geometry that uses the same program and draw it without changing states.

  • Thanks for your ideas - I wish there were more numbers available on this. I can use `glUseProgram` to switch between shaders or simply `glUniform` to the same shader to have exactly the same effect. The shader will perform exactly the same calculation work in either case, hence why I'm asking for a direct head-to-head speed comparison. The `glUseProgram` switch is to an already-linked program, & `glUniform` is to send 6 floats (separately or batched as one array) to the currently-used program, on a typical Android device. Can't seem to find enough info to tell definitely which would be faster – Navigateur Apr 30 '13 at 02:17
  • 1
    If you have branching inside your shader code to handle different rendering paths the penalty could be higher than switching programs. – William Rodriguez May 03 '13 at 02:00
  • 1
    Good point, but there's no branching in my case - just `glUniform` to send a few floats to a single-case calculation (vs `glUseProgram` to switch programs where each program has those very same floats hard-coded in it). Obviously this only applies to values which remain fixed over the lifetime of the app (but different before each `draw` call per frame). I must send the other, dynamic values which are in the shader calculations via `glUniform` anyway. – Navigateur May 03 '13 at 09:13