1
\$\begingroup\$

I am programming in Haxe (language compiling to multiple platforms) and I have written some shaders.

My fragment shader runs fine in html5, but when I try to compile for native (OS X and/or Neko, a VM for Haxe) I get a shader compilation error, but no details (I am using lime which is a platform abstraction that does these things for me).

Here is the shader:

precision mediump float;

varying vec4 v_color;

void main() {
    gl_FragColor = v_color;
}

Very simple as you can see. It runs fine in webGL, but it seems it won't compile in OpenGL. I am no expert in shaders so I have no idea what might be wrong. Am I using some syntax that only exists in webGL?

Also just in case, here is my vertex shader (which compiles fine):

attribute vec3 a_position;
attribute vec4 a_color;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;

varying vec4 v_color;

void main() {
    gl_Position = uMVMatrix * vec4(a_position, 1);
    v_color = a_color;
}
\$\endgroup\$
3
  • \$\begingroup\$ I tried removing the precision mediump float; part, and now the shader compiles. I don't understand, isn't that supposed to work in OpenGL too? \$\endgroup\$
    – nialna2
    Commented May 24, 2015 at 11:28
  • \$\begingroup\$ stackoverflow.com/questions/20126918/… \$\endgroup\$
    – glampert
    Commented May 24, 2015 at 18:19
  • 2
    \$\begingroup\$ BTW, I suggest self answering this question so that this info can be more visible to other having the same problem in the future. \$\endgroup\$
    – glampert
    Commented May 24, 2015 at 18:20

2 Answers 2

1
\$\begingroup\$

As suggested in the comments, the precision directives are not supported in the OpenGL version that haxe/lime compiles to.

Adding a #version 130 doesn't work either, maybe haxe or lime doesn't read these I don't know.

Anyway I found the solution reading through the lime samples:

#if !desktop
"precision mediump float;" +
#end
"varying vec4 v_color;

void main() {
    gl_FragColor = v_color;
}

That's not really beautiful but it works. If someone knows a way to get precision in desktop shaders, that would be better though.

\$\endgroup\$
1
  • \$\begingroup\$ I do the same thing. OpenGL 2.1 does not allow the precision directive, but OpenGL 2 ES requires them, so the same shader won't work in both. This page uses #ifdef in the shader as a workaround but I haven't tried that yet. \$\endgroup\$
    – amitp
    Commented Sep 11, 2015 at 18:32
0
\$\begingroup\$

WebGL is based on OpenGL ES. Precision qualifiers (like "precision mediump float;") were introduced in OpenGL ES to optimize performance on embedded systems with lower hardware (like smartphones). In desktop OpenGL these precision qualifier do nothing. They only exist for compatibillity to OpenGL ES / WebGL. Source: https://www.opengl.org/wiki/Type_Qualifier_%28GLSL%29#Precision_qualifiers

Pehraps you need a newer version of desktop gl to support those qualifiers?

\$\endgroup\$

You must log in to answer this question.

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