From 8c9ec8fc9f580a893f456a9dbb7962001ed8586a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 4 Sep 2021 17:55:47 +0900 Subject: [PATCH] interna/affine: Avoid UnsafeElements at At Updates #1796 --- colorm.go | 2 +- internal/affine/colorm.go | 27 +++++++++++++++++++-------- internal/affine/colorm_test.go | 4 ++-- internal/driver/graphics.go | 1 + internal/mipmap/mipmap.go | 11 ++++------- 5 files changed, 27 insertions(+), 18 deletions(-) diff --git a/colorm.go b/colorm.go index ac24568e6..37611a502 100644 --- a/colorm.go +++ b/colorm.go @@ -99,7 +99,7 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale // Element returns a value of a matrix at (i, j). func (c *ColorM) Element(i, j int) float64 { - return float64(affine.ColorMElement(c.affineColorM(), i, j)) + return float64(c.affineColorM().At(i, j)) } // SetElement sets an element at (i, j). diff --git a/internal/affine/colorm.go b/internal/affine/colorm.go index 61338246b..3ecfa7174 100644 --- a/internal/affine/colorm.go +++ b/internal/affine/colorm.go @@ -44,6 +44,7 @@ var ( type ColorM interface { IsIdentity() bool ScaleOnly() bool + At(i, j int) float32 UnsafeElements(body *[16]float32, translate *[4]float32) Apply(clr color.Color) color.Color @@ -454,15 +455,25 @@ func (c *colorMImplBodyTranslate) Invert() ColorM { return m } -// ColorMElement returns a value of a matrix at (i, j). -func ColorMElement(c ColorM, i, j int) float32 { - var b [16]float32 - var t [4]float32 - c.UnsafeElements(&b, &t) - if j < ColorMDim-1 { - return b[i+j*(ColorMDim-1)] +func (c ColorMIdentity) At(i, j int) float32 { + if i == j { + return 1 } - return t[i] + return 0 +} + +func (c colorMImplScale) At(i, j int) float32 { + if i == j { + return c.scale[i] + } + return 0 +} + +func (c *colorMImplBodyTranslate) At(i, j int) float32 { + if j < ColorMDim-1 { + return c.body[i+j*(ColorMDim-1)] + } + return c.translate[i] } // ColorMSetElement sets an element at (i, j). diff --git a/internal/affine/colorm_test.go b/internal/affine/colorm_test.go index 6ba7fc704..c6157b267 100644 --- a/internal/affine/colorm_test.go +++ b/internal/affine/colorm_test.go @@ -169,8 +169,8 @@ func abs(x float32) float32 { func equalWithDelta(a, b ColorM, delta float32) bool { for j := 0; j < 5; j++ { for i := 0; i < 4; i++ { - ea := ColorMElement(a, i, j) - eb := ColorMElement(b, i, j) + ea := a.At(i, j) + eb := b.At(i, j) if abs(ea-eb) > delta { return false } diff --git a/internal/driver/graphics.go b/internal/driver/graphics.go index 65bb869d1..411e83b53 100644 --- a/internal/driver/graphics.go +++ b/internal/driver/graphics.go @@ -35,6 +35,7 @@ const ( type ColorM interface { IsIdentity() bool + At(i, j int) float32 UnsafeElements(body *[16]float32, translate *[4]float32) } diff --git a/internal/mipmap/mipmap.go b/internal/mipmap/mipmap.go index bf56a1404..7d1ae8d24 100644 --- a/internal/mipmap/mipmap.go +++ b/internal/mipmap/mipmap.go @@ -124,13 +124,10 @@ func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageNum]*Mipmap, vertices [ } if colorm.ScaleOnly() { - var body [16]float32 - var translate [4]float32 - colorm.UnsafeElements(&body, &translate) - cr := body[0] - cg := body[5] - cb := body[10] - ca := body[15] + cr := colorm.At(0, 0) + cg := colorm.At(1, 1) + cb := colorm.At(2, 2) + ca := colorm.At(3, 3) colorm = affine.ColorMIdentity{} const n = graphics.VertexFloatNum for i := 0; i < len(vertices)/n; i++ {