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"
|
2020-07-28 17:57:47 +02:00
|
|
|
gconstant "go/constant"
|
2022-07-07 14:25:02 +02:00
|
|
|
"strings"
|
2020-05-08 17:46:01 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2020-05-08 17:46:01 +02:00
|
|
|
)
|
|
|
|
|
2020-07-29 05:02:07 +02:00
|
|
|
func (cs *compileState) parseType(block *block, expr ast.Expr) (shaderir.Type, bool) {
|
2020-05-08 17:46:01 +02:00
|
|
|
switch t := expr.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
switch t.Name {
|
2020-05-30 10:03:43 +02:00
|
|
|
case "bool":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Bool}, true
|
2020-05-30 10:03:43 +02:00
|
|
|
case "int":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Int}, true
|
2020-05-08 17:46:01 +02:00
|
|
|
case "float":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Float}, true
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec2":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Vec2}, true
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec3":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Vec3}, true
|
2020-05-08 17:46:01 +02:00
|
|
|
case "vec4":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Vec4}, true
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat2":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Mat2}, true
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat3":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Mat3}, true
|
2020-05-08 17:46:01 +02:00
|
|
|
case "mat4":
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{Main: shaderir.Mat4}, true
|
2020-05-10 16:56:56 +02:00
|
|
|
default:
|
2020-05-30 10:03:43 +02:00
|
|
|
cs.addError(t.Pos(), fmt.Sprintf("unexpected type: %s", t.Name))
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{}, false
|
2020-05-10 17:13:08 +02:00
|
|
|
}
|
2020-07-28 17:57:47 +02:00
|
|
|
case *ast.ArrayType:
|
|
|
|
if t.Len == nil {
|
|
|
|
cs.addError(t.Pos(), fmt.Sprintf("array length must be specified"))
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{}, false
|
2020-07-28 17:57:47 +02:00
|
|
|
}
|
2020-08-11 16:51:22 +02:00
|
|
|
var length int
|
|
|
|
if _, ok := t.Len.(*ast.Ellipsis); ok {
|
|
|
|
length = -1 // Determine the length later.
|
|
|
|
} else {
|
2020-09-13 13:13:30 +02:00
|
|
|
exprs, _, _, ok := cs.parseExpr(block, t.Len, true)
|
2020-08-11 16:51:22 +02:00
|
|
|
if !ok {
|
|
|
|
return shaderir.Type{}, false
|
|
|
|
}
|
|
|
|
if len(exprs) != 1 {
|
|
|
|
cs.addError(t.Pos(), fmt.Sprintf("invalid length of array"))
|
|
|
|
return shaderir.Type{}, false
|
|
|
|
}
|
|
|
|
if exprs[0].Type != shaderir.NumberExpr {
|
|
|
|
cs.addError(t.Pos(), fmt.Sprintf("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(), fmt.Sprintf("length of array must be an integer"))
|
|
|
|
return shaderir.Type{}, false
|
|
|
|
}
|
|
|
|
length = int(l)
|
2020-07-28 17:57:47 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 05:02:07 +02:00
|
|
|
elm, ok := cs.parseType(block, t.Elt)
|
|
|
|
if !ok {
|
|
|
|
return shaderir.Type{}, false
|
|
|
|
}
|
2020-07-28 17:57:47 +02:00
|
|
|
if elm.Main == shaderir.Array {
|
|
|
|
cs.addError(t.Pos(), fmt.Sprintf("array of array is forbidden"))
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{}, false
|
2020-07-28 17:57:47 +02:00
|
|
|
}
|
|
|
|
return shaderir.Type{
|
|
|
|
Main: shaderir.Array,
|
|
|
|
Sub: []shaderir.Type{elm},
|
2020-08-11 16:51:22 +02:00
|
|
|
Length: length,
|
2020-07-29 05:02:07 +02:00
|
|
|
}, true
|
2020-05-10 17:13:08 +02:00
|
|
|
case *ast.StructType:
|
2020-05-30 10:03:43 +02:00
|
|
|
cs.addError(t.Pos(), "struct is not implemented")
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{}, false
|
2020-05-10 17:13:08 +02:00
|
|
|
default:
|
2020-05-30 10:03:43 +02:00
|
|
|
cs.addError(t.Pos(), fmt.Sprintf("unepxected type: %v", t))
|
2020-07-29 05:02:07 +02:00
|
|
|
return shaderir.Type{}, false
|
2020-05-08 17:46:01 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 18:25:40 +02:00
|
|
|
|
|
|
|
func canBeFloatImplicitly(expr shaderir.Expr, t shaderir.Type) bool {
|
|
|
|
// TODO: For integers, should only constants be allowed?
|
|
|
|
if t.Main == shaderir.Int {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if t.Main == shaderir.Float {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if expr.Const != nil {
|
|
|
|
if expr.Const.Kind() == gconstant.Int {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if expr.Const.Kind() == gconstant.Float {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-07 17:07:32 +02:00
|
|
|
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 && canTruncateToInteger(args[0].Const) {
|
|
|
|
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 canBeFloatImplicitly(args[0], argts[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("invalid arguments for float: (%s)", argts[0].String())
|
|
|
|
}
|
|
|
|
|
2022-07-05 18:25:40 +02:00
|
|
|
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 canBeFloatImplicitly(args[0], argts[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Vec2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if canBeFloatImplicitly(args[0], argts[0]) && canBeFloatImplicitly(args[1], argts[1]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
2022-07-07 17:07:32 +02:00
|
|
|
return fmt.Errorf("invalid number of arguments for vec2")
|
2022-07-05 18:25:40 +02:00
|
|
|
}
|
2022-07-07 14:25:02 +02:00
|
|
|
|
|
|
|
var str []string
|
|
|
|
for _, t := range argts {
|
|
|
|
str = append(str, t.String())
|
|
|
|
}
|
|
|
|
return fmt.Errorf("invalid arguments for vec2: (%s)", strings.Join(str, ", "))
|
2022-07-05 18:25:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 canBeFloatImplicitly(args[0], argts[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Vec3 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if canBeFloatImplicitly(args[0], argts[0]) && argts[1].Main == shaderir.Vec2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Vec2 && canBeFloatImplicitly(args[1], argts[1]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
if canBeFloatImplicitly(args[0], argts[0]) && canBeFloatImplicitly(args[1], argts[1]) && canBeFloatImplicitly(args[2], argts[2]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
2022-07-07 17:07:32 +02:00
|
|
|
return fmt.Errorf("invalid number of arguments for vec3")
|
2022-07-05 18:25:40 +02:00
|
|
|
}
|
2022-07-07 14:25:02 +02:00
|
|
|
|
|
|
|
var str []string
|
|
|
|
for _, t := range argts {
|
|
|
|
str = append(str, t.String())
|
|
|
|
}
|
|
|
|
return fmt.Errorf("invalid arguments for vec3: (%s)", strings.Join(str, ", "))
|
2022-07-05 18:25:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 canBeFloatImplicitly(args[0], argts[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Vec4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if canBeFloatImplicitly(args[0], argts[0]) && argts[1].Main == shaderir.Vec3 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Vec2 && argts[1].Main == shaderir.Vec2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Vec3 && canBeFloatImplicitly(args[1], argts[1]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
if canBeFloatImplicitly(args[0], argts[0]) && canBeFloatImplicitly(args[1], argts[1]) && argts[2].Main == shaderir.Vec2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if canBeFloatImplicitly(args[0], argts[0]) && argts[1].Main == shaderir.Vec2 && canBeFloatImplicitly(args[2], argts[2]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Vec2 && canBeFloatImplicitly(args[1], argts[1]) && canBeFloatImplicitly(args[2], argts[2]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
if canBeFloatImplicitly(args[0], argts[0]) && canBeFloatImplicitly(args[1], argts[1]) && canBeFloatImplicitly(args[2], argts[2]) && canBeFloatImplicitly(args[3], argts[3]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
2022-07-07 17:07:32 +02:00
|
|
|
return fmt.Errorf("invalid number of arguments for vec4")
|
2022-07-05 18:25:40 +02:00
|
|
|
}
|
2022-07-07 14:25:02 +02:00
|
|
|
|
|
|
|
var str []string
|
|
|
|
for _, t := range argts {
|
|
|
|
str = append(str, t.String())
|
|
|
|
}
|
|
|
|
return fmt.Errorf("invalid arguments for vec4: (%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 canBeFloatImplicitly(args[0], argts[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Mat2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
if argts[0].Main == shaderir.Vec2 && argts[1].Main == shaderir.Vec2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
ok := true
|
|
|
|
for i := range argts {
|
|
|
|
if !canBeFloatImplicitly(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 canBeFloatImplicitly(args[0], argts[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Mat3 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
if argts[0].Main == shaderir.Vec3 && argts[1].Main == shaderir.Vec3 && argts[2].Main == shaderir.Vec3 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 9:
|
|
|
|
ok := true
|
|
|
|
for i := range argts {
|
|
|
|
if !canBeFloatImplicitly(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 canBeFloatImplicitly(args[0], argts[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if argts[0].Main == shaderir.Mat4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 4:
|
|
|
|
if argts[0].Main == shaderir.Vec4 && argts[1].Main == shaderir.Vec4 && argts[2].Main == shaderir.Vec4 && argts[3].Main == shaderir.Vec4 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case 16:
|
|
|
|
ok := true
|
|
|
|
for i := range argts {
|
|
|
|
if !canBeFloatImplicitly(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, ", "))
|
2022-07-05 18:25:40 +02:00
|
|
|
}
|