Using options in shader

Started by
4 comments, last by V-man 12 years, 4 months ago
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?
Advertisement
It probably depends on the GPU's capabilities. Perhaps the next generation will have bit manipulation capabilities. Desktop GPUs already have it.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

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.
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];
}

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

[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.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement