Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opengl es 2.0 GLSL compiling fails on OSX when using const

i'm a little frustrated. I'm about to use a mac (OS X mavericks) for coding stuff.

My shader works fine under windows 7 and android. When i'm running my app under OS X i'm getting the following compiler errors during runtime.

Exception in thread "LWJGL Application" java.lang.IllegalStateException: ERROR: 0:6: Initializer not allowed
ERROR: 0:6: Use of undeclared identifier 'light'
ERROR: 0:6: Use of undeclared identifier 'spec'
ERROR: 0:6: Use of undeclared identifier 'spec'
ERROR: 0:6: Use of undeclared identifier 'spec'

Fragment Shader Snippet:

 String fragmentShader =   
                "#ifdef GL_ES\n" +
                "precision mediump float;\n" +
                "#endif\n" +
                "uniform sampler2D cubeMap;\n"+
                "uniform sampler2D normalMap;"+                 
                "varying vec2 v_TexCoords;"+

                "void main() {\n"+

                "   const vec4 ambientColor = vec4(1.0, 1.0, 1.0, 1.0);" +
                "   const vec4 diffuseColor = vec4(1.0, 1.0, 1.0, 1.0);"+
                "   const vec3 light =  normalize(vec3(-0.5,-0.8,-1.0));"+

                "   vec3 camDir = normalize(vec3(v_TexCoords,0) - vec3(0.5,0.5,10.0));"+ 
                "   vec4 normalPxl = texture2D(normalMap,v_TexCoords);" +
                "   vec3 normal = normalize(normalPxl.xyz - vec3(0.5,0.5,0.5));"+ 


                "   vec3 reflectDir = reflect(camDir,normal);"+   

                "   float spec = pow(max(0.0,dot(camDir,reflect(light,normal))),4.0);"+
                "   gl_FragColor = vec4(texture2D(cubeMap, reflectDir.xy * 0.5+0.5).xyz,normalPxl.w-0.1)"+
....

I've red through OpenGL Specs and found nothing problematic with using const or declaring some variables in main.

Appreciate your Help

like image 402
fky Avatar asked Dec 03 '25 10:12

fky


2 Answers

Sleepless around 4am, this was nagging at me, and I finally figured out why. Long story short: this behavior is correct, and you might like to use #version 120 for your desktop shaders. Explanation follows...

There are two GLSL language standards at play here, ES 100 and Desktop 110. Contrary to the version numbers, 100 was standardized after 110, and contains some (but not all) features of Desktop 120.

There are two semantic features at play here, one is initialization of a local variable marked const, and the other is const-ness of built-in functions applied to constant arguments. So we need to look in two different specs at two different features to understand the behavior.

Both 100 and 110 allow initialization of a const local ONLY with a constant expression (100 §4.3.2 "Initializers for const declarations must be a constant expression” and 110 §4.3.2 "Initializers for const declarations must be formed from literal values, other const variables (not including function call paramaters [sic]), or expressions of these.”). This restriction is relaxed in GLSL 420.

100 and 110 differ in whether they allow built-in functions to participate in constant expressions. 110 does not (§4.3.3, "an expression whose operands are integral constant expressions, including constructors, but excluding function calls.”) but 100 does (§5.10, “[...] a built-in function call whose arguments are all constant expressions, with the exception of the texture lookup functions.”). This restriction is relaxed on the desktop in GLSL 120.

The relevant error from the Apple compiler unfortunately clipped into the horizontal scroll of the OP else I might have come to this conclusion sooner :(

"ERROR: 0:13: Initializer not allowed”
like image 142
OneSadCookie Avatar answered Dec 06 '25 06:12

OneSadCookie


I'll repost my comment here so that this could be closed out, but it appears that the GLSL compiler on OS X didn't like this line:

const vec3 light =  normalize(vec3(-0.5,-0.8,-1.0));

where a calculated value was being assigned to a constant. It may not optimize that out, where other compilers do. Replacing the normalization with the end vector it produces should fix this.

If you think this is a bug, I recommend filing it at http://bugreport.apple.com , because I know Apple's OpenGL driver team does pay attention to cases like this and has fixed them in the past.

like image 26
Brad Larson Avatar answered Dec 06 '25 04:12

Brad Larson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!