Avoid 'Get' call if possible

(*js.Value).Get has some overhead especially on Wasm.
This commit is contained in:
Hajime Hoshi 2018-06-19 03:40:37 +09:00
parent 702a429afb
commit 62bd35e412
2 changed files with 25 additions and 22 deletions

View File

@ -307,6 +307,10 @@ func (c *Context) UniformFloat(p Program, location string, v float32) {
gl.Call("uniform1f", js.Value(l), v) gl.Call("uniform1f", js.Value(l), v)
} }
var (
float32Array = js.Global.Get("Float32Array")
)
func (c *Context) UniformFloats(p Program, location string, v []float32) { func (c *Context) UniformFloats(p Program, location string, v []float32) {
gl := c.gl gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location) l := c.locationCache.GetUniformLocation(c, p, location)
@ -317,7 +321,7 @@ func (c *Context) UniformFloats(p Program, location string, v []float32) {
gl.Call("uniform4f", js.Value(l), v[0], v[1], v[2], v[3]) gl.Call("uniform4f", js.Value(l), v[0], v[1], v[2], v[3])
case 16: case 16:
u8 := js.ValueOf(v) u8 := js.ValueOf(v)
m := js.Global.Get("Float32Array").New(u8.Get("buffer"), u8.Get("byteOffset").Int(), 16) m := float32Array.New(u8.Get("buffer"), u8.Get("byteOffset").Int(), 16)
gl.Call("uniformMatrix4fv", js.Value(l), false, m) gl.Call("uniformMatrix4fv", js.Value(l), false, m)
default: default:
panic("not reached") panic("not reached")

View File

@ -49,9 +49,13 @@ var currentUI = &userInterface{
pageVisible: true, pageVisible: true,
} }
var (
window = js.Global.Get("window")
document = js.Global.Get("document")
)
func MonitorSize() (int, int) { func MonitorSize() (int, int) {
w := js.Global.Get("window") return window.Get("innerWidth").Int(), window.Get("innerHeight").Int()
return w.Get("innerWidth").Int(), w.Get("innerHeight").Int()
} }
func SetScreenSize(width, height int) bool { func SetScreenSize(width, height int) bool {
@ -122,8 +126,7 @@ func SetCursorVisible(visible bool) {
} }
func SetWindowTitle(title string) { func SetWindowTitle(title string) {
doc := js.Global.Get("document") document.Set("title", title)
doc.Set("title", title)
} }
func SetWindowIcon(iconImages []image.Image) { func SetWindowIcon(iconImages []image.Image) {
@ -142,8 +145,7 @@ func (u *userInterface) getScale() float64 {
if !u.fullscreen { if !u.fullscreen {
return u.scale return u.scale
} }
doc := js.Global.Get("document") body := document.Get("body")
body := doc.Get("body")
bw := body.Get("clientWidth").Float() bw := body.Get("clientWidth").Float()
bh := body.Get("clientHeight").Float() bh := body.Get("clientHeight").Float()
sw := bw / float64(u.width) sw := bw / float64(u.width)
@ -213,7 +215,7 @@ func (u *userInterface) loop(g GraphicsContext) error {
close(ch) close(ch)
return return
} }
js.Global.Get("window").Call("requestAnimationFrame", cf) window.Call("requestAnimationFrame", cf)
} }
cf = js.NewCallback(f) cf = js.NewCallback(f)
// Call f asyncly to be async since ch is used in f. // Call f asyncly to be async since ch is used in f.
@ -228,9 +230,7 @@ func init() {
if web.IsNodeJS() { if web.IsNodeJS() {
return return
} }
doc := js.Global.Get("document") if document.Get("body") == js.Null {
window := js.Global.Get("window")
if doc.Get("body") == js.Null {
ch := make(chan struct{}) ch := make(chan struct{})
window.Call("addEventListener", "load", js.NewCallback(func([]js.Value) { window.Call("addEventListener", "load", js.NewCallback(func([]js.Value) {
close(ch) close(ch)
@ -254,8 +254,8 @@ func init() {
hooks.ResumeAudio() hooks.ResumeAudio()
} }
})) }))
doc.Call("addEventListener", "visibilitychange", js.NewCallback(func([]js.Value) { document.Call("addEventListener", "visibilitychange", js.NewCallback(func([]js.Value) {
currentUI.pageVisible = !doc.Get("hidden").Bool() currentUI.pageVisible = !document.Get("hidden").Bool()
if currentUI.suspended() { if currentUI.suspended() {
hooks.SuspendAudio() hooks.SuspendAudio()
} else { } else {
@ -268,22 +268,22 @@ func init() {
// Adjust the initial scale to 1. // Adjust the initial scale to 1.
// https://developer.mozilla.org/en/docs/Mozilla/Mobile/Viewport_meta_tag // https://developer.mozilla.org/en/docs/Mozilla/Mobile/Viewport_meta_tag
meta := doc.Call("createElement", "meta") meta := document.Call("createElement", "meta")
meta.Set("name", "viewport") meta.Set("name", "viewport")
meta.Set("content", "width=device-width, initial-scale=1") meta.Set("content", "width=device-width, initial-scale=1")
doc.Get("head").Call("appendChild", meta) document.Get("head").Call("appendChild", meta)
canvas = doc.Call("createElement", "canvas") canvas = document.Call("createElement", "canvas")
canvas.Set("width", 16) canvas.Set("width", 16)
canvas.Set("height", 16) canvas.Set("height", 16)
doc.Get("body").Call("appendChild", canvas) document.Get("body").Call("appendChild", canvas)
htmlStyle := doc.Get("documentElement").Get("style") htmlStyle := document.Get("documentElement").Get("style")
htmlStyle.Set("height", "100%") htmlStyle.Set("height", "100%")
htmlStyle.Set("margin", "0") htmlStyle.Set("margin", "0")
htmlStyle.Set("padding", "0") htmlStyle.Set("padding", "0")
bodyStyle := doc.Get("body").Get("style") bodyStyle := document.Get("body").Get("style")
bodyStyle.Set("backgroundColor", "#000") bodyStyle.Set("backgroundColor", "#000")
bodyStyle.Set("position", "relative") bodyStyle.Set("position", "relative")
bodyStyle.Set("height", "100%") bodyStyle.Set("height", "100%")
@ -291,7 +291,7 @@ func init() {
bodyStyle.Set("padding", "0") bodyStyle.Set("padding", "0")
// TODO: This is OK as long as the game is in an independent iframe. // TODO: This is OK as long as the game is in an independent iframe.
// What if the canvas is embedded in a HTML directly? // What if the canvas is embedded in a HTML directly?
doc.Get("body").Call("addEventListener", "click", js.NewCallback(func([]js.Value) { document.Get("body").Call("addEventListener", "click", js.NewCallback(func([]js.Value) {
canvas.Call("focus") canvas.Call("focus")
})) }))
@ -340,8 +340,7 @@ func RunMainThreadLoop(ch <-chan error) error {
func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool) error { func Run(width, height int, scale float64, title string, g GraphicsContext, mainloop bool) error {
u := currentUI u := currentUI
doc := js.Global.Get("document") document.Set("title", title)
doc.Set("title", title)
u.setScreenSize(width, height, scale, u.fullscreen) u.setScreenSize(width, height, scale, u.fullscreen)
canvas.Call("focus") canvas.Call("focus")
if err := opengl.Init(); err != nil { if err := opengl.Init(); err != nil {