mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
jsutil: Refactoring: SliceToTypedArray -> CopySliceToJS
This unifieslocations of the temporary buffer.
This commit is contained in:
parent
32c04767da
commit
3c29fbdce2
@ -27,7 +27,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/audio"
|
||||
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
||||
)
|
||||
|
||||
// TODO: This just uses decodeAudioData, that can treat audio files other than MP3.
|
||||
@ -176,9 +175,9 @@ func init() {
|
||||
|
||||
func float32ArrayToSlice(arr js.Value) []float32 {
|
||||
f := make([]float32, arr.Length())
|
||||
a, free := jsutil.SliceToTypedArray(f)
|
||||
a := js.TypedArrayOf(f)
|
||||
a.Call("set", arr)
|
||||
free()
|
||||
a.Release()
|
||||
return f
|
||||
}
|
||||
|
||||
@ -197,7 +196,8 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
||||
// TODO: 1 is a correct second argument?
|
||||
oc := offlineAudioContextClass.New(2, 1, context.SampleRate())
|
||||
|
||||
u8, free := jsutil.SliceToTypedArray(buf)
|
||||
u8 := js.TypedArrayOf(buf)
|
||||
defer u8.Release()
|
||||
a := u8.Get("buffer").Call("slice", u8.Get("byteOffset"), u8.Get("byteOffset").Int()+u8.Get("byteLength").Int())
|
||||
|
||||
succeeded := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||||
@ -231,7 +231,6 @@ func decode(context *audio.Context, buf []byte, try int) (*Stream, error) {
|
||||
defer failed.Release()
|
||||
|
||||
oc.Call("decodeAudioData", a, succeeded, failed)
|
||||
free()
|
||||
|
||||
timeout := time.Duration(math.Pow(2, float64(try))) * time.Second
|
||||
t := time.NewTimer(timeout)
|
||||
|
@ -20,8 +20,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"syscall/js"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
||||
)
|
||||
|
||||
var flatten = js.Global().Get("window").Call("eval", `(function(arr) {
|
||||
@ -121,9 +119,9 @@ func DecodeVorbis(buf []byte) (*Samples, int, int, error) {
|
||||
}
|
||||
|
||||
s := make([]float32, flattened.Length())
|
||||
arr, free := jsutil.SliceToTypedArray(s)
|
||||
arr := js.TypedArrayOf(s)
|
||||
arr.Call("set", flattened)
|
||||
free()
|
||||
arr.Release()
|
||||
|
||||
samples.samples = append(samples.samples, s)
|
||||
samples.lengthInSamples += int64(len(s)) / int64(samples.channels)
|
||||
@ -131,9 +129,9 @@ func DecodeVorbis(buf []byte) (*Samples, int, int, error) {
|
||||
})
|
||||
defer f.Release()
|
||||
|
||||
arr, free := jsutil.SliceToTypedArray(buf)
|
||||
arr := js.TypedArrayOf(buf)
|
||||
js.Global().Get("stbvorbis").Call("decode", arr, f)
|
||||
free()
|
||||
arr.Release()
|
||||
|
||||
if err := <-ch; err != nil {
|
||||
return nil, 0, 0, err
|
||||
|
@ -90,11 +90,6 @@ var (
|
||||
unsignedShort = contextPrototype.Get("UNSIGNED_SHORT")
|
||||
)
|
||||
|
||||
// temporaryBuffer is a temporary buffer used at gl.readPixels.
|
||||
// The read data is converted to Go's byte slice as soon as possible.
|
||||
// To avoid often allocating ArrayBuffer, reuse the buffer whenever possible.
|
||||
var temporaryBuffer = js.Global().Get("ArrayBuffer").New(16)
|
||||
|
||||
type contextImpl struct {
|
||||
gl js.Value
|
||||
lastProgramID programID
|
||||
@ -196,14 +191,7 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte,
|
||||
|
||||
c.bindFramebuffer(f.native)
|
||||
|
||||
l := 4 * width * height
|
||||
if bufl := temporaryBuffer.Get("byteLength").Int(); bufl < l {
|
||||
for bufl < l {
|
||||
bufl *= 2
|
||||
}
|
||||
temporaryBuffer = js.Global().Get("ArrayBuffer").New(bufl)
|
||||
}
|
||||
p := js.Global().Get("Uint8Array").New(temporaryBuffer, 0, l)
|
||||
p := jsutil.TemporaryUint8Array(4 * width * height)
|
||||
gl.Call("readPixels", 0, 0, width, height, rgba, unsignedByte, p)
|
||||
|
||||
return jsutil.Uint8ArrayToSlice(p), nil
|
||||
@ -240,9 +228,9 @@ func (c *context) texSubImage2D(t textureNative, pixels []byte, x, y, width, hei
|
||||
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
||||
// GLsizei width, GLsizei height,
|
||||
// GLenum format, GLenum type, ArrayBufferView? pixels);
|
||||
p, free := jsutil.SliceToTypedArray(pixels)
|
||||
gl.Call("texSubImage2D", texture2d, 0, x, y, width, height, rgba, unsignedByte, p)
|
||||
free()
|
||||
arr := jsutil.TemporaryUint8Array(len(pixels))
|
||||
jsutil.CopySliceToJS(arr, pixels)
|
||||
gl.Call("texSubImage2D", texture2d, 0, x, y, width, height, rgba, unsignedByte, arr)
|
||||
}
|
||||
|
||||
func (c *context) newFramebuffer(t textureNative) (framebufferNative, error) {
|
||||
@ -380,9 +368,10 @@ func (c *context) uniformFloats(p program, location string, v []float32) {
|
||||
case 4:
|
||||
gl.Call("uniform4f", js.Value(l), v[0], v[1], v[2], v[3])
|
||||
case 16:
|
||||
arr, free := jsutil.SliceToTypedArray(v)
|
||||
arr8 := jsutil.TemporaryUint8Array(len(v) * 4)
|
||||
arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(v))
|
||||
jsutil.CopySliceToJS(arr, v)
|
||||
gl.Call("uniformMatrix4fv", js.Value(l), false, arr)
|
||||
free()
|
||||
default:
|
||||
panic(fmt.Sprintf("opengl: invalid uniform floats num: %d", len(v)))
|
||||
}
|
||||
@ -433,17 +422,19 @@ func (c *context) bindBuffer(bufferType bufferType, b buffer) {
|
||||
func (c *context) arrayBufferSubData(data []float32) {
|
||||
c.ensureGL()
|
||||
gl := c.gl
|
||||
arr, free := jsutil.SliceToTypedArray(data)
|
||||
arr8 := jsutil.TemporaryUint8Array(len(data) * 4)
|
||||
arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data))
|
||||
jsutil.CopySliceToJS(arr, data)
|
||||
gl.Call("bufferSubData", int(arrayBuffer), 0, arr)
|
||||
free()
|
||||
}
|
||||
|
||||
func (c *context) elementArrayBufferSubData(data []uint16) {
|
||||
c.ensureGL()
|
||||
gl := c.gl
|
||||
arr, free := jsutil.SliceToTypedArray(data)
|
||||
arr8 := jsutil.TemporaryUint8Array(len(data) * 2)
|
||||
arr := js.Global().Get("Uint16Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data))
|
||||
jsutil.CopySliceToJS(arr, data)
|
||||
gl.Call("bufferSubData", int(elementArrayBuffer), 0, arr)
|
||||
free()
|
||||
}
|
||||
|
||||
func (c *context) deleteBuffer(b buffer) {
|
||||
|
34
internal/jsutil/buf_js.go
Normal file
34
internal/jsutil/buf_js.go
Normal file
@ -0,0 +1,34 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
// temporaryBuffer is a temporary buffer used at gl.readPixels.
|
||||
// The read data is converted to Go's byte slice as soon as possible.
|
||||
// To avoid often allocating ArrayBuffer, reuse the buffer whenever possible.
|
||||
var temporaryBuffer = js.Global().Get("ArrayBuffer").New(16)
|
||||
|
||||
func TemporaryUint8Array(byteLength int) js.Value {
|
||||
if bufl := temporaryBuffer.Get("byteLength").Int(); bufl < byteLength {
|
||||
for bufl < byteLength {
|
||||
bufl *= 2
|
||||
}
|
||||
temporaryBuffer = js.Global().Get("ArrayBuffer").New(bufl)
|
||||
}
|
||||
return js.Global().Get("Uint8Array").New(temporaryBuffer, 0, byteLength)
|
||||
}
|
@ -35,10 +35,11 @@ func ArrayBufferToSlice(value js.Value) []byte {
|
||||
return Uint8ArrayToSlice(js.Global().Get("Uint8Array").New(value))
|
||||
}
|
||||
|
||||
func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
||||
func CopySliceToJS(dst js.Value, src interface{}) {
|
||||
// Note that TypedArrayOf cannot work correcly on Wasm.
|
||||
// See https://github.com/golang/go/issues/31980
|
||||
|
||||
a := js.TypedArrayOf(s)
|
||||
return a.Value, func() { a.Release() }
|
||||
a := js.TypedArrayOf(src)
|
||||
dst.Call("set", a)
|
||||
a.Release()
|
||||
}
|
||||
|
@ -96,60 +96,14 @@ func sliceToByteSlice(s interface{}) (bs []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
var temporaryBuffer = js.Global().Get("ArrayBuffer").New(16)
|
||||
|
||||
func getTemporaryUint8Array(size int) js.Value {
|
||||
if l := temporaryBuffer.Get("byteLength").Int(); l < size {
|
||||
for l < size {
|
||||
l *= 2
|
||||
}
|
||||
temporaryBuffer = js.Global().Get("ArrayBuffer").New(l)
|
||||
}
|
||||
return js.Global().Get("Uint8Array").New(temporaryBuffer, 0, size)
|
||||
}
|
||||
|
||||
func SliceToTypedArray(s interface{}) (js.Value, func()) {
|
||||
switch s := s.(type) {
|
||||
case []int8:
|
||||
a := getTemporaryUint8Array(len(s))
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), func() {}
|
||||
case []int16:
|
||||
a := getTemporaryUint8Array(len(s) * 2)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
||||
case []int32:
|
||||
a := getTemporaryUint8Array(len(s) * 4)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||
func CopySliceToJS(dst js.Value, src interface{}) {
|
||||
switch s := src.(type) {
|
||||
case []uint8:
|
||||
a := getTemporaryUint8Array(len(s))
|
||||
js.CopyBytesToJS(a, s)
|
||||
return a, func() {}
|
||||
case []uint16:
|
||||
a := getTemporaryUint8Array(len(s) * 2)
|
||||
js.CopyBytesToJS(dst, s)
|
||||
case []int8, []int16, []int32, []uint16, []uint32, []float32, []float64:
|
||||
a := js.Global().Get("Uint8Array").New(dst.Get("buffer"), dst.Get("byteOffset"), dst.Get("byteLength"))
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), func() {}
|
||||
case []uint32:
|
||||
a := getTemporaryUint8Array(len(s) * 4)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||
case []float32:
|
||||
a := getTemporaryUint8Array(len(s) * 4)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), func() {}
|
||||
case []float64:
|
||||
a := getTemporaryUint8Array(len(s) * 8)
|
||||
js.CopyBytesToJS(a, sliceToByteSlice(s))
|
||||
buf := a.Get("buffer")
|
||||
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), func() {}
|
||||
default:
|
||||
panic(fmt.Sprintf("jsutil: unexpected value at SliceToTypedArray: %T", s))
|
||||
panic(fmt.Sprintf("jsutil: unexpected value at CopySliceToJS: %T", s))
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ func TestArrayBufferToSlice(t *testing.T) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
func TestSliceToTypedArray(t *testing.T) {
|
||||
func TestCopySliceToJS(t *testing.T) {
|
||||
tests := []struct {
|
||||
in interface{}
|
||||
out js.Value
|
||||
@ -66,12 +66,9 @@ func TestSliceToTypedArray(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
got, free := SliceToTypedArray(test.in)
|
||||
defer free()
|
||||
got := test.out.Get("constructor").New(test.out.Get("length"))
|
||||
CopySliceToJS(got, test.in)
|
||||
want := test.out
|
||||
if got.Get("constructor") != want.Get("constructor") {
|
||||
t.Errorf("class: got: %s, want: %s", got.Get("constructor").Get("name"), want.Get("constructor").Get("name"))
|
||||
}
|
||||
if got.Length() != want.Length() {
|
||||
t.Errorf("length: got: %d, want: %d", got.Length(), want.Length())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user