affine: Remove GeoM to avoid GeoM allocations on the heap

This commit is contained in:
Hajime Hoshi 2018-06-17 00:52:25 +09:00
parent 460c47a9eb
commit b4ace53596
8 changed files with 200 additions and 284 deletions

153
geom.go
View File

@ -16,54 +16,61 @@ package ebiten
import (
"fmt"
"github.com/hajimehoshi/ebiten/internal/affine"
"math"
)
// GeoMDim is a dimension of a GeoM.
const GeoMDim = affine.GeoMDim
const GeoMDim = 3
// A GeoM represents a matrix to transform geometry when rendering an image.
//
// The initial value is identity.
type GeoM struct {
impl *affine.GeoM
a_1 float64 // The actual 'a' value minus 1
b float64
c float64
d_1 float64 // The actual 'd' value minus 1
tx float64
ty float64
}
// String returns a string representation of GeoM.
func (g *GeoM) String() string {
a, b, c, d, tx, ty := g.impl.Elements()
return fmt.Sprintf("[[%f, %f, %f], [%f, %f, %f]]", a, b, tx, c, d, ty)
return fmt.Sprintf("[[%f, %f, %f], [%f, %f, %f]]", g.a_1+1, g.b, g.tx, g.c, g.d_1+1, g.ty)
}
// Reset resets the GeoM as identity.
func (g *GeoM) Reset() {
g.impl = nil
g.a_1 = 0
g.b = 0
g.c = 0
g.d_1 = 0
g.tx = 0
g.ty = 0
}
// Apply pre-multiplies a vector (x, y, 1) by the matrix.
// In other words, Apply calculates GeoM * (x, y, 1)^T.
// The return value is x and y values of the result vector.
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
return g.impl.Apply(x, y)
return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty
}
// Element returns a value of a matrix at (i, j).
func (g *GeoM) Element(i, j int) float64 {
a, b, c, d, tx, ty := g.impl.Elements()
switch {
case i == 0 && j == 0:
return a
return g.a_1 + 1
case i == 0 && j == 1:
return b
return g.b
case i == 0 && j == 2:
return tx
return g.tx
case i == 1 && j == 0:
return c
return g.c
case i == 1 && j == 1:
return d
return g.d_1 + 1
case i == 1 && j == 2:
return ty
return g.ty
default:
panic("ebiten: i or j is out of index")
}
@ -72,46 +79,126 @@ func (g *GeoM) Element(i, j int) float64 {
// Concat multiplies a geometry matrix with the other geometry matrix.
// This is same as muptiplying the matrix other and the matrix g in this order.
func (g *GeoM) Concat(other GeoM) {
g.impl = g.impl.Concat(other.impl)
a := (other.a_1+1)*(g.a_1+1) + other.b*g.c
b := (other.a_1+1)*g.b + other.b*(g.d_1+1)
tx := (other.a_1+1)*g.tx + other.b*g.ty + other.tx
c := other.c*(g.a_1+1) + (other.d_1+1)*g.c
d := other.c*g.b + (other.d_1+1)*(g.d_1+1)
ty := other.c*g.tx + (other.d_1+1)*g.ty + other.ty
g.a_1 = a - 1
g.b = b
g.c = c
g.d_1 = d - 1
g.tx = tx
g.ty = ty
}
// Add is deprecated as of 1.5.0-alpha.
// Note that this doesn't make sense as an operation for affine matrices.
func (g *GeoM) Add(other GeoM) {
g.impl = g.impl.Add(other.impl)
g.a_1 += other.a_1
g.b += other.b
g.c += other.c
g.d_1 += other.d_1
g.tx += other.tx
g.ty += other.ty
}
// Scale scales the matrix by (x, y).
func (g *GeoM) Scale(x, y float64) {
g.impl = g.impl.Scale(x, y)
a := (g.a_1 + 1) * x
b := g.b * x
tx := g.tx * x
c := g.c * y
d := (g.d_1 + 1) * y
ty := g.ty * y
g.a_1 = a - 1
g.b = b
g.c = c
g.d_1 = d - 1
g.tx = tx
g.ty = ty
}
// Translate translates the matrix by (tx, ty).
func (g *GeoM) Translate(tx, ty float64) {
g.impl = g.impl.Translate(tx, ty)
}
// IsInvertible returns a boolean value indicating
// whether the matrix g is invertible or not.
func (g *GeoM) IsInvertible() bool {
return g.impl.IsInvertible()
}
// Invert inverts the matrix.
// If g is not invertible, Invert panics.
func (g *GeoM) Invert() {
g.impl = g.impl.Invert()
g.tx += tx
g.ty += ty
}
// Rotate rotates the matrix by theta.
// The unit is radian.
func (g *GeoM) Rotate(theta float64) {
g.impl = g.impl.Rotate(theta)
sin, cos := math.Sincos(theta)
a := cos*(g.a_1+1) - sin*g.c
b := cos*g.b - sin*(g.d_1+1)
tx := cos*g.tx - sin*g.ty
c := sin*(g.a_1+1) + cos*g.c
d := sin*g.b + cos*(g.d_1+1)
ty := sin*g.tx + cos*g.ty
g.a_1 = a - 1
g.b = b
g.c = c
g.d_1 = d - 1
g.tx = tx
g.ty = ty
}
func (g *GeoM) det() float64 {
return (g.a_1+1)*(g.d_1+1) - g.b*g.c
}
// IsInvertible returns a boolean value indicating
// whether the matrix g is invertible or not.
func (g *GeoM) IsInvertible() bool {
return g.det() != 0
}
// Invert inverts the matrix.
// If g is not invertible, Invert panics.
func (g *GeoM) Invert() {
det := g.det()
if det == 0 {
panic("ebiten: g is not invertible")
}
a := (g.d_1 + 1) / det
b := -g.b / det
c := -g.c / det
d := (g.a_1 + 1) / det
tx := (-(g.d_1+1)*g.tx + g.b*g.ty) / det
ty := (g.c*g.tx + -(g.a_1+1)*g.ty) / det
g.a_1 = a - 1
g.b = b
g.c = c
g.d_1 = d - 1
g.tx = tx
g.ty = ty
}
// SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) {
g.impl = g.impl.SetElement(i, j, element)
switch {
case i == 0 && j == 0:
g.a_1 = element - 1
case i == 0 && j == 1:
g.b = element
case i == 0 && j == 2:
g.tx = element
case i == 1 && j == 0:
g.c = element
case i == 1 && j == 1:
g.d_1 = element - 1
case i == 1 && j == 2:
g.ty = element
default:
panic("ebiten: i or j is out of index")
}
}
// ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead.

View File

@ -19,7 +19,6 @@ import (
"image/color"
"runtime"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicsutil"
"github.com/hajimehoshi/ebiten/internal/opengl"
@ -212,7 +211,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
sy1 = r.Max.Y
}
}
geom := options.GeoM.impl
geom := &options.GeoM
if sx0 < 0 || sy0 < 0 {
dx := 0.0
dy := 0.0
@ -224,10 +223,9 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error {
dy = -float64(sy0)
sy0 = 0
}
var g *affine.GeoM
g = g.Translate(dx, dy)
g = g.Concat(geom)
geom = g
geom = &GeoM{}
geom.Translate(dx, dy)
geom.Concat(options.GeoM)
}
mode := opengl.CompositeMode(options.CompositeMode)

