NStal
NStal

Reputation: 999

webgl performance cost of switching shader and texture

I.To switching a shader effect which way is better? 1.using a big shader program and using uniform an if/else clause in shader program to use difference effect. 2.switch program between calls.

II.Is it better to use a big texture or use several small texture? And does upload texture cost mush,how about bind texture?

Upvotes: 8

Views: 3284

Answers (1)

user128511
user128511

Reputation:

Well, it would probably be best to write some perf tests and try it but in general.

  • Small shaders are faster than big.
  • 1 texture is faster the many textures
  • uploading textures is slow
  • binding textures is fast
  • switching programs is slow but usually much faster than combining 2 small programs into 1 big program.

Fragment shaders in particular get executed millions of times a frame. A 1920x1080 display has 2 million pixels so of there was no overdraw that would still mean your shader gets executed 2 million times per frame. For anything executed 2 million times a frame, or 120 million times a second of you're targeting 60 frames per second, smaller is going to be better.

As for textures, mips are faster than no mips because the GPU has a cache for textures and if the pixel it needs next are near the ones it previously read they'll likely already be in the cache. If they are far away they won't be in the cache. That also means randomly reading from a texture is particularly slow. But most apps read fairly linearly through a texture.

Switching programs is slow enough that sorting models by which program they use so that you draw all models that use program A first then all models that use program B is generally faster than drawing them in a random order. But there are other things the effect performance too. For example if a large model is obscuring a small model it's better to draw the large model first since the small model will then fail the depth test (z-buffer) and will not have its fragment shader executed for any pixels. So it's a trade off. All you can really do is test your particular application.

Also, it's important to test in the correct way. http://updates.html5rocks.com/2012/07/How-to-measure-browser-graphics-performance

Upvotes: 15

Related Questions