diff --git a/internal/graphics/vertices.go b/internal/graphics/vertices.go index 380e3286a..68791585a 100644 --- a/internal/graphics/vertices.go +++ b/internal/graphics/vertices.go @@ -23,23 +23,22 @@ type verticesBackend struct { head int } +const VertexFloatNum = 10 + func (v *verticesBackend) slice(n int) []float32 { - const ( - num = 1024 - vertexFloatNum = 10 - ) + const num = 1024 if n > num { panic("not reached") } - need := n * vertexFloatNum + need := n * VertexFloatNum if v.head+need > len(v.backend) { v.backend = nil v.head = 0 } if v.backend == nil { - v.backend = make([]float32, vertexFloatNum*num) + v.backend = make([]float32, VertexFloatNum*num) } s := v.backend[v.head : v.head+need] @@ -75,6 +74,8 @@ func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, a, b, c, d, tx, ty func quadVerticesImpl(x, y, u0, v0, u1, v1, a, b, c, d, tx, ty, cr, cg, cb, ca float32) []float32 { // Specifying a range explicitly here is redundant but this helps optimization // to eliminate boundry checks. + // + // 4*VertexFloatNum is better than 40, but in GopherJS, optimization might not work. vs := theVerticesBackend.slice(4)[0:40] ax, by, cx, dy := a*x, b*y, c*x, d*y diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index b1a6161b9..1081962e0 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -126,7 +126,7 @@ func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float q.appendVertices(vertices) q.appendIndices(indices, uint16(q.nextIndex)) - q.nextIndex += len(vertices) / opengl.ArrayBufferLayoutFloatNum() + q.nextIndex += len(vertices) / graphics.VertexFloatNum q.tmpNumIndices += len(indices) q.doEnqueueDrawImageCommand(dst, src, len(vertices), len(indices), color, mode, filter, split) diff --git a/internal/opengl/program.go b/internal/opengl/program.go index 3adabb121..2713c391b 100644 --- a/internal/opengl/program.go +++ b/internal/opengl/program.go @@ -101,8 +101,11 @@ func initializeArrayBuferLayout() { } } -func ArrayBufferLayoutFloatNum() int { - return theArrayBufferLayout.totalBytes() / float.SizeInBytes() +func init() { + vertexFloatNum := theArrayBufferLayout.totalBytes() / float.SizeInBytes() + if graphics.VertexFloatNum != vertexFloatNum { + panic(fmt.Sprintf("vertex float num must be %d but %d", graphics.VertexFloatNum, vertexFloatNum)) + } } // openGLState is a state for