Jump to content
  • Advertisement
Sign in to follow this  
Postie

Best practises for writing shaders

This topic is 2115 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,

So I'm a few years into my part-time game project and have been learning HLSL and DirectX as I go. I'm pretty confident in my ability to write HLSL shaders to do pretty much everything I need currently but have a question about the general architecture of shaders and how they should fit together.

For example, I have a terrain shader that handles some funky texture, tile-based lighting stuff and shadow mapping as well as some special effects, like highlighting an individual tile or drawing some special construction lines.

I also have an entity shader that I use for all the objects in my game and can also do Blinn-Phong shading.

I also have many other specific use shaders, such as a water shader, a fire shader.. etc etc...

I've realised that I now want to add the shadow mapping code to the entity shader, as I originally only implemented it in the terrain shader. But then it occurred to me that I'll probably want that feature in some of the other shaders down the track.

So my first question is, should I be implementing a given feature (eg shadow mapping) multiple times for every shader that needs it, or should I be trying to create a "it-does-everything" shader? And as a follow up, could I be writing my shaders to be more modular? I'm currently not using the ability to have multiple techniques or passes as I don't follow how they should be used.

Share this post


Link to post
Share on other sites
Advertisement
Cheers for clearing that up. I didn't even realise hlsl could use #include.

I had a feeling that multi-pass shaders had fallen out of favour in recent years. I'm guessing the reason for multi-pass shaders historically was to get around the lower instruction counts for the shader models at the time?

Share this post


Link to post
Share on other sites
So my first question is, should I be implementing a given feature (eg shadow mapping) multiple times for every shader that needs it, or should I be trying to create a "it-does-everything" shader? And as a follow up, could I be writing my shaders to be more modular? I'm currently not using the ability to have multiple techniques or passes as I don't follow how they should be used.[/quote]
The relevant keyword is "uber shader", by the way, that should give you a lot more google hits on the subject.

Share this post


Link to post
Share on other sites

I had a feeling that multi-pass shaders had fallen out of favour in recent years. I'm guessing the reason for multi-pass shaders historically was to get around the lower instruction counts for the shader models at the time?


Yes, and also as a workaround for hardware that didn't support multiple render targets.

Share this post


Link to post
Share on other sites
I think this is a really tricky problem to solve.

I've used uber-shaders, graph based solutions (similar to Unreal's) and fragment solutions (where little pieces of shader text are concatenated together) and they all have their merits.

For small projects though (e.g. 1-5 programmers, coders in control of shaders rather than artists), my current opinion is that the best approach is to have a handful of shaders (like you described, one for terrain, one for skinned characters, etc), but each of those shaders has a degree of customizability. e.g. My skinned character shader is actually a collection of 40 or so shader programs as the number of weights per bone, the number of lights, whether fogging is supported, etc, are all variable and a separate shader is compiled for each permutation. So for want of a better term, I think my preferred system is a collection of mini uber shaders. Edited by C0lumbo

Share this post


Link to post
Share on other sites

The relevant keyword is "uber shader", by the way, that should give you a lot more google hits on the subject.


Half the problem is knowing the right terminology. Now that I know how to describe what I'm looking for, I've found quite a lot of information on this topic. For what it's worth, I did try to google a solution before I created this post, I just never tried the keyword "uber shader". hehehe.

Thanks everyone, I reckon I'm on the right track now.

Share this post


Link to post
Share on other sites

Cheers for clearing that up. I didn't even realise hlsl could use #include.


If you feel the basic model of a language (like HLSL) is too static or too low-level, you can always levitate things by employing a secondary language as a metalanguage. For example, you could contruct a string of HLSL code in a scripting language (e.g. Lua or Python) and pass that through an HLSL compiler.

String hlsl;

if(diffuse) hlsl += do diffuse lighting();
if(spec) hlsl += do also specular lighting();
if(alpha) hlsl += calculate blend-factor();

String bytecode = compile(hlsl);

Share this post


Link to post
Share on other sites

If you feel the basic model of a language (like HLSL) is too static or too low-level, you can always levitate things by employing a secondary language as a metalanguage. For example, you could contruct a string of HLSL code in a scripting language (e.g. Lua or Python) and pass that through an HLSL compiler.


Interesting suggestion, but I don't think I'll have enough variations at this stage to warrant adding the extra complexity.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!