SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
C++ on the Web
A Tale from the Trenches
Andre Weissflog
Head of Development, Berlin
Bigpoint GmbH
GDC Europe 2013
What’s this about?
• the web as a new target platform for C++ code
• differences to traditional platforms
• differences between C++/web technologies
• porting problems and solutions
Demos
• Dragons Demo: minimal 3D skinned
character demo [show demo]
• Map Demo: more advanced 3D demo
[show demo]
• based on Nebula3 engine, also used in
Drakensang Online
Why develop for the web
HTML5 + WebGL?
Create Deploy Play
• no walled gardens, no gate-keepers, no certification process
• free choice of hosting & payment providers
• no installations, no updates, no plugins, no lengthy downloads
• multi-platform “for free”
• battle-hardened security infrastructure
The web is the most open and seamless
platform for users and developers.
C++ to web technologies
Google’s pNaCl
Mozilla’s emscripten
Adobe’s crossbridge
LLVM has opened up a lot of new usage
scenarios for C/C++...
...for instance running C/C++ code inside byte
codeVMs and other sandboxed environments:
Mozilla’s emscripten
• OpenSource project, started in 2010
• .cpp → LLVM .bc → .js
• extremely active and responsive dev team
• lots of wrapper APIs (OpenGL, SDL, GLUT, ...)
• limited threading support (no pthreads)
Recent Developments:
• asm.js (highly optimizable subset of JS)
• massive compilation speed improvements
• inline Javascript directly into C++
Google’s pNaCl
• OpenSource project, started in 2008
• .cpp → LLVM .bc → (deploy) x86/x64/ARM
• Google Chrome only
• safe sandbox for native code execution
• full pthreads implementation
Recent Developments:
• pNaCl finally ready for prime-time
• enabled in Chrome v.30 and up
• no longer restricted to Chrome Web Store apps
• formerly known as Alchemy and flascc
• started in 2008, recently open-sourced
• .cpp → LLVM .bc → AVM2 byte code
• runs in Flash plugin
• proprietary 3D API (Stage3D)
• incredibly slow and resource hungry build process :/
Adobe’s crossbridge
Focus...
Will mostly talk about emscripten (and some pNaCl)
Why:
• emscripten has widest reach (all major browsers)
• emscripten progresses incredibly fast
• pNaCl currently has edge in threading support
• pNaCl and emscripten are actually quite similar from dev
perspective
But Javascript is slow, isn’t it?
asm.js generated code is probably faster than you think, and pNaCl
generated code is probably slower than you think (don’t have hard
benchmark numbers yet... sorry)
IMHO: for 3D games, the real performance gains will come
through WebGL extensions, high call-overhead requires
extensions to reduce number of GL calls!
My [OSX] dev environment
• Xcode (for compiling/debugging native OSX and iOS apps)
• Eclipse (for emscripten and NaCl specific dev work)
• emscripten SDK
• NaCl SDK
• cmake
• a local HTTP server (e.g.“python -m SimpleHTTPServer”)
Multiplatform Build System
ios.toolchain.cmake
osx.toolchain.cmake
pnacl.toolchain.cmake
emscripten.toolchain.cmake
android.toolchain.cmake
CMakeLists.txt + make
cmake: flexible meta-build-system, generates IDE
project files and/or makefiles from generic
“CMakeLists” files.
cmake
cmake toolchain files define
platform-specific tools, header/lib
search paths and compile options
windows.toolchain.cmake
pNaCl
CMakeLists.txt files define
compile targets and their source
files
Multiplatform Ecosystem
32 BIT
+
64 BIT
x86, x86_64,
ARM
OpenGL
vs
Direct3D?
BigEndian no
longer matters
POSIX
+
Windows
code must be
32/64 bit clean
no exceptions
no RTTI
no STL
no Boost
Windows still
big, everything
else is POSIX-ish
OpenGL Renaissance,
but D3D9 still relevant
these make
porting to exotic
platforms often
harder, not easier
•WinXP is still incredibly big in Eastern Europe & Asia
• 3D feature base-line is OpenGL ES 2.0 w/o extensions (== WebGL)
• go fully GL on all platforms? (GL driver quality? Win8 Metro apps? ANGLE?)
What keeps me awake at night:
N3 Multiplatform Philosophy
Platform-specific code lives in its
own sub-directories.
__POSIX__
__IOS__
__NACL__
__EMSCRIPTEN__
...
Platform-specific pre-processor defines
provided by build system.
class DisplayCoreBase
class NaclDisplayCore class EmscDisplayCore class IOSDisplayCore
#if __NACL__ #if __EMSCRIPTEN__ #if __IOS__
#endif #endif #endif
class DisplayCore
Diamond-shape class hierarchy resolved at compile time:
Multiplatform Line Counts
Dragons Demo (~170k lines of code)
platform-agnostic: 148k
POSIX/CRT: 7k
OpenGL: 6.7k
emscripten: 3k
pNaCl: 3.5k
OSX/iOS: 2.2k
about ~2% platform-
specific code
Size Comparisons (Dragons Demo)
~170k lines of C++ code
OSX (-arch i386 -O3):
• orig: 2027 kByte
• +no asserts: 1457 kByte
• +stripped: 1237 kByte
• +gzipped: 413 kByte
OSX (-arch x86_64 -O3):
• orig: 2134 kByte
• +no asserts: 1663 kByte
• +stripped: 1427 kByte
• +gzipped: 460 kByte
iOS (-arch armv7 -O3):
• orig: 1542 kByte
• +no asserts: 1196 kByte
• +stripped: 972 kByte
• +gzipped: 395 kByte
pNaCl (-O2):
• orig: 1654 kByte
• +no asserts: 1333 kByte
• +stripped: 1333 kByte
• +gzipped: 842 kByte
emscripten (-O2 --llvm-opts 3 --llvm-lto 3):
• orig: 5414 kByte
• +no asserts: 2154 kByte
• +closure pass: 1951 kByte
• +gzipped: 486 kByte
wow, surprisingly compact!
smaller than expected
bigger than expected
closure: Google’s JS
optimizer/minifier
The Callback Problem
Key Point to understand (and accept):
Browser runtime environment uses callback
model for asynchronous programming.
Start lengthy operation, provide callback which will be
called when operation is finished: becomes very messy
very quickly.
Games are usually frame-driven, not callback-driven.
This is the main riddle when trying to port a game
engine to browser platforms.
The Game Loop Problem
Most event-driven platforms don’t let you “own the
game loop”.
Instead the application runs completely inside event
callback functions which must return quickly.
Failing to return quickly results in unresponsive
behaviour or even your app being killed.
pNaCl
The Game Loop Problem
Best solution is to use the app main thread
exclusively for system event handling...
...and spawn a “Game Thread” which runs the actual
game loop.
Main
Thread
Game
Thread
input events
system events
quit event
display change
events
Only wakes
up on system
events.
Runs your typical
“infinite” game
loop.
The CallOnMainThread-Problem
Some platforms have restrictions what OS functionality
is accessible from threads.
E.g. must call OpenGL or IO functions from the
main thread only.
pNaCl
Either run everything on main thread, or dispatch
“system calls” to run asynchronously on main thread.
CallOnMainThread problem
All “PPAPI calls” must happen on main thread, and the
main thread must never block.
pNaCl
Threads can push function pointers for deferred
execution on main thread.
Deferred function calls and result callbacks execute in a simple
run-loop after your per-frame callback on the main thread.
This primitive runloop/callback model makes it easy to shoot
yourself in the foot by waiting for events triggered by your own
callbacks.This stops the entire runloop and freezes the app.
But: All other threads can block as much as they want, waiting for events
triggered by callbacks on the main thread. Nice way to simulate blocking
I/O.
Conclusion: pNaCl’s full threading support can be used to
workaround many of its restrictions by moving the actual game logic
into its own thread, and use the main thread only for “system calls” and
their result callbacks.
CallOnMainThread visualizedpNaCl
Init():
launch Game Thread
StartIO():
begin async IO,
set finish-callback
to FinishIO()
FinishIO():
set finished-condvar
Main Thread
put StartIO func ptr on
main thread’s run
queue and wait for
finished-condvar
new thread
CallOnMainThread(StartIO)
your
Game Thread
finished-condvar is set,
continue Game Thread
...blocked...
finished-condvar set
your pNaCl
main-thread code
invoke callbacks to
pNaCl app code:
initialization
invoke deferred funcs
invoke result callbacks
...
pNaCl runtime
(runloop/callbacks)
Limitations
Similar restrictions as pNaCl, but can’t
easily use threads to workaround them:
• most “interesting functions” (WebGL!) must be called from main thread
• main thread must not block
• no pthreads, only WebWorkers for threading
•WebWorkers have their own “address space”
Can’t move entire game loop into WebWorker
thread (yet?)
Browser vendors working towards more flexible
WebWorkers, but HTML5 standardization takes time.
Limitation Workarounds
All your code must run inside “slices”,
always return within 16 or 32 ms to browser.
If something takes longer, either spread work
over several frames, or move into WebWorker.
N3 has new “PhasedApplication” model: app goes through phases,
which tick themselves forward when finished.
OnInit
OnPreloading
OnOpening
OnRunning
OnClosing
OnQuit
OnFrame
emscripten
runtime
environment
max 16ms or 32ms (for 60 or 30 fps)
Threading Workarounds
Failed approach: Try to wrap low-level threading code in some sort of
“co-operative thread scheduling” system.
Success: Move abstraction to a higher level (don’t wrap “low level
threads”, but wrap “parallel task system”).
2 uses for threading: hide blocking / make use of additional CPU cores.
Dispatcher
Worker
Thread(s)
request
Nebula3 parallel task system model
3 Flavours:
• Blocking: thread sleeps until messages arrives
• Timeout: block until messages arrive, or timeout occurs
• Run-through: infinite loop doing per-frame work, pull messages
emscripten port adds 2 “run modes”:
• Parallel: work is pushed to WebWorker threads (makes use of cpu cores)
• Sliced: runs on main-thread, work is “triggered” per frame (hides callback mess)
response
queue
Nebula3 IO System
IO
System
HTTP File
System
App Code
IO request
IO response
with Stream
object
Closer to HTTP philosophy then fopen()/fclose():
• URLs instead of file system paths
• asynchronous IO is default, synchronous is special case
• pluggable filesystem handlers associated with URL scheme (http://, file://, ...)
• Stream objects with StreamReaders and StreamWriters
Local File
System
http://..,
file://...
Stream object with
file data
• Filesystem modules return Stream objects holding downloaded data
• Stream objects have typical Read/Seek/... methods
• IO reponse is a “Future” object, app code polls whether response has become valid
Asset Loading
Easy way: emscripten can pre-load assets into memory before app starts,
accessible through fopen() / fread()
HTTP
File System
Web Server
HTTP request
Downside: delay on startup, memory cost - doesn’t work well for big asset sets.
Solution: need to stream and uncompress all assets on demand asynchronously
HTTP response
Problem: HTTP downloads much slower than loading from HDD, can’t
block while waiting for download to finish.
Uncompress
WebWorker
App Code
IO request
IO response
HTTP File System has platform-specific implementations:
• emscripten: emscripten_async_wget_data()
• pNaCl: pp::URLLoader
• OSX/iOS: NSURLRequest
• Linux / Windows: libCURL
• fallback: home-made HTTP client using raw TCP sockets (tricky!)
Preloading Phase
Loading Screen On
Preloading
Problem: Sometimes asynchronous loading is too much hassle, or even impossible
(for instance when using 3rd party libs).
Solution: Have pre-loading app phases, show loading screen, download and pin
files into a memory filesystem, continue to next app phase when files have
finished downloading.
Synchronous IO functions exclusively access data in memory filesystem, fail if file
hasn’t been preloaded.
Running
Loading Screen Off
Loading Screen On
Preloading
Loading Screen Off
Running
Memory
File System
fread()
fread()
populate
populate
Web Server
HTTP
Only use this approach when absolutely
necessary and only for small files, not for
textures, geometry, audio, etc...
Debugging
None of the C++ web solutions have really good
interactive debugging support (yet).
Develop and debug your app mainly as a native desktop app for
OSX or Windows inside XCode orVStudio, this gives the best turn-
around time and “debugging experience”
Only fall-back to low-level debugging for platform-specific code.
emscripten debugging can be surprisingly easy:
• generated Javascript can be made very readable (see -g options in emcc)
• can inject debugging statements without recompiling
• see emscripten/src/settings.js for some interesting runtime debug options
JS Debugging with Source Maps
emcc -g4 generates source maps containing
reference data to the original C++ sources.
Interactively debug C++ code in the browser!
(still feels very rough around the edges though)
Too many slides, too little time...
Other interesting problem areas:
Audio Networking
WebAudio vs Audio tag
no common
compressed audio
format across browsers
WebSockets or WebRTC
much more restrictive
than Berkley Sockets
(security reasons)
Feels like back in the 90’s,
have to roll our own Audio and
Networking libs AGAIN :(
Too many slides, too little time...
OpenGL
have to settle on OpenGL ES 2 feature set
“it just works!”
...even on mobile: Main problem is
call overhead into
WebGL, but it’s
still surprisingly
fast.
Questions?
Resources
http://flohofwoe.blogspot.com
http://www.flohofwoe.net/demos.html

Contenu connexe

Dernier

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Dernier (20)

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 

En vedette

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

En vedette (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

C++ on the Web (GDCE 2013)

  • 1. C++ on the Web A Tale from the Trenches Andre Weissflog Head of Development, Berlin Bigpoint GmbH GDC Europe 2013
  • 2. What’s this about? • the web as a new target platform for C++ code • differences to traditional platforms • differences between C++/web technologies • porting problems and solutions
  • 3. Demos • Dragons Demo: minimal 3D skinned character demo [show demo] • Map Demo: more advanced 3D demo [show demo] • based on Nebula3 engine, also used in Drakensang Online
  • 4. Why develop for the web HTML5 + WebGL? Create Deploy Play • no walled gardens, no gate-keepers, no certification process • free choice of hosting & payment providers • no installations, no updates, no plugins, no lengthy downloads • multi-platform “for free” • battle-hardened security infrastructure The web is the most open and seamless platform for users and developers.
  • 5. C++ to web technologies Google’s pNaCl Mozilla’s emscripten Adobe’s crossbridge LLVM has opened up a lot of new usage scenarios for C/C++... ...for instance running C/C++ code inside byte codeVMs and other sandboxed environments:
  • 6. Mozilla’s emscripten • OpenSource project, started in 2010 • .cpp → LLVM .bc → .js • extremely active and responsive dev team • lots of wrapper APIs (OpenGL, SDL, GLUT, ...) • limited threading support (no pthreads) Recent Developments: • asm.js (highly optimizable subset of JS) • massive compilation speed improvements • inline Javascript directly into C++
  • 7. Google’s pNaCl • OpenSource project, started in 2008 • .cpp → LLVM .bc → (deploy) x86/x64/ARM • Google Chrome only • safe sandbox for native code execution • full pthreads implementation Recent Developments: • pNaCl finally ready for prime-time • enabled in Chrome v.30 and up • no longer restricted to Chrome Web Store apps
  • 8. • formerly known as Alchemy and flascc • started in 2008, recently open-sourced • .cpp → LLVM .bc → AVM2 byte code • runs in Flash plugin • proprietary 3D API (Stage3D) • incredibly slow and resource hungry build process :/ Adobe’s crossbridge
  • 9. Focus... Will mostly talk about emscripten (and some pNaCl) Why: • emscripten has widest reach (all major browsers) • emscripten progresses incredibly fast • pNaCl currently has edge in threading support • pNaCl and emscripten are actually quite similar from dev perspective But Javascript is slow, isn’t it? asm.js generated code is probably faster than you think, and pNaCl generated code is probably slower than you think (don’t have hard benchmark numbers yet... sorry) IMHO: for 3D games, the real performance gains will come through WebGL extensions, high call-overhead requires extensions to reduce number of GL calls!
  • 10. My [OSX] dev environment • Xcode (for compiling/debugging native OSX and iOS apps) • Eclipse (for emscripten and NaCl specific dev work) • emscripten SDK • NaCl SDK • cmake • a local HTTP server (e.g.“python -m SimpleHTTPServer”)
  • 11. Multiplatform Build System ios.toolchain.cmake osx.toolchain.cmake pnacl.toolchain.cmake emscripten.toolchain.cmake android.toolchain.cmake CMakeLists.txt + make cmake: flexible meta-build-system, generates IDE project files and/or makefiles from generic “CMakeLists” files. cmake cmake toolchain files define platform-specific tools, header/lib search paths and compile options windows.toolchain.cmake pNaCl CMakeLists.txt files define compile targets and their source files
  • 12. Multiplatform Ecosystem 32 BIT + 64 BIT x86, x86_64, ARM OpenGL vs Direct3D? BigEndian no longer matters POSIX + Windows code must be 32/64 bit clean no exceptions no RTTI no STL no Boost Windows still big, everything else is POSIX-ish OpenGL Renaissance, but D3D9 still relevant these make porting to exotic platforms often harder, not easier •WinXP is still incredibly big in Eastern Europe & Asia • 3D feature base-line is OpenGL ES 2.0 w/o extensions (== WebGL) • go fully GL on all platforms? (GL driver quality? Win8 Metro apps? ANGLE?) What keeps me awake at night:
  • 13. N3 Multiplatform Philosophy Platform-specific code lives in its own sub-directories. __POSIX__ __IOS__ __NACL__ __EMSCRIPTEN__ ... Platform-specific pre-processor defines provided by build system. class DisplayCoreBase class NaclDisplayCore class EmscDisplayCore class IOSDisplayCore #if __NACL__ #if __EMSCRIPTEN__ #if __IOS__ #endif #endif #endif class DisplayCore Diamond-shape class hierarchy resolved at compile time:
  • 14. Multiplatform Line Counts Dragons Demo (~170k lines of code) platform-agnostic: 148k POSIX/CRT: 7k OpenGL: 6.7k emscripten: 3k pNaCl: 3.5k OSX/iOS: 2.2k about ~2% platform- specific code
  • 15. Size Comparisons (Dragons Demo) ~170k lines of C++ code OSX (-arch i386 -O3): • orig: 2027 kByte • +no asserts: 1457 kByte • +stripped: 1237 kByte • +gzipped: 413 kByte OSX (-arch x86_64 -O3): • orig: 2134 kByte • +no asserts: 1663 kByte • +stripped: 1427 kByte • +gzipped: 460 kByte iOS (-arch armv7 -O3): • orig: 1542 kByte • +no asserts: 1196 kByte • +stripped: 972 kByte • +gzipped: 395 kByte pNaCl (-O2): • orig: 1654 kByte • +no asserts: 1333 kByte • +stripped: 1333 kByte • +gzipped: 842 kByte emscripten (-O2 --llvm-opts 3 --llvm-lto 3): • orig: 5414 kByte • +no asserts: 2154 kByte • +closure pass: 1951 kByte • +gzipped: 486 kByte wow, surprisingly compact! smaller than expected bigger than expected closure: Google’s JS optimizer/minifier
  • 16. The Callback Problem Key Point to understand (and accept): Browser runtime environment uses callback model for asynchronous programming. Start lengthy operation, provide callback which will be called when operation is finished: becomes very messy very quickly. Games are usually frame-driven, not callback-driven. This is the main riddle when trying to port a game engine to browser platforms.
  • 17. The Game Loop Problem Most event-driven platforms don’t let you “own the game loop”. Instead the application runs completely inside event callback functions which must return quickly. Failing to return quickly results in unresponsive behaviour or even your app being killed. pNaCl
  • 18. The Game Loop Problem Best solution is to use the app main thread exclusively for system event handling... ...and spawn a “Game Thread” which runs the actual game loop. Main Thread Game Thread input events system events quit event display change events Only wakes up on system events. Runs your typical “infinite” game loop.
  • 19. The CallOnMainThread-Problem Some platforms have restrictions what OS functionality is accessible from threads. E.g. must call OpenGL or IO functions from the main thread only. pNaCl Either run everything on main thread, or dispatch “system calls” to run asynchronously on main thread.
  • 20. CallOnMainThread problem All “PPAPI calls” must happen on main thread, and the main thread must never block. pNaCl Threads can push function pointers for deferred execution on main thread. Deferred function calls and result callbacks execute in a simple run-loop after your per-frame callback on the main thread. This primitive runloop/callback model makes it easy to shoot yourself in the foot by waiting for events triggered by your own callbacks.This stops the entire runloop and freezes the app. But: All other threads can block as much as they want, waiting for events triggered by callbacks on the main thread. Nice way to simulate blocking I/O. Conclusion: pNaCl’s full threading support can be used to workaround many of its restrictions by moving the actual game logic into its own thread, and use the main thread only for “system calls” and their result callbacks.
  • 21. CallOnMainThread visualizedpNaCl Init(): launch Game Thread StartIO(): begin async IO, set finish-callback to FinishIO() FinishIO(): set finished-condvar Main Thread put StartIO func ptr on main thread’s run queue and wait for finished-condvar new thread CallOnMainThread(StartIO) your Game Thread finished-condvar is set, continue Game Thread ...blocked... finished-condvar set your pNaCl main-thread code invoke callbacks to pNaCl app code: initialization invoke deferred funcs invoke result callbacks ... pNaCl runtime (runloop/callbacks)
  • 22. Limitations Similar restrictions as pNaCl, but can’t easily use threads to workaround them: • most “interesting functions” (WebGL!) must be called from main thread • main thread must not block • no pthreads, only WebWorkers for threading •WebWorkers have their own “address space” Can’t move entire game loop into WebWorker thread (yet?) Browser vendors working towards more flexible WebWorkers, but HTML5 standardization takes time.
  • 23. Limitation Workarounds All your code must run inside “slices”, always return within 16 or 32 ms to browser. If something takes longer, either spread work over several frames, or move into WebWorker. N3 has new “PhasedApplication” model: app goes through phases, which tick themselves forward when finished. OnInit OnPreloading OnOpening OnRunning OnClosing OnQuit OnFrame emscripten runtime environment max 16ms or 32ms (for 60 or 30 fps)
  • 24. Threading Workarounds Failed approach: Try to wrap low-level threading code in some sort of “co-operative thread scheduling” system. Success: Move abstraction to a higher level (don’t wrap “low level threads”, but wrap “parallel task system”). 2 uses for threading: hide blocking / make use of additional CPU cores. Dispatcher Worker Thread(s) request Nebula3 parallel task system model 3 Flavours: • Blocking: thread sleeps until messages arrives • Timeout: block until messages arrive, or timeout occurs • Run-through: infinite loop doing per-frame work, pull messages emscripten port adds 2 “run modes”: • Parallel: work is pushed to WebWorker threads (makes use of cpu cores) • Sliced: runs on main-thread, work is “triggered” per frame (hides callback mess) response queue
  • 25. Nebula3 IO System IO System HTTP File System App Code IO request IO response with Stream object Closer to HTTP philosophy then fopen()/fclose(): • URLs instead of file system paths • asynchronous IO is default, synchronous is special case • pluggable filesystem handlers associated with URL scheme (http://, file://, ...) • Stream objects with StreamReaders and StreamWriters Local File System http://.., file://... Stream object with file data • Filesystem modules return Stream objects holding downloaded data • Stream objects have typical Read/Seek/... methods • IO reponse is a “Future” object, app code polls whether response has become valid
  • 26. Asset Loading Easy way: emscripten can pre-load assets into memory before app starts, accessible through fopen() / fread() HTTP File System Web Server HTTP request Downside: delay on startup, memory cost - doesn’t work well for big asset sets. Solution: need to stream and uncompress all assets on demand asynchronously HTTP response Problem: HTTP downloads much slower than loading from HDD, can’t block while waiting for download to finish. Uncompress WebWorker App Code IO request IO response HTTP File System has platform-specific implementations: • emscripten: emscripten_async_wget_data() • pNaCl: pp::URLLoader • OSX/iOS: NSURLRequest • Linux / Windows: libCURL • fallback: home-made HTTP client using raw TCP sockets (tricky!)
  • 27. Preloading Phase Loading Screen On Preloading Problem: Sometimes asynchronous loading is too much hassle, or even impossible (for instance when using 3rd party libs). Solution: Have pre-loading app phases, show loading screen, download and pin files into a memory filesystem, continue to next app phase when files have finished downloading. Synchronous IO functions exclusively access data in memory filesystem, fail if file hasn’t been preloaded. Running Loading Screen Off Loading Screen On Preloading Loading Screen Off Running Memory File System fread() fread() populate populate Web Server HTTP Only use this approach when absolutely necessary and only for small files, not for textures, geometry, audio, etc...
  • 28. Debugging None of the C++ web solutions have really good interactive debugging support (yet). Develop and debug your app mainly as a native desktop app for OSX or Windows inside XCode orVStudio, this gives the best turn- around time and “debugging experience” Only fall-back to low-level debugging for platform-specific code. emscripten debugging can be surprisingly easy: • generated Javascript can be made very readable (see -g options in emcc) • can inject debugging statements without recompiling • see emscripten/src/settings.js for some interesting runtime debug options
  • 29. JS Debugging with Source Maps emcc -g4 generates source maps containing reference data to the original C++ sources. Interactively debug C++ code in the browser! (still feels very rough around the edges though)
  • 30. Too many slides, too little time... Other interesting problem areas: Audio Networking WebAudio vs Audio tag no common compressed audio format across browsers WebSockets or WebRTC much more restrictive than Berkley Sockets (security reasons) Feels like back in the 90’s, have to roll our own Audio and Networking libs AGAIN :(
  • 31. Too many slides, too little time... OpenGL have to settle on OpenGL ES 2 feature set “it just works!” ...even on mobile: Main problem is call overhead into WebGL, but it’s still surprisingly fast.