Best practises for writing shaders

Started by
9 comments, last by cephalo 11 years, 4 months ago
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.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
Advertisement
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?
Regardless of whether you want to make an "everything shader" or not, you shouldn't have to re-implement a feature over and over again by copying and pasting code. Put that feature into a function, and put that function into a header file, then [font=courier new,courier,monospace]#include[/font] the header file wherever it's needed and call the function.
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]"Techniques" are a property of the Microsoft FX system, not something that necessarily has to exist. IMO, an "FX Technique" is a single shader, and an "FX File" is a collection of shaders.
Whether you want to include multiple techniques in an FX file (multiple shaders in a collection) is just an organizational choice. It makes no real difference.

The ability for FX Techniques to have multiple passes was more useful on older hardware, when people actually wrote multi-pass shaders all the time.
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?
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


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

mini uber shaders

tongue.png

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”


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.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

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);

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.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

This topic is closed to new replies.

Advertisement