Page 1 of 3

Optimization tips

Posted: Sat Dec 29, 2012 5:57 pm
by OmarShehata
I'm at a point where I need to do some serious optimization.

My biggest issue is with rendering large amounts of objects. I would appreciate any tips or tricks. Like, do you use SpriteBatches? Perhaps a clever use of canvases? I know I'm being super vague, but any general tips would be awesome. Just for example if you want to have as many animated enemies on screen as you can.

Now for a specific question. I'm applying a shader to every object in play. Instead of doing that, if I take a screenshot of the stage, render that image, and apply the shader on it, would I have effectively gone from running my shader on thousands of objects to 1? Sounds a bit too good to be true. Any foreseeable drawbacks?

Re: Optimization tips

Posted: Sat Dec 29, 2012 6:53 pm
by Nixola
Using a screenshot instead of a canvas doesn't seem a good idea to me, taking a screenshot and converting it into an image every frame is quite slow, and it uses lots and lots of RAM, forcing the garbage collector to run way more frequently than normal

Re: Optimization tips

Posted: Sat Dec 29, 2012 6:58 pm
by bartbes
As for the "shader optimization" (which really shouldn't affect you too much unless you've got tons and tons of overlap), this does in fact work this way. I would recommend using a canvas for it instead, though, as Nixola mentioned.

Re: Optimization tips

Posted: Sat Dec 29, 2012 9:31 pm
by Jasoco
Though be careful as a bunch of our users (Not me) have computers that can't use Canvases because their video cards are old or they have outdated drivers or random other reasons. So its best to include a Canvas-less mode for them and detect whether they support them by using the isSupported() function.

Re: Optimization tips

Posted: Sun Dec 30, 2012 12:41 pm
by OmarShehata
Thanks for the replies Nixola and bartbes!
Jasoco wrote:Though be careful as a bunch of our users (Not me) have computers that can't use Canvases because their video cards are old or they have outdated drivers or random other reasons. So its best to include a Canvas-less mode for them and detect whether they support them by using the isSupported() function.
I am planning on doing that, but I'm having doubts on whether it's worth it. There's a lot of stuff that would be dependent on shaders and/or canvases, and without it the game would either look horrible or have really bad performance. We could try to fake some things and cut some corners just so people without the hardware can play.

My first priority is actually getting the game to work correctly on my machines, then I can start to worry about making it work for everyone, even if I have to rewrite some things.

Re: Optimization tips

Posted: Mon Dec 31, 2012 4:38 pm
by holothurian
Omar I had a look at some concerned joe videos and I love his face getting more and more anxious when he's standing still. Very expressive sprite work!

Re: Optimization tips

Posted: Tue Jan 01, 2013 3:39 am
by BlackBulletIV
OmarShehata wrote:I am planning on doing that, but I'm having doubts on whether it's worth it. There's a lot of stuff that would be dependent on shaders and/or canvases, and without it the game would either look horrible or have really bad performance. We could try to fake some things and cut some corners just so people without the hardware can play.

My first priority is actually getting the game to work correctly on my machines, then I can start to worry about making it work for everyone, even if I have to rewrite some things.
Don't let it deter you from using shaders and canvases; almost all games use things like that nowadays. I'd recommend giving people the option to not run it with those features, but only if it's not too much of a hassle.

Re: Optimization tips

Posted: Tue Jan 01, 2013 5:35 am
by Jasoco
Yeah. A lot of games will let you turn settings off to get better performance or for compatibility reasons. It's always nice to have really high settings and something that can still be enjoyed by others.

Re: Optimization tips

Posted: Tue Jan 01, 2013 5:55 am
by BlackBulletIV
That's what I'm doing with my arena shooter, although the game looks terrible without bloom on.

Re: Optimization tips

Posted: Tue Jan 01, 2013 3:30 pm
by OmarShehata
Ok so, as far limitations go, all objects rendered with one SpriteBatch need to share the same:

-Alpha & color
-Depth (as in, something outside the sprite batch can't be between two things inside a sprite batch)
-Shader effect

So I just need to be smart about which objects I group into an atlas.

There doesn't seem to be a way to remove individual items from a Batch, would I have to clear everything and add them all back if I wanted to remove 1?

So so far it seems Sprite Batches are really good for things that won't move or get changed often, that don't need to be updated every frame.

Also, is there any way I can use canvases to speed things up even more? I keep thinking I could draw the backgrounds and static tiles onto a canvas, so that I only draw it once, and only redraw that canvas when I change anything, but then I could do the same thing with a sprite batch and update the sprite batch when I need to.