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 65fb8fc222
commit c87ce07212
2 changed files with 35 additions and 5 deletions

View File

@ -239,10 +239,11 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
} }
filter := builtinshader.Filter(options.Filter) filter := builtinshader.Filter(options.Filter)
geoM := options.GeoM
if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 { 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() bounds := img.Bounds()
sx0, sy0 := img.adjustPosition(bounds.Min.X, bounds.Min.Y) sx0, sy0 := img.adjustPosition(bounds.Min.X, bounds.Min.Y)
@ -268,7 +269,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. // Vertex represents a vertex passed to DrawTriangles.
@ -740,10 +741,11 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
sr = img.adjustedRegion() sr = img.adjustedRegion()
} }
geoM := options.GeoM
if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 { 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() cr, cg, cb, ca := options.ColorScale.elements()
vs := i.ensureTmpVertices(4 * graphics.VertexFloatCount) 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) 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

@ -4080,3 +4080,31 @@ func TestImageSetAndSubImage(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want) t.Errorf("got: %v, want: %v", got, want)
} }
} }
// 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)
}
}