• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
newObjekt

Glsl - Which is faster: lots of if statements or lots of seperate shaders?

8 posts in this topic

Ive noticed that as I add more features to my glsl shader I have been getting really poor performance. I currently have one shader with multiple blocks of if statements that apply different effects such as normal mapping, specular, lighting and etc... I was wondering if it would be more efficent to just generate a new frag and vert shader for each material in my scene so that I would have no conditonal blocks in the glsl shader. I also would need to know if there is a limit on the number of shader programs you can have, and if constantly switching shader programs during drawing has a major impact on performance.
0

Share this post


Link to post
Share on other sites

From experience on mobile platforms (GLSL ES) it is massively faster to use specialised shaders with no conditionals.

 

I don't have much experience on modern desktop hardware but I've heard they cope pretty well with conditionals, particularly if the conditionals can be evaluated from uniforms (as opposed to, say, vertex data or texture data).

1

Share this post


Link to post
Share on other sites

There's no general rule. On one hand, a specialist shader is often faster than a more general uber-shader with a lot of branches. But if you need to subdivide a shader into mutliple passes, you could get in trouble with bandwidth or with state changes if you over-do the number of shaders (e.g. each object get its own shader, so that batching is impossible).

 

An other option is, to write an uber shader, but to use pre-processor statements instead of dynamic conditions to generate the shader you need on-the-fly.

Edited by Ashaman73
2

Share this post


Link to post
Share on other sites

Alright, so you say as long as I fill a good amount of screen space it's good to use multiple shaders. The way I currently have my rendering work is this.

 

for(objects in the game)

for(models in the object)

for(materials in the model)

renderTriangles();

 

When the model is loaded it takes every triangle of each material and puts it into a floatBuffer and saves it. This essentially divides the model by material. I then just bind the textures and what not and send the floatbuffer to GL and it renders it on each frame.

 

For the most part that is going to usually be more than 500 pixels... but what if the object is offscreen? Should I check and see if an object is visible on the cpu and just skip uploading it if it isn't?

 

One of the map models I'm using in game is about 20k triangles with around 20 or so materials. Some of those materials will probably use the same glsl shader, but I will most likely end up switching shaders between them anyways because I'd be doing it per material. 

 

Would it be worth the performance to also divide these groups of triangles by glsl shader.

 

So:

 

for(objects in the game)

for(models in the object)

for(glsl shaders in the model)

for(materials that use this GLSL shader)

renderTriangles();

 

Thanks for the advice so far.

0

Share this post


Link to post
Share on other sites

IME:

"if()" seems fairly cheap if it is working with uniforms (such as to enable/disable certain features, at least as far as tested thus-far).

 

OTOH, if using the conditionals for things which tend to vary, such as per-pixel data, then they will cost a fair bit more.

 

 

 

I have actually been considering moving between several smaller shaders possibly to a bigger one.

 

the issue is mostly that there are at present several different lighting shaders, which need to be switched between for various combinations of light-source settings and materials, and it may be cheaper in this case to use a single bigger shader, which enables/disables various lighting features instead (say, we can enable or disable shadow-mapping, normal and specular mapping, the use of projection and falloff textures, ...).

 

granted, in this case, I would probably have to make a split between light-source and material attributes (as-is, all are being setup in a big ugly mass every time either the light-source or bound material changes).

 

but, either way, the idea is that it may make sense for the code to instead just change which attributes are relevant to the current material (say, enable/disable normal-mapping, ...).

 

 

however, if what the shaders do is unrelated, say, they operate at different stages of the rendering pipeline, then it probably makes sense to use different shaders.

 

I am not entirely sure what the actual cost of an if based on a uniform actually is.

1

Share this post


Link to post
Share on other sites

IME:

"if()" seems fairly cheap if it is working with uniforms (such as to enable/disable certain features, at least as far as tested thus-far).

 

OTOH, if using the conditionals for things which tend to vary, such as per-pixel data, then they will cost a fair bit more.

 

 

 

I have actually been considering moving between several smaller shaders possibly to a bigger one.

 

the issue is mostly that there are at present several different lighting shaders, which need to be switched between for various combinations of light-source settings and materials, and it may be cheaper in this case to use a single bigger shader, which enables/disables various lighting features instead (say, we can enable or disable shadow-mapping, normal and specular mapping, the use of projection and falloff textures, ...).

 

granted, in this case, I would probably have to make a split between light-source and material attributes (as-is, all are being setup in a big ugly mass every time either the light-source or bound material changes).

 

but, either way, the idea is that it may make sense for the code to instead just change which attributes are relevant to the current material (say, enable/disable normal-mapping, ...).

 

 

however, if what the shaders do is unrelated, say, they operate at different stages of the rendering pipeline, then it probably makes sense to use different shaders.

 

I am not entirely sure what the actual cost of an if based on a uniform actually is.

 

Ah, that's interesting. For the most part I have uniforms that just enable and disable features.

 

It kind of looks like this... (I'm not on my linux HDD right now so I can't get the exact code)

 

uniform boolean unlit;

uniform integer normalSource;

uniform integer specularSource;

uniform integer illuminationSource;

uniform integer transparencySource;

 

if(!unlit) {

//lighting stuff

}

 

//do diffuse texture and detail mixing and crap

 

 

if(specularSource> -1) {

//do specular mapping

}

 

if(normalSource > -1) {

//do normal mapping

}

 

 

if(illuminationSource> -1) {

//do illum mapping

}

 

 

 

if(transparencySource> -1) {

//do transparency mapping

}

 

///finish up and set fragColor

 

 

Also, I'd like to point out that the reason I use integers for the differnt texture sources is I have a system where I use integers to specify which textures and color channels to use for operations. IE If i set specularSource to 14 it will use the diffuse.alpha for specular. Or if I set illuminationSource to 11 it would use the diffuse.red channel.

Edited by newObjekt
0

Share this post


Link to post
Share on other sites

Should I check and see if an object is visible on the cpu and just skip uploading it if it isn't?

it's a standard optimization to perform 'frustum culling' - eg check if an object's bounding sphere is outside of the camera's FOV/frustum - which saves CPU time in GL calls, and saves GPU time processing useless vertices.

for(objects in the game)
for(models in the object)
for(glsl shaders in the model)
for(materials that use this GLSL shader)
renderTriangles();

I would replace that last line with pushToQueue(drawData), then outside of the loops, sortQueueByMaterial(), then
For(data in queue) draw(data)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0