Game Development Community

Getting a primer in iTGB optimizations

by BeyondtheTech · in iTorque 2D · 06/11/2009 (8:32 am) · 16 replies

In case you haven't read my blog entry, I only recently came back into iTGB after nearly a seven-month hiatus.

My game is coming along nicely, but I've ran into a few snags, particularly with low memory crashes and fluctuating frame rates. However, my game has always been tested on my iPhone 3G with no optimizations whatsoever, either from the Build Project options, or at compilation time in Xcode. The reason is, I don't know what these optimizations mean, and the ones I've tried either spit back an error message in Xcode, or the results are rather quirky (nothing seems to move).

The information here on the forums and in the documentation are spread out, sparse, and sporadic at best, so I'm hoping everyone who really knows them can chime in here and explain what some of these optimizations do and how exactly, in step-by-step, to apply them to your current project. I'm sure I'm not the only one who could benefit from this, and it may answer or clarify some misconceptions or confusion that others who may or may not know fully how they work.


For instance, in one of my threads, someone wrote:
Quote:These are the flags I am using right now:

__IPHONE__ TORQUE_PLAYER TORQUE_RELEASE TORQUE_DISABLE_MEMORY_MANAGER PUAP_SCRIPT_CHANGE PUAP_OPTIMIZE USE_COMPONENTS

Also note that unless you fixed it already there is a typo in the TORQUE_DISABLE_MEMORY_MANAGER, you need to add the 'R' on the end.

  • ...where do you put these, and what do they do? If iTGB is doing live memory management as the label suggests, and it's resulting in crashing my game on the iPhone, will this resolve it?

  • There are four levels of optimization in the Build Project tab: None (which I use), and Simulation, Scripts, and Full. When would I need each one, why, and wouldn't anyone always want "Full" to begin with?

  • What are the different targets in Xcode for?

  • I've read that DSOs need to be rebuilt for certain scenarios - which ones exactly, and is it just as simple as searching for all DSO files and deleting them, and iTGB will recreate them on the fly?

  • I've read that setPhysics(true); is needed when objects don't seem to move in the optimized scripts. Can this be inserted in the .t2d scene files, or do you have to explicity apply it for every object you have? Do you have to change any of the object's properties in the editor's interface, specifically, the Physics section (i.e. Calculate Mass and Inertia)?

  • Do PVR graphic files really work with the current version of iTGB? Does it simply make the file smaller, or does using it improve game performance over PNG files? Are there any precautions to using them?
Thanks to everyone in advance for their answers.

#1
06/11/2009 (11:35 am)
About the different targets: Next release will drop down to two. The different flags mentioned have different combinations of those flags. Use the search box in the build menu, and you'll find them (preprocessor flags, or something like that).

PVRTC is a form of compression with linear compression ratios - in other words, you can exactly predict the size. It also has the benefit that the compressed image is used directly from graphics memory, decompressed on the fly for display. The size of the file is how much VRAM it uses, and the smaller a file is, the faster it loads. In fairly raw code (not iTGB) using PVRs, I've noticed a huge drop in memory usage when going from PNG to PVR. From 7MB to 4.5MB with one test of just a few PNGs converted to PVR (background image, a few tiles).

The default compression is 8:1, while you can do 2-bit and get 16:1. The only drawback is that it seriously screws with colours. Think JPEG, but in some cases worse. Certain psychedelic patterns look unaffected (but hurt your eyes ;), so if you're going for the Jeff Minter aesthetic, you can live with all PVRTC graphics.

4-bit PVR means either 4444 RGBA (or GBRA or some such rearrangement) or RGB 565 (without alpha). Not sure what 2-bit does, but mud and grass might be the only textures to survive the conversion.

I haven't tested PVRs with iTGB 1.2 yet, actually, but I think there is a problem where the build process copies over the PNGs as well, making it pointless.

What I'll be doing is to create a PVR tester in Cocos2D which I can drop some PVRs into, and it lets me scroll through the files in an image directory, viewing each on my device. Cocos loads real fast, and can do very basic stuff easily, so it's better than rebuilding iTGB (sorry, it needs some more optimisations ;).
#2
06/12/2009 (6:22 am)
Yes, in 1.2 the build process copies over PNGs as well - your game will still use PVRs as long as you don't specify an extension in the datablock. PVRs work, just not for celled sprite sheets (animations). The only downside to using PVRs is that image quality can suffer.
#3
06/12/2009 (7:45 am)
Good to know. Does that mean I can delete the PNG files once it's compiled over to Xcode for final building?

I'm still confused about using any optimizations. Are they all related, does one type require a set of flags, which target in Xcode should I be using?

I couldn't find the location to put the flags in there. I see Preprocessor Macros and I also see Other C Flags, but not Preprocessor Flags. I basically copy and pasted that quoted line above into Preprocessor Macros and compiled in Xcode, but found no discernible difference in performance of my game.

