From 4812f2e76e7b2f1c0d057b1a9ad0e419d309ae3f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 5 Jun 2016 00:32:57 +0900 Subject: [PATCH] graphics: Reduce calls of glUniformMatrix4fv --- image.go | 22 ++++++++++++++++++++-- imageparts.go | 19 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/image.go b/image.go index 7a135b11e..7574d73f9 100644 --- a/image.go +++ b/image.go @@ -269,6 +269,24 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error { parts = &wholeImage{image.impl.width, image.impl.height} } } + geom := &options.GeoM + colorm := &options.ColorM + dxf := geom.Element(0, 2) + dyf := geom.Element(1, 2) + // If possible, avoid using a geometry matrix so that we can reduce calls of + // glUniformMatrix4fv. + if geom.Element(0, 0) == 1 && geom.Element(1, 0) == 0 && geom.Element(0, 1) == 0 && geom.Element(1, 1) == 1 && (dxf != 0 && dyf != 0) { + if dxf == float64(int64(dxf)) && dyf == float64(int64(dyf)) { + dx := int(dxf) + dy := int(dyf) + parts = &transitionImageParts{ + parts: parts, + dx: dx, + dy: dy, + } + geom = &GeoM{} + } + } quads := &textureQuads{parts: parts, width: image.impl.width, height: image.impl.height} // TODO: Reuse one vertices instead of making here, but this would need locking. vertices := make([]int16, parts.Len()*16) @@ -286,8 +304,8 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error { return errors.New("ebiten: image is already disposed") } i.pixels = nil - m := opengl.CompositeMode(options.CompositeMode) - return i.framebuffer.DrawTexture(ui.GLContext(), image.impl.texture, vertices[:16*n], &options.GeoM, &options.ColorM, m) + mode := opengl.CompositeMode(options.CompositeMode) + return i.framebuffer.DrawTexture(ui.GLContext(), image.impl.texture, vertices[:16*n], geom, colorm, mode) } if theDelayedImageTasks.add(f) { return nil diff --git a/imageparts.go b/imageparts.go index 5fb77e925..f124aa463 100644 --- a/imageparts.go +++ b/imageparts.go @@ -124,3 +124,22 @@ func (t *textureQuads) vertices(vertices []int16) int { } return n } + +type transitionImageParts struct { + parts ImageParts + dx int + dy int +} + +func (t *transitionImageParts) Len() int { + return t.parts.Len() +} + +func (t *transitionImageParts) Dst(index int) (int, int, int, int) { + x0, y0, x1, y1 := t.parts.Dst(index) + return x0 + t.dx, y0 + t.dy, x1 + t.dx, y1 + t.dy +} + +func (t *transitionImageParts) Src(index int) (int, int, int, int) { + return t.parts.Src(index) +}