NeHatchick Verma
NeHatchick Verma

Reputation: 121

No uniform with name in shader

I'm new to all this shader stuff, and right not I'm simply trying to replace the default SpriteBatch renderer in LibGDX.

I copy pasted the default shader and tried to add a mouse uniform, but it gives me the 'No uniform with name 'u_mouse' in shader.

This is all the related code:

     String vertexShader = "attribute vec4 " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
             + "attribute vec4 " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
             + "attribute vec2 " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
             + "uniform mat4 u_projTrans;\n" //
             + "uniform vec2 u_mouse;\n" //
             + "varying vec4 v_color;\n" //
             + "varying vec2 v_texCoords;\n" //
             + "\n" //
             + "void main()\n" //
             + "{\n" //
             + "   v_color = u_mouse * " + ShaderProgram.COLOR_ATTRIBUTE + ";\n" //
             + "   v_texCoords = " + ShaderProgram.TEXCOORD_ATTRIBUTE + "0;\n" //
             + "   gl_Position =  u_projTrans * " + ShaderProgram.POSITION_ATTRIBUTE + ";\n" //
             + "}\n";
     String fragmentShader = 
             "#ifdef GL_ES\n" //
             + "#define LOWP lowp\n" //
             + "precision mediump float;\n" //
             + "#else\n" //
             + "#define LOWP \n" //
             + "#endif\n" //
             + "varying LOWP vec4 v_color;\n" //
             + "varying vec2 v_texCoords;\n" //
             + "uniform sampler2D u_texture;\n" //
             + "uniform vec2 u_mouse;\n" //
             + "void main()\n"//
             + "{\n" //
             + "  gl_FragColor = v_color * texture2D(u_texture,  u_mouse);\n" //
             + "}";


ShaderProgram shaderTest = new ShaderProgram(vertexShader, fragmentShader);

shaderTest.setUniformf("u_mouse", Gdx.input.getX(),  Gdx.input.getY());

This is the stack trace:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.IllegalArgumentException: no uniform with name 'u_mouse' in shader
    at     com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: java.lang.IllegalArgumentException: no uniform with name 'u_mouse' in shader
    at     com.badlogic.gdx.graphics.glutils.ShaderProgram.fetchUniformLocation(ShaderProgram.java:283)
    at com.badlogic.gdx.graphics.glutils.ShaderProgram.setUniformf(ShaderProgram.java:394)
    at com.nehatchick.games.wf.WhiteMountains.render(WhiteMountains.java:424)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:187)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)

Upvotes: 0

Views: 2128

Answers (3)

abhimanyud3dx
abhimanyud3dx

Reputation: 13

try calling it between batch begin and end batch.setShader(shaderTest); batch.begin(); shaderTest.setUniformf("u_mouse", Gdx.input.getX(), Gdx.input.getY()); batch.draw............... batch.end();

Upvotes: 0

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

This boils down to the fact that after the outputs of your vertex shader and inputs of your fragment shader are determined, the linker realizes that u_mouse is not used in any active code.

To put this another way, while u_mouse is used in your shader to compute v_color, v_color is not properly linked between the vertex and fragment stages. The precision in your vertex shader does not match the precision in your fragment shader, so as far as GLSL is concerned the output of v_color in your vertex shader is not used.

Since u_mouse does not affect your linked program, it is not assigned a uniform number. More importantly, this type of situation should be generating a linker warning at the least. I would check the output of glGetProgramInfoLog (...) after linking.

You can correct everything I just discussed by either declaring v_color LOWP in your vertex shader, or removing the LOWP declaration in the fragment shader. You simply need the output from your vertex shader and input to your fragment shader to have matching variable declarations.

Upvotes: 3

ssantos
ssantos

Reputation: 16526

Not sure, but maybe u_mouse is expecting a Vector2 variable. Try this.-

shaderTest.setUniformf("u_mouse", new Vector2(Gdx.input.getX(),  Gdx.input.getY()));

Upvotes: 0

Related Questions