View File

@ -1,214 +0,0 @@
// Copyright 2014 Hajime Hoshi
//
// 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.
package affine
import (
"fmt"
"math"
)
// GeoMDim is a dimension of a GeoM.
const GeoMDim = 3
// A GeoM represents a matrix to transform geometry when rendering an image.
//
// The nil and initial value is identity.
type GeoM struct {
a_1 float64 // The actual 'a' value minus 1
b float64
c float64
d_1 float64 // The actual 'd' value minus 1
tx float64
ty float64
}
func (g *GeoM) Apply(x, y float64) (x2, y2 float64) {
if g == nil {
return x, y
}
return (g.a_1+1)*x + g.b*y + g.tx, g.c*x + (g.d_1+1)*y + g.ty
}
func (g *GeoM) Apply32(x, y float64) (x2, y2 float32) {
if g == nil {
return float32(x), float32(y)
}
return float32((g.a_1+1)*x + g.b*y + g.tx), float32(g.c*x + (g.d_1+1)*y + g.ty)
}
func (g *GeoM) Elements() (a, b, c, d, tx, ty float64) {
if g == nil {
return 1, 0, 0, 1, 0, 0
}
return g.a_1 + 1, g.b, g.c, g.d_1 + 1, g.tx, g.ty
}
// SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) *GeoM {
a, b, c, d, tx, ty := 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
if g != nil {
a, b, c, d, tx, ty = g.a_1+1, g.b, g.c, g.d_1+1, g.tx, g.ty
}
switch {
case i == 0 && j == 0:
a = element
case i == 0 && j == 1:
b = element
case i == 0 && j == 2:
tx = element
case i == 1 && j == 0:
c = element
case i == 1 && j == 1:
d = element
case i == 1 && j == 2:
ty = element
default:
panic(fmt.Sprintf("affine: i or j is out of index: (%d, %d)", i, j))
}
return &GeoM{
a_1: a - 1,
b: b,
c: c,
d_1: d - 1,
tx: tx,
ty: ty,
}
}
// Concat multiplies a geometry matrix with the other geometry matrix.
// This is same as muptiplying the matrix other and the matrix g in this order.
func (g *GeoM) Concat(other *GeoM) *GeoM {
if g == nil {
return other
}
if other == nil {
return g
}
return &GeoM{
a_1: (other.a_1+1)*(g.a_1+1) + other.b*g.c - 1,
b: (other.a_1+1)*g.b + other.b*(g.d_1+1),
tx: (other.a_1+1)*g.tx + other.b*g.ty + other.tx,
c: other.c*(g.a_1+1) + (other.d_1+1)*g.c,
d_1: other.c*g.b + (other.d_1+1)*(g.d_1+1) - 1,
ty: other.c*g.tx + (other.d_1+1)*g.ty + other.ty,
}
}
// Add is deprecated.
func (g *GeoM) Add(other *GeoM) *GeoM {
if g == nil {
g = &GeoM{}
}
if other == nil {
other = &GeoM{}
}
return &GeoM{
a_1: (g.a_1 + 1) + (other.a_1 + 1) - 1,
b: g.b + other.b,
c: g.c + other.c,
d_1: (g.d_1 + 1) + (other.d_1 + 1) - 1,
tx: g.tx + other.tx,
ty: g.ty + other.ty,
}
}
// Scale scales the matrix by (x, y).
func (g *GeoM) Scale(x, y float64) *GeoM {
if g == nil {
return &GeoM{
a_1: x - 1,
b: 0,
c: 0,
d_1: y - 1,
}
}
return &GeoM{
a_1: (g.a_1+1)*x - 1,
b: g.b * x,
tx: g.tx * x,
c: g.c * y,
d_1: (g.d_1+1)*y - 1,
ty: g.ty * y,
}
}
// Translate translates the matrix by (x, y).
func (g *GeoM) Translate(tx, ty float64) *GeoM {
if g == nil {
return &GeoM{
tx: tx,
ty: ty,
}
}
return &GeoM{
a_1: g.a_1,
b: g.b,
c: g.c,
d_1: g.d_1,
tx: g.tx + tx,
ty: g.ty + ty,
}
}
// Rotate rotates the matrix by theta.
func (g *GeoM) Rotate(theta float64) *GeoM {
sin, cos := math.Sincos(theta)
if g == nil {
return &GeoM{
a_1: cos - 1,
b: -sin,
c: sin,
d_1: cos - 1,
}
}
return &GeoM{
a_1: cos*(g.a_1+1) - sin*g.c - 1,
b: cos*g.b - sin*(g.d_1+1),
tx: cos*g.tx - sin*g.ty,
c: sin*(g.a_1+1) + cos*g.c,
d_1: sin*g.b + cos*(g.d_1+1) - 1,
ty: sin*g.tx + cos*g.ty,
}
}
func (g *GeoM) det() float64 {
if g == nil {
return 1
}
return (g.a_1+1)*(g.d_1+1) - g.b*g.c
}
func (g *GeoM) IsInvertible() bool {
return g.det() != 0
}
func (g *GeoM) Invert() *GeoM {
if g == nil {
return nil
}
det := g.det()
if det == 0 {
panic("affine: g is not invertible")
}
return &GeoM{
a_1: ((g.d_1 + 1) / det) - 1,
b: -g.b / det,
c: -g.c / det,
d_1: ((g.a_1 + 1) / det) - 1,
tx: (-(g.d_1+1)*g.tx + g.b*g.ty) / det,
ty: (g.c*g.tx + -(g.a_1+1)*g.ty) / det,
}
}

