ebiten/internal/shader/shader.go

468 lines
11 KiB
Go
Raw Normal View History

2020-05-08 17:46:01 +02:00
// Copyright 2020 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shader
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"regexp"
"sort"
"strings"
)
const (
2020-05-10 16:19:39 +02:00
// TODO: Remove this. This struct is a user-defined struct.
2020-05-08 17:46:01 +02:00
varyingStructName = "VertexOut"
)
var (
kageTagRe = regexp.MustCompile("^`" + `kage:\"(.+)\"` + "`$")
)
2020-05-08 21:21:45 +02:00
type variable struct {
2020-05-10 16:19:39 +02:00
name string
basicType basicType
init ast.Expr
2020-05-09 17:39:26 +02:00
}
type constant struct {
2020-05-10 16:19:39 +02:00
name string
basicType basicType
init ast.Expr
2020-05-08 17:46:01 +02:00
}
2020-05-09 11:05:30 +02:00
type function struct {
name string
2020-05-09 12:21:01 +02:00
args []variable
rets []variable
2020-05-09 16:38:52 +02:00
body *block
2020-05-09 11:05:30 +02:00
}
2020-05-08 17:46:01 +02:00
type Shader struct {
fs *token.FileSet
2020-05-08 17:46:01 +02:00
// position is the field name of VertexOut that represents a vertex position (gl_Position in GLSL).
2020-05-08 21:21:45 +02:00
position variable
2020-05-08 17:46:01 +02:00
// varyings is a collection of varying variables.
2020-05-08 21:21:45 +02:00
varyings []variable
2020-05-08 17:46:01 +02:00
// uniforms is a collection of uniform variables.
2020-05-08 21:21:45 +02:00
uniforms []variable
2020-05-09 17:59:18 +02:00
global block
2020-05-09 11:05:30 +02:00
2020-05-08 17:46:01 +02:00
errs []string
}
type ParseError struct {
errs []string
}
func (p *ParseError) Error() string {
return strings.Join(p.errs, "\n")
}
func NewShader(src []byte) (*Shader, error) {
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, "", src, parser.AllErrors)
2020-05-08 17:46:01 +02:00
if err != nil {
return nil, err
}
s := &Shader{
fs: fs,
}
2020-05-08 17:46:01 +02:00
s.parse(f)
if len(s.errs) > 0 {
return nil, &ParseError{s.errs}
}
2020-05-09 16:38:52 +02:00
// TODO: Resolve identifiers?
// TODO: Resolve constants
2020-05-08 17:46:01 +02:00
// TODO: Make a call graph and reorder the elements.
return s, nil
}
func (s *Shader) addError(pos token.Pos, str string) {
p := s.fs.Position(pos)
s.errs = append(s.errs, fmt.Sprintf("%s: %s", p, str))
}
2020-05-09 10:32:10 +02:00
func (sh *Shader) parse(f *ast.File) {
for _, d := range f.Decls {
sh.parseDecl(&sh.global, d, true)
2020-05-09 17:59:18 +02:00
}
// TODO: This is duplicated with parseBlock.
sort.Slice(sh.global.consts, func(a, b int) bool {
return sh.global.consts[a].name < sh.global.consts[b].name
})
sort.Slice(sh.global.funcs, func(a, b int) bool {
return sh.global.funcs[a].name < sh.global.funcs[b].name
})
sort.Slice(sh.varyings, func(a, b int) bool {
return sh.varyings[a].name < sh.varyings[b].name
})
sort.Slice(sh.uniforms, func(a, b int) bool {
return sh.uniforms[a].name < sh.uniforms[b].name
})
}
func (sh *Shader) parseDecl(b *block, d ast.Decl, global bool) {
2020-05-09 17:59:18 +02:00
switch d := d.(type) {
case *ast.GenDecl:
switch d.Tok {
case token.TYPE:
2020-05-10 16:19:39 +02:00
// TODO: Parse other types
2020-05-09 17:59:18 +02:00
for _, s := range d.Specs {
s := s.(*ast.TypeSpec)
2020-05-10 16:19:39 +02:00
sh.parseStruct(s)
}
2020-05-09 17:59:18 +02:00
case token.CONST:
for _, s := range d.Specs {
s := s.(*ast.ValueSpec)
cs := sh.parseConstant(s)
b.consts = append(b.consts, cs...)
}
case token.VAR:
for _, s := range d.Specs {
s := s.(*ast.ValueSpec)
2020-05-10 12:32:40 +02:00
vs := sh.parseVariable(b, s)
if !global {
b.vars = append(b.vars, vs...)
continue
}
for i, v := range vs {
2020-05-10 16:19:39 +02:00
if v.name[0] < 'A' || 'Z' < v.name[0] {
sh.addError(s.Names[i].Pos(), fmt.Sprintf("global variables must be exposed: %s", v.name))
}
2020-05-10 16:19:39 +02:00
// TODO: Check RHS
sh.uniforms = append(sh.uniforms, v)
}
2020-05-09 17:59:18 +02:00
}
case token.IMPORT:
sh.addError(d.Pos(), "import is forbidden")
2020-05-09 10:32:10 +02:00
default:
2020-05-09 17:59:18 +02:00
sh.addError(d.Pos(), "unexpected token")
2020-05-08 17:46:01 +02:00
}
2020-05-09 17:59:18 +02:00
case *ast.FuncDecl:
2020-05-10 12:32:40 +02:00
b.funcs = append(b.funcs, sh.parseFunc(d, b))
2020-05-09 17:59:18 +02:00
default:
sh.addError(d.Pos(), "unexpected decl")
2020-05-08 17:46:01 +02:00
}
}
2020-05-10 16:19:39 +02:00
func (sh *Shader) parseStruct(t *ast.TypeSpec) {
2020-05-09 10:32:10 +02:00
s, ok := t.Type.(*ast.StructType)
2020-05-08 17:46:01 +02:00
if !ok {
2020-05-09 10:32:10 +02:00
sh.addError(t.Type.Pos(), fmt.Sprintf("%s must be a struct but not", t.Name))
2020-05-08 17:46:01 +02:00
return
}
for _, f := range s.Fields.List {
if f.Tag != nil {
tag := f.Tag.Value
m := kageTagRe.FindStringSubmatch(tag)
if m == nil {
sh.addError(f.Tag.Pos(), fmt.Sprintf("invalid struct tag: %s", tag))
2020-05-08 17:46:01 +02:00
continue
}
if m[1] != "position" {
sh.addError(f.Tag.Pos(), fmt.Sprintf("struct tag value must be position in %s but %s", varyingStructName, m[1]))
2020-05-08 17:46:01 +02:00
continue
}
if len(f.Names) != 1 {
sh.addError(f.Pos(), fmt.Sprintf("position members must be one"))
2020-05-08 17:46:01 +02:00
continue
}
2020-05-09 19:01:28 +02:00
t := parseType(f.Type)
2020-05-10 16:19:39 +02:00
if t == basicTypeNone {
2020-05-09 19:01:28 +02:00
sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type))
2020-05-08 17:46:01 +02:00
continue
}
2020-05-10 16:19:39 +02:00
if t != basicTypeVec4 {
sh.addError(f.Type.Pos(), fmt.Sprintf("position must be vec4 but %s", t))
2020-05-08 17:46:01 +02:00
continue
}
2020-05-08 21:21:45 +02:00
sh.position = variable{
2020-05-10 16:19:39 +02:00
name: f.Names[0].Name,
basicType: t,
2020-05-08 17:46:01 +02:00
}
continue
}
2020-05-09 19:01:28 +02:00
t := parseType(f.Type)
2020-05-10 16:19:39 +02:00
if t == basicTypeNone {
2020-05-09 19:01:28 +02:00
sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type))
2020-05-08 17:46:01 +02:00
continue
}
if !t.numeric() {
sh.addError(f.Type.Pos(), fmt.Sprintf("members in %s must be numeric but %s", varyingStructName, t))
2020-05-08 17:46:01 +02:00
continue
}
for _, n := range f.Names {
2020-05-08 21:21:45 +02:00
sh.varyings = append(sh.varyings, variable{
2020-05-10 16:19:39 +02:00
name: n.Name,
basicType: t,
2020-05-08 17:46:01 +02:00
})
}
}
}
2020-05-10 12:32:40 +02:00
func (s *Shader) parseVariable(block *block, vs *ast.ValueSpec) []variable {
2020-05-10 16:19:39 +02:00
var t basicType
2020-05-09 16:38:52 +02:00
if vs.Type != nil {
2020-05-09 19:01:28 +02:00
t = parseType(vs.Type)
2020-05-10 16:19:39 +02:00
if t == basicTypeNone {
2020-05-09 19:01:28 +02:00
s.addError(vs.Type.Pos(), fmt.Sprintf("unexpected type: %s", vs.Type))
2020-05-09 16:38:52 +02:00
return nil
}
}
2020-05-09 16:38:52 +02:00
var vars []variable
2020-05-09 19:01:28 +02:00
for i, n := range vs.Names {
var init ast.Expr
if len(vs.Values) > 0 {
init = vs.Values[i]
2020-05-10 16:19:39 +02:00
if t == basicTypeNone {
2020-05-10 12:32:40 +02:00
t = s.detectType(block, init)
}
2020-05-09 19:01:28 +02:00
}
2020-05-09 10:32:10 +02:00
name := n.Name
2020-05-09 16:38:52 +02:00
vars = append(vars, variable{
2020-05-10 16:19:39 +02:00
name: name,
basicType: t,
init: init,
2020-05-09 16:38:52 +02:00
})
2020-05-08 21:21:45 +02:00
}
2020-05-09 16:38:52 +02:00
return vars
2020-05-08 21:21:45 +02:00
}
2020-05-09 17:39:26 +02:00
func (s *Shader) parseConstant(vs *ast.ValueSpec) []constant {
2020-05-10 16:19:39 +02:00
var t basicType
2020-05-09 17:39:26 +02:00
if vs.Type != nil {
2020-05-09 19:01:28 +02:00
t = parseType(vs.Type)
2020-05-10 16:19:39 +02:00
if t == basicTypeNone {
2020-05-09 19:01:28 +02:00
s.addError(vs.Type.Pos(), fmt.Sprintf("unexpected type: %s", vs.Type))
2020-05-09 17:39:26 +02:00
return nil
}
2020-05-08 21:21:45 +02:00
}
2020-05-09 17:39:26 +02:00
var cs []constant
2020-05-09 10:32:10 +02:00
for i, n := range vs.Names {
2020-05-09 17:39:26 +02:00
cs = append(cs, constant{
2020-05-10 16:19:39 +02:00
name: n.Name,
basicType: t,
init: vs.Values[i],
2020-05-09 17:39:26 +02:00
})
}
2020-05-09 17:39:26 +02:00
return cs
2020-05-08 17:46:01 +02:00
}
2020-05-10 12:32:40 +02:00
func (sh *Shader) parseFunc(d *ast.FuncDecl, block *block) function {
2020-05-09 11:05:30 +02:00
if d.Name == nil {
sh.addError(d.Pos(), "function must have a name")
2020-05-09 17:59:18 +02:00
return function{}
2020-05-09 11:05:30 +02:00
}
if d.Body == nil {
sh.addError(d.Pos(), "function must have a body")
2020-05-09 17:59:18 +02:00
return function{}
2020-05-09 11:05:30 +02:00
}
2020-05-09 12:21:01 +02:00
var args []variable
for _, f := range d.Type.Params.List {
2020-05-09 19:01:28 +02:00
t := parseType(f.Type)
2020-05-10 16:19:39 +02:00
if t == basicTypeNone {
2020-05-09 19:01:28 +02:00
sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type))
2020-05-09 12:21:01 +02:00
continue
}
for _, n := range f.Names {
args = append(args, variable{
2020-05-10 16:19:39 +02:00
name: n.Name,
basicType: t,
2020-05-09 12:21:01 +02:00
})
}
}
var rets []variable
2020-05-10 13:41:43 +02:00
if d.Type.Results != nil {
for _, f := range d.Type.Results.List {
t := parseType(f.Type)
2020-05-10 16:19:39 +02:00
if t == basicTypeNone {
2020-05-10 13:41:43 +02:00
sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type))
continue
}
if len(f.Names) == 0 {
2020-05-09 12:21:01 +02:00
rets = append(rets, variable{
2020-05-10 16:19:39 +02:00
name: "",
basicType: t,
2020-05-09 12:21:01 +02:00
})
2020-05-10 13:41:43 +02:00
} else {
for _, n := range f.Names {
rets = append(rets, variable{
2020-05-10 16:19:39 +02:00
name: n.Name,
basicType: t,
2020-05-10 13:41:43 +02:00
})
}
2020-05-09 12:21:01 +02:00
}
}
}
2020-05-09 17:59:18 +02:00
return function{
2020-05-09 11:05:30 +02:00
name: d.Name.Name,
2020-05-09 12:21:01 +02:00
args: args,
rets: rets,
2020-05-10 14:37:59 +02:00
body: sh.parseBlock(block, d.Body),
2020-05-09 11:05:30 +02:00
}
}
2020-05-10 14:37:59 +02:00
func (sh *Shader) parseBlock(outer *block, b *ast.BlockStmt) *block {
2020-05-10 12:32:40 +02:00
block := &block{
outer: outer,
}
2020-05-09 16:38:52 +02:00
for _, l := range b.List {
switch l := l.(type) {
case *ast.AssignStmt:
2020-05-09 19:01:28 +02:00
switch l.Tok {
case token.DEFINE:
2020-05-10 12:32:40 +02:00
for i, s := range l.Lhs {
v := variable{
name: s.(*ast.Ident).Name,
}
if len(l.Rhs) > 0 {
2020-05-10 16:19:39 +02:00
v.basicType = sh.detectType(block, l.Rhs[i])
2020-05-10 12:32:40 +02:00
}
block.vars = append(block.vars, v)
2020-05-09 16:38:52 +02:00
}
2020-05-09 19:01:28 +02:00
for i := range l.Rhs {
block.stmts = append(block.stmts, stmt{
stmtType: stmtAssign,
exprs: []ast.Expr{l.Lhs[i], l.Rhs[i]},
})
}
case token.ASSIGN:
// TODO: What about the statement `a,b = b,a?`
for i := range l.Rhs {
block.stmts = append(block.stmts, stmt{
stmtType: stmtAssign,
exprs: []ast.Expr{l.Lhs[i], l.Rhs[i]},
})
}
2020-05-09 16:38:52 +02:00
}
2020-05-10 14:37:59 +02:00
case *ast.BlockStmt:
block.stmts = append(block.stmts, stmt{
stmtType: stmtBlock,
block: sh.parseBlock(block, l),
})
2020-05-09 16:38:52 +02:00
case *ast.DeclStmt:
sh.parseDecl(block, l.Decl, false)
2020-05-09 16:38:52 +02:00
case *ast.ReturnStmt:
2020-05-09 19:01:28 +02:00
var exprs []ast.Expr
2020-05-09 16:58:22 +02:00
for _, r := range l.Results {
2020-05-09 19:01:28 +02:00
exprs = append(exprs, r)
2020-05-09 16:58:22 +02:00
}
block.stmts = append(block.stmts, stmt{
stmtType: stmtReturn,
exprs: exprs,
})
2020-05-09 16:38:52 +02:00
}
}
2020-05-09 17:59:18 +02:00
sort.Slice(block.consts, func(a, b int) bool {
return block.consts[a].name < block.consts[b].name
})
sort.Slice(block.funcs, func(a, b int) bool {
return block.funcs[a].name < block.funcs[b].name
})
2020-05-09 16:38:52 +02:00
return block
}
2020-05-10 16:19:39 +02:00
func (s *Shader) detectType(b *block, expr ast.Expr) basicType {
2020-05-10 12:32:40 +02:00
switch e := expr.(type) {
case *ast.BasicLit:
if e.Kind == token.FLOAT {
2020-05-10 16:19:39 +02:00
return basicTypeFloat
2020-05-10 12:32:40 +02:00
}
2020-05-10 13:41:43 +02:00
if e.Kind == token.INT {
s.addError(expr.Pos(), fmt.Sprintf("integer literal is not implemented yet: %s", e.Value))
} else {
s.addError(expr.Pos(), fmt.Sprintf("unexpected literal: %s", e.Value))
}
2020-05-10 16:19:39 +02:00
return basicTypeNone
2020-05-10 12:32:40 +02:00
case *ast.CompositeLit:
return parseType(e.Type)
case *ast.Ident:
n := e.Name
for _, v := range b.vars {
if v.name == n {
2020-05-10 16:19:39 +02:00
return v.basicType
2020-05-10 12:32:40 +02:00
}
}
if b == &s.global {
for _, v := range s.uniforms {
if v.name == n {
2020-05-10 16:19:39 +02:00
return v.basicType
}
}
}
2020-05-10 12:32:40 +02:00
if b.outer != nil {
return s.detectType(b.outer, e)
}
s.addError(expr.Pos(), fmt.Sprintf("unexpected identity: %s", n))
2020-05-10 16:19:39 +02:00
return basicTypeNone
2020-05-10 12:32:40 +02:00
//case *ast.SelectorExpr:
//return fmt.Sprintf("%s.%s", dumpExpr(e.X), dumpExpr(e.Sel))
default:
s.addError(expr.Pos(), fmt.Sprintf("detecting type not implemented: %#v", expr))
2020-05-10 16:19:39 +02:00
return basicTypeNone
2020-05-10 12:32:40 +02:00
}
}
2020-05-08 17:46:01 +02:00
// Dump dumps the shader state in an intermediate language.
func (s *Shader) Dump() string {
var lines []string
2020-05-10 13:41:43 +02:00
if s.position.name != "" {
2020-05-10 16:19:39 +02:00
lines = append(lines, fmt.Sprintf("var %s varying %s // position", s.position.name, s.position.basicType))
2020-05-10 13:41:43 +02:00
}
2020-05-08 17:46:01 +02:00
for _, v := range s.varyings {
2020-05-10 16:19:39 +02:00
lines = append(lines, fmt.Sprintf("var %s varying %s", v.name, v.basicType))
2020-05-08 17:46:01 +02:00
}
for _, u := range s.uniforms {
2020-05-10 16:19:39 +02:00
lines = append(lines, fmt.Sprintf("var %s uniform %s", u.name, u.basicType))
}
2020-05-09 17:59:18 +02:00
lines = append(lines, s.global.dump(0)...)
2020-05-09 11:05:30 +02:00
2020-05-08 17:46:01 +02:00
return strings.Join(lines, "\n") + "\n"
}
func (s *Shader) GlslVertex() string {
var lines []string
for _, v := range s.varyings {
// TODO: variable names must be escaped not to conflict with keywords.
2020-05-10 16:19:39 +02:00
lines = append(lines, fmt.Sprintf("varying %s %s;", v.basicType.glslString(), v.name))
2020-05-08 17:46:01 +02:00
}
return strings.Join(lines, "\n") + "\n"
}