diff --git a/internal/shader/expr.go b/internal/shader/expr.go index c9461e03c..fef0ee7cd 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -430,9 +430,6 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar return nil, nil, nil, false } t = shaderir.Type{Main: shaderir.Mat4} - case shaderir.Smoothstep: - // TODO: Check arg types. - t = argts[2] case shaderir.Texture2DF: // TODO: Check arg types. t = shaderir.Type{Main: shaderir.Vec4} @@ -450,7 +447,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar }) return nil, nil, stmts, true - case shaderir.Clamp, shaderir.Mix: + case shaderir.Clamp, shaderir.Mix, shaderir.Smoothstep: // 3 arguments if len(args) != 3 { cs.addError(e.Pos(), fmt.Sprintf("number of %s's arguments must be 3 but %d", callee.BuiltinFunc, len(args))) @@ -483,9 +480,19 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar cs.addError(e.Pos(), fmt.Sprintf("the third arguments for %s must equal to the first/second argument %s or float but %s", callee.BuiltinFunc, argts[0].String(), argts[2].String())) return nil, nil, nil, false } + case shaderir.Smoothstep: + if (!argts[0].Equal(&argts[1]) || !argts[0].Equal(&argts[2])) && (argts[0].Main != shaderir.Float || argts[1].Main != shaderir.Float) { + cs.addError(e.Pos(), fmt.Sprintf("the first and the second arguments for %s must equal to the third argument %s or float but %s and %s", callee.BuiltinFunc, argts[2].String(), argts[0].String(), argts[1].String())) + return nil, nil, nil, false + } } - t = argts[0] + switch callee.BuiltinFunc { + case shaderir.Smoothstep: + t = argts[2] + default: + t = argts[0] + } case shaderir.Atan2, shaderir.Mod, shaderir.Min, shaderir.Max, shaderir.Step, shaderir.Distance, shaderir.Dot, shaderir.Cross, shaderir.Reflect: // 2 arguments diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index 64f7989fe..607920a41 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -1944,6 +1944,59 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { } } +// Issue #2184 +func TestSyntaxBuiltinFuncSmoothstepType(t *testing.T) { + cases := []struct { + stmt string + err bool + }{ + {stmt: "a := smoothstep(); _ = a", err: true}, + {stmt: "a := smoothstep(1); _ = a", err: true}, + {stmt: "a := smoothstep(false, false); _ = a", err: true}, + {stmt: "a := smoothstep(1, 1); _ = a", err: true}, + {stmt: "a := smoothstep(false, false, false); _ = a", err: true}, + {stmt: "a := smoothstep(1, 1, 1); _ = a", err: false}, + {stmt: "a := smoothstep(1.0, 1, 1); _ = a", err: false}, + {stmt: "a := smoothstep(1, 1.0, 1); _ = a", err: false}, + {stmt: "a := smoothstep(1, 1, 1.0); _ = a", err: false}, + {stmt: "a := smoothstep(1, vec2(1), 1); _ = a", err: true}, + {stmt: "a := smoothstep(1, 1, vec2(1)); _ = a", err: false}, + {stmt: "a := smoothstep(1, 1, vec3(1)); _ = a", err: false}, + {stmt: "a := smoothstep(1, 1, vec4(1)); _ = a", err: false}, + {stmt: "a := smoothstep(1, vec2(1), vec2(1)); _ = a", err: true}, + {stmt: "a := smoothstep(vec2(1), 1, 1); _ = a", err: true}, + {stmt: "a := smoothstep(vec2(1), 1, vec2(1)); _ = a", err: true}, + {stmt: "a := smoothstep(vec2(1), vec2(1), 1); _ = a", err: true}, + {stmt: "a := smoothstep(vec2(1), vec2(1), vec2(1)); _ = a", err: false}, + {stmt: "a := smoothstep(vec2(1), vec2(1), vec3(1)); _ = a", err: true}, + {stmt: "a := smoothstep(vec3(1), 1, 1); _ = a", err: true}, + {stmt: "a := smoothstep(vec3(1), 1, vec3(1)); _ = a", err: true}, + {stmt: "a := smoothstep(vec3(1), vec3(1), 1); _ = a", err: true}, + {stmt: "a := smoothstep(vec3(1), vec3(1), vec3(1)); _ = a", err: false}, + {stmt: "a := smoothstep(vec4(1), 1, 1); _ = a", err: true}, + {stmt: "a := smoothstep(vec4(1), 1, vec4(1)); _ = a", err: true}, + {stmt: "a := smoothstep(vec4(1), vec4(1), 1); _ = a", err: true}, + {stmt: "a := smoothstep(vec4(1), vec4(1), vec4(1)); _ = a", err: false}, + {stmt: "a := smoothstep(1, 1, 1, 1); _ = a", err: true}, + } + + for _, c := range cases { + stmt := c.stmt + src := fmt.Sprintf(`package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + %s + return position +}`, stmt) + _, err := compileToIR([]byte(src)) + if err == nil && c.err { + t.Errorf("%s must return an error but does not", stmt) + } else if err != nil && !c.err { + t.Errorf("%s must not return nil but returned %v", stmt, err) + } + } +} + // Issue #2184 func TestSyntaxBuiltinFuncCrossType(t *testing.T) { cases := []struct {