• 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
Postie

Best practises for writing shaders

10 posts in this topic

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]
1

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
[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.
2

Share this post


Link to post
Share on other sites
[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.
1

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
1

Share this post


Link to post
Share on other sites
[quote name='C0lumbo' timestamp='1354695491' post='5007335']
mini uber shaders
[/quote]
[img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]
0

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
[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);
1

Share this post


Link to post
Share on other sites
[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.
0

Share this post


Link to post
Share on other sites
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.
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