2020-05-11 17:19:42 +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.
|
|
|
|
|
2020-05-16 16:42:32 +02:00
|
|
|
// Package shaderir offers intermediate representation for shader programs.
|
2020-05-11 17:19:42 +02:00
|
|
|
package shaderir
|
|
|
|
|
2020-06-20 16:26:29 +02:00
|
|
|
import (
|
2020-06-20 18:22:30 +02:00
|
|
|
"go/constant"
|
2020-06-20 16:26:29 +02:00
|
|
|
"go/token"
|
|
|
|
)
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
type Program struct {
|
2020-05-16 15:18:58 +02:00
|
|
|
Uniforms []Type
|
2020-07-05 20:36:15 +02:00
|
|
|
TextureNum int
|
2020-05-16 15:18:58 +02:00
|
|
|
Attributes []Type
|
|
|
|
Varyings []Type
|
|
|
|
Funcs []Func
|
|
|
|
VertexFunc VertexFunc
|
|
|
|
FragmentFunc FragmentFunc
|
2020-05-11 17:19:42 +02:00
|
|
|
|
|
|
|
structNames map[string]string
|
|
|
|
structTypes []Type
|
|
|
|
}
|
|
|
|
|
|
|
|
type Func struct {
|
2020-06-07 11:57:46 +02:00
|
|
|
Index int
|
|
|
|
InParams []Type
|
|
|
|
OutParams []Type
|
|
|
|
Return Type
|
|
|
|
Block Block
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:18:58 +02:00
|
|
|
// VertexFunc takes pseudo params, and the number if len(attributes) + len(varyings) + 1.
|
2020-06-02 17:46:52 +02:00
|
|
|
// If 0 <= index < len(attributes), the params are in-params and represent attribute variables.
|
|
|
|
// If index == len(attributes), the param is an out-param and repesents the position in vec4 (gl_Position in GLSL)
|
|
|
|
// If len(attributes) + 1 <= index < len(attributes) + len(varyings) + 1, the params are out-params and represent
|
|
|
|
// varying variables.
|
2020-05-16 15:18:58 +02:00
|
|
|
type VertexFunc struct {
|
|
|
|
Block Block
|
|
|
|
}
|
|
|
|
|
2020-05-24 10:14:37 +02:00
|
|
|
// FragmentFunc takes pseudo params, and the number is len(varyings) + 2.
|
2020-06-02 19:01:50 +02:00
|
|
|
// If index == 0, the param represents the coordinate of the fragment (gl_FragCoord in GLSL).
|
|
|
|
// If index == len(varyings), the param represents (index-1)th verying variable.
|
2020-05-23 19:07:57 +02:00
|
|
|
// If index == len(varyings)+1, the param is an out-param representing the color of the pixel (gl_FragColor in GLSL).
|
2020-05-16 15:18:58 +02:00
|
|
|
type FragmentFunc struct {
|
|
|
|
Block Block
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
type Block struct {
|
|
|
|
LocalVars []Type
|
|
|
|
Stmts []Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
type Stmt struct {
|
2020-05-15 20:10:03 +02:00
|
|
|
Type StmtType
|
|
|
|
Exprs []Expr
|
|
|
|
Blocks []Block
|
|
|
|
ForInit int
|
|
|
|
ForEnd int
|
2020-05-16 08:51:54 +02:00
|
|
|
ForOp Op
|
2020-05-15 20:10:03 +02:00
|
|
|
ForDelta int
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type StmtType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ExprStmt StmtType = iota
|
2020-05-13 18:45:33 +02:00
|
|
|
BlockStmt
|
2020-05-11 17:19:42 +02:00
|
|
|
Assign
|
|
|
|
If
|
|
|
|
For
|
|
|
|
Continue
|
|
|
|
Break
|
2020-05-13 20:14:39 +02:00
|
|
|
Return
|
2020-05-11 17:19:42 +02:00
|
|
|
Discard
|
|
|
|
)
|
|
|
|
|
2020-06-20 18:22:30 +02:00
|
|
|
type ConstType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ConstTypeNone ConstType = iota
|
|
|
|
ConstTypeBool
|
|
|
|
ConstTypeInt
|
|
|
|
ConstTypeFloat
|
|
|
|
)
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
type Expr struct {
|
2020-05-16 17:25:31 +02:00
|
|
|
Type ExprType
|
|
|
|
Exprs []Expr
|
2020-06-20 18:22:30 +02:00
|
|
|
Const constant.Value
|
|
|
|
ConstType ConstType
|
2020-05-16 17:25:31 +02:00
|
|
|
BuiltinFunc BuiltinFunc
|
2020-05-16 20:00:57 +02:00
|
|
|
Swizzling string
|
|
|
|
Index int
|
2020-05-16 17:25:31 +02:00
|
|
|
Op Op
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExprType int
|
|
|
|
|
|
|
|
const (
|
2020-06-20 18:22:30 +02:00
|
|
|
NumberExpr ExprType = iota
|
2020-05-16 21:28:03 +02:00
|
|
|
UniformVariable
|
2020-07-05 20:36:15 +02:00
|
|
|
TextureVariable
|
2020-05-16 21:28:03 +02:00
|
|
|
LocalVariable
|
|
|
|
StructMember
|
2020-05-16 17:25:31 +02:00
|
|
|
BuiltinFuncExpr
|
2020-05-16 19:24:35 +02:00
|
|
|
SwizzlingExpr
|
2020-05-16 20:00:57 +02:00
|
|
|
FunctionExpr
|
2020-05-11 17:19:42 +02:00
|
|
|
Unary
|
|
|
|
Binary
|
2020-05-16 10:22:17 +02:00
|
|
|
Selection
|
2020-05-11 17:19:42 +02:00
|
|
|
Call
|
2020-05-16 10:22:17 +02:00
|
|
|
FieldSelector
|
2020-05-11 17:19:42 +02:00
|
|
|
Index
|
|
|
|
)
|
|
|
|
|
|
|
|
type Op string
|
|
|
|
|
|
|
|
const (
|
2020-05-16 17:25:31 +02:00
|
|
|
Add Op = "+"
|
|
|
|
Sub Op = "-"
|
2020-06-02 15:45:44 +02:00
|
|
|
NotOp Op = "!"
|
2020-05-16 17:25:31 +02:00
|
|
|
Mul Op = "*"
|
|
|
|
Div Op = "/"
|
|
|
|
ModOp Op = "%"
|
|
|
|
LeftShift Op = "<<"
|
|
|
|
RightShift Op = ">>"
|
|
|
|
LessThanOp Op = "<"
|
|
|
|
LessThanEqualOp Op = "<="
|
|
|
|
GreaterThanOp Op = ">"
|
|
|
|
GreaterThanEqualOp Op = ">="
|
|
|
|
EqualOp Op = "=="
|
|
|
|
NotEqualOp Op = "!="
|
|
|
|
And Op = "&"
|
|
|
|
Xor Op = "^"
|
|
|
|
Or Op = "|"
|
|
|
|
AndAnd Op = "&&"
|
|
|
|
OrOr Op = "||"
|
|
|
|
)
|
|
|
|
|
2020-06-20 16:26:29 +02:00
|
|
|
func OpFromToken(t token.Token) (Op, bool) {
|
|
|
|
switch t {
|
|
|
|
case token.ADD:
|
|
|
|
return Add, true
|
|
|
|
case token.SUB:
|
|
|
|
return Sub, true
|
|
|
|
case token.NOT:
|
|
|
|
return NotOp, true
|
|
|
|
case token.MUL:
|
|
|
|
return Mul, true
|
|
|
|
case token.QUO:
|
|
|
|
return Div, true
|
|
|
|
case token.REM:
|
|
|
|
return ModOp, true
|
|
|
|
case token.SHL:
|
|
|
|
return LeftShift, true
|
|
|
|
case token.SHR:
|
|
|
|
return RightShift, true
|
|
|
|
case token.LSS:
|
|
|
|
return LessThanOp, true
|
|
|
|
case token.LEQ:
|
|
|
|
return LessThanEqualOp, true
|
|
|
|
case token.GTR:
|
|
|
|
return GreaterThanOp, true
|
|
|
|
case token.GEQ:
|
|
|
|
return GreaterThanEqualOp, true
|
|
|
|
case token.EQL:
|
|
|
|
return EqualOp, true
|
|
|
|
case token.NEQ:
|
|
|
|
return NotEqualOp, true
|
|
|
|
case token.AND:
|
|
|
|
return And, true
|
|
|
|
case token.XOR:
|
|
|
|
return Xor, true
|
|
|
|
case token.OR:
|
|
|
|
return Or, true
|
|
|
|
case token.LAND:
|
|
|
|
return AndAnd, true
|
|
|
|
case token.LOR:
|
|
|
|
return OrOr, true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
2020-05-16 17:25:31 +02:00
|
|
|
type BuiltinFunc string
|
|
|
|
|
|
|
|
const (
|
2020-05-23 11:07:28 +02:00
|
|
|
Vec2F BuiltinFunc = "vec2"
|
|
|
|
Vec3F BuiltinFunc = "vec3"
|
|
|
|
Vec4F BuiltinFunc = "vec4"
|
2020-05-23 15:01:10 +02:00
|
|
|
Mat2F BuiltinFunc = "mat2"
|
|
|
|
Mat3F BuiltinFunc = "mat3"
|
|
|
|
Mat4F BuiltinFunc = "mat4"
|
2020-05-16 17:25:31 +02:00
|
|
|
Radians BuiltinFunc = "radians"
|
|
|
|
Degrees BuiltinFunc = "degrees"
|
|
|
|
Sin BuiltinFunc = "sin"
|
|
|
|
Cos BuiltinFunc = "cos"
|
|
|
|
Tan BuiltinFunc = "tan"
|
|
|
|
Asin BuiltinFunc = "asin"
|
|
|
|
Acos BuiltinFunc = "acos"
|
|
|
|
Atan BuiltinFunc = "atan"
|
|
|
|
Pow BuiltinFunc = "pow"
|
|
|
|
Exp BuiltinFunc = "exp"
|
|
|
|
Log BuiltinFunc = "log"
|
|
|
|
Exp2 BuiltinFunc = "exp2"
|
|
|
|
Log2 BuiltinFunc = "log2"
|
|
|
|
Sqrt BuiltinFunc = "sqrt"
|
|
|
|
Inversesqrt BuiltinFunc = "inversesqrt"
|
|
|
|
Abs BuiltinFunc = "abs"
|
|
|
|
Sign BuiltinFunc = "sign"
|
|
|
|
Floor BuiltinFunc = "floor"
|
|
|
|
Ceil BuiltinFunc = "ceil"
|
|
|
|
Fract BuiltinFunc = "fract"
|
|
|
|
Mod BuiltinFunc = "mod"
|
|
|
|
Min BuiltinFunc = "min"
|
|
|
|
Max BuiltinFunc = "max"
|
|
|
|
Clamp BuiltinFunc = "clamp"
|
|
|
|
Mix BuiltinFunc = "mix"
|
|
|
|
Step BuiltinFunc = "step"
|
|
|
|
Smoothstep BuiltinFunc = "smoothstep"
|
|
|
|
Length BuiltinFunc = "length"
|
|
|
|
Distance BuiltinFunc = "distance"
|
|
|
|
Dot BuiltinFunc = "dot"
|
|
|
|
Cross BuiltinFunc = "cross"
|
|
|
|
Normalize BuiltinFunc = "normalize"
|
|
|
|
Faceforward BuiltinFunc = "faceforward"
|
|
|
|
Reflect BuiltinFunc = "reflect"
|
|
|
|
Transpose BuiltinFunc = "transpose"
|
2020-07-11 14:42:50 +02:00
|
|
|
LessThan BuiltinFunc = "lessthan"
|
|
|
|
LessThanEqual BuiltinFunc = "lessthanequal"
|
|
|
|
GreaterThan BuiltinFunc = "greaterthan"
|
|
|
|
GreaterThanEqual BuiltinFunc = "greaterthanequal"
|
2020-05-16 17:25:31 +02:00
|
|
|
Equal BuiltinFunc = "equal"
|
2020-07-11 14:42:50 +02:00
|
|
|
NotEqual BuiltinFunc = "notequal"
|
2020-05-16 17:25:31 +02:00
|
|
|
Any BuiltinFunc = "any"
|
|
|
|
All BuiltinFunc = "all"
|
|
|
|
Not BuiltinFunc = "not"
|
2020-05-30 10:48:56 +02:00
|
|
|
Texture2DF BuiltinFunc = "texture2D"
|
2020-05-11 17:19:42 +02:00
|
|
|
)
|
2020-05-31 11:11:55 +02:00
|
|
|
|
|
|
|
func ParseBuiltinFunc(str string) (BuiltinFunc, bool) {
|
|
|
|
switch BuiltinFunc(str) {
|
|
|
|
case Vec2F,
|
|
|
|
Vec3F,
|
|
|
|
Vec4F,
|
|
|
|
Mat2F,
|
|
|
|
Mat3F,
|
|
|
|
Mat4F,
|
|
|
|
Radians,
|
|
|
|
Degrees,
|
|
|
|
Sin,
|
|
|
|
Cos,
|
|
|
|
Tan,
|
|
|
|
Asin,
|
|
|
|
Acos,
|
|
|
|
Atan,
|
|
|
|
Pow,
|
|
|
|
Exp,
|
|
|
|
Log,
|
|
|
|
Exp2,
|
|
|
|
Log2,
|
|
|
|
Sqrt,
|
|
|
|
Inversesqrt,
|
|
|
|
Abs,
|
|
|
|
Sign,
|
|
|
|
Floor,
|
|
|
|
Ceil,
|
|
|
|
Fract,
|
|
|
|
Mod,
|
|
|
|
Min,
|
|
|
|
Max,
|
|
|
|
Clamp,
|
|
|
|
Mix,
|
|
|
|
Step,
|
|
|
|
Smoothstep,
|
|
|
|
Length,
|
|
|
|
Distance,
|
|
|
|
Dot,
|
|
|
|
Cross,
|
|
|
|
Normalize,
|
|
|
|
Faceforward,
|
|
|
|
Reflect,
|
|
|
|
Transpose,
|
|
|
|
LessThan,
|
|
|
|
LessThanEqual,
|
|
|
|
GreaterThan,
|
|
|
|
GreaterThanEqual,
|
|
|
|
Equal,
|
|
|
|
NotEqual,
|
|
|
|
Any,
|
|
|
|
All,
|
|
|
|
Not,
|
|
|
|
Texture2DF:
|
|
|
|
return BuiltinFunc(str), true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
2020-07-11 14:46:21 +02:00
|
|
|
|
|
|
|
func (f BuiltinFunc) Glsl() string {
|
|
|
|
switch f {
|
|
|
|
case LessThan:
|
|
|
|
return "lessThan"
|
|
|
|
case LessThanEqual:
|
|
|
|
return "lessThanEqual"
|
|
|
|
case GreaterThan:
|
|
|
|
return "greaterThan"
|
|
|
|
case GreaterThanEqual:
|
|
|
|
return "greaterThanEqual"
|
|
|
|
case NotEqual:
|
|
|
|
return "notEqual"
|
|
|
|
default:
|
|
|
|
return string(f)
|
|
|
|
}
|
|
|
|
}
|