• Popular Now

• 9
• 10
• 9
• 10
• 10

This topic is 1398 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites

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

Share on other sites

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

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

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

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