View Full Version : Using Shaders Efficiently

05-10-2017, 12:43 AM
Im almost learned core of openGL. My rendering system works by using different shaders gör different type of objects, like one shader for all normal mapped objects one for amimateds one for non changeable objects etc... By using a single shader i am calculating all the stuf in one shader, like diffuse lighting, specular
Lighting, fragment color by texturing the texture object uses. It causes efficiency drop and fps lose. There must be better way of doing the calculations efficiently. What tips you can give me for increasing efficiency? So far rendering types are just object, animated object, normal mapped object, water, shadows, skybox. All of them calculates diffuse and specular lighting and samples the texture.

05-10-2017, 03:20 AM
Like most optimisation problems, this revolves around trade-offs.

Having fewer shaders means that you can render the scene with fewer draw calls, which increases speed. But it may also mean that shaders are often doing more work than is necessary, which reduces speed.

The optimum trade-off can only be found empirically, not theoretically.

If you're considering splitting a generic shader into multiple specialised shaders, first see if you can optimise the generic shader for the most common cases, as that will avoid the need to split draw calls.

If you "disable" texture mapping by using a constant texture, use a 1x1 texture; reducing memory consumption reduces memory bandwidth which increases speed.

Modern GPUs have actual branches for dynamically-uniform boolean expressions (those having the same value for all elements within a SIMD vector), so if it will be common for a value to be e.g. 0.0 or 1.0 over many consecutive vertices or fragments, put a specialised version of the code inside a conditional block.