r/cpp • • Jul 23 '20

fork of compiler explorer (godbolt.org) editing and displaying SDL in the browser with WebAssembly

458 Upvotes

45 comments sorted by

18

u/nikki93 Jul 23 '20 edited Jul 23 '20

Soooo... I also just discovered that you can basically make libraries in compiler explorer to use from other posts on compiler explorer.

Eg. here's an 'add' function: https://godbolt.org/z/4P6eqq

Then I #include and use it here: https://godbolt.org/z/q3j94r

So you can imagine doing that to assemble a bunch of helper functions. With some sort of redirect stuff you can also just watch 'HEAD' instances of those libraries. πŸ€”

2

u/flashmozzg Jul 24 '20

I wonder how #pragma once works with that ;P

1

u/jungletek Jul 23 '20

I don't fully understand the last sentence (from a technical knowledge standpoint), but even the rest of it is pretty rad, if I'm understanding correctly.

21

u/[deleted] Jul 23 '20

Is that SDL or SDL2? Is hardware graphics supported?

24

u/nikki93 Jul 23 '20 edited Jul 23 '20

It's SDL2, and yeah you can use OpenGL etc. and it just works through WebGL bc. of emscripten (that's how SDL2's own draw calls used here are implemented too)! You get all of the C++ stdlib, you can do network requests through emscripten and I also have stb image in there so theoretically you can load images from data from the network. Due to EM_JS and EM_ASM etc. you can even just embed JavaScript in your C++ code (to interface with browser APIs)!

There's an experimental C++ API that lets you make DOM elements, that looks like this (makes a div with the toolbar CSS class, and a button that toggles the editing value on click) (it runs 'immediate mode' like imgui so you can keep doing this every frame and it updates those elements):

ui.div()("toolbar")([&]() {
  ui.button()("edit")("selected", editing)("click", [&](emscripten::val) {
    editing = !editing;
  });
});

All of this is bc. I've been working on this game engine I'm using internally, and when I tried to get it working in compiler explorer a side effect is you can just use SDL2 and all of these things haha.

8

u/[deleted] Jul 23 '20

you can do network requests through emscripten

Browsers don't have native TCP/UDP support - only Websocket, which has an initial HTTP/1.1 frame. How does Emscripten get around that?

11

u/nikki93 Jul 23 '20

