Debugging.html (30057B)
1 <h1 id="content-title">Debugging</h1> 2 <h1 id="content-url" style='display:none;'>In-Practice/Debugging</h1> 3 <p> 4 Graphics programming can be a lot of fun, but it can also be a large source of frustration whenever something isn't rendering just right, or perhaps not even rendering at all! Seeing as most of what we do involves manipulating pixels, it can be difficult to figure out the cause of error whenever something doesn't work the way it's supposed to. Debugging these kinds of <em>visual</em> errors is different than what you're used to when debugging errors on the CPU. We have no console to output text to, no breakpoints to set on GLSL code, and no way of easily checking the state of GPU execution. 5 </p> 6 7 <p> 8 In this chapter we'll look into several techniques and tricks of debugging your OpenGL program. Debugging in OpenGL is not too difficult to do and getting a grasp of its techniques definitely pays out in the long run. 9 </p> 10 11 <h2>glGetError()</h2> 12 <p> 13 The moment you incorrectly use OpenGL (like configuring a buffer without first binding any) it will take notice and generate one or more user error flags behind the scenes. We can query these error flags using a function named <fun>glGetError</fun> that checks the error flag(s) set and returns an error value if OpenGL got misused: 14 </p> 15 16 <pre><code> 17 GLenum glGetError(); 18 </code></pre> 19 20 <p> 21 22 The moment <fun>glGetError</fun> is called, it returns either an error flag or no error at all. The error codes that <fun>glGetError</fun> can return are listed below: 23 </p> 24 25 <table> 26 <tr> 27 <th>Flag</th> 28 <th>Code</th> 29 <th>Description</th> 30 </tr> 31 <tr> 32 <td><var>GL_NO_ERROR</var></td> 33 <td>0</td> 34 <td>No user error reported since the last call to <fun>glGetError</fun>.</td> 35 </tr> 36 <tr> 37 <td><var>GL_INVALID_ENUM</var></td> 38 <td>1280</td> 39 <td>Set when an enumeration parameter is not legal.</td> 40 </tr> 41 <tr> 42 <td><var>GL_INVALID_VALUE</var></td> 43 <td>1281</td> 44 <td>Set when a value parameter is not legal.</td> 45 </tr> 46 <tr> 47 <td><var>GL_INVALID_OPERATION</var></td> 48 <td>1282</td> 49 <td>Set when the state for a command is not legal for its given parameters.</td> 50 </tr> 51 <tr> 52 <td><var>GL_STACK_OVERFLOW</var></td> 53 <td>1283</td> 54 <td>Set when a stack pushing operation causes a stack overflow.</td> 55 </tr> 56 <tr> 57 <td><var>GL_STACK_UNDERFLOW</var></td> 58 <td>1284</td> 59 <td>Set when a stack popping operation occurs while the stack is at its lowest point.</td> 60 </tr> 61 <tr> 62 <td><var>GL_OUT_OF_MEMORY</var></td> 63 <td>1285</td> 64 <td>Set when a memory allocation operation cannot allocate (enough) memory.</td> 65 </tr> 66 <tr> 67 <td><var>GL_INVALID_FRAMEBUFFER_OPERATION</var></td> 68 <td>1286</td> 69 <td>Set when reading or writing to a framebuffer that is not complete.</td> 70 </tr> 71 </table> 72 73 <p> 74 Within OpenGL's function documentation you can always find the error codes a function generates the moment it is incorrectly used. For instance, if you take a look at the documentation of <a href="http://docs.gl/gl3/glBindTextur%65" target="_blank"><function id='48'>glBindTexture</function></a> function, you can find all the user error codes it could generate under the <em>Errors</em> section. 75 </p> 76 77 <p> 78 The moment an error flag is set, no other error flags will be reported. Furthermore, the moment <fun>glGetError</fun> is called it clears all error flags (or only one if on a distributed system, see note below). This means that if you call <fun>glGetError</fun> once at the end of each frame and it returns an error, you can't conclude this was the only error, and the source of the error could've been anywhere in the frame. 79 </p> 80 81 <note> 82 Note that when OpenGL runs distributedly like frequently found on X11 systems, other user error codes can still be generated as long as they have different error codes. Calling <fun>glGetError</fun> then only resets one of the error code flags instead of all of them. Because of this, it is recommended to call <fun>glGetError</fun> inside a loop. 83 </note> 84 85 <pre><code> 86 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, tex); 87 std::cout << glGetError() << std::endl; // returns 0 (no error) 88 89 <function id='52'>glTexImage2D</function>(GL_TEXTURE_3D, 0, GL_RGB, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, data); 90 std::cout << glGetError() << std::endl; // returns 1280 (invalid enum) 91 92 <function id='50'>glGenTextures</function>(-5, textures); 93 std::cout << glGetError() << std::endl; // returns 1281 (invalid value) 94 95 std::cout << glGetError() << std::endl; // returns 0 (no error) 96 </code></pre> 97 98 <p> 99 The great thing about <fun>glGetError</fun> is that it makes it relatively easy to pinpoint where any error may be and to validate the proper use of OpenGL. Let's say you get a black screen and you have no idea what's causing it: is the framebuffer not properly set? Did I forget to bind a texture? By calling <fun>glGetError</fun> all over your codebase, you can quickly catch the first place an OpenGL error starts showing up. 100 </p> 101 102 <p> 103 By default <fun>glGetError</fun> only prints error numbers, which isn't easy to understand unless you've memorized the error codes. It often makes sense to write a small helper function to easily print out the error strings together with where the error check function was called: 104 </p> 105 106 <pre><code> 107 GLenum glCheckError_(const char *file, int line) 108 { 109 GLenum errorCode; 110 while ((errorCode = glGetError()) != GL_NO_ERROR) 111 { 112 std::string error; 113 switch (errorCode) 114 { 115 case GL_INVALID_ENUM: error = "INVALID_ENUM"; break; 116 case GL_INVALID_VALUE: error = "INVALID_VALUE"; break; 117 case GL_INVALID_OPERATION: error = "INVALID_OPERATION"; break; 118 case GL_STACK_OVERFLOW: error = "STACK_OVERFLOW"; break; 119 case GL_STACK_UNDERFLOW: error = "STACK_UNDERFLOW"; break; 120 case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break; 121 case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break; 122 } 123 std::cout << error << " | " << file << " (" << line << ")" << std::endl; 124 } 125 return errorCode; 126 } 127 #define glCheckError() glCheckError_(__FILE__, __LINE__) 128 </code></pre> 129 130 <p> 131 In case you're unaware of what the preprocessor directives <code>__FILE__</code> and <code>__LINE__</code> are: these variables get replaced during compile time with the respective file and line they were compiled in. If we decide to stick a large number of these <fun>glCheckError</fun> calls in our codebase it's helpful to more precisely know which <fun>glCheckError</fun> call returned the error. 132 </p> 133 134 <pre><code> 135 <function id='32'>glBindBuffer</function>(GL_VERTEX_ARRAY, vbo); 136 glCheckError(); 137 </code></pre> 138 139 <p> 140 This will give us the following output: 141 </p> 142 143 <img src="/img/in-practice/debugging_glgeterror.png" alt="Output of glGetError in OpenGL debugging."/> 144 145 <p> 146 <fun>glGetError</fun> doesn't help you too much as the information it returns is rather simple, but it does often help you catch typos or quickly pinpoint where in your code things went wrong; a simple but effective tool in your debugging toolkit. 147 </p> 148 149 150 <h2>Debug output</h2> 151 <p> 152 A less common, but more useful tool than <fun>glCheckError</fun> is an OpenGL extension called <def>debug output</def> that became part of core OpenGL since version 4.3. With the debug output extension, OpenGL itself will directly send an error or warning message to the user with a lot more details compared to <fun>glCheckError</fun>. Not only does it provide more information, it can also help you catch errors exactly where they occur by intelligently using a debugger. 153 </p> 154 155 <note> 156 Debug output is core since OpenGL version 4.3, which means you'll find this functionality on any machine that runs OpenGL 4.3 or higher. If they're not available, its functionality can be queried from the <code>ARB_debug_output</code> or <code>AMD_debug_output</code> extension. Note that OS X does not seem to support debug output functionality (as gathered online). 157 </note> 158 159 <p> 160 In order to start using debug output we have to request a debug output context from OpenGL at our initialization process. This process varies based on whatever windowing system you use; here we will discuss setting it up on GLFW, but you can find info on other systems in the additional resources at the end of the chapter. 161 </p> 162 163 <h3>Debug output in GLFW</h3> 164 <p> 165 Requesting a debug context in GLFW is surprisingly easy as all we have to do is pass a hint to GLFW that we'd like to have a debug output context. We have to do this before we call <fun><function id='20'>glfwCreateWindow</function></fun>: 166 </p> 167 168 <pre><code> 169 <function id='18'>glfwWindowHint</function>(GLFW_OPENGL_DEBUG_CONTEXT, true); 170 </code></pre> 171 172 <p> 173 Once we've then initialized GLFW, we should have a debug context if we're using OpenGL version 4.3 or higher. If not, we have to take our chances and hope the system is still able to request a debug context. Otherwise we have to request debug output using its OpenGL extension(s). 174 </p> 175 176 <note> 177 Using OpenGL in debug context can be significantly slower compared to a non-debug context, so when working on optimizations or releasing your application you want to remove GLFW's debug request hint. 178 </note> 179 180 <p> 181 To check if we successfully initialized a debug context we can query OpenGL: 182 </p> 183 184 <pre><code> 185 int flags; glGetIntegerv(GL_CONTEXT_FLAGS, &flags); 186 if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) 187 { 188 // initialize debug output 189 } 190 </code></pre> 191 192 <p> 193 The way debug output works is that we pass OpenGL an error logging function callback (similar to GLFW's input callbacks) and in the callback function we are free to process the OpenGL error data as we see fit; in our case we'll be displaying useful error data to the console. Below is the callback function prototype that OpenGL expects for debug output: 194 </p> 195 196 <pre><code> 197 void APIENTRY glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum severity, 198 GLsizei length, const char *message, const void *userParam); 199 </code></pre> 200 201 <p> 202 Given the large set of data we have at our exposal, we can create a useful error printing tool like below: 203 </p> 204 205 <pre><code> 206 void APIENTRY glDebugOutput(GLenum source, 207 GLenum type, 208 unsigned int id, 209 GLenum severity, 210 GLsizei length, 211 const char *message, 212 const void *userParam) 213 { 214 // ignore non-significant error/warning codes 215 if(id == 131169 || id == 131185 || id == 131218 || id == 131204) return; 216 217 std::cout << "---------------" << std::endl; 218 std::cout << "Debug message (" << id << "): " << message << std::endl; 219 220 switch (source) 221 { 222 case GL_DEBUG_SOURCE_API: std::cout << "Source: API"; break; 223 case GL_DEBUG_SOURCE_WINDOW_SYSTEM: std::cout << "Source: Window System"; break; 224 case GL_DEBUG_SOURCE_SHADER_COMPILER: std::cout << "Source: Shader Compiler"; break; 225 case GL_DEBUG_SOURCE_THIRD_PARTY: std::cout << "Source: Third Party"; break; 226 case GL_DEBUG_SOURCE_APPLICATION: std::cout << "Source: Application"; break; 227 case GL_DEBUG_SOURCE_OTHER: std::cout << "Source: Other"; break; 228 } std::cout << std::endl; 229 230 switch (type) 231 { 232 case GL_DEBUG_TYPE_ERROR: std::cout << "Type: Error"; break; 233 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: std::cout << "Type: Deprecated Behaviour"; break; 234 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: std::cout << "Type: Undefined Behaviour"; break; 235 case GL_DEBUG_TYPE_PORTABILITY: std::cout << "Type: Portability"; break; 236 case GL_DEBUG_TYPE_PERFORMANCE: std::cout << "Type: Performance"; break; 237 case GL_DEBUG_TYPE_MARKER: std::cout << "Type: Marker"; break; 238 case GL_DEBUG_TYPE_PUSH_GROUP: std::cout << "Type: Push Group"; break; 239 case GL_DEBUG_TYPE_POP_GROUP: std::cout << "Type: Pop Group"; break; 240 case GL_DEBUG_TYPE_OTHER: std::cout << "Type: Other"; break; 241 } std::cout << std::endl; 242 243 switch (severity) 244 { 245 case GL_DEBUG_SEVERITY_HIGH: std::cout << "Severity: high"; break; 246 case GL_DEBUG_SEVERITY_MEDIUM: std::cout << "Severity: medium"; break; 247 case GL_DEBUG_SEVERITY_LOW: std::cout << "Severity: low"; break; 248 case GL_DEBUG_SEVERITY_NOTIFICATION: std::cout << "Severity: notification"; break; 249 } std::cout << std::endl; 250 std::cout << std::endl; 251 } 252 </code></pre> 253 254 <p> 255 Whenever debug output detects an OpenGL error, it will call this callback function and we'll be able to print out a large deal of information regarding the OpenGL error. Note that we ignore a few error codes that tend to not really display anything useful (like <code>131185</code> in NVidia drivers that tells us a buffer was successfully created). 256 </p> 257 258 <p> 259 Now that we have the callback function it's time to initialize debug output: 260 </p> 261 262 <pre><code> 263 if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) 264 { 265 <function id='60'>glEnable</function>(GL_DEBUG_OUTPUT); 266 <function id='60'>glEnable</function>(GL_DEBUG_OUTPUT_SYNCHRONOUS); 267 glDebugMessageCallback(glDebugOutput, nullptr); 268 glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE); 269 } 270 </code></pre> 271 272 <p> 273 Here we tell OpenGL to enable debug output. The <code><function id='60'>glEnable</function>(GL_DEBUG_SYNCRHONOUS)</code> call tells OpenGL to directly call the callback function the moment an error occurred. 274 </p> 275 276 <h3>Filter debug output</h3> 277 <p> 278 With <fun>glDebugMessageControl</fun> you can potentially filter the type(s) of errors you'd like to receive a message from. In our case we decided to not filter on any of the sources, types, or severity rates. If we wanted to only show messages from the OpenGL API, that are errors, and have a high severity, we'd configure it as follows: 279 </p> 280 281 <pre><code> 282 glDebugMessageControl(GL_DEBUG_SOURCE_API, 283 GL_DEBUG_TYPE_ERROR, 284 GL_DEBUG_SEVERITY_HIGH, 285 0, nullptr, GL_TRUE); 286 </code></pre> 287 288 <p> 289 Given our configuration, and assuming you have a context that supports debug output, every incorrect OpenGL command will now print a large bundle of useful data: 290 </p> 291 292 <img src="/img/in-practice/debugging_debug_output.png" alt="Output of OpenGL debug output on a text console."/> 293 294 <h3>Backtracking the debug error source</h3> 295 <p> 296 Another great trick with debug output is that you can relatively easy figure out the exact line or call an error occurred. By setting a breakpoint in <fun>DebugOutput</fun> at a specific error type (or at the top of the function if you don't care), the debugger will catch the error thrown and you can move up the call stack to whatever function caused the message dispatch: 297 </p> 298 299 <img src="/img/in-practice/debugging_debug_output_breakpoint.png" alt="Setting a breaking and using the callstack in OpenGL to catch the line of an error with debug output."/> 300 301 <p> 302 It requires some manual intervention, but if you roughly know what you're looking for it's incredibly useful to quickly determine which call causes an error. 303 </p> 304 305 <h3>Custom error output</h3> 306 <p> 307 Aside from reading messages, we can also push messages to the debug output system with <fun>glDebugMessageInsert</fun>: 308 </p> 309 310 <pre><code> 311 glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, 312 GL_DEBUG_SEVERITY_MEDIUM, -1, "error message here"); 313 </code></pre> 314 315 <p> 316 This is especially useful if you're hooking into other application or OpenGL code that makes use of a debug output context. Other developers can quickly figure out any <em>reported</em> bug that occurs in your custom OpenGL code. 317 </p> 318 319 <p> 320 In summary, debug output (if you can use it) is incredibly useful for quickly catching errors and is well worth the effort in setting up as it saves considerable development time. You can find a source code example <a href="/code_viewer_gh.php?code=src/7.in_practice/1.debugging/debugging.cpp" target="_blank">here</a> with both <fun>glGetError</fun> and debug output context configured; see if you can fix all the errors. 321 </p> 322 323 <h2>Debugging shader output</h2> 324 <p> 325 When it comes to GLSL, we unfortunately don't have access to a function like <fun>glGetError</fun> nor the ability to step through the shader code. When you end up with a black screen or the completely wrong visuals, it's often difficult to figure out if something's wrong with the shader code. Yes, we have the compilation error reports that report syntax errors, but catching the semantic errors is another beast. 326 </p> 327 328 <p> 329 One frequently used trick to figure out what is wrong with a shader is to evaluate all the relevant variables in a shader program by sending them directly to the fragment shader's output channel. By outputting shader variables directly to the output color channels, we can convey interesting information by inspecting the visual results. For instance, let's say we want to check if a model has correct normal vectors. We can pass them (either transformed or untransformed) from the vertex shader to the fragment shader where we'd then output the normals as follows: 330 </p> 331 332 <pre><code> 333 #version 330 core 334 out vec4 FragColor; 335 in vec3 Normal; 336 [...] 337 338 void main() 339 { 340 [...] 341 FragColor.rgb = Normal; 342 FragColor.a = 1.0f; 343 } 344 </code></pre> 345 346 <p> 347 By outputting a (non-color) variable to the output color channel like this we can quickly inspect if the variable is, as far as you can tell, displaying correct values. If, for instance, the visual result is completely black it is clear the normal vectors aren't correctly passed to the shaders; and when they are displayed it's relatively easy to check if they're (sort of) correct or not: 348 </p> 349 350 <img src="/img/in-practice/debugging_glsl_output.png" alt="The image of a 3D model with its normal vectors displayed as the fragment shader output in OpenGL for debugging"/> 351 352 <p> 353 From the visual results we can see the world-space normal vectors appear to be correct as the right sides of the backpack model is mostly colored red (which would mean the normals roughly point (correctly) towards the positive x axis). Similarly, the front side of the backpack is mostly colored towards the positive z axis (blue). 354 </p> 355 356 <p> 357 This approach can easily extend to any type of variable you'd like to test. Whenever you get stuck and suspect there's something wrong with your shaders, try displaying multiple variables and/or intermediate results to see at which part of the algorithm something's missing or seemingly incorrect. 358 </p> 359 360 <h2>OpenGL GLSL reference compiler</h2> 361 <p> 362 Each driver has its own quirks and tidbits; for instance, NVIDIA drivers are more flexible and tend to overlook some restrictions on the specification, while ATI/AMD drivers tend to better enforce the OpenGL specification (which is the better approach in my opinion). The result of this is that shaders on one machine may not work on the other due to driver differences. 363 </p> 364 365 <p> 366 With years of experience you'll eventually get to learn the minor differences between GPU vendors, but if you want to be sure your shader code runs on all kinds of machines you can directly check your shader code against the official specification using OpenGL's GLSL <a href="https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/" target="_blank">reference compiler</a>. You can download the so called <def>GLSL lang validator</def> binaries from <a href="https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/" target="_blank">here</a> or its complete source code from <a href="https://github.com/KhronosGroup/glslang" target="_blank">here</a>. 367 </p> 368 369 <p> 370 Given the binary GLSL lang validator you can easily check your shader code by passing it as the binary's first argument. Keep in mind that the GLSL lang validator determines the type of shader by a list of fixed extensions: 371 <p> 372 373 <ul> 374 <li><code>.vert</code>: vertex shader.</li> 375 <li><code>.frag</code>: fragment shader.</li> 376 <li><code>.geom</code>: geometry shader.</li> 377 <li><code>.tesc</code>: tessellation control shader.</li> 378 <li><code>.tese</code>: tessellation evaluation shader.</li> 379 <li><code>.comp</code>: compute shader.</li> 380 </ul> 381 382 <p> 383 Running the GLSL reference compiler is as simple as: 384 </p> 385 386 <pre><code> 387 glsllangvalidator shaderFile.vert 388 </code></pre> 389 390 <p> 391 Note that if it detects no error, it returns no output. Testing the GLSL reference compiler on a broken vertex shader gives the following output: 392 </p> 393 394 <img src="/img/in-practice/debugging_glsl_reference_compiler.png" alt="Output of the GLSL reference compiler (GLSL lang validator) in OpenGL"/> 395 396 <p> 397 It won't show you the subtle differences between AMD, NVidia, or Intel GLSL compilers, nor will it help you completely bug proof your shaders, but it does at least help you to check your shaders against the direct GLSL specification. 398 </p> 399 400 <h2>Framebuffer output</h2> 401 <p> 402 Another useful trick for your debugging toolkit is displaying a framebuffer's content(s) in some pre-defined region of your screen. You're likely to use <a href="https://learnopengl.com/Advanced-OpenGL/Framebuffers" target="_blank">framebuffers</a> quite often and, as most of their magic happens behind the scenes, it's sometimes difficult to figure out what's going on. Displaying the content(s) of a framebuffer on your screen is a useful trick to quickly see if things look correct. 403 </p> 404 405 <note> 406 Note that displaying the contents (attachments) of a framebuffer as explained here only works on texture attachments, not render buffer objects. 407 </note> 408 409 <p> 410 Using a simple shader that only displays a texture, we can easily write a small helper function to quickly display any texture at the top-right of the screen: 411 </p> 412 413 <pre><code> 414 // vertex shader 415 #version 330 core 416 layout (location = 0) in vec2 position; 417 layout (location = 1) in vec2 texCoords; 418 419 out vec2 TexCoords; 420 421 void main() 422 { 423 gl_Position = vec4(position, 0.0f, 1.0f); 424 TexCoords = texCoords; 425 } 426 427 // fragment shader 428 #version 330 core 429 out vec4 FragColor; 430 in vec2 TexCoords; 431 432 uniform sampler2D fboAttachment; 433 434 void main() 435 { 436 FragColor = texture(fboAttachment, TexCoords); 437 } 438 </code></pre> 439 440 <pre><code> 441 void DisplayFramebufferTexture(unsigned int textureID) 442 { 443 if (!notInitialized) 444 { 445 // initialize shader and vao w/ NDC vertex coordinates at top-right of the screen 446 [...] 447 } 448 449 <function id='49'>glActiveTexture</function>(GL_TEXTURE0); 450 <function id='28'>glUseProgram</function>(shaderDisplayFBOOutput); 451 <function id='48'>glBindTexture</function>(GL_TEXTURE_2D, textureID); 452 <function id='27'>glBindVertexArray</function>(vaoDebugTexturedRect); 453 <function id='1'>glDrawArrays</function>(GL_TRIANGLES, 0, 6); 454 <function id='27'>glBindVertexArray</function>(0); 455 <function id='28'>glUseProgram</function>(0); 456 } 457 458 int main() 459 { 460 [...] 461 while (!<function id='14'>glfwWindowShouldClose</function>(window)) 462 { 463 [...] 464 DisplayFramebufferTexture(fboAttachment0); 465 466 <function id='24'>glfwSwapBuffers</function>(window); 467 } 468 } 469 </code></pre> 470 471 <p> 472 This will give you a nice little window at the corner of your screen for debugging framebuffer output. Useful, for example, for determining if the normal vectors of the geometry pass in a deferred renderer look correct: 473 </p> 474 475 <img src="/img/in-practice/debugging_fbo_output.png" alt="Framebuffer attachment output to a texture for debugging purposes in OpenGL"/> 476 477 <p> 478 You can of course extend such a utility function to support rendering more than one texture. This is a quick and dirty way to get continuous feedback from whatever is in your framebuffer(s). 479 </p> 480 481 <h2>External debugging software</h2> 482 <p> 483 When all else fails there is still the option to use a 3rd party tool to help us in our debugging efforts. Third party applications often inject themselves in the OpenGL drivers and are able to intercept all kinds of OpenGL calls to give you a large array of interesting data. These tools can help you in all kinds of ways like: profiling OpenGL function usage, finding bottlenecks, inspecting buffer memory, and displaying textures and framebuffer attachments. When you're working on (large) production code, these kinds of tools can become invaluable in your development process. 484 </p> 485 486 <p> 487 I've listed some of the more popular debugging tools here; try out several of them to see which fits your needs the best. 488 </p> 489 490 <!--<h3>gDebugger</h3> 491 <p> 492 gDebugger is a cross-platform and easy to use debugging tool for OpenGL applications. gDebugger sits alongside your running OpenGL application and provides a detailed overview of the running OpenGL state. You can pause the application at any moment to inspect the current state, texture memory and/or buffer usage. You can download gDebugger <a href="http://www.gremedy.com/" target="_blank">here</a>. 493 </p> 494 495 <p> 496 Running gDebugger is as easy as opening the application, creating a new project and giving it the location and working directory of your OpenGL executable. 497 </p> 498 499 <img src="/img/in-practice/debugging_external_gdebugger.png" alt="Image of gDebugger running on an OpenGL application."> 500 --> 501 502 <!--<h3>APItrace</h3> 503 <p> 504 <a href="https://github.com/apitrace/apitrace" target="_blank">APItrace</a> 505 </p>--> 506 507 <h3>RenderDoc</h3> 508 <p> 509 RenderDoc is a great (completely <a href="https://github.com/baldurk/renderdoc" target="_blank">open source</a>) standalone debugging tool. To start a capture, you specify the executable you'd like to capture and a working directory. The application then runs as usual, and whenever you want to inspect a particular frame, you let RenderDoc capture one or more frames at the executable's current state. Within the captured frame(s) you can view the pipeline state, all OpenGL commands, buffer storage, and textures in use. 510 </p> 511 512 <img src="/img/in-practice/debugging_external_renderdoc.png" alt="Image of RenderDoc running on an OpenGL application."/> 513 514 <h3>CodeXL</h3> 515 <p> 516 <a href="https://gpuopen.com/compute-product/codexl/" target="_blank">CodeXL</a> is GPU debugging tool released as both a standalone tool and a Visual Studio plugin. CodeXL gives a good set of information and is great for profiling graphics applications. CodeXL also works on NVidia or Intel cards, but without support for OpenCL debugging. 517 </p> 518 519 <img src="/img/in-practice/debugging_external_codexl.png" alt="Image of CodeXL running on an OpenGL application."/> 520 521 <p> 522 I personally don't have much experience with CodeXL since I found RenderDoc easier to use, but I've included it anyways as it looks to be a pretty solid tool and developed by one of the larger GPU manufacturers. 523 </p> 524 525 <h3>NVIDIA Nsight</h3> 526 <p> 527 NVIDIA's popular <a href="https://developer.nvidia.com/nvidia-nsight-visual-studio-edition" target="_blank">Nsight</a> GPU debugging tool is not a standalone tool, but a plugin to either the Visual Studio IDE or the Eclipse IDE (NVIDIA now has a <a href="https://developer.nvidia.com/nsight-graphics" target="_blank">standalone version</a> as well). The Nsight plugin is an incredibly useful tool for graphics developers as it gives a large host of run-time statistics regarding GPU usage and the frame-by-frame GPU state. 528 </p> 529 530 <p> 531 The moment you start your application from within Visual Studio (or Eclipse), using Nsight's debugging or profiling commands, Nsight will run within the application itself. The great thing about Nsight is that it renders an overlay GUI system from within your application that you can use to gather all kinds of interesting information about your application, both at run-time and during frame-by-frame analysis. 532 </p> 533 534 <img src="/img/in-practice/debugging_external_nsight.png" alt="Image of RenderDoc running on an OpenGL application."/> 535 536 <p> 537 Nsight is an incredibly useful tool, but it does come with one major drawback in that it only works on NVIDIA cards. If you are working on NVIDIA cards (and use Visual Studio) it's definitely worth a shot. 538 </p> 539 540 <p> 541 I'm sure there's plenty of other debugging tools I've missed (some that come to mind are Valve's <a href="https://github.com/ValveSoftware/vogl" target="_blank">VOGL</a> and <a href="https://apitrace.github.io/" target="_blank">APItrace</a>), but I feel this list should already get you plenty of tools to experiment with. 542 </p> 543 544 <h2>Additional resources</h2> 545 <ul> 546 <li><a href="http://retokoradi.com/2014/04/21/opengl-why-is-your-code-producing-a-black-window/" target="_blank">Why is your code producing a black window</a>: list of general causes by Reto Koradi of why your screen may not be producing any output.</li> 547 <li><a href="https://vallentin.dev/2015/02/23/debugging-opengl" target="_blank">Debug Output in OpenGL</a>: an extensive debug output write-up by Vallentin with detailed information on setting up a debug context on multiple windowing systems.</li> 548 </ul> 549 550 </div> 551 552 <div id="hover"> 553 HI 554 </div> 555 <!-- 728x90/320x50 sticky footer --> 556 <div id="waldo-tag-6196"></div> 557 558 <div id="disqus_thread"></div> 559 560 561 562 563 </div> <!-- container div --> 564 565 566 </div> <!-- super container div --> 567 </body> 568 </html>