ebiten: bug fix: DrawImage/DrawRectShader unexpectedly modified the given options

Closes #2733
This commit is contained in:
Hajime Hoshi 2023-08-27 01:40:37 +09:00
parent dec08b9e4e
commit 3678b20c5d
2 changed files with 35 additions and 5 deletions

View File

@ -240,10 +240,11 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
}
filter := builtinshader.Filter(options.Filter)
geoM := options.GeoM
if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 {
options.GeoM.Translate(float64(offsetX), float64(offsetY))
geoM.Translate(float64(offsetX), float64(offsetY))
}
a, b, c, d, tx, ty := options.GeoM.elements32()
a, b, c, d, tx, ty := geoM.elements32()
bounds := img.Bounds()
sx0, sy0 := img.adjustPosition(bounds.Min.X, bounds.Min.Y)
@ -269,7 +270,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
})
}
i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, false, canSkipMipmap(options.GeoM, filter), false)
i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, false, canSkipMipmap(geoM, filter), false)
}
// Vertex represents a vertex passed to DrawTriangles.
@ -792,10 +793,11 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
}
}
geoM := options.GeoM
if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 {
options.GeoM.Translate(float64(offsetX), float64(offsetY))
geoM.Translate(float64(offsetX), float64(offsetY))
}
a, b, c, d, tx, ty := options.GeoM.elements32()
a, b, c, d, tx, ty := geoM.elements32()
cr, cg, cb, ca := options.ColorScale.elements()
vs := i.ensureTmpVertices(4 * graphics.VertexFloatCount)
graphics.QuadVertices(vs, float32(sx), float32(sy), float32(sx+width), float32(sy+height), a, b, c, d, tx, ty, cr, cg, cb, ca)

View File

@ -4203,3 +4203,31 @@ func TestImageDrawTrianglesShaderWithTooBigIndex(t *testing.T) {
}
dst.DrawTrianglesShader(vs, is, shader, nil)
}
// Issue #2733
func TestImageGeoMAfterDraw(t *testing.T) {
src := ebiten.NewImage(1, 1)
dst := ebiten.NewImageWithOptions(image.Rect(-1, -1, 0, 0), nil)
op0 := &ebiten.DrawImageOptions{}
dst.DrawImage(src, op0)
if x, y := op0.GeoM.Apply(0, 0); x != 0 || y != 0 {
t.Errorf("got: (%0.2f, %0.2f), want: (0, 0)", x, y)
}
s, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(1)
}
`))
if err != nil {
t.Fatal(err)
}
op1 := &ebiten.DrawRectShaderOptions{}
dst.DrawRectShader(1, 1, s, op1)
if x, y := op1.GeoM.Apply(0, 0); x != 0 || y != 0 {
t.Errorf("got: (%0.2f, %0.2f), want: (0, 0)", x, y)
}
}