Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

OpenGL Using options in shader

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a shader and I want to use an integer to store bits for various options.

Eg.

const int OPTION_POSITION = 2;
const int OPTION_COLOR = 4;
const int OPTION_NORMAL = 8;
const int OPTION_TEXTURE = 16;

void main()
{
if((options & OPTION_COLOR) == OPTION_COLOR) ...
if((options & OPTION_NORMAL) == OPTION_NORMAL) ...
}


The probem is you can't use & in OpenGL ES 2.0 so I have to check the bits are set without it. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
It probably depends on the GPU's capabilities. Perhaps the next generation will have bit manipulation capabilities. Desktop GPUs already have it.

Share this post


Link to post
Share on other sites

It probably depends on the GPU's capabilities. Perhaps the next generation will have bit manipulation capabilities. Desktop GPUs already have it.


Yeah I'm stuck on GLSL 1.20 at the moment. I ended up using a bool for each option.

Share this post


Link to post
Share on other sites
The most performant way to implement options like this is via shader permutations.

e.g. a simple system:// GLSL
void main()
{
#ifdef OPTION_COLOR
...
#endif
#ifdef OPTION_NORMAL
...
#endif
}

//C++
const int OPTION_COLOR = 1;
const int OPTION_NORMAL = 2;
const int END_PERMUTATION = 4;

void CompilePermutations( const char* shaderCode, Shader* shaders[END_PERMUTATION] )
{
for( int i=0; i!=END_PERMUTATION; ++i )
{
std::string code;
if( i & OPTION_COLOR ) code += "#define OPTION_COLOR";
if( i & OPTION_NORMAL ) code += "#define OPTION_NORMAL";
code += shaderCode;
shaders = CompileShader( code );
}
}
Shader* SelectPermutation( int options, Shader* shaders[END_PERMUTATION] )
{
return shaders[options];
}

Share this post


Link to post
Share on other sites

There is no & but you should consider how often you will use it
Easiest way is to process options comparing values from MSB to LSB before you use them


opt_tex = (options >= OPTION_TEXTURE)?1:0;
options -= opt_tex * OPTION_TEXTURE;
...
opt_pos = (options >= OPTION_POSITION)?1:0;


if you are planing to use only 4 options you should use fract() and masks

const float inverseNextMSB = 1.0/16.0;
const vec4 bits = vec4( 1.0,2.0,4.0,8.0);
const vec4 mask = vec4(0.5,0.5,0.5,0.0);
void main()
{
float normalizedOpts = options * inverseNextMSB;
vec4 opts = vec4( fract( bits* normalizedOpts ) );
opts -= opts.yzww * mask;
//at this point xyzw represent bits 0:NO, 0.5:YES from MSB to LSB (x: MSB, w:LSB)
...
}

Last thing I should try is to create const vec4 array of all possible combination (dumb but it should work)

If you want to make it fast just make all possible versions of shader and compile it using #define

Share this post


Link to post
Share on other sites

[quote name='V-man' timestamp='1321720306' post='4885634']
It probably depends on the GPU's capabilities. Perhaps the next generation will have bit manipulation capabilities. Desktop GPUs already have it.


Yeah I'm stuck on GLSL 1.20 at the moment. I ended up using a bool for each option.
[/quote]

There is nothing wrong with using bool and if and else, unless if it gives performance problems on your target GPU. Personally, I would have multiple shader versions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!