Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Copying texture from rendertexture.  (Read 7730 times)

0 Members and 2 Guests are viewing this topic.

killerloader

  • Newbie
  • *
  • Posts: 28
    • View Profile
[SOLVED] Copying texture from rendertexture.
« on: February 22, 2015, 01:08:03 pm »
I want to use one rendertexture multiple times to create different images (As i've found deleting and creating new render textures to create massive memory leaks)
Basically i want to copy the 'texture' data from the rendertexture into a new sf::Texture.
I saw that you could save a render texture's texture to file, and then load it again into a new texture, however this would be pretty inefficient for per-step events.

Sorry if there is a simple function for this, i did look for a few minutes, but found no 'completely obvious' functions that do this.
As RenderTexture.GetTexture() will change as the render-texture changes.
As i was writing this, i thought of doing:
sf::Texture TestTexture;
TestTexture = RenderTexture.GetTexture();
Would this copy the texture data? or would it also keep a pointer to the texture data. I don't exactly have time to test it right now, so, sorry for a seemingly stupid question which i should probably hold off until i test that code myself.
« Last Edit: February 23, 2015, 11:09:04 am by killerloader »

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Copying texture from rendertexture.
« Reply #1 on: February 22, 2015, 01:55:29 pm »
Yes it does. A RenderTexture's getTexture() returns a const reference to the texture (so it is not compied). But calling the copy assignment or copy construction of sf::Texture, the entire texture is copied. Keep in mind, that this operation might be expensive.

Btw
sf::Texture TestTexture(RenderTexture.getTexture());
would be the call using the copy constructor.

/EDIT: I recomment using different instances of sf::RenderTexture instead one RenderTexture and copy to buffer-like Textures. Having multiple render textures might be faster because you don't need to copy the pixels and reupload it to the graphics card. But I'm not quite sure about those implementation details.
« Last Edit: February 22, 2015, 02:01:14 pm by Glocke »
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

killerloader

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Copying texture from rendertexture.
« Reply #2 on: February 23, 2015, 10:36:53 am »
Thankyou! sorry for the unorganised question haha.
I'm using it to create a 'map' of solids in my game. the textures only need to be created at the load time, or in edit mode when a solid is placed, so speed isn't a problem. I'm using it to remove blood/ effects from the top of solid objects.

I cannot use lots of rendertextures due to them not deallocating their memory after being destroyed.
I am already using lots of rendertextures for another system, but they can be reused.

This is solved.
« Last Edit: February 23, 2015, 11:10:42 am by killerloader »

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Copying texture from rendertexture.
« Reply #3 on: February 23, 2015, 11:33:44 am »
I cannot use lots of rendertextures due to them not deallocating their memory after being destroyed.

Where did you found that information? In general, a class shouldn't leak when its destructor was called correctly.
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

killerloader

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: [SOLVED] Copying texture from rendertexture.
« Reply #4 on: February 23, 2015, 11:37:24 am »
Not certain, i just gave up on trying to get around it.
http://en.sfml-dev.org/forums/index.php?topic=17235.0
eXpl0it3r says in that article that "It has most likely to do with the infamous context leak or a leak in the GPU driver."

I havn't tested it since getting new GPU drivers, so that may just be the problem.

Basically i created a loop which created and destroyed render textures, and the ram kept going up.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: [SOLVED] Copying texture from rendertexture.
« Reply #5 on: February 23, 2015, 11:46:13 am »
I cannot use lots of rendertextures due to them not deallocating their memory after being destroyed.
Where did you found that information? In general, a class shouldn't leak when its destructor was called correctly.
I think killerloader misinterpreted what was originally said. If they don't deallocate their memory after being destroyed then that would constitute a leak, even in the classical "everybody understands this" sense. The difference here is that we can't destroy any some contexts we create until right before the application ends due to operating system constraints, so this isn't the classical "leak" but more of a "we have no choice but to hold on to them until the end". Since RenderTextures each create their own context, creating many of them will create a lot of contexts as well, which, in some cases will not be able to be destroyed until the end of the application.

EDIT: Reworded to be more precise.
« Last Edit: February 23, 2015, 11:52:28 am by binary1248 »
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: [SOLVED] Copying texture from rendertexture.
« Reply #6 on: February 23, 2015, 11:49:15 am »
Since RenderTextures each create their own context, creating many of them will create a lot of contexts as well, which will not be able to be destroyed until the end of the application.
Thanks for that information! So (re)using a limited pool of render textures might be useful.
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: [SOLVED] Copying texture from rendertexture.
« Reply #7 on: February 23, 2015, 11:52:37 am »
Additionally, what I described above is what SFML has to do due to operating system constraints. Depending on your driver, the driver might leak memory as well (this is a "real leak"). If you have applications that create and destroy a lot of contexts, such as how SFML does it, the driver will slowly leak out memory over time. My guess is that this scenario doesn't get tested that thoroughly by the vendors, so it is something we have to live with.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

killerloader

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: [SOLVED] Copying texture from rendertexture.
« Reply #8 on: February 23, 2015, 11:55:11 am »
Thankyou binary, that actually made a lot of sense!

Anyways, after learning that it creates memory issues, i've begun reusing rendertextures.
This question is for a similar reason, i don't want to have to create any more render textures than i already have, so i'd rather just use one and copy it into textures where i can.

I don't think my GPU was contributing to the majority of the leak anyways, so that should be fine.

Gerard Wensink

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Github
    • Email
Re: [SOLVED] Copying texture from rendertexture.
« Reply #9 on: November 21, 2018, 11:33:32 am »
What does the function cost?
sf::Texture TestTexture(RenderTexture.getTexture());

 

anything