Compare commits

..

No commits in common. "99ffe09b63e0d906cc1f502c24f4d2325e6cc09d" and "6940435c0322a93d3ebaee2583509e123ad496d3" have entirely different histories.

5 changed files with 70 additions and 56 deletions

View File

@ -17,6 +17,8 @@ package gl
import (
"fmt"
"syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/jsutil"
)
type defaultContext struct {
@ -286,7 +288,7 @@ func (c *defaultContext) BufferInit(target uint32, size int, usage uint32) {
func (c *defaultContext) BufferSubData(target uint32, offset int, data []byte) {
l := len(data)
arr := tmpUint8ArrayFromUint8Slice(l, data)
arr := jsutil.TemporaryUint8ArrayFromUint8Slice(l, data)
c.fnBufferSubData.Invoke(target, offset, arr, 0, l)
}
@ -492,7 +494,7 @@ func (c *defaultContext) ReadPixels(dst []byte, x int32, y int32, width int32, h
c.fnReadPixels.Invoke(x, y, width, height, format, xtype, 0)
return
}
p := tmpUint8ArrayFromUint8Slice(len(dst), nil)
p := jsutil.TemporaryUint8ArrayFromUint8Slice(len(dst), nil)
c.fnReadPixels.Invoke(x, y, width, height, format, xtype, p)
js.CopyBytesToGo(dst, p)
}
@ -529,7 +531,7 @@ func (c *defaultContext) TexParameteri(target uint32, pname uint32, param int32)
}
func (c *defaultContext) TexSubImage2D(target uint32, level int32, xoffset int32, yoffset int32, width int32, height int32, format uint32, xtype uint32, pixels []byte) {
arr := tmpUint8ArrayFromUint8Slice(len(pixels), pixels)
arr := jsutil.TemporaryUint8ArrayFromUint8Slice(len(pixels), pixels)
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
// GLsizei width, GLsizei height,
// GLenum format, GLenum type, ArrayBufferView pixels, srcOffset);
@ -538,7 +540,7 @@ func (c *defaultContext) TexSubImage2D(target uint32, level int32, xoffset int32
func (c *defaultContext) Uniform1fv(location int32, value []float32) {
l := c.getUniformLocation(location)
arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
arr := jsutil.TemporaryFloat32Array(len(value), value)
c.fnUniform1fv.Invoke(l, arr, 0, len(value))
}
@ -549,61 +551,61 @@ func (c *defaultContext) Uniform1i(location int32, v0 int32) {
func (c *defaultContext) Uniform1iv(location int32, value []int32) {
l := c.getUniformLocation(location)
arr := tmpInt32ArrayFromInt32Slice(len(value), value)
arr := jsutil.TemporaryInt32Array(len(value), value)
c.fnUniform1iv.Invoke(l, arr, 0, len(value))
}
func (c *defaultContext) Uniform2fv(location int32, value []float32) {
l := c.getUniformLocation(location)
arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
arr := jsutil.TemporaryFloat32Array(len(value), value)
c.fnUniform2fv.Invoke(l, arr, 0, len(value))
}
func (c *defaultContext) Uniform2iv(location int32, value []int32) {
l := c.getUniformLocation(location)
arr := tmpInt32ArrayFromInt32Slice(len(value), value)
arr := jsutil.TemporaryInt32Array(len(value), value)
c.fnUniform2iv.Invoke(l, arr, 0, len(value))
}
func (c *defaultContext) Uniform3fv(location int32, value []float32) {
l := c.getUniformLocation(location)
arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
arr := jsutil.TemporaryFloat32Array(len(value), value)
c.fnUniform3fv.Invoke(l, arr, 0, len(value))
}
func (c *defaultContext) Uniform3iv(location int32, value []int32) {
l := c.getUniformLocation(location)
arr := tmpInt32ArrayFromInt32Slice(len(value), value)
arr := jsutil.TemporaryInt32Array(len(value), value)
c.fnUniform3iv.Invoke(l, arr, 0, len(value))
}
func (c *defaultContext) Uniform4fv(location int32, value []float32) {
l := c.getUniformLocation(location)
arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
arr := jsutil.TemporaryFloat32Array(len(value), value)
c.fnUniform4fv.Invoke(l, arr, 0, len(value))
}
func (c *defaultContext) Uniform4iv(location int32, value []int32) {
l := c.getUniformLocation(location)
arr := tmpInt32ArrayFromInt32Slice(len(value), value)
arr := jsutil.TemporaryInt32Array(len(value), value)
c.fnUniform4iv.Invoke(l, arr, 0, len(value))
}
func (c *defaultContext) UniformMatrix2fv(location int32, value []float32) {
l := c.getUniformLocation(location)
arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
arr := jsutil.TemporaryFloat32Array(len(value), value)
c.fnUniformMatrix2fv.Invoke(l, false, arr, 0, len(value))
}
func (c *defaultContext) UniformMatrix3fv(location int32, value []float32) {
l := c.getUniformLocation(location)
arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
arr := jsutil.TemporaryFloat32Array(len(value), value)
c.fnUniformMatrix3fv.Invoke(l, false, arr, 0, len(value))
}
func (c *defaultContext) UniformMatrix4fv(location int32, value []float32) {
l := c.getUniformLocation(location)
arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
arr := jsutil.TemporaryFloat32Array(len(value), value)
c.fnUniformMatrix4fv.Invoke(l, false, arr, 0, len(value))
}

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package gl
package jsutil
import (
"syscall/js"
@ -27,77 +27,77 @@ var (
)
var (
tmpArrayBufferByteLength = 16
temporaryArrayBufferByteLength = 16
// tmpArrayBuffer is a temporary buffer used at gl.readPixels or gl.texSubImage2D.
// temporaryArrayBuffer is a temporary buffer used at gl.readPixels or gl.texSubImage2D.
// The read data is converted to Go's byte slice as soon as possible.
// To avoid often allocating ArrayBuffer, reuse the buffer whenever possible.
tmpArrayBuffer = arrayBuffer.New(tmpArrayBufferByteLength)
temporaryArrayBuffer = arrayBuffer.New(temporaryArrayBufferByteLength)
// tmpUint8Array is a Uint8ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
tmpUint8Array = uint8Array.New(tmpArrayBuffer)
// temporaryUint8Array is a Uint8ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
temporaryUint8Array = uint8Array.New(temporaryArrayBuffer)
// tmpFloat32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
tmpFloat32Array = float32Array.New(tmpArrayBuffer)
// temporaryFloat32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
temporaryFloat32Array = float32Array.New(temporaryArrayBuffer)
// tmpInt32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
tmpInt32Array = int32Array.New(tmpArrayBuffer)
// temporaryInt32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
temporaryInt32Array = int32Array.New(temporaryArrayBuffer)
)
func ensureTemporaryArrayBufferSize(byteLength int) {
if bufl := tmpArrayBufferByteLength; bufl < byteLength {
if bufl := temporaryArrayBufferByteLength; bufl < byteLength {
for bufl < byteLength {
bufl *= 2
}
tmpArrayBufferByteLength = bufl
tmpArrayBuffer = arrayBuffer.New(bufl)
tmpUint8Array = uint8Array.New(tmpArrayBuffer)
tmpFloat32Array = float32Array.New(tmpArrayBuffer)
tmpInt32Array = int32Array.New(tmpArrayBuffer)
temporaryArrayBufferByteLength = bufl
temporaryArrayBuffer = arrayBuffer.New(bufl)
temporaryUint8Array = uint8Array.New(temporaryArrayBuffer)
temporaryFloat32Array = float32Array.New(temporaryArrayBuffer)
temporaryInt32Array = int32Array.New(temporaryArrayBuffer)
}
}
// tmpUint8ArrayFromUint8Slice returns a Uint8Array whose length is at least minLength from an uint8 slice.
// TemporaryUint8ArrayFromUint8Slice returns a Uint8Array whose length is at least minLength from an uint8 slice.
// Be careful that the length can exceed the given minLength.
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
func tmpUint8ArrayFromUint8Slice(minLength int, data []uint8) js.Value {
func TemporaryUint8ArrayFromUint8Slice(minLength int, data []uint8) js.Value {
ensureTemporaryArrayBufferSize(minLength)
copyUint8SliceToTemporaryArrayBuffer(data)
return tmpUint8Array
return temporaryUint8Array
}
// tmpUint8ArrayFromUint16Slice returns a Uint8Array whose length is at least minLength from an uint16 slice.
// TemporaryUint8ArrayFromUint16Slice returns a Uint8Array whose length is at least minLength from an uint16 slice.
// Be careful that the length can exceed the given minLength.
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
func tmpUint8ArrayFromUint16Slice(minLength int, data []uint16) js.Value {
func TemporaryUint8ArrayFromUint16Slice(minLength int, data []uint16) js.Value {
ensureTemporaryArrayBufferSize(minLength * 2)
copySliceToTemporaryArrayBuffer(data)
return tmpUint8Array
return temporaryUint8Array
}
// tmpUint8ArrayFromFloat32Slice returns a Uint8Array whose length is at least minLength from a float32 slice.
// TemporaryUint8ArrayFromFloat32Slice returns a Uint8Array whose length is at least minLength from a float32 slice.
// Be careful that the length can exceed the given minLength.
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
func tmpUint8ArrayFromFloat32Slice(minLength int, data []float32) js.Value {
func TemporaryUint8ArrayFromFloat32Slice(minLength int, data []float32) js.Value {
ensureTemporaryArrayBufferSize(minLength * 4)
copySliceToTemporaryArrayBuffer(data)
return tmpUint8Array
return temporaryUint8Array
}
// tmpFloat32ArrayFromFloat32Slice returns a Float32Array whose length is at least minLength.
// TemporaryFloat32Array returns a Float32Array whose length is at least minLength.
// Be careful that the length can exceed the given minLength.
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
func tmpFloat32ArrayFromFloat32Slice(minLength int, data []float32) js.Value {
func TemporaryFloat32Array(minLength int, data []float32) js.Value {
ensureTemporaryArrayBufferSize(minLength * 4)
copySliceToTemporaryArrayBuffer(data)
return tmpFloat32Array
return temporaryFloat32Array
}
// tmpInt32ArrayFromInt32Slice returns a Int32Array whose length is at least minLength.
// TemporaryInt32Array returns a Int32Array whose length is at least minLength.
// Be careful that the length can exceed the given minLength.
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
func tmpInt32ArrayFromInt32Slice(minLength int, data []int32) js.Value {
func TemporaryInt32Array(minLength int, data []int32) js.Value {
ensureTemporaryArrayBufferSize(minLength * 4)
copySliceToTemporaryArrayBuffer(data)
return tmpInt32Array
return temporaryInt32Array
}

19
internal/jsutil/doc_js.go Normal file
View File

@ -0,0 +1,19 @@
// Copyright 2019 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This can be compiled in non-JS environments to avoid a mysterious error: 'no Go source files'
// See https://travis-ci.org/hajimehoshi/ebiten/builds/603539948
// Package jsutil offers utility functions for Wasm.
package jsutil

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package gl
package jsutil
import (
"runtime"
@ -24,7 +24,7 @@ func copyUint8SliceToTemporaryArrayBuffer(src []uint8) {
if len(src) == 0 {
return
}
js.CopyBytesToJS(tmpUint8Array, src)
js.CopyBytesToJS(temporaryUint8Array, src)
}
type numeric interface {
@ -35,6 +35,6 @@ func copySliceToTemporaryArrayBuffer[T numeric](src []T) {
if len(src) == 0 {
return
}
js.CopyBytesToJS(tmpUint8Array, unsafe.Slice((*byte)(unsafe.Pointer(&src[0])), len(src)*int(unsafe.Sizeof(T(0)))))
js.CopyBytesToJS(temporaryUint8Array, unsafe.Slice((*byte)(unsafe.Pointer(&src[0])), len(src)*int(unsafe.Sizeof(T(0)))))
runtime.KeepAlive(src)
}

View File

@ -83,7 +83,6 @@ type userInterfaceImpl struct {
// bufferOnceSwapped must be accessed from the main thread.
bufferOnceSwapped bool
updateOnceCalled bool
origWindowPosX int
origWindowPosY int
@ -1277,10 +1276,6 @@ func (u *UserInterface) setFPSMode(fpsMode FPSModeType) error {
// update must be called from the main thread.
func (u *UserInterface) update() (float64, float64, error) {
defer func() {
u.updateOnceCalled = true
}()
if err := u.error(); err != nil {
return 0, 0, err
}
@ -1388,9 +1383,7 @@ func (u *UserInterface) update() (float64, float64, error) {
}
}
// If isRunnableOnUnfocused is false and the window is not focused, wait here.
// For the first update, skip this check as the window might not be seen yet in some environments like ChromeOS (#3091).
for !u.isRunnableOnUnfocused() && u.updateOnceCalled {
for !u.isRunnableOnUnfocused() {
// In the initial state on macOS, the window is not shown (#2620).
visible, err := u.window.GetAttrib(glfw.Visible)
if err != nil {