graphicsdriver/opengl: Reduce allocations of Float32Array on go2cpp

Updates #1426
This commit is contained in:
Hajime Hoshi 2020-12-06 03:23:07 +09:00
parent 2bf4a3bb6e
commit 2c31156a34
2 changed files with 28 additions and 3 deletions

View File

@ -386,8 +386,9 @@ func (c *context) uniformFloats(p program, location string, v []float32, typ sha
base = typ.Sub[0].Main
}
arr8 := jsutil.TemporaryUint8Array(len(v) * 4)
arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(v))
//arr8 := jsutil.TemporaryUint8Array(len(v) * 4)
//arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(v))
arr := jsutil.TemporaryFloat32Array(len(v) * 4)
jsutil.CopySliceToJS(arr, v)
switch base {

View File

@ -28,13 +28,17 @@ var isTypedArrayWritable = js.Global().Get("go2cpp").Truthy()
// To avoid often allocating ArrayBuffer, reuse the buffer whenever possible.
var temporaryBuffer = js.Global().Get("ArrayBuffer").New(16)
func TemporaryUint8Array(byteLength int) js.Value {
func ensureTemporaryBufferSize(byteLength int) {
if bufl := temporaryBuffer.Get("byteLength").Int(); bufl < byteLength {
for bufl < byteLength {
bufl *= 2
}
temporaryBuffer = js.Global().Get("ArrayBuffer").New(bufl)
}
}
func TemporaryUint8Array(byteLength int) js.Value {
ensureTemporaryBufferSize(byteLength)
return uint8Array(temporaryBuffer, 0, byteLength)
}
@ -52,3 +56,23 @@ func uint8Array(buffer js.Value, byteOffset, byteLength int) js.Value {
}
return js.Global().Get("Uint8Array").New(buffer, byteOffset, byteLength)
}
func TemporaryFloat32Array(byteLength int) js.Value {
ensureTemporaryBufferSize(byteLength)
return float32Array(temporaryBuffer, 0, byteLength)
}
var float32ArrayObj js.Value
func float32Array(buffer js.Value, byteOffset, byteLength int) js.Value {
if isTypedArrayWritable {
if Equal(float32ArrayObj, js.Undefined()) {
float32ArrayObj = js.Global().Get("Float32Array").New()
}
float32ArrayObj.Set("buffer", buffer)
float32ArrayObj.Set("byteOffset", byteOffset)
float32ArrayObj.Set("byteLength", byteLength)
return float32ArrayObj
}
return js.Global().Get("Float32Array").New(buffer, byteOffset/4, byteLength/4)
}