From 15f62bfeb5a031bd7fd7e486f5a3f586974426c4 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 31 Oct 2021 05:40:44 +0900 Subject: [PATCH] internal/graphicsdriver/opengl: Optimization: Cache texture variable names --- internal/graphicsdriver/opengl/graphics.go | 1 + internal/graphicsdriver/opengl/program.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 5640037b0..c2bc9942b 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -47,6 +47,7 @@ type Graphics struct { drawCalled bool uniformVariableNameCache map[int]string + textureVariableNameCache map[int]string uniformVars []uniformVariable diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 0dd7e1b35..d753be6cc 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -251,6 +251,18 @@ type textureVariable struct { native textureNative } +func (g *Graphics) textureVariableName(idx int) string { + if v, ok := g.textureVariableNameCache[idx]; ok { + return v + } + if g.textureVariableNameCache == nil { + g.textureVariableNameCache = map[int]string{} + } + name := fmt.Sprintf("T%d", idx) + g.textureVariableNameCache[idx] = name + return name +} + // useProgram uses the program (programTexture). func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textures [graphics.ShaderImageNum]textureVariable) error { if !g.state.lastProgram.equal(program) { @@ -318,7 +330,7 @@ loop: // Rebinding the same texture seems problematic (#1193). for _, at := range g.activatedTextures { if t.native.equal(at.textureNative) { - g.context.uniformInt(program, fmt.Sprintf("T%d", i), at.index) + g.context.uniformInt(program, g.textureVariableName(i), at.index) continue loop } } @@ -327,7 +339,7 @@ loop: textureNative: t.native, index: idx, }) - g.context.uniformInt(program, fmt.Sprintf("T%d", i), idx) + g.context.uniformInt(program, g.textureVariableName(i), idx) if g.state.lastActiveTexture != idx { g.context.activeTexture(idx) g.state.lastActiveTexture = idx