vendor dependencies, make some changes to how input is done
This commit is contained in:
+1265
File diff suppressed because it is too large
Load Diff
+948
@@ -0,0 +1,948 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
gconstant "go/constant"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||
)
|
||||
|
||||
type variable struct {
|
||||
name string
|
||||
typ shaderir.Type
|
||||
forLoopCounter bool
|
||||
}
|
||||
|
||||
type constant struct {
|
||||
name string
|
||||
typ shaderir.Type
|
||||
value gconstant.Value
|
||||
}
|
||||
|
||||
type function struct {
|
||||
name string
|
||||
|
||||
ir shaderir.Func
|
||||
}
|
||||
|
||||
type compileState struct {
|
||||
fs *token.FileSet
|
||||
|
||||
vertexEntry string
|
||||
fragmentEntry string
|
||||
unit shaderir.Unit
|
||||
|
||||
ir shaderir.Program
|
||||
|
||||
funcs []function
|
||||
|
||||
global block
|
||||
|
||||
varyingParsed bool
|
||||
|
||||
errs []string
|
||||
}
|
||||
|
||||
func (cs *compileState) findFunction(name string) (int, bool) {
|
||||
for i, f := range cs.funcs {
|
||||
if f.name == name {
|
||||
return i, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (cs *compileState) findUniformVariable(name string) (int, bool) {
|
||||
for i, u := range cs.ir.UniformNames {
|
||||
if u == name {
|
||||
return i, true
|
||||
}
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
type typ struct {
|
||||
name string
|
||||
ir shaderir.Type
|
||||
}
|
||||
|
||||
type block struct {
|
||||
types []typ
|
||||
vars []variable
|
||||
unusedVars map[int]token.Pos
|
||||
consts []constant
|
||||
pos token.Pos
|
||||
outer *block
|
||||
|
||||
ir *shaderir.Block
|
||||
}
|
||||
|
||||
func (b *block) totalLocalVariableCount() int {
|
||||
c := len(b.vars)
|
||||
if b.outer != nil {
|
||||
c += b.outer.totalLocalVariableCount()
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (b *block) addNamedLocalVariable(name string, typ shaderir.Type, pos token.Pos) {
|
||||
b.vars = append(b.vars, variable{
|
||||
name: name,
|
||||
typ: typ,
|
||||
})
|
||||
if name == "_" {
|
||||
return
|
||||
}
|
||||
idx := len(b.vars) - 1
|
||||
if b.unusedVars == nil {
|
||||
b.unusedVars = map[int]token.Pos{}
|
||||
}
|
||||
b.unusedVars[idx] = pos
|
||||
}
|
||||
|
||||
func (b *block) findLocalVariable(name string, markLocalVariableUsed bool) (int, shaderir.Type, bool) {
|
||||
if name == "" || name == "_" {
|
||||
panic("shader: variable name must be non-empty and non-underscore")
|
||||
}
|
||||
|
||||
idx := 0
|
||||
for outer := b.outer; outer != nil; outer = outer.outer {
|
||||
idx += len(outer.vars)
|
||||
}
|
||||
for i, v := range b.vars {
|
||||
if v.name == name {
|
||||
if markLocalVariableUsed {
|
||||
delete(b.unusedVars, i)
|
||||
}
|
||||
return idx + i, v.typ, true
|
||||
}
|
||||
}
|
||||
if b.outer != nil {
|
||||
return b.outer.findLocalVariable(name, markLocalVariableUsed)
|
||||
}
|
||||
return 0, shaderir.Type{}, false
|
||||
}
|
||||
|
||||
func (b *block) findLocalVariableByIndex(idx int) (shaderir.Type, bool) {
|
||||
bs := []*block{b}
|
||||
for outer := b.outer; outer != nil; outer = outer.outer {
|
||||
bs = append(bs, outer)
|
||||
}
|
||||
for i := len(bs) - 1; i >= 0; i-- {
|
||||
if len(bs[i].vars) <= idx {
|
||||
idx -= len(bs[i].vars)
|
||||
continue
|
||||
}
|
||||
return bs[i].vars[idx].typ, true
|
||||
}
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
|
||||
func (b *block) findConstant(name string) (constant, bool) {
|
||||
if name == "" || name == "_" {
|
||||
panic("shader: constant name must be non-empty and non-underscore")
|
||||
}
|
||||
|
||||
for _, c := range b.consts {
|
||||
if c.name == name {
|
||||
return c, true
|
||||
}
|
||||
}
|
||||
if b.outer != nil {
|
||||
return b.outer.findConstant(name)
|
||||
}
|
||||
|
||||
return constant{}, false
|
||||
}
|
||||
|
||||
type ParseError struct {
|
||||
errs []string
|
||||
}
|
||||
|
||||
func (p *ParseError) Error() string {
|
||||
return strings.Join(p.errs, "\n")
|
||||
}
|
||||
|
||||
func Compile(src []byte, vertexEntry, fragmentEntry string, textureCount int) (*shaderir.Program, error) {
|
||||
unit, err := ParseCompilerDirectives(src)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fs := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fs, "", src, parser.AllErrors)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s := &compileState{
|
||||
fs: fs,
|
||||
vertexEntry: vertexEntry,
|
||||
fragmentEntry: fragmentEntry,
|
||||
unit: unit,
|
||||
}
|
||||
s.global.ir = &shaderir.Block{}
|
||||
s.parse(f)
|
||||
|
||||
if len(s.errs) > 0 {
|
||||
return nil, &ParseError{s.errs}
|
||||
}
|
||||
|
||||
// TODO: Resolve identifiers?
|
||||
// TODO: Resolve constants
|
||||
|
||||
// TODO: Make a call graph and reorder the elements.
|
||||
|
||||
s.ir.TextureCount = textureCount
|
||||
return &s.ir, nil
|
||||
}
|
||||
|
||||
func ParseCompilerDirectives(src []byte) (shaderir.Unit, error) {
|
||||
// TODO: Change the unit to pixels in v3 (#2645).
|
||||
unit := shaderir.Texels
|
||||
|
||||
// Go's whitespace is U+0020 (SP), U+0009 (\t), U+000d (\r), and U+000A (\n).
|
||||
// See https://go.dev/ref/spec#Tokens
|
||||
reUnit := regexp.MustCompile(`^[ \t\r\n]*//kage:unit\s+([^ \t\r\n]+)[ \t\r\n]*$`)
|
||||
var unitParsed bool
|
||||
|
||||
buf := bytes.NewBuffer(src)
|
||||
s := bufio.NewScanner(buf)
|
||||
for s.Scan() {
|
||||
m := reUnit.FindStringSubmatch(s.Text())
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
if unitParsed {
|
||||
return 0, fmt.Errorf("shader: at most one //kage:unit can exist in a shader")
|
||||
}
|
||||
switch m[1] {
|
||||
case "pixels":
|
||||
unit = shaderir.Pixels
|
||||
case "texels":
|
||||
unit = shaderir.Texels
|
||||
default:
|
||||
return 0, fmt.Errorf("shader: invalid value for //kage:unit: %s", m[1])
|
||||
}
|
||||
unitParsed = true
|
||||
}
|
||||
|
||||
return unit, nil
|
||||
}
|
||||
|
||||
func (s *compileState) addError(pos token.Pos, str string) {
|
||||
p := s.fs.Position(pos)
|
||||
s.errs = append(s.errs, fmt.Sprintf("%s: %s", p, str))
|
||||
}
|
||||
|
||||
func (cs *compileState) parse(f *ast.File) {
|
||||
cs.ir.Unit = cs.unit
|
||||
|
||||
// Parse GenDecl for global variables, and then parse functions.
|
||||
for _, d := range f.Decls {
|
||||
if _, ok := d.(*ast.FuncDecl); !ok {
|
||||
ss, ok := cs.parseDecl(&cs.global, "", d)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
cs.global.ir.Stmts = append(cs.global.ir.Stmts, ss...)
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the uniform variable so that special variable starting with __ should come first.
|
||||
var unames []string
|
||||
var utypes []shaderir.Type
|
||||
for i, u := range cs.ir.UniformNames {
|
||||
if strings.HasPrefix(u, "__") {
|
||||
unames = append(unames, u)
|
||||
utypes = append(utypes, cs.ir.Uniforms[i])
|
||||
}
|
||||
}
|
||||
// TODO: Check len(unames) == graphics.PreservedUniformVariablesNum. Unfortunately this is not true on tests.
|
||||
for i, u := range cs.ir.UniformNames {
|
||||
if !strings.HasPrefix(u, "__") {
|
||||
unames = append(unames, u)
|
||||
utypes = append(utypes, cs.ir.Uniforms[i])
|
||||
}
|
||||
}
|
||||
cs.ir.UniformNames = unames
|
||||
cs.ir.Uniforms = utypes
|
||||
|
||||
// Parse function names so that any other function call the others.
|
||||
// The function data is provisional and will be updated soon.
|
||||
for _, d := range f.Decls {
|
||||
fd, ok := d.(*ast.FuncDecl)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
n := fd.Name.Name
|
||||
if n == cs.vertexEntry {
|
||||
continue
|
||||
}
|
||||
if n == cs.fragmentEntry {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, f := range cs.funcs {
|
||||
if f.name == n {
|
||||
cs.addError(d.Pos(), fmt.Sprintf("redeclared function: %s", n))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
inParams, outParams, ret := cs.parseFuncParams(&cs.global, n, fd)
|
||||
var inT, outT []shaderir.Type
|
||||
for _, v := range inParams {
|
||||
inT = append(inT, v.typ)
|
||||
}
|
||||
for _, v := range outParams {
|
||||
outT = append(outT, v.typ)
|
||||
}
|
||||
|
||||
cs.funcs = append(cs.funcs, function{
|
||||
name: n,
|
||||
ir: shaderir.Func{
|
||||
Index: len(cs.funcs),
|
||||
InParams: inT,
|
||||
OutParams: outT,
|
||||
Return: ret,
|
||||
Block: &shaderir.Block{},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Parse functions.
|
||||
for _, d := range f.Decls {
|
||||
if f, ok := d.(*ast.FuncDecl); ok {
|
||||
ss, ok := cs.parseDecl(&cs.global, f.Name.Name, d)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
cs.global.ir.Stmts = append(cs.global.ir.Stmts, ss...)
|
||||
}
|
||||
}
|
||||
|
||||
if len(cs.errs) > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range cs.funcs {
|
||||
cs.ir.Funcs = append(cs.ir.Funcs, f.ir)
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *compileState) parseDecl(b *block, fname string, d ast.Decl) ([]shaderir.Stmt, bool) {
|
||||
var stmts []shaderir.Stmt
|
||||
|
||||
switch d := d.(type) {
|
||||
case *ast.GenDecl:
|
||||
switch d.Tok {
|
||||
case token.TYPE:
|
||||
// TODO: Parse other types
|
||||
for _, s := range d.Specs {
|
||||
s := s.(*ast.TypeSpec)
|
||||
t, ok := cs.parseType(b, fname, s.Type)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
n := s.Name.Name
|
||||
for _, t := range b.types {
|
||||
if t.name == n {
|
||||
cs.addError(s.Pos(), fmt.Sprintf("%s redeclared in this block", n))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
b.types = append(b.types, typ{
|
||||
name: n,
|
||||
ir: t,
|
||||
})
|
||||
}
|
||||
case token.CONST:
|
||||
for _, s := range d.Specs {
|
||||
s := s.(*ast.ValueSpec)
|
||||
cs, ok := cs.parseConstant(b, fname, s)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
b.consts = append(b.consts, cs...)
|
||||
}
|
||||
case token.VAR:
|
||||
for _, s := range d.Specs {
|
||||
s := s.(*ast.ValueSpec)
|
||||
vs, inits, ss, ok := cs.parseVariable(b, fname, s)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
stmts = append(stmts, ss...)
|
||||
if b == &cs.global {
|
||||
if len(inits) > 0 {
|
||||
cs.addError(s.Pos(), "a uniform variable cannot have initial values")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// TODO: Should rhs be ignored?
|
||||
for i, v := range vs {
|
||||
if !strings.HasPrefix(v.name, "__") {
|
||||
if v.name[0] < 'A' || 'Z' < v.name[0] {
|
||||
cs.addError(s.Names[i].Pos(), fmt.Sprintf("global variables must be exposed: %s", v.name))
|
||||
}
|
||||
}
|
||||
for _, name := range cs.ir.UniformNames {
|
||||
if name == v.name {
|
||||
cs.addError(s.Pos(), fmt.Sprintf("%s redeclared in this block", name))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
cs.ir.UniformNames = append(cs.ir.UniformNames, v.name)
|
||||
cs.ir.Uniforms = append(cs.ir.Uniforms, v.typ)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// base must be obtained before adding the variables.
|
||||
base := b.totalLocalVariableCount()
|
||||
for _, v := range vs {
|
||||
b.addNamedLocalVariable(v.name, v.typ, d.Pos())
|
||||
}
|
||||
|
||||
if len(inits) > 0 {
|
||||
for i := range vs {
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{
|
||||
{
|
||||
Type: shaderir.LocalVariable,
|
||||
Index: base + i,
|
||||
},
|
||||
inits[i],
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
case token.IMPORT:
|
||||
cs.addError(d.Pos(), "import is forbidden")
|
||||
default:
|
||||
cs.addError(d.Pos(), "unexpected token")
|
||||
}
|
||||
case *ast.FuncDecl:
|
||||
f, ok := cs.parseFunc(b, d)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if b != &cs.global {
|
||||
cs.addError(d.Pos(), "non-global function is not implemented")
|
||||
return nil, false
|
||||
}
|
||||
switch d.Name.Name {
|
||||
case cs.vertexEntry:
|
||||
cs.ir.VertexFunc.Block = f.ir.Block
|
||||
case cs.fragmentEntry:
|
||||
cs.ir.FragmentFunc.Block = f.ir.Block
|
||||
default:
|
||||
// The function is already registered for their names.
|
||||
for i := range cs.funcs {
|
||||
if cs.funcs[i].name == d.Name.Name {
|
||||
// Index is already determined by the provisional parsing.
|
||||
f.ir.Index = cs.funcs[i].ir.Index
|
||||
cs.funcs[i] = f
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
cs.addError(d.Pos(), "unexpected decl")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return stmts, true
|
||||
}
|
||||
|
||||
// functionReturnTypes returns the original returning value types, if the given expression is call.
|
||||
//
|
||||
// Note that parseExpr returns the returning types for IR, not the original function.
|
||||
func (cs *compileState) functionReturnTypes(block *block, expr ast.Expr) ([]shaderir.Type, bool) {
|
||||
call, ok := expr.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
ident, ok := call.Fun.(*ast.Ident)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
for _, f := range cs.funcs {
|
||||
if f.name == ident.Name {
|
||||
// TODO: Is it correct to combine out-params and return param?
|
||||
ts := f.ir.OutParams
|
||||
if f.ir.Return.Main != shaderir.None {
|
||||
ts = append(ts, f.ir.Return)
|
||||
}
|
||||
return ts, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (s *compileState) parseVariable(block *block, fname string, vs *ast.ValueSpec) ([]variable, []shaderir.Expr, []shaderir.Stmt, bool) {
|
||||
if len(vs.Names) != len(vs.Values) && len(vs.Values) != 1 && len(vs.Values) != 0 {
|
||||
s.addError(vs.Pos(), "the numbers of lhs and rhs don't match")
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
|
||||
var declt shaderir.Type
|
||||
if vs.Type != nil {
|
||||
var ok bool
|
||||
declt, ok = s.parseType(block, fname, vs.Type)
|
||||
if !ok {
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
vars []variable
|
||||
inits []shaderir.Expr
|
||||
stmts []shaderir.Stmt
|
||||
)
|
||||
|
||||
// These variables are used only in multiple-value context.
|
||||
var inittypes []shaderir.Type
|
||||
var initexprs []shaderir.Expr
|
||||
|
||||
for i, n := range vs.Names {
|
||||
t := declt
|
||||
switch {
|
||||
case len(vs.Values) == 0:
|
||||
// No initialization
|
||||
|
||||
case len(vs.Names) == len(vs.Values):
|
||||
// Single-value context
|
||||
|
||||
init := vs.Values[i]
|
||||
|
||||
es, rts, ss, ok := s.parseExpr(block, fname, init, true)
|
||||
if !ok {
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
|
||||
if t.Main == shaderir.None {
|
||||
ts, ok := s.functionReturnTypes(block, init)
|
||||
if !ok {
|
||||
ts = rts
|
||||
}
|
||||
if len(ts) > 1 {
|
||||
s.addError(vs.Pos(), "the numbers of lhs and rhs don't match")
|
||||
}
|
||||
t = ts[0]
|
||||
if t.Main == shaderir.None {
|
||||
t = toDefaultType(es[0].Const)
|
||||
}
|
||||
}
|
||||
|
||||
for i, rt := range rts {
|
||||
if !canAssign(&t, &rt, es[i].Const) {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("cannot use type %s as type %s in variable declaration", rt.String(), t.String()))
|
||||
}
|
||||
}
|
||||
|
||||
inits = append(inits, es...)
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
default:
|
||||
// Multiple-value context
|
||||
// See testcase/var_multiple.go for an actual case.
|
||||
|
||||
if i == 0 {
|
||||
init := vs.Values[0]
|
||||
|
||||
var ss []shaderir.Stmt
|
||||
var ok bool
|
||||
initexprs, inittypes, ss, ok = s.parseExpr(block, fname, init, true)
|
||||
if !ok {
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
if t.Main == shaderir.None {
|
||||
ts, ok := s.functionReturnTypes(block, init)
|
||||
if ok {
|
||||
inittypes = ts
|
||||
}
|
||||
if len(ts) != len(vs.Names) {
|
||||
s.addError(vs.Pos(), "the numbers of lhs and rhs don't match")
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if t.Main == shaderir.None && len(inittypes) > 0 {
|
||||
t = inittypes[i]
|
||||
// TODO: Is it possible to reach this?
|
||||
if t.Main == shaderir.None {
|
||||
t = toDefaultType(initexprs[i].Const)
|
||||
}
|
||||
}
|
||||
|
||||
if !canAssign(&t, &inittypes[i], initexprs[i].Const) {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("cannot use type %s as type %s in variable declaration", inittypes[i].String(), t.String()))
|
||||
}
|
||||
|
||||
// Add the same initexprs for each variable.
|
||||
inits = append(inits, initexprs...)
|
||||
}
|
||||
|
||||
name := n.Name
|
||||
for _, v := range append(block.vars, vars...) {
|
||||
if v.name == name {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("duplicated local variable name: %s", name))
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
}
|
||||
for _, c := range block.consts {
|
||||
if c.name == name {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("duplicated local constant/variable name: %s", name))
|
||||
return nil, nil, nil, false
|
||||
}
|
||||
}
|
||||
vars = append(vars, variable{
|
||||
name: name,
|
||||
typ: t,
|
||||
})
|
||||
}
|
||||
|
||||
return vars, inits, stmts, true
|
||||
}
|
||||
|
||||
func (s *compileState) parseConstant(block *block, fname string, vs *ast.ValueSpec) ([]constant, bool) {
|
||||
var t shaderir.Type
|
||||
if vs.Type != nil {
|
||||
var ok bool
|
||||
t, ok = s.parseType(block, fname, vs.Type)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
var cs []constant
|
||||
for i, n := range vs.Names {
|
||||
name := n.Name
|
||||
for _, c := range block.consts {
|
||||
if c.name == name {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("duplicated local constant name: %s", name))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
for _, v := range block.vars {
|
||||
if v.name == name {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("duplicated local constant/variable name: %s", name))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
es, ts, ss, ok := s.parseExpr(block, fname, vs.Values[i], false)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if len(ss) > 0 {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("invalid constant expression: %s", name))
|
||||
return nil, false
|
||||
}
|
||||
if len(ts) != 1 || len(es) != 1 {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("invalid constant expression: %s", n))
|
||||
return nil, false
|
||||
}
|
||||
if es[0].Type != shaderir.NumberExpr {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("constant expression must be a number but not: %s", n))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if !t.Equal(&shaderir.Type{}) && !canAssign(&t, &ts[0], es[0].Const) {
|
||||
s.addError(vs.Pos(), fmt.Sprintf("cannot use %v as %s value in constant declaration", es[0].Const, t.String()))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
c := es[0].Const
|
||||
switch t.Main {
|
||||
case shaderir.Bool:
|
||||
case shaderir.Int:
|
||||
c = gconstant.ToInt(c)
|
||||
case shaderir.Float:
|
||||
c = gconstant.ToFloat(c)
|
||||
}
|
||||
|
||||
cs = append(cs, constant{
|
||||
name: name,
|
||||
typ: t,
|
||||
value: c,
|
||||
})
|
||||
}
|
||||
return cs, true
|
||||
}
|
||||
|
||||
func (cs *compileState) parseFuncParams(block *block, fname string, d *ast.FuncDecl) (in, out []variable, ret shaderir.Type) {
|
||||
for _, f := range d.Type.Params.List {
|
||||
t, ok := cs.parseType(block, fname, f.Type)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
for _, n := range f.Names {
|
||||
in = append(in, variable{
|
||||
name: n.Name,
|
||||
typ: t,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if d.Type.Results == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, f := range d.Type.Results.List {
|
||||
t, ok := cs.parseType(block, fname, f.Type)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if len(f.Names) == 0 {
|
||||
out = append(out, variable{
|
||||
name: "",
|
||||
typ: t,
|
||||
})
|
||||
} else {
|
||||
for _, n := range f.Names {
|
||||
out = append(out, variable{
|
||||
name: n.Name,
|
||||
typ: t,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If there is only one returning value, it is treated as a returning value.
|
||||
// An array cannot be a returning value, especially for HLSL (#2923).
|
||||
if len(out) == 1 && out[0].name == "" && out[0].typ.Main != shaderir.Array {
|
||||
ret = out[0].typ
|
||||
out = nil
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (cs *compileState) parseFunc(block *block, d *ast.FuncDecl) (function, bool) {
|
||||
if d.Name == nil {
|
||||
cs.addError(d.Pos(), "function must have a name")
|
||||
return function{}, false
|
||||
}
|
||||
if d.Name.Name == "init" {
|
||||
cs.addError(d.Pos(), "init function is not implemented")
|
||||
return function{}, false
|
||||
}
|
||||
if d.Body == nil {
|
||||
cs.addError(d.Pos(), "function must have a body")
|
||||
return function{}, false
|
||||
}
|
||||
|
||||
inParams, outParams, returnType := cs.parseFuncParams(block, d.Name.Name, d)
|
||||
|
||||
checkVaryings := func(vs []variable) {
|
||||
if len(cs.ir.Varyings) != len(vs) {
|
||||
cs.addError(d.Pos(), "the number of vertex entry point's returning values and the number of fragment entry point's params must be the same")
|
||||
return
|
||||
}
|
||||
for i, t := range cs.ir.Varyings {
|
||||
if t.Main != vs[i].typ.Main {
|
||||
cs.addError(d.Pos(), "vertex entry point's returning value types and fragment entry point's param types must match")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if block == &cs.global {
|
||||
switch d.Name.Name {
|
||||
case cs.vertexEntry:
|
||||
for _, v := range inParams {
|
||||
cs.ir.Attributes = append(cs.ir.Attributes, v.typ)
|
||||
}
|
||||
|
||||
// For the vertex entry, a parameter (variable) is used as a returning value.
|
||||
// For example, GLSL doesn't treat gl_Position as a returning value.
|
||||
// TODO: This can be resolved by having an indirect function like what the fragment entry already does.
|
||||
// See internal/shaderir/glsl.adjustProgram.
|
||||
if len(outParams) == 0 {
|
||||
outParams = append(outParams, variable{
|
||||
typ: shaderir.Type{Main: shaderir.Vec4},
|
||||
})
|
||||
}
|
||||
|
||||
// The first out-param is treated as gl_Position in GLSL.
|
||||
if outParams[0].typ.Main != shaderir.Vec4 {
|
||||
cs.addError(d.Pos(), "vertex entry point must have at least one returning vec4 value for a position")
|
||||
return function{}, false
|
||||
}
|
||||
|
||||
if cs.varyingParsed {
|
||||
checkVaryings(outParams[1:])
|
||||
} else {
|
||||
for _, v := range outParams[1:] {
|
||||
// TODO: Check that these params are not arrays or structs
|
||||
cs.ir.Varyings = append(cs.ir.Varyings, v.typ)
|
||||
}
|
||||
}
|
||||
cs.varyingParsed = true
|
||||
case cs.fragmentEntry:
|
||||
if len(inParams) == 0 {
|
||||
cs.addError(d.Pos(), "fragment entry point must have at least one vec4 parameter for a position")
|
||||
return function{}, false
|
||||
}
|
||||
if inParams[0].typ.Main != shaderir.Vec4 {
|
||||
cs.addError(d.Pos(), "fragment entry point must have at least one vec4 parameter for a position")
|
||||
return function{}, false
|
||||
}
|
||||
|
||||
if len(outParams) != 0 || returnType.Main != shaderir.Vec4 {
|
||||
cs.addError(d.Pos(), "fragment entry point must have one returning vec4 value for a color")
|
||||
return function{}, false
|
||||
}
|
||||
|
||||
if cs.varyingParsed {
|
||||
checkVaryings(inParams[1:])
|
||||
} else {
|
||||
for _, v := range inParams[1:] {
|
||||
cs.ir.Varyings = append(cs.ir.Varyings, v.typ)
|
||||
}
|
||||
}
|
||||
cs.varyingParsed = true
|
||||
}
|
||||
}
|
||||
|
||||
b, ok := cs.parseBlock(block, d.Name.Name, d.Body.List, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return function{}, false
|
||||
}
|
||||
|
||||
if len(outParams) > 0 || returnType.Main != shaderir.None {
|
||||
var hasReturn func(stmts []shaderir.Stmt) bool
|
||||
hasReturn = func(stmts []shaderir.Stmt) bool {
|
||||
for _, stmt := range stmts {
|
||||
if stmt.Type == shaderir.Return {
|
||||
return true
|
||||
}
|
||||
for _, b := range stmt.Blocks {
|
||||
if hasReturn(b.Stmts) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if !hasReturn(b.ir.Stmts) {
|
||||
cs.addError(d.Pos(), fmt.Sprintf("function %s must have a return statement but not", d.Name))
|
||||
return function{}, false
|
||||
}
|
||||
}
|
||||
|
||||
var inT, outT []shaderir.Type
|
||||
for _, v := range inParams {
|
||||
inT = append(inT, v.typ)
|
||||
}
|
||||
for _, v := range outParams {
|
||||
outT = append(outT, v.typ)
|
||||
}
|
||||
|
||||
return function{
|
||||
name: d.Name.Name,
|
||||
ir: shaderir.Func{
|
||||
InParams: inT,
|
||||
OutParams: outT,
|
||||
Return: returnType,
|
||||
Block: b.ir,
|
||||
},
|
||||
}, true
|
||||
}
|
||||
|
||||
func (cs *compileState) parseBlock(outer *block, fname string, stmts []ast.Stmt, inParams, outParams []variable, returnType shaderir.Type, checkLocalVariableUsage bool) (*block, bool) {
|
||||
var vars []variable
|
||||
if outer == &cs.global {
|
||||
vars = make([]variable, 0, len(inParams)+len(outParams))
|
||||
vars = append(vars, inParams...)
|
||||
vars = append(vars, outParams...)
|
||||
}
|
||||
|
||||
var offset int
|
||||
for b := outer; b != nil; b = b.outer {
|
||||
offset += len(b.vars)
|
||||
}
|
||||
if outer == &cs.global {
|
||||
offset += len(inParams) + len(outParams)
|
||||
}
|
||||
|
||||
block := &block{
|
||||
vars: vars,
|
||||
outer: outer,
|
||||
ir: &shaderir.Block{
|
||||
LocalVarIndexOffset: offset,
|
||||
},
|
||||
}
|
||||
|
||||
defer func() {
|
||||
var offset int
|
||||
if outer == &cs.global {
|
||||
offset = len(inParams) + len(outParams)
|
||||
}
|
||||
for _, v := range block.vars[offset:] {
|
||||
if v.forLoopCounter {
|
||||
block.ir.LocalVars = append(block.ir.LocalVars, shaderir.Type{})
|
||||
continue
|
||||
}
|
||||
block.ir.LocalVars = append(block.ir.LocalVars, v.typ)
|
||||
}
|
||||
}()
|
||||
|
||||
if outer.outer == nil && len(outParams) > 0 && outParams[0].name != "" {
|
||||
for i := range outParams {
|
||||
block.ir.Stmts = append(block.ir.Stmts, shaderir.Stmt{
|
||||
Type: shaderir.Init,
|
||||
InitIndex: len(inParams) + i,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, stmt := range stmts {
|
||||
ss, ok := cs.parseStmt(block, fname, stmt, inParams, outParams, returnType)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
block.ir.Stmts = append(block.ir.Stmts, ss...)
|
||||
}
|
||||
|
||||
if checkLocalVariableUsage && len(block.unusedVars) > 0 {
|
||||
for idx, pos := range block.unusedVars {
|
||||
cs.addError(pos, fmt.Sprintf("local variable %s is not used", block.vars[idx].name))
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return block, true
|
||||
}
|
||||
+909
@@ -0,0 +1,909 @@
|
||||
// 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"
|
||||
gconstant "go/constant"
|
||||
"go/token"
|
||||
"strings"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||
)
|
||||
|
||||
func (cs *compileState) forceToInt(node ast.Node, expr *shaderir.Expr) bool {
|
||||
if !canTruncateToInteger(expr.Const) {
|
||||
cs.addError(node.Pos(), fmt.Sprintf("constant %s truncated to integer", expr.Const.String()))
|
||||
return false
|
||||
}
|
||||
expr.Const = gconstant.ToInt(expr.Const)
|
||||
return true
|
||||
}
|
||||
|
||||
func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inParams, outParams []variable, returnType shaderir.Type) ([]shaderir.Stmt, bool) {
|
||||
var stmts []shaderir.Stmt
|
||||
|
||||
switch stmt := stmt.(type) {
|
||||
case *ast.AssignStmt:
|
||||
switch stmt.Tok {
|
||||
case token.DEFINE:
|
||||
if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
|
||||
cs.addError(stmt.Pos(), "single-value context and multiple-value context cannot be mixed")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
ss, ok := cs.assign(block, fname, stmt.Pos(), stmt.Lhs, stmt.Rhs, inParams, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
case token.ASSIGN:
|
||||
if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
|
||||
cs.addError(stmt.Pos(), "single-value context and multiple-value context cannot be mixed")
|
||||
return nil, false
|
||||
}
|
||||
ss, ok := cs.assign(block, fname, stmt.Pos(), stmt.Lhs, stmt.Rhs, inParams, false)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
case token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, token.XOR_ASSIGN, token.AND_NOT_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN:
|
||||
rhs, rts, ss, ok := cs.parseExpr(block, fname, stmt.Rhs[0], true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
lhs, lts, ss, ok := cs.parseExpr(block, fname, stmt.Lhs[0], true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
if lhs[0].Type == shaderir.UniformVariable {
|
||||
cs.addError(stmt.Pos(), "a uniform variable cannot be assigned")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
var op shaderir.Op
|
||||
switch stmt.Tok {
|
||||
case token.ADD_ASSIGN:
|
||||
op = shaderir.Add
|
||||
case token.SUB_ASSIGN:
|
||||
op = shaderir.Sub
|
||||
case token.MUL_ASSIGN:
|
||||
if lts[0].IsMatrix() || rts[0].IsMatrix() {
|
||||
op = shaderir.MatrixMul
|
||||
} else {
|
||||
op = shaderir.ComponentWiseMul
|
||||
}
|
||||
case token.QUO_ASSIGN:
|
||||
op = shaderir.Div
|
||||
case token.REM_ASSIGN:
|
||||
op = shaderir.ModOp
|
||||
case token.AND_ASSIGN:
|
||||
op = shaderir.And
|
||||
case token.OR_ASSIGN:
|
||||
op = shaderir.Or
|
||||
case token.XOR_ASSIGN:
|
||||
op = shaderir.Xor
|
||||
case token.SHL_ASSIGN:
|
||||
op = shaderir.LeftShift
|
||||
case token.SHR_ASSIGN:
|
||||
op = shaderir.RightShift
|
||||
default:
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if lts[0].Main == rts[0].Main {
|
||||
if op == shaderir.Div && rts[0].IsMatrix() {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator / not defined on %s", rts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
|
||||
if lts[0].Main != shaderir.Int && !lts[0].IsIntVector() {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
|
||||
}
|
||||
if rts[0].Main != shaderir.Int && !rts[0].IsIntVector() {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, rts[0].String()))
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
if lts[0].Main == shaderir.Int && rhs[0].Const != nil {
|
||||
if !cs.forceToInt(stmt, &rhs[0]) {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
switch lts[0].Main {
|
||||
case shaderir.Int, shaderir.IVec2, shaderir.IVec3, shaderir.IVec4:
|
||||
if rts[0].Main != shaderir.Int {
|
||||
if !rts[0].Equal(&shaderir.Type{}) {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
if !cs.forceToInt(stmt, &rhs[0]) {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
case shaderir.Float:
|
||||
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
|
||||
} else if rhs[0].Const != nil &&
|
||||
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
|
||||
gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown {
|
||||
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
|
||||
} else {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
|
||||
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
|
||||
} else if (op == shaderir.MatrixMul || op == shaderir.Div) &&
|
||||
(rts[0].Main == shaderir.Float ||
|
||||
(rhs[0].Const != nil &&
|
||||
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
|
||||
gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) {
|
||||
if rhs[0].Const != nil {
|
||||
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
|
||||
}
|
||||
} else if op == shaderir.MatrixMul && ((lts[0].Main == shaderir.Vec2 && rts[0].Main == shaderir.Mat2) ||
|
||||
(lts[0].Main == shaderir.Vec3 && rts[0].Main == shaderir.Mat3) ||
|
||||
(lts[0].Main == shaderir.Vec4 && rts[0].Main == shaderir.Mat4)) {
|
||||
// OK
|
||||
} else if (op == shaderir.MatrixMul || op == shaderir.ComponentWiseMul || lts[0].IsFloatVector()) &&
|
||||
(rts[0].Main == shaderir.Float ||
|
||||
(rhs[0].Const != nil &&
|
||||
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
|
||||
gconstant.ToFloat(rhs[0].Const).Kind() != gconstant.Unknown)) {
|
||||
if rhs[0].Const != nil {
|
||||
rhs[0].Const = gconstant.ToFloat(rhs[0].Const)
|
||||
}
|
||||
} else {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
default:
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: mismatched types %s and %s", lts[0].String(), rts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
if op == shaderir.ModOp && lts[0].Main != shaderir.Int && lts[0].Main != shaderir.IVec2 && lts[0].Main != shaderir.IVec3 && lts[0].Main != shaderir.IVec4 {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %% not defined on %s", lts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{
|
||||
lhs[0],
|
||||
{
|
||||
Type: shaderir.Binary,
|
||||
Op: op,
|
||||
Exprs: []shaderir.Expr{
|
||||
lhs[0],
|
||||
rhs[0],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
default:
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
|
||||
}
|
||||
case *ast.BlockStmt:
|
||||
b, ok := cs.parseBlock(block, fname, stmt.List, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.BlockStmt,
|
||||
Blocks: []*shaderir.Block{
|
||||
b.ir,
|
||||
},
|
||||
})
|
||||
case *ast.DeclStmt:
|
||||
ss, ok := cs.parseDecl(block, fname, stmt.Decl)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
case *ast.ForStmt:
|
||||
ss, ok := cs.parseFor(block, fname, stmt, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
case *ast.IfStmt:
|
||||
if stmt.Init != nil {
|
||||
init := stmt.Init
|
||||
stmt.Init = nil
|
||||
b, ok := cs.parseBlock(block, fname, []ast.Stmt{init, stmt}, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.BlockStmt,
|
||||
Blocks: []*shaderir.Block{b.ir},
|
||||
})
|
||||
return stmts, true
|
||||
}
|
||||
|
||||
exprs, ts, ss, ok := cs.parseExpr(block, fname, stmt.Cond, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if len(ts) != 1 || ts[0].Main != shaderir.Bool {
|
||||
var tss []string
|
||||
for _, t := range ts {
|
||||
tss = append(tss, t.String())
|
||||
}
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("if-condition must be bool but: %s", strings.Join(tss, ", ")))
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
var bs []*shaderir.Block
|
||||
b, ok := cs.parseBlock(block, fname, stmt.Body.List, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
bs = append(bs, b.ir)
|
||||
|
||||
if stmt.Else != nil {
|
||||
switch s := stmt.Else.(type) {
|
||||
case *ast.BlockStmt:
|
||||
b, ok := cs.parseBlock(block, fname, s.List, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
bs = append(bs, b.ir)
|
||||
default:
|
||||
b, ok := cs.parseBlock(block, fname, []ast.Stmt{s}, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
bs = append(bs, b.ir)
|
||||
}
|
||||
}
|
||||
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.If,
|
||||
Exprs: exprs,
|
||||
Blocks: bs,
|
||||
})
|
||||
|
||||
case *ast.IncDecStmt:
|
||||
exprs, ts, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
var op shaderir.Op
|
||||
switch stmt.Tok {
|
||||
case token.INC:
|
||||
op = shaderir.Add
|
||||
case token.DEC:
|
||||
op = shaderir.Sub
|
||||
}
|
||||
var c gconstant.Value
|
||||
switch {
|
||||
case ts[0].Main == shaderir.Int, ts[0].IsIntVector():
|
||||
c = gconstant.MakeInt64(1)
|
||||
case ts[0].Main == shaderir.Float, ts[0].IsFloatVector():
|
||||
c = gconstant.MakeFloat64(1)
|
||||
default:
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation %s (non-numeric type %s)", stmt.Tok.String(), ts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{
|
||||
exprs[0],
|
||||
{
|
||||
Type: shaderir.Binary,
|
||||
Op: op,
|
||||
Exprs: []shaderir.Expr{
|
||||
exprs[0],
|
||||
{
|
||||
Type: shaderir.NumberExpr,
|
||||
Const: c,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
case *ast.ReturnStmt:
|
||||
if len(stmt.Results) != len(outParams) && len(stmt.Results) != 1 {
|
||||
if !(len(stmt.Results) == 0 && len(outParams) > 0 && outParams[0].name != "") {
|
||||
// TODO: Check variable shadowings.
|
||||
// https://go.dev/ref/spec#Return_statements
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
var exprs []shaderir.Expr
|
||||
var types []shaderir.Type
|
||||
for _, r := range stmt.Results {
|
||||
es, ts, ss, ok := cs.parseExpr(block, fname, r, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
if len(es) > 1 && (len(stmt.Results) > 1 || len(outParams) == 1) {
|
||||
cs.addError(r.Pos(), "single-value context and multiple-value context cannot be mixed")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if len(outParams) > 1 && len(stmt.Results) == 1 {
|
||||
if len(es) == 1 {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
|
||||
return nil, false
|
||||
}
|
||||
if len(es) > 1 && len(es) != len(outParams) {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(es)))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
exprs = append(exprs, es...)
|
||||
types = append(types, ts...)
|
||||
}
|
||||
|
||||
for i, t := range types {
|
||||
expr := exprs[i]
|
||||
var outT shaderir.Type
|
||||
if len(outParams) == 0 {
|
||||
outT = returnType
|
||||
} else {
|
||||
outT = outParams[i].typ
|
||||
}
|
||||
if expr.Const != nil {
|
||||
switch outT.Main {
|
||||
case shaderir.Bool:
|
||||
if expr.Const.Kind() != gconstant.Bool {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
||||
return nil, false
|
||||
}
|
||||
t = shaderir.Type{Main: shaderir.Bool}
|
||||
case shaderir.Int:
|
||||
if gconstant.ToInt(expr.Const).Kind() == gconstant.Unknown {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
||||
return nil, false
|
||||
}
|
||||
expr.Const = gconstant.ToInt(expr.Const)
|
||||
t = shaderir.Type{Main: shaderir.Int}
|
||||
case shaderir.Float:
|
||||
if gconstant.ToFloat(expr.Const).Kind() == gconstant.Unknown {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
||||
return nil, false
|
||||
}
|
||||
expr.Const = gconstant.ToFloat(expr.Const)
|
||||
t = shaderir.Type{Main: shaderir.Float}
|
||||
}
|
||||
}
|
||||
|
||||
if !t.Equal(&outT) {
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outT))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if len(outParams) > 0 {
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{
|
||||
{
|
||||
Type: shaderir.LocalVariable,
|
||||
Index: len(inParams) + i,
|
||||
},
|
||||
expr,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Return,
|
||||
Exprs: []shaderir.Expr{expr},
|
||||
})
|
||||
// When a return type is specified, there should be only one expr here.
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(outParams) > 0 {
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Return,
|
||||
})
|
||||
}
|
||||
|
||||
case *ast.BranchStmt:
|
||||
switch stmt.Tok {
|
||||
case token.BREAK:
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Break,
|
||||
})
|
||||
case token.CONTINUE:
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Continue,
|
||||
})
|
||||
default:
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("invalid token: %s", stmt.Tok))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
case *ast.ExprStmt:
|
||||
if _, ok := stmt.X.(*ast.CallExpr); !ok {
|
||||
cs.addError(stmt.Pos(), "the statement is evaluated but not used")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
exprs, _, ss, ok := cs.parseExpr(block, fname, stmt.X, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
for _, expr := range exprs {
|
||||
// There can be a non-call expr like LocalVariable expressions.
|
||||
// These are necessary to be used as arguments for an outside function callers.
|
||||
if expr.Type != shaderir.Call {
|
||||
continue
|
||||
}
|
||||
if expr.Exprs[0].Type == shaderir.BuiltinFuncExpr {
|
||||
cs.addError(stmt.Pos(), "the statement is evaluated but not used")
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.ExprStmt,
|
||||
Exprs: []shaderir.Expr{expr},
|
||||
})
|
||||
}
|
||||
|
||||
default:
|
||||
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected statement: %#v", stmt))
|
||||
return nil, false
|
||||
}
|
||||
return stmts, true
|
||||
}
|
||||
|
||||
func (cs *compileState) assign(block *block, fname string, pos token.Pos, lhs, rhs []ast.Expr, inParams []variable, define bool) ([]shaderir.Stmt, bool) {
|
||||
var stmts []shaderir.Stmt
|
||||
var rhsExprs []shaderir.Expr
|
||||
var rhsTypes []shaderir.Type
|
||||
allblank := true
|
||||
|
||||
if len(lhs) == len(rhs) {
|
||||
for i, e := range lhs {
|
||||
// Prase RHS first for the order of the statements.
|
||||
r, rts, ss, ok := cs.parseExpr(block, fname, rhs[i], true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
if define {
|
||||
if _, ok := e.(*ast.Ident); !ok {
|
||||
cs.addError(pos, "non-name on the left side of :=")
|
||||
return nil, false
|
||||
}
|
||||
name := e.(*ast.Ident).Name
|
||||
if name != "_" {
|
||||
for _, v := range block.vars {
|
||||
if v.name == name {
|
||||
cs.addError(pos, fmt.Sprintf("duplicated local variable name: %s", name))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
}
|
||||
ts, ok := cs.functionReturnTypes(block, rhs[i])
|
||||
if !ok {
|
||||
ts = rts
|
||||
}
|
||||
if len(ts) > 1 {
|
||||
cs.addError(pos, "single-value context and multiple-value context cannot be mixed")
|
||||
return nil, false
|
||||
}
|
||||
t := ts[0]
|
||||
if t.Main == shaderir.None {
|
||||
t = toDefaultType(r[0].Const)
|
||||
}
|
||||
block.addNamedLocalVariable(name, t, e.Pos())
|
||||
}
|
||||
|
||||
if len(r) > 1 {
|
||||
cs.addError(pos, "single-value context and multiple-value context cannot be mixed")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
l, lts, ss, ok := cs.parseExpr(block, fname, lhs[i], false)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
if len(l) != len(r) {
|
||||
if len(r) == 0 {
|
||||
cs.addError(pos, "right-hand side (no value) used as value")
|
||||
} else {
|
||||
cs.addError(pos, fmt.Sprintf("assignment mismatch: %d variables but the right-hand side has %d values", len(l), len(r)))
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if l[0].Type == shaderir.Blank {
|
||||
continue
|
||||
}
|
||||
|
||||
var isAssignmentForbidden func(e *shaderir.Expr) bool
|
||||
isAssignmentForbidden = func(e *shaderir.Expr) bool {
|
||||
switch e.Type {
|
||||
case shaderir.UniformVariable:
|
||||
return true
|
||||
case shaderir.LocalVariable:
|
||||
if fname == cs.vertexEntry || fname == cs.fragmentEntry {
|
||||
return e.Index < len(inParams)
|
||||
}
|
||||
case shaderir.FieldSelector:
|
||||
return isAssignmentForbidden(&e.Exprs[0])
|
||||
case shaderir.Index:
|
||||
return isAssignmentForbidden(&e.Exprs[0])
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if isAssignmentForbidden(&l[0]) {
|
||||
cs.addError(pos, "a uniform variable cannot be assigned")
|
||||
return nil, false
|
||||
}
|
||||
allblank = false
|
||||
|
||||
for i := range lts {
|
||||
if !canAssign(<s[i], &rts[i], r[i].Const) {
|
||||
cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rts[i].String(), lts[i].String()))
|
||||
return nil, false
|
||||
}
|
||||
switch lts[0].Main {
|
||||
case shaderir.Int:
|
||||
r[i].Const = gconstant.ToInt(r[i].Const)
|
||||
case shaderir.Float:
|
||||
r[i].Const = gconstant.ToFloat(r[i].Const)
|
||||
}
|
||||
}
|
||||
|
||||
if len(lhs) == 1 {
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{l[0], r[0]},
|
||||
})
|
||||
} else {
|
||||
// For variable swapping, use temporary variables.
|
||||
t := rts[0]
|
||||
if t.Main == shaderir.None {
|
||||
t = toDefaultType(r[0].Const)
|
||||
}
|
||||
block.vars = append(block.vars, variable{
|
||||
typ: t,
|
||||
})
|
||||
idx := block.totalLocalVariableCount() - 1
|
||||
stmts = append(stmts,
|
||||
shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{
|
||||
{
|
||||
Type: shaderir.LocalVariable,
|
||||
Index: idx,
|
||||
},
|
||||
r[0],
|
||||
},
|
||||
},
|
||||
shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{
|
||||
l[0],
|
||||
{
|
||||
Type: shaderir.LocalVariable,
|
||||
Index: idx,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var ss []shaderir.Stmt
|
||||
var ok bool
|
||||
rhsExprs, rhsTypes, ss, ok = cs.parseExpr(block, fname, rhs[0], true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if len(lhs) != len(rhsExprs) {
|
||||
cs.addError(pos, fmt.Sprintf("assignment mismatch: %d variables but %d", len(lhs), len(rhsExprs)))
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
for i, e := range lhs {
|
||||
if define {
|
||||
if _, ok := e.(*ast.Ident); !ok {
|
||||
cs.addError(pos, "non-name on the left side of :=")
|
||||
return nil, false
|
||||
}
|
||||
name := e.(*ast.Ident).Name
|
||||
if name != "_" {
|
||||
for _, v := range block.vars {
|
||||
if v.name == name {
|
||||
cs.addError(pos, fmt.Sprintf("duplicated local variable name: %s", name))
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
}
|
||||
t := rhsTypes[i]
|
||||
if t.Main == shaderir.None {
|
||||
// TODO: This is to determine a type when the rhs values are constants (not literals),
|
||||
// but there are no actual cases when len(lhs) != len(rhs). Is this correct?
|
||||
t = toDefaultType(rhsExprs[i].Const)
|
||||
}
|
||||
block.addNamedLocalVariable(name, t, e.Pos())
|
||||
}
|
||||
|
||||
l, lts, ss, ok := cs.parseExpr(block, fname, lhs[i], false)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
stmts = append(stmts, ss...)
|
||||
|
||||
if len(l) != 1 {
|
||||
cs.addError(pos, fmt.Sprintf("unexpected count of types in lhs: %d", len(l)))
|
||||
return nil, false
|
||||
}
|
||||
if len(lts) != 1 {
|
||||
cs.addError(pos, fmt.Sprintf("unexpected count of expressions in lhs: %d", len(l)))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if l[0].Type == shaderir.Blank {
|
||||
continue
|
||||
}
|
||||
allblank = false
|
||||
|
||||
if !canAssign(<s[0], &rhsTypes[i], rhsExprs[i].Const) {
|
||||
cs.addError(pos, fmt.Sprintf("cannot use type %s as type %s in variable declaration", rhsTypes[i].String(), lts[0].String()))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
stmts = append(stmts, shaderir.Stmt{
|
||||
Type: shaderir.Assign,
|
||||
Exprs: []shaderir.Expr{l[0], rhsExprs[i]},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if define && allblank {
|
||||
cs.addError(pos, "no new variables on left side of :=")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return stmts, true
|
||||
}
|
||||
|
||||
func toDefaultType(v gconstant.Value) shaderir.Type {
|
||||
switch v.Kind() {
|
||||
case gconstant.Bool:
|
||||
return shaderir.Type{Main: shaderir.Bool}
|
||||
case gconstant.Int:
|
||||
return shaderir.Type{Main: shaderir.Int}
|
||||
case gconstant.Float:
|
||||
return shaderir.Type{Main: shaderir.Float}
|
||||
}
|
||||
// TODO: Should this be an error?
|
||||
return shaderir.Type{}
|
||||
}
|
||||
|
||||
func canAssign(lt *shaderir.Type, rt *shaderir.Type, rc gconstant.Value) bool {
|
||||
if lt.Equal(rt) {
|
||||
return true
|
||||
}
|
||||
|
||||
if rc == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if !rt.Equal(&shaderir.Type{}) {
|
||||
return false
|
||||
}
|
||||
|
||||
switch lt.Main {
|
||||
case shaderir.Bool:
|
||||
return rc.Kind() == gconstant.Bool
|
||||
case shaderir.Int:
|
||||
return gconstant.ToInt(rc).Kind() != gconstant.Unknown
|
||||
case shaderir.Float:
|
||||
return gconstant.ToFloat(rc).Kind() != gconstant.Unknown
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (cs *compileState) parseFor(block *block, fname string, stmt *ast.ForStmt, inParams, outParams []variable, returnType shaderir.Type, checkLocalVariableUsage bool) ([]shaderir.Stmt, bool) {
|
||||
msg := "for-statement must follow this format: for (varname) := (constant); (varname) (op) (constant); (varname) (op) (constant) { ..."
|
||||
if stmt.Init == nil {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if stmt.Cond == nil {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if stmt.Post == nil {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Create a new pseudo block for the initial statement, so that the counter variable belongs to the
|
||||
// new pseudo block for each for-loop. Without this, the same-named counter variables in different
|
||||
// for-loops confuses the parser.
|
||||
pseudoBlock, ok := cs.parseBlock(block, fname, []ast.Stmt{stmt.Init}, inParams, outParams, returnType, false)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
ss := pseudoBlock.ir.Stmts
|
||||
|
||||
if len(ss) != 1 {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if ss[0].Type != shaderir.Assign {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if ss[0].Exprs[0].Type != shaderir.LocalVariable {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
varidx := ss[0].Exprs[0].Index
|
||||
if ss[0].Exprs[1].Const == nil {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if len(pseudoBlock.vars) != 1 {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
vartype := pseudoBlock.vars[0].typ
|
||||
init := ss[0].Exprs[1].Const
|
||||
|
||||
exprs, ts, ss, ok := cs.parseExpr(pseudoBlock, fname, stmt.Cond, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if len(exprs) != 1 {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if len(ts) != 1 || ts[0].Main != shaderir.Bool {
|
||||
cs.addError(stmt.Pos(), "for-statement's condition must be bool")
|
||||
return nil, false
|
||||
}
|
||||
if len(ss) != 0 {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if exprs[0].Type != shaderir.Binary {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
op := exprs[0].Op
|
||||
if op != shaderir.LessThanOp && op != shaderir.LessThanEqualOp && op != shaderir.GreaterThanOp && op != shaderir.GreaterThanEqualOp && op != shaderir.EqualOp && op != shaderir.NotEqualOp {
|
||||
cs.addError(stmt.Pos(), "for-statement's condition must have one of these operators: <, <=, >, >=, ==, !=")
|
||||
return nil, false
|
||||
}
|
||||
if exprs[0].Exprs[0].Type != shaderir.LocalVariable {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if exprs[0].Exprs[0].Index != varidx {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if exprs[0].Exprs[1].Const == nil {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
end := exprs[0].Exprs[1].Const
|
||||
|
||||
postSs, ok := cs.parseStmt(pseudoBlock, fname, stmt.Post, inParams, outParams, returnType)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
if len(postSs) != 1 {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if postSs[0].Type != shaderir.Assign {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if postSs[0].Exprs[0].Type != shaderir.LocalVariable {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if postSs[0].Exprs[0].Index != varidx {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if postSs[0].Exprs[1].Type != shaderir.Binary {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if postSs[0].Exprs[1].Exprs[0].Type != shaderir.LocalVariable {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if postSs[0].Exprs[1].Exprs[0].Index != varidx {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
if postSs[0].Exprs[1].Exprs[1].Const == nil {
|
||||
cs.addError(stmt.Pos(), msg)
|
||||
return nil, false
|
||||
}
|
||||
delta := postSs[0].Exprs[1].Exprs[1].Const
|
||||
switch postSs[0].Exprs[1].Op {
|
||||
case shaderir.Add:
|
||||
case shaderir.Sub:
|
||||
delta = gconstant.UnaryOp(token.SUB, delta, 0)
|
||||
default:
|
||||
cs.addError(stmt.Pos(), "for-statement's post statement must have one of these operators: +=, -=, ++, --")
|
||||
return nil, false
|
||||
}
|
||||
|
||||
b, ok := cs.parseBlock(pseudoBlock, fname, []ast.Stmt{stmt.Body}, inParams, outParams, returnType, true)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
bodyir := b.ir
|
||||
for len(bodyir.Stmts) == 1 && bodyir.Stmts[0].Type == shaderir.BlockStmt {
|
||||
bodyir = bodyir.Stmts[0].Blocks[0]
|
||||
}
|
||||
|
||||
// As the pseudo block is not actually used, copy the variable part to the actual block.
|
||||
// This must be done after parsing the for-loop is done, or the duplicated variables confuses the
|
||||
// parsing.
|
||||
v := pseudoBlock.vars[0]
|
||||
v.forLoopCounter = true
|
||||
block.vars = append(block.vars, v)
|
||||
|
||||
return []shaderir.Stmt{
|
||||
{
|
||||
Type: shaderir.For,
|
||||
Blocks: []*shaderir.Block{bodyir},
|
||||
ForVarType: vartype,
|
||||
ForVarIndex: varidx,
|
||||
ForInit: init,
|
||||
ForEnd: end,
|
||||
ForOp: op,
|
||||
ForDelta: delta,
|
||||
},
|
||||
}, true
|
||||
}
|
||||
+542
@@ -0,0 +1,542 @@
|
||||
// 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"
|
||||
gconstant "go/constant"
|
||||
"strings"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
||||
)
|
||||
|
||||
func (cs *compileState) parseType(block *block, fname string, expr ast.Expr) (shaderir.Type, bool) {
|
||||
switch t := expr.(type) {
|
||||
case *ast.Ident:
|
||||
switch t.Name {
|
||||
case "bool":
|
||||
return shaderir.Type{Main: shaderir.Bool}, true
|
||||
case "int":
|
||||
return shaderir.Type{Main: shaderir.Int}, true
|
||||
case "float":
|
||||
return shaderir.Type{Main: shaderir.Float}, true
|
||||
case "vec2":
|
||||
return shaderir.Type{Main: shaderir.Vec2}, true
|
||||
case "vec3":
|
||||
return shaderir.Type{Main: shaderir.Vec3}, true
|
||||
case "vec4":
|
||||
return shaderir.Type{Main: shaderir.Vec4}, true
|
||||
case "ivec2":
|
||||
return shaderir.Type{Main: shaderir.IVec2}, true
|
||||
case "ivec3":
|
||||
return shaderir.Type{Main: shaderir.IVec3}, true
|
||||
case "ivec4":
|
||||
return shaderir.Type{Main: shaderir.IVec4}, true
|
||||
case "mat2":
|
||||
return shaderir.Type{Main: shaderir.Mat2}, true
|
||||
case "mat3":
|
||||
return shaderir.Type{Main: shaderir.Mat3}, true
|
||||
case "mat4":
|
||||
return shaderir.Type{Main: shaderir.Mat4}, true
|
||||
default:
|
||||
cs.addError(t.Pos(), fmt.Sprintf("unexpected type: %s", t.Name))
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
case *ast.ArrayType:
|
||||
if t.Len == nil {
|
||||
cs.addError(t.Pos(), "array length must be specified")
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
var length int
|
||||
if _, ok := t.Len.(*ast.Ellipsis); ok {
|
||||
length = -1 // Determine the length later.
|
||||
} else {
|
||||
exprs, _, _, ok := cs.parseExpr(block, fname, t.Len, true)
|
||||
if !ok {
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
if len(exprs) != 1 {
|
||||
cs.addError(t.Pos(), "invalid length of array")
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
if exprs[0].Type != shaderir.NumberExpr {
|
||||
cs.addError(t.Pos(), "length of array must be a constant number")
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
l, ok := gconstant.Int64Val(exprs[0].Const)
|
||||
if !ok {
|
||||
cs.addError(t.Pos(), "length of array must be an integer")
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
length = int(l)
|
||||
}
|
||||
|
||||
elm, ok := cs.parseType(block, fname, t.Elt)
|
||||
if !ok {
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
if elm.Main == shaderir.Array {
|
||||
cs.addError(t.Pos(), "array of array is forbidden")
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
return shaderir.Type{
|
||||
Main: shaderir.Array,
|
||||
Sub: []shaderir.Type{elm},
|
||||
Length: length,
|
||||
}, true
|
||||
case *ast.StructType:
|
||||
cs.addError(t.Pos(), "struct is not implemented")
|
||||
return shaderir.Type{}, false
|
||||
default:
|
||||
cs.addError(t.Pos(), fmt.Sprintf("unepxected type: %v", t))
|
||||
return shaderir.Type{}, false
|
||||
}
|
||||
}
|
||||
|
||||
func isFloat(expr shaderir.Expr, t shaderir.Type) bool {
|
||||
if expr.Const != nil {
|
||||
if t.Main == shaderir.Float {
|
||||
return true
|
||||
}
|
||||
if canTruncateToFloat(expr.Const) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if t.Main == shaderir.Float {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isInt(expr shaderir.Expr, t shaderir.Type) bool {
|
||||
if expr.Const != nil {
|
||||
if t.Main == shaderir.Float {
|
||||
return true
|
||||
}
|
||||
if canTruncateToInteger(expr.Const) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if t.Main == shaderir.Int {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkArgsForBoolBuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("number of bool's arguments must be 1 but %d", len(args))
|
||||
}
|
||||
if argts[0].Main == shaderir.Bool {
|
||||
return nil
|
||||
}
|
||||
if args[0].Const != nil && args[0].Const.Kind() == gconstant.Bool {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for bool: (%s)", argts[0].String())
|
||||
}
|
||||
|
||||
func checkArgsForIntBuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("number of int's arguments must be 1 but %d", len(args))
|
||||
}
|
||||
if argts[0].Main == shaderir.Int || argts[0].Main == shaderir.Float {
|
||||
return nil
|
||||
}
|
||||
if args[0].Const != nil && gconstant.ToInt(args[0].Const).Kind() != gconstant.Unknown {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for int: (%s)", argts[0].String())
|
||||
}
|
||||
|
||||
func checkArgsForFloatBuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("number of float's arguments must be 1 but %d", len(args))
|
||||
}
|
||||
if argts[0].Main == shaderir.Int || argts[0].Main == shaderir.Float {
|
||||
return nil
|
||||
}
|
||||
if args[0].Const != nil && gconstant.ToFloat(args[0].Const).Kind() != gconstant.Unknown {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for float: (%s)", argts[0].String())
|
||||
}
|
||||
|
||||
func checkArgsForVec2BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isFloat(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
// Allow any vectors to perform a cast-like function.
|
||||
if (argts[0].IsFloatVector() || argts[0].IsIntVector()) && argts[0].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
case 2:
|
||||
if isFloat(args[0], argts[0]) && isFloat(args[1], argts[1]) {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for vec2")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for vec2: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForVec3BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isFloat(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
// Allow any vectors to perform a cast-like function.
|
||||
if (argts[0].IsFloatVector() || argts[0].IsIntVector()) && argts[0].VectorElementCount() == 3 {
|
||||
return nil
|
||||
}
|
||||
case 2:
|
||||
if isFloat(args[0], argts[0]) && argts[1].IsFloatVector() && argts[1].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsFloatVector() && argts[0].VectorElementCount() == 2 && isFloat(args[1], argts[1]) {
|
||||
return nil
|
||||
}
|
||||
case 3:
|
||||
if isFloat(args[0], argts[0]) && isFloat(args[1], argts[1]) && isFloat(args[2], argts[2]) {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for vec3")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for vec3: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForVec4BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isFloat(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
// Allow any vectors to perform a cast-like function.
|
||||
if (argts[0].IsFloatVector() || argts[0].IsIntVector()) && argts[0].VectorElementCount() == 4 {
|
||||
return nil
|
||||
}
|
||||
case 2:
|
||||
if isFloat(args[0], argts[0]) && argts[1].IsFloatVector() && argts[1].VectorElementCount() == 3 {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsFloatVector() && argts[0].VectorElementCount() == 2 && argts[1].IsFloatVector() && argts[1].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsFloatVector() && argts[0].VectorElementCount() == 3 && isFloat(args[1], argts[1]) {
|
||||
return nil
|
||||
}
|
||||
case 3:
|
||||
if isFloat(args[0], argts[0]) && isFloat(args[1], argts[1]) && argts[2].IsFloatVector() && argts[2].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
if isFloat(args[0], argts[0]) && argts[1].IsFloatVector() && argts[1].VectorElementCount() == 2 && isFloat(args[2], argts[2]) {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsFloatVector() && argts[0].VectorElementCount() == 2 && isFloat(args[1], argts[1]) && isFloat(args[2], argts[2]) {
|
||||
return nil
|
||||
}
|
||||
case 4:
|
||||
if isFloat(args[0], argts[0]) && isFloat(args[1], argts[1]) && isFloat(args[2], argts[2]) && isFloat(args[3], argts[3]) {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for vec4")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for vec4: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForIVec2BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isInt(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
// Allow any vectors to perform a cast-like function.
|
||||
if (argts[0].IsFloatVector() || argts[0].IsIntVector()) && argts[0].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
case 2:
|
||||
if isInt(args[0], argts[0]) && isInt(args[1], argts[1]) {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for vec2")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for ivec2: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForIVec3BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isInt(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
// Allow any vectors to perform a cast-like function.
|
||||
if (argts[0].IsFloatVector() || argts[0].IsIntVector()) && argts[0].VectorElementCount() == 3 {
|
||||
return nil
|
||||
}
|
||||
case 2:
|
||||
if isInt(args[0], argts[0]) && argts[1].IsIntVector() && argts[1].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsIntVector() && argts[0].VectorElementCount() == 2 && isInt(args[1], argts[1]) {
|
||||
return nil
|
||||
}
|
||||
case 3:
|
||||
if isInt(args[0], argts[0]) && isInt(args[1], argts[1]) && isInt(args[2], argts[2]) {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for vec3")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for ivec3: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForIVec4BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isInt(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
// Allow any vectors to perform a cast-like function.
|
||||
if (argts[0].IsFloatVector() || argts[0].IsIntVector()) && argts[0].VectorElementCount() == 4 {
|
||||
return nil
|
||||
}
|
||||
case 2:
|
||||
if isInt(args[0], argts[0]) && argts[1].IsIntVector() && argts[1].VectorElementCount() == 3 {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsIntVector() && argts[0].VectorElementCount() == 2 && argts[1].IsIntVector() && argts[1].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsIntVector() && argts[0].VectorElementCount() == 3 && isInt(args[1], argts[1]) {
|
||||
return nil
|
||||
}
|
||||
case 3:
|
||||
if isInt(args[0], argts[0]) && isInt(args[1], argts[1]) && argts[2].IsIntVector() && argts[2].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
if isInt(args[0], argts[0]) && argts[1].IsIntVector() && argts[1].VectorElementCount() == 2 && isInt(args[2], argts[2]) {
|
||||
return nil
|
||||
}
|
||||
if argts[0].IsIntVector() && argts[0].VectorElementCount() == 2 && isInt(args[1], argts[1]) && isInt(args[2], argts[2]) {
|
||||
return nil
|
||||
}
|
||||
case 4:
|
||||
if isInt(args[0], argts[0]) && isInt(args[1], argts[1]) && isInt(args[2], argts[2]) && isInt(args[3], argts[3]) {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for vec4")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for ivec4: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForMat2BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isFloat(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
if argts[0].Main == shaderir.Mat2 {
|
||||
return nil
|
||||
}
|
||||
case 2:
|
||||
if argts[0].IsFloatVector() && argts[0].VectorElementCount() == 2 && argts[1].IsFloatVector() && argts[1].VectorElementCount() == 2 {
|
||||
return nil
|
||||
}
|
||||
case 4:
|
||||
ok := true
|
||||
for i := range argts {
|
||||
if !isFloat(args[i], argts[i]) {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for mat2")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for mat2: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForMat3BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isFloat(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
if argts[0].Main == shaderir.Mat3 {
|
||||
return nil
|
||||
}
|
||||
case 3:
|
||||
if argts[0].IsFloatVector() && argts[0].VectorElementCount() == 3 &&
|
||||
argts[1].IsFloatVector() && argts[1].VectorElementCount() == 3 &&
|
||||
argts[2].IsFloatVector() && argts[2].VectorElementCount() == 3 {
|
||||
return nil
|
||||
}
|
||||
case 9:
|
||||
ok := true
|
||||
for i := range argts {
|
||||
if !isFloat(args[i], argts[i]) {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for mat3")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for mat3: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
|
||||
func checkArgsForMat4BuiltinFunc(args []shaderir.Expr, argts []shaderir.Type) error {
|
||||
if len(args) != len(argts) {
|
||||
return fmt.Errorf("the number of arguments and types doesn't match: %d vs %d", len(args), len(argts))
|
||||
}
|
||||
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if isFloat(args[0], argts[0]) {
|
||||
return nil
|
||||
}
|
||||
if argts[0].Main == shaderir.Mat4 {
|
||||
return nil
|
||||
}
|
||||
case 4:
|
||||
if argts[0].IsFloatVector() && argts[0].VectorElementCount() == 4 &&
|
||||
argts[1].IsFloatVector() && argts[1].VectorElementCount() == 4 &&
|
||||
argts[2].IsFloatVector() && argts[2].VectorElementCount() == 4 &&
|
||||
argts[3].IsFloatVector() && argts[3].VectorElementCount() == 4 {
|
||||
return nil
|
||||
}
|
||||
case 16:
|
||||
ok := true
|
||||
for i := range argts {
|
||||
if !isFloat(args[i], argts[i]) {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid number of arguments for mat4")
|
||||
}
|
||||
|
||||
var str []string
|
||||
for _, t := range argts {
|
||||
str = append(str, t.String())
|
||||
}
|
||||
return fmt.Errorf("invalid arguments for mat4: (%s)", strings.Join(str, ", "))
|
||||
}
|
||||
Reference in New Issue
Block a user