graphics: Make matrices faster

This commit is contained in:
Hajime Hoshi 2017-01-20 00:37:51 +09:00
parent eec59d5cf8
commit dfcd9fc30c
10 changed files with 144 additions and 275 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.Elements()[i*affine.ColorMDim+j] return c.impl.UnsafeElements()[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.Elements()[i*affine.GeoMDim+j] return g.impl.UnsafeElements()[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

@ -14,39 +14,6 @@
package affine package affine
import (
"github.com/hajimehoshi/ebiten/internal/endian"
)
var identityValues = map[int]string{
ColorMDim: colorMValueString([ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
}),
GeoMDim: geoMValueString([GeoMDim - 1][GeoMDim]float64{
{1, 0, 0},
{0, 1, 0},
}),
}
func uint64ToBytes(value uint64) []uint8 {
result := make([]uint8, 8)
if endian.IsLittle() {
for i := 0; i < 8; i++ {
result[i] = uint8(value)
value >>= 8
}
} else {
for i := 7; 0 <= i; i-- {
result[i] = uint8(value)
value >>= 8
}
}
return result
}
type affine interface { type affine interface {
dim() int dim() int
element(i, j int) float64 element(i, j int) float64

View File

@ -1,54 +0,0 @@
// Copyright 2017 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.
// +build js
package affine
import (
"github.com/gopherjs/gopherjs/js"
)
func elements(values string, dim int) []float64 {
if values == "" {
result := make([]float64, dim*(dim-1))
for i := 0; i < dim-1; i++ {
result[i*dim+i] = 1
}
return result
}
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)
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 {
if values == "" {
values = identityValues[dim]
}
a := js.NewArrayBuffer([]uint8(values))
a8 := js.Global.Get("Uint8Array").New(a)
af64 := js.Global.Get("Float64Array").New(a)
af64.SetIndex(i*dim+j, value)
return string(a8.Interface().([]uint8))
}

View File

@ -1,70 +0,0 @@
// Copyright 2017 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.
// +build !js
package affine
import (
"math"
"github.com/hajimehoshi/ebiten/internal/endian"
)
func elements(values string, dim int) []float64 {
result := make([]float64, dim*(dim-1))
if values == "" {
for i := 0; i < dim-1; i++ {
result[i*dim+i] = 1
}
return result
}
if endian.IsLittle() {
for i := 0; i < len(values)/8; i++ {
v := uint64(0)
for k := 7; 0 <= k; k-- {
v <<= 8
v += uint64(values[i*8+k])
}
result[i] = math.Float64frombits(v)
}
} else {
for i := 0; i < len(values)/8; i++ {
v := uint64(0)
for k := 0; k < 8; k++ {
v <<= 8
v += uint64(values[i*8+k])
}
result[i] = 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 {
if values == "" {
values = identityValues[dim]
}
b := uint64ToBytes(math.Float64bits(value))
offset := 8 * (i*dim + j)
return values[:offset] + string(b) + values[offset+8:]
}

View File

@ -21,15 +21,14 @@ import (
// ColorMDim is a dimension of a ColorM. // ColorMDim is a dimension of a ColorM.
const ColorMDim = 5 const ColorMDim = 5
func colorMValueString(values [ColorMDim - 1][ColorMDim]float64) string { var (
b := make([]uint8, 0, (ColorMDim-1)*(ColorMDim)*8) colorMIdentityElements = []float64{
for i := 0; i < ColorMDim-1; i++ { 1, 0, 0, 0, 0,
for j := 0; j < ColorMDim; j++ { 0, 1, 0, 0, 0,
b = append(b, uint64ToBytes(math.Float64bits(values[i][j]))...) 0, 0, 1, 0, 0,
} 0, 0, 0, 1, 0,
} }
return string(b) )
}
// A ColorM represents a matrix to transform coloring when rendering an image. // A ColorM represents a matrix to transform coloring when rendering an image.
// //
@ -40,35 +39,56 @@ func colorMValueString(values [ColorMDim - 1][ColorMDim]float64) string {
// //
// The initial value is identity. // The initial value is identity.
type ColorM struct { type ColorM struct {
// When values is empty, this matrix is identity. // When elements is nil, this matrix is identity.
values string // elements is immutable and a new array must be created when updating.
elements []float64
} }
func (c *ColorM) dim() int { func (c *ColorM) dim() int {
return ColorMDim return ColorMDim
} }
func (c *ColorM) Elements() []float64 { func (c *ColorM) UnsafeElements() []float64 {
return elements(c.values, ColorMDim) if c.elements == nil {
c.elements = colorMIdentityElements
}
return c.elements
} }
func (c *ColorM) element(i, j int) float64 { func (c *ColorM) element(i, j int) float64 {
return c.Elements()[i*ColorMDim+j] if c.elements == nil {
c.elements = colorMIdentityElements
}
return c.elements[i*ColorMDim+j]
} }
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float64) { func (c *ColorM) SetElement(i, j int, element float64) {
c.values = setElement(c.values, ColorMDim, i, j, element) if c.elements == nil {
c.elements = colorMIdentityElements
}
es := make([]float64, len(c.elements))
copy(es, c.elements)
es[i*ColorMDim+j] = element
c.elements = es
} }
func (c *ColorM) Equals(other *ColorM) bool { func (c *ColorM) Equals(other *ColorM) bool {
if c.values == "" { if c.elements == nil {
c.values = identityValues[ColorMDim] if other.elements == nil {
return true
}
c.elements = colorMIdentityElements
} }
if other.values == "" { if other.elements == nil {
other.values = identityValues[ColorMDim] other.elements = colorMIdentityElements
} }
return c.values == other.values for i := range c.elements {
if c.elements[i] != other.elements[i] {
return false
}
}
return true
} }
// Concat multiplies a color matrix with the other color matrix. // Concat multiplies a color matrix with the other color matrix.
@ -88,24 +108,32 @@ 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) if c.elements == nil {
for i := 0; i < ColorMDim; i++ { c.elements = colorMIdentityElements
v[i] *= r
v[i+ColorMDim] *= g
v[i+ColorMDim*2] *= b
v[i+ColorMDim*3] *= a
} }
c.values = setElements(v, ColorMDim) es := make([]float64, len(c.elements))
copy(es, c.elements)
for i := 0; i < ColorMDim; i++ {
es[i] *= r
es[i+ColorMDim] *= g
es[i+ColorMDim*2] *= b
es[i+ColorMDim*3] *= a
}
c.elements = es
} }
// 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) {
v := elements(c.values, ColorMDim) if c.elements == nil {
v[4] += r c.elements = colorMIdentityElements
v[4+ColorMDim] += g }
v[4+ColorMDim*2] += b es := make([]float64, len(c.elements))
v[4+ColorMDim*3] += a copy(es, c.elements)
c.values = setElements(v, ColorMDim) es[4] += r
es[4+ColorMDim] += g
es[4+ColorMDim*2] += b
es[4+ColorMDim*3] += a
c.elements = es
} }
// RotateHue rotates the hue. // RotateHue rotates the hue.
@ -120,24 +148,24 @@ var (
// Cr: [-0.5 - 0.5] // Cr: [-0.5 - 0.5]
rgbToYCbCr = ColorM{ rgbToYCbCr = ColorM{
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{ elements: []float64{
{0.2990, 0.5870, 0.1140, 0, 0}, 0.2990, 0.5870, 0.1140, 0, 0,
{-0.1687, -0.3313, 0.5000, 0, 0}, -0.1687, -0.3313, 0.5000, 0, 0,
{0.5000, -0.4187, -0.0813, 0, 0}, 0.5000, -0.4187, -0.0813, 0, 0,
{0, 0, 0, 1, 0}, 0, 0, 0, 1, 0,
}), },
} }
yCbCrToRgb = ColorM{ yCbCrToRgb = ColorM{
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{ elements: []float64{
{1, 0, 1.40200, 0, 0}, 1, 0, 1.40200, 0, 0,
{1, -0.34414, -0.71414, 0, 0}, 1, -0.34414, -0.71414, 0, 0,
{1, 1.77200, 0, 0, 0}, 1, 1.77200, 0, 0, 0,
{0, 0, 0, 1, 0}, 0, 0, 0, 1, 0,
}), },
} }
) )
// ChangeHSV changes HSV (Hue-Saturation-Value) values. // ChangeHSV changes HSV (Hue-Saturation-Value) elements.
// hueTheta is a radian value to ratate hue. // hueTheta is a radian value to ratate hue.
// saturationScale is a value to scale saturation. // saturationScale is a value to scale saturation.
// valueScale is a value to scale value (a.k.a. brightness). // valueScale is a value to scale value (a.k.a. brightness).
@ -147,12 +175,12 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale
sin, cos := math.Sincos(hueTheta) sin, cos := math.Sincos(hueTheta)
c.Concat(rgbToYCbCr) c.Concat(rgbToYCbCr)
c.Concat(ColorM{ c.Concat(ColorM{
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{ elements: []float64{
{1, 0, 0, 0, 0}, 1, 0, 0, 0, 0,
{0, cos, -sin, 0, 0}, 0, cos, -sin, 0, 0,
{0, sin, cos, 0, 0}, 0, sin, cos, 0, 0,
{0, 0, 0, 1, 0}, 0, 0, 0, 1, 0,
}), },
}) })
s := saturationScale s := saturationScale
v := valueScale v := valueScale
@ -173,26 +201,16 @@ func Monochrome() ColorM {
// ScaleColor is deprecated as of 1.2.0-alpha. Use Scale instead. // ScaleColor is deprecated as of 1.2.0-alpha. Use Scale instead.
func ScaleColor(r, g, b, a float64) ColorM { func ScaleColor(r, g, b, a float64) ColorM {
return ColorM{ c := ColorM{}
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{ c.Scale(r, g, b, a)
{r, 0, 0, 0, 0}, return c
{0, g, 0, 0, 0},
{0, 0, b, 0, 0},
{0, 0, 0, a, 0},
}),
}
} }
// TranslateColor is deprecated as of 1.2.0-alpha. Use Translate instead. // TranslateColor is deprecated as of 1.2.0-alpha. Use Translate instead.
func TranslateColor(r, g, b, a float64) ColorM { func TranslateColor(r, g, b, a float64) ColorM {
return ColorM{ c := ColorM{}
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{ c.Translate(r, g, b, a)
{1, 0, 0, 0, r}, return c
{0, 1, 0, 0, g},
{0, 0, 1, 0, b},
{0, 0, 0, 1, a},
}),
}
} }
// RotateHue is deprecated as of 1.2.0-alpha. Use RotateHue member function instead. // RotateHue is deprecated as of 1.2.0-alpha. Use RotateHue member function instead.

