flash performance tuning (en)

49
© 2012 Adobe Systems Incorporated. All Rights Reserved. 60fps or Bust! Flash Game Performance Tuning from A to Z Andy Hall Adobe Japan

Upload: andy-hall

Post on 08-May-2015

49.544 views

Category:

Technology


1 download

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

Page 1: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

60fps or Bust! Flash Game Performance Tuning from A to Z

Andy Hall Adobe Japan

Page 2: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Andy Hall Game Evangelist

Adobe Japan

アンディ ホール ゲームエバンジェリスト アドビ システムズ 株式会社 @fenomas

Page 3: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Agenda

• Overview • Tuning ActionScript 3.0 • Flash Player Internals • Rendering Optimization • Upcoming topics

Page 4: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Principles of Optimization

“Premature optimization is the

root of all evil” Donald Knuth

(早すぎる最適化は諸悪の根源)

Page 5: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Optimization Flow

Architect for performance

Build for speed and correctness

Optimize! (only the slow parts)

Page 6: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Metrics

To optimize you need measurable metrics!

• FPS • Memory usage • CPU usage • Bandwidth? • Battery usage? • etc...

Page 7: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Profiling

Now: Flash Builder 4.6 Profiler

Soon: Codename “Monocle”

Page 8: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

TUNING ACTIONSCRIPT

Page 9: Flash performance tuning (EN)

© 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”

Page 10: Flash performance tuning (EN)

© 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

Page 11: Flash performance tuning (EN)

© 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

Page 12: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Function Calls

Function calls can be expensive too. Keep a shallow call stack and avoid recursion:

Page 13: Flash performance tuning (EN)

© 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..)

Page 14: Flash performance tuning (EN)

© 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

Page 15: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

FLASH INTERNALS

Page 16: Flash performance tuning (EN)

© 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

Page 17: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Flash’s Internal Loop

Handle various events

Next SWF frame Execute scripts

Rendering

sleep

Page 18: Flash performance tuning (EN)

© 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

Page 19: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

The Display List

Display List

Bitmap Video

Vector shapes

Page 20: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Dirty Rectangles

Display List

(frame update)

“Dirty” (redrawn)

Page 21: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Display Planes

Display List

3D Video

Vector

Page 22: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Drawing Modes

wmode (Flash), renderMode (AIR)

direct

transparent opaque

Browser

CPU

GPU

Flash Renderer

gpu

cpu

Page 23: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

RENDERING OPTIMIZATION

Page 24: Flash performance tuning (EN)

© 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

Page 25: Flash performance tuning (EN)

© 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!)

Page 26: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Rendering Basics

More basics: • Keep extraneous stuff off the stage • Use the right framerate • Simplify vector shapes

Page 27: Flash performance tuning (EN)

© 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.

Page 28: Flash performance tuning (EN)

© 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!)

Page 29: Flash performance tuning (EN)

© 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!

Page 30: Flash performance tuning (EN)

© 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!)

Page 31: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Bitmap Caching

Use Monocle to see what’s being cached

Page 32: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Rendering

One more technique: Adjust visual effects at runtime!

500 particles 200 particles

Page 33: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

RENDERING MODELS

Page 34: Flash performance tuning (EN)

© 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

Page 35: Flash performance tuning (EN)

© 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.

Page 36: Flash performance tuning (EN)

© 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

Page 37: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

GPU Mode Example Monster Planet SMASH!

(GREE)

More info:

Page 38: Flash performance tuning (EN)

© 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!

Page 39: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

4. Stage3D

Page 40: Flash performance tuning (EN)

© 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!)

Page 41: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

4. Stage3D

• Two officially supported libraries:

• Starling (2D)

• Away3D

• And many, many more...

N2D2 Genome2D

(Take this!)

Page 42: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Stage3D examples

Page 43: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

NEW / FUTURE TOPICS

Page 44: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

AS3 Workers

create

MessageChannel

API

Shared memory

Main Worker

(regular

Flash player)

Background Worker

(some

limitations)

mutex

Page 45: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

AS3 Workers

create

MessageChannel

API

Shared memory

Main Worker

(regular

Flash player)

Background Worker

(some

limitations)

mutex

Page 46: Flash performance tuning (EN)

© 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

Page 47: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

Longer term

Project Monocle!

Better Flash Pro!

Better ActionScript!

Page 48: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.

[email protected] @fenomas

Thanks!

Page 49: Flash performance tuning (EN)

© 2012 Adobe Systems Incorporated. All Rights Reserved.