没有错误消息的 OpenGL 着色器链接错误
OpenGL shader linking error with no error message
很长一段时间以来,我一直在努力解决这个问题,但始终无法解决问题。我只想 compile/run 我的 OpenGL 程序,但我的着色器没有正确 link,而且我没有收到有用的错误消息。在一种情况下,错误纯粹是空的,而在另一种情况下,由于某种原因它是 ??y?
。
要创建我的程序并编译我的着色器,我有以下代码:
GLuint buildProgram(const char* vertexShaderFile, const char* fragmentShaderFile)
{
std::string vs = fileContents(vertexShaderFile);
std::string fs = fileContents(fragmentShaderFile);
return createProgram(vs.c_str(), fs.c_str());
}
着色器看起来像
#version 330
out vec4 out_color;
void main()
{
out_color = vec4(0.5, 0.5, 0.5, 0.5);
}
#version 330
in vec4 in_Position;
void main()
{
gl_Position = in_Position;
}
有
GLuint createProgram(const char* vertexSource, const char* fragmentSource)
{
GLuint vertexshader = createShader(vertexSource, GL_VERTEX_SHADER);
GLuint fragmentshader = createShader(fragmentSource, GL_FRAGMENT_SHADER);
GLuint program = glCreateProgram();
glAttachShader(program, vertexshader);
glAttachShader(program, fragmentshader);
glLinkProgram(program);
GLint ret;
checkShader(vertexshader, GL_LINK_STATUS, &ret, "unable to link vertex shader");
checkShader(fragmentshader, GL_LINK_STATUS, &ret, "unable to link fragment shader");
glUseProgram(program);
return program;
}
GLuint createShader(const char* source, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint isCompiled = 0;
checkShader(shader, GL_COMPILE_STATUS, &isCompiled, "shader failed to compile");
if (isCompiled == GL_FALSE)
{
glDeleteShader(shader);
return 0;
}
return shader;
}
我尝试通过
从着色器生成错误消息
void checkShader(GLuint shader, GLuint type, GLint* ret, const char* onfail)
{
switch(type) {
case(GL_COMPILE_STATUS):
glGetShaderiv(shader, type, ret);
if(*ret == false) {
int infologLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
GLchar buffer[infologLength];
GLsizei charsWritten = 0;
std::cout << onfail << std::endl;
glGetShaderInfoLog(shader, infologLength, &charsWritten, buffer);
std::cout << buffer << std::endl;
}
break;
case(GL_LINK_STATUS):
glGetProgramiv(shader, type, ret);
if(*ret == false) {
int infologLength = 0;
glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
GLchar buffer[infologLength];
GLsizei charsWritten = 0;
std::cout << onfail << std::endl;
glGetProgramInfoLog(shader, infologLength, &charsWritten, buffer);
std::cout << buffer << std::endl;
}
break;
default:
break;
};
}
运行 GLuint program = buildProgram("shaders/test.vert", "shaders/test.frag");
给我以下输出:
unable to link vertex shader
unable to link fragment shader
??y?
error build program
error 502 : GL_INVALID_OPERATION
有没有人猜到为什么我的着色器编译得很好但没有 link?
您正在 shader objects 上调用 getProgramiv(...,GL_LINK_STATUS,...)
。当您尝试获取有关 program 的信息(其中有只有一个)。对于 GL_LINK_STATUS
情况,以这种方式命名函数也没有意义。
很长一段时间以来,我一直在努力解决这个问题,但始终无法解决问题。我只想 compile/run 我的 OpenGL 程序,但我的着色器没有正确 link,而且我没有收到有用的错误消息。在一种情况下,错误纯粹是空的,而在另一种情况下,由于某种原因它是 ??y?
。
要创建我的程序并编译我的着色器,我有以下代码:
GLuint buildProgram(const char* vertexShaderFile, const char* fragmentShaderFile)
{
std::string vs = fileContents(vertexShaderFile);
std::string fs = fileContents(fragmentShaderFile);
return createProgram(vs.c_str(), fs.c_str());
}
着色器看起来像
#version 330
out vec4 out_color;
void main()
{
out_color = vec4(0.5, 0.5, 0.5, 0.5);
}
#version 330
in vec4 in_Position;
void main()
{
gl_Position = in_Position;
}
有
GLuint createProgram(const char* vertexSource, const char* fragmentSource)
{
GLuint vertexshader = createShader(vertexSource, GL_VERTEX_SHADER);
GLuint fragmentshader = createShader(fragmentSource, GL_FRAGMENT_SHADER);
GLuint program = glCreateProgram();
glAttachShader(program, vertexshader);
glAttachShader(program, fragmentshader);
glLinkProgram(program);
GLint ret;
checkShader(vertexshader, GL_LINK_STATUS, &ret, "unable to link vertex shader");
checkShader(fragmentshader, GL_LINK_STATUS, &ret, "unable to link fragment shader");
glUseProgram(program);
return program;
}
GLuint createShader(const char* source, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint isCompiled = 0;
checkShader(shader, GL_COMPILE_STATUS, &isCompiled, "shader failed to compile");
if (isCompiled == GL_FALSE)
{
glDeleteShader(shader);
return 0;
}
return shader;
}
我尝试通过
从着色器生成错误消息void checkShader(GLuint shader, GLuint type, GLint* ret, const char* onfail)
{
switch(type) {
case(GL_COMPILE_STATUS):
glGetShaderiv(shader, type, ret);
if(*ret == false) {
int infologLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
GLchar buffer[infologLength];
GLsizei charsWritten = 0;
std::cout << onfail << std::endl;
glGetShaderInfoLog(shader, infologLength, &charsWritten, buffer);
std::cout << buffer << std::endl;
}
break;
case(GL_LINK_STATUS):
glGetProgramiv(shader, type, ret);
if(*ret == false) {
int infologLength = 0;
glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &infologLength);
GLchar buffer[infologLength];
GLsizei charsWritten = 0;
std::cout << onfail << std::endl;
glGetProgramInfoLog(shader, infologLength, &charsWritten, buffer);
std::cout << buffer << std::endl;
}
break;
default:
break;
};
}
运行 GLuint program = buildProgram("shaders/test.vert", "shaders/test.frag");
给我以下输出:
unable to link vertex shader
unable to link fragment shader
??y?
error build program error 502 : GL_INVALID_OPERATION
有没有人猜到为什么我的着色器编译得很好但没有 link?
您正在 shader objects 上调用 getProgramiv(...,GL_LINK_STATUS,...)
。当您尝试获取有关 program 的信息(其中有只有一个)。对于 GL_LINK_STATUS
情况,以这种方式命名函数也没有意义。