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

Author Topic: Efficient way to draw and get pixels  (Read 2112 times)

0 Members and 1 Guest are viewing this topic.

karagal

  • Newbie
  • *
  • Posts: 3
    • View Profile
Efficient way to draw and get pixels
« on: January 28, 2022, 11:32:43 am »
Hello,

I have been using SFML to render a driving simulator in C++, which is interfaced to Python using pybind11 for a deep learning project. At each frame I am drawing to a render texture, updating it, copying it to an image, getting the unsigned char pixel array (80*80*4), and copying it into a vector which I then return (I'm copying it to a vector so that the pixels can be moved to Python without copy). Right now this happens about 1000 times per second and is quite slow.

I'm coming here to see if anyone sees a more efficient way to do this, or if SFML is just not the right library for this task. Is there a way to avoid that costly GPU-to-CPU operation? (I just want the pixels, not to render to the screen, but I'm not sure it changes anything).

Thanks! :)

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Efficient way to draw and get pixels
« Reply #1 on: January 28, 2022, 12:32:26 pm »
moving GPU to CPU is always slow, unfortunately.
but in this case you could try an OpenGL function, like glReadPixels.

unsigned char data[4];
glReadPixels(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &data);
 
« Last Edit: January 28, 2022, 12:34:24 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

karagal

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Efficient way to draw and get pixels
« Reply #2 on: January 30, 2022, 01:48:20 pm »
Thanks!

I've managed to avoid copying the pixel arrays several time with that. However the time I'm saving is tiny compared to the cost of moving from GPU to CPU.

Thing is in the deep learning process, those pixels are moved back to GPU for faster computations. Guess I have to figure out a way to make it stay on the GPU, but I have no idea how for now. Not very familiar with GPU computations, plus I'd have to pass GPU data from C++ SFML to Python.

Well, if someone here ever did something somewhat similar, please let me know!

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Efficient way to draw and get pixels
« Reply #3 on: February 02, 2022, 02:47:04 pm »
well, I had a great conversation with Paul in a topic I made a while ago. We used this GL approach to identify colors in the screen, and get some units ID.
I don't know anythin much faster than that... but what are you trying to do, exactly?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

 

anything