SFML community forums

Help => General => Topic started by: karagal on January 28, 2022, 11:32:43 am

Title: Efficient way to draw and get pixels
Post by: karagal 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! :)
Title: Re: Efficient way to draw and get pixels
Post by: Stauricus 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);
 
Title: Re: Efficient way to draw and get pixels
Post by: karagal 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!
Title: Re: Efficient way to draw and get pixels
Post by: Stauricus 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?