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 (
|
|
|
|
varyingStructName = "VertexOut"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
kageTagRe = regexp.MustCompile("^`" + `kage:\"(.+)\"` + "`$")
|
|
|
|
)
|
|
|
|
|
2020-05-08 21:21:45 +02:00
|
|
|
type variable struct {
|
2020-05-09 17:39:26 +02:00
|
|
|
name string
|
|
|
|
typ typ
|
|
|
|
init string
|
|
|
|
}
|
|
|
|
|
|
|
|
type constant struct {
|
|
|
|
name string
|
|
|
|
typ typ
|
|
|
|
init string
|
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 {
|
2020-05-09 09:47:07 +02:00
|
|
|
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
|
|
|
|
2020-05-08 20:38:37 +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
|
|
|
|
2020-05-08 20:38:37 +02:00
|
|
|
// uniforms is a collection of uniform variables.
|
2020-05-08 21:21:45 +02:00
|
|
|
uniforms []variable
|
2020-05-08 20:38:37 +02:00
|
|
|
|
|
|
|
// globals is a collection of global variables.
|
2020-05-08 21:21:45 +02:00
|
|
|
globals []variable
|
2020-05-08 20:38:37 +02:00
|
|
|
|
2020-05-09 17:39:26 +02:00
|
|
|
constants []constant
|
|
|
|
|
2020-05-09 11:05:30 +02:00
|
|
|
funcs []function
|
|
|
|
|
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) {
|
2020-05-09 09:47:07 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:47:07 +02:00
|
|
|
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 20:38:37 +02:00
|
|
|
sort.Slice(s.varyings, func(a, b int) bool {
|
|
|
|
return s.varyings[a].name < s.varyings[b].name
|
|
|
|
})
|
|
|
|
sort.Slice(s.uniforms, func(a, b int) bool {
|
|
|
|
return s.uniforms[a].name < s.uniforms[b].name
|
|
|
|
})
|
|
|
|
sort.Slice(s.globals, func(a, b int) bool {
|
|
|
|
return s.globals[a].name < s.globals[b].name
|
|
|
|
})
|
|
|
|
|
2020-05-08 17:46:01 +02:00
|
|
|
// TODO: Make a call graph and reorder the elements.
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2020-05-09 09:47:07 +02:00
|
|
|
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-08 20:38:37 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 10:32:10 +02:00
|
|
|
func (sh *Shader) parse(f *ast.File) {
|
|
|
|
for _, d := range f.Decls {
|
|
|
|
switch d := d.(type) {
|
|
|
|
case *ast.GenDecl:
|
|
|
|
switch d.Tok {
|
|
|
|
case token.TYPE:
|
|
|
|
// TODO: Parse regular structs or other types
|
|
|
|
for _, s := range d.Specs {
|
|
|
|
s := s.(*ast.TypeSpec)
|
|
|
|
if s.Name.Name == varyingStructName {
|
|
|
|
sh.parseVaryingStruct(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case token.CONST:
|
|
|
|
for _, s := range d.Specs {
|
|
|
|
s := s.(*ast.ValueSpec)
|
2020-05-09 17:39:26 +02:00
|
|
|
cs := sh.parseConstant(s)
|
|
|
|
sh.constants = append(sh.constants, cs...)
|
2020-05-09 10:32:10 +02:00
|
|
|
}
|
|
|
|
case token.VAR:
|
|
|
|
for _, s := range d.Specs {
|
|
|
|
s := s.(*ast.ValueSpec)
|
2020-05-09 16:38:52 +02:00
|
|
|
vs := sh.parseVariable(s)
|
|
|
|
for _, v := range vs {
|
|
|
|
if 'A' <= v.name[0] && v.name[0] <= 'Z' {
|
|
|
|
sh.uniforms = append(sh.uniforms, v)
|
|
|
|
} else {
|
|
|
|
sh.globals = append(sh.globals, v)
|
|
|
|
}
|
|
|
|
}
|
2020-05-09 10:32:10 +02:00
|
|
|
}
|
2020-05-09 11:05:30 +02:00
|
|
|
case token.IMPORT:
|
|
|
|
sh.addError(d.Pos(), "import is forbidden")
|
|
|
|
default:
|
|
|
|
sh.addError(d.Pos(), "unexpected token")
|
2020-05-08 20:38:37 +02:00
|
|
|
}
|
2020-05-09 11:05:30 +02:00
|
|
|
case *ast.FuncDecl:
|
|
|
|
sh.parseFunc(d)
|
2020-05-09 10:32:10 +02:00
|
|
|
default:
|
2020-05-09 11:05:30 +02:00
|
|
|
sh.addError(d.Pos(), "unexpected decl")
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 10:32:10 +02:00
|
|
|
func (sh *Shader) parseVaryingStruct(t *ast.TypeSpec) {
|
|
|
|
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 {
|
2020-05-09 09:47:07 +02:00
|
|
|
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" {
|
2020-05-09 09:47:07 +02:00
|
|
|
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 {
|
2020-05-09 09:47:07 +02:00
|
|
|
sh.addError(f.Pos(), fmt.Sprintf("position members must be one"))
|
2020-05-08 17:46:01 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
t, err := parseType(f.Type)
|
|
|
|
if err != nil {
|
2020-05-09 09:47:07 +02:00
|
|
|
sh.addError(f.Type.Pos(), err.Error())
|
2020-05-08 17:46:01 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if t != typVec4 {
|
2020-05-09 09:47:07 +02:00
|
|
|
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-08 17:46:01 +02:00
|
|
|
name: f.Names[0].Name,
|
|
|
|
typ: t,
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t, err := parseType(f.Type)
|
|
|
|
if err != nil {
|
2020-05-09 09:47:07 +02:00
|
|
|
sh.addError(f.Type.Pos(), err.Error())
|
2020-05-08 17:46:01 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !t.numeric() {
|
2020-05-09 09:47:07 +02:00
|
|
|
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-08 17:46:01 +02:00
|
|
|
name: n.Name,
|
|
|
|
typ: t,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 20:38:37 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 16:38:52 +02:00
|
|
|
func (s *Shader) parseVariable(vs *ast.ValueSpec) []variable {
|
|
|
|
var t typ
|
|
|
|
if vs.Type != nil {
|
|
|
|
var err error
|
|
|
|
t, err = parseType(vs.Type)
|
|
|
|
if err != nil {
|
|
|
|
s.addError(vs.Type.Pos(), err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-08 20:38:37 +02:00
|
|
|
}
|
2020-05-09 16:38:52 +02:00
|
|
|
|
|
|
|
var vars []variable
|
2020-05-09 10:32:10 +02:00
|
|
|
for _, n := range vs.Names {
|
|
|
|
name := n.Name
|
2020-05-09 16:38:52 +02:00
|
|
|
vars = append(vars, variable{
|
2020-05-09 10:32:10 +02:00
|
|
|
name: name,
|
|
|
|
typ: t,
|
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 {
|
|
|
|
var t typ
|
|
|
|
if vs.Type != nil {
|
|
|
|
var err error
|
|
|
|
t, err = parseType(vs.Type)
|
|
|
|
if err != nil {
|
|
|
|
s.addError(vs.Type.Pos(), err.Error())
|
|
|
|
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 {
|
|
|
|
v := vs.Values[i]
|
2020-05-08 21:21:45 +02:00
|
|
|
var init string
|
|
|
|
switch v := v.(type) {
|
|
|
|
case *ast.BasicLit:
|
|
|
|
if v.Kind != token.INT && v.Kind != token.FLOAT {
|
2020-05-09 09:47:07 +02:00
|
|
|
s.addError(v.Pos(), fmt.Sprintf("literal must be int or float but %s", v.Kind))
|
2020-05-09 17:39:26 +02:00
|
|
|
return cs
|
2020-05-08 21:21:45 +02:00
|
|
|
}
|
2020-05-09 16:58:22 +02:00
|
|
|
init = v.Value // TODO: This should be go/constant.Value
|
2020-05-08 21:21:45 +02:00
|
|
|
default:
|
|
|
|
// TODO: Parse the expression.
|
|
|
|
}
|
2020-05-09 17:39:26 +02:00
|
|
|
cs = append(cs, constant{
|
|
|
|
name: n.Name,
|
|
|
|
typ: t,
|
|
|
|
init: init,
|
|
|
|
})
|
2020-05-08 20:38:37 +02:00
|
|
|
}
|
2020-05-09 17:39:26 +02:00
|
|
|
return cs
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 11:05:30 +02:00
|
|
|
func (sh *Shader) parseFunc(d *ast.FuncDecl) {
|
|
|
|
if d.Name == nil {
|
|
|
|
sh.addError(d.Pos(), "function must have a name")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if d.Body == nil {
|
|
|
|
sh.addError(d.Pos(), "function must have a body")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-09 12:21:01 +02:00
|
|
|
var args []variable
|
|
|
|
for _, f := range d.Type.Params.List {
|
|
|
|
t, err := parseType(f.Type)
|
|
|
|
if err != nil {
|
|
|
|
sh.addError(f.Type.Pos(), err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, n := range f.Names {
|
|
|
|
args = append(args, variable{
|
|
|
|
name: n.Name,
|
|
|
|
typ: t,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var rets []variable
|
|
|
|
for _, f := range d.Type.Results.List {
|
|
|
|
t, err := parseType(f.Type)
|
|
|
|
if err != nil {
|
|
|
|
sh.addError(f.Type.Pos(), err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(f.Names) == 0 {
|
|
|
|
rets = append(rets, variable{
|
|
|
|
name: "",
|
|
|
|
typ: t,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
for _, n := range f.Names {
|
|
|
|
rets = append(rets, variable{
|
|
|
|
name: n.Name,
|
|
|
|
typ: t,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 11:05:30 +02:00
|
|
|
f := function{
|
|
|
|
name: d.Name.Name,
|
2020-05-09 12:21:01 +02:00
|
|
|
args: args,
|
|
|
|
rets: rets,
|
2020-05-09 16:38:52 +02:00
|
|
|
body: sh.parseBlock(d.Body),
|
2020-05-09 11:05:30 +02:00
|
|
|
}
|
|
|
|
sh.funcs = append(sh.funcs, f)
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:38:52 +02:00
|
|
|
func (sh *Shader) parseBlock(b *ast.BlockStmt) *block {
|
|
|
|
block := &block{}
|
|
|
|
|
|
|
|
for _, l := range b.List {
|
|
|
|
switch l := l.(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
if l.Tok == token.DEFINE {
|
|
|
|
for _, s := range l.Lhs {
|
|
|
|
ident := s.(*ast.Ident)
|
|
|
|
block.vars = append(block.vars, variable{
|
|
|
|
name: ident.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
case *ast.DeclStmt:
|
|
|
|
switch d := l.Decl.(type) {
|
|
|
|
case *ast.GenDecl:
|
|
|
|
switch d.Tok {
|
|
|
|
case token.VAR:
|
|
|
|
for _, s := range d.Specs {
|
|
|
|
s := s.(*ast.ValueSpec)
|
|
|
|
vs := sh.parseVariable(s)
|
|
|
|
block.vars = append(block.vars, vs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *ast.ReturnStmt:
|
2020-05-09 16:58:22 +02:00
|
|
|
var exprs []expr
|
|
|
|
for _, r := range l.Results {
|
|
|
|
exprs = append(exprs, sh.parseExpr(r))
|
|
|
|
}
|
|
|
|
block.stmts = append(block.stmts, stmt{
|
|
|
|
stmtType: stmtReturn,
|
|
|
|
exprs: exprs,
|
|
|
|
})
|
2020-05-09 16:38:52 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(block.vars, func(a, b int) bool {
|
|
|
|
return block.vars[a].name < block.vars[b].name
|
|
|
|
})
|
|
|
|
|
|
|
|
return block
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:58:22 +02:00
|
|
|
func (sh *Shader) parseExpr(e ast.Expr) expr {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
return expr{
|
|
|
|
exprType: exprIdent,
|
|
|
|
value: e.Name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return expr{}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
lines = append(lines, fmt.Sprintf("var %s varying %s // position", s.position.name, s.position.typ))
|
|
|
|
for _, v := range s.varyings {
|
|
|
|
lines = append(lines, fmt.Sprintf("var %s varying %s", v.name, v.typ))
|
|
|
|
}
|
2020-05-08 20:38:37 +02:00
|
|
|
|
|
|
|
for _, u := range s.uniforms {
|
|
|
|
lines = append(lines, fmt.Sprintf("var %s uniform %s", u.name, u.typ))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range s.globals {
|
2020-05-08 21:21:45 +02:00
|
|
|
init := ""
|
|
|
|
if g.init != "" {
|
|
|
|
init = " = " + g.init
|
|
|
|
}
|
2020-05-09 17:39:26 +02:00
|
|
|
lines = append(lines, fmt.Sprintf("var %s %s%s", g.name, g.typ, init))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range s.constants {
|
|
|
|
lines = append(lines, fmt.Sprintf("const %s %s = %s", g.name, g.typ, g.init))
|
2020-05-08 20:38:37 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 11:05:30 +02:00
|
|
|
for _, f := range s.funcs {
|
2020-05-09 12:21:01 +02:00
|
|
|
var args []string
|
|
|
|
for _, a := range f.args {
|
|
|
|
args = append(args, fmt.Sprintf("%s %s", a.name, a.typ))
|
|
|
|
}
|
|
|
|
var rets []string
|
|
|
|
for _, r := range f.rets {
|
|
|
|
name := r.name
|
|
|
|
if name == "" {
|
|
|
|
name = "_"
|
|
|
|
}
|
|
|
|
rets = append(rets, fmt.Sprintf("%s %s", name, r.typ))
|
|
|
|
}
|
|
|
|
l := fmt.Sprintf("func %s(%s)", f.name, strings.Join(args, ", "))
|
|
|
|
if len(rets) > 0 {
|
|
|
|
l += " (" + strings.Join(rets, ", ") + ")"
|
|
|
|
}
|
2020-05-09 16:38:52 +02:00
|
|
|
l += " {"
|
2020-05-09 12:21:01 +02:00
|
|
|
lines = append(lines, l)
|
2020-05-09 16:38:52 +02:00
|
|
|
lines = append(lines, f.body.dump(1)...)
|
|
|
|
lines = append(lines, "}")
|
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.
|
|
|
|
lines = append(lines, fmt.Sprintf("varying %s %s;", v.typ.glslString(), v.name))
|
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n") + "\n"
|
|
|
|
}
|