Skip to content

Commit

Permalink
ebiten: Bug fix: Source regions should not be passed when not needed
Browse files Browse the repository at this point in the history
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 hajimehoshi#1293
  • Loading branch information
hajimehoshi committed Aug 11, 2020
1 parent 95022ff commit d4042a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
28 changes: 18 additions & 10 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions internal/shareable/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4042a5

Please sign in to comment.