为什么这个glsl着色器在尝试使用该程序时会导致“无效操作”?

时间:2013-10-29 21:59:04

标签: opengl glsl shader core

我正在检查日志,一切都很好。着色器看起来像这样:

顶点:

#version 330 core
layout(location = 0) in struct InData {
    vec3 position;
    vec4 color;
} inData;
out struct OutData {
    vec3 position;
    vec4 color;
} outData;
void main()
{  
    outData.position = inData.position;
    outData.color = inData.color;
}

片段:

#version 330 core
in struct InData {
    vec2 position;
    vec4 color;
} inData;
out vec4 color;
void main(){
    color = inData.color;
}

我正在准备这样的着色器:

    public Shader(string src, ShaderType type)
    {

        shaderId = GL.CreateShader(type);
        GL.ShaderSource(shaderId, GetShader(src));
        GL.CompileShader(shaderId);
        EventSystem.Log.Message(GL.GetShaderInfoLog(shaderId));
        EventSystem.Log.Message("GLERROR: " + GL.GetError());
        packs = new List<ShaderPack>();
    }
    public void Attach(ShaderPack pack)
    {
        packs.Add(pack);
        GL.AttachShader(pack.ProgramID, shaderId);
        EventSystem.Log.Message(GL.GetProgramInfoLog(pack.ProgramID));
        EventSystem.Log.Message("GLERROR: " + GL.GetError());
    }

然后我编译着色器:

    public void Compile()
    {
        if(program >= 0)
            GL.DeleteProgram(program);
        program = GL.CreateProgram();
        foreach (var s in shaders.Values)
            s.Attach(this);
        EventSystem.Log.Message(GL.GetProgramInfoLog(program));
        EventSystem.Log.Message("GLERROR: " + GL.GetError());
    }

然后我正在尝试使用它:

        mesh = new Mesh();
        mesh.AddTriangle(
            new Vector3(0, 0, 0), new Vector4(1, 0, 0, 1),
            new Vector3(0, sizeY, 0), new Vector4(0, 1, 0, 1),
            new Vector3(sizeX, sizeY, 0), new Vector4(0, 0, 1, 1));
        mesh.RefreshBuffer();

        shaderPack.Apply();
        shaderPack.SetVertexAttribute<Mesh.MeshData1>("vertex", 0, mesh.meshData);
        EventSystem.Log.Message("GLERROR: " + GL.GetError());

在应用GL.UseProgram被调用,GetError返回“无效操作”

更新:

好的,我改变了代码:

    public void Compile()
    {
        if(program >= 0)
            GL.DeleteProgram(program);
        program = GL.CreateProgram();
        foreach (var s in shaders.Values)
            s.Attach(this);
       // GL.LinkProgram(program);
        //GL.ValidateProgram(program);

        GL.ValidateProgram(program);
        EventSystem.Log.Message("Validate: " + GL.GetProgramInfoLog(program) + " - " + GL.GetError());
    }
    public void Apply()
    {

        GL.UseProgram(program);
        EventSystem.Log.Message("GLERROR (Apply): " + GL.GetError());
    }

输出

  

[23:25:55] [Log]:验证: - NoError

     

[23:25:55] [日志]:GLERROR(申请):InvalidOperation

编辑:好的,我改变了顶点着色器:

#version 330 core
layout(location = 0) in struct InData {
    vec3 position;
    vec4 color;
} inData;
void main()
{  
    gl_Position = vec4(inData.position, 1);
}

...

#version 330 core
//in struct InData {
//    vec2 position;
//    vec4 color;
//} inData;
out vec4 color;

void main(){
    color = vec4(1,0,0,1);
}

它编译没有错误,但我有一个空白的屏幕......


预回滚:


编辑:好的,我怀疑问题出在这里:

    public void VertexAttribute<T>(int loc, ShaderPack p, T[] dataArray) where T : struct
    {
        int buf;
        GL.GenBuffers(1, out buf);
        GL.BindBuffer(BufferTarget.ArrayBuffer, buf);
        GL.BufferData<T>(BufferTarget.ArrayBuffer, (IntPtr)(dataArray.Length * Marshal.SizeOf(typeof(T))), dataArray, BufferUsageHint.StaticDraw);
        GL.EnableVertexAttribArray(0);
        GL.VertexAttribPointer<T>(loc, 2, VertexAttribPointerType.Float, false, 0, ref dataArray[0]);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

    }

我正在使用以下类型的数组传递它:

    public struct MeshData1
    {
        public Vector3 vertex;
        public Vector4 color;
        public MeshData1(Vector3 vertex, Vector4 color)
        {
            this.vertex = vertex;
            this.color = color;
        }
    }

输入如下:

#version 330 core
layout(location = 0) in struct InData {
    vec3 position;
    vec4 color;
} inData;
void main()
{  
    gl_Position = vec4(inData.position, 1.0);
}

我做错了什么?

1 个答案:

答案 0 :(得分:4)

立刻想到两个问题:

  1. 您从未在程序对象中链接已连接的着色器阶段(最重要的
  2. glGetProgramInfoLog (...)输出的字符串仅在链接或验证GLSL程序后生成/更新。
  3. 要解决此问题,您应在连接着色器后调用glLinkProgram (...),并了解这一点,直到执行此操作时,程序信息日志将不确定。

    <小时/> glValidateProgram (...)是更新程序信息日志内容的另一种方法。除了生成信息日志之外,此函数还将返回您的程序是否处于适合执行的状态。此操作的结果存储在名为GL_VALIDATE_STATUS的每个程序状态中。