Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 763
NStal Avatar asked Jul 29 '12 01:07

NStal


People also ask

Is switching shaders expensive?

glUseProgram : Changing shader programs can be very expensive, as the shader program is responsible (according to the GLES spec) for storing and restoring the state of all of its uniforms (or shader constants). The more uniforms in the shader, the more expensive swapping will be.

Does WebGL use shaders?

As mentioned in how it works WebGL requires 2 shaders every time you draw something. A vertex shader and a fragment shader. Each shader is a function. A vertex shader and fragment shader are linked together into a shader program (or just program).

What shader language does WebGL use?

WebGL require a shader program for the vertex and fragment manipulation sections of the graphics pipeline. A shader program are written in GLSL (Graphics Library Shader Language). This section provides a general introduction to GLSL. The GLSL language has gone through many versions.


1 Answers

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

like image 145
gman Avatar answered Dec 06 '22 01:12

gman