无法将着色器链接到 OpenGL 中的程序对象,无法调试
Posted
技术标签:
【中文标题】无法将着色器链接到 OpenGL 中的程序对象,无法调试【英文标题】:Can't link shaders to program object in OpenGL, can't debug 【发布时间】:2015-02-09 19:22:09 【问题描述】:我将顶点和片段着色器附加到程序对象,然后尝试链接所述程序。 GL_LINK_STATUS 返回 false。我检查了信息日志,这是一堆乱码。我检查了GL_INFO_LOG_LENGTH,它是0。我该如何调试这种情况?
//this is the end of my LoadBasicShaders function
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
GLint logLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
std::vector<GLchar> log(logLength + 1);
glGetProgramInfoLog(program, logLength, &logLength, &log[0]);
fprintf(stderr, "%s\n\n", log);
//logLength returns 0, log returns seemingly random chars
return -3;//just my error code
我的着色器是最简单的,因为我才刚刚开始。 这是顶点着色器:
#version 330
layout(location = 0) in vec4 position;
void main()
gl_Position = position;
这是片段着色器:
#version 330
out vec4 outputColor;
void main()
outputColor = vec4(1.0, 1.0, 1.0, 1.0);
我使用 GLFW 创建 OpenGL 窗口,使用 GLEW 加载函数:
if (!glfwInit())
/*error checking*/
window = glfwCreateWindow(800, 600, "Swash", NULL, NULL);
if (!window)
/*more error checking*/
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
if (glewInit() != GLEW_OK)
/*you guessed it*/
//don't call anything that involves nonstandard OpenGL functions before this point
GLuint shaderProgram;
int shaderLoadResult = LoadBasicShaders(shaderProgram, "../res/vert.shader", "../res/frag.shader");
【问题讨论】:
发布MCVE,包括您的着色器和上下文创建。 @genpfault 我假设你的意思是 OpenGL 上下文创建,我还应该包括我如何从着色器文件中读取? 是的,创建窗口/上下文。您可以使用类似this 的方式将着色器文本内联到字符串中。使用in context。 【参考方案1】: 使用GLFW error callback 为您的着色器加载程序提供防弹 使用ARB_debug_output
or KHR_debug
通过glfwWindowHint()
请求明确的 GL 版本和配置文件(核心/兼容性)
一起(这就是我发布MCVE的意思):
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cstdlib>
struct GlDebugOutput
static void Install()
if( !glewIsSupported( "GL_ARB_debug_output" ) )
std::cerr << "GL_ARB_debug_output not supported" << std::endl;
return;
glEnable( GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB );
glDebugMessageCallbackARB( DebugCallback, 0 );
glDebugMessageControlARB( GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE );
private:
static const char* Source( GLenum source )
switch( source )
case GL_DEBUG_SOURCE_API_ARB : return "API";
case GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB : return "WINDOW_SYSTEM";
case GL_DEBUG_SOURCE_SHADER_COMPILER_ARB : return "SHADER_COMPILER";
case GL_DEBUG_SOURCE_THIRD_PARTY_ARB : return "THIRD_PARTY";
case GL_DEBUG_SOURCE_APPLICATION_ARB : return "APPLICATION";
case GL_DEBUG_SOURCE_OTHER_ARB : return "OTHER";
default : return "Unknown source";
static const char* Type( GLenum type )
switch( type )
case GL_DEBUG_TYPE_ERROR_ARB : return "ERROR";
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB : return "DEPRECATED_BEHAVIOR";
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB : return "UNDEFINED_BEHAVIOR";
case GL_DEBUG_TYPE_PORTABILITY_ARB : return "PORTABILITY";
case GL_DEBUG_TYPE_PERFORMANCE_ARB : return "PERFORMANCE";
case GL_DEBUG_TYPE_OTHER_ARB : return "OTHER";
default : return "Unknown type";
static const char* Severity( GLenum severity )
switch( severity )
case GL_DEBUG_SEVERITY_HIGH_ARB : return "HIGH";
case GL_DEBUG_SEVERITY_MEDIUM_ARB : return "MEDIUM";
case GL_DEBUG_SEVERITY_LOW_ARB : return "LOW";
default : return "Unknown severity";
static void APIENTRY DebugCallback
(
GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam
)
std::cerr << "GL_DEBUG"
<< ": " << Source( source )
<< ": " << Type( type )
<< ": " << Severity( severity )
<< ": " << message
<< std::endl;
;
struct Program
static GLuint Load( const char* vert, const char* geom, const char* frag )
GLuint prog = glCreateProgram();
if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
glLinkProgram( prog );
CheckStatus( prog );
return prog;
private:
static void CheckStatus( GLuint obj )
GLint status = GL_FALSE;
if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
if( status == GL_TRUE ) return;
GLchar log[ 1 << 15 ] = 0 ;
if( glIsShader(obj) ) glGetShaderInfoLog( obj, sizeof(log), NULL, log );
if( glIsProgram(obj) ) glGetProgramInfoLog( obj, sizeof(log), NULL, log );
std::cerr << log << std::endl;
exit( EXIT_FAILURE );
static void AttachShader( GLuint program, GLenum type, const char* src )
GLuint shader = glCreateShader( type );
glShaderSource( shader, 1, &src, NULL );
glCompileShader( shader );
CheckStatus( shader );
glAttachShader( program, shader );
glDeleteShader( shader );
;
#define GLSL(version, shader) "#version " #version "\n" #shader
const char* vert = GLSL
(
330,
layout( location = 0 ) in vec4 position;
void main()
gl_Position = position;
);
const char* frag = GLSL
(
330,
out vec4 outputColor;
void main()
outputColor = vec4( 1.0, 1.0, 1.0, 1.0 );
);
GLuint VAO;
GLuint VBO;
GLuint prog;
void init()
glGenVertexArrays( 1, &VAO );
glBindVertexArray( VAO );
glGenBuffers( 1,&VBO );
glBindBuffer( GL_ARRAY_BUFFER, VBO );
float verts[] =
-1.0, -1.0,
1.0, -1.0,
0.0, 1.0,
;
glBufferData( GL_ARRAY_BUFFER, sizeof( verts ), verts, GL_STATIC_DRAW );
prog = Program::Load( vert, NULL, frag );
glUseProgram( prog );
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, 0 );
void display()
glClearColor( 0, 0, 0, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glUseProgram( prog );
glBindVertexArray( VAO );
glDrawArrays( GL_TRIANGLES, 0, 3 );
void glfwErrorCallback( int error, const char* description )
std::cerr << "GLFW error: " << description << std::endl;
int main( int argc, char** argv )
glfwSetErrorCallback( glfwErrorCallback );
if( GL_FALSE == glfwInit() )
return EXIT_FAILURE;
std::cout << "GLFW version : " << glfwGetVersionString() << std::endl;
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE );
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
GLFWwindow* window = glfwCreateWindow( 640, 480, "Test", NULL, NULL );
if( NULL == window )
glfwTerminate();
return EXIT_FAILURE;
glfwMakeContextCurrent( window );
glfwSwapInterval( 1 );
glewExperimental = GL_TRUE;
const GLenum glewErr = glewInit();
if( GLEW_OK != glewErr )
std::cerr << "glewInit() failed: " << glewGetErrorString( glewErr ) << std::endl;
glfwTerminate();
return EXIT_FAILURE;
// consume spurious GL error from GLEW init
glGetError();
GlDebugOutput::Install();
init();
while( !glfwWindowShouldClose( window ) )
glfwPollEvents();
int w, h;
glfwGetFramebufferSize( window, &w, &h );
glViewport( 0, 0, w, h );
display();
glfwSwapBuffers( window );
return EXIT_SUCCESS;
如果您的 OpenGL 实现和/或着色器有任何问题,该程序应该向stderr
尖叫血腥谋杀并退出。
【讨论】:
在我将 userParams 参数设为 const 之前,您的代码无法正常工作。但它帮助很大,所以谢谢 @Yago:抱歉,我使用的 GLEW 过时,GLDEBUGPROCARB
的函数签名错误。【参考方案2】:
抱歉更新太慢了,我一直在忙其他事情。
无论如何,问题是在编写代码时,我已经包含了这一行
program = glCreateProgram();
但在某些时候,在整理时,我不小心删除了它。这就是着色器正在编译但没有链接的原因,它们没有链接到程序对象。
【讨论】:
以上是关于无法将着色器链接到 OpenGL 中的程序对象,无法调试的主要内容,如果未能解决你的问题,请参考以下文章