mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
jsutil: Avoid creating Uint8Array when copying bytes from Go to JS
Updates #1435
This commit is contained in:
parent
49b86843c3
commit
7e32075abd
@ -202,7 +202,7 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte {
|
||||
c.bindFramebuffer(f.native)
|
||||
|
||||
l := 4 * width * height
|
||||
p := jsutil.TemporaryUint8Array(l)
|
||||
p := jsutil.TemporaryUint8Array(l, nil)
|
||||
gl.Call("readPixels", 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, p)
|
||||
|
||||
return jsutil.Uint8ArrayToSlice(p, l)
|
||||
@ -387,8 +387,7 @@ func (c *context) uniformFloats(p program, location string, v []float32, typ sha
|
||||
base = typ.Sub[0].Main
|
||||
}
|
||||
|
||||
arr := jsutil.TemporaryFloat32Array(len(v))
|
||||
jsutil.CopySliceToJS(arr, v)
|
||||
arr := jsutil.TemporaryFloat32Array(len(v), v)
|
||||
|
||||
switch base {
|
||||
case shaderir.Float:
|
||||
@ -484,8 +483,7 @@ func (c *context) bindElementArrayBuffer(b buffer) {
|
||||
func (c *context) arrayBufferSubData(data []float32) {
|
||||
gl := c.gl
|
||||
l := len(data) * 4
|
||||
arr := jsutil.TemporaryUint8Array(l)
|
||||
jsutil.CopySliceToJS(arr, data)
|
||||
arr := jsutil.TemporaryUint8Array(l, data)
|
||||
if isWebGL2Available {
|
||||
gl.Call("bufferSubData", gles.ARRAY_BUFFER, 0, arr, 0, l)
|
||||
} else {
|
||||
@ -496,8 +494,7 @@ func (c *context) arrayBufferSubData(data []float32) {
|
||||
func (c *context) elementArrayBufferSubData(data []uint16) {
|
||||
gl := c.gl
|
||||
l := len(data) * 2
|
||||
arr := jsutil.TemporaryUint8Array(l)
|
||||
jsutil.CopySliceToJS(arr, data)
|
||||
arr := jsutil.TemporaryUint8Array(l, data)
|
||||
if isWebGL2Available {
|
||||
gl.Call("bufferSubData", gles.ELEMENT_ARRAY_BUFFER, 0, arr, 0, l)
|
||||
} else {
|
||||
@ -542,8 +539,7 @@ func (c *context) texSubImage2D(t textureNative, width, height int, args []*driv
|
||||
c.bindTexture(t)
|
||||
gl := c.gl
|
||||
for _, a := range args {
|
||||
arr := jsutil.TemporaryUint8Array(len(a.Pixels))
|
||||
jsutil.CopySliceToJS(arr, a.Pixels)
|
||||
arr := jsutil.TemporaryUint8Array(len(a.Pixels), a.Pixels)
|
||||
if isWebGL2Available {
|
||||
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
||||
// GLsizei width, GLsizei height,
|
||||
@ -578,8 +574,7 @@ func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, he
|
||||
|
||||
stride := 4 * width
|
||||
for _, a := range args {
|
||||
arr := jsutil.TemporaryUint8Array(len(a.Pixels))
|
||||
jsutil.CopySliceToJS(arr, a.Pixels)
|
||||
arr := jsutil.TemporaryUint8Array(len(a.Pixels), a.Pixels)
|
||||
offset := 4 * (a.Y*width + a.X)
|
||||
for j := 0; j < a.Height; j++ {
|
||||
gl.Call("bufferSubData", gles.PIXEL_UNPACK_BUFFER, offset+stride*j, arr, 4*a.Width*j, 4*a.Width)
|
||||
@ -597,7 +592,7 @@ func (c *context) getBufferSubData(buffer buffer, width, height int) []byte {
|
||||
gl := c.gl
|
||||
gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, buffer)
|
||||
l := 4 * width * height
|
||||
arr := jsutil.TemporaryUint8Array(l)
|
||||
arr := jsutil.TemporaryUint8Array(l, nil)
|
||||
if isWebGL2Available {
|
||||
gl.Call("getBufferSubData", gles.PIXEL_UNPACK_BUFFER, 0, arr, 0, l)
|
||||
} else {
|
||||
|
@ -18,11 +18,6 @@ import (
|
||||
"syscall/js"
|
||||
)
|
||||
|
||||
// isTypedArrayWritable represents whether TypedArray is writable or not.
|
||||
// TypedArray's properties are not writable in the Web standard, but are writable with go2cpp.
|
||||
// This enables to avoid unnecessary allocations of js.Value.
|
||||
var isTypedArrayWritable = js.Global().Get("go2cpp").Truthy()
|
||||
|
||||
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.
|
||||
@ -43,40 +38,32 @@ func ensureTemporaryArrayBufferSize(byteLength int) {
|
||||
}
|
||||
temporaryArrayBuffer = js.Global().Get("ArrayBuffer").New(bufl)
|
||||
}
|
||||
if temporaryUint8Array.Get("byteLength").Int() < temporaryArrayBuffer.Get("byteLength").Int() {
|
||||
temporaryUint8Array = js.Global().Get("Uint8Array").New(temporaryArrayBuffer)
|
||||
}
|
||||
if temporaryFloat32Array.Get("byteLength").Int() < temporaryArrayBuffer.Get("byteLength").Int() {
|
||||
temporaryFloat32Array = js.Global().Get("Float32Array").New(temporaryArrayBuffer)
|
||||
}
|
||||
}
|
||||
|
||||
// TemporaryUint8Array returns a Uint8Array whose length is at least minLength.
|
||||
// Be careful that the length can exceed the given minLength.
|
||||
func TemporaryUint8Array(minLength int) js.Value {
|
||||
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
|
||||
func TemporaryUint8Array(minLength int, data interface{}) js.Value {
|
||||
ensureTemporaryArrayBufferSize(minLength)
|
||||
if temporaryUint8Array.Get("byteLength").Int() < temporaryArrayBuffer.Get("byteLength").Int() {
|
||||
temporaryUint8Array = js.Global().Get("Uint8Array").New(temporaryArrayBuffer)
|
||||
if data != nil {
|
||||
copySliceToTemporaryArrayBuffer(data)
|
||||
}
|
||||
return temporaryUint8Array
|
||||
}
|
||||
|
||||
// TemporaryFloat32Array returns a Float32Array whose length is at least minLength.
|
||||
// Be careful that the length can exceed the given minLength.
|
||||
func TemporaryFloat32Array(minLength int) js.Value {
|
||||
// data must be a slice of a numeric type for initialization, or nil if you don't need initialization.
|
||||
func TemporaryFloat32Array(minLength int, data interface{}) js.Value {
|
||||
ensureTemporaryArrayBufferSize(minLength * 4)
|
||||
if temporaryFloat32Array.Get("byteLength").Int() < temporaryArrayBuffer.Get("byteLength").Int() {
|
||||
temporaryFloat32Array = js.Global().Get("Float32Array").New(temporaryArrayBuffer)
|
||||
if data != nil {
|
||||
copySliceToTemporaryArrayBuffer(data)
|
||||
}
|
||||
return temporaryFloat32Array
|
||||
}
|
||||
|
||||
var uint8ArrayObj js.Value
|
||||
|
||||
// TODO: Remove this
|
||||
func uint8Array(buffer js.Value, byteOffset, byteLength int) js.Value {
|
||||
if isTypedArrayWritable {
|
||||
if Equal(uint8ArrayObj, js.Undefined()) {
|
||||
uint8ArrayObj = js.Global().Get("Uint8Array").New()
|
||||
}
|
||||
uint8ArrayObj.Set("buffer", buffer)
|
||||
uint8ArrayObj.Set("byteOffset", byteOffset)
|
||||
uint8ArrayObj.Set("byteLength", byteLength)
|
||||
return uint8ArrayObj
|
||||
}
|
||||
return js.Global().Get("Uint8Array").New(buffer, byteOffset, byteLength)
|
||||
}
|
||||
|
@ -1,77 +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.
|
||||
|
||||
package jsutil_test
|
||||
|
||||
import (
|
||||
"syscall/js"
|
||||
"testing"
|
||||
|
||||
. "github.com/hajimehoshi/ebiten/v2/internal/jsutil"
|
||||
)
|
||||
|
||||
func TestCopySliceToJS(t *testing.T) {
|
||||
tests := []struct {
|
||||
in interface{}
|
||||
out js.Value
|
||||
}{
|
||||
{
|
||||
in: []int8{1, 2, 3},
|
||||
out: js.Global().Get("Int8Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []int16{1, 2, 3},
|
||||
out: js.Global().Get("Int16Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []int32{1, 2, 3},
|
||||
out: js.Global().Get("Int32Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []uint8{1, 2, 3},
|
||||
out: js.Global().Get("Uint8Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []uint16{1, 2, 3},
|
||||
out: js.Global().Get("Uint16Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []uint32{1, 2, 3},
|
||||
out: js.Global().Get("Uint32Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []float32{1, 2, 3},
|
||||
out: js.Global().Get("Float32Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
{
|
||||
in: []float64{1, 2, 3},
|
||||
out: js.Global().Get("Float64Array").Call("of", 1, 2, 3),
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
got := test.out.Get("constructor").New(test.out.Get("length"))
|
||||
CopySliceToJS(got, test.in)
|
||||
want := test.out
|
||||
if got.Length() != want.Length() {
|
||||
t.Errorf("length: got: %d, want: %d", got.Length(), want.Length())
|
||||
}
|
||||
for i := 0; i < got.Length(); i++ {
|
||||
gotv := got.Index(i).Float()
|
||||
wantv := want.Index(i).Float()
|
||||
if gotv != wantv {
|
||||
t.Errorf("[%d]: got: %v, want: %v", i, gotv, wantv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -93,13 +93,12 @@ func sliceToByteSlice(s interface{}) (bs []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func CopySliceToJS(dst js.Value, src interface{}) {
|
||||
func copySliceToTemporaryArrayBuffer(src interface{}) {
|
||||
switch s := src.(type) {
|
||||
case []uint8:
|
||||
js.CopyBytesToJS(dst, s)
|
||||
js.CopyBytesToJS(temporaryUint8Array, s)
|
||||
case []int8, []int16, []int32, []uint16, []uint32, []float32, []float64:
|
||||
a := uint8Array(dst.Get("buffer"), dst.Get("byteOffset").Int(), dst.Get("byteLength").Int())
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
js.CopyBytesToJS(temporaryUint8Array, sliceToByteSlice(s))
|
||||
default:
|
||||
panic(fmt.Sprintf("jsutil: unexpected value at CopySliceToJS: %T", s))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user