4.30.2020

Visual Debugger 3

I started integrating Dear ImGui the other day; it's a UI framework that has been evangelized recommended to me multiple times and this seemed like a good opportunity to give it a shot. I'm sure I'll have more thoughts in the future as I use it more, but thus far it has been pretty easy - the verbose documentation, seemingly responsive support based on my Googling, and the ability to step through all the code when something isn't working have all made debugging issues much less frustrating.

However, I did hit one issue that was quite annoying and difficult to debug - my text was rendering as blocks. At first it was inconsistent, so I added it to my todo list to investigate, and kept working. Eventually I hit a state where it seemed to repro one-hundred percent of the time, which made it pretty difficult to iterate on UI code.


The initial inconsistency made me think that it was some sort of race condition - in fact, when I first encountered it and tried to step through with the debugger, the issue didn't occur. Seemingly, a heisenbug. My first thought was to dig into the font loading code of Dear ImGui, but as the callstack got deeper and the concepts became more foreign I slowly backed away. I peeked at the font texture it output, and while I wasn't certain the data looked "right", it didn't look bad.

Fun debugging trick: use the memory debug window to visualize data

Next, I started poking at the OpenGL texture data to see if if something was happening there. No GL Errors, but when Dear ImGui tried to render text, the texture size came back 0 x 0. This was mystifying, but did correlate with weirdness I had seen in gDebugger, a free OpenGL debugging tool: it had displayed the texture as invalid.

Teaser: the bug is in this picture, can you spot it?

I had found this post suggesting that it might be because the texture was too large for my GPU memory, and I had been futzing with my graphics card the other night... But the texture was 512x64 RGBA (4 bytes per pixel), so that's only 128KB. Still I eventually started looking around gDebugger to see if it would give me a picture of my GPU's memory. It didn't exactly give me that, but the picture it gave was still illuminating.


So I could now see that I had two Open GL contexts, whatever those were. The font texture belonged to the first one, which appeared to be deleted, and the second one had the empty texture I was looking at. At this point I went back to my code, where the bug certainly was, and realized that I probably shouldn't be destroying and recreating an OpenGL context (which maintains state used by OpenGL, like textures) every time I resized the window.

Tada!

No comments:

Post a Comment