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

Compiling 'ubershader' permutations

5 posts in this topic

Hi,

I'm implementing a shader framework where I have 1 ubershader and a bulk of different permutations/ 'defined combinations'. So when I render I select the right one, based on the scene and material.

 

Now I'm wondering if there's a better/ more efficient way to compile the whole bunch of combinations.

Below is the matrix of all possible combinations I need to compile.

 

Any input is appreciated, about ways to do this other then say 100+ lines of code with just different 'true's' and 'false's'.

I also thought about maybe defining some sort of 'key', to identify all the true's and false's with 1 key, which also might help in selecting the needed version of the effect.

	/** Possible defines and combinations

	Scene specific:		
	AMBIENT	FOG		Y/N
	DIRLIGHTS		Y/N
	POINTLIGHTS		Y/N
	NUM DIR LIGHTS		0/1/2/3
	NUM POINT LIGHTS	4/8

	Material specific:	
	NORMALMAP		Y/N
	SPECULAR		Y/N

	// might be more later on, specular map, gloss map etc.

	**/

/*	HLSL_DEFINES fullDefines;
	fullDefines.AmbientHemi		= true;
	fullDefines.Fog			= true;
	fullDefines.NormalMapping	= true;
	fullDefines.Specular		= true;
	fullDefines.DirLights		= true;
	fullDefines.PointLights		= true;
	fullDefines.NumDirLights	= 2;
	fullDefines.NumPointLights	= 8;*/

	if(!mEffects[0].Compile(mFilename, mD3ddev, mEffectPool, HLSL_DEFINES(true, true, true, true, true, true, 2, 8))) return false;

Edited by cozzie
0

Share this post


Link to post
Share on other sites

How about making a dword:

 

Assign each shader option the required amount of bits ie. for each Y/N fields 1 bit, num dir lights 2 bits, point lights 3 bits etc. Then calculate the total amount of bits you need.

 

Then you may create a loop:

 

HLSL_DEFINES MyDefines;

 

for(dword i = 0; i < 2^Numbits; ++i)

{

    DecodeShaderPermutation(i, MyDefines);   //decode bits fields from the loop counter value and update the HLSL_DEFINES based on the value

    Compile(...);       // profit

}

 

The loop counter will produce all the combinations required.

 

Cheers!

Edited by kauna
2

Share this post


Link to post
Share on other sites

How about making a dword:

 

Assign each shader option the required amount of bits ie. for each Y/N fields 1 bit, num dir lights 2 bits, point lights 3 bits etc. Then calculate the total amount of bits you need.

 

Then you may create a loop:

 

HLSL_DEFINES MyDefines;

 

for(dword i = 0; i < 2^Numbits; ++i)

{

    DecodeShaderPermutation(i, MyDefines);   //decode bits fields from the loop counter value and update the HLSL_DEFINES based on the value

    Compile(...);       // profit

}

 

The loop counter will produce all the combinations required.

 

Cheers!

 

 

I've done this at work, and it was quite successful. One added benefit is that by encoding permutations inside a dword you have a very easy way to name and refer to specific permutations. What might be called "uber_shader" out in your tools becomes a bunch of "uber_shader_XXXXXXXX" after the permutations have been compiled, where XXXXXXXX is the permutation index. Then when you want to select a shader, your material can contain a set of permutation bits (use normal map, use specular, etc), and the runtime game can provide the remaining bits (n point lights, 1 direction light), which get OR'ed together resulting in a full dword which can be used to quickly look up the shader you need to select.

Edited by Samith
1

Share this post


Link to post
Share on other sites

Thanks. To implement this if I understand the method right, I'm gonna need some more help I'm afraid.

 

- do I still have to save all possible combinations in objects of the HLSL_DEFINES struct?

- how do I go from the several true's and false's, to one DWORD? (and how can I determine what the DWORD/ index will be when I 'call' a specific permutation of the shader?

 

The only solution I found out till now was manually defining all (192) combinations (current situation), which I saved in a file, and read out line by line followed by compiling.

But the way you proposed above sounds cleaner (although for my programming level not understable yet).

 

How about passing 2 keys when I want to select the right effect? (the defines are 1) scene dependent, and 2) material dependent)

Edited by cozzie
0

Share this post


Link to post
Share on other sites

Well it requires some bitwork, ie. shifting and masking. First of all, you'll need to decide yourself the layout of the bits such as

 

bit 0 : Ambient fog (one bit is enough for yes/no)

bit 1 : Normal map 

bit 2-3 : Num Point lights (2 bits is enough for values between 0-3)

bit ... 

 

As the loop counter presents all the shader permutations, only thing left is to extract each bit field (variable "i" is the loop counter presented in the earlier post):

 

bool AmbientFog = i & 1; (the desired value is in the first bit so masking out other bits is enough)

bool NormalMap = (i >> 1) & 1; (the desired value is in the second bit so one shift is necessary and masking after)

int   NumPointLights = (i >> 2) & 3;

...

 

4 bits are used here (you'll need few bits more) so the loop needs to be run 2^4 times. 

 

Shifts are practically multiplies / divisions by 2^(shift value) which effectively will move the bits in the desired positions. After shifting is is necessary to use AND (&) operator to mask out unwanted bits.

 

The loop creating all the permutations doesn't know about the required usage - ie. it can't know whether a permutation is used or not. Nothing prevents you from creating permutations while program is running (and saving them to disk in order to accelerate the program operation). Of course this maybe produce slowdowns if a new shader permuation is required.

 

Cheers!

Edited by kauna
1

Share this post


Link to post
Share on other sites

Well it requires some bitwork, ie. shifting and masking. First of all, you'll need to decide yourself the layout of the bits such as

 

bit 0 : Ambient fog (one bit is enough for yes/no)

bit 1 : Normal map 

bit 2-3 : Num Point lights (2 bits is enough for values between 0-3)

bit ... 

 

As the loop counter presents all the shader permutations, only thing left is to extract each bit field (variable "i" is the loop counter presented in the earlier post):

 

bool AmbientFog = i & 1; (the desired value is in the first bit so masking out other bits is enough)

bool NormalMap = (i >> 1) & 1; (the desired value is in the second bit so one shift is necessary and masking after)

int   NumPointLights = (i >> 2) & 3;

...

 

4 bits are used here (you'll need few bits more) so the loop needs to be run 2^4 times. 

 

Shifts are practically multiplies / divisions by 2^(shift value) which effectively will move the bits in the desired positions. After shifting is is necessary to use AND (&) operator to mask out unwanted bits.

 

The loop creating all the permutations doesn't know about the required usage - ie. it can't know whether a permutation is used or not. Nothing prevents you from creating permutations while program is running (and saving them to disk in order to accelerate the program operation). Of course this maybe produce slowdowns if a new shader permuation is required.

 

Cheers!

Do i understand this approach correctly: You compile all the shader permutations in runtime (at startup for example)?

Is it possible to compile the shader permutations in build time, but still having the luxury of having them named nicely, so they can be identified at runtime by using the bitmask you suggested above?

Edited by pulo
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