View File

@ -21,39 +21,49 @@ import (
// GeoMDim is a dimension of a GeoM. // GeoMDim is a dimension of a GeoM.
const GeoMDim = 3 const GeoMDim = 3
func geoMValueString(values [GeoMDim - 1][GeoMDim]float64) string { var (
b := make([]uint8, 0, (GeoMDim-1)*(GeoMDim)*8) geoMIdentityElements = []float64{
for i := 0; i < GeoMDim-1; i++ { 1, 0, 0,
for j := 0; j < GeoMDim; j++ { 0, 1, 0,
b = append(b, uint64ToBytes(math.Float64bits(values[i][j]))...)
}
} }
return string(b) )
}
// A GeoM represents a matrix to transform geometry when rendering an image. // A GeoM represents a matrix to transform geometry when rendering an image.
// //
// The initial value is identity. // The initial value is identity.
type GeoM struct { type GeoM struct {
// When values is empty, this matrix is identity. // When elements is empty, this matrix is identity.
values string // elements is immutable and a new array must be created when updating.
elements []float64
} }
func (g *GeoM) dim() int { func (g *GeoM) dim() int {
return GeoMDim return GeoMDim
} }
func (g *GeoM) Elements() []float64 { func (g *GeoM) UnsafeElements() []float64 {
return elements(g.values, GeoMDim) if g.elements == nil {
g.elements = geoMIdentityElements
}
return g.elements
} }
func (g *GeoM) element(i, j int) float64 { func (g *GeoM) element(i, j int) float64 {
return g.Elements()[i*GeoMDim+j] if g.elements == nil {
g.elements = geoMIdentityElements
}
return g.elements[i*GeoMDim+j]
} }
// SetElement sets an element at (i, j). // SetElement sets an element at (i, j).
func (g *GeoM) SetElement(i, j int, element float64) { func (g *GeoM) SetElement(i, j int, element float64) {
g.values = setElement(g.values, GeoMDim, i, j, element) if g.elements == nil {
g.elements = geoMIdentityElements
}
es := make([]float64, len(g.elements))
copy(es, g.elements)
es[i*GeoMDim+j] = element
g.elements = es
} }
// Concat multiplies a geometry matrix with the other geometry matrix. // Concat multiplies a geometry matrix with the other geometry matrix.
@ -73,60 +83,58 @@ 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) if g.elements == nil {
for i := 0; i < GeoMDim; i++ { g.elements = geoMIdentityElements
v[i] *= x
v[i+GeoMDim] *= y
} }
g.values = setElements(v, GeoMDim) es := make([]float64, len(g.elements))
copy(es, g.elements)
for i := 0; i < GeoMDim; i++ {
es[i] *= x
es[i+GeoMDim] *= y
}
g.elements = es
} }
// 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) {
v := elements(g.values, GeoMDim) if g.elements == nil {
v[2] += tx g.elements = geoMIdentityElements
v[2+GeoMDim] += ty }
g.values = setElements(v, GeoMDim) es := make([]float64, len(g.elements))
copy(es, g.elements)
es[2] += tx
es[2+GeoMDim] += ty
g.elements = es
} }
// Rotate rotates the matrix by theta. // Rotate rotates the matrix by theta.
func (g *GeoM) Rotate(theta float64) { func (g *GeoM) Rotate(theta float64) {
sin, cos := math.Sincos(theta) sin, cos := math.Sincos(theta)
g.Concat(GeoM{ g.Concat(GeoM{
values: geoMValueString([GeoMDim - 1][GeoMDim]float64{ elements: []float64{
{cos, -sin, 0}, cos, -sin, 0,
{sin, cos, 0}, sin, cos, 0,
}), },
}) })
} }
// ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead. // ScaleGeo is deprecated as of 1.2.0-alpha. Use Scale instead.
func ScaleGeo(x, y float64) GeoM { func ScaleGeo(x, y float64) GeoM {
return GeoM{ g := GeoM{}
values: geoMValueString([GeoMDim - 1][GeoMDim]float64{ g.Scale(x, y)
{x, 0, 0}, return g
{0, y, 0},
}),
}
} }
// TranslateGeo is deprecated as of 1.2.0-alpha. Use Translate instead. // TranslateGeo is deprecated as of 1.2.0-alpha. Use Translate instead.
func TranslateGeo(tx, ty float64) GeoM { func TranslateGeo(tx, ty float64) GeoM {
return GeoM{ g := GeoM{}
values: geoMValueString([GeoMDim - 1][GeoMDim]float64{ g.Translate(tx, ty)
{1, 0, tx}, return g
{0, 1, ty},
}),
}
} }
// RotateGeo is deprecated as of 1.2.0-alpha. Use Rotate instead. // RotateGeo is deprecated as of 1.2.0-alpha. Use Rotate instead.
func RotateGeo(theta float64) GeoM { func RotateGeo(theta float64) GeoM {
sin, cos := math.Sincos(theta) g := GeoM{}
return GeoM{ g.Rotate(theta)
values: geoMValueString([GeoMDim - 1][GeoMDim]float64{ return g
{cos, -sin, 0},
{sin, cos, 0},
}),
}
} }

