internal/shader: add type checks for the builtin function faceforward

Updates #2184
This commit is contained in:
Hajime Hoshi 2022-08-19 01:37:45 +09:00
parent 73f12fa704
commit 13ae8f5872
2 changed files with 62 additions and 1 deletions

View File

@ -447,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, shaderir.Smoothstep:
case shaderir.Clamp, shaderir.Mix, shaderir.Smoothstep, shaderir.Faceforward:
// 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)))
@ -485,6 +485,11 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
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
}
default:
if !argts[0].Equal(&argts[1]) || !argts[0].Equal(&argts[2]) {
cs.addError(e.Pos(), fmt.Sprintf("all the argument types for %s must be the same but %s, %s, and %s", callee.BuiltinFunc, argts[0].String(), argts[1].String(), argts[2].String()))
return nil, nil, nil, false
}
}
switch callee.BuiltinFunc {

View File

@ -1842,6 +1842,62 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
}
}
// Issue #2184
func TestSyntaxBuiltinFuncTripleArgsType(t *testing.T) {
cases := []struct {
stmt string
err bool
}{
{stmt: "a := {{.Func}}(); _ = a", err: true},
{stmt: "a := {{.Func}}(1); _ = a", err: true},
{stmt: "a := {{.Func}}(false, false); _ = a", err: true},
{stmt: "a := {{.Func}}(1, 1); _ = a", err: true},
{stmt: "a := {{.Func}}(false, false, false); _ = a", err: true},
{stmt: "a := {{.Func}}(1, 1, 1); _ = a", err: false},
{stmt: "a := {{.Func}}(1.0, 1, 1); _ = a", err: false},
{stmt: "a := {{.Func}}(1, 1.0, 1); _ = a", err: false},
{stmt: "a := {{.Func}}(1, 1, 1.0); _ = a", err: false},
{stmt: "a := {{.Func}}(1, vec2(1), 1); _ = a", err: true},
{stmt: "a := {{.Func}}(1, 1, vec2(1)); _ = a", err: true},
{stmt: "a := {{.Func}}(1, vec2(1), vec2(1)); _ = a", err: true},
{stmt: "a := {{.Func}}(vec2(1), 1, 1); _ = a", err: true},
{stmt: "a := {{.Func}}(vec2(1), 1, vec2(1)); _ = a", err: true},
{stmt: "a := {{.Func}}(vec2(1), vec2(1), 1); _ = a", err: true},
{stmt: "a := {{.Func}}(vec2(1), vec2(1), vec2(1)); _ = a", err: false},
{stmt: "a := {{.Func}}(vec2(1), vec2(1), vec3(1)); _ = a", err: true},
{stmt: "a := {{.Func}}(vec3(1), 1, 1); _ = a", err: true},
{stmt: "a := {{.Func}}(vec3(1), 1, vec3(1)); _ = a", err: true},
{stmt: "a := {{.Func}}(vec3(1), vec3(1), 1); _ = a", err: true},
{stmt: "a := {{.Func}}(vec3(1), vec3(1), vec3(1)); _ = a", err: false},
{stmt: "a := {{.Func}}(vec4(1), 1, 1); _ = a", err: true},
{stmt: "a := {{.Func}}(vec4(1), 1, vec4(1)); _ = a", err: true},
{stmt: "a := {{.Func}}(vec4(1), vec4(1), 1); _ = a", err: true},
{stmt: "a := {{.Func}}(vec4(1), vec4(1), vec4(1)); _ = a", err: false},
{stmt: "a := {{.Func}}(1, 1, 1, 1); _ = a", err: true},
}
funcs := []string{
"faceforward",
}
for _, c := range cases {
for _, f := range funcs {
stmt := strings.ReplaceAll(c.stmt, "{{.Func}}", f)
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 TestSyntaxBuiltinFuncClampType(t *testing.T) {
cases := []struct {