Usually, the back buffer is also in video card memory, so that present() is just updating an internal video card pointer.
The default assumption for accelerated 2D graphics is that most frames are built by blitting regions of prerendered images into the framebuffer. You send all the images to the video card once, and then everything is a video-memory to video-memory copy, which is incredibly fast.
In fact, replacing into_canvas().accelerated() with into_canvas().software() might increase performance here: the operations that @fernando is doing arenât the ones acceleration is designed to speed up.
The suggestion to use a [u8] feels the best to me. I have some code that does thisâfirst, youâll want to get a TextureCreator from the windowâs canvas and make a Texture with it (I use ARGB8888 but thatâs just because Iâm copying from a source with that format):
let tex_creator = canvas.texture_creator();
let mut game_tex = tex_creator
.create_texture(
sdl2::pixels::PixelFormatEnum::ARGB8888,
sdl2::render::TextureAccess::Streaming,
w,
h
)
.expect("Couldn't make game render texture");
Later, you can modify your u8 array as you like without any sdl calls, and then call game_tex.update(...) to copy it to the texture, and finally canvas.copy(...) to blit the texture to the canvas before presenting.
I tried that and that pretty much doubled the speed so thank you for the great trick, I wasn't expecting that!
Unfortunately, it is still at least 4 times too slow to render at 60fps
@Finn Thanks a lot, it's much faster but it seems to panic with a "thread 'main' panicked at 'index out of bounds" when I try to make the canvas bigger.
I increased the subsystem window and canvas size but it fails nonetheless.
I will try to figure out why in the morning and come back with some measurements but I can see already that it's much faster.
Ok, so I did the measurements and it's significantly faster providing I create a release.
In debug mode, the frame time is around 60ms for a 1280x800 window.
Once compiled to a release, the frame time varies between 1 and 6ms, so it's great! it's finally above 60fps! I'm not sure the reason for the big variation in frame time, maybe some cache miss, I will need to investigate but first I need to understand your code entirely
Thank you very much to all for helping me with this issue!
PS: Sorry if I couldn't reply earlier but I reached the daily limit as a new user