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

Author Topic: Quick sf::RenderTexture question  (Read 4718 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Quick sf::RenderTexture question
« on: January 04, 2019, 07:58:50 pm »
I'm confused about something: If I'm drawing everything into sf::RenderTexture first, do I still need to batch for performance (like minimizing state changes) or does it not matter? I thought it did, but now some source is telling me that it doesn't matter because it's pre-rendering.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Quick sf::RenderTexture question
« Reply #1 on: January 04, 2019, 10:23:48 pm »
A render texture is conceptually nothing else than an off-screen "window" you render to.
Rendering to the render texture should still be done carefully if performance is important. However once it's on the RT, you don't need to think about the render objects anymore, you already have them rendered into a texture.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Quick sf::RenderTexture question
« Reply #2 on: January 04, 2019, 11:22:38 pm »
If you render into it every frame or often then you should care about performance (within reason) but if you do it just once at start, level load, whatever and never again then just using its sf::Texture is cheap.
Back to C++ gamedev with SFML in May 2023

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Quick sf::RenderTexture question
« Reply #3 on: January 05, 2019, 07:32:22 am »
I'm rendering into it every frame with 2 different textures and text with differing sizes/colors so I batch it as if I was batching for opengl state changes like so:

draw texture 1 for each open panel
draw text font 1 size 1 color 1 for each open panel
draw text font 1 size 2 color 1 for each open panel
draw text font 1 size 2 color 2  (so on and on for all differently colored/sized text)
draw texture 2 for each open panel (this has to be on top of text)

It would be so convenient to have each panel draw its own texture1, texts, and texture2; but if the cost of texture switching is the same as actual rendering, then I don't even have to bother testing this way because it's just too many texture changes it would definitely kill performance.
« Last Edit: January 05, 2019, 07:34:27 am by NGM88 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Quick sf::RenderTexture question
« Reply #4 on: January 05, 2019, 10:39:23 am »
but if the cost of texture switching is the same as actual rendering, then I don't even have to bother testing this way because it's just too many texture changes it would definitely kill performance.
How many switches are we talking about here? 2 different textures per frame, switched a few times is nothing.

To imagine that, look at any 3D game with a reasonably complex scene, and try to estimate how many different surface textures you see on the terrain, models, and UI. Even if some of them share an actual hardware texture, you can still do a lot.

In the end, just run a test to see if it's killing performance. It's hard to estimate the impact without knowing the specifics, but I would not optimize prematurely if it makes your application considerably more complex.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Quick sf::RenderTexture question
« Reply #5 on: January 05, 2019, 11:40:09 am »
To imagine that, look at any 3D game with a reasonably complex scene, and try to estimate how many different surface textures you see on the terrain, models, and UI. Even if some of them share an actual hardware texture, you can still do a lot.

In the end, just run a test to see if it's killing performance. It's hard to estimate the impact without knowing the specifics, but I would not optimize prematurely if it makes your application considerably more complex.

As far as I know, a professionally-made game like that would use texture arrays and have the entire scene be drawn in a single draw call, but that's not really something I know much about.

I usually see advice like "don't go over 10 draw calls if you're making a mobile game" online. If I stop batching now, the UI alone will have up to 10 texture changes along with all the font / text state changes.
I feel like I can save a ton time if I prepare my classes to batch / group stuff properly while I'm still coding.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Quick sf::RenderTexture question
« Reply #6 on: January 05, 2019, 12:20:05 pm »
As mentioned: if you think it's an optimization to consider, especially because it comes at a price (code complexity), you should benchmark it and not base your decision on random vague statements like "don't go over 10 draw calls".

You don't need to finish your project until you can benchmark. It's very easy to simulate the same number of draw calls or texture switches with a sample application -- you don't need logic, UI or anything for this. And particularly benchmark it on your target platform, mobile and desktop hardwares vary a lot.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Quick sf::RenderTexture question
« Reply #7 on: January 05, 2019, 12:48:28 pm »
Points well taken. Thank you very much for the replies.

 

anything