Sign in to follow this  
Postie

Best practises for writing shaders

Recommended Posts

Postie    1559
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.

[b]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.[/b]

Share this post


Link to post
Share on other sites
Postie    1559
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
Bacterius    13165
[quote]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
MJP    19755
[quote name='Postie' timestamp='1354677226' post='5007291']
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?
[/quote]

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
C0lumbo    4411
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
Bacterius    13165
[quote name='C0lumbo' timestamp='1354695491' post='5007335']
mini uber shaders
[/quote]
[img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Share this post


Link to post
Share on other sites
Postie    1559
[quote name='Bacterius' timestamp='1354678193' post='5007292']
The relevant keyword is "uber shader", by the way, that should give you a lot more google hits on the subject.
[/quote]

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
eppo    4877
[quote name='Postie' timestamp='1354677226' post='5007291']
Cheers for clearing that up. I didn't even realise hlsl could use #include.
[/quote]

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
Postie    1559
[quote name='eppo' timestamp='1354709276' post='5007366']
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.
[/quote]

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
cephalo    739
Are we talking about dx9 shaders or dx11? I'm in the process of learning dx11 and porting stuff over, and I get the impression that 'best practice' has changed drastically in the jump. Most of the examples I've seen use separate files for pixel shaders and vertex shaders, and the effect framework seems to be deprecated.

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