mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
shader: Define special variables for textures __t%d
This eanbles to define texture0At to texture3At. Updates #1193
This commit is contained in:
parent
e0d5763a60
commit
4bd3bc16ac
@ -25,7 +25,7 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/shaderir"
|
"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) {
|
func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr, []shaderir.Type, []shaderir.Stmt, bool) {
|
||||||
switch e := expr.(type) {
|
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}
|
t = shaderir.Type{Main: shaderir.Vec3}
|
||||||
case shaderir.Texture2DF:
|
case shaderir.Texture2DF:
|
||||||
t = shaderir.Type{Main: shaderir.Vec4}
|
t = shaderir.Type{Main: shaderir.Vec4}
|
||||||
args = append([]shaderir.Expr{
|
|
||||||
{
|
|
||||||
Type: shaderir.TextureVariable,
|
|
||||||
Index: callee.Index,
|
|
||||||
},
|
|
||||||
}, args...)
|
|
||||||
default:
|
default:
|
||||||
t = argts[0]
|
t = argts[0]
|
||||||
}
|
}
|
||||||
@ -359,13 +353,12 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
|
|||||||
},
|
},
|
||||||
}, nil, nil, true
|
}, nil, nil, true
|
||||||
}
|
}
|
||||||
if m := textureAtRe.FindStringSubmatch(e.Name); m != nil {
|
if m := textureVariableRe.FindStringSubmatch(e.Name); m != nil {
|
||||||
i, _ := strconv.Atoi(m[1])
|
i, _ := strconv.Atoi(m[1])
|
||||||
return []shaderir.Expr{
|
return []shaderir.Expr{
|
||||||
{
|
{
|
||||||
Type: shaderir.BuiltinFuncExpr,
|
Type: shaderir.TextureVariable,
|
||||||
BuiltinFunc: shaderir.Texture2DF,
|
Index: i,
|
||||||
Index: i, // Index is used as a texture ID later.
|
|
||||||
},
|
},
|
||||||
}, nil, nil, true
|
}, nil, nil, true
|
||||||
}
|
}
|
||||||
|
18
shader.go
18
shader.go
@ -16,15 +16,17 @@ package ebiten
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/buffered"
|
"github.com/hajimehoshi/ebiten/internal/buffered"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/shader"
|
"github.com/hajimehoshi/ebiten/internal/shader"
|
||||||
)
|
)
|
||||||
|
|
||||||
const shaderSuffix = `
|
var shaderSuffix = `
|
||||||
var __viewportSize vec2
|
var __viewportSize vec2
|
||||||
|
|
||||||
func 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 {
|
type Shader struct {
|
||||||
shader *buffered.Shader
|
shader *buffered.Shader
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user