graphicsdriver/opengl: Bug fix: Binding the same texture multiple times did not work

Updates #1193
This commit is contained in:
Hajime Hoshi 2020-07-19 03:21:47 +09:00
parent 36e9803cea
commit 5506491c03
2 changed files with 33 additions and 7 deletions

View File

@ -284,17 +284,43 @@ 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 { for i, t := range textures {
if !t.valid { if !t.valid {
continue continue
} }
g.context.uniformInt(program, fmt.Sprintf("T%d", i), i)
if g.state.lastActiveTexture != i { // If the texture is already bound, set the texture variable to point to the texture.
g.context.activeTexture(i) // Rebinding the same texture seems problematic (#1193).
g.state.lastActiveTexture = i for _, at := range textureToActivatedTexture {
if t.native.equal(at.textureNative) {
g.context.uniformInt(program, fmt.Sprintf("T%d", i), at.index)
continue loop
}
} }
textureToActivatedTexture = append(textureToActivatedTexture, activatedTexture{
textureNative: t.native,
index: idx,
})
g.context.uniformInt(program, fmt.Sprintf("T%d", i), idx)
if g.state.lastActiveTexture != idx {
g.context.activeTexture(idx)
g.state.lastActiveTexture = idx
}
// Apparently, a texture must be bound every time. The cache is not used here. // Apparently, a texture must be bound every time. The cache is not used here.
g.context.bindTexture(t.native) g.context.bindTexture(t.native)
idx++
} }
return nil return nil

View File

@ -130,13 +130,13 @@ func TestShaderMultipleSourcesOnOneTexture(t *testing.T) {
t.Skip("shader is not available on this environment") t.Skip("shader is not available on this environment")
} }
var srcs [graphics.ShaderImageNum]*Image src := NewImage(3, 1, false)
srcs[0] = NewImage(3, 1, false) src.ReplacePixels([]byte{
srcs[0].ReplacePixels([]byte{
0x40, 0, 0, 0xff, 0x40, 0, 0, 0xff,
0, 0x80, 0, 0xff, 0, 0x80, 0, 0xff,
0, 0, 0xc0, 0xff, 0, 0, 0xc0, 0xff,
}, 0, 0, 3, 1) }, 0, 0, 3, 1)
srcs := [graphics.ShaderImageNum]*Image{src, src, src}
dst := NewImage(1, 1, false) dst := NewImage(1, 1, false)