diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index 8dd54b1ed..ca0395a1a 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -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) } diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index dfe6f26a8..018bc77e7 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -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 diff --git a/internal/graphicsdriver/opengl/graphics.go b/internal/graphicsdriver/opengl/graphics.go index 25258b6a0..d0cca0d8b 100644 --- a/internal/graphicsdriver/opengl/graphics.go +++ b/internal/graphicsdriver/opengl/graphics.go @@ -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 diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index 43540d698..07fd6f1ed 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -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