graphics: Improve matrices speed

This commit is contained in:
Hajime Hoshi 2017-01-19 12:07:31 +09:00
parent 9087269212
commit 71a4465c6f
8 changed files with 84 additions and 44 deletions

View File

@ -72,7 +72,7 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale
// Element returns a value of a matrix at (i, j). // Element returns a value of a matrix at (i, j).
func (c *ColorM) Element(i, j int) float64 { func (c *ColorM) Element(i, j int) float64 {
return c.impl.Element(i, j) return c.impl.Elements()[i*affine.ColorMDim+j]
} }
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).

View File

@ -30,7 +30,7 @@ type GeoM struct {
// Element returns a value of a matrix at (i, j). // Element returns a value of a matrix at (i, j).
func (g *GeoM) Element(i, j int) float64 { func (g *GeoM) Element(i, j int) float64 {
return g.impl.Element(i, j) return g.impl.Elements()[i*affine.GeoMDim+j]
} }
// Concat multiplies a geometry matrix with the other geometry matrix. // Concat multiplies a geometry matrix with the other geometry matrix.

View File

@ -49,7 +49,7 @@ func uint64ToBytes(value uint64) []uint8 {
type affine interface { type affine interface {
dim() int dim() int
Element(i, j int) float64 element(i, j int) float64
SetElement(i, j int, element float64) SetElement(i, j int, element float64)
} }
@ -62,7 +62,7 @@ func add(lhs, rhs, result affine) {
for i := 0; i < dim-1; i++ { for i := 0; i < dim-1; i++ {
for j := 0; j < dim; j++ { for j := 0; j < dim; j++ {
v := lhs.Element(i, j) + rhs.Element(i, j) v := lhs.element(i, j) + rhs.element(i, j)
result.SetElement(i, j, v) result.SetElement(i, j, v)
} }
} }
@ -78,11 +78,11 @@ func mul(lhs, rhs, result affine) {
for j := 0; j < dim; j++ { for j := 0; j < dim; j++ {
element := float64(0) element := float64(0)
for k := 0; k < dim-1; k++ { for k := 0; k < dim-1; k++ {
element += lhs.Element(i, k) * element += lhs.element(i, k) *
rhs.Element(k, j) rhs.element(k, j)
} }
if j == dim-1 { if j == dim-1 {
element += lhs.Element(i, j) element += lhs.element(i, j)
} }
result.SetElement(i, j, element) result.SetElement(i, j, element)
} }

View File

@ -20,16 +20,26 @@ import (
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
) )
func element(values string, dim int, i, j int) float64 { func elements(values string, dim int) []float64 {
if values == "" { if values == "" {
if i == j { result := make([]float64, dim*(dim-1))
return 1 for i := 0; i < dim-1; i++ {
result[i*dim+i] = 1
} }
return 0 return result
} }
a := js.NewArrayBuffer([]uint8(values)) a := js.NewArrayBuffer([]uint8(values))
return js.Global.Get("Float64Array").New(a).Interface().([]float64)
}
func setElements(values []float64, dim int) string {
a := js.NewArrayBuffer(make([]uint8, len(values)*8))
a8 := js.Global.Get("Uint8Array").New(a)
af64 := js.Global.Get("Float64Array").New(a) af64 := js.Global.Get("Float64Array").New(a)
return af64.Index(i*dim + j).Float() for i, v := range values {
af64.SetIndex(i, v)
}
return string(a8.Interface().([]uint8))
} }
func setElement(values string, dim int, i, j int, value float64) string { func setElement(values string, dim int, i, j int, value float64) string {

View File

@ -22,27 +22,42 @@ import (
"github.com/hajimehoshi/ebiten/internal/endian" "github.com/hajimehoshi/ebiten/internal/endian"
) )
func element(values string, dim int, i, j int) float64 { func elements(values string, dim int) []float64 {
result := make([]float64, dim*(dim-1))
if values == "" { if values == "" {
if i == j { for i := 0; i < dim-1; i++ {
return 1 result[i*dim+i] = 1
} }
return 0 return result
} }
offset := 8 * (i*dim + j)
v := uint64(0)
if endian.IsLittle() { if endian.IsLittle() {
for k := 7; 0 <= k; k-- { for i := 0; i < len(values)/8; i++ {
v <<= 8 v := uint64(0)
v += uint64(values[offset+k]) for k := 7; 0 <= k; k-- {
v <<= 8
v += uint64(values[i*8+k])
}
result[i] = math.Float64frombits(v)
} }
} else { } else {
for k := 0; k < 8; k++ { for i := 0; i < len(values)/8; i++ {
v <<= 8 v := uint64(0)
v += uint64(values[offset+k]) for k := 0; k < 8; k++ {
v <<= 8
v += uint64(values[i*8+k])
}
result[i] = math.Float64frombits(v)
} }
} }
return math.Float64frombits(v) return result
}
func setElements(values []float64, dim int) string {
result := make([]uint8, len(values)*8)
for i, v := range values {
copy(result[i*8:(i+1)*8], uint64ToBytes(math.Float64bits(v)))
}
return string(result)
} }
func setElement(values string, dim int, i, j int, value float64) string { func setElement(values string, dim int, i, j int, value float64) string {

View File

@ -48,9 +48,12 @@ func (c *ColorM) dim() int {
return ColorMDim return ColorMDim
} }
// Element returns a value of a matrix at (i, j). func (c *ColorM) Elements() []float64 {
func (c *ColorM) Element(i, j int) float64 { return elements(c.values, ColorMDim)
return element(c.values, ColorMDim, i, j) }
func (c *ColorM) element(i, j int) float64 {
return c.Elements()[i*ColorMDim+j]
} }
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).
@ -85,20 +88,24 @@ func (c *ColorM) Add(other ColorM) {
// Scale scales the matrix by (r, g, b, a). // Scale scales the matrix by (r, g, b, a).
func (c *ColorM) Scale(r, g, b, a float64) { func (c *ColorM) Scale(r, g, b, a float64) {
v := elements(c.values, ColorMDim)
for i := 0; i < ColorMDim; i++ { for i := 0; i < ColorMDim; i++ {
c.SetElement(0, i, c.Element(0, i)*r) v[i] *= r
c.SetElement(1, i, c.Element(1, i)*g) v[i+ColorMDim] *= g
c.SetElement(2, i, c.Element(2, i)*b) v[i+ColorMDim*2] *= b
c.SetElement(3, i, c.Element(3, i)*a) v[i+ColorMDim*3] *= a
} }
c.values = setElements(v, ColorMDim)
} }
// Translate translates the matrix by (r, g, b, a). // Translate translates the matrix by (r, g, b, a).
func (c *ColorM) Translate(r, g, b, a float64) { func (c *ColorM) Translate(r, g, b, a float64) {
c.SetElement(0, 4, c.Element(0, 4)+r) v := elements(c.values, ColorMDim)
c.SetElement(1, 4, c.Element(1, 4)+g) v[4] += r
c.SetElement(2, 4, c.Element(2, 4)+b) v[4+ColorMDim] += g
c.SetElement(3, 4, c.Element(3, 4)+a) v[4+ColorMDim*2] += b
v[4+ColorMDim*3] += a
c.values = setElements(v, ColorMDim)
} }
// RotateHue rotates the hue. // RotateHue rotates the hue.

View File

@ -43,9 +43,12 @@ func (g *GeoM) dim() int {
return GeoMDim return GeoMDim
} }
// Element returns a value of a matrix at (i, j). func (g *GeoM) Elements() []float64 {
func (g *GeoM) Element(i, j int) float64 { return elements(g.values, GeoMDim)
return element(g.values, GeoMDim, i, j) }
func (g *GeoM) element(i, j int) float64 {
return g.Elements()[i*GeoMDim+j]
} }
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).
@ -70,16 +73,20 @@ func (g *GeoM) Add(other GeoM) {
// Scale scales the matrix by (x, y). // Scale scales the matrix by (x, y).
func (g *GeoM) Scale(x, y float64) { func (g *GeoM) Scale(x, y float64) {
v := elements(g.values, GeoMDim)
for i := 0; i < GeoMDim; i++ { for i := 0; i < GeoMDim; i++ {
g.SetElement(0, i, g.Element(0, i)*x) v[i] *= x
g.SetElement(1, i, g.Element(1, i)*y) v[i+GeoMDim] *= y
} }
g.values = setElements(v, GeoMDim)
} }
// Translate translates the matrix by (x, y). // Translate translates the matrix by (x, y).
func (g *GeoM) Translate(tx, ty float64) { func (g *GeoM) Translate(tx, ty float64) {
g.SetElement(0, 2, g.Element(0, 2)+tx) v := elements(g.values, GeoMDim)
g.SetElement(1, 2, g.Element(1, 2)+ty) v[2] += tx
v[2+GeoMDim] += ty
g.values = setElements(v, GeoMDim)
} }
// Rotate rotates the matrix by theta. // Rotate rotates the matrix by theta.

View File

@ -231,9 +231,10 @@ func (p *programContext) begin() error {
} }
e := [4][5]float32{} e := [4][5]float32{}
es := p.colorM.Elements()
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {
e[i][j] = float32(p.colorM.Element(i, j)) e[i][j] = float32(es[i*affine.ColorMDim+j])
} }
} }