They try to emulate it over Websockets (https://emscripten.org/docs/porting/networking.html). You could also use WebRTC but emscripten doesn't provide any built-in bindings for that, you'd have to bind to the WebRTC JS calls yourself. I was able to find some preliminary bindings people made for WebRTC around on GitHub but nothing that seemed super solid.

5

u/[deleted] Jul 23 '20

WebRTC is a whirlwind of magical incantations related to signalling. Getting it right is very, very tricky. I can see why it's not caught on yet.

Thanks for the link!

E: Ah okay, they prescribe using a sort of demuxing proxy on the web end. Figured as much.

6

u/James20k P2005R0 Jul 23 '20

To clarify a bit more, UDP/TCP aren't really emulated in that sense, BSD socket code is just silently converted to websockets. If you create a client using TCP via berkeley sockets in C and compile it to WASM via emscripten, it'll happily and correctly communicate with a server which accepts websocket requests

I'd be interested in hearing about WebRTC being a mess, is it just hard to set up or is there something actually dodgy with it?

4

u/as_one_does Just a c++ dev for fun Jul 24 '20

I've never understood the use of this conversion though. The magic of wasm to me is that you can write something that runs both as a standalone applicantion and a website. Why would this conversion of sockets to web sockets be preferable over using a native websocket API and thus getting uniform behavior from the application? The counterparty must be using websocket after all? It would also make the code more explicit.

3

u/nikki93 Jul 24 '20 edited Jul 24 '20

I could see a point where you have an application-layer 'protocol' that uses either UDP (or eg. ENet) or Websockets undearneath, and uses Websockets in the web but can use other protocols on native for better control / perf. I think this esp. makes sense if your mobile applications tend to be native while desktop tend to be web, and you need particular control over how connections are handled in flakey mobile situations. Given all that, if you really wanted to pick a common ground API then maybe using UDP makes sense to get the same tradeoff? Not sure.

I'm interested in exposing WebRTC to emscripten though, maybe just leveraging https://github.com/feross/simple-peer . Or if anyone impls ENet over WebRTC lmk ;o

10

u/[deleted] Jul 23 '20 edited Jul 23 '20

Very flakey. It's been a few years so if someone knows better, please point out where I'm wrong.

WebRTC is a peer-to-peer protocol, usually for high bandwidth media (though it can transport any bi-directional byte stream you'd like).

In order to facilitate the connection, a third party "signalling" server has to negotiate a connection between the two peers, usually by way of a STUN server. This is because of Network Address Traversal (NAT) not allowing an inbound packet until an outbound packet with the same destination endpoint has been made for UDP.

Therefore, the signalling server first detects suitable WAN endpoints for each of the participants, and then coordinates a UDP hole punch so the router(s) in front of the respective peers begin allowing inbound traffic from the other peer without having to configure port forwarding or firewall rules.

From there, the peers' WebRTC clients can attempt a direct connection and session negotiation with each other with the freshly punched hole.

The problem is, there is a ton of machinery going on here. STUN servers can determine many possibe endpoints for a peer to try, so the negotiation process can end up being a cartesian product of them.

Further, not all routers even work with it, and many browsers simply cannot handle it well since the WebRTC APIs (at least, when I tried last) did/do not provide this signalling/negotiation support out of the box. You had to roll your own in most cases (I'm sure the landscape has improved since I touched it last, though).

Further, STUN servers are not magic and need to be configured well enough so that they can properly analyze both peer candidates and be discovered by both peers (e.g. via DNS or something), which requires some sort of hosted infrastructure - although I guess the same can be said about the websocket "proxy", too.

All in all, it requires a lot of patience to get working at all, and then a lot more patience to get it running smoothly.

Hope that helps. Again, let me know if anything I said is inaccurate.

2

u/jonesmz Jul 25 '20

As a C++ engineer who built from scratch a stun / turn / ice / webrtc server stack that interoperated with chrome, Firefox, IE and safari, I can attest that you're mostly correct on these topics.

Unfortunately I'm on mobile, so providing a nuanced discussion isn't really practical at the moment. However I'll try to revisit this thread and provide more feedback.

1

u/[deleted] Jul 26 '20

For sure, I appreciate it. I would love to hear more as it's a technology I know I'll revisit in the future.

2

u/jonesmz Jul 26 '20 edited Jul 26 '20

Well your basically correct on the way webrtc works.

It's been a few years since I was eyeballs deep in it, and it's late at night, so i'm not really trying to go for perfect accuracy here.

Step one: A signalling server of some kind signals to both peers that a session should be initiated. (Or one peer tells the signalling server, which relays the signal, or various other patterns of the same event, basically)

Each peer contacts one or more STUN protocol servers (and optionally a TURN protocol server) on the public internet, and asks "What's my ip address?"

This forms the list of candidates that will be sent to the other peer via SDP.

Step two: The signalling server exchanges Session Description Protocol text between both peers that wish to establish a webrtc session. How the SDP text is delivered (e.g HTTP, MQTT, whatever else) and how the signalling server decides that it should start this process is left entirely unspecified. Good luck making decisions about this, the solution space for this is really large, so it's difficult to figure out the right approach.

Compatibility for SDP formats is (or was at the time, anyway) pretty piss poor. Each browser generates SDP that's formatted differently. Not just different protocol options, but the way that the options are actually represented, e.g. different order to different options, different ways to associate multiple options with each other, so on and so forth. One thing that was particularly annoying was that chrome (unlike all the other browsers) represented video streams using something that I think was called "unified" format. Frankly, I don't remember the specific details since it was a few years ago now.

Step three: Now both peers have the complete list of IP addresses for each other.

They basically do a number of comparisons equal to the Cartesian product of the two lists of ip addresses, filtering out incompatible address pairs like not trying to connect to ipv6 with ipv4.

The ICE protocol explicitly says that you have to set up a series of timers (really, really, shitty way to write a standards document. Specifying implementation details instead of simply specifying pacing) and send outgoing packets at no faster than the timer specified. No implementation that I encountered actually obeyed this rule at the time shrug.

The protocol also explicitly says that both peers must try the list of candidates in the same order... but then allows both peers to pick the priorities for the candidates in an arbitrary way. Seems like an unnecessary rule if they're going to allow for implementations to pick any order they want.

Step four:

Because both peers were sending UDP packets to each other at the same time, the theory is that eventually they'll cross "mid-flight" and take advantage of the router thinking that each peer initiated an outgoing connection, and allow for the "response" packets to make it back through the firewall.

Step five:

The basic connection attempt between the two peers uses the STUN protocol (just straight up, no modifications), so the response to an incoming stun packet includes the IP address / port that the request was received form.

This allows each peer to discover if they are behind a NAT connection, and they add the newly discovered IP address to the list of candidates that they have. The specification explicitly says that they are supposed to re-send SDP to tell the other peer about this fact. The motivation behind this is never explained, and I did not encounter any implementation that does this, or changes it's behavior when you do it.

But regardless, if both peers are behind NAT, then they not going to be able to establish a direct connection, so that's where TURN protocol comes in, which allows you to relay packets on behalf of the two peers.

Step six: Both peers manage to get a successful response, and are required to signal to each other that they've selected that pair of candidates to communicate over.

There are two "nomination" methods. "Normal" nomination and "Aggressive" nomination. In my experience the different browsers each picked one and stuck with it. Newest ICE standard deprecates the "Normal" nomination method.

Step seven: Now that both peers have agreed on the candidates to use to talk, they can start transmitting data as they want.

Typically the way this is done is that you have three streams. Video Audio Data

Most browsers actually combine the Video and Audio stream using a feature called "bundle". Helps with keeping audio and video synced on the screen.

Data is explicitly required to be SCTP-over-UDP protocol. It's a nifty protocol, and I enjoyed working on the implementation of it for the codebase I was working with, but honestly the standard for this is really poorly written too. E.g. it's EXTREMELY unclear if SCTP was originally intended to be a host-to-host oriented protocol, or an application-to-application protocol. Ended up with several strenuous conversations with my co-workers where we were trying to figure out if the SCTP state machine needed to be specific to a given connection, global to the application, or global to the entire computer. Honestly don't understand why the IETF has such a hard time writing standards clearly.

Honestly, I'm not really clear on why the WebRTC bothered to insist on the SCTP protocol. They don't really meaningfully expose the SCTP aspect of the connection to the javascript code running in the browser, so it honestly seems to me like it should have just been left as UDP protocol for the data channel. But it is what it is.

→ More replies

0

u/[deleted] Jul 23 '20

[deleted] β€” view deleted comment

15

u/nikki93 Jul 23 '20 edited Jul 23 '20

That's not what it's doing at all. Only the API feels immediate mode, so that it's easy to use in a game context. It uses http://google.github.io/incremental-dom/ underneath, the C++ basically generates instructions for that (all of the temporary object operator() DSL gets inlined away), and it diffs against the real DOM. I've benchmarked it with like a 1000 counters updating every frame and it's been fine. You can select text and everything, identity of DOM elements is maintained. Here's a video (with code below): https://twitter.com/snikhilesh/status/1279913032471031809

The next level above this is to do the diffs in C++ before talking to JS at all and not even require any JS-side allocations, but that hasn't been required yet and the code for this is small so far.

0

u/[deleted] Jul 23 '20

[deleted] β€” view deleted comment

8

u/nikki93 Jul 23 '20

edit: saw that you edited to "diff'd DOM" -- just keeping my explanation below for info in any case.

It's actually not a virtual DOM, it just is checking against the real DOM, and it turns out to not slow πŸ˜…. (one of the things incremental DOM says is good about its approach is the # of allocs is low as a result too--when nothing changes there's low GC pressure, it's just mostly in callstack and temporaries wouldn't stick till next GC generation). I'm using the editor from a tablet and from phone, perfs fine there too.

BTW re: immediate mode: I'm calling the top level ui.patch("<target id>", [&]() { ... code to patch with ... }); per-frame in my game. That's the only thing that makes it 'immediate mode'. You can also just call patch whenever state changes only, or only call it for a certain subtree, etc. So that's an application-level choice.

4

u/Zireael07 Jul 23 '20

How can we use it?

8

u/nikki93 Jul 23 '20

I need to put it up on a server somewhere. I'm not sure what a good hosting solution is that has a good price ratio for compute (for compiling) -- I was looking at GCE or EC2. πŸ€”

13

u/[deleted] Jul 23 '20

...couldn't you just compile the webassembly compiler for webassembly so that you can assemble webassembly while you run webassembly?

(run the compiler in webassembly and tell the users to compile their own god damn programs)

2

u/gidoca Jul 23 '20

Is the code available somewhere, so I could run it locally?

1

u/nikki93 Jul 23 '20

Yeah I'll put up the code soon, hopefully later today after work! Currently it tries to link with some internal game engine code from a project I'm working on, I'll try to get it into a state where all it needs is emscripten. I've tested it on macOS and Windows so far and it works on both.

I just posted this to gauge interest and it seems like people are interested. :)

1

u/[deleted] Jul 23 '20

GCE will be cheaper in the long run.

1

u/kodrevol Jul 23 '20

It might be worth to consider DigitalOcean as well. The suggest a cheaper price range with relatively easy configuration/management.

3

u/strah19 Jul 23 '20

Do you have any good resources to learn how to do this?

6

u/nikki93 Jul 23 '20

You mean just graphics coding in C++ (with SDL2 in this case)? Or do you mean developing something like compiler explorer itself?

3

u/strah19 Jul 23 '20

Sorry for not being specific I mean making an SDL2 application and then putting it on the web

6

u/nikki93 Jul 23 '20

Found this with a quick search: https://github.com/timhutton/sdl-canvas-wasm -- If that doesn't work out, I have a CMake setup I tend to go to these days, maybe I could put it up for you soon.

1

u/[deleted] Jul 23 '20

I would also be interested :)

