Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Sprite performance disappointing

This thread is locked; no one can reply to it. rss feed Print
Sprite performance disappointing
roger levy
Member #2,513
July 2002

I'm only able to draw about 3000 sprites on my Intel HD, 2.7ghz machine, running with no throttling, before the framerate dips. This is a lot less than I expected.

I've tried everything, and I am using bitmap holding (and a single spritesheet) and it makes no difference (it does make a huge difference when drawing a 20x14 tilemap strangely ... 0.5ms vs 1.5ms)

I've also made sure it's not my code that is slowing it down - removing the call to al_draw_tinted_scaled_rotated_bitmap_region speeds things up by about 40%. Also tried calling al_draw_bitmap_region instead, and that didn't make a difference.

Any ideas?

EDIT: I just realized I am sorting my sprites by their Y position, and I lied, I am using 2 spritesheets, so that would account for the lack of speedup from holding. Still, I was expecting more, like around 5-6000?

André Silva
Member #11,991
May 2010
avatar

With no throttling, any extra bitmap should, in theory, make the framerate dip.

Still, if you're unhappy with the performance, there are a few things that come to mind.

The size of each bitmap should play a large role. Are we talking about 8x8 sprites, or 256x256?

In addition, it's possible that your program is rendering by software, though I doubt it. After everything is loaded, add a line in your code that calls al_get_bitmap_flags, and check the state of the ALLEGRO_VIDEO_BITMAP and ALLEGRO_MEMORY_BITMAP. If it turns out your bitmap is a memory bitmap, that could easily explain the slow rendering.

roger levy
Member #2,513
July 2002

16x16. Yes to video bitmaps.

I added a quick-and-dirty visibility culling.

Forth:

    : sprite  ( - )
        x 2@ spritewh aabb cam 's x 2@ viewwh aabb overlap? if sprite ;then ;

Now we're down to 5ms to render about 1700 objects (500 of them are drawing themselves twice to create a shadow-casting effect)

(So actually it was less than 3000 before and more like 2000.)

300-500us of that is the tilemap background, and the rest is I guess engine overhead. If I didn't depth-sort everything it might save a little, but it's a radix sort so it's extremely fast.

GullRaDriel
Member #3,861
September 2003
avatar

Can we have a testable example ? Have you checked the allegro5 examples ?

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

SiegeLord
Member #7,827
October 2006
avatar

Yeah, unfortunately this is a known issue. Back in the day I made a mini-library for super-fast bitmap drawing in Allegro, I just updated it a tiny bit too: https://github.com/SiegeLord/FastDraw It's based on the primitives addon + a few simplifications and it gets ~2x the speed of Allegro on my computer. I'm planning on trying to incorporate what it's doing into core Allegro one day.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Ariesnl
Member #2,902
November 2002
avatar

Why on earth do you need to draw 3000+ sprites in one frame ?
holy smoke :o

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Audric
Member #907
January 2001

It takes 8100 tiles of 16x16 pixels to fill a 1980x1080 screen.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

But redrawing all 8100 tiles if they don't change is silly. ;)

You can use buffering techniques, dirty rectangles, shaders, there are lots of options.

If your layers don't change often, you can also upload a VBO (Vertex Buffer Object) to the gpu, so you don't have to waste bandwidth uploading the same vertices over and over again.

Ariesnl
Member #2,902
November 2002
avatar

16 * 16 tiles in a a 1980x1080 screen.. why

I already use 64 * 64 tiles in a 800 * 600 screen

Perhaps one day we will find that the human factor is more complicated than space and time (Jean luc Picard)
Current project: [Star Trek Project ] Join if you want ;-)

Chris Katko
Member #1,881
January 2002
avatar

Ariesnl said:

Why on earth do you need to draw 3000+ sprites in one frame ?

I hate this argument. Why? Why not.

Ask Factorio.

{"name":"7e3tiqtd3fk11.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/7\/6738552d07106bd70b2874e322801c9a.jpg","w":1920,"h":1080,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/7\/6738552d07106bd70b2874e322801c9a"}7e3tiqtd3fk11.jpg

That's 1080p.

I play in 4K

ala

video

And even with screen real estate, don't forget the fact that it's not just 2d pixel area. You can (and often do) have multiple layers, and multiple rendering passes (shadows, lighting, fog-of-war, and more).

No part of an API should be orders-of-a-magnitude slower than ideal performance without the API. Maybe that's why Factorio ripped out Allegro from their code last year. There's a huge difference between say, Vulkan, requiring you to model your codebase around hardware (as opposed to a simpler abstract API model), and Allegro flatout being 2X+ slower than the exact same operation without Allegro.

But redrawing all 8100 tiles if they don't change is silly.

And what if they DO change every frame, as in the aforementioned lighting example?

[edit]

Lastly, Factorio has made MILLIONS upon MILLIONS of dollars. So I've already shown a real-world example of 1) this exact use case, and 2) Allegro possibly being a problem for it. This is not a hypothetical scenario. This is a massively sold, real product that exhibits this exact use-case, not a what-if.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Saying allegro shouldn't bother to try because it can be done better is silly.

If you need high performance, you use atlases and shaders. That's not allegro's fault. In fact, allegro provides shader support so you can do this very thing!

Niunio
Member #1,975
March 2002
avatar

As Edgar said, if Factorio is 2x slower using Allegro then they were doing something wrong. Just using atlases with a correct rendering pipe/algorithm/loop should be as fast as doing it calling OpenGL/DirectX directly (or a little slower, but not a half!).

-----------------
Current projects: Allegro.pas | MinGRo

amarillion
Member #940
January 2001
avatar

It is possible to create a custom shader, using texture indexes, and use multiple bitmaps in a single GL draw call. Phasar uses this approach and it's blazingly fast.

And I totally agree 3000 sprites per frame should be peanuts.

But before you start meddling with shaders, first check that this is really the problem. In my experience allegro standard shader should have no problem with 3000 bitmaps per frame. If you use bitmap holding AND a single sprite sheet, do you see any speed up? If yes, then we can try the multi bitmap shader technique. If no, then your problem lies elsewhere.

piccolo
Member #3,163
January 2003
avatar

check where the this slow down is you can use some beter sorting logic so you don't sort if there is no change. after a complete sort. do smaller sorts on objects that have changed

used the same logic used in database backups full/intermediate

edit: you can also use object tricks like object blocks

when you have many of the same object together change them into a single object this works great for this type of game. 4 factories in placed next to each other becomes one factor object one sprite. the game knows its 4 but the sort will know it as one. if object takes damage than you can revert it to 4 if you need to show that one of the 4 took more damage than the others

wow
-------------------------------
i am who you are not am i

Go to: