I'm trying to find a way to have one RenderTexture that's reused multiple times throughout one frame. Basically something like this:
// Render stuff to a reusable RenderTexture
// Use the RenderTexture's texture in a sprite
// Clear the RenderTexture, render new stuff to it
// Use the RenderTexture's texture in a different sprite
// Clear the RenderTexture, render new stuff to it
// Use the RenderTexture's texture in a different sprite
// Clear the RenderTexture, render new stuff to it
// Use the RenderTexture's texture in a different sprite
// Clear the RenderTexture, render new stuff to it
// Use the RenderTexture's texture in a different sprite
// (etc.)
My previous attempt just used a ton of RenderTextures, one for each object that needed it, but since they're not lightweight objects like normal textures are (relatively speaking), that gets out of hand
really quickly, and causes a crash after too many have been allocated and memory runs out.
After that I tried just allocating a pool of RenderTextures and doling them out to whoever needed them, but RenderTextures can't be resized without
arduously destroying and recreating their context (it takes about a quarter of a second on my machine--completely impossible without the user noticing), having a ton of RenderTextures that I might never need uses a lot of memory, and having a fixed size creates an artificial limit that I don't really like hanging over my head.
Finally, I tried just copying the RenderTexture's texture to a new Texture and using that, but even using glCopyImageSubData the performance hit was still too much.
So none of these options are really all that great. I did try one other thing, but the results were kind of weird:
sf::RenderTexture renderTexture;
renderTexture.create(256, 256);
// RenderWindow creation and event loop boilerplate here
{
window.clear();
renderTexture.clear(sf::Color::Red);
renderTexture.display();
window.draw(QuickSprite{ 0, 0, renderTexture.getTexture() });
renderTexture.clear(sf::Color::Blue);
renderTexture.display();
window.draw(QuickSprite{ 256, 256, renderTexture.getTexture() });
window.display();
}
All this code is supposed to do is draw a red rectangle (via a RenderTexture) on one part of the screen and a blue one on another. I didn't really expect it to work...and it didn't. But what caught my attention was that instead of drawing two blue rectangles like I thought (or one of each color like I hoped), it drew two
red ones, and I can't imagine why. I also noticed while trying to figure this out that if I add
renderTexture.getTexture().copyToImage() before the call to
window.draw it works as I wanted, drawing a red and blue rectangle. Specifically
glGetTexImage does some magic to get it working, but that's a hack and I'm like 99% sure that even if it wasn't a performance hit I couldn't actually rely on it to do this completely unrelated task.
Anyway, this is just a very long-winded way of asking if there's any way I can get the reuse of RenderTextures working, because nothing I've come up with so far has really been satisfactory. I'd be super appreciative if anyone out there has any ideas and could share them with me.