jsutil: Enable to specify length at Uint8ArrayToSlice

This commit is contained in:
Hajime Hoshi 2020-12-15 01:09:40 +09:00
parent b40a9cf7a2
commit 146357c298
2 changed files with 11 additions and 6 deletions

View File

@ -201,10 +201,11 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte {
c.bindFramebuffer(f.native) c.bindFramebuffer(f.native)
p := jsutil.TemporaryUint8Array(4 * width * height) l := 4 * width * height
p := jsutil.TemporaryUint8Array(l)
gl.Call("readPixels", 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, p) gl.Call("readPixels", 0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, p)
return jsutil.Uint8ArrayToSlice(p) return jsutil.Uint8ArrayToSlice(p, l)
} }
func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) { func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) {
@ -574,8 +575,9 @@ func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, he
func (c *context) getBufferSubData(buffer buffer, width, height int) []byte { func (c *context) getBufferSubData(buffer buffer, width, height int) []byte {
gl := c.gl gl := c.gl
gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, buffer) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, buffer)
arr := jsutil.TemporaryUint8Array(4 * width * height) l := 4 * width * height
arr := jsutil.TemporaryUint8Array(l)
gl.Call("getBufferSubData", gles.PIXEL_UNPACK_BUFFER, 0, arr) gl.Call("getBufferSubData", gles.PIXEL_UNPACK_BUFFER, 0, arr)
gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, 0) gl.Call("bindBuffer", gles.PIXEL_UNPACK_BUFFER, 0)
return jsutil.Uint8ArrayToSlice(arr) return jsutil.Uint8ArrayToSlice(arr, l)
} }

View File

@ -22,8 +22,11 @@ import (
"unsafe" "unsafe"
) )
func Uint8ArrayToSlice(value js.Value) []byte { func Uint8ArrayToSlice(value js.Value, length int) []byte {
s := make([]byte, value.Get("byteLength").Int()) if l := value.Get("byteLength").Int(); length > l {
length = l
}
s := make([]byte, length)
js.CopyBytesToGo(s, value) js.CopyBytesToGo(s, value)
return s return s
} }