SFML community forums

Help => Graphics => Topic started by: LakySimi1 on September 21, 2022, 10:22:56 pm

Title: Any way to optimize 'pixelsArray to GPU' process?
Post by: LakySimi1 on September 21, 2022, 10:22:56 pm
Hi everyone,
for my small projects i'm using a  "sf::Uint8* pixelsArray = new sf::Uint8[640 * 480 * 4];" that gives me the possibility to manipulate every pixel as i want but feels kinda slow..

Here's a pseudocode of what i'm currently doing. is there any way to speed things up?
I've the feel that the last 3 rows are slowing everthing because i have to pass my 'pixelsArray' to GPU (right?) every "new updated frame".

Is there a way to modify my 'pixelsArray' somehow directly in the GPU (to avoid the continuous texture update)?

Code: [Select]
sf::RenderWindow window(sf::VideoMode(640, 480));
sf::Texture texture;
texture.create(640, 480);
sf::Sprite sprite(texture);

sf::Uint8* pixelsArray = new sf::Uint8[640 * 480 * 4];

//UPDATING PIXELS ARRAY
pixelsArray [x + y*640 * i] = 0-256;
so on..

//UPDATING  WINDOW
texture.update(pixels);
window.draw(sprite);
window.display();


BONUS QUESTION:
Suppose i have a large pixelsArray [4096x4096] but want to draw (fast) just a rectangular portion of it?
(like as map scrolling as seen in Age of Empires or Commandos [supposed paused for semplicity])

PS I'm sure i got something wrong but i'm not really into technicals of SFML/OGL.. just need something to draw.
Title: Re: Any way to optimize 'pixelsArray to GPU' process?
Post by: fallahn on September 21, 2022, 11:18:12 pm
You can use this overload (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Texture.php#a1352d8e16c2aeb4df586ed65dd2c36b9) of Texture::update() to update only part of a texture. And FWIW it's possible to use a vector for your array so you don't have to worry about memory management with new/delete:

std::vector<sf::Uint8> pixels(640*480*4);

texture.update(pixels.data());
 
Title: Re: Any way to optimize 'pixelsArray to GPU' process?
Post by: LakySimi1 on September 21, 2022, 11:45:46 pm
Probably your overload is faster if only part of the screen/image needs to be updated but, let's say a sidescrolling scenario, like having a larger than screen resolution image, it will not speed anything up because its needed to update all the texture so it's not different from my method, am i right??

Thw fact is that if i use the sfml function to draw a 640x480 pixels rectangle it is fast as light, but if i pass a fixed pixelsArray to texture etc (method in OP) it slow down like 95%!

I just need a fast (the fastest possible) way to display a pixelsArray, pixelsArray that i could modify during execution.

Thanks for the overload btw, surely will experiment with it!