From 4ac11bf156dff118cca672132ebe9c43e234f638 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 3 Sep 2020 02:45:22 +0900 Subject: [PATCH] shader: Bug fix: Treat number literals in function calls correctly --- internal/shader/expr.go | 8 ++++++++ internal/shader/shader_test.go | 20 +++++++++++++++++++- internal/shader/stmt.go | 4 ++-- internal/shader/testdata/number.expected.vs | 14 ++++++++++++++ internal/shader/testdata/number.go | 13 ++++++++++++- 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/internal/shader/expr.go b/internal/shader/expr.go index 5dc9a32dc..6373e251d 100644 --- a/internal/shader/expr.go +++ b/internal/shader/expr.go @@ -286,6 +286,14 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr, f := cs.funcs[callee.Index] + for i, p := range f.ir.InParams { + if args[i].Type == shaderir.NumberExpr && p.Main == shaderir.Int { + if !cs.forceToInt(e, &args[i]) { + return nil, nil, nil, false + } + } + } + var outParams []int for _, p := range f.ir.OutParams { idx := block.totalLocalVariableNum() diff --git a/internal/shader/shader_test.go b/internal/shader/shader_test.go index c09c0b088..aaf4af4fb 100644 --- a/internal/shader/shader_test.go +++ b/internal/shader/shader_test.go @@ -15,6 +15,7 @@ package shader_test import ( + "fmt" "go/parser" "go/token" "io/ioutil" @@ -117,7 +118,24 @@ func TestCompile(t *testing.T) { } vs, fs := glsl.Compile(s) if got, want := normalize(vs), normalize(string(tc.VS)); got != want { - t.Errorf("got: %v, want: %v", got, want) + var msg string + gotlines := strings.Split(got, "\n") + wantlines := strings.Split(want, "\n") + for i := range gotlines { + if len(wantlines) <= i { + msg = fmt.Sprintf(`lines %d: +got: %s +want: (out of range)`, i+1, gotlines[i]) + break + } + if gotlines[i] != wantlines[i] { + msg = fmt.Sprintf(`lines %d: +got: %s +want: %s`, i+1, gotlines[i], wantlines[i]) + break + } + } + t.Errorf("got: %v, want: %v\n\n%s", got, want, msg) } if tc.FS != nil { if got, want := normalize(fs), normalize(string(tc.FS)); got != want { diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index d1f718300..12424c962 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -24,9 +24,9 @@ import ( "github.com/hajimehoshi/ebiten/internal/shaderir" ) -func (cs *compileState) forceToInt(stmt ast.Stmt, expr *shaderir.Expr) bool { +func (cs *compileState) forceToInt(node ast.Node, expr *shaderir.Expr) bool { if !canTruncateToInteger(expr.Const) { - cs.addError(stmt.Pos(), fmt.Sprintf("constant %s truncated to integer", expr.Const.String())) + cs.addError(node.Pos(), fmt.Sprintf("constant %s truncated to integer", expr.Const.String())) return false } expr.ConstType = shaderir.ConstTypeInt diff --git a/internal/shader/testdata/number.expected.vs b/internal/shader/testdata/number.expected.vs index 00a5577c1..4c02e6d0e 100644 --- a/internal/shader/testdata/number.expected.vs +++ b/internal/shader/testdata/number.expected.vs @@ -2,6 +2,9 @@ void F0(out vec2 l0); void F1(out vec2 l0); void F2(out float l0); void F3(out int l0); +void F4(in float l0); +void F5(in int l0); +void F6(void); void F0(out vec2 l0) { float l1 = float(0); @@ -46,3 +49,14 @@ void F3(out int l0) { l0 = 1; return; } + +void F4(in float l0) { +} + +void F5(in int l0) { +} + +void F6(void) { + F4(1.0); + F5(1); +} diff --git a/internal/shader/testdata/number.go b/internal/shader/testdata/number.go index c99279cd2..38894e0e9 100644 --- a/internal/shader/testdata/number.go +++ b/internal/shader/testdata/number.go @@ -17,5 +17,16 @@ func Float() float { } func Int() int { - return 1 + return 1.0 +} + +func TakeFloat(x float) { +} + +func TakeInt(x int) { +} + +func Foo3() { + TakeFloat(1.0) + TakeInt(1.0) }