c++ - 无法使用单独的着色器程序 "<program> object is not successfully linked."

标签 c++ opengl

 const char *vs_source =
      "#version 440\n\
      layout(location = 0) in vec2 position;\n\
      layout(location = 1) in float offset;\n\
      void main() {\n\
        vec2 new_pos = vec2(position.x + offset,position.y);\n\
        gl_Position = vec4(new_pos,0,1);\n\
                  }";

 const char *fs_source =
      "#version 440\n\
      out vec4 out_color;\n\
      void main() {\n\
        out_color = vec4(1,0,0,1);\n\
                  }";

 auto vsp = createShaderProgram(GL_VERTEX_SHADER,1,&vs_source);
 auto fsp = createShaderProgram(GL_FRAGMENT_SHADER,1,&fs_source);
 GLuint pipeline;
 glGenProgramPipelines(1,&pipeline);
 glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT  , vsp.handle);
 glUseProgramStages(pipeline, GL_FRAGMENT_SHADER_BIT, fsp.handle);
 glBindProgramPipeline(pipeline);

  struct Program{
      GLuint handle;
  };
  Program createShaderProgram(GLenum type,
                              GLsizei count,
                              const char **strings){
    const GLuint shader = glCreateShader(type);
    if (shader) {
      glShaderSource(shader, count, strings, NULL);
      glCompileShader(shader);
      const GLuint program = glCreateProgram();
      if (program) {
        GLint compiled = GL_FALSE;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
        glProgramParameteri(program, GL_PROGRAM_SEPARABLE, GL_TRUE);
        if (compiled) {
          glAttachShader(program, shader);
          glLinkProgram(program);
          glDetachShader(program, shader);
        }
        else{
          /* append-shader-info-log-to-program-info-log */
          GLint infoLogLength;
          glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);

          GLchar* infoLog = new GLchar[infoLogLength + 1];
          glGetShaderInfoLog(shader, infoLogLength, NULL, infoLog);

          std::cout << "ERROR: Unable to compile shader" << std::endl << infoLog << std::endl;
          delete[] infoLog;
        }
      }
      glDeleteShader(shader);
      return Program{program};
    } else {
      return Program{0};
    }
  }

OpenGL 告诉我

message: GL_INVALID_OPERATION error generated. <program> object is not successfully linked.
type: ERROR
HIGH

我不确定为什么。如果我以旧方式链接它并只创建一个程序,它会完美运行。

我猜我误用了新的管道系统?

我还应该提到错误仅在我开始调用时出现

 glUseProgramStages(pipeline, GL_VERTEX_SHADER_BIT  , vsp.handle);
 glUseProgramStages(pipeline, GL_FRAGMENT_SHADER_BIT, fsp.handle);

这可能是驱动程序错误吗?

最佳答案

解决了。顶点着色器没有链接。我不得不重新声明 gl_Position。

  const char *vs_source =
      "#version 440\n\
      layout(location = 0) in vec2 position;\n\
      layout(location = 1) in float offset;\n\
      out gl_PerVertex\n\
      {\n\
        vec4 gl_Position;\n\
        float gl_PointSize;\n\
        float gl_ClipDistance[];\n\
      };\n\
      void main() {\n\
        vec2 new_pos = vec2(position.x + offset,position.y);\n\
        gl_Position = vec4(new_pos,0,1);\n\
                  }";

关于c++ - 无法使用单独的着色器程序 "<program> object is not successfully linked.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809288/

相关文章:

c++ - llvm IRBuilder<>::CreateUDiv/CreateSDiv/CreateExactUDiv

c++ - 可视化 C++ 以帮助理解它

c++ - OpenGL Programming Guild 第八版,示例程序和 'NumVAOs'

linux - 加载 3D 纹理 : SDL/OpenGL on Linux Eclipse

python - 处理相机旋转的正确方法

C++ std::vector 迭代器行为奇怪,不允许递增

python - 如何将 Python 代码转换为 C++

c++ - 模板参数作为其他模板的参数

c++ - Opengl 数组纹理,适用于原始数据但不适用于图像数据

opengl - 渲染到 FBO + glReadPixels 全黑