High Performance 3D Games on Windows Phone 7 Series
description
Transcript of High Performance 3D Games on Windows Phone 7 Series
![Page 1: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/1.jpg)
High Performance 3D Games on Windows Phone 7 Series
Shawn HargreavesMicrosoft Corporation
Blog: http://shawnhargreaves.comTwitter: @ShawnHargreave
![Page 2: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/2.jpg)
Windows Phone 7 Series Hardware
Consistent sets of hardware capabilities defined by Microsoft
800
or 4
80
480 or 320
One resolution at launchSecond resolution added laterSame touch inputConsistent processor / GPUSame available RAMOptional keyboard
![Page 3: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/3.jpg)
The CPU
![Page 4: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/4.jpg)
The Evolution of Programming
Low level control
Straight to the metal
Raw performance tuning
High level abstraction
Rely on compiler and runtime
Developer productivity
![Page 5: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/5.jpg)
C++ Interview Question
for (EntityList::iterator it = entities.begin(); it != entities.end(); it++){ ICollidable* col = dynamic_cast<ICollidable*>(*it);
if (col) pendingCollisions.push_back(new CollisionInfo(col));}
![Page 6: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/6.jpg)
Why C# r0x0rz
Powerful and expressive
Type safety reduces hard-to-track-down bugs
Reflection
Initializer syntax
Great tooling (IntelliSense)Similar enough to C that learning and porting are easy
Blazingly fast compilesC#
![Page 7: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/7.jpg)
.NET on Windows
Usually within a few percent of native performance
Awesome generational garbage collection
Performance shootout: Raymond Chen vs. Rico Marianihttp://blogs.msdn.com/ricom/archive/2005/05/10/416151.aspx
![Page 8: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/8.jpg)
.NET on Xbox 360
Significant delta between managed and native
.NET Compact FrameworkSimplistic mark-and-sweep garbage collection
Xbox is not a general purpose computerUnforgiving in-order CPU architectureRequires custom VMX instructions for optimal math perfSecurity architecture poses challenges for jitted code
![Page 9: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/9.jpg)
.NET on Windows Phone 7 Series
In between Windows and Xbox 360
.NET Compact FrameworkKeep an eye on garbage collection!
ARMv7 CPUMore forgiving toward jitted codeARM jitter is more mature than PPC
![Page 10: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/10.jpg)
Ways To Call Code
Instance method
Interface
Delegate / event
Reflection
Virtual method
![Page 11: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/11.jpg)
Choose Your Own Address
C++ allows independent choice of
.NET types dictate their allocation and usage semantics
Data typeThe memory in which a type lives (placement new)How a type instance is referenced (T, T*, T&, const T&)
Value typesint, bool, struct, Vector3Reference typesclass, array, string, delegate, boxed value types
![Page 12: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/12.jpg)
A Popular Myth
Oft-repeated wisdom
Value types live on the stack
Reference types live on the heap
Value types live wherever they are declaredReference types have two piecesMemory allocated from the heapA pointer to this heap memory
That is subtly incorrect
![Page 13: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/13.jpg)
class vs. struct
By default, prefer class over structUse struct for things that
areSmall (<= 16 bytes)
Short lived
Pass large structs by reference
Matrix a, b, c;c = Matrix.Multiply(a, b); // copies 192 bytes!Matrix.Multiply(ref a, ref b, out c);
![Page 14: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/14.jpg)
Memory Management
C++ .NETAllocate Initially fast, becoming
slower as fragmentation increases
Very fast, apart from periodic garbage collections
Free Fast InstantaneousFragmentation Increases over time NoneCache coherency
Requires custom allocators
Things allocated close in time are also close in physical location
Garbage collection is not optionalCan’t have type safety without automatic memory management
![Page 15: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/15.jpg)
Mark and Sweep
Triggered per megabyte of allocation1Starts with root references (stack variables, statics)2Recursively follows all references to see what other objects can be reached3Anything we didn’t reach must be garbage4Compacts the heap, sliding live objects down to fill holes5
![Page 16: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/16.jpg)
Two Ways To Keep GC Happy
Make it run Less Often
If you never allocate, GC will never run
Make it Finish QuicklyCollection time is proportional to how many
object references must be traversed
Use object pools Simple heap = fast collection
Use value types and integer handles
![Page 17: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/17.jpg)
GC.Collect
Explicitly forces a garbage collection
Use wisely to give yourself more headroomAfter loadingDuring pauses in gameplay
Don’t call every frame!
![Page 18: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/18.jpg)
Avoiding Allocation
Beware of boxing
string vs. StringBuilder
Use WeakReference to track GC frequencyhttp://blogs.msdn.com/shawnhar/archive/2007/10/12/monitoring-the-garbage-collector.aspx
Use CLR Profiler on WindowsSee Cullen Waters talk: “Development and Debugging Tools for Windows Phone 7 Series”
Use .NET Reflector to peek behind the curtainhttp://www.red-gate.com/products/reflector/
![Page 19: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/19.jpg)
foreach is Syntactic Sugar
foreach (var x in collection) DoStuff(x);
becomes:
var enumerator = collection.GetEnumerator();while (enumerator.MoveNext()) DoStuff(enumerator.Current);
Is the enumerator a value type?Array, List<T>, and most XNA types are fineSome collection types create garbage
![Page 20: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/20.jpg)
Iterator Methods
IEnumerable<Action> Think(){ while (true) { Heading = ChooseRandomDirection();
while (ElapsedTime < 23) yield return Action.WalkForward;
yield return Action.LookAround;
if (Vector3.Distance(Position, Enemy.Position) < 42) { yield return Action.LoadWeapon; yield return Action.FireWeapon; } }}
![Page 21: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/21.jpg)
Iterators Are Compiler Magic
[CompilerGenerated]private sealed class <Think>d__0 : IEnumerable<Action>{ private int <>1__state; private Action <>2__current;
private bool MoveNext() { switch (this.<>1__state) { case 0: this.<>1__state = -1; break;
case 1: goto Label_0073;
case 2: this.<>1__state = -1; if (Vector3.Distance(this.<>4__this.Position, ... break; this.<>2__current = Action.LoadWeapon; this.<>1__state = 3; return true;
case 3: this.<>1__state = -1; this.<>2__current = Action.FireWeapon; this.<>1__state = 4; return true;
case 4: this.<>1__state = -1; break;
default: return false; } this.<>4__this.Heading = ChooseRandomDirection(); while (this.<>4__this.ElapsedTime < 23f) { this.<>2__current = Action.WalkForward; this.<>1__state = 1; return true; Label_0073: this.<>1__state = -1; } this.<>2__current = Action.LookAround; this.<>1__state = 2; return true; }
![Page 22: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/22.jpg)
The GPU
![Page 23: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/23.jpg)
Five Configurable Effects
Plus hardware accelerated 2D sprite drawing
BasicEffect SkinnedEffect EnvironmentMapEffectAlphaTestEffectDualTextureEffect
![Page 24: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/24.jpg)
BasicEffect
BasicEffect SkinnedEffect EnvironmentMapEffectAlphaTestEffectDualTextureEffect
• 0-3 directional lights• Blinn-Phong shading• Optional texture• Optional fog• Optional vertex color
Vertex Cost Pixel CostNo lighting 5 1One vertex light 40 1Three vertex lights 60 1Three pixel lights 18 50+ Texture +1 +2+ Fog +4 +2
![Page 25: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/25.jpg)
DualTextureEffect
BasicEffect SkinnedEffect EnvironmentMapEffectAlphaTestEffectDualTextureEffect
• For lightmaps, detail textures, decals• Blends two textures• Separate texture coordinates• Modulate 2X combine mode (A*B*2)• Good visuals at low pixel cost
Vertex Cost
Pixel Cost
Two Textures 7 6+ Fog +4 +2
![Page 26: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/26.jpg)
AlphaTestEffect
BasicEffect SkinnedEffect EnvironmentMapEffectAlphaTestEffectDualTextureEffect
• For billboards and imposters• Adds alpha test operations (pixel kill)• Standard blending is free with all effects• Only need alpha test if you want to
disable depth/stencil writes
Vertex Cost Pixel Cost<, <=, >=, >
6 6
==, != 6 10+ Fog +4 +2
![Page 27: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/27.jpg)
SkinnedEffect
BasicEffect SkinnedEffect EnvironmentMapEffectAlphaTestEffectDualTextureEffect
• For animated models and instancing• Game code animates bones on CPU• Vertex skinning performed by GPU• Up to 72 bones• One, two, or four weights per vertex
Vertex Cost
Pixel Cost
One vertex light 55 4Three vertex lights 75 4Three pixel lights 33 51+ Two bones +7 +0+ Four bones +13 +0+ Fog +0 +2
![Page 28: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/28.jpg)
EnvironmentMapEffect
BasicEffect SkinnedEffect EnvironmentMapEffectAlphaTestEffectDualTextureEffect
• Oooh, shiny!• Diffuse texture + cube environment map• Cheap way to fake many complex lights• Fresnel term simulates behavior when light reaches
a surface and some reflects, some penetrates
Vertex Cost Pixel Cost
One light 32 6Three lights 36 6
+ Fresnel +7 +0
+ Specular +0 +2
+ Fog +0 +2
![Page 29: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/29.jpg)
A Balancing Act
Framerate
Numberof PixelsPixel Cost
![Page 30: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/30.jpg)
Balancing Framerate
Framerate
• 30 hz refresh rate• No point updating faster than the
display!
Game.TargetElapsedTime = TimeSpan.FromSeconds(1f / 30);
![Page 31: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/31.jpg)
Balancing Pixel Cost
Pixel Cost• Prefer cheaper effects• Minimize overdraw• Many known algorithms:•Distance, frustum, BSP, sort front to back
• Implement “overdraw xray mode” •Draw untextured with additive blending•Brighter areas indicate overdraw
![Page 32: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/32.jpg)
Balancing Number of Pixels
Numberof Pixels
• 800x480 is 25% more pixels than Xbox 1• Great for text• Too many pixels for intensive
games• 800x480 = 384,000 pixels• 600x360 = 216,000 pixels (56%)
• Dedicated hardware scaler• Does not consume any GPU• Higher quality than bilinear
upsampling
![Page 33: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/33.jpg)
Demo
Scaler Demo
![Page 34: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/34.jpg)
XNA Framework API Cheat SheetAvoid Prefer
RenderTargetUsage.PreserveContents RenderTargetUsage.DiscardContents
device.BlendState = new BlendState {…}; // At startupstatic BlendState myState = new BlendState
{…};
// Per frameDevice.BlendState = myState;
VertexBuffer.SetData(…) device.DrawUserPrimitives(…);
// or
DynamicVertexBuffer.SetData(…, SetDataOptions.NoOverwrite);
![Page 35: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/35.jpg)
Summary
Great performance comes from great knowledgeUnderstand
Actions
Value types vs. reference typesGarbage collectionC# compiler magic (foreach, iterator methods, closures)Cost of the different graphical effect options
Use CLR Profiler and .NET ReflectorRender smaller than display resolution, rely on scaler
![Page 36: High Performance 3D Games on Windows Phone 7 Series](https://reader035.fdocuments.us/reader035/viewer/2022062812/5681635b550346895dd41ce4/html5/thumbnails/36.jpg)
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the
date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.