View File

@ -15,7 +15,6 @@
package graphicsutil
import (
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/opengl"
)
@ -44,7 +43,11 @@ func (v *verticesBackend) sliceForOneQuad() []float32 {
return s
}
func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geo *affine.GeoM) []float32 {
type GeoM interface {
Apply(x, y float64) (x2, y2 float64)
}
func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geom GeoM) []float32 {
if sx0 >= sx1 || sy0 >= sy1 {
return nil
}
@ -71,10 +74,13 @@ func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geo *affine.GeoM) [
hf := float32(h)
u0, v0, u1, v1 := float32(sx0)/wf, float32(sy0)/hf, float32(sx1)/wf, float32(sy1)/hf
x, y := geo.Apply32(x0, y0)
x, y := x0, y0
if geom != nil {
x, y = geom.Apply(x, y)
}
// Vertex coordinates
vs[0] = x
vs[1] = y
vs[0] = float32(x)
vs[1] = float32(y)
// Texture coordinates: first 2 values indicates the actual coodinate, and
// the second indicates diagonally opposite coodinates.
@ -85,25 +91,34 @@ func QuadVertices(width, height int, sx0, sy0, sx1, sy1 int, geo *affine.GeoM) [
vs[5] = v1
// and the same for the other three coordinates
x, y = geo.Apply32(x1, y0)
vs[6] = x
vs[7] = y
x, y = x1, y0
if geom != nil {
x, y = geom.Apply(x, y)
}
vs[6] = float32(x)
vs[7] = float32(y)
vs[8] = u1
vs[9] = v0
vs[10] = u0
vs[11] = v1
x, y = geo.Apply32(x0, y1)
vs[12] = x
vs[13] = y
x, y = x0, y1
if geom != nil {
x, y = geom.Apply(x, y)
}
vs[12] = float32(x)
vs[13] = float32(y)
vs[14] = u0
vs[15] = v1
vs[16] = u1
vs[17] = v0
x, y = geo.Apply32(x1, y1)
vs[18] = x
vs[19] = y
x, y = x1, y1
if geom != nil {
x, y = geom.Apply(x, y)
}
vs[18] = float32(x)
vs[19] = float32(y)
vs[20] = u1
vs[21] = v1
vs[22] = u0

View File

@ -122,6 +122,17 @@ var (
quadIndices = []uint16{0, 1, 2, 1, 2, 3}
)
type geoM struct {
scaleX float64
scaleY float64
tx float64
ty float64
}
func (g *geoM) Apply(x, y float64) (float64, float64) {
return x*g.scaleX + g.tx, y*g.scaleY + g.ty
}
// ReplacePixels replaces the image pixels with the given pixels slice.
//
// If pixels is nil, ReplacePixels clears the specified reagion.
@ -146,10 +157,13 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
// However, that's ok since this image will be stale or have updated pixel data
// and this image can be restored without dummyImage.
w, h := dummyImage.Size()
geom := (*affine.GeoM)(nil).Scale(float64(width)/float64(w), float64(height)/float64(h))
geom = geom.Translate(float64(x), float64(y))
colorm := (*affine.ColorM)(nil).Scale(0, 0, 0, 0)
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, geom)
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{
scaleX: float64(width) / float64(w),
scaleY: float64(height) / float64(h),
tx: float64(x),
ty: float64(y),
})
i.image.DrawImage(dummyImage.image, vs, quadIndices, colorm, opengl.CompositeModeCopy, graphics.FilterNearest)
}

