From d4042a5cfa6fe02258f6abb1d6465d18346adc28 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 12 Aug 2020 02:12:32 +0900 Subject: [PATCH] ebiten: Bug fix: Source regions should not be passed when not needed The source region information affects the condition of merging graphics commands. To avoid performance issues by the big number of graphcis commands, do not pass the source region whenever possible. Fixes #1293 --- image.go | 28 ++++++++++++++++++---------- internal/shareable/image.go | 8 ++++++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/image.go b/image.go index ff2bb32c1..69c4ba44a 100644 --- a/image.go +++ b/image.go @@ -271,11 +271,15 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) error { is := graphics.QuadIndices() var sr driver.Region - sr = driver.Region{ - X: float32(bounds.Min.X), - Y: float32(bounds.Min.Y), - Width: float32(bounds.Dx()), - Height: float32(bounds.Dy()), + // Pass the source region only when the shader is used, since this affects the condition of merging graphics + // commands (#1293). + if options.Shader != nil { + sr = driver.Region{ + X: float32(bounds.Min.X), + Y: float32(bounds.Min.Y), + Width: float32(bounds.Dx()), + Height: float32(bounds.Dy()), + } } srcs := [graphics.ShaderImageNum]*mipmap.Mipmap{img.mipmap} @@ -430,11 +434,15 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o var sr driver.Region b := img.Bounds() - sr = driver.Region{ - X: float32(b.Min.X), - Y: float32(b.Min.Y), - Width: float32(b.Dx()), - Height: float32(b.Dy()), + // Pass the source region only when the shader is used, since this affects the condition of merging graphics + // commands (#1293). + if options.Shader != nil || options.Address != AddressUnsafe { + sr = driver.Region{ + X: float32(b.Min.X), + Y: float32(b.Min.Y), + Width: float32(b.Dx()), + Height: float32(b.Dy()), + } } var srcs [graphics.ShaderImageNum]*mipmap.Mipmap diff --git a/internal/shareable/image.go b/internal/shareable/image.go index 4cb3fa6f6..0ae46ee7b 100644 --- a/internal/shareable/image.go +++ b/internal/shareable/image.go @@ -337,8 +337,12 @@ func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []f vertices[i*graphics.VertexFloatNum+2] += oxf vertices[i*graphics.VertexFloatNum+3] += oyf } - sourceRegion.X += oxf - sourceRegion.Y += oyf + // sourceRegion can be delibarately empty when this is not needed in order to avoid unexpected + // performance issue (#1293). + if sourceRegion.Width != 0 && sourceRegion.Height != 0 { + sourceRegion.X += oxf + sourceRegion.Y += oyf + } } var offsets [graphics.ShaderImageNum - 1][2]float32