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?

  • 1
    \$\begingroup\$ For future reference, you will get better answers if you ask general programming questions like this one at Stack Overflow. stackoverflow.com - depending on community feedback this question might be migrated there anyway. \$\endgroup\$
    – Ricket
    Commented Feb 17, 2011 at 18:48
  • \$\begingroup\$ For reinterpret casts, there are now functions such as floatBitsToInt. \$\endgroup\$
    – jozxyqk
    Commented Oct 10, 2014 at 9:35
  • \$\begingroup\$ @jozxyqk In ES, supported since ES 3.0 \$\endgroup\$
    – bobobobo
    Commented Apr 2 at 0:23

1 Answer 1


Try this:

highp int index = int(indexf);

I found it here.

  • \$\begingroup\$ 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. \$\endgroup\$
    – Ricket
    Commented Feb 17, 2011 at 20:03
  • 10
    \$\begingroup\$ 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. \$\endgroup\$
    – user744
    Commented Feb 17, 2011 at 20:19
  • \$\begingroup\$ @Joe That's an interesting fact that I never thought about, and I just confirmed it with a Java test case. \$\endgroup\$
    – Ricket
    Commented Feb 18, 2011 at 3:05
  • 4
    \$\begingroup\$ Is highp important here? (On an Integer) \$\endgroup\$ Commented Jul 5, 2015 at 6:11
  • 1
    \$\begingroup\$ How cna I convert this to const so that I can sample an array? \$\endgroup\$ Commented Feb 7, 2019 at 20:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .