【问题标题】:Invalid fragment shader. Link cannot proceed片段着色器无效。链接无法继续
【发布时间】:2012-01-01 18:09:39
【问题描述】:

在我的项目开始时,我使用简单的Strings 来用代码填充我的两个Shaders。这看起来像这样:

    public final static String chunkDefaultVertexInit = ""
    +constantParameters
    +"precision mediump float;"

    +"uniform mat4 mPMatrix;"
    +"uniform mat4 mVMatrix;"
    +"uniform mat4 mMMatrix;"
    +"uniform mat4 mMVMatrix;"

    +"attribute vec4 Vertex;"
    +"attribute vec3 Normal;"
    +"attribute vec2 TexCoord;"

    +"varying vec3 normal;"
    +"varying vec2 uv;"
    +"varying vec4 positionM;"
    +"varying vec4 positionMV;";
    etc....

这对我有用,但不是很清楚。所以我考虑了如何让我的代码对每个人都更干净、更清晰。我的想法是,将我的全部代码放在一个真正的.cc 文件中,然后将其移动到 res/raw 文件夹中。说到做到。 我想通过 Inputstreams 读出我的代码并将其保存到一个字符串中。这也很好,所以我给着色器提供了字符串源。

所以...现在碰巧有一个问题,正如我所说,我还没有得到它。我什至对自己有点生气,因为我想了一个简单的方法来解决它,但我没有看到。

我什至展示了我输入的源代码……但它看起来是正确的! o.O

Log.i("Llama3D Shader",shaderCode);

(别担心奇怪的“调试 ID”,它是项目名称)

这是着色器的源代码:

顶点着色器:

//vertexshader
precision mediump float;

uniform mat4 mPMatrix;
uniform mat4 mVMatrix;
uniform mat4 mMMatrix;
uniform mat4 mMVMatrix;

attribute vec4 aVertex;
attribute vec3 aNormal;
attribute vec2 aTexCoord;

varying vec2 vecTexCoord;
varying vec3 vecNormal;
varying vec4 vecVertex[2];

void main() {
    gl_Position = mPMatrix * mMVMatrix * aVertex;
    vecVertex[0] = mMMatrix * aVertex;
    vecVertex[1] = mMVMatrix * aVertex;
    vecTexCoord = aTexCoord;
    vecNormal = normalize(vec3(mMMatrix * -vec4(aNormal,0.0)));
}

片段着色器:

#define MAX_POINT_LIGHTS 4
precision mediump float;

varying vec2 vecTexCoord;
varying vec3 vecNormal;
varying vec4 vecVertex[2];

uniform vec3 uVecEye;
uniform vec3 uPointLightPosition[MAX_POINT_LIGHTS];
uniform vec3 uPointLightColor[MAX_POINT_LIGHTS];
uniform sampler2D textureHandle;

vec3 V = normalize(uVecEye.xyz-vecVertex[1].xyz);
vec3 N = vNormal;

vec3 vecLight[MAX_POINT_LIGHTS];
vec4 pointDiffuse  = vec4(0.0);
vec4 pointSpecular = vec4(0.0);

vec4 ambient = vec4(0.2,0.2,0.2,1.0);
vec4 color = vec4(1.0,1.0,1.0,1.0);
vec4 matSpec = vec4(1.0,1.0,1.0,1.0);
vec4 lightSpec = vec4(1.0,1.0,1.0,1.0);
vec4 spec = matSpec * lightSpec;

float shininess = 20.0;

void main() {
    for (int i=0;i<MAX_POINT_LIGHTS;i++) {

        vecLight[i].xyz = vecVertex[0].xyz - uPointLightPosition[i].xyz;
        float vecDistance = length(vecLight[i].xyz);

        if (vecDistance<=25.0) {

            vecDistance = 1.0 - max(0.0,vecDistance)/25.0;
            vec3 L = normalize(vecLight[i]);
            vec3 R = normalize(reflect(L,N));
            float LND = max(0.0,dot(N,L)) * vecDistance;

            pointDiffuse += color * vec4(uPointLightColor[i].xyz,0.0) * LND;

            if (shininess!=0.0 && spec!=0.0) {
                pointSpecular += spec * pow(max(0.0,dot(R,V)),shininess) * LND;
            } else {
                pointSpecular += vec4(0.0,0.0,0.0,0.0);
            }
        }
    }
    vec4 colorTexture = texture2D(textureHandle,vec2(+vTexCoord.x,-vTexCoord.y));
    gl_FragColor = ambient + colorTexture * pointDiffuse + pointSpecular;
}

每次我尝试运行程序时,ShaderlogInfo 和 ProgramlogInfo 都会对我说:

片段着色器无效。链接无法继续。*

我是疯了还是瞎了?! 我希望你知道答案...我真的不知道...请帮助我!

【问题讨论】:

  • 编译链接日志是怎么说的?
  • 如何在eclipse中获取这些编译链接日志?显然我还没有看到它们:/
  • 这不是 Eclipse 的责任。着色器编译/链接错误是必须在代码中检查的内容。它们是着色器运行时编译的一部分。如果存在编译器/链接器错误,您必须使用代码获取它们并显示它们。我不知道 Android 和 Java 是如何处理它的,但是this page 解释了如何在 C/C++ 代码中做到这一点。
  • 是的,好吧,我确实这样做了 o.O 但是日志没有说其他任何内容。它仍然说:无效的片段着色器。链接无法继续。不是或多或少......它甚至说,当我将我的 Fragmentshader 减少到变化和一个“gl_FragColor = vec4(1.0,1.0,1.0,1.0);”时命令。问题可能是变化之一吗?
  • 你说的不可能。 shader 信息日志肯定没有说“链接无法继续”,因为着色器不执行 linking。那么 shader 信息日志对片段着色器说了什么?请发布您的日志打印代码。

标签: java android glsl shader fragment-shader


【解决方案1】:

你得到的日志来自程​​序链接阶段,glGetProgramInfoLog。

你需要调试的是片段着色器日志,glGetShaderInfoLog。

类似的东西:

def _compile(self, source):
    ptr = cast(c_char_p(source), POINTER(c_char))
    glShaderSource(self.id, 1, byref(ptr), None)
    glCompileShader(self.id)
    status = c_int(0)
    glGetShaderiv(self.id, GL_COMPILE_STATUS, byref(status))
    log = self.check()
    print(log),
    if not status.value:
        raise Exception(log)

def check(self):
    length = c_int(0)
    glGetShaderiv(self.id, GL_INFO_LOG_LENGTH, byref(length))
    log = create_string_buffer(length.value)
    glGetShaderInfoLog(self.id, length.value, None, log)
    return log.value

虽然这不是在 java 中,而是在 python 中,但它应该让您了解如何获取着色器编译日志。

在我的环境中编译你的着色器会给我这个日志,它可能对你有用也可能没用:

Vertex shader was successfully compiled to run on hardware.
WARNING: 0:2: warning(#260) Keyword 'precision' is supported in GLSL 1.3 
Fragment shader failed to compile with the following errors:
WARNING: 0:2: warning(#260) Keyword 'precision' is supported in GLSL 1.3
ERROR: 0:14: error(#143) Undeclared identifier vNormal
WARNING: 0:14: warning(#402) Implicit truncation of vector from size 1 to size 3. 
ERROR: 0:50: error(#143) Undeclared identifier vTexCoord
ERROR: 0:50: error(#216) Vector field selection out of range 'y'
ERROR: error(#273) 4 compilation errors.  No code generated

【讨论】:

  • 希望我能@Berserker 但我没有问这个问题:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-21
  • 2021-08-18
  • 1970-01-01
相关资源
最近更新 更多