internal/affine: refactoring: use slices instead of array pointers

This doesn't change the performance at the test using ColorM:

```
name           old time/op  new time/op  delta
ColorMScale-8  1.11µs ±43%  1.23µs ±70%   ~     (p=1.000 n=5+5)
```
This commit is contained in:
Hajime Hoshi 2022-09-30 22:18:36 +09:00
parent 1f70307582
commit cff64894cc
5 changed files with 21 additions and 21 deletions

View File

@ -47,7 +47,7 @@ type ColorM interface {
IsIdentity() bool
ScaleOnly() bool
At(i, j int) float32
Elements(body *[16]float32, translate *[4]float32)
Elements(body []float32, translate []float32)
Apply(clr color.Color) color.Color
// IsInvertible returns a boolean value indicating
@ -76,7 +76,7 @@ type ColorM interface {
func colorMString(c ColorM) string {
var b [16]float32
var t [4]float32
c.Elements(&b, &t)
c.Elements(b[:], t[:])
return fmt.Sprintf("[[%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f], [%f, %f, %f, %f, %f]]",
b[0], b[4], b[8], b[12], t[0],
b[1], b[5], b[9], b[13], t[1],
@ -255,7 +255,7 @@ func (c *colorMImplBodyTranslate) Apply(clr color.Color) color.Color {
}
}
func (c ColorMIdentity) Elements(body *[16]float32, translate *[4]float32) {
func (c ColorMIdentity) Elements(body []float32, translate []float32) {
body[0] = 1
body[1] = 0
body[2] = 0
@ -278,7 +278,7 @@ func (c ColorMIdentity) Elements(body *[16]float32, translate *[4]float32) {
translate[3] = 0
}
func (c colorMImplScale) Elements(body *[16]float32, translate *[4]float32) {
func (c colorMImplScale) Elements(body []float32, translate []float32) {
body[0] = c.scale[0]
body[1] = 0
body[2] = 0
@ -301,9 +301,9 @@ func (c colorMImplScale) Elements(body *[16]float32, translate *[4]float32) {
translate[3] = 0
}
func (c *colorMImplBodyTranslate) Elements(body *[16]float32, translate *[4]float32) {
copy(body[:], c.body[:])
copy(translate[:], c.translate[:])
func (c *colorMImplBodyTranslate) Elements(body []float32, translate []float32) {
copy(body, c.body[:])
copy(translate, c.translate[:])
}
func (c *colorMImplBodyTranslate) det() float32 {
@ -496,7 +496,7 @@ func ColorMSetElement(c ColorM, i, j int, element float32) ColorM {
body: colorMIdentityBody,
}
if !c.IsIdentity() {
c.Elements(&newImpl.body, &newImpl.translate)
c.Elements(newImpl.body[:], newImpl.translate[:])
}
if j < (ColorMDim - 1) {
newImpl.body[i+j*(ColorMDim-1)] = element
@ -539,11 +539,11 @@ func (c *colorMImplBodyTranslate) Equals(other ColorM) bool {
// Instead, cast `other` to a concrete type and call `Elements` functions of it.
switch other := other.(type) {
case ColorMIdentity:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
case colorMImplScale:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
case *colorMImplBodyTranslate:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
default:
panic("affine: unexpected ColorM implementation")
}
@ -571,11 +571,11 @@ func (c colorMImplScale) Concat(other ColorM) ColorM {
// Instead, cast `other` to a concrete type and call `Elements` functions of it.
switch other := other.(type) {
case ColorMIdentity:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
case colorMImplScale:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
case *colorMImplBodyTranslate:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
default:
panic("affine: unexpected ColorM implementation")
}
@ -604,11 +604,11 @@ func (c *colorMImplBodyTranslate) Concat(other ColorM) ColorM {
// Instead, cast `other` to a concrete type and call `Elements` functions of it.
switch other := other.(type) {
case ColorMIdentity:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
case colorMImplScale:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
case *colorMImplBodyTranslate:
other.Elements(&lhsb, &lhst)
other.Elements(lhsb[:], lhst[:])
default:
panic("affine: unexpected ColorM implementation")
}

View File

@ -1252,7 +1252,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcs [graphics.Sh
}
var esBody [16]float32
var esTranslate [4]float32
colorM.Elements(&esBody, &esTranslate)
colorM.Elements(esBody[:], esTranslate[:])
flattenUniforms = []float32{
float32(screenWidth),

View File

@ -36,7 +36,7 @@ const (
type ColorM interface {
IsIdentity() bool
At(i, j int) float32
Elements(body *[16]float32, translate *[4]float32)
Elements(body []float32, translate []float32)
}
type Graphics interface {

View File

@ -834,7 +834,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.
}
var esBody [16]float32
var esTranslate [4]float32
colorM.Elements(&esBody, &esTranslate)
colorM.Elements(esBody[:], esTranslate[:])
uniformVars = [][]float32{
{float32(w), float32(h)},
sourceSize,

View File

@ -224,7 +224,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.
// ColorM's elements are immutable. It's OK to hold the reference without copying.
var esBody [16]float32
var esTranslate [4]float32
colorM.Elements(&esBody, &esTranslate)
colorM.Elements(esBody[:], esTranslate[:])
g.uniformVars = append(g.uniformVars, uniformVariable{
name: "color_matrix_body",
value: esBody[:],