graphicsdriver: Bug fix: Pass texture natives as uniform variables correctly

This commit is contained in:
Hajime Hoshi 2020-05-25 02:31:54 +09:00
parent 732f288d20
commit 85730b433e
4 changed files with 33 additions and 15 deletions

View File

@ -424,7 +424,16 @@ func (c *drawTrianglesCommand) Exec(indexOffset int) error {
}
if c.shader != nil {
return theGraphicsDriver.DrawShader(c.dst.image.ID(), c.shader.shader.ID(), c.nindices, indexOffset, c.mode, c.uniforms)
us := map[int]interface{}{}
for k, v := range c.uniforms {
switch v := v.(type) {
case *Image:
us[k] = v.image.ID()
default:
us[k] = v
}
}
return theGraphicsDriver.DrawShader(c.dst.image.ID(), c.shader.shader.ID(), c.nindices, indexOffset, c.mode, us)
}
return theGraphicsDriver.Draw(c.dst.image.ID(), c.src.image.ID(), c.nindices, indexOffset, c.mode, c.color, c.filter, c.address)
}

View File

@ -162,24 +162,17 @@ func (i *Image) DrawTriangles(src *Image, vertices []float32, indices []uint16,
}
}
var us map[int]interface{}
if src != nil {
src.resolveBufferedReplacePixels()
} else {
us = map[int]interface{}{}
for k, v := range uniforms {
switch v := v.(type) {
case *Image:
v.resolveBufferedReplacePixels()
us[k] = v.image
default:
us[k] = v
}
}
for _, v := range uniforms {
if img, ok := v.(*Image); ok {
img.resolveBufferedReplacePixels()
}
}
i.resolveBufferedReplacePixels()
theCommandQueue.EnqueueDrawTrianglesCommand(i, src, vertices, indices, clr, mode, filter, address, shader, us)
theCommandQueue.EnqueueDrawTrianglesCommand(i, src, vertices, indices, clr, mode, filter, address, shader, uniforms)
if i.lastCommand == lastCommandNone && !i.screen {
i.lastCommand = lastCommandClear

View File

@ -16,6 +16,7 @@ package opengl
import (
"fmt"
"sort"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/driver"
@ -265,8 +266,21 @@ func (g *Graphics) DrawShader(dst driver.ImageID, shader driver.ShaderID, indexL
g.context.blendFunc(mode)
us := map[string]interface{}{}
for k, v := range uniforms {
us[fmt.Sprintf("U%d", k)] = v
var keys []int
for k := range uniforms {
keys = append(keys, k)
}
sort.Ints(keys)
tidx := 0
for _, k := range keys {
v := uniforms[k]
switch v := v.(type) {
case driver.ImageID:
us[fmt.Sprintf("U%d/%d", k, tidx)] = g.images[v].textureNative
tidx++
default:
us[fmt.Sprintf("U%d", k)] = v
}
}
if err := g.useProgram(s.p, us); err != nil {
return err

View File

@ -287,6 +287,8 @@ func (g *Graphics) useProgram(program program, uniforms map[string]interface{})
g.state.lastActiveTexture = idx
}
g.context.bindTexture(u)
default:
return fmt.Errorf("opengl: unexpected uniform value: %v", u)
}
}
return nil