From d5ddd38ca9140e0a78816bbd38fa227936c8a2cc Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 26 Oct 2016 23:35:29 +0900 Subject: [PATCH] graphics: Optimization for browsers (#285) --- imageparts.go | 9 --------- vertices.go | 10 +++++++++- vertices_js.go | 28 ++++++++++++++++------------ 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/imageparts.go b/imageparts.go index 47195d08e..f31359704 100644 --- a/imageparts.go +++ b/imageparts.go @@ -16,7 +16,6 @@ package ebiten import ( "image" - "math" ) // An ImagePart is deprecated (as of 1.1.0-alpha): Use ImageParts instead. @@ -65,11 +64,3 @@ func (w *wholeImage) Dst(i int) (x0, y0, x1, y1 int) { func (w *wholeImage) Src(i int) (x0, y0, x1, y1 int) { return 0, 0, w.width, w.height } - -func u(x, width2p int) int16 { - return int16(math.MaxInt16 * x / width2p) -} - -func v(y, height2p int) int16 { - return int16(math.MaxInt16 * y / height2p) -} diff --git a/vertices.go b/vertices.go index ca43af99e..a0536ba1e 100644 --- a/vertices.go +++ b/vertices.go @@ -37,6 +37,14 @@ func floatsToInt16s(xs ...float64) []int16 { return r } +func u(x, width2p int) int16 { + return int16(math.MaxInt16 * x / width2p) +} + +func v(y, height2p int) int16 { + return int16(math.MaxInt16 * y / height2p) +} + func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { // TODO: This function should be in graphics package? totalSize := graphics.QuadVertexSizeInBytes() / 2 @@ -45,13 +53,13 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { vs := make([]int16, l*totalSize) width2p := graphics.NextPowerOf2Int(width) height2p := graphics.NextPowerOf2Int(height) - n := 0 geo16 := floatsToInt16s(geo.Element(0, 0), geo.Element(0, 1), geo.Element(1, 0), geo.Element(1, 1), geo.Element(0, 2), geo.Element(1, 2)) + n := 0 for i := 0; i < l; i++ { dx0, dy0, dx1, dy1 := parts.Dst(i) if dx0 == dx1 || dy0 == dy1 { diff --git a/vertices_js.go b/vertices_js.go index f0af8578a..19263ab02 100644 --- a/vertices_js.go +++ b/vertices_js.go @@ -17,6 +17,8 @@ package ebiten import ( + "math" + "github.com/gopherjs/gopherjs/js" "github.com/hajimehoshi/ebiten/internal/graphics" ) @@ -29,8 +31,8 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { a := js.Global.Get("ArrayBuffer").New(l * totalSize * 2) af32 := js.Global.Get("Float32Array").New(a) a16 := js.Global.Get("Int16Array").New(a) - width2p := graphics.NextPowerOf2Int(width) - height2p := graphics.NextPowerOf2Int(height) + w2p := graphics.NextPowerOf2Int(width) + h2p := graphics.NextPowerOf2Int(height) gs := []float64{geo.Element(0, 0), geo.Element(0, 1), geo.Element(1, 0), @@ -43,36 +45,38 @@ func vertices(parts ImageParts, width, height int, geo *GeoM) []int16 { if dx0 == dx1 || dy0 == dy1 { continue } - x0, y0, x1, y1 := int16(dx0), int16(dy0), int16(dx1), int16(dy1) sx0, sy0, sx1, sy1 := parts.Src(i) if sx0 == sx1 || sy0 == sy1 { continue } - u0, v0, u1, v1 := u(sx0, width2p), v(sy0, height2p), u(sx1, width2p), v(sy1, height2p) + u0 := math.MaxInt16 * sx0 / w2p + v0 := math.MaxInt16 * sy0 / h2p + u1 := math.MaxInt16 * sx1 / w2p + v1 := math.MaxInt16 * sy1 / h2p offset := n * totalSize - a16.SetIndex(offset, x0) - a16.SetIndex(offset+1, y0) + a16.SetIndex(offset, dx0) + a16.SetIndex(offset+1, dy0) a16.SetIndex(offset+2, u0) a16.SetIndex(offset+3, v0) for j, g := range gs { af32.SetIndex((offset+4)/2+j, g) } - a16.SetIndex(offset+oneSize, x1) - a16.SetIndex(offset+oneSize+1, y0) + a16.SetIndex(offset+oneSize, dx1) + a16.SetIndex(offset+oneSize+1, dy0) a16.SetIndex(offset+oneSize+2, u1) a16.SetIndex(offset+oneSize+3, v0) for j, g := range gs { af32.SetIndex((offset+oneSize+4)/2+j, g) } - a16.SetIndex(offset+2*oneSize, x0) - a16.SetIndex(offset+2*oneSize+1, y1) + a16.SetIndex(offset+2*oneSize, dx0) + a16.SetIndex(offset+2*oneSize+1, dy1) a16.SetIndex(offset+2*oneSize+2, u0) a16.SetIndex(offset+2*oneSize+3, v1) for j, g := range gs { af32.SetIndex((offset+2*oneSize+4)/2+j, g) } - a16.SetIndex(offset+3*oneSize, x1) - a16.SetIndex(offset+3*oneSize+1, y1) + a16.SetIndex(offset+3*oneSize, dx1) + a16.SetIndex(offset+3*oneSize+1, dy1) a16.SetIndex(offset+3*oneSize+2, u1) a16.SetIndex(offset+3*oneSize+3, v1) for j, g := range gs {