Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a GLSL fragment shader I am trying to cast a float into an int. The compiler raises an error:

ERROR: 0:60: '=' :  cannot convert from 'mediump float' to 'highp int'

I tried raising the precision of the int:

mediump float indexf = floor(2.0 * mixer);
highp int index = indexf;

but to no avail.

How do I cast the int properly?

share|improve this question
For future reference, you will get better answers if you ask general programming questions like this one at Stack Overflow. - depending on community feedback this question might be migrated there anyway. – Ricket Feb 17 '11 at 18:48
For reinterpret casts, there are now functions such as floatBitsToInt. – jozxyqk Oct 10 '14 at 9:35
up vote 17 down vote accepted

Try this:

highp int index = int(indexf);

I found it here.

share|improve this answer
Thanks for the tip – dugla Feb 17 '11 at 19:10
Also note that casting a float to an int automatically floors it (at least in any implementation I've ever seen) so your call to floor should be unnecessary. – Ricket Feb 17 '11 at 20:03
In most languages the rounding mode is truncate / round-to-zero, which is equivalent to floor for positive numbers but not negative numbers. I don't remember if this applies to GLSL, but I'd be surprised if it doesn't. – user744 Feb 17 '11 at 20:19
@Joe That's an interesting fact that I never thought about, and I just confirmed it with a Java test case. – Ricket Feb 18 '11 at 3:05
Is highp important here? (On an Integer) – Léon Pelletier Jul 5 '15 at 6:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.