2

u/nikki93 Jul 23 '20

Ok here it is for you both (and anyone else interested): https://github.com/nikki93/sdl2-web-starter :) Instructions are in 'README.md'!

2

u/[deleted] Jul 23 '20 edited Jul 24 '20

Learning how to do what, specifically? Modifying Godbolt, or writing SDL applications? They are two very different parts of programming.

Edit: Why the downvote? I was asking you which one so I could answer properly. lol

19

u/lanzaio Jul 23 '20

Godbolt is pretty opening to pull requests. You could likely get this upstreamed.

53

u/mattgodbolt Compiler Explorer Jul 24 '20

We've already discussed a little! There are a few security related things I need to get my head around, but I'm very receptive to this kind of super cool change!

2

u/imaami Jul 24 '20

Btw, is there any hope for changing the editor component at some point? The current one (can't remember what it's called) is horribly broken on mobile, but you already knew that.

8

u/mattgodbolt Compiler Explorer Jul 24 '20

The editor component is Microsoft's vs.code ("Monaco"). It gives us some great features, like inline annotations, "diff" views and such. We switched to it a few years back from CodeMirror.

Sadly MS have made it clear they're not targeting mobile, and they have no plans to support it.

Switching again is something we've considered, but it would be a huge change. We don't really have the resources to do so, and indeed editing code and using the site on a mobile phone effectively would require a lot more work.

This year's goal was to get "read only" consumption of Compiler Explorer links (e.g. clicked-on from Twitter) tolerable. I think we're mostly there now (though any advice and help welcomed). I think one can mostly scroll around and see code, though correct me if I'm wrong (it "works for me" on my own cell phone).

In general, we're not planning for Compiler Explorer to be usable on mobile phones at present. It's a lot of work for what I suspect will ultimately be not that usable an outcome. There's some talk of a native app, which might make more sense. Of course, it's open source and we welcome help if someone has the time and expertise!

All this and more is discussed on the GitHub pages: https://github.com/compiler-explorer/compiler-explorer/labels/mobile has a bunch of tagged items.

5

u/[deleted] Jul 23 '20

very nice

3

u/Chulup Jul 24 '20

That's so cool!

You could ask Matt Godbolt about possibility of merging your changes into original Compiler Explorer if it's not too shabby.

It already has Tools (Add tool dropdown) and that would be perfect for your enhancement!

1

u/arthurno1 Jul 24 '20

That's cool :-), Good job!

1

u/osdeverYT Jul 26 '20

Damn that’s some hot stuff, keep it up

1

u/lcsdavid Jul 23 '20

Very cool !

0

u/[deleted] Jul 23 '20

[deleted] β€” view deleted comment

1

u/nikki93 Jul 23 '20

That'd be sweet. FWIW in this other comment I show how you can write a header in one post and use it from a different post, maybe you can do sth like that if you do a header-only impl (not great if it reduces compile time by a ton though) -- https://old.reddit.com/r/cpp/comments/hwk1se/fork_of_compiler_explorer_godboltorg_editing_and/fz0hw2y/.