Compare commits

..

2 Commits

Author SHA1 Message Date
Hajime Hoshi
99ffe09b63 internal/ui: bug fix: skip focus check for the first update 2024-09-14 17:08:21 +09:00
Hajime Hoshi
9a511fecb5 internal/jsutil: move to internal/graphicsdriver/opengl/gl 2024-09-14 16:10:49 +09:00
5 changed files with 56 additions and 70 deletions

View File

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

View File

@ -17,8 +17,6 @@ package gl
import ( import (
"fmt" "fmt"
"syscall/js" "syscall/js"
"github.com/hajimehoshi/ebiten/v2/internal/jsutil"
) )
type defaultContext struct { type defaultContext struct {
@ -288,7 +286,7 @@ func (c *defaultContext) BufferInit(target uint32, size int, usage uint32) {
func (c *defaultContext) BufferSubData(target uint32, offset int, data []byte) { func (c *defaultContext) BufferSubData(target uint32, offset int, data []byte) {
l := len(data) l := len(data)
arr := jsutil.TemporaryUint8ArrayFromUint8Slice(l, data) arr := tmpUint8ArrayFromUint8Slice(l, data)
c.fnBufferSubData.Invoke(target, offset, arr, 0, l) c.fnBufferSubData.Invoke(target, offset, arr, 0, l)
} }
@ -494,7 +492,7 @@ func (c *defaultContext) ReadPixels(dst []byte, x int32, y int32, width int32, h
c.fnReadPixels.Invoke(x, y, width, height, format, xtype, 0) c.fnReadPixels.Invoke(x, y, width, height, format, xtype, 0)
return return
} }
p := jsutil.TemporaryUint8ArrayFromUint8Slice(len(dst), nil) p := tmpUint8ArrayFromUint8Slice(len(dst), nil)
c.fnReadPixels.Invoke(x, y, width, height, format, xtype, p) c.fnReadPixels.Invoke(x, y, width, height, format, xtype, p)
js.CopyBytesToGo(dst, p) js.CopyBytesToGo(dst, p)
} }
@ -531,7 +529,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) { func (c *defaultContext) TexSubImage2D(target uint32, level int32, xoffset int32, yoffset int32, width int32, height int32, format uint32, xtype uint32, pixels []byte) {
arr := jsutil.TemporaryUint8ArrayFromUint8Slice(len(pixels), pixels) arr := tmpUint8ArrayFromUint8Slice(len(pixels), pixels)
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, // void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
// GLsizei width, GLsizei height, // GLsizei width, GLsizei height,
// GLenum format, GLenum type, ArrayBufferView pixels, srcOffset); // GLenum format, GLenum type, ArrayBufferView pixels, srcOffset);
@ -540,7 +538,7 @@ func (c *defaultContext) TexSubImage2D(target uint32, level int32, xoffset int32
func (c *defaultContext) Uniform1fv(location int32, value []float32) { func (c *defaultContext) Uniform1fv(location int32, value []float32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryFloat32Array(len(value), value) arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
c.fnUniform1fv.Invoke(l, arr, 0, len(value)) c.fnUniform1fv.Invoke(l, arr, 0, len(value))
} }
@ -551,61 +549,61 @@ func (c *defaultContext) Uniform1i(location int32, v0 int32) {
func (c *defaultContext) Uniform1iv(location int32, value []int32) { func (c *defaultContext) Uniform1iv(location int32, value []int32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryInt32Array(len(value), value) arr := tmpInt32ArrayFromInt32Slice(len(value), value)
c.fnUniform1iv.Invoke(l, arr, 0, len(value)) c.fnUniform1iv.Invoke(l, arr, 0, len(value))
} }
func (c *defaultContext) Uniform2fv(location int32, value []float32) { func (c *defaultContext) Uniform2fv(location int32, value []float32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryFloat32Array(len(value), value) arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
c.fnUniform2fv.Invoke(l, arr, 0, len(value)) c.fnUniform2fv.Invoke(l, arr, 0, len(value))
} }
func (c *defaultContext) Uniform2iv(location int32, value []int32) { func (c *defaultContext) Uniform2iv(location int32, value []int32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryInt32Array(len(value), value) arr := tmpInt32ArrayFromInt32Slice(len(value), value)
c.fnUniform2iv.Invoke(l, arr, 0, len(value)) c.fnUniform2iv.Invoke(l, arr, 0, len(value))
} }
func (c *defaultContext) Uniform3fv(location int32, value []float32) { func (c *defaultContext) Uniform3fv(location int32, value []float32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryFloat32Array(len(value), value) arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
c.fnUniform3fv.Invoke(l, arr, 0, len(value)) c.fnUniform3fv.Invoke(l, arr, 0, len(value))
} }
func (c *defaultContext) Uniform3iv(location int32, value []int32) { func (c *defaultContext) Uniform3iv(location int32, value []int32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryInt32Array(len(value), value) arr := tmpInt32ArrayFromInt32Slice(len(value), value)
c.fnUniform3iv.Invoke(l, arr, 0, len(value)) c.fnUniform3iv.Invoke(l, arr, 0, len(value))
} }
func (c *defaultContext) Uniform4fv(location int32, value []float32) { func (c *defaultContext) Uniform4fv(location int32, value []float32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryFloat32Array(len(value), value) arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
c.fnUniform4fv.Invoke(l, arr, 0, len(value)) c.fnUniform4fv.Invoke(l, arr, 0, len(value))
} }
func (c *defaultContext) Uniform4iv(location int32, value []int32) { func (c *defaultContext) Uniform4iv(location int32, value []int32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryInt32Array(len(value), value) arr := tmpInt32ArrayFromInt32Slice(len(value), value)
c.fnUniform4iv.Invoke(l, arr, 0, len(value)) c.fnUniform4iv.Invoke(l, arr, 0, len(value))
} }
func (c *defaultContext) UniformMatrix2fv(location int32, value []float32) { func (c *defaultContext) UniformMatrix2fv(location int32, value []float32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryFloat32Array(len(value), value) arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
c.fnUniformMatrix2fv.Invoke(l, false, arr, 0, len(value)) c.fnUniformMatrix2fv.Invoke(l, false, arr, 0, len(value))
} }
func (c *defaultContext) UniformMatrix3fv(location int32, value []float32) { func (c *defaultContext) UniformMatrix3fv(location int32, value []float32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryFloat32Array(len(value), value) arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
c.fnUniformMatrix3fv.Invoke(l, false, arr, 0, len(value)) c.fnUniformMatrix3fv.Invoke(l, false, arr, 0, len(value))
} }
func (c *defaultContext) UniformMatrix4fv(location int32, value []float32) { func (c *defaultContext) UniformMatrix4fv(location int32, value []float32) {
l := c.getUniformLocation(location) l := c.getUniformLocation(location)
arr := jsutil.TemporaryFloat32Array(len(value), value) arr := tmpFloat32ArrayFromFloat32Slice(len(value), value)
c.fnUniformMatrix4fv.Invoke(l, false, arr, 0, len(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 // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package jsutil package gl
import ( import (
"runtime" "runtime"
@ -24,7 +24,7 @@ func copyUint8SliceToTemporaryArrayBuffer(src []uint8) {
if len(src) == 0 { if len(src) == 0 {
return return
} }
js.CopyBytesToJS(temporaryUint8Array, src) js.CopyBytesToJS(tmpUint8Array, src)
} }
type numeric interface { type numeric interface {
@ -35,6 +35,6 @@ func copySliceToTemporaryArrayBuffer[T numeric](src []T) {
if len(src) == 0 { if len(src) == 0 {
return return
} }
js.CopyBytesToJS(temporaryUint8Array, unsafe.Slice((*byte)(unsafe.Pointer(&src[0])), len(src)*int(unsafe.Sizeof(T(0))))) js.CopyBytesToJS(tmpUint8Array, unsafe.Slice((*byte)(unsafe.Pointer(&src[0])), len(src)*int(unsafe.Sizeof(T(0)))))
runtime.KeepAlive(src) runtime.KeepAlive(src)
} }

View File

@ -1,19 +0,0 @@
// 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

@ -83,6 +83,7 @@ type userInterfaceImpl struct {
// bufferOnceSwapped must be accessed from the main thread. // bufferOnceSwapped must be accessed from the main thread.
bufferOnceSwapped bool bufferOnceSwapped bool
updateOnceCalled bool
origWindowPosX int origWindowPosX int
origWindowPosY int origWindowPosY int
@ -1276,6 +1277,10 @@ func (u *UserInterface) setFPSMode(fpsMode FPSModeType) error {
// update must be called from the main thread. // update must be called from the main thread.
func (u *UserInterface) update() (float64, float64, error) { func (u *UserInterface) update() (float64, float64, error) {
defer func() {
u.updateOnceCalled = true
}()
if err := u.error(); err != nil { if err := u.error(); err != nil {
return 0, 0, err return 0, 0, err
} }
@ -1383,7 +1388,9 @@ func (u *UserInterface) update() (float64, float64, error) {
} }
} }
for !u.isRunnableOnUnfocused() { // 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 {
// In the initial state on macOS, the window is not shown (#2620). // In the initial state on macOS, the window is not shown (#2620).
visible, err := u.window.GetAttrib(glfw.Visible) visible, err := u.window.GetAttrib(glfw.Visible)
if err != nil { if err != nil {