2019-10-27 14:40:36 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package jsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall/js"
|
|
|
|
)
|
|
|
|
|
2021-09-04 09:13:58 +02:00
|
|
|
var go2cpp = js.Global().Get("go2cpp").Truthy()
|
|
|
|
|
2020-12-15 18:41:06 +01:00
|
|
|
var (
|
2021-09-04 09:13:58 +02:00
|
|
|
object = js.Global().Get("Object")
|
2020-12-15 18:41:06 +01:00
|
|
|
arrayBuffer = js.Global().Get("ArrayBuffer")
|
|
|
|
uint8Array = js.Global().Get("Uint8Array")
|
|
|
|
float32Array = js.Global().Get("Float32Array")
|
|
|
|
)
|
|
|
|
|
2020-12-15 17:14:19 +01:00
|
|
|
var (
|
|
|
|
// 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.
|
2020-12-26 09:04:12 +01:00
|
|
|
temporaryArrayBuffer = arrayBuffer.New(16)
|
2019-10-27 14:40:36 +01:00
|
|
|
|
2020-12-15 17:14:19 +01:00
|
|
|
// temporaryUint8Array is a Uint8ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
|
2020-12-26 09:04:12 +01:00
|
|
|
temporaryUint8Array = uint8Array.New(temporaryArrayBuffer)
|
2020-12-15 17:14:19 +01:00
|
|
|
|
|
|
|
// temporaryFloat32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer.
|
2020-12-26 09:04:12 +01:00
|
|
|
temporaryFloat32Array = float32Array.New(temporaryArrayBuffer)
|
2020-12-15 17:14:19 +01:00
|
|
|
)
|
2020-12-13 19:02:58 +01:00
|
|
|
|
2021-09-04 09:13:58 +02:00
|
|
|
var (
|
|
|
|
temporaryArrayBufferByteLengthFunc js.Value
|
|
|
|
temporaryUint8ArrayByteLengthFunc js.Value
|
|
|
|
temporaryFloat32ArrayByteLengthFunc js.Value
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
if go2cpp {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
temporaryArrayBufferByteLengthFunc = object.Call("getOwnPropertyDescriptor", arrayBuffer.Get("prototype"), "byteLength").Get("get").Call("bind", temporaryArrayBuffer)
|
|
|
|
temporaryUint8ArrayByteLengthFunc = object.Call("getOwnPropertyDescriptor", object.Call("getPrototypeOf", uint8Array).Get("prototype"), "byteLength").Get("get").Call("bind", temporaryUint8Array)
|
|
|
|
temporaryFloat32ArrayByteLengthFunc = object.Call("getOwnPropertyDescriptor", object.Call("getPrototypeOf", float32Array).Get("prototype"), "byteLength").Get("get").Call("bind", temporaryFloat32Array)
|
|
|
|
}
|
|
|
|
|
|
|
|
func temporaryArrayBufferByteLength() int {
|
|
|
|
if go2cpp {
|
|
|
|
return temporaryArrayBuffer.Get("byteLength").Int()
|
|
|
|
}
|
|
|
|
return temporaryArrayBufferByteLengthFunc.Invoke().Int()
|
|
|
|
}
|
|
|
|
|
|
|
|
func temporaryUint8ArrayByteLength() int {
|
|
|
|
if go2cpp {
|
|
|
|
return temporaryUint8Array.Get("byteLength").Int()
|
|
|
|
}
|
|
|
|
return temporaryUint8ArrayByteLengthFunc.Invoke().Int()
|
|
|
|
}
|
|
|
|
|
|
|
|
func temporaryFloat32ArrayByteLength() int {
|
|
|
|
if go2cpp {
|
|
|
|
return temporaryFloat32Array.Get("byteLength").Int()
|
|
|
|
}
|
|
|
|
return temporaryFloat32ArrayByteLengthFunc.Invoke().Int()
|
|
|
|
}
|
|
|
|
|
2020-12-13 19:02:58 +01:00
|
|
|
func ensureTemporaryArrayBufferSize(byteLength int) {
|
2021-09-04 09:13:58 +02:00
|
|
|
bufl := temporaryArrayBufferByteLength()
|
2020-12-25 21:12:02 +01:00
|
|
|
if bufl < byteLength {
|
2019-10-27 14:40:36 +01:00
|
|
|
for bufl < byteLength {
|
|
|
|
bufl *= 2
|
|
|
|
}
|
2020-12-15 18:41:06 +01:00
|
|
|
temporaryArrayBuffer = arrayBuffer.New(bufl)
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2021-09-04 09:13:58 +02:00
|
|
|
if temporaryUint8ArrayByteLength() < bufl {
|
2020-12-15 18:41:06 +01:00
|
|
|
temporaryUint8Array = uint8Array.New(temporaryArrayBuffer)
|
2020-12-15 17:30:09 +01:00
|
|
|
}
|
2021-09-04 09:13:58 +02:00
|
|
|
if temporaryFloat32ArrayByteLength() < bufl {
|
2020-12-15 18:41:06 +01:00
|
|
|
temporaryFloat32Array = float32Array.New(temporaryArrayBuffer)
|
2020-12-15 17:30:09 +01:00
|
|
|
}
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
|
|
|
|
2021-10-30 11:43:46 +02:00
|
|
|
// TemporaryUint8ArrayFromUint8Slice returns a Uint8Array whose length is at least minLength from a uint8 slice.
|
2020-12-15 17:14:19 +01:00
|
|
|
// Be careful that the length can exceed the given minLength.
|
2020-12-15 17:30:09 +01:00
|
|
|
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
|
2021-10-30 11:43:46 +02:00
|
|
|
func TemporaryUint8ArrayFromUint8Slice(minLength int, data []uint8) js.Value {
|
2020-12-15 17:14:19 +01:00
|
|
|
ensureTemporaryArrayBufferSize(minLength)
|
2021-10-30 11:43:46 +02:00
|
|
|
copyUint8SliceToTemporaryArrayBuffer(data)
|
|
|
|
return temporaryUint8Array
|
|
|
|
}
|
|
|
|
|
|
|
|
// TemporaryUint8ArrayFromUint16Slice returns a Uint8Array whose length is at least minLength from a 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 TemporaryUint8ArrayFromUint16Slice(minLength int, data []uint16) js.Value {
|
|
|
|
ensureTemporaryArrayBufferSize(minLength * 2)
|
|
|
|
copyUint16SliceToTemporaryArrayBuffer(data)
|
|
|
|
return temporaryUint8Array
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 TemporaryUint8ArrayFromFloat32Slice(minLength int, data []float32) js.Value {
|
|
|
|
ensureTemporaryArrayBufferSize(minLength * 4)
|
|
|
|
copyFloat32SliceToTemporaryArrayBuffer(data)
|
2020-12-15 17:14:19 +01:00
|
|
|
return temporaryUint8Array
|
2020-12-05 19:23:07 +01:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:14:19 +01:00
|
|
|
// TemporaryFloat32Array returns a Float32Array whose length is at least minLength.
|
|
|
|
// Be careful that the length can exceed the given minLength.
|
2020-12-15 17:30:09 +01:00
|
|
|
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
|
2021-10-30 11:43:46 +02:00
|
|
|
func TemporaryFloat32Array(minLength int, data []float32) js.Value {
|
2020-12-15 17:14:19 +01:00
|
|
|
ensureTemporaryArrayBufferSize(minLength * 4)
|
2021-10-30 11:43:46 +02:00
|
|
|
copyFloat32SliceToTemporaryArrayBuffer(data)
|
2020-12-15 17:14:19 +01:00
|
|
|
return temporaryFloat32Array
|
2020-12-05 18:57:57 +01:00
|
|
|
}
|