I'm learning OpenGL, and trying to do something which will inform me of any OpenGL errors occurred. unfortunately function glDebugMessageCallback
works only from GL version 4.3, while I have 3.0 (I didn't manage to update OpenGL). I've written the following function
static void GLCheckError(){
if (GLenum err = glGetError())
throw std::runtime_error("[OpenGL Error]" + std::to_string(err) + "\n");
}
It throws an exception if any OpenGL error has occurred.
Since I want any OpenGL function to inform me of any error, I need to place it after all gl*
function call. But I don't wanna do that, so
I wrote the following class.
class GLErrorChecker : private std::thread {
bool running = true;
unsigned int wait;
public:
GLErrorChecker(float time) : thread(&GLErrorChecker::run, this), wait(time) {}
void run(){
while (running) {
GLCheckError();
std::this_thread::sleep_for(std::chrono::seconds(wait));
}
}
void stop(){
running = false;
join();
}
};
Unfortunately it doesn't throw anything, while calling function GLCheckError
from main thread still does.
Here is my full code just in case.
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>
#include <unistd.h>
#include <string>
#include <fstream>
#include <thread>
static unsigned int CompileShader(unsigned int type, const std::string& source){
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result; /* = */ glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE) {
int length; glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char errmsg[length];
glGetShaderInfoLog(id, length, &length, errmsg);
glDeleteShader(id);
throw std::runtime_error("Failed to compile shader " + std::string(errmsg));
}
return id;
}
static unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader){
unsigned int prog = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glBindAttribLocation(prog, 0, "position");
glLinkProgram(prog);
glValidateProgram(prog);
glDeleteShader(vs);
glDeleteShader(fs);
return prog;
}
static void GLClearError(){
while (glGetError() != GL_NO_ERROR)
;
}
static void GLCheckError(){
if (GLenum err = glGetError())
throw std::runtime_error("[OpenGL Error]" + std::to_string(err) + "\n");
}
class GLErrorChecker : private std::thread {
bool running = true;
unsigned int wait;
public:
GLErrorChecker(float time) : thread(&GLErrorChecker::run, this), wait(time) {}
void run(){
while (running) {
GLCheckError();
std::this_thread::sleep_for(std::chrono::seconds(wait));
}
}
void stop(){
running = false;
join();
}
};
int main() {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
if (auto err = glewInit(); err != GLEW_OK){
std::cerr << "glewInit ERROR " << glewGetErrorString(err);
}
std::cout << glGetString(GL_VERSION) << '\n';
GLErrorChecker checker(1);
float points[] = {
-.5, -.5,
.5, -.5,
.5, .5,
-.5, .5,
};
unsigned int indicies[]{
0,1,2,
2,3,0,
};
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
unsigned int index_buffer_object;
glGenBuffers(1, &index_buffer_object);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_object);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_STATIC_DRAW);
std::ifstream t("../vshader.shader");
std::string vshader((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
t = std::ifstream("../fshader.shader");
std::string fshader((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
unsigned int shader = CreateShader(vshader, fshader);
glUseProgram(shader);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glDrawElements(GL_LINE_LOOP, sizeof(indicies) / sizeof(unsigned int), GL_INT, nullptr);
// GLCheckError();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
checker.stop();
return 0;
}
My question is why glGetError
works only from main thread?
glGetError
) there has to be a valid OpenGL Context which is the current context. The current context is a thread-local variable.glGetError
in a differnt thread, then it still would be unreliable, you might miss some errors. Since OpenGL 4.3 you can useglDebugMessageCallback
with this function you do not need theglGetError
anymore. If your plattform does not support OpenGL 4.3, e.g. if you use macOS, then you could use glad which would allow you to add pre and post hooks to each function.