Skip to content

Commit

Permalink
need to use Uint8ClampedArray in BytesToJS so that it works in core f…
Browse files Browse the repository at this point in the history
…or making ImageData
  • Loading branch information
kkoreilly committed Sep 6, 2024
1 parent 27294de commit 4c0db81
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions wgpu/adapt_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ func ensureBufferSize(size int) {
uint8Array = js.Global().Get("Uint8Array").New(arrayBuffer)
}

// BytesToJS converts the given bytes to a js Uint8Array
// BytesToJS converts the given bytes to a js Uint8ClampedArray
// by using the global wasm memory bytes. This avoids the
// copying present in [js.CopyBytesToJS].
func BytesToJS(b []byte) js.Value {
ptr := uintptr(unsafe.Pointer(&b[0]))
memoryBytes := js.Global().Get("Uint8Array").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"))
memoryBytes := js.Global().Get("Uint8ClampedArray").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"))
// using subarray instead of slice gives a 5x performance improvement due to no copying
return memoryBytes.Call("subarray", ptr, ptr+uintptr(len(b)))
}
Expand Down

0 comments on commit 4c0db81

Please sign in to comment.