16

I have managed to isolate the problem in this code:

var gl;
_main_web = function() {
    gl = document.getElementById("canvas").getContext("experimental-webgl");

    gl = WebGLDebugUtils.makeDebugContext(gl,
    function (err, funcName, args) {
        throw(WebGLDebugUtils.glEnumToString(err) + " was caused by call to " + funcName);
    }
    );

    vert_shader = gl.createShader(gl.VERTEX_SHADER);
    gl.shaderSource(vert_shader,"attribute vec4 vertex;attribute vec2 uv; void main(void) {gl_Position = vertex;}\n");
    gl.compileShader(vert_shader);
    if( !gl.getShaderParameter(vert_shader,gl.COMPILE_STATUS ) ) {
        throw 0;
    }
    frag_shader = gl.createShader(gl.FRAGMENT_SHADER);
    gl.shaderSource(frag_shader,"void main(void) { gl_FragColor = vec4(1.0,1.0,1.0,1.0); } \n");
    gl.compileShader(frag_shader);
    if( !gl.getShaderParameter(frag_shader,gl.COMPILE_STATUS) ) {
        throw 1;
    }
    program = gl.createProgram();
    gl.attachShader(program,vert_shader);
    gl.attachShader(program,frag_shader);
    gl.linkProgram(program);
    if( !gl.getProgramParameter(program,gl.LINK_STATUS) ) {
        throw 2;
    }

    vertexLocation = gl.getAttribLocation(program,"vertex");
    textureLocation = gl.getAttribLocation(program,"uv");
}

vertexLocation is alright, it is 0. But textureLocation is -1, what am I missing?

André Puel
  • 8,741
  • 9
  • 52
  • 83

1 Answers1

51

You're trying to get the location for an attribute that you declare but never use. Your vertex shader code is (expanded for clarity):

attribute vec4 vertex;
attribute vec2 uv;
void main(void) {
    gl_Position = vertex;
}

During the compilation of your shader "uv" will be identified as an unused parameter and be stripped out. Even if you assign it to a varying in this shader but don't ever use it in the fragment shader, it may still be stripped out because it's been identified as not contributing to the final fragment.

Toji
  • 33,927
  • 22
  • 105
  • 115
  • 1
    This point here: "Even if you assign it to a varying in this shader but don't ever use it in the fragment shader, it may still be stripped out" is what helped me in the end. I knew that unused attributes would be stripped away, but I didn't know that it could carry over between the vertex and fragment shaders. Thanks! – aclave1 Nov 17 '15 at 17:37