mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-13 22:47:26 +01:00
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:
parent
1f70307582
commit
cff64894cc
@ -47,7 +47,7 @@ type ColorM interface {
|
|||||||
IsIdentity() bool
|
IsIdentity() bool
|
||||||
ScaleOnly() bool
|
ScaleOnly() bool
|
||||||
At(i, j int) float32
|
At(i, j int) float32
|
||||||
Elements(body *[16]float32, translate *[4]float32)
|
Elements(body []float32, translate []float32)
|
||||||
Apply(clr color.Color) color.Color
|
Apply(clr color.Color) color.Color
|
||||||
|
|
||||||
// IsInvertible returns a boolean value indicating
|
// IsInvertible returns a boolean value indicating
|
||||||
@ -76,7 +76,7 @@ type ColorM interface {
|
|||||||
func colorMString(c ColorM) string {
|
func colorMString(c ColorM) string {
|
||||||
var b [16]float32
|
var b [16]float32
|
||||||
var t [4]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]]",
|
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[0], b[4], b[8], b[12], t[0],
|
||||||
b[1], b[5], b[9], b[13], t[1],
|
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[0] = 1
|
||||||
body[1] = 0
|
body[1] = 0
|
||||||
body[2] = 0
|
body[2] = 0
|
||||||
@ -278,7 +278,7 @@ func (c ColorMIdentity) Elements(body *[16]float32, translate *[4]float32) {
|
|||||||
translate[3] = 0
|
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[0] = c.scale[0]
|
||||||
body[1] = 0
|
body[1] = 0
|
||||||
body[2] = 0
|
body[2] = 0
|
||||||
@ -301,9 +301,9 @@ func (c colorMImplScale) Elements(body *[16]float32, translate *[4]float32) {
|
|||||||
translate[3] = 0
|
translate[3] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) Elements(body *[16]float32, translate *[4]float32) {
|
func (c *colorMImplBodyTranslate) Elements(body []float32, translate []float32) {
|
||||||
copy(body[:], c.body[:])
|
copy(body, c.body[:])
|
||||||
copy(translate[:], c.translate[:])
|
copy(translate, c.translate[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *colorMImplBodyTranslate) det() float32 {
|
func (c *colorMImplBodyTranslate) det() float32 {
|
||||||
@ -496,7 +496,7 @@ func ColorMSetElement(c ColorM, i, j int, element float32) ColorM {
|
|||||||
body: colorMIdentityBody,
|
body: colorMIdentityBody,
|
||||||
}
|
}
|
||||||
if !c.IsIdentity() {
|
if !c.IsIdentity() {
|
||||||
c.Elements(&newImpl.body, &newImpl.translate)
|
c.Elements(newImpl.body[:], newImpl.translate[:])
|
||||||
}
|
}
|
||||||
if j < (ColorMDim - 1) {
|
if j < (ColorMDim - 1) {
|
||||||
newImpl.body[i+j*(ColorMDim-1)] = element
|
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.
|
// Instead, cast `other` to a concrete type and call `Elements` functions of it.
|
||||||
switch other := other.(type) {
|
switch other := other.(type) {
|
||||||
case ColorMIdentity:
|
case ColorMIdentity:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
case colorMImplScale:
|
case colorMImplScale:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
case *colorMImplBodyTranslate:
|
case *colorMImplBodyTranslate:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
default:
|
default:
|
||||||
panic("affine: unexpected ColorM implementation")
|
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.
|
// Instead, cast `other` to a concrete type and call `Elements` functions of it.
|
||||||
switch other := other.(type) {
|
switch other := other.(type) {
|
||||||
case ColorMIdentity:
|
case ColorMIdentity:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
case colorMImplScale:
|
case colorMImplScale:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
case *colorMImplBodyTranslate:
|
case *colorMImplBodyTranslate:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
default:
|
default:
|
||||||
panic("affine: unexpected ColorM implementation")
|
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.
|
// Instead, cast `other` to a concrete type and call `Elements` functions of it.
|
||||||
switch other := other.(type) {
|
switch other := other.(type) {
|
||||||
case ColorMIdentity:
|
case ColorMIdentity:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
case colorMImplScale:
|
case colorMImplScale:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
case *colorMImplBodyTranslate:
|
case *colorMImplBodyTranslate:
|
||||||
other.Elements(&lhsb, &lhst)
|
other.Elements(lhsb[:], lhst[:])
|
||||||
default:
|
default:
|
||||||
panic("affine: unexpected ColorM implementation")
|
panic("affine: unexpected ColorM implementation")
|
||||||
}
|
}
|
||||||
|
@ -1252,7 +1252,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcs [graphics.Sh
|
|||||||
}
|
}
|
||||||
var esBody [16]float32
|
var esBody [16]float32
|
||||||
var esTranslate [4]float32
|
var esTranslate [4]float32
|
||||||
colorM.Elements(&esBody, &esTranslate)
|
colorM.Elements(esBody[:], esTranslate[:])
|
||||||
|
|
||||||
flattenUniforms = []float32{
|
flattenUniforms = []float32{
|
||||||
float32(screenWidth),
|
float32(screenWidth),
|
||||||
|
@ -36,7 +36,7 @@ const (
|
|||||||
type ColorM interface {
|
type ColorM interface {
|
||||||
IsIdentity() bool
|
IsIdentity() bool
|
||||||
At(i, j int) float32
|
At(i, j int) float32
|
||||||
Elements(body *[16]float32, translate *[4]float32)
|
Elements(body []float32, translate []float32)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Graphics interface {
|
type Graphics interface {
|
||||||
|
@ -834,7 +834,7 @@ func (g *Graphics) DrawTriangles(dstID graphicsdriver.ImageID, srcIDs [graphics.
|
|||||||
}
|
}
|
||||||
var esBody [16]float32
|
var esBody [16]float32
|
||||||
var esTranslate [4]float32
|
var esTranslate [4]float32
|
||||||
colorM.Elements(&esBody, &esTranslate)
|
colorM.Elements(esBody[:], esTranslate[:])
|
||||||
uniformVars = [][]float32{
|
uniformVars = [][]float32{
|
||||||
{float32(w), float32(h)},
|
{float32(w), float32(h)},
|
||||||
sourceSize,
|
sourceSize,
|
||||||
|
@ -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.
|
// ColorM's elements are immutable. It's OK to hold the reference without copying.
|
||||||
var esBody [16]float32
|
var esBody [16]float32
|
||||||
var esTranslate [4]float32
|
var esTranslate [4]float32
|
||||||
colorM.Elements(&esBody, &esTranslate)
|
colorM.Elements(esBody[:], esTranslate[:])
|
||||||
g.uniformVars = append(g.uniformVars, uniformVariable{
|
g.uniformVars = append(g.uniformVars, uniformVariable{
|
||||||
name: "color_matrix_body",
|
name: "color_matrix_body",
|
||||||
value: esBody[:],
|
value: esBody[:],
|
||||||
|
Loading…
Reference in New Issue
Block a user