internal/graphicsdriver/opengl: Avoid heap allocation of a local variable

If a variable is passed to an argument interface{}, the variable might be
allocated on the heap unexpectedly.
This commit is contained in:
Hajime Hoshi 2021-10-31 01:11:09 +09:00
parent a082db04fd
commit 98083ccd0a

View File

@ -273,7 +273,7 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu
if len(u.value.Float32s) == 0 { if len(u.value.Float32s) == 0 {
if u.typ.Main != shaderir.Float { if u.typ.Main != shaderir.Float {
expected := &shaderir.Type{Main: shaderir.Float} expected := &shaderir.Type{Main: shaderir.Float}
got := &u.typ got := u.typ
return fmt.Errorf("opengl: uniform variable %s type doesn't match: expected %s but %s", u.name, expected.String(), got.String()) return fmt.Errorf("opengl: uniform variable %s type doesn't match: expected %s but %s", u.name, expected.String(), got.String())
} }
@ -289,7 +289,11 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu
g.state.lastUniforms[u.name] = u.value g.state.lastUniforms[u.name] = u.value
} else { } else {
if got, expected := len(u.value.Float32s), u.typ.FloatNum(); got != expected { if got, expected := len(u.value.Float32s), u.typ.FloatNum(); got != expected {
return fmt.Errorf("opengl: length of a uniform variables %s (%s) doesn't match: expected %d but %d", u.name, u.typ.String(), expected, got) // Copy a shaderir.Type value once. Do not pass u.typ directly to fmt.Errorf arguments, or
// the value u would be allocated on heap.
typ := u.typ
return fmt.Errorf("opengl: length of a uniform variables %s (%s) doesn't match: expected %d but %d", u.name, typ.String(), expected, got)
return nil
} }
cached, ok := g.state.lastUniforms[u.name] cached, ok := g.state.lastUniforms[u.name]