简体   繁体   中英

GLSL ES local variables crash?

I'm trying to implement some shaders from online tutorials (lighthouse3d.com) in my OpenGL ES 2.0 engine.

The problem is that for some reason, ANY variables declared in the scope of main() causes the entire shader to fail. for example, this fails:

void main(){
    vec4 color;     
    gl_FragColor = vec4(1.0, 0.5, 0.5, 1.0);
}

but this works perfectly:

void main(){
    //vec4 color;       
    gl_FragColor = vec4(1.0, 0.5, 0.5, 1.0);
}

Same thing happens with my vertex shaders.( EDIT :nvm, only seems to happen with fragment shaders) The only way to use any type of non-constant value is to use attributes, varyings, uniforms, etc. for example, this works as you would expect:

uniform sampler2D texture;
varying lowp vec4 fragcol;
varying lowp vec2 texco;

void main(){
    gl_FragColor = fragcol * texture2D(texture, texco);
}

Also, I'm having a hell of a lot of trouble trying to find documentation or resources specifically about GLSL ES (or whatever this version is called). All I've been able to find is this: http://old.siggraph.org/publications/2006cn/course16/KhronosSpecs/ESLanguageSpec1.10.11.pdf

This is all I could find related to variable declarations:

[snip]There are no default types. All variable and function declarations must have a declared type, and optionally qualifiers. A variable is declared by specifying its type followed by one or more names separated by commas.[snip]

And that is exactly what I did:

declared type: vec4

followed by one or more names: color ;

vec4 color

I'm clueless

EDIT: GLES20.glGetError() gives error 1282

GLSL ES differs from traditional GLSL in that it requires precision modifiers in order to specify a full type. Have you tried, eg:

void main(){
    lowp vec4 color;     
    gl_FragColor = vec4(1.0, 0.5, 0.5, 1.0);
}

You can also throw something like this into the top of a source file:

precision highp float;

To set the default precision, so you can omit it later on.

To get detailed information about an error compiling GLSL, you can use glGetProgramInfoLog (with assistance from glGetProgramiv ). Most GL implementations return a meaningful error and a line number. I'm sadly backward with Java, but in C you might do:

    glCompileShader(shader);

    // check whether compilation was successful; if not
    // then dump the log to the console
    GLint status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if(!status)
    {
        GLint logLength;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
        if (logLength > 0)
        {
            GLchar *log = (GLchar *)malloc(logLength);
            glGetShaderInfoLog(shader, logLength, &logLength, log);
            printf("Shader compile log:\n%s", log);
            free(log);
        }
    }

I've been tempering with those kind of error for a while... I don't know why precisely but it would seem that a declared variable crasshes the compiler if it is not used throughout the GLSL pipeline. It does not depend on the type of variable, or if it is initialized or not. This is a pain in the A** as there is no native intelisense for shader programs... You just have to be carefull (and patient!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM