engo icon indicating copy to clipboard operation
engo copied to clipboard

Web Assembly

Open Noofbiz opened this issue 7 years ago • 15 comments

Web Assembly as a target is coming to Go so we could target that https://docs.google.com/document/d/131vjr4DH6JFnb-blm_uRdaC0_Nv3OUwjEY5qVCxCup4/mobilebasic#

Noofbiz avatar Mar 02 '18 19:03 Noofbiz

changing to milestone v1.1 since it's now out in go 1.11 https://blog.golang.org/go1.11

Noofbiz avatar Aug 28 '18 01:08 Noofbiz

We're now using gopherwasm instead of gopherjs for the web builds, so compiling to wasm should be as simple as using wasm as the target goos now. I'm going to try it out and see how that goes and fix any thing that comes up ^_^

Noofbiz avatar Nov 04 '18 07:11 Noofbiz

@Noofbiz any update on this?

jonbonazza avatar Dec 29 '18 22:12 jonbonazza

Yeah, we're now using the gopherwasm library as a junction between GopherJS and syscall/js. When syscall/js is stable (right now it's still experimental) I'll probably update the readme to use that instead of gopherjs and close this.

Noofbiz avatar Dec 29 '18 23:12 Noofbiz

There seems to be something wrong with the Web Assembly target. Here's what I'm getting:

$ GOOS=js GOARCH=wasm go build
# github.com/engoengine/math
/Users/bfreis/go/pkg/mod/github.com/engoengine/[email protected]/sin.go:65:6: missing function body
/Users/bfreis/go/pkg/mod/github.com/engoengine/[email protected]/sin.go:122:6: missing function body
/Users/bfreis/go/pkg/mod/github.com/engoengine/[email protected]/sqrt.go:92:6: missing function body

The code is nothing more than a "Hello World":

package main

import (
	"fmt"
	"engo.io/engo"
)

type myScene struct{}
func (*myScene) Preload() {}
func (*myScene) Setup(engo.Updater) {}
func (*myScene) Type() string { return "myscene" }
func main() {
	engo.Run(engo.RunOptions{
		Title:  "Hello World",
		Width:  400,
		Height: 400,
	}, &myScene{})
	fmt.Println("ha")
}

bfreis avatar Jan 11 '19 11:01 bfreis

Yeah looks like it has to do with the netgo build tag which we shouldn't be using anyway https://github.com/gopherjs/gopherjs/issues/143

Noofbiz avatar Jan 11 '19 17:01 Noofbiz

Actually had to fix it so the assembly files in EngoEngine/math didn't build on js. But we're also now using the correct build tags and building wasm as a target is working!

Noofbiz avatar Jan 11 '19 21:01 Noofbiz

Fabulous, it is indeed working now with HEAD!

A question about the Wasm build - is there currently a way to control which <canvas/> element Engo uses to display content? This would be great to integrate Engo with something like Vecty!

Thanks Bruno

bfreis avatar Jan 11 '19 23:01 bfreis

Hmmm, actually there seems to be a problem. I quickly assumed it was working as I noticed the WASM file had built. But when I run, this happens:

goroutine 1 [running]:
wasm_exec.js:45:6
syscall/js.Value.float(0x7ff8000000000001, 0x615a8, 0x9, 0x7ff8000000000001)
wasm_exec.js:45:6
	/usr/local/Cellar/go/1.11.4/libexec/src/syscall/js/js.go:326 +0x35
wasm_exec.js:45:6
syscall/js.Value.Int(0x7ff8000000000001, 0x62495)
wasm_exec.js:45:6
	/usr/local/Cellar/go/1.11.4/libexec/src/syscall/js/js.go:338 +0x2
wasm_exec.js:45:6
engo.io/gl.(*Context).InitialContextValues(0xc078000)
wasm_exec.js:45:6
	/Users/bfreis/go/pkg/mod/engo.io/[email protected]/gl_webgl.go:601 +0x1a0
wasm_exec.js:45:6
engo.io/gl.NewContext(0x7ff800000000001e, 0x0, 0x5, 0x269e0, 0xc00e460)
wasm_exec.js:45:6
	/Users/bfreis/go/pkg/mod/engo.io/[email protected]/gl_webgl.go:365 +0x13
wasm_exec.js:45:6
engo.io/engo.CreateWindow(0x62413, 0xd, 0x190, 0x190, 0x0, 0x1)
wasm_exec.js:45:6
	/Users/bfreis/go/pkg/mod/engo.io/[email protected]/engo_js.go:58 +0x18
wasm_exec.js:45:6
engo.io/engo.Run(0x0, 0x62413, 0xd, 0x0, 0x190, 0x190, 0x3f8000003f800000, 0x0, 0x3c, 0x0, ...)
wasm_exec.js:45:6
	/Users/bfreis/go/pkg/mod/engo.io/[email protected]/engo.go:225 +0x36
wasm_exec.js:45:6
main.main()
wasm_exec.js:45:6
	/Users/bfreis/dev/ats/go/platform/frontend/main.go:43 +0x3

The line in gl_webgl.go that is failing is this:

	c.STENCIL_INDEX = webCtx.Get("STENCIL_INDEX").Int()

Seems like STENCIL_INDEX is somehow undefined? I tried running this on both Chrome and Firefox.

bfreis avatar Jan 11 '19 23:01 bfreis

Looks like that's been removed from webgl here https://github.com/KhronosGroup/WebGL/issues/2370 I'll update to reflect that. I guess it's been fixed in gopherjs so that's why it works there okay

Noofbiz avatar Jan 12 '19 00:01 Noofbiz

Weird, I tried on GopherJS and it doesn't seem to work either. It builds the JS file, but when I try to run it I get the same error. I'm using @myitcv 's fork of GopherJS (https://github.com/myitcv/gopherjs) as I need Go Modules support to build. Here's the error:

Uncaught Error: syscall/js: call of Value.Int on undefined
    at $callDeferred (frontend.js:1567)
    at $panic (frontend.js:1606)
    at Object.$packages.github.com/gopherjs/gopherwasm/js.Value.ptr.Int (frontend.js:14105)
    at Object.$packages.engo.io/gl.Context.ptr.InitialContextValues (frontend.js:29066)
    at Object.NewContext (frontend.js:28826)
    at CreateWindow (frontend.js:32226)
    at Object.Run (frontend.js:32114)
    at Object.Run (frontend.js:65051)
    at main (frontend.js:65079)
    at $init (frontend.js:65093)
    at $goroutine (frontend.js:1636)
    at $runScheduled (frontend.js:1679)
    at $schedule (frontend.js:1695)
    at $go (frontend.js:1671)
    at frontend.js:65104
    at frontend.js:65107
$callDeferred @ frontend.js:1567
$panic @ frontend.js:1606
$packages.github.com/gopherjs/gopherwasm/js.Value.ptr.Int @ frontend.js:14105
$packages.engo.io/gl.Context.ptr.InitialContextValues @ frontend.js:29066
NewContext @ frontend.js:28826
CreateWindow @ frontend.js:32226
Run @ frontend.js:32114
Run @ frontend.js:65051
main @ frontend.js:65079
$init @ frontend.js:65093
$goroutine @ frontend.js:1636
$runScheduled @ frontend.js:1679
$schedule @ frontend.js:1695
$go @ frontend.js:1671
(anonymous) @ frontend.js:65104
(anonymous) @ frontend.js:65107

bfreis avatar Jan 22 '19 01:01 bfreis

From a quick glance at that stack trace you're getting a mention of syscall/js from something compiled with GopherJS, correct?

If so that's likely your problem. GopherJS does not know/understand syscall/js.

All JavaScript/WASM "interop" files need to be guarded with build tags: js,!wasm for GopherJS, and js,wasm for WASM.

myitcv avatar Jan 22 '19 05:01 myitcv

What I was trying to run was really just a main function calling engo.Run to create an empty window, and no other dependencies. The code I was testing didn't have anything specific about either WASM or JS. I simply compiled it with "gopherjs build", it built, and the final result was showing that error at launch.

Anyways, what I really want is to use just WASM (and no GopherJS), so I guess I'll wait to see if it starts working once that STENCIL_INDEX thing is resolved.

Thanks!

bfreis avatar Jan 22 '19 05:01 bfreis

I've fixed the gl package so STENCIL_INDEX is removed. I was able to build and run games using both Gopherjs and wasmgo. Hope it works out for you! ^_^

Noofbiz avatar Jan 23 '19 21:01 Noofbiz

It's working for me, too! Thanks.

bfreis avatar Jan 24 '19 03:01 bfreis