Search code examples
wpfwinformsperformancexnatile

WPF Slow Tilemap Rendering Performance


I've been working on migrating my existing tilemap editor based on XNA/WinForms to WPF. I'm rather new to WPF, but most of the concepts haven't been too difficult to grasp. I have a Viewbox embedded into a ScrollViewer, using the DrawImage method from the DrawingContext provided by the (Viewbox)derived OnRender method to draw the map. The problem is I cannot achieve anywhere near the same performance as with the XNA/WinForm version when a rather large amount of tiles are on screen(>1000 on my machine). I have implemented zooming and panning, but when I zoom out too far or pan when a large amount of tiles are on screen, there is major lag which ruins the user experience. I have taken several obvious optimization measures(freezing the source bitmap, culling tiles that cannot be seen, setting the bitmaps scaling mode to LowQuality, providing the Cache caching hint), but they don't seem to resolve the issue. The problem is obviously in the way WPF handles drawing textured quads with DrawingContext.

I have provided a sample application with all of the code necessary to reproduce the problem: SampleGridDrawingExample

Hold the middle mouse button and move the mouse to pan. It becomes more and more noticeable as the size of the window increases(obviously because more tiles are being rendered).

Is there a faster method than DrawingContext within WPF to draw large amounts of textured quads with reasonable performance?. Using XNA/WinForms I can draw ~50000 tiles in multiple layers with reasonable lag, whereas in WPF using my current approach going over 1000 causes noticeable lag(>10000 brings the application to a crawl).


Solution

  • It seems that with WPF alone, it's currently impossible to get anywhere near the performance of XNA. However after taking a look at Nick Gravelyns post on XNA integration, I settled on using the method listed there, as the performance is acceptable for the application I'm developing. After a bit of modification to the sample and subsequent stress testing, I managed to make it stable enough for production code. I highly suggest anyone who is interested in XNA->WPF integration to take a look at that post.