flash performance tuning (en)
DESCRIPTION
English version of CEDEC Flash performance tuning presentation Note: what these slides refer to as "Project Monocle" has now been released, and is called Adobe Scout ( http://gaming.adobe.com/technologies/scout/ )TRANSCRIPT
© 2012 Adobe Systems Incorporated. All Rights Reserved.
60fps or Bust! Flash Game Performance Tuning from A to Z
Andy Hall Adobe Japan
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Andy Hall Game Evangelist
Adobe Japan
アンディ ホール ゲームエバンジェリスト アドビ システムズ 株式会社 @fenomas
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Agenda
• Overview • Tuning ActionScript 3.0 • Flash Player Internals • Rendering Optimization • Upcoming topics
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Principles of Optimization
“Premature optimization is the
root of all evil” Donald Knuth
(早すぎる最適化は諸悪の根源)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Optimization Flow
Architect for performance
Build for speed and correctness
Optimize! (only the slow parts)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Metrics
To optimize you need measurable metrics!
• FPS • Memory usage • CPU usage • Bandwidth? • Battery usage? • etc...
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Profiling
Now: Flash Builder 4.6 Profiler
Soon: Codename “Monocle”
© 2012 Adobe Systems Incorporated. All Rights Reserved.
TUNING ACTIONSCRIPT
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Tuning ActionScript 3.0
Caveats: • Often not as important
as rendering
• Beware of low-impact “tips and tricks”
© 2012 Adobe Systems Incorporated. All Rights Reserved.
The Basics
• Always use AS3.0 • Type everything • Prefer Vector.<type> over
Array or Dictionary • Prefer String methods over RegExp • Be careful with E4X and XML
( ) • Callbacks are faster than Event
Always!
Only in critical areas
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Pooling
Instantiation can be expensive! Pool or reuse objects to avoid the cost of frequent creation and collection
• Static temps
• Object pooling
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Function Calls
Function calls can be expensive too. Keep a shallow call stack and avoid recursion:
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Garbage Collections
Know your GC!
• Flash’s GC does both reference counting and mark-sweep. (If you want to tune memory usage, you need to understand these!)
• Use Monocle to find if GC is firing too often • Pooling/reuse makes GC collection less frequent • For large collections, prefer literals (String, int..) or plain
objects over complex objects (Sprite, Rectangle..)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Smart GC
Tell Flash when to trigger a GC!
Call this between levels, when the game pauses, etc. Flash’s GC marks incrementally, and this command tells it to finish marking and do a collection if a GC was already imminent. The argument specifies how long you’re willing to wait for a GC: imminence=0.99; // GC if ready now imminence=0.01; // GC even if you need to pause a while
© 2012 Adobe Systems Incorporated. All Rights Reserved.
FLASH INTERNALS
© 2012 Adobe Systems Incorporated. All Rights Reserved.
The big picture // Flash’s internal loop (simplified) while() { sleep until (nextEvent OR externalEvent) if ( various events pending ) { handleEvents(); // handles Timer events, // fills audio/video buffers } if ( time for next SWF frame ) { parseSWFFrame(); executeActionScript(); } if ( screen needs update ) { updateScreen(); } }
Gory details: http://blog.kaourantin.net/?p=82
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Flash’s Internal Loop
Handle various events
Next SWF frame Execute scripts
Rendering
sleep
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Point: Because of this, most recurring scripts should be handled
in Event.ENTER_FRAME !
Handle external events
Next SWF frame Execute scripts
Rendering
sleep
ENTER_FRAME
© 2012 Adobe Systems Incorporated. All Rights Reserved.
The Display List
Display List
Bitmap Video
Vector shapes
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Dirty Rectangles
Display List
(frame update)
“Dirty” (redrawn)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Display Planes
Display List
3D Video
Vector
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Drawing Modes
wmode (Flash), renderMode (AIR)
direct
transparent opaque
Browser
CPU
GPU
Flash Renderer
gpu
cpu
© 2012 Adobe Systems Incorporated. All Rights Reserved.
RENDERING OPTIMIZATION
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Rendering Basics
The basics: • Keep a shallow display list.
Reason: everything in that rectangle is getting redrawn every frame!
Display List
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Rendering Basics
More basics: • Understand that certain features are just heavy!
• Bitmap Effects (shadow, glow, emboss...) • Masks (inherently vector-based) • Alpha channels • Blend modes (add, multiply...) and even...
• Embedded fonts (especially Japanese!)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Rendering Basics
More basics: • Keep extraneous stuff off the stage • Use the right framerate • Simplify vector shapes
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Stage Settings • StageQuality.LOW:
Lower-quality vector shapes. Never anti-alias, never smooth bitmaps.
• StageQuality.MEDIUM: Better vectors. Some anti-aliasing but no bitmaps smoothing. Default value for mobile devices.
• StageQuality.HIGH: Always uses anti-aliasing. Uses smoothing on bitmaps depending on whether the SWF is animating. Default value on desktop PCs.
• StageQuality.BEST: Best quality. Always anti-alias, always smooth bitmaps.
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Rendering Internals
Understand rasterization and the conditions for cached bitmaps to get redrawn! (Designers need to know this too!)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Bitmap Caching
• Lets complex assets be rasterized once instead of every frame.
• Turned on automatically if you use bitmap effects
Caching is the single most important thing to understand for tuning display list content!
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Bitmap Caching foo.cacheAsBitmap = true;
foo.cacheAsBitmapMatrix = new Matrix();
(cached) (redrawn)
(cached) (drawn with GPU) (only in AIR!)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Bitmap Caching
Use Monocle to see what’s being cached
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Rendering
One more technique: Adjust visual effects at runtime!
500 particles 200 particles
© 2012 Adobe Systems Incorporated. All Rights Reserved.
RENDERING MODELS
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Rendering Models
There are four main rendering models in use today for Flash games.
1. Display List 2. GPU mode 3. Blitting 4. Stage3D
© 2012 Adobe Systems Incorporated. All Rights Reserved.
1. Display List Rendering
• Plain vanilla flash content, made in Flash Pro
• Easiest to build – worst performance.
• Suitable for prototypes and light desktop content.
Bad for mobile! (because the GPU is not used)
• Your key tuning technique will be bitmap caching.
© 2012 Adobe Systems Incorporated. All Rights Reserved.
2. GPU Mode
• GPU mode is a publish setting available for AIR apps on
devices (Android and iOS)
• When selected, Flash uses the GPU – vectors and cached
surfaces are rendered through hardware.
• Can be very powerful with careful, correct use of
movieclip.cacheAsBitmapMatrix
• Powerful technique today, but not recommended for
new/future projects
© 2012 Adobe Systems Incorporated. All Rights Reserved.
GPU Mode Example Monster Planet SMASH!
(GREE)
More info:
© 2012 Adobe Systems Incorporated. All Rights Reserved.
3. Blitting
• Blitting refers to putting a Bitmap object on the stage for
your game area and manually drawing contents into it
with BitmapData.copyPixels().
• Hence, you manage your own rendering, caching, etc.
• Can be very fast in certain restricted cases
• Does not scale up for retina/hi-res devices!
© 2012 Adobe Systems Incorporated. All Rights Reserved.
4. Stage3D
© 2012 Adobe Systems Incorporated. All Rights Reserved.
4. Stage3D
• New feature from Flash 11.0, allowing ActionScript to
load shader programs directly to the GPU.
• Based on AGAL (Adobe Graphics Assembly Language)
• AGAL looks like this:
• You probably want
to use a 2D/3D library!
m44 op, va0, vc0 dp4 op.x, va0, vc0 dp4 op.y, va0, vc1 dp4 op.z, va0, vc2 dp4 op.w, va0, vc3 m44 op, va0, vc0 mov v0, va1
(It’s dangerous to go alone!)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
4. Stage3D
• Two officially supported libraries:
• Starling (2D)
• Away3D
• And many, many more...
N2D2 Genome2D
(Take this!)
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Stage3D examples
© 2012 Adobe Systems Incorporated. All Rights Reserved.
NEW / FUTURE TOPICS
© 2012 Adobe Systems Incorporated. All Rights Reserved.
AS3 Workers
create
MessageChannel
API
Shared memory
Main Worker
(regular
Flash player)
Background Worker
(some
limitations)
mutex
© 2012 Adobe Systems Incorporated. All Rights Reserved.
AS3 Workers
create
MessageChannel
API
Shared memory
Main Worker
(regular
Flash player)
Background Worker
(some
limitations)
mutex
© 2012 Adobe Systems Incorporated. All Rights Reserved.
ASC2.0
• All new ActionScript compiler! • Included with the Flash Builder 4.7 preview:
http://labs.adobe.com/technologies/flashbuilder4-7/
• Key features: • Faster compiles! More optimized bytecode! • Error messages localized to JA, FR, ZH • Inline functions, get/setters (when possible) (use –inline arg)
• goto statement (!?!?!?)
• Details: http://www.bytearray.org/?p=4789
© 2012 Adobe Systems Incorporated. All Rights Reserved.
Longer term
Project Monocle!
Better Flash Pro!
Better ActionScript!
© 2012 Adobe Systems Incorporated. All Rights Reserved.