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

Author Topic: fastest way to draw pixels in screen.  (Read 5459 times)

0 Members and 2 Guests are viewing this topic.

weremsoft

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
fastest way to draw pixels in screen.
« on: December 13, 2018, 12:39:15 pm »
I'd like to implement several celular automatas algorithms and play with pixels arround. And I wonder what's the most efficient way(the one that will give me the highest FPS count) to draw an image in the screen.

I have two approachs:

1. Use an image, to update a texture of a sprite on the screen.
2. Use an array of RectangleShape's to draw squares on the screen.

My guess is that the first one is better, since the individual pixels are not "transformables", but I don't know for sure.

Just to clarify myself. I'm looking to achieve something like this at a high framerrate without using shaders.

https://www.ssaurel.com/fireeffect/fireeffect.htm

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: fastest way to draw pixels in screen.
« Reply #1 on: December 13, 2018, 12:47:00 pm »
The best approach would be to write your automata as a shader, so you're operating directly on the GPU and have direct access to VRAM, but of course you'll be "constraint" by how shaders operate.

Alternatively, I'd recommend to use a texture and call update() with a std::vector<sf::Uint8> which holds all the pixel data (can also be retrieved from an sf::Image with getPixelsPtr()).

As for number 2. If you do want to draw primitives, you basically have to use a vertex array (or depending on the updating also vertex buffer), so your just have one draw call instead of thousands.

It's hard to say whether using update on a texture performs better than drawing tons of primitives, it will also heavily depend on the number of "pixels" you're changing etc.
Best would be to simply do a more or less realistic benchmark of each version and see what works better.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

weremsoft

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: fastest way to draw pixels in screen.
« Reply #2 on: December 13, 2018, 01:34:13 pm »
Thanks. I'd like to avoid render shader since it's a totally different beast. BUT!, I saw this guy that uses "Quads".

https://github.com/Hopson97/CellularAutomaton/blob/master/Source/CellularAutomaton.cpp

He says that is very performant but It's hard to me to understand that code.

What do you think. Is there any advantage of using quads that way?

Maybe "pixels" is not what I need.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: fastest way to draw pixels in screen.
« Reply #3 on: December 13, 2018, 01:52:14 pm »
It depends on the size of your grid. If grid cells are 1x1 pixel, then you'll end up with millions of quads to draw which is obviously not optimal. If grid elements are 10x10 pixels, then this solution starts to be interesting, as you'll send around ~100000 vertices to your GPU instead of millions of pixels, every frame.

So it all depends on the resolution of your grid. And of course of your screen resolution, and your hardware, etc. ;)
Laurent Gomila - SFML developer

 

anything