着色器中没有名称统一的制服 [英] No uniform with name in shader

查看:111
本文介绍了着色器中没有名称统一的制服的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是所有这些着色器知识的新手,不是,我只是想替换LibGDX中的默认SpriteBatch渲染器.

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

我复制粘贴了默认的着色器,并尝试添加鼠标制服,但这在着色器中给了我名称为'u_mouse'的无制服".

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.

这是所有相关代码:

     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());

这是堆栈跟踪:

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)

推荐答案

这归结为以下事实:确定了顶点着色器的输出和片段着色器的输入后,链接器意识到未使用u_mouse使用任何 有效 代码.

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.

换句话说,虽然在着色器中使用u_mouse来计算v_color,但v_color在顶点阶段和片段阶段之间未正确链接.顶点着色器中的精度与片段着色器中的精度不匹配,因此就GLSL而言,顶点着色器中的v_color输出未使用.

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.

由于u_mouse不会影响您的链接程序,因此不会为其分配统一编号.更重要的是,这种情况至少应生成链接器警告.链接后,我将检查glGetProgramInfoLog (...)的输出.

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.

您可以通过在顶点着色器中声明v_color LOWP或在片段着色器中删除LOWP声明来纠正我刚刚讨论的所有内容.您只需要顶点着色器的输出和片段着色器的输入即可具有匹配的变量声明.

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.

这篇关于着色器中没有名称统一的制服的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