Question

I have an issue with WebGL.

Sometimes, when I add a new shader to my application, I start to get uniform4f: location not for current program error (displayed in Chrome console) every frame when I set an uniform of my image drawing shader in my 2D drawing functions.

While I got the same error a few times before, it was not very hard to fix them by doing something random unknown manipulations. However, when I added a screen warping shader, I tried everything I could, even glFinish (I think the error occurs because of desynchronization), but nothing helped.

The error occurs when I try to set a vec4 uniform of my 2D image shader in the console drawing function. If I swap the console drawing function and the menu drawing function, the menu is not drawn.

Here's the source code of my program creation and switching functions: http://pastebin.com/zDEWWgKV

The error stops if I do anything from the following list:

  • Close the menu.
  • Load a map and do some 3D rendering (causing switching of programs at least 4 times).
  • Enable screen warping (causing switching of programs 1 time).

Could anybody tell me, what may cause the error and how do I fix it?

Was it helpful?

Solution

It means the location you are using came from another program. Programs do not share locations, even if they are using the same shaders. Example:

// make a vertex and a fragment shader
var vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, vSource);
gl.compileShader(vs);

var fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, fSource);
gl.compileShader(fs);

// now make 2 identical programs from those shaders
var p1 = gl.createProgram();
gl.attachShader(p1, vs);
gl.attachShader(p1, fs);
gl.linkProgram(p1);

var p2 = gl.createProgram();
gl.attachShader(p2, vs);
gl.attachShader(p2, fs);
gl.linkProgram(p2);

// Get a uniform location from program1
var location1 = gl.getUniformLocation(p1, "someUniform");

// Try to use that location on program2
gl.useProgram(p2);
gl.uniform4fv(location1, [1, 2, 3, 4]);  // ERROR!!!

Trying to use a location from program 1 on program 2 is not allowed. You must get locations for program 2 to set uniforms on program 2.

OTHER TIPS

There's a function that sets projection matrices and runs every frame in my code. It iterates on all shaders and usePrograms them, but doesn't change GL.currentprogram, so if the last shader in the previous frame was Pic, and the first shader in the current frame is Pic too, but the loop has stopped on, for instance, Warp, my shader-selecting function would think that the current shader is Pic, while it's not, and won't change the shader.

I fixed it by setting GL.currentprogram to null and disabling current attribute arrays before iterating on all shaders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top