Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing GLSL shaders in OpenGL engine

Which is better ?

  1. To have one shader program with a lot of uniforms specifying lights to use, or mappings to do (e.g. I need one mesh to be parallax mapped, and another one parallax/specular mapped). I'd make a cached list of uniforms for lazy transfers, and just change a couple of uniforms for every next mesh if it needs to do so.

  2. To have a lot of shader programs for every needed case, each one with small amount of uniforms, and do the lazy bind with glUseProgram for every mesh if it needs to do so. Here I assume that meshes are properly batched, to avoid redundant switches.

like image 407
Pythagoras of Samos Avatar asked Jan 10 '11 17:01

Pythagoras of Samos


People also ask

How do I optimize GLSL shader?

One way to speed up GLSL code, is by marking some variables constant at compile-time. This way the compiler may optimize code (e.g. unroll loops) and remove unused code (e.g. if hard shadows are disabled). The drawback is that changing these constant variables requires that the GLSL code is compiled again.

Does OpenGL use GLSL?

The OpenGL Shading Language (GLSL) is the principal shading language for OpenGL.


2 Answers

Most modern engines I know have a "shader cache" and use the second option, because apparently it's faster.

Also you can take a look at the ARB_shader_subroutine which allows dynamic linkage. But I think it's only available on DX11 class hardware.

like image 159
Axel Gneiting Avatar answered Oct 05 '22 23:10

Axel Gneiting


Generally, option 2 will be faster/better unless you have a truly huge number of programs. You can also use buffer objects shared across programs so that you need not reset any values when you change programs.

In addition, once you link a program, you can free all of the shaders that you linked into the program. This will free up all the source code and any pre-link info the driver is keeping around, leaving just the fully-linked program in memory.

like image 37
Chris Dodd Avatar answered Oct 05 '22 22:10

Chris Dodd