DirectX adoptedPosted: February 20, 2011
GS used to take its sweet time with the CPU when you had the graph view open. This was because it wasn’t taking advantage of the GPU to draw things. Converting the drawing code over to DirectX turned out to be way easier than I anticipated, and resulting in halving CPU usage when the graph view’s open. I had to learn a bit about the way DirectX expects primitives, then I implemented some wrapping functions to emulate the old ones I had been using in System.Drawing.Graphics. I did have some problems though.
Firstly, the panel control that DirectX was drawing to was flickering. The panel has a bunch of event handlers for clicking and dragging which lets the user zoom, pan, and rotate the graph view. After a long time searching online, I found one person who suggested that a redraw is forced by the panel before the event is raised, which would leave a slight time gap before it is redrawn again. Surely enough, using two panels (using a separate one to draw to) resolved the issue. I just made the clickable one invisible and placed them in the same location.
A second issue was that DirectX only comes in 32 bit. That means GS now has to be 32 bit as well. While it was easy enough to change the build option, it introduced a bug that I didn’t find until the end of the day. Because I was only testing the graph view, the fact that the bot could no longer walk slipped by me. What does walking have to do with drawing the graph? It stumped me at first, then I remembered that I had changed to 32 bit earlier. Little did I know that user32.dll’s SendInput() requires a slightly different argument on 32 bit vs 64 bit. It asks for an array of INPUT structures, but the “type” field at the start of the structure is either 4 or 8 bytes long. When it still didn’t work, I grew pretty frustrated until I finally noticed that my third argument in the call to SendInput was still 40. The third argument is supposed to be the size of the INPUT structure. I made the argument Marshal.SizeOf(typeof(INPUT)), which I should have done at the beginning to save me all this trouble. Lesson learned!