Let me start from scratch...

  • When I build my project in iTGB, should I be choosing any of the particular optimizations, such as Scripts, or Full?

  • When it's finally compiled for Xcode, is this where I should enter the flags, and where exactly?

  • What are the four different targets for (iTGB, iTGB_Optimize, iTGB_Script, and iTGB_Script_Optimize), and how does it relate to the flags, and the optimization level that was selected in iTGB?
I don't know if any of this was explicitly discussed, or explained in any documentation, but even if it is, I don't think it is obvious to people who are just starting out in iTGB. If anyone could clear this up, I (and probably many others) would greatly appreciate it.
#4
06/12/2009 (8:20 am)
http://www.garagegames.com/community/forums/viewthread/83173
#5
06/12/2009 (8:47 am)
Thanks Bret, I found the answer in the second page of that thread, as to where you put the flags in - I was right in my first guess: "Preprocessor Macros."

But I didn't see any difference in performance, unless it's tied into the optimization levels in iTGB and choosing the right target in Xcode?
#6
06/12/2009 (10:41 am)
* When I build my project in iTGB, should I be choosing any of the particular optimizations, such as Scripts, or Full?

It depends on what you want, and also what your desktop version has. Scripts makes some changes to the DSO files for optimization, so if your DSOs were not compiled with this but your game has it set, you will get crashes. The link Bret posted explains a bit about what the flag does, but in short, it speeds up variable lookups dramatically. If your scripts are compiled from a PC/Mac version of your game that you're testing on, make sure these flags match up


* What are the four different targets for (iTGB, iTGB_Optimize, iTGB_Script, and iTGB_Script_Optimize), and how does it relate to the flags, and the optimization level that was selected in iTGB?

Optimization level is an XCode thing, the different targets just set different combinations of the flags for convenience.
#7
06/12/2009 (11:06 am)
Are you saying that if I choose iTGB_Script_Optimize, I don't need to set the Preprocessor Macros because it's already built-in?

Sorry if the question is stupid, but why would someone NOT want optimizations?

What about the setPhysics(true); command? Should I just applying that all over my script to objects that require movement, if I'm planning to optimize?
#8
06/12/2009 (11:36 am)
yes just select the target and you get all the optimizations.

Only reason to disable them is if your having some form of issue.

Yes you need to call setUsesPhysics to everything that moves/collides etc.
#9
06/13/2009 (1:09 am)
Just out of interest sake, Marble Blast Mobile uses PVR2 for most of its textures. Only a few (including all that need alpha), use PVR4. We only use PNG for a couple of HUD / Menu images that don't compress well, otherwise all menus are also done in PVR4.
#10
06/14/2009 (9:22 pm)
Well, how about that...

After manually adding in "setUsesPhysics" to all the appropriate objects after loading the level, and using all the flags and optimizations, I AM NOT CRASHING! Knock on wood, this is fantastic. Now, I can go about my business and finish the game, once and for all!
#11
06/26/2009 (7:24 am)
OK, I take it back. I'm crashing.

I'm 99% completed with my game. Just putting in the instructions and credits.

But, I can't get the program to run properly on the device anymore!!!

With various optimizations, it either crashes on startup or hangs at the splash screen. I chose to Build Project with Full optimizations, copied all the optimization flags above into the Preprocessor Macros, disabled Compile for Thumb, selected iTGB_Script_Optimize (tried iTGB_Optimized, too), cleaned, then Build and Go.

Trust me when I say that my program is not complex, does not use any modifications of the source code other than implementing Dave Calabrese's multitouch, and loading of separate datablocks for levels. That's it.

Can some kind soul please give me a step-by-step walkthrough from the beginning of the Build Project option in iTGB to the end of Build and Go in Xcode? Please provide all details, including proper flags, settings, etc. I've already incorporated setUsesPhysics in my TorqueScript.
#12
06/26/2009 (8:19 am)
You need to rebuild the TGBGame and then rerun your game from the editor so that the DSO files are recompiled with those options also.

The iPhone build optimizations have to be in sync with the TGBGame buidl optimizations so that the DSO files are compatible.
#13
06/26/2009 (8:22 am)
Thanks, but can you be a bit more descriptive? What do you mean rebuild TGBGame? Is any of this documented somewhere? I thought you just build the game with the editor, it sends to Xcode, then compile onto the device. Now, I'm really lost.
#14
06/26/2009 (8:40 am)
Open the engine\compilers\Xcode\ project and then rebuild all of the projects there with the same optimization flags you build the iphone game with.

the DSO file format changed when they added the scripting optimizations. I don't have a mac with me at the moment to be more descriptive. Suffice it to say that if you want to build your iPhone game with all the optimizations you need to rebuild the Desktop TGB and TGBGame using the supplied xcode project with those optimizations one time. Then make sure all of your DSO files are recompiled with this new version.
#15
06/26/2009 (9:00 am)
Just ensure not to come up with the idea to compile the editor with those optimizations or it will crash regularily.
#16
10/27/2009 (11:11 pm)
Helpful info here, thanks.