shader: Define special variables for textures __t%d

This eanbles to define texture0At to texture3At.

Updates #1193
This commit is contained in:
Hajime Hoshi 2020-07-18 19:17:19 +09:00
parent e0d5763a60
commit 4bd3bc16ac
2 changed files with 21 additions and 12 deletions

View File

@ -25,7 +25,7 @@ import (
"github.com/hajimehoshi/ebiten/internal/shaderir"
)
var textureAtRe = regexp.MustCompile(`\Atexture(\d+)At\z`)
var textureVariableRe = regexp.MustCompile(`\A__t(\d+)\z`)
func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr, []shaderir.Type, []shaderir.Stmt, bool) {
switch e := expr.(type) {
@ -220,12 +220,6 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
t = shaderir.Type{Main: shaderir.Vec3}
case shaderir.Texture2DF:
t = shaderir.Type{Main: shaderir.Vec4}
args = append([]shaderir.Expr{
{
Type: shaderir.TextureVariable,
Index: callee.Index,
},
}, args...)
default:
t = argts[0]
}
@ -359,13 +353,12 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
},
}, nil, nil, true
}
if m := textureAtRe.FindStringSubmatch(e.Name); m != nil {
if m := textureVariableRe.FindStringSubmatch(e.Name); m != nil {
i, _ := strconv.Atoi(m[1])
return []shaderir.Expr{
{
Type: shaderir.BuiltinFuncExpr,
BuiltinFunc: shaderir.Texture2DF,
Index: i, // Index is used as a texture ID later.
Type: shaderir.TextureVariable,
Index: i,
},
}, nil, nil, true
}

View File

@ -16,15 +16,17 @@ package ebiten
import (
"bytes"
"fmt"
"go/parser"
"go/token"
"strings"
"github.com/hajimehoshi/ebiten/internal/buffered"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/shader"
)
const shaderSuffix = `
var shaderSuffix = `
var __viewportSize vec2
func viewportSize() vec2 {
@ -32,6 +34,20 @@ func viewportSize() vec2 {
}
`
func init() {
// __t%d is a special variable for a texture variable.
// TODO: Add appropriate offsets for second and following images.
var fs []string
for i := 0; i < graphics.ShaderImageNum; i++ {
fs = append(fs, fmt.Sprintf(`func texture%[1]dAt(pos vec2) vec4 {
return texture2D(__t%[1]d, pos)
}
`, i))
}
shaderSuffix += "\n" + strings.Join(fs, "\n")
}
type Shader struct {
shader *buffered.Shader
}