I have a function in my program that renders a whole bunch of text to the screen as once. In order to cut down on the performance hit from calling a bunch of spritebatch.drawStrings each frame, I created a workaround by rendering all my text onto a RenderTarget2D in a patchwork fashion, which is then copied into a 2D array of Texture2D. Then instead of drawing several hundred strings to the screen, I only need to draw, say, four sprites, using new textures from the array, pieced together into the full “screen” of text.
This works pretty great when the textures have already been generated (no persistent performance hit). However, it’s a pretty significant performance hit whenever the contents of the textures need to be updated, causing a big ugly stutter where everything will freeze while it’s rendering to my patchwork textures. This is partially from the spritebatch.drawString calls, partially from then copying the texture data from the RenderTarget2D over into my Texture2D array elements. Either way it’s not great.
My first attempt to fix this was to try to move some of the operation to asynchronous execution, so the rest of the game wouldn’t lock up while it rendered. But that was essentially a nonstarter, because the texture operations can only run on the UI thread (so synchronously with the rest of my update/draw calls). I have also tried shifting around the (internal) render operation variously within the update and draw methods, with little difference.
Not sure if I’m missing something obvious that is causing this to be such a pain to fix, or that would bypass the performance hit entirely. Does anyone have any good ideas of workarounds or alternative approaches?
Edit: Here is the current operation in question
I am copying the texture data out of the RenderTarget2D because previously, trying to create a new RenderTarget each iteration was causing more performance issues, but I am open to alternative approaches to this.