Skip to content

Commit c4aabad

Browse files
committed
opengl: Add error reporting at major positions
1 parent e2f90c1 commit c4aabad

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

examples/protonect/src/opengl_depth_packet_processor.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838

3939
#include <stdint.h>
4040

41+
#define CHECKGL() do { \
42+
for (GLenum glerror = glGetError(); glerror != GL_NO_ERROR; glerror = glGetError()) \
43+
LOG_ERROR << "line " << __LINE__ << ": GL error " << glerror; \
44+
} while(0)
45+
4146
namespace libfreenect2
4247
{
4348

@@ -164,6 +169,7 @@ struct ShaderProgram : public WithOpenGLBindings
164169
const GLchar *sources[] = {"#version 140\n", defines.c_str(), src.c_str()};
165170
vertex_shader = gl()->glCreateShader(GL_VERTEX_SHADER);
166171
gl()->glShaderSource(vertex_shader, 3, sources, NULL);
172+
CHECKGL();
167173
}
168174

169175
void setFragmentShader(const std::string& src)
@@ -172,6 +178,7 @@ struct ShaderProgram : public WithOpenGLBindings
172178
const GLchar *sources[] = {"#version 140\n", defines.c_str(), src.c_str()};
173179
fragment_shader = gl()->glCreateShader(GL_FRAGMENT_SHADER);
174180
gl()->glShaderSource(fragment_shader, 3, sources, NULL);
181+
CHECKGL();
175182
}
176183

177184
void bindFragDataLocation(const std::string &name, int output)
@@ -221,6 +228,7 @@ struct ShaderProgram : public WithOpenGLBindings
221228
gl()->glGetProgramInfoLog(program, sizeof(error_buffer), NULL, error_buffer);
222229
LOG_ERROR << "failed to link shader program!" << std::endl << error_buffer;
223230
}
231+
CHECKGL();
224232
}
225233

226234
GLint getAttributeLocation(const std::string& name)
@@ -234,6 +242,7 @@ struct ShaderProgram : public WithOpenGLBindings
234242
if(idx == -1) return;
235243

236244
gl()->glUniform1i(idx, value);
245+
CHECKGL();
237246
}
238247

239248
void setUniform(const std::string& name, GLfloat value)
@@ -242,6 +251,7 @@ struct ShaderProgram : public WithOpenGLBindings
242251
if(idx == -1) return;
243252

244253
gl()->glUniform1f(idx, value);
254+
CHECKGL();
245255
}
246256

247257
void setUniformVector3(const std::string& name, GLfloat value[3])
@@ -250,6 +260,7 @@ struct ShaderProgram : public WithOpenGLBindings
250260
if(idx == -1) return;
251261

252262
gl()->glUniform3fv(idx, 1, value);
263+
CHECKGL();
253264
}
254265

255266
void setUniformMatrix3(const std::string& name, GLfloat value[9])
@@ -258,11 +269,13 @@ struct ShaderProgram : public WithOpenGLBindings
258269
if(idx == -1) return;
259270

260271
gl()->glUniformMatrix3fv(idx, 1, false, value);
272+
CHECKGL();
261273
}
262274

263275
void use()
264276
{
265277
gl()->glUseProgram(program);
278+
CHECKGL();
266279
}
267280
};
268281

@@ -302,6 +315,7 @@ struct Texture : public WithOpenGLBindings
302315
{
303316
gl()->glActiveTexture(unit);
304317
glBindTexture(GL_TEXTURE_RECTANGLE, texture);
318+
CHECKGL();
305319
}
306320

307321
void allocate(size_t new_width, size_t new_height)
@@ -326,13 +340,15 @@ struct Texture : public WithOpenGLBindings
326340
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
327341
glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
328342
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, FormatT::InternalFormat, width, height, 0, FormatT::Format, FormatT::Type, 0);
343+
CHECKGL();
329344
}
330345

331346
void upload()
332347
{
333348
bindToUnit(GL_TEXTURE0);
334349
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
335350
glTexSubImage2D(GL_TEXTURE_RECTANGLE, /*level*/0, /*xoffset*/0, /*yoffset*/0, width, height, FormatT::Format, FormatT::Type, data);
351+
CHECKGL();
336352
}
337353

338354
void download()
@@ -343,6 +359,7 @@ struct Texture : public WithOpenGLBindings
343359
void downloadToBuffer(unsigned char *data)
344360
{
345361
glReadPixels(0, 0, width, height, FormatT::Format, FormatT::Type, data);
362+
CHECKGL();
346363
}
347364

348365
void flipY()
@@ -482,6 +499,11 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
482499
debug.gl(b);
483500
}
484501

502+
static void glfwErrorCallback(int error, const char* description)
503+
{
504+
LOG_ERROR << "GLFW error " << error << " " << description;
505+
}
506+
485507
void checkFBO(GLenum target)
486508
{
487509
GLenum status = gl()->glCheckFramebufferStatus(target);
@@ -490,6 +512,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
490512
LOG_ERROR << "incomplete FBO " << status;
491513
exit(-1);
492514
}
515+
CHECKGL();
493516
}
494517

495518
void initialize()
@@ -629,6 +652,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
629652
GLint texcoord_attr = stage1.getAttributeLocation("InputTexCoord");
630653
gl()->glVertexAttribPointer(texcoord_attr, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(2 * sizeof(float)));
631654
gl()->glEnableVertexAttribArray(texcoord_attr);
655+
CHECKGL();
632656
}
633657

634658
void deinitialize()
@@ -696,6 +720,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
696720

697721
gl()->glBindVertexArray(square_vao);
698722
glDrawArrays(GL_TRIANGLES, 0, 6);
723+
CHECKGL();
699724

700725
if(ir != 0)
701726
{
@@ -729,6 +754,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
729754

730755
stage2.use();
731756
updateShaderParametersForProgram(stage2);
757+
CHECKGL();
732758

733759
if(config.EnableBilateralFilter)
734760
{
@@ -749,6 +775,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
749775

750776
gl()->glBindVertexArray(square_vao);
751777
glDrawArrays(GL_TRIANGLES, 0, 6);
778+
CHECKGL();
752779

753780
if(config.EnableEdgeAwareFilter)
754781
{
@@ -782,6 +809,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
782809
*depth = stage2_depth.downloadToNewFrame();
783810
}
784811
}
812+
CHECKGL();
785813

786814
if(do_debug)
787815
{
@@ -809,6 +837,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPe
809837

810838
glDrawArrays(GL_TRIANGLES, 0, 6);
811839
}
840+
CHECKGL();
812841

813842
params_need_update = false;
814843
}
@@ -818,6 +847,10 @@ OpenGLDepthPacketProcessor::OpenGLDepthPacketProcessor(void *parent_opengl_conte
818847
{
819848
GLFWwindow* parent_window = (GLFWwindow *)parent_opengl_context_ptr;
820849

850+
GLFWerrorfun prev_func = glfwSetErrorCallback(&OpenGLDepthPacketProcessorImpl::glfwErrorCallback);
851+
if (prev_func)
852+
glfwSetErrorCallback(prev_func);
853+
821854
// init glfw - if already initialized nothing happens
822855
if (glfwInit() == GL_FALSE)
823856
{

0 commit comments

Comments
 (0)