From f6fd8f9c6d165fa575cc4cfa3e8c1debdb67635d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 29 May 2016 22:40:51 +0900 Subject: [PATCH] graphics: Reduce calling glUseProgram --- image.go | 1 - internal/graphics/program.go | 20 +++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/image.go b/image.go index 91f4d435f..7a135b11e 100644 --- a/image.go +++ b/image.go @@ -276,7 +276,6 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error { if n == 0 { return nil } - if i == image.impl { return errors.New("ebiten: Image.DrawImage: image should be different from the receiver") } diff --git a/internal/graphics/program.go b/internal/graphics/program.go index ee0ad06c6..717889594 100644 --- a/internal/graphics/program.go +++ b/internal/graphics/program.go @@ -24,10 +24,11 @@ type openGLState struct { indexBufferQuads opengl.Buffer programTexture opengl.Program - lastProgram opengl.Program - lastProjectionMatrix []float32 - lastModelviewMatrix []float32 - lastColorMatrix []float32 + lastProgram opengl.Program + lastProjectionMatrix []float32 + lastModelviewMatrix []float32 + lastColorMatrix []float32 + lastColorMatrixTranslation []float32 } var theOpenGLState openGLState @@ -57,6 +58,7 @@ func (s *openGLState) initialize(c *opengl.Context) error { s.lastProjectionMatrix = nil s.lastModelviewMatrix = nil s.lastColorMatrix = nil + s.lastColorMatrixTranslation = nil shaderVertexModelviewNative, err := c.NewShader(c.VertexShader, shader(c, shaderVertexModelview)) if err != nil { @@ -101,6 +103,7 @@ func (s *openGLState) finalize(c *opengl.Context) error { s.lastProjectionMatrix = nil s.lastModelviewMatrix = nil s.lastColorMatrix = nil + s.lastColorMatrixTranslation = nil c.DeleteBuffer(s.indexBufferQuads) c.DeleteProgram(s.programTexture) return nil @@ -136,6 +139,7 @@ func (p *programContext) begin() { p.state.lastProjectionMatrix = nil p.state.lastModelviewMatrix = nil p.state.lastColorMatrix = nil + p.state.lastColorMatrixTranslation = nil } c.BindElementArrayBuffer(p.state.indexBufferQuads) @@ -192,7 +196,13 @@ func (p *programContext) begin() { colorMatrixTranslation := []float32{ e[0][4], e[1][4], e[2][4], e[3][4], } - c.UniformFloats(p.program, "color_matrix_translation", colorMatrixTranslation) + if !areSameFloat32Array(p.state.lastColorMatrixTranslation, colorMatrixTranslation) { + c.UniformFloats(p.program, "color_matrix_translation", colorMatrixTranslation) + if p.state.lastColorMatrixTranslation == nil { + p.state.lastColorMatrixTranslation = make([]float32, 4) + } + copy(p.state.lastColorMatrixTranslation, colorMatrixTranslation) + } // We don't have to call gl.ActiveTexture here: GL_TEXTURE0 is the default active texture // See also: https://www.opengl.org/sdk/docs/man2/xhtml/glActiveTexture.xml