View File

@ -22,7 +22,6 @@ import (
"testing"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicsutil"
"github.com/hajimehoshi/ebiten/internal/opengl"
@ -257,6 +256,15 @@ func TestRestoreOverrideSource(t *testing.T) {
}
}
type geoM struct {
tx float64
ty float64
}
func (g *geoM) Apply(x, y float64) (x2, y2 float64) {
return x + g.tx, y + g.ty
}
func TestRestoreComplexGraph(t *testing.T) {
const (
w = 4
@ -299,23 +307,23 @@ func TestRestoreComplexGraph(t *testing.T) {
img1.Dispose()
img0.Dispose()
}()
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
img3.DrawImage(img0, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
img3.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
img4.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(2, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{2, 0})
img4.DrawImage(img2, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
img5.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
img6.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
img6.DrawImage(img4, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(0, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{0, 0})
img7.DrawImage(img2, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(2, 0))
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{2, 0})
img7.DrawImage(img3, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
if err := ResolveStaleImages(); err != nil {
t.Fatal(err)
@ -408,7 +416,7 @@ func TestRestoreRecursive(t *testing.T) {
img1.Dispose()
img0.Dispose()
}()
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, (*affine.GeoM)(nil).Translate(1, 0))
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, &geoM{1, 0})
img1.DrawImage(img0, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
img0.DrawImage(img1, vs, quadIndices, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest)
if err := ResolveStaleImages(); err != nil {

View File

@ -135,7 +135,7 @@ func (i *Image) Size() (width, height int) {
return i.width, i.height
}
func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom *affine.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
func (i *Image) DrawImage(img *Image, sx0, sy0, sx1, sy1 int, geom graphicsutil.GeoM, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) {
backendsM.Lock()
defer backendsM.Unlock()

View File

@ -21,7 +21,6 @@ import (
"testing"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/opengl"
. "github.com/hajimehoshi/ebiten/internal/shareable"
@ -46,6 +45,15 @@ func TestMain(m *testing.M) {
const bigSize = 2049
type geoM struct {
tx float64
ty float64
}
func (g *geoM) Apply(x, y float64) (x2, y2 float64) {
return x + g.tx, y + g.ty
}
func TestEnsureNotShared(t *testing.T) {
// Create img1 and img2 with this size so that the next images are allocated
// with non-upper-left location.
@ -85,7 +93,7 @@ func TestEnsureNotShared(t *testing.T) {
dy1 = size * 3 / 4
)
// img4.ensureNotShared() should be called.
geom := (*affine.GeoM)(nil).Translate(size/4, size/4)
geom := &geoM{size / 4, size / 4}
img4.DrawImage(img3, 0, 0, size/2, size/2, geom, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
for j := 0; j < size; j++ {