[Solved] [LibGDX] Shader problem is beginning to piss me off

I’ve written and used shaders for libgdx in the past, but never run into this stupid f**king problem.

“no uniform with name ‘u_texture’ in shader”


Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: no uniform with name 'u_texture' in shader
	at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:293)
	at com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:283)
	at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformi(ShaderProgram.java:307)

Here’s my shader fragment program


#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D u_sampler2D;
uniform vec2 u_resolution;

varying vec4 v_color;
varying vec2 v_texCoord0;

void main()
{
	vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color;
	vec2 fragPos = gl_FragCoord.xy / u_resolution.xy; //will give range in [0-1, 0-1]
	float dist = distance(fragPos, vec2(0.5, 0.5)); //distance to the center of the screen
	
	gl_FragColor = vec4(0, 0, dist, 1);
}

I’ve tried for so long to get this shit to work. None of my other shaders have this f**king problem.

And here’s the real kicker. I put my exact same shader code in a different libgdx project and it worked, but in this new project it keeps giving me this stupid f**king error.

I even tried setting the sampler2D’s name to u_texture and that didn’t work.

Um yeah you need to actually have that uniform in the shader, which you don’t. Of course it will throw the error if the uniform doesn’t exist. Try renaming the Sampler2D uniform again and then post whatever error you have.

I said that I did rename the sampler2D to u_texture and it gave me the exact same error with the exact same stack trace.

Could it be because I created this new project with libgdx 1.1 instead of the nightly 0.9.9 that was used on the previous projects? This is absolutely absurd.

You’re not actually using u_sampler2D even in the original shader, as you’re not using “color”, so the whole uniform is optimised away.

Cas :slight_smile:

That’s not the problem. I switched it to this and it still gave me that stupid error:


#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D u_sampler2D;
uniform vec2 u_resolution;

varying vec4 v_color;
varying vec2 v_texCoord0;

void main()
{
	vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color;
	vec2 fragPos = gl_FragCoord.xy / u_resolution.xy; //will give range in [0-1, 0-1]
	float dist = distance(fragPos, vec2(0.5, 0.5)); //distance to the center of the screen
	
	gl_FragColor = color * vec4(fragPos, 0, 1);
}

Also, like I said, I used the exact same shader code in another project and it worked. So, even in that one, the color wasn’t being used, but it still worked without errors.

Okay, I’ve solved this issue.

Like I said, for some reason that exact shader code worked in a previous version of LibGDX, but wouldn’t work the new one.

What I did:
I set the sampler2D to “u_texture”

uniform sampler2D u_texture;

AND I also had to use the “color” variable so that it wouldn’t get optimized out. So, my current fragment shader looks like this:


#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D u_texture;
uniform vec2 u_resolution;

varying vec4 v_color;
varying vec2 v_texCoord0;

const vec4 glowColor = vec4(1, 1, 1, 1);

void main()
{
	vec4 color = texture2D(u_texture, v_texCoord0) * v_color;
	vec2 fragPos = gl_FragCoord.xy / u_resolution.xy; //will give range in [0-1, 0-1]
	float dist = distance(fragPos, vec2(0.5, 0.5)); //distance to the center of the screen
	
	color = mix(color, glowColor, dist);
	gl_FragColor = color;
}

It’s very irritating how it works in previous LibGDX versions but not in the new one.

Unfortunately this is a GLSL issue - if an attribute isn’t used, then the compiler is free to completely remove it. There’s no requirement for the driver to still expose a dummy attribute that doesn’t do anything. I’m not sure I agree with that but that’s what the spec says.

I would guess that libGDX is now actually reporting when you try and set an invalid attribute rather than blindly ignoring them like it did before.

I believe this can be “fixed” (as in the issue is ignored like before) if you set [icode]ShaderProgram.pedantic = false;[/icode] before creating a ShaderProgram.

If I could kiss you right now I would, you magnificent bastard.
That’s why it was working in my previous project but not in the current one.