From b4f45edff8644df3402bf4f6c7860929c9fbcd29 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 30 Oct 2021 00:13:57 +0900 Subject: [PATCH] internal/graphicsdriver/opengl: Optimization: Avoid creating slices for every frame --- internal/graphicsdriver/opengl/graphics.go | 9 +++++++++ internal/graphicsdriver/opengl/program.go | 16 +++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 960241b8a..a10ae3e19 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -28,6 +28,11 @@ func Get() *Graphics { return &theGraphics } +type activatedTexture struct { + textureNative textureNative + index int +} + type Graphics struct { state openGLState context context @@ -42,6 +47,10 @@ type Graphics struct { drawCalled bool uniformVariableNameCache map[int]string + + // activatedTextures is a set of activated textures. + // textureNative cannot be a map key unfortunately. + activatedTextures []activatedTexture } func (g *Graphics) Begin() { diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 94135e28e..05b452055 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -297,13 +297,6 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu } } - type activatedTexture struct { - textureNative textureNative - index int - } - - // textureNative cannot be a map key unfortunately. - textureToActivatedTexture := []activatedTexture{} var idx int loop: for i, t := range textures { @@ -313,14 +306,14 @@ loop: // If the texture is already bound, set the texture variable to point to the texture. // Rebinding the same texture seems problematic (#1193). - for _, at := range textureToActivatedTexture { + for _, at := range g.activatedTextures { if t.native.equal(at.textureNative) { g.context.uniformInt(program, fmt.Sprintf("T%d", i), at.index) continue loop } } - textureToActivatedTexture = append(textureToActivatedTexture, activatedTexture{ + g.activatedTextures = append(g.activatedTextures, activatedTexture{ textureNative: t.native, index: idx, }) @@ -336,5 +329,10 @@ loop: idx++ } + for i := range g.activatedTextures { + g.activatedTextures[i] = activatedTexture{} + } + g.activatedTextures = g.activatedTextures[:0] + return nil }