Linux as a gaming platform, ideology aside

23 580 vues

Publié le

Linux commonly connotes with open-source zealots and a small PC market
share, not blockbuster video games. However, the arrival of Steam on
the platform might change the outlook quite dramatically, and Linux
support may soon become a must-have feature for your game. Setting the
open-source ideology aside, this lecture is an overview of the
technical challenges a game developer may face while porting their
game to this platform, along with solutions.

Publié dans : Technologie
1 commentaire
2 j’aime
  • Leszek also posted a few corrections:
    Voulez-vous vraiment ?  Oui  Non
    Votre message apparaîtra ici
Aucun téléchargement
Nombre de vues
23 580
Sur SlideShare
Issues des intégrations
18 282
Intégrations 0
Aucune incorporation

Aucune remarque pour cette diapositive

Linux as a gaming platform, ideology aside

  1. 1. Linux as a gaming platformIdeology asideDigital Dragons · 19-04-2013Leszek
  2. 2. 2What is this talk about?www.thefarm51.comIts NOT about:● Free software● Ideology● BusinessIt is about:● Linux 101● Development● Deployment2
  3. 3. 3Who is this guy?www.thefarm51.comCross-platform work(→ de facto – Linux)●● Toy projects, personal utilities,university stuff● Games as well!● AC-130● Crystal Space● idTech 3-based games● Painkiller Hell & Damnation● Linux as preferred OS for 7+ years● Former open-source evangelist● Not anymore – lost interest3
  4. 4. 4Who is this guy?www.thefarm51.com4WHAT IF I TOLD YOUWE PREACHED FREE SOFTWARE
  5. 5. 5Who is this guy?www.thefarm51.com5
  6. 6. 6www.thefarm51.comLinux 1016
  7. 7. 7OMG LINUX!!11www.thefarm51.com7
  8. 8. 8OMG LINUX!!11www.thefarm51.comRichard M. StallmanSource:
  9. 9. 9OMG LINUX!!“F**k you, NVIDIA!” - Linus TorvalsSource:
  10. 10. 10Kernel and the distroswww.thefarm51.com10Linux distro(Ubuntu, Debian, Fedora...)Linux distro(Ubuntu, Debian, Fedora...)LinuxkernelLinuxkernelFreesoftwareFreesoftwareProprietarysoftwareProprietarysoftware27+ supported CPU architectures! usually mean i386/amd64
  11. 11. 11Who and why uses Linux?www.thefarm51.com11Who:● Scientists (TOP500)● IT companies● Hackers andpower usersWhy:● Its free (well, duh...)● Its robust● Its customizable● Long, intrinsic traditions of free software● No established channels of proprietarysoftware distribution● But Steam can become a gamechanger!
  12. 12.● Linux adheres to ISO/IEC standards, suchas POSIX and Single Unix Specification...● System API – file system access, threading,access control, IPC etc.● Command-line shell utilities● ...which also happen to be supported byMacOS X, Android, iOS (yes, really!) anda handful of other OSes12
  13. 13.● Linux has OpenGL (ES) for 3D graphicsrendering● Linux has OpenAL for 3D audio with DSP● Linux has BSD sockets for networking● Linux has lots of other portable libraries● And all of these things are also availableon other platforms – Android, MacOS X,iOS, Windows!13
  14. 14. 14What does that mean?www.thefarm51.com14MacOS XLinux Mobile** not to scale; actualoverlap is greater
  15. 15. 15There are issues,● Open source legacy → unstable ABIs● Why need a stable one when you can justrecompile the source?● This is why NVIDIA distributes glue code withtheir drivers● Extreme heterogeneity● Package managers (software distributionmechanisms)● Patches applied to upstream code → potentialdifferences in functionality● Highly customized system configurations15
  16. 16. 16There are issues,● Proprietary vs free GPU drivers● Rule of thumb: free drivers perform morepoorly than proprietary ones● Intel only has free, but the hardware is bad anyway ☺● Legacy AMD/ATI GPUs only have free, and its quitegood actually● NVIDIA drivers are equally good as theWindows ones● AMD/ATI drivers are a bit worse than theWindows ones ☹16
  17. 17. 17Solution?www.thefarm51.com17Assume Ubuntu.● For development and testing alike● Probably the most popular desktopdistro*● This is what Valve does- Bonus points: Steam compatiblity!● Power users will get your game runningon others anyway* there are claims its Linux Mint now, but its still based on Ubuntu
  18. 18. 18www.thefarm51.comDevelopment18
  19. 19. 19Getting● Virtual machine● Fair enough for building● No such luck with testing(unless you can virtualise your GPU)● Native installation● Painless dual boot● Which disc image to download?● Get whatever Valve recommends forSteam(the Most recent LTS release is a good bet)19
  20. 20.● Tons and tons of IDEs, mostly average- Code::Blocks, Codelite are OK- Theres also Eclipse if you can stand it● Tried & tested native code toolchains- GCC, LLVM (clang)- gold – the new multithreaded linker by Google- GNU binutils – objcopy, objdump etc.● VM toolchains- Mono, OpenJDK/Oracle JDK● Build tools- GNU make, Cmake, SCons, GNU autotools20
  21. 21. 21Debuggingwww.thefarm51.comCPU● Classic command-line gdb, gdbserver● Tons of front-ends- cgdb (command-line)- Nemiver, DDD (GUI)Graphics● gDEBugger/CodeXL● NVIDIA Nsight forEclipse● apitrace21
  22. 22. 22SDL – the cross-platform “DirectX”● Provides:- API in C with mappings to other languages- Timers and threads- Input – mouse, keyboard, joysticks/pads (incl.Unicode characters)- Window management (incl. GL context)- Audio (very barebone)- 2D graphics (hardware blitting)● Be sure to use 2.0 for new projects!- Support for OpenGL 3+- Support for multi-viewport apps22
  23. 23. 23SDL – the cross-platform “DirectX”● SDL hides away most platform-specific code- Thats right, no need to even use POSIX!23int main (int argc, char *argv[]){// initialize SDLif (SDL_Init(SDL_INIT_VIDEO| SDL_INIT_TIMER) < 0)return 1;// set window captionSDL_WM_SetCaption("My SDL Game","MySDLGame");// hide mouse, grab inputSDL_ShowCursor(0);SDL_WM_GrabInput(SDL_GRAB_ON);// make SDL clean up on exitatexit(SDL_Quit);// main game loopwhile (1) {// dispatch eventswhile (SDL_PollEvent(&ev)){switch (event.type) {// ...}}}SDL_ShowCursor(1);SDL_WM_GrabInput(SDL_GRAB_OFF);return 0;}
  24. 24. 24What if SDL doesnt cut it?www.thefarm51.comDespite its awesomeness, SDL has itsshortcomings● No explicit GLX/WGL context data sharingand no direct context access→ no threaded rendering ☹(hit this corner while porting Painkiller HD)● No 3D positioning or DSP support in thestock SDL audio subsystem- Partially remedied by SDL_mixerBut we need those! Now what?!24
  25. 25. 25Just DIY – POSIX● Most WinAPI features map to POSIX- Threading (pthreads)- Advanced file I/O, incl. memory-mapped- Advanced memory allocation, incl. paging- IP sockets25#if defined(WIN32) || defined(__WIN32__)Emitter->InternalPtr = VirtualAlloc(NULL,Size, MEM_COMMIT | MEM_RESERVE,PAGE_EXECUTE_READWRITE);// ...#else // Its a UNIX system! I know this!Emitter->InternalPtr = mmap(NULL, Size,PROT_READ | PROT_WRITE | PROT_EXEC,MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);// ...#endif
  26. 26. 26POSIX APIwww.thefarm51.comThere is an extensive, centralised referencemanual in the manpages-dev package● Type in a shell to access:man <section number> <subject>● Section number cheat sheet1. General console commands2. System calls3. C standard library calls4. Special files (/dev/*) and drivers5. File formats and conventions26
  27. 27. 27X11 client● Windows graphical mode maps to X11- Xlib is a popular client library for the X(11) WindowSystem server- XCB is a more modern replacement (often a back-end for Xlib)- Raw event pump, window management- OpenGL interoperability via the GLX extension27// event loopXEvent Event;while (XPending(GX11Display)) {XNextEvent(GX11Display, &Event);switch (Event.type) {// ...}}
  28. 28. 28Joystick/gamepad● Not part of X11 input event framework● Kernel block devices- Created as /dev/input/js*- Hotplug events via libudev (also in SteamLinux Runtime)- Handled via open() and ioctl()- Detailed documentation in the kernel
  29. 29. 29Porting a D3D renderer to OpenGLwww.thefarm51.com29
  30. 30. 30Porting a D3D renderer to OpenGLwww.thefarm51.com30Porting Source To Linux:Valves Lessons LearnedValve & NVIDIA, GTC 2013Video:
  31. 31. 31How to deal with heterogeneity?● As mentioned earlier, library ABIs tend tobe unstable- Glibc tags its symbols with version → multipleversions for backwards compatibility- Compile with newest version → binary might notrun on older versions● The ELF format is constant; but even then:- Dynamic libraries “sonames” (including versionnumbers) are baked in at link time → binary mightnot run if system provides a different version31
  32. 32. 32How to deal with heterogeneity?www.thefarm51.com32How?Static linking+ will work forever- binary bloatDistribute with libs+ will work forever- redundant to othergames (also a form ofbloat)- potential licensingissues
  33. 33. 33How to deal with heterogeneity?www.thefarm51.com33Or use the Steam Linux Runtime● Collection of essential packages “ripped”from Ubuntu repos + patches- freetype, glew, gtk+2.0, libgsm, libogg, libopenal1, libsdl1.2,libsdl2, libvorbis, libtheora, libx11, libxcb, pulseaudio...- Release and debug versions of all libs● Ready-to-use GCC-based toolchains fori386 and amd64● Ships with Steam, so every (non-orthodox)Linux gamer has it
  34. 34. 34Steam Runtime for non-Steam● Start-up shell scripts are common practice- Search for an installed Steam Runtimeinstance- If not found, install your private copy- Only when the SLR is available, run theactual binary34
  35. 35. 35Steam Runtime for non-Steam gameswww.thefarm51.com35#!/bin/shif [ -z $STEAM_RUNTIME ]; thenSTEAM_RUNTIME=$HOME/.steam/steam/ubuntu12_32/steam-runtimefiif [ ! -d $STEAM_RUNTIME ]; then# install private copy of Steam Runtimefiexport LD_LIBRARY_PATH="$STEAM_RUNTIME/i386/lib/i386-linux-gnu:""$STEAM_RUNTIME/i386/lib:""$STEAM_RUNTIME/i386/usr/lib/i386-linux-gnu:""$STEAM_RUNTIME/i386/usr/lib:""$STEAM_RUNTIME/amd64/lib/x86_64-linux-gnu:""$STEAM_RUNTIME/amd64/lib:""$STEAM_RUNTIME/amd64/usr/lib/x86_64-linux-gnu:""$STEAM_RUNTIME/amd64/usr/lib:""$LD_LIBRARY_PATH"# launch the actual game here
  36. 36.● The C standard defines locales for languageand regional settings (see man setlocale)● They affect *printf()/*scanf() and more● Xlib can modify locale categories for GUIapps (see man XSetLocaleModifiers)● Example POSIX locales: pl_PL.UTF8,en_GB.ISO-8859-1, ru_RU.KOI8-R● The Linux manual might make it seem likeretrieving current locale is as easy as callingchar *locale = setlocale(LC_ALL, NULL);36
  37. 37. 37Localeswww.thefarm51.comNo such luck!// backup the original application localechar *Locale = setlocale(LC_ALL, NULL);char *OriginalLocale = NULL;if (Locale && *Locale)OriginalLocale = strdup(Locale);// reset locale to system (user) defaultsetlocale(LC_ALL, "");// retrieve the default localeLocale = setlocale(LC_ALL, NULL);// process locale here// restore original localeif (OriginalLocale){setlocale(LC_ALL, OrigLocale);free(OriginalLocale);}37
  38. 38. 38Extracting debugging● We often want to keep symbols forshipping binaries● On Windows – keep the .pdb files● On Linuxobjcopy --only-keep-debug "${tostripfile}" "${debugdir}/${debugfile}"strip --strip-debug --strip-unneeded "${tostripfile}"objcopy --add-gnu-debuglink= "${debugdir}/${debugfile}" "${tostripfile}"38
  39. 39. 39Crash● On POSIX this is called a signal handler● Signals are a primitive IPC mechanism usednot only for crashes- Debugger traps, floating point exceptions, terminalhangup, exit requests... See the output of kill -l● You can take a shortcut by using:- libSegFault – part of glibc, try:$ LD_PRELOAD=/lib/ /lib/ Andrew Tridgells segv_handler
  40. 40. 40A word on licensingwww.thefarm51.comWhenever you use a library, check itslicensing terms!● GPL is restrictive- Requires derivative works to use GPL→ sharing the source code● LGPL is restrictive with linking exception- Allows linking to an LGPL library withoutsharing the source code● MIT, BSD and Zlib licenses are generallypermissive, some might require advertising40
  41. 41. 41www.thefarm51.comDeployment41
  42. 42. 42Where do I put the files?● Windows- Game data and binaries: C:Program Files- User data: %APPDATA%, Documents...- AoS-like organization● Linux (conventional)- Game binaries: /usr/bin/, /usr/games/- Game data: /usr/share/games/- User data: $HOME/.config/($XDG_CONFIG_HOME environment variable)- SoA-like organization- Filesystem Hierarchy Standard42
  43. 43. 43Where do I put the files?● FHS mainly concerns distro packages● Proprietary software often installs into/opt/<package name>/(“Add-on application software packages”)● Proprietary software also often installs“wherever”● Steam games live in~/.steam/steam/SteamApps/● Just put game data and binaries in oneplace, and save user data to ~/.config/43
  44. 44. 44Creating installation● Not relevant for Steam games (duh)● Simple .tar.gz archives$ tar czvf <archive name> <input files...>● Distro packages (.rpm, .deb...)● Self-extracting command-line installers asshell scripts● MojoSetup
  45. 45. 45www.thefarm51.comSummary45
  46. 46.● Engineering costs of a Linux port for analready multiplatfrom game engine are low- “Its just an API”™● SDL covers a lot of boilerplate for you● Overcoming heterogeneity is essential- Steam Linux Runtime is helping to fix it● Watch out for those licenses!● When in doubt:- Use Ubuntu- Do whatever Valve does46
  47. 47. 47www.thefarm51.comQuestions?leszek.godlewski@thefarm51.com47
  48. 48. 48Special thankswww.thefarm51.comRyan “Icculus” GordonInspiration & software contributionsMichał WielgusCritical review & free software comradeReinhard PolliceGetting stuff done for me on the PKHD port48
  49. 49. 49Thank you!Like us on Facebook!