Mono for Game Developers Miguel de Icaza [email protected], @migueldeicaza Xamarin Inc Agenda • Mono in Games • Using Mono for Games • Performance • Garbage CollecKon • Co-rouKnes, Asynchronous Programming Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com MONO IN GAMES C# Java JavaScript Ruby Python Visual Basic F# Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com C# Java JavaScript Ruby Python Visual Basic F# Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Sims 3 • Mixed Code: – C/C++ engine – C# scripKng/AI – C# high-level • Visual Studio + Mono • X86, PS3, Xbox360 Credit: www.thesims3facts.webs.com BasKon – on Google Chrome NaCl • C# XNA codebase • Originally on Xbox • Ported to NaveClient – Mono – MonoGame (XNA) • Mac, Windows, Linux Pure C# - SoulCra • DeltaEngine – Pure C# engine – Open source – Android, iOS, Mac, Win Unity 3D • Unity Engine – C/C++ game engine – Embedded Mono • User code – C# or UnityScript – Extends Unity itself Shadow Gun, built with Unity SecondLife • Mono on the server • Powers LSL scripts • Nice 200x perf boost • Code InjecKon Infinite Flight • Subject of the second part of this session WHY MONO? Because Life is too Short • To debug another memory leak • To track another memory corrupKon bug • Because you deserve be=er Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com The Quest for ProducKvity System Languages Scripng Languages Pros: Pros: • Low-level • High-level, good producKvity • Good control of hardware • • Typed Easy to write • Fast code • Safe, prevent crashes • Loosely typed Cons: • Easy to corrupt state Cons: • Low producKvity • Poor control of hardware • Crash o_en • Complex for newcomers • Slow (interpreted) John Ousterhout ScripKng Quest IEEE 1998 Summary Paper hp://www.stanford.edu/~ouster/cgi-bin/papersMono for Game Developers – AltDevConf 2012 /scripng.pdf hp://www.xamarin.com John was always ahead of his Kme • Professional workstaons in 1998 – SPARC, HP-PA • Not achievable on PCs of the Kme Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com 2000 – Desktop Development • Building desktop apps with C and C++ – Slow progress, error prone, frequent crashes • Windows 2000 Requirements: – 133 Mhz or more – 64 megs for desktop, 256 for server • Windows XP Requirements (one year later) – 233Mhz or more – 128 megs for desktop • Development desktops at the Kme: – ~1Ghz speed – ~1 GB of memory Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com C# Introduced in 2000 • C# 1.0 was a Java-like system • With many design fixes – 10 years of experience – Change defaults (all virtual, vs opt-in virtual) – Introduce structs (help GC, no boxing) – Direct access to nave libraries (P/Invoke) – Delegates (foundaon for lambdas) Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Language Choices Fully Dynamic Easy C#/Java C/C++ ProducKvity Assembly Language Difficult Slow Performance Fast Game So_ware Components . Display Simulaon Game Logic Support • Rendering • Physics • World rules • Audio • Shading • Collision • Enemy AI • Input • Scene • ParKcles • User • Networking • Animaon • Terrain control • Geometry • Camera • GUI • Behavior The Problem Games are real-Kme programs • 30 to 60 frames per second (0.016 seconds) Input AI Updates • User control • Scripted, slow • Render Graphics • Network • React to • Play audio events change • Update scene Problem: ScripKng Is A Bo=leneck Gaming's Achilles' Heel Display Simulaon Game Logic Support • Rendering • Physics • World rules • Audio • Shading • Collision • Enemy AI • Input • Scene • ParKcles • User • Networking • Animaon • Terrain control • Geometry • Camera • GUI • Behavior C/C++ C/C++ Script C/C++ Problem: ScripKng Is A Bo=leneck Gaming's Achilles' Heel Display Simulaon Game Logic Support • Rendering • Physics • World rules • Audio • Shading • Collision • Enemy AI • Input • Scene • ParKcles • User • Networking • Animaon • Terrain control • Geometry • Camera • GUI • Behavior C/C++ C/C++ Script C/C++ C# What C# Offers • Close to nave performance – 50%-90% of nave performance • Safe ExecuKon Environment – With opKonal support to shoot yourself in the foot. Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com C# - An Evolving Language 2012 5.0 Asynchronous Programming 2010 4.0 Dynamic extensions 2007 3.0 Language Integrated Query, FuncKonal 2005 2.0 Generics, Iterators, Lambdas 2002 1.0 Managed Code, strongly typed USING MONO Designing Mono Applicaons • Provided: – C# Language – Base Class Libraries • Not Provided: – User Interface, Graphics, Audio – These are all plaorm specific Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Code Sharing and Nave Experience C# Core Engine, Shared Logic, Business Logic Plus ECMA languages RunKme .NET Mono Windows OS Xbox Mac iOS Android WinPhone Not a comprehensive list Code Sharing and Nave Experience Nave UI XAML MonoMac MonoTouch MonoDroid APIs XNA C# Core Engine, Shared Logic, Business Logic Plus ECMA languages RunKme .NET Mono Windows OS Xbox Mac iOS Android WinPhone Not a comprehensive list Modes of Use • Drive the applicaon • Scripng engine – Sandboxed – Full access Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Run on Mono Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Run on Mono Game – Your C# Code Mono C#/.NET Libraries RunKme MonoGame OpenTK Physics Operang System OpenAL OpenGL Networking Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Using Mono as a Library Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Use Mono as a Library Game Engine Game Engine Libraries Mono Game – Your Audio Graphics C# Code Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com TIPS ON USING MONO Two Code Generaon Backends Mono’s Na<ve Backend LLVM Backend • Very fast codegen • Very slow codegen – .3 seconds bootstrap – 7 second bootstrap • Not great code output • Great output quality • JIT’s default engine • Opt-in: – mono --llvm Just in Time vs Ahead of Time • Just in Time Compilaon – Default Mode of Operaon – Very fast at compiling code – Not great quality of code generaon • Ahead of Time Compilaon – Mandatory on some plaorms • PS3, XBox360, iOS – Can afford expensive compiler opKmizaons Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Arrays Bounds Checking Mono RunKme translates this to: Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Disabling Arrays Bounds Checking • Very unsafe – GC depends on system integrity – But admissible if no error ever found on tesKng • We give you the tools to shoot your feet – mono –O=unsafe • Ask your QA team Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com GARBAGE COLLECTION Mono’s Garbage Collectors • Boehm GC: – TradiKonal Mono GC – Mostly-precise, stack conservave – Scans everything on each GC • Generaonal Collector (SGen) – New (default on Android) – Generaonal (Old generaon, nurseries) – Copying (plus mark+sweep for large objects) Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com SGen Old Nursery Generaon • New objects • Aged objects • Small size (4MB) • Slower collecon • Per thread regions • Fixed or variable • Very fast heaps collecon • Parallel collecKon Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Garbage CollecKon Memory Allocated Released later Garbage Collector determines when to run and release memory • HeurisKcs are plaorm-specific • GC.Collect() is the only determinisKc opKon Best PracKces • Pre-allocate major objects before Game Loop – Managed objects – Or unmanaged buffers – Try to only use the nursery (stay under 4M) – If you must collect, only collect the nursery: • GC.Collect (0) – Performs only a nursery collecKon • GC.Collect () – Performs a complete GC on the heap • On Main loop: – Use structs instead of classes Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Schedule GC Collecon Game Game Game Network Audio Physics Logic 1 Logic 2 Logic 3 Checks Updates Game Game Game GC Network Audio Physics Logic 1 Logic 2 Logic 3 Collect Checks Updates GC.Collect (0) Limit CollecKon to Nursery Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Mono’s GC Thread Control • Garbage CollecKon Stops all Mono Threads • Non-Mono threads are not affected • Alternave: • Use a Render Scene + Render Thread • Like Apple’s CoreAnimaon or Microso_ WPF Game Network Mono IO Thread Thread Thread Render Scene Render Real Time Mono Thread Thread Thread 1 Nave Thread Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com COROUTINES State-based programming Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Problems with Callbacks and State-Machines Systems • RepeKKve • Cumbersome • Error Prone • Poor Error Propagaon protocols/pracKces • Life is too short Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Co-rounes • Popular soluKon to simplify AI code • Each Game Object has a script aached – Runs Game Logic – AI bits • Many soluKons – longjmp/setjmp for unmanaged code – Stack fiddling (Mono.Tasklets) – Interpreted languages with VM support Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com C# 5.0 and Async Programming • Mono master has a complete C# 5 Compiler • Turns repeKKve callback-based async programming into linear programming – Compiler rewrites the code into a state machine – Tasks are scheduled on the main thread – Scheduling is customizable • Originally designed for interacKve UIs Mono for Game Developers – AltDevConf 2012 hp://www.xamarin.com Using Await Mono
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages70 Page
-
File Size-