SFML community forums

Help => Graphics => Topic started by: FloweyTF on June 22, 2020, 06:05:11 am

Title: Should I use sf::Texture or sf::Image
Post by: FloweyTF on June 22, 2020, 06:05:11 am
So, I’m writing a game, and there are a lot of different textures. Should I use a sf::Texture to store it or a sf::Image? There might be a issue with running out of graphics memory, but ram can be swapped (to my understanding you can’t swap gpu memory)
Title: Re: Should I use sf::Texture or sf::Image
Post by: Hapax on June 22, 2020, 10:11:31 pm
An sf::Texture is stored in graphics memory and an sf::Image is stored on the heap (in RAM).

To be able to apply an image to a shape to render it to a target, it must first be in graphics memory.
To clarify a little bit, an sf::Image is pixel data in memory that you can manipulate directly but to be able to render it, it must first be copied to an sf::Texture. All bitmap data for use by the graphics card are textures and need to be stored in their own memory.

If there is a possibility that the graphics card may run out of memory for storing textures, you should also provide lower resolution versions to use instead for lower memory cards.

One thing to note is that "a lot of different textures" could also mean "a lot of different bitmaps/images" so I should mention that you can store multiple images in a single texture and use a texture rectangle/the texture co-ordinates to select which part to use.
Title: Re: Should I use sf::Texture or sf::Image
Post by: FloweyTF on June 23, 2020, 05:28:11 am
Yes, I understand the difference, but due to concerns about taking up too much gpu memory, should I :
1) store texture in ram (sf::Image), then load to texture for drawing
2) store texture in gpu ram (sf::Texture), then draw directly
Title: Re: Should I use sf::Texture or sf::Image
Post by: Stauricus on June 23, 2020, 01:33:37 pm
there is no sense in doing the first, since you'll need to load it in a texture anyway.
sf::Image is ONLY for when you want to modify that image (like drawing), for everything else use a Texture.
Title: Re: Should I use sf::Texture or sf::Image
Post by: Hapax on June 23, 2020, 09:01:24 pm
Leave in sf::Texture. Transferring data between RAM and graphics memory is "expensive".
Title: Re: Should I use sf::Texture or sf::Image
Post by: FloweyTF on June 23, 2020, 09:43:02 pm
I’m just worried about running out of gpu ram (because as far as I know, it’s not virtual memory)
Title: Re: Should I use sf::Texture or sf::Image
Post by: Laurent on June 23, 2020, 10:09:04 pm
We could talk about this for hours, but we still know nothing about your problem. We could be much more efficient in helping you, if you told us a little bit more about what you're trying to achieve.

How much RAM are we talking about? Do you need all these GB of textures all at once? Is each texture small or big? etc.
Title: Re: Should I use sf::Texture or sf::Image
Post by: FloweyTF on June 24, 2020, 05:46:14 am
I load all textures at once (size is 16*16), and I have about 1000 of the textures
Title: Re: Should I use sf::Texture or sf::Image
Post by: Laurent on June 24, 2020, 08:26:29 am
It's just 1 MB of data... Nothing to worry about. You could even combine them all into a single texture, tu speed up drawing.
Title: Re: Should I use sf::Texture or sf::Image
Post by: FloweyTF on June 24, 2020, 04:10:50 pm
Yeah, your right, it shouldn’t be that much of an issue