1
\$\begingroup\$

My game generates small "minimaps" for each room you go to, which are rendered onto render targets then stored in textures and displayed on the world map. When the game is saved, each new minimap is saved on the hard drive. If the game is in full screen and the user minimizes the game, the minimaps that were generated, not loaded off the hard drive, will become blank.

Since I don't know exactly what render targets are doing in the background, I have no idea how to prevent this, or restore the textures if they are lost.

How can I prevent/workaround this problem?

EDIT:
I did do

textureToDisplay = (Texture2D) textureRenderTarget;

but alas, this conversion doesn't seem to prevent the issue from occuring. I believe this is because textureToDisplay is still a reference to a render target. Perhaps I need to perform a deep clone? If so, how?
Also, the preserve contents thing doesn't work for this situation either.

\$\endgroup\$
0
\$\begingroup\$

Store the rendertarget into a Texture2D like this:

Texture2D texture = (Texture2D)renderTarget;

The graphics device clear clears all rendertargets, so that is why you need to store it elsewhere. You could do a rendertargetusage.preserve ( https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.rendertargetusage.aspx ) but that comes with a performance cost. Rendertarget2D is based on Texture2D so the cast is usually better.

\$\endgroup\$
0
\$\begingroup\$

I found a fix:

Texture2D texture = createBlankTexture(128, 128);
Color[] data = new Color[128*128];
renderTarget.getData<Color>(data);
texture.setData<Color>(data);

If my understanding is correct, this moves the memory from VRAM into RAM. Casting the render target to a texture does not move the memory into RAM, so it was still subject to VRAM shenanigans.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.