View File

@ -231,7 +231,7 @@ func (p *programContext) begin() error {
} }
e := [4][5]float32{} e := [4][5]float32{}
es := p.colorM.Elements() es := p.colorM.UnsafeElements()
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(es[i*affine.ColorMDim+j]) e[i][j] = float32(es[i*affine.ColorMDim+j])

View File

@ -28,7 +28,7 @@ func vertices(parts ImageParts, width, height int, geo *affine.GeoM) []float32 {
totalSize := graphics.QuadVertexSizeInBytes() / 4 totalSize := graphics.QuadVertexSizeInBytes() / 4
l := parts.Len() l := parts.Len()
vs := js.Global.Get("Float32Array").New(l * totalSize) vs := js.Global.Get("Float32Array").New(l * totalSize)
g := geo.Elements() g := geo.UnsafeElements()
g0 := g[0] g0 := g[0]
g1 := g[1] g1 := g[1]
g2 := g[3] g2 := g[3]

View File

@ -26,7 +26,7 @@ func vertices(parts ImageParts, width, height int, geo *affine.GeoM) []float32 {
totalSize := graphics.QuadVertexSizeInBytes() / 4 totalSize := graphics.QuadVertexSizeInBytes() / 4
l := parts.Len() l := parts.Len()
vs := make([]float32, l*totalSize) vs := make([]float32, l*totalSize)
g := geo.Elements() g := geo.UnsafeElements()
g0 := float32(g[0]) g0 := float32(g[0])
g1 := float32(g[1]) g1 := float32(g[1])
g2 := float32(g[3]) g2 := float32(g[3])