// Copyright 2023 The Ebitengine 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. // Modified by the MyGo contributors: uint and uvecN. package shaderir import ( "go/constant" ) func ResolveUntypedConstsForBinaryOp(op Op, lhs, rhs constant.Value, lhst, rhst Type) (newLhs, newRhs constant.Value, ok bool) { if lhst.Main == None && rhst.Main == None { if op == LeftShift || op == RightShift { newLhs = constant.ToInt(lhs) newRhs = constant.ToInt(rhs) if newLhs.Kind() == constant.Unknown { return nil, nil, false } if newRhs.Kind() == constant.Unknown { return nil, nil, false } return newLhs, newRhs, true } if lhs.Kind() == rhs.Kind() { return lhs, rhs, true } if lhs.Kind() == constant.Float && constant.ToFloat(rhs).Kind() != constant.Unknown { return lhs, constant.ToFloat(rhs), true } if rhs.Kind() == constant.Float && constant.ToFloat(lhs).Kind() != constant.Unknown { return constant.ToFloat(lhs), rhs, true } if lhs.Kind() == constant.Int && constant.ToInt(rhs).Kind() != constant.Unknown { return lhs, constant.ToInt(rhs), true } if rhs.Kind() == constant.Int && constant.ToInt(lhs).Kind() != constant.Unknown { return constant.ToInt(lhs), rhs, true } return nil, nil, false } if lhst.Main == None { if (rhst.Main == Float || rhst.IsFloatVector() || rhst.IsMatrix()) && constant.ToFloat(lhs).Kind() != constant.Unknown { return constant.ToFloat(lhs), rhs, true } if rhst.IsInteger() && constant.ToInt(lhs).Kind() != constant.Unknown { return constant.ToInt(lhs), rhs, true } if rhst.Main == Bool && lhs.Kind() == constant.Bool { return lhs, rhs, true } return nil, nil, false } if rhst.Main == None { if (lhst.Main == Float || lhst.IsFloatVector() || lhst.IsMatrix()) && constant.ToFloat(rhs).Kind() != constant.Unknown { return lhs, constant.ToFloat(rhs), true } if lhst.IsInteger() && constant.ToInt(rhs).Kind() != constant.Unknown { return lhs, constant.ToInt(rhs), true } if lhst.Main == Bool && rhs.Kind() == constant.Bool { return lhs, rhs, true } return nil, nil, false } // lhst and rhst might not match, but this has nothing to do with resolving untyped consts. return lhs, rhs, true } func TypeFromBinaryOp(op Op, lhst, rhst Type, lhsConst, rhsConst constant.Value) (Type, bool) { switch op { case Add, Sub, ComponentWiseMul, MatrixMul, Div: if lhst.Main == Bool || rhst.Main == Bool { return Type{}, false } if lhst.Main == Array || rhst.Main == Array { return Type{}, false } if lhst.Main == Texture || rhst.Main == Texture { return Type{}, false } if (lhsConst != nil && lhsConst.Kind() == constant.Bool) || (rhsConst != nil && rhsConst.Kind() == constant.Bool) { return Type{}, false } } // If both are untyped consts, compare the constants and try to truncate them if necessary. if lhst.Main == None && rhst.Main == None { // Assume that the constant types are already adjusted. if lhsConst.Kind() != rhsConst.Kind() { panic("shaderir: const types for a binary op must be adjusted") } if op == AndAnd || op == OrOr { if lhsConst.Kind() == constant.Bool && rhsConst.Kind() == constant.Bool { return Type{Main: Bool}, true } return Type{}, false } // For %, both operands must be integers if both are constants. Truncatable to an integer is not enough. if op == ModOp { if lhsConst.Kind() == constant.Int && rhsConst.Kind() == constant.Int { return Type{}, true } return Type{}, false } if op == And || op == AndNot || op == Or || op == Xor { if lhsConst.Kind() == constant.Int && rhsConst.Kind() == constant.Int { return Type{}, true } return Type{}, false } if op == LeftShift || op == RightShift { if lhsConst.Kind() == constant.Int && rhsConst.Kind() == constant.Int { return Type{}, true } return Type{}, false } if op == EqualOp || op == NotEqualOp || op == LessThanOp || op == LessThanEqualOp || op == GreaterThanOp || op == GreaterThanEqualOp { return Type{Main: Bool}, true } if lhst.Main == Float || rhst.Main == Float { return Type{Main: Float}, true } if lhst.Main == Int || rhst.Main == Int { return Type{Main: Int}, true } if lhst.Main == Bool || rhst.Main == Bool { return Type{Main: Bool}, true } // If both operands are untyped, keep untyped. return Type{}, true } // Both types must not be untyped. if lhst.Main == None || rhst.Main == None { panic("shaderir: cannot resolve untyped values") } if op == AndAnd || op == OrOr { if lhst.Main == Bool && rhst.Main == Bool { return Type{Main: Bool}, true } return Type{}, false } if op == VectorEqualOp || op == VectorNotEqualOp { if (lhst.IsFloatVector() || lhst.IsIntegerVector()) && lhst.Equal(&rhst) { return Type{Main: Bool}, true } return Type{}, false } if op == LessThanOp || op == LessThanEqualOp || op == GreaterThanOp || op == GreaterThanEqualOp { if (lhst.Main == Int && rhst.Main == Int) || (lhst.Main == Uint && rhst.Main == Uint) || (lhst.Main == Float && rhst.Main == Float) { return Type{Main: Bool}, true } return Type{}, false } // Comparing matrices is forbidden (#2187). // Comparing arrays is forbidden as well, as most of the shading languages don't have the // operation (#3535). // Comparing textures is forbidden as well, as a texture is an opaque handle. if op == EqualOp || op == NotEqualOp { if lhst.IsMatrix() || rhst.IsMatrix() { return Type{}, false } if lhst.Main == Array || rhst.Main == Array { return Type{}, false } if lhst.Main == Texture || rhst.Main == Texture { return Type{}, false } if lhst.Equal(&rhst) { return Type{Main: Bool}, true } return Type{}, false } if op == Div && rhst.IsMatrix() { return Type{}, false } // The operands of %, &, &^, |, ^ are integers of one kind: int and ivecN, // or uint and uvecN (MyGo's), a vector with a vector of its size or a // scalar (on either side, but for %). if op == ModOp || op == And || op == AndNot || op == Or || op == Xor { if !lhst.IsInteger() || !rhst.IsInteger() || lhst.Scalar() != rhst.Scalar() { return Type{}, false } if lhst.Equal(&rhst) { return lhst, true } if lhst.IsIntegerVector() && !rhst.IsIntegerVector() { return lhst, true } if op != ModOp && !lhst.IsIntegerVector() && rhst.IsIntegerVector() { return rhst, true } return Type{}, false } // A shift's count may be of either kind of integer, as in Go: a scalar, // or a vector of the shifted vector's size. if op == LeftShift || op == RightShift { if !lhst.IsInteger() || !rhst.IsInteger() { return Type{}, false } if !rhst.IsIntegerVector() { return lhst, true } if lhst.IsIntegerVector() && lhst.VectorElementCount() == rhst.VectorElementCount() { return lhst, true } return Type{}, false } if lhst.Equal(&rhst) { if lhst.Main == None { return rhst, true } return lhst, true } if op == MatrixMul { if lhst.IsMatrix() && rhst.Main == Float { return lhst, true } if lhst.Main == Mat2 && rhst.Main == Vec2 { return rhst, true } if lhst.Main == Mat3 && rhst.Main == Vec3 { return rhst, true } if lhst.Main == Mat4 && rhst.Main == Vec4 { return rhst, true } if lhst.Main == Float && rhst.IsMatrix() { return rhst, true } if lhst.Main == Vec2 && rhst.Main == Mat2 { return lhst, true } if lhst.Main == Vec3 && rhst.Main == Mat3 { return lhst, true } if lhst.Main == Vec4 && rhst.Main == Mat4 { return lhst, true } return Type{}, false } if op == Div { if lhst.IsMatrix() && rhst.Main == Float { return lhst, true } // fallback } if lhst.IsFloatVector() && rhst.Main == Float { return lhst, true } if lhst.Main == Float && rhst.IsFloatVector() { return rhst, true } if lhst.IsIntVector() && rhst.Main == Int { return lhst, true } if lhst.Main == Int && rhst.IsIntVector() { return rhst, true } if lhst.IsUintVector() && rhst.Main == Uint { return lhst, true } if lhst.Main == Uint && rhst.IsUintVector() { return rhst, true } return Type{}, false }