Jump to content

  • Log In with Google      Sign In   
  • Create Account


Best practises for writing shaders


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Postie   Members   -  Reputation: 930

Like
1Likes
Like

Posted 04 December 2012 - 06:22 PM

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

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 29680

Like
5Likes
Like

Posted 04 December 2012 - 06:46 PM

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

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

Edited by Hodgman, 04 December 2012 - 06:48 PM.


#3 Postie   Members   -  Reputation: 930

Like
0Likes
Like

Posted 04 December 2012 - 09:13 PM

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?
Currently working on an open world survival RPG - For info check out my Development blog: ByteWrangler

#4 Bacterius   Crossbones+   -  Reputation: 8658

Like
2Likes
Like

Posted 04 December 2012 - 09:29 PM

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.

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

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 MJP   Moderators   -  Reputation: 11010

Like
1Likes
Like

Posted 05 December 2012 - 12:42 AM

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.

#6 C0lumbo   Crossbones+   -  Reputation: 2214

Like
1Likes
Like

Posted 05 December 2012 - 02:18 AM

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, 05 December 2012 - 02:18 AM.


#7 Bacterius   Crossbones+   -  Reputation: 8658

Like
0Likes
Like

Posted 05 December 2012 - 02:23 AM

mini uber shaders

Posted Image

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#8 Postie   Members   -  Reputation: 930

Like
0Likes
Like

Posted 05 December 2012 - 02:53 AM

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

#9 eppo   Crossbones+   -  Reputation: 2406

Like
1Likes
Like

Posted 05 December 2012 - 06:07 AM

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

#10 Postie   Members   -  Reputation: 930

Like
0Likes
Like

Posted 06 December 2012 - 01:59 AM

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

#11 cephalo   Members   -  Reputation: 540

Like
0Likes
Like

Posted 06 December 2012 - 11:01 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS