Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Using options in shader


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Headkaze   Members   -  Reputation: 583

Like
0Likes
Like

Posted 18 November 2011 - 11:04 PM

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?

Sponsor:

#2 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 19 November 2011 - 10:31 AM

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);

#3 Headkaze   Members   -  Reputation: 583

Like
0Likes
Like

Posted 19 November 2011 - 07:06 PM

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.

#4 Hodgman   Moderators   -  Reputation: 31911

Like
0Likes
Like

Posted 19 November 2011 - 08:26 PM

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[i] = CompileShader( code );
	}
}
Shader* SelectPermutation( int options, Shader* shaders[END_PERMUTATION] )
{
	return shaders[options];
}


#5 SaTANO   Members   -  Reputation: 418

Like
1Likes
Like

Posted 19 November 2011 - 08:46 PM


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

#6 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 21 November 2011 - 06:09 AM


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.


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);




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS