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

Author Topic: Drawing text vs making a render texture once and then draw  (Read 4508 times)

0 Members and 1 Guest are viewing this topic.

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Drawing text vs making a render texture once and then draw
« on: November 19, 2013, 11:42:16 pm »
Hello.
Hint:I simplified the question as best i know, therefore the request may seem a little OOD.

I am wondering the following since i have no idea how cpu consuming is to draw text?
Question 1:If i have wall of text is it more cpu efficient to draw to render texture(that will continue to live after current loop) and then draw the render texture each loop(to screen) or draw the text itself(to the screen).
Question 2:If i have text that is gonna change each 5 seconds that will hold about 20 characters, draw it onto render texture once and then use it until the text is changed upon witch time i will redraw the renderTexture or draw the text itself to the screen?
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Drawing text vs making a render texture once and then draw
« Reply #1 on: November 20, 2013, 02:10:39 am »
There are a few pitfalls with that approach:
I found when I made a RenderTexture to draw a text on it the context was deactivated and I needed to call setActive with the window or loose a frame of drawing.
I circumvented it with creating a thread every time, but that is also bad because the contexts of threads that ended will never be destroyed before the program ends (for making it safer to use for users). So I would have to create a single longrunning thread with synchronization and io-queues.
It may cost to create the RenderTexture and activate/deactivate of contexts is expensive and it would also contain more state than a regular Texture.
I decided to get the texture, create a fresh texture with the copy constructor, then destroy the RenderTexture, as I will keep a large number in a cache for a longer time than you. But that will fetch an Image from GPU to main memory and then recreate a Texture instead of a direct GPU memory copy.
I'm shortly before ripping this out and using sf::Text directly (maybe with a bit of Shape::scale) to improve quality, as you only save one size when prerendering a texture (and there is no mipmapping so you have to use a medium size not large and hope activating smoothing helps a bit) and if you resize it later it looks a bit worse, though you may not plan to resize it.
Maybe I later profile it and put in the contrived longrunning thread version with a single large RenderTexture and a single Text to avoid that memory problem, and maybe direct GPU-mem texture copy, but thats most likely not worth it as its fast enough without doing all that complicated stuff.
More likely I put in the direct use of Glyphs/Font as I think they have a single texture with letters inside already. I can prepare my own array of Vertex that reference that texture then and save that instead.
« Last Edit: November 20, 2013, 02:20:33 am by wintertime »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Drawing text vs making a render texture once and then draw
« Reply #2 on: November 20, 2013, 07:54:13 am »
Are we talking about one text, or thousands? Do you have performances problem that need to be solved?
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11049
    • View Profile
    • development blog
    • Email
AW: Drawing text vs making a render texture once and then draw
« Reply #3 on: November 20, 2013, 08:04:06 am »
As Laurent hinted and as with all of these "what performs better"-topics, go with the most simplest approach. If and only if you run into actual performance issues, you can start optimizing.

Plus with such topics we can either just argue logically or from experience. But if one is really interested in an actual answer, there's no way around writing a test. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Drawing text vs making a render texture once and then draw
« Reply #4 on: November 20, 2013, 11:39:38 am »
i did some tests on that, and didn't really see any change at all.
it only made a little difference when i was using the "outline" method (from one of my topics), because in that case, the difference was between drawing a single sprite and about 80+ texts each frame.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Drawing text vs making a render texture once and then draw
« Reply #5 on: November 20, 2013, 01:26:36 pm »
So i see there is no going easy.

Text drawing vs Render Texture drawing
Quote
there's no way around writing a test.
Done testing, drawing 150 characters 1000 times vs drawing a renderTexture 1000 times(that holds the 150char txt drawn onto it)
Results: the renderTexture is faster by at least double speed, but its requirement makes it unusable in many cases unless the text is really a wall of text witch in my case is true and is pretty good speed improvement that i required.

To explain what is happening, i have small font size text that explain events and is somewhat like list of stuff that has recently happened, instead of looking last few stuff that happened picking what to draw etc.
I just draw stuff to Render Texture and use that for drawing i only redraw it when it is required and it is better cpu wise witch i was trying to achieve since my game is hauling allot for its BADLY made AI.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Drawing text vs making a render texture once and then draw
« Reply #6 on: November 23, 2013, 04:57:35 pm »
i think that it's a good approach for texts that will not change very often.
i use this idea to make menus, for example. i draw the background; then the cursor; and then i put all the menu texts in a single renderTexture, and draw it.
Visit my game site (and hopefully help funding it? )
Website | IndieDB

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Drawing text vs making a render texture once and then draw
« Reply #7 on: November 23, 2013, 08:26:29 pm »
I did the switch back to sf::Text, because I found out I was wasting too much memory in keeping a cache of single lines of text. That was only after I tried switches from 64 pixel height to doubling, quadrupling or more and noticed weird corruption inside the textures and outright errors creating more Textures.
I would still consider keeping a single fullscreen RenderTexture for when there is much static drawing like half the screen filled with text and menus, but much more than that not.
Before that I even implemented mipmaps into sf::Texture for testing if they would help, but there is no way around that resizing always reduces quality. If I get in the mood to verify there is no weird interactions with RenderTexture that I suspect and add the needed feature tests with GLEW and possibly add another way of generating the mipmaps I may even share it later (test code to verify it works well might also be needed).

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
Re: Drawing text vs making a render texture once and then draw
« Reply #8 on: November 25, 2013, 01:00:37 pm »
Thanks on help id close the disscusion  ;D.
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0