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 (
|
2024-04-20 16:48:33 +02:00
|
|
|
"encoding/hex"
|
2020-06-20 18:22:30 +02:00
|
|
|
"go/constant"
|
2020-06-20 16:26:29 +02:00
|
|
|
"go/token"
|
2024-04-20 16:48:33 +02:00
|
|
|
"hash/fnv"
|
2022-11-03 09:44:11 +01:00
|
|
|
"sort"
|
2020-08-08 12:08:07 +02:00
|
|
|
"strings"
|
2020-06-20 16:26:29 +02:00
|
|
|
)
|
|
|
|
|
2023-04-16 11:56:14 +02:00
|
|
|
type Unit int
|
|
|
|
|
|
|
|
const (
|
2023-08-01 04:41:27 +02:00
|
|
|
Texels Unit = iota
|
|
|
|
Pixels
|
2023-04-16 11:56:14 +02:00
|
|
|
)
|
|
|
|
|
2024-04-20 16:48:33 +02:00
|
|
|
type SourceHash [16]byte
|
|
|
|
|
|
|
|
func CalcSourceHash(source []byte) SourceHash {
|
|
|
|
h := fnv.New128a()
|
|
|
|
_, _ = h.Write(source)
|
|
|
|
|
|
|
|
var hash SourceHash
|
|
|
|
h.Sum(hash[:0])
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s SourceHash) String() string {
|
|
|
|
return hex.EncodeToString(s[:])
|
|
|
|
}
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
type Program struct {
|
2020-09-05 20:41:13 +02:00
|
|
|
UniformNames []string
|
2020-05-16 15:18:58 +02:00
|
|
|
Uniforms []Type
|
2022-12-24 07:55:05 +01:00
|
|
|
TextureCount int
|
2020-05-16 15:18:58 +02:00
|
|
|
Attributes []Type
|
|
|
|
Varyings []Type
|
|
|
|
Funcs []Func
|
|
|
|
VertexFunc VertexFunc
|
|
|
|
FragmentFunc FragmentFunc
|
2023-04-16 11:56:14 +02:00
|
|
|
Unit Unit
|
2022-11-03 17:23:17 +01:00
|
|
|
|
2024-04-20 16:48:33 +02:00
|
|
|
SourceHash SourceHash
|
|
|
|
|
2023-08-19 20:08:35 +02:00
|
|
|
uniformFactors []uint32
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Func struct {
|
2020-06-07 11:57:46 +02:00
|
|
|
Index int
|
|
|
|
InParams []Type
|
|
|
|
OutParams []Type
|
|
|
|
Return Type
|
2020-08-09 08:55:59 +02:00
|
|
|
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.
|
2023-01-28 11:06:38 +01:00
|
|
|
// If index == len(attributes), the param is an out-param and represents the position in vec4 (gl_Position in GLSL)
|
2020-06-02 17:46:52 +02:00
|
|
|
// 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 {
|
2020-08-09 08:55:59 +02:00
|
|
|
Block *Block
|
2020-05-16 15:18:58 +02:00
|
|
|
}
|
|
|
|
|
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).
|
2023-01-28 11:06:38 +01:00
|
|
|
// If 0 < index <= len(varyings), the param represents (index-1)th varying variable.
|
2020-05-16 15:18:58 +02:00
|
|
|
type FragmentFunc struct {
|
2020-08-09 08:55:59 +02:00
|
|
|
Block *Block
|
2020-05-16 15:18:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-11 17:19:42 +02:00
|
|
|
type Block struct {
|
2020-08-09 16:40:41 +02:00
|
|
|
LocalVars []Type
|
|
|
|
LocalVarIndexOffset int
|
|
|
|
Stmts []Stmt
|
2020-05-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Stmt struct {
|
2020-07-12 14:53:18 +02:00
|
|
|
Type StmtType
|
|
|
|
Exprs []Expr
|
2020-08-09 08:55:59 +02:00
|
|
|
Blocks []*Block
|
2020-07-12 15:22:22 +02:00
|
|
|
ForVarType Type
|
2020-07-12 14:53:18 +02:00
|
|
|
ForVarIndex int
|
2020-07-12 15:22:22 +02:00
|
|
|
ForInit constant.Value
|
|
|
|
ForEnd constant.Value
|
2020-07-12 14:53:18 +02:00
|
|
|
ForOp Op
|
2020-07-12 15:22:22 +02:00
|
|
|
ForDelta constant.Value
|
2020-09-06 17:13:46 +02:00
|
|
|
InitIndex 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
|
2020-09-06 17:13:46 +02:00
|
|
|
Init
|
2020-05-11 17:19:42 +02:00
|
|
|
If
|
|
|
|
For
|
|
|
|
Continue
|
|
|
|
Break
|
2020-05-13 20:14:39 +02:00
|
|
|
Return
|
2020-05-11 17:19:42 +02:00
|
|
|
Discard
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
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-09-02 18:15:37 +02:00
|
|
|
Blank ExprType = iota
|
|
|
|
NumberExpr
|
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
|
|
|
|
)
|
|
|
|
|
2022-03-13 08:20:01 +01:00
|
|
|
type Op int
|
2020-05-11 17:19:42 +02:00
|
|
|
|
|
|
|
const (
|
2022-03-13 08:20:01 +01:00
|
|
|
Add Op = iota
|
|
|
|
Sub
|
|
|
|
NotOp
|
2022-03-13 11:17:44 +01:00
|
|
|
ComponentWiseMul
|
|
|
|
MatrixMul
|
2022-03-13 08:20:01 +01:00
|
|
|
Div
|
|
|
|
ModOp
|
|
|
|
LeftShift
|
|
|
|
RightShift
|
|
|
|
LessThanOp
|
|
|
|
LessThanEqualOp
|
|
|
|
GreaterThanOp
|
|
|
|
GreaterThanEqualOp
|
|
|
|
EqualOp
|
|
|
|
NotEqualOp
|
2022-07-08 19:26:33 +02:00
|
|
|
VectorEqualOp
|
|
|
|
VectorNotEqualOp
|
2022-03-13 08:20:01 +01:00
|
|
|
And
|
|
|
|
Xor
|
|
|
|
Or
|
|
|
|
AndAnd
|
|
|
|
OrOr
|
2020-05-16 17:25:31 +02:00
|
|
|
)
|
|
|
|
|
2022-03-13 11:17:44 +01:00
|
|
|
func OpFromToken(t token.Token, lhs, rhs Type) (Op, bool) {
|
2020-06-20 16:26:29 +02:00
|
|
|
switch t {
|
|
|
|
case token.ADD:
|
|
|
|
return Add, true
|
|
|
|
case token.SUB:
|
|
|
|
return Sub, true
|
|
|
|
case token.NOT:
|
|
|
|
return NotOp, true
|
|
|
|
case token.MUL:
|
2022-03-13 11:17:44 +01:00
|
|
|
if lhs.IsMatrix() || rhs.IsMatrix() {
|
|
|
|
return MatrixMul, true
|
|
|
|
}
|
|
|
|
return ComponentWiseMul, true
|
2020-06-20 16:26:29 +02:00
|
|
|
case token.QUO:
|
|
|
|
return Div, true
|
2023-07-23 16:40:22 +02:00
|
|
|
case token.QUO_ASSIGN:
|
|
|
|
// QUO_ASSIGN indicates an integer division.
|
|
|
|
// https://pkg.go.dev/go/constant/#BinaryOp
|
|
|
|
return Div, true
|
2020-06-20 16:26:29 +02:00
|
|
|
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:
|
2023-07-27 17:13:40 +02:00
|
|
|
if lhs.IsFloatVector() || lhs.IsIntVector() || rhs.IsFloatVector() || rhs.IsIntVector() {
|
2022-07-08 19:26:33 +02:00
|
|
|
return VectorEqualOp, true
|
|
|
|
}
|
2020-06-20 16:26:29 +02:00
|
|
|
return EqualOp, true
|
|
|
|
case token.NEQ:
|
2023-07-27 17:13:40 +02:00
|
|
|
if lhs.IsFloatVector() || lhs.IsIntVector() || rhs.IsFloatVector() || rhs.IsIntVector() {
|
2022-07-08 19:26:33 +02:00
|
|
|
return VectorNotEqualOp, true
|
|
|
|
}
|
2020-06-20 16:26:29 +02:00
|
|
|
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
|
|
|
|
}
|
2022-03-13 08:20:01 +01:00
|
|
|
return 0, false
|
2020-06-20 16:26:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 17:25:31 +02:00
|
|
|
type BuiltinFunc string
|
|
|
|
|
|
|
|
const (
|
2020-08-15 19:07:36 +02:00
|
|
|
Len BuiltinFunc = "len"
|
2020-10-17 16:36:07 +02:00
|
|
|
Cap BuiltinFunc = "cap"
|
2020-08-11 16:04:49 +02:00
|
|
|
BoolF BuiltinFunc = "bool"
|
|
|
|
IntF BuiltinFunc = "int"
|
|
|
|
FloatF BuiltinFunc = "float"
|
2020-08-04 18:41:06 +02:00
|
|
|
Vec2F BuiltinFunc = "vec2"
|
|
|
|
Vec3F BuiltinFunc = "vec3"
|
|
|
|
Vec4F BuiltinFunc = "vec4"
|
2022-11-20 07:36:07 +01:00
|
|
|
IVec2F BuiltinFunc = "ivec2"
|
|
|
|
IVec3F BuiltinFunc = "ivec3"
|
|
|
|
IVec4F BuiltinFunc = "ivec4"
|
2020-08-04 18:41:06 +02:00
|
|
|
Mat2F BuiltinFunc = "mat2"
|
|
|
|
Mat3F BuiltinFunc = "mat3"
|
|
|
|
Mat4F BuiltinFunc = "mat4"
|
2022-08-18 10:46:51 +02:00
|
|
|
Radians BuiltinFunc = "radians" // This function is not used yet (#2253)
|
|
|
|
Degrees BuiltinFunc = "degrees" // This function is not used yet (#2253)
|
2020-08-04 18:41:06 +02:00
|
|
|
Sin BuiltinFunc = "sin"
|
|
|
|
Cos BuiltinFunc = "cos"
|
|
|
|
Tan BuiltinFunc = "tan"
|
|
|
|
Asin BuiltinFunc = "asin"
|
|
|
|
Acos BuiltinFunc = "acos"
|
|
|
|
Atan BuiltinFunc = "atan"
|
2020-09-21 21:29:31 +02:00
|
|
|
Atan2 BuiltinFunc = "atan2"
|
2020-08-04 18:41:06 +02:00
|
|
|
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"
|
2022-08-18 18:43:40 +02:00
|
|
|
Refract BuiltinFunc = "refract"
|
2020-08-04 18:41:06 +02:00
|
|
|
Transpose BuiltinFunc = "transpose"
|
|
|
|
Dfdx BuiltinFunc = "dfdx"
|
|
|
|
Dfdy BuiltinFunc = "dfdy"
|
|
|
|
Fwidth BuiltinFunc = "fwidth"
|
2022-08-09 04:31:56 +02:00
|
|
|
DiscardF BuiltinFunc = "discard"
|
2023-04-23 15:57:30 +02:00
|
|
|
TexelAt BuiltinFunc = "__texelAt"
|
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) {
|
2020-08-15 19:07:36 +02:00
|
|
|
case Len,
|
2020-10-17 16:36:07 +02:00
|
|
|
Cap,
|
2020-08-15 19:07:36 +02:00
|
|
|
BoolF,
|
2020-08-11 16:04:49 +02:00
|
|
|
IntF,
|
|
|
|
FloatF,
|
|
|
|
Vec2F,
|
2020-05-31 11:11:55 +02:00
|
|
|
Vec3F,
|
|
|
|
Vec4F,
|
2022-11-20 07:36:07 +01:00
|
|
|
IVec2F,
|
|
|
|
IVec3F,
|
|
|
|
IVec4F,
|
2020-05-31 11:11:55 +02:00
|
|
|
Mat2F,
|
|
|
|
Mat3F,
|
|
|
|
Mat4F,
|
|
|
|
Sin,
|
|
|
|
Cos,
|
|
|
|
Tan,
|
|
|
|
Asin,
|
|
|
|
Acos,
|
|
|
|
Atan,
|
2020-09-21 21:29:31 +02:00
|
|
|
Atan2,
|
2020-05-31 11:11:55 +02:00
|
|
|
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,
|
2022-08-18 18:43:40 +02:00
|
|
|
Refract,
|
2020-05-31 11:11:55 +02:00
|
|
|
Transpose,
|
2020-07-11 14:50:07 +02:00
|
|
|
Dfdx,
|
|
|
|
Dfdy,
|
2022-08-09 04:31:56 +02:00
|
|
|
Fwidth,
|
2023-04-23 15:57:30 +02:00
|
|
|
DiscardF,
|
|
|
|
TexelAt:
|
2020-05-31 11:11:55 +02:00
|
|
|
return BuiltinFunc(str), true
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
2020-08-08 12:08:07 +02:00
|
|
|
|
|
|
|
func IsValidSwizzling(s string) bool {
|
|
|
|
if len(s) < 1 || 4 < len(s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
xyzw = "xyzw"
|
|
|
|
rgba = "rgba"
|
|
|
|
strq = "strq"
|
|
|
|
)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.IndexByte(xyzw, s[0]) >= 0:
|
|
|
|
for _, c := range s {
|
|
|
|
if strings.IndexRune(xyzw, c) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case strings.IndexByte(rgba, s[0]) >= 0:
|
|
|
|
for _, c := range s {
|
|
|
|
if strings.IndexRune(rgba, c) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case strings.IndexByte(strq, s[0]) >= 0:
|
|
|
|
for _, c := range s {
|
|
|
|
if strings.IndexRune(strq, c) == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-07-09 11:11:56 +02:00
|
|
|
|
2022-11-03 10:48:59 +01:00
|
|
|
func (p *Program) ReachableFuncsFromBlock(block *Block) []*Func {
|
2021-07-09 11:11:56 +02:00
|
|
|
indexToFunc := map[int]*Func{}
|
|
|
|
for _, f := range p.Funcs {
|
|
|
|
f := f
|
|
|
|
indexToFunc[f.Index] = &f
|
|
|
|
}
|
2022-11-03 10:16:54 +01:00
|
|
|
|
2021-07-09 11:11:56 +02:00
|
|
|
visited := map[int]struct{}{}
|
2022-11-03 10:16:54 +01:00
|
|
|
var indices []int
|
|
|
|
var f func(expr *Expr)
|
|
|
|
f = func(expr *Expr) {
|
|
|
|
if expr.Type != FunctionExpr {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := visited[expr.Index]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
indices = append(indices, expr.Index)
|
|
|
|
visited[expr.Index] = struct{}{}
|
|
|
|
walkExprs(f, indexToFunc[expr.Index].Block)
|
|
|
|
}
|
|
|
|
walkExprs(f, block)
|
|
|
|
|
2022-11-03 09:44:11 +01:00
|
|
|
sort.Ints(indices)
|
2022-11-03 10:16:54 +01:00
|
|
|
|
2022-11-03 09:44:11 +01:00
|
|
|
funcs := make([]*Func, 0, len(indices))
|
|
|
|
for _, i := range indices {
|
|
|
|
funcs = append(funcs, indexToFunc[i])
|
|
|
|
}
|
|
|
|
return funcs
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
|
|
|
|
2022-11-03 10:16:54 +01:00
|
|
|
func walkExprs(f func(expr *Expr), block *Block) {
|
|
|
|
if block == nil {
|
|
|
|
return
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
2022-11-03 10:16:54 +01:00
|
|
|
for _, s := range block.Stmts {
|
2021-07-09 11:11:56 +02:00
|
|
|
for _, e := range s.Exprs {
|
2022-11-03 10:16:54 +01:00
|
|
|
walkExprsInExpr(f, &e)
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
2022-11-03 10:16:54 +01:00
|
|
|
for _, b := range s.Blocks {
|
|
|
|
walkExprs(f, b)
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-03 10:16:54 +01:00
|
|
|
func walkExprsInExpr(f func(expr *Expr), expr *Expr) {
|
|
|
|
if expr == nil {
|
|
|
|
return
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
2022-11-03 10:16:54 +01:00
|
|
|
f(expr)
|
|
|
|
for _, e := range expr.Exprs {
|
|
|
|
walkExprsInExpr(f, &e)
|
2021-07-09 11:11:56 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-03 10:48:59 +01:00
|
|
|
|
2023-05-04 07:34:09 +02:00
|
|
|
func (p *Program) appendReachableUniformVariablesFromBlock(indices []int, block *Block) []int {
|
2022-11-03 10:48:59 +01:00
|
|
|
indexToFunc := map[int]*Func{}
|
|
|
|
for _, f := range p.Funcs {
|
|
|
|
f := f
|
|
|
|
indexToFunc[f.Index] = &f
|
|
|
|
}
|
|
|
|
|
|
|
|
visitedFuncs := map[int]struct{}{}
|
2023-05-04 07:34:09 +02:00
|
|
|
indicesSet := map[int]struct{}{}
|
2022-11-03 10:48:59 +01:00
|
|
|
var f func(expr *Expr)
|
|
|
|
f = func(expr *Expr) {
|
|
|
|
switch expr.Type {
|
|
|
|
case UniformVariable:
|
2023-05-05 07:24:48 +02:00
|
|
|
if _, ok := indicesSet[expr.Index]; ok {
|
|
|
|
return
|
|
|
|
}
|
2023-05-04 07:34:09 +02:00
|
|
|
indicesSet[expr.Index] = struct{}{}
|
2023-05-05 07:24:48 +02:00
|
|
|
indices = append(indices, expr.Index)
|
2022-11-03 10:48:59 +01:00
|
|
|
case FunctionExpr:
|
|
|
|
if _, ok := visitedFuncs[expr.Index]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
visitedFuncs[expr.Index] = struct{}{}
|
|
|
|
walkExprs(f, indexToFunc[expr.Index].Block)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
walkExprs(f, block)
|
|
|
|
|
2023-05-04 07:34:09 +02:00
|
|
|
return indices
|
2022-11-03 10:48:59 +01:00
|
|
|
}
|
2022-11-03 17:23:17 +01:00
|
|
|
|
2023-12-24 15:10:41 +01:00
|
|
|
// FilterUniformVariables replaces uniform variables with 0 when they are not used.
|
2022-12-03 07:37:14 +01:00
|
|
|
// By minimizing uniform variables, more commands can be merged in the graphicscommand package.
|
2022-12-03 08:11:26 +01:00
|
|
|
func (p *Program) FilterUniformVariables(uniforms []uint32) {
|
2023-08-19 20:08:35 +02:00
|
|
|
if p.uniformFactors == nil {
|
2023-05-04 07:34:09 +02:00
|
|
|
indices := p.appendReachableUniformVariablesFromBlock(nil, p.VertexFunc.Block)
|
|
|
|
indices = p.appendReachableUniformVariablesFromBlock(indices, p.FragmentFunc.Block)
|
2023-05-04 07:04:48 +02:00
|
|
|
reachableUniforms := make([]bool, len(p.Uniforms))
|
2023-05-04 07:34:09 +02:00
|
|
|
for _, idx := range indices {
|
|
|
|
reachableUniforms[idx] = true
|
2022-11-03 17:23:17 +01:00
|
|
|
}
|
2023-09-27 18:56:51 +02:00
|
|
|
p.uniformFactors = make([]uint32, len(uniforms))
|
|
|
|
var idx int
|
2022-12-03 15:09:41 +01:00
|
|
|
for i, typ := range p.Uniforms {
|
2023-09-27 18:56:51 +02:00
|
|
|
c := typ.Uint32Count()
|
2023-05-04 07:04:48 +02:00
|
|
|
if reachableUniforms[i] {
|
2023-09-27 18:56:51 +02:00
|
|
|
for i := idx; i < idx+c; i++ {
|
|
|
|
p.uniformFactors[i] = 1
|
2023-05-04 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-27 18:56:51 +02:00
|
|
|
idx += c
|
2022-12-03 15:09:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 20:08:35 +02:00
|
|
|
for i, factor := range p.uniformFactors {
|
|
|
|
uniforms[i] *= factor
|
2022-11-03 17:23:17 +01:00
|
|
|
}
|
|
|
|
}
|