Dima
Dima

Reputation: 1189

Fragment shader: No uniform with name in shader

I have problem with fragment shader in libgdx. Below is my fragment shader.

#ifdef GL_ES
precision mediump float;
#endif
uniform float u_aspectRatio;
varying vec2 v_texCoords;
uniform sampler2D u_texture;     
void main()                 
{                       
  gl_FragColor = texture2D(u_texture, v_texCoords);
}

In program I do

shader.setUniformi("u_texture", 0); // work fine
shader.setUniformf("u_aspectRatio", 0.0f); //no uniform with name 'u_aspectRatio' in shader

shader.isCompiled() return true and first set work fine, but in second I have error "no uniform with name 'u_aspectRatio' in shader". If delete line:

uniform float u_aspectRatio;

from shader all work fine, but when I add this line(in feature I want work with this object) and try set some data I have error.

Upvotes: 0

Views: 2509

Answers (3)

P.T.
P.T.

Reputation: 25177

The shader compiler will optimize away unused uniforms, so there is no u_aspectRatio uniform at run-time. See http://ogltotd.blogspot.com/2007/12/active-shader-uniforms-and-vertex.html

The other way to "fix" this shader would be to use the variable somewhere (e.g., multiply v_texCoords by it).

Upvotes: 3

Andreas Haferburg
Andreas Haferburg

Reputation: 5520

That's because the shader compiler optimizes away unused uniforms. You can ignore it. If you can't, use a shader program class that does.

Upvotes: 3

Vikalp Jain
Vikalp Jain

Reputation: 1419

I was having the same problem as you are getting but the following solution worked for me

Use float uniform Like this :

 int a = shader.getUniformLocation("u_aspectRatio");
 shader.setUniformf(a ,0.0f);

Upvotes: 2

Related Questions