No uniform named "u_proj" in shader
I wrote a couple of shaders to render textures as grayscale instead of full color. I used these shaders with libGDX embedded in the SpriteBatch class and it worked. Then when I tried to use it with the built-in SpriteCache class it didn't work. I looked at the SpriteCache code and saw that it had installed several different uniforms which I tried to account for, but something seems to be wrong.
The SpriteCache class in libGDX sets up the following uniforms:
customShader.setUniformMatrix("u_proj", projectionMatrix);
customShader.setUniformMatrix("u_trans", transformMatrix);
customShader.setUniformMatrix("u_projTrans", combinedMatrix);
customShader.setUniformi("u_texture", 0);
This is my vertex shader:
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_proj;
uniform mat4 u_projTrans;
uniform mat4 u_trans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = a_position* u_proj * u_trans;
}
and this is the fragment shader:
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform u_projTrans;
void main() {
vec4 color = texture2D(u_texture, v_texCoords).rgba;
float gray = (color.r + color.g + color.b) / 3.0;
vec3 grayscale = vec3(gray + 0* u_projTrans[0][0]);
gl_FragColor = vec4(grayscale, color.a);
}
The error I am getting:
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: no uniform with name 'u_proj' in shader
...
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)ackends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
I think any of you guys know why this isn't working? There is a uniform named u_proj. Thanks everyone!
source to share
What Reto Coradi said is true, I forgot to put mat4 tag before u_projTrans, which helped me.
Then what Tenfour04 said helped a lot too! I didn't know about:
if (!shader.isCompiled()) throw new GdxRuntimeException("Couldn't compile shader: " + shader.getLog());
What helped me the most in the long run was finding that glsl would get rid of unused imports on compilation and that if you couldn't trick the compiler into thinking that unused imports in use were in use, the shader would compile and then crash at runtime ...
Libgdx has a static "pedantic" variable that you can set. If it is set to false, the application will not crash, if variables are sent to the shader that are not used by the shader, they will simply be ignored. The code in my libgdx program looked something like this:
ShaderProgram.pedantic = false;
Thank you for your help! Hope this helps someone in the future.
source to share
Make sure you check that the shaders are compiled and linked successfully. Your fragment shader won't compile:
uniform u_projTrans;
This variable declaration requires a type. It should be:
uniform mat4 u_projTrans;
You can use the following calls to check for errors while tuning your shader programs:
glGetShaderiv(shaderId, GL_COMPILE_STATUS, ...); glGetShaderInfoLog(shaderId, ...); glGetProgramiv(programId, GL_LINK_STATUS, ...); glGetProgramInfoLog(programId, ...);
source to share