From 9a511fecb5db506f0f620e1c999a3577fe10e79c Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 14 Sep 2024 16:08:57 +0900 Subject: [PATCH] internal/jsutil: move to internal/graphicsdriver/opengl/gl --- .../opengl/gl}/buf_js.go | 62 +++++++++---------- .../graphicsdriver/opengl/gl/default_js.go | 30 +++++---- .../opengl/gl}/slice_js.go | 6 +- internal/jsutil/doc_js.go | 19 ------ 4 files changed, 48 insertions(+), 69 deletions(-) rename internal/{jsutil => graphicsdriver/opengl/gl}/buf_js.go (53%) rename internal/{jsutil => graphicsdriver/opengl/gl}/slice_js.go (84%) delete mode 100644 internal/jsutil/doc_js.go diff --git a/internal/jsutil/buf_js.go b/internal/graphicsdriver/opengl/gl/buf_js.go similarity index 53% rename from internal/jsutil/buf_js.go rename to internal/graphicsdriver/opengl/gl/buf_js.go index 50c334a20..e1aaccaac 100644 --- a/internal/jsutil/buf_js.go +++ b/internal/graphicsdriver/opengl/gl/buf_js.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package jsutil +package gl import ( "syscall/js" @@ -27,77 +27,77 @@ var ( ) var ( - temporaryArrayBufferByteLength = 16 + tmpArrayBufferByteLength = 16 - // temporaryArrayBuffer is a temporary buffer used at gl.readPixels or gl.texSubImage2D. + // tmpArrayBuffer 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. // To avoid often allocating ArrayBuffer, reuse the buffer whenever possible. - temporaryArrayBuffer = arrayBuffer.New(temporaryArrayBufferByteLength) + tmpArrayBuffer = arrayBuffer.New(tmpArrayBufferByteLength) - // temporaryUint8Array is a Uint8ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. - temporaryUint8Array = uint8Array.New(temporaryArrayBuffer) + // tmpUint8Array is a Uint8ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. + tmpUint8Array = uint8Array.New(tmpArrayBuffer) - // temporaryFloat32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. - temporaryFloat32Array = float32Array.New(temporaryArrayBuffer) + // tmpFloat32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. + tmpFloat32Array = float32Array.New(tmpArrayBuffer) - // temporaryInt32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. - temporaryInt32Array = int32Array.New(temporaryArrayBuffer) + // tmpInt32Array is a Float32ArrayBuffer whose underlying buffer is always temporaryArrayBuffer. + tmpInt32Array = int32Array.New(tmpArrayBuffer) ) func ensureTemporaryArrayBufferSize(byteLength int) { - if bufl := temporaryArrayBufferByteLength; bufl < byteLength { + if bufl := tmpArrayBufferByteLength; bufl < byteLength { for bufl < byteLength { bufl *= 2 } - temporaryArrayBufferByteLength = bufl - temporaryArrayBuffer = arrayBuffer.New(bufl) - temporaryUint8Array = uint8Array.New(temporaryArrayBuffer) - temporaryFloat32Array = float32Array.New(temporaryArrayBuffer) - temporaryInt32Array = int32Array.New(temporaryArrayBuffer) + tmpArrayBufferByteLength = bufl + tmpArrayBuffer = arrayBuffer.New(bufl) + tmpUint8Array = uint8Array.New(tmpArrayBuffer) + tmpFloat32Array = float32Array.New(tmpArrayBuffer) + tmpInt32Array = int32Array.New(tmpArrayBuffer) } } -// TemporaryUint8ArrayFromUint8Slice returns a Uint8Array whose length is at least minLength from an uint8 slice. +// tmpUint8ArrayFromUint8Slice returns a Uint8Array whose length is at least minLength from an uint8 slice. // Be careful that the length can exceed the given minLength. // data must be a slice of a numeric type for initialization, or nil if you don't need initialization. -func TemporaryUint8ArrayFromUint8Slice(minLength int, data []uint8) js.Value { +func tmpUint8ArrayFromUint8Slice(minLength int, data []uint8) js.Value { ensureTemporaryArrayBufferSize(minLength) copyUint8SliceToTemporaryArrayBuffer(data) - return temporaryUint8Array + return tmpUint8Array } -// TemporaryUint8ArrayFromUint16Slice returns a Uint8Array whose length is at least minLength from an uint16 slice. +// tmpUint8ArrayFromUint16Slice returns a Uint8Array whose length is at least minLength from an uint16 slice. // Be careful that the length can exceed the given minLength. // data must be a slice of a numeric type for initialization, or nil if you don't need initialization. -func TemporaryUint8ArrayFromUint16Slice(minLength int, data []uint16) js.Value { +func tmpUint8ArrayFromUint16Slice(minLength int, data []uint16) js.Value { ensureTemporaryArrayBufferSize(minLength * 2) copySliceToTemporaryArrayBuffer(data) - return temporaryUint8Array + return tmpUint8Array } -// TemporaryUint8ArrayFromFloat32Slice returns a Uint8Array whose length is at least minLength from a float32 slice. +// tmpUint8ArrayFromFloat32Slice returns a Uint8Array whose length is at least minLength from a float32 slice. // Be careful that the length can exceed the given minLength. // data must be a slice of a numeric type for initialization, or nil if you don't need initialization. -func TemporaryUint8ArrayFromFloat32Slice(minLength int, data []float32) js.Value { +func tmpUint8ArrayFromFloat32Slice(minLength int, data []float32) js.Value { ensureTemporaryArrayBufferSize(minLength * 4) copySliceToTemporaryArrayBuffer(data) - return temporaryUint8Array + return tmpUint8Array } -// TemporaryFloat32Array returns a Float32Array whose length is at least minLength. +// tmpFloat32ArrayFromFloat32Slice returns a Float32Array whose length is at least minLength. // Be careful that the length can exceed the given minLength. // data must be a slice of a numeric type for initialization, or nil if you don't need initialization. -func TemporaryFloat32Array(minLength int, data []float32) js.Value { +func tmpFloat32ArrayFromFloat32Slice(minLength int, data []float32) js.Value { ensureTemporaryArrayBufferSize(minLength * 4) copySliceToTemporaryArrayBuffer(data) - return temporaryFloat32Array + return tmpFloat32Array } -// TemporaryInt32Array returns a Int32Array whose length is at least minLength. +// tmpInt32ArrayFromInt32Slice returns a Int32Array whose length is at least minLength. // Be careful that the length can exceed the given minLength. // data must be a slice of a numeric type for initialization, or nil if you don't need initialization. -func TemporaryInt32Array(minLength int, data []int32) js.Value { +func tmpInt32ArrayFromInt32Slice(minLength int, data []int32) js.Value { ensureTemporaryArrayBufferSize(minLength * 4) copySliceToTemporaryArrayBuffer(data) - return temporaryInt32Array + return tmpInt32Array } diff --git a/internal/graphicsdriver/opengl/gl/default_js.go b/internal/graphicsdriver/opengl/gl/default_js.go index 1545e76e1..13b92e899 100644 --- a/internal/graphicsdriver/opengl/gl/default_js.go +++ b/internal/graphicsdriver/opengl/gl/default_js.go @@ -17,8 +17,6 @@ package gl import ( "fmt" "syscall/js" - - "github.com/hajimehoshi/ebiten/v2/internal/jsutil" ) type defaultContext struct { @@ -288,7 +286,7 @@ func (c *defaultContext) BufferInit(target uint32, size int, usage uint32) { func (c *defaultContext) BufferSubData(target uint32, offset int, data []byte) { l := len(data) - arr := jsutil.TemporaryUint8ArrayFromUint8Slice(l, data) + arr := tmpUint8ArrayFromUint8Slice(l, data) c.fnBufferSubData.Invoke(target, offset, arr, 0, l) } @@ -494,7 +492,7 @@ func (c *defaultContext) ReadPixels(dst []byte, x int32, y int32, width int32, h c.fnReadPixels.Invoke(x, y, width, height, format, xtype, 0) return } - p := jsutil.TemporaryUint8ArrayFromUint8Slice(len(dst), nil) + p := tmpUint8ArrayFromUint8Slice(len(dst), nil) c.fnReadPixels.Invoke(x, y, width, height, format, xtype, p) js.CopyBytesToGo(dst, p) } @@ -531,7 +529,7 @@ func (c *defaultContext) TexParameteri(target uint32, pname uint32, param int32) } func (c *defaultContext) TexSubImage2D(target uint32, level int32, xoffset int32, yoffset int32, width int32, height int32, format uint32, xtype uint32, pixels []byte) { - arr := jsutil.TemporaryUint8ArrayFromUint8Slice(len(pixels), pixels) + arr := tmpUint8ArrayFromUint8Slice(len(pixels), pixels) // void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, // GLsizei width, GLsizei height, // GLenum format, GLenum type, ArrayBufferView pixels, srcOffset); @@ -540,7 +538,7 @@ func (c *defaultContext) TexSubImage2D(target uint32, level int32, xoffset int32 func (c *defaultContext) Uniform1fv(location int32, value []float32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryFloat32Array(len(value), value) + arr := tmpFloat32ArrayFromFloat32Slice(len(value), value) c.fnUniform1fv.Invoke(l, arr, 0, len(value)) } @@ -551,61 +549,61 @@ func (c *defaultContext) Uniform1i(location int32, v0 int32) { func (c *defaultContext) Uniform1iv(location int32, value []int32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryInt32Array(len(value), value) + arr := tmpInt32ArrayFromInt32Slice(len(value), value) c.fnUniform1iv.Invoke(l, arr, 0, len(value)) } func (c *defaultContext) Uniform2fv(location int32, value []float32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryFloat32Array(len(value), value) + arr := tmpFloat32ArrayFromFloat32Slice(len(value), value) c.fnUniform2fv.Invoke(l, arr, 0, len(value)) } func (c *defaultContext) Uniform2iv(location int32, value []int32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryInt32Array(len(value), value) + arr := tmpInt32ArrayFromInt32Slice(len(value), value) c.fnUniform2iv.Invoke(l, arr, 0, len(value)) } func (c *defaultContext) Uniform3fv(location int32, value []float32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryFloat32Array(len(value), value) + arr := tmpFloat32ArrayFromFloat32Slice(len(value), value) c.fnUniform3fv.Invoke(l, arr, 0, len(value)) } func (c *defaultContext) Uniform3iv(location int32, value []int32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryInt32Array(len(value), value) + arr := tmpInt32ArrayFromInt32Slice(len(value), value) c.fnUniform3iv.Invoke(l, arr, 0, len(value)) } func (c *defaultContext) Uniform4fv(location int32, value []float32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryFloat32Array(len(value), value) + arr := tmpFloat32ArrayFromFloat32Slice(len(value), value) c.fnUniform4fv.Invoke(l, arr, 0, len(value)) } func (c *defaultContext) Uniform4iv(location int32, value []int32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryInt32Array(len(value), value) + arr := tmpInt32ArrayFromInt32Slice(len(value), value) c.fnUniform4iv.Invoke(l, arr, 0, len(value)) } func (c *defaultContext) UniformMatrix2fv(location int32, value []float32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryFloat32Array(len(value), value) + arr := tmpFloat32ArrayFromFloat32Slice(len(value), value) c.fnUniformMatrix2fv.Invoke(l, false, arr, 0, len(value)) } func (c *defaultContext) UniformMatrix3fv(location int32, value []float32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryFloat32Array(len(value), value) + arr := tmpFloat32ArrayFromFloat32Slice(len(value), value) c.fnUniformMatrix3fv.Invoke(l, false, arr, 0, len(value)) } func (c *defaultContext) UniformMatrix4fv(location int32, value []float32) { l := c.getUniformLocation(location) - arr := jsutil.TemporaryFloat32Array(len(value), value) + arr := tmpFloat32ArrayFromFloat32Slice(len(value), value) c.fnUniformMatrix4fv.Invoke(l, false, arr, 0, len(value)) } diff --git a/internal/jsutil/slice_js.go b/internal/graphicsdriver/opengl/gl/slice_js.go similarity index 84% rename from internal/jsutil/slice_js.go rename to internal/graphicsdriver/opengl/gl/slice_js.go index ea9a840b6..01291eeca 100644 --- a/internal/jsutil/slice_js.go +++ b/internal/graphicsdriver/opengl/gl/slice_js.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package jsutil +package gl import ( "runtime" @@ -24,7 +24,7 @@ func copyUint8SliceToTemporaryArrayBuffer(src []uint8) { if len(src) == 0 { return } - js.CopyBytesToJS(temporaryUint8Array, src) + js.CopyBytesToJS(tmpUint8Array, src) } type numeric interface { @@ -35,6 +35,6 @@ func copySliceToTemporaryArrayBuffer[T numeric](src []T) { if len(src) == 0 { return } - js.CopyBytesToJS(temporaryUint8Array, unsafe.Slice((*byte)(unsafe.Pointer(&src[0])), len(src)*int(unsafe.Sizeof(T(0))))) + js.CopyBytesToJS(tmpUint8Array, unsafe.Slice((*byte)(unsafe.Pointer(&src[0])), len(src)*int(unsafe.Sizeof(T(0))))) runtime.KeepAlive(src) } diff --git a/internal/jsutil/doc_js.go b/internal/jsutil/doc_js.go deleted file mode 100644 index 02853fee8..000000000 --- a/internal/jsutil/doc_js.go +++ /dev/null @@ -1,19 +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. - -// This can be compiled in non-JS environments to avoid a mysterious error: 'no Go source files' -// See https://travis-ci.org/hajimehoshi/ebiten/builds/603539948 - -// Package jsutil offers utility functions for Wasm. -package jsutil