shader: Bug fix: Treat number literals in function calls correctly

This commit is contained in:
Hajime Hoshi 2020-09-03 02:45:22 +09:00
parent 0b1d29b6e7
commit 4ac11bf156
5 changed files with 55 additions and 4 deletions

View File

@ -286,6 +286,14 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
f := cs.funcs[callee.Index] 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 var outParams []int
for _, p := range f.ir.OutParams { for _, p := range f.ir.OutParams {
idx := block.totalLocalVariableNum() idx := block.totalLocalVariableNum()

View File

@ -15,6 +15,7 @@
package shader_test package shader_test
import ( import (
"fmt"
"go/parser" "go/parser"
"go/token" "go/token"
"io/ioutil" "io/ioutil"
@ -117,7 +118,24 @@ func TestCompile(t *testing.T) {
} }
vs, fs := glsl.Compile(s) vs, fs := glsl.Compile(s)
if got, want := normalize(vs), normalize(string(tc.VS)); got != want { 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 tc.FS != nil {
if got, want := normalize(fs), normalize(string(tc.FS)); got != want { if got, want := normalize(fs), normalize(string(tc.FS)); got != want {

View File

@ -24,9 +24,9 @@ import (
"github.com/hajimehoshi/ebiten/internal/shaderir" "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) { 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 return false
} }
expr.ConstType = shaderir.ConstTypeInt expr.ConstType = shaderir.ConstTypeInt

View File

@ -2,6 +2,9 @@ void F0(out vec2 l0);
void F1(out vec2 l0); void F1(out vec2 l0);
void F2(out float l0); void F2(out float l0);
void F3(out int l0); void F3(out int l0);
void F4(in float l0);
void F5(in int l0);
void F6(void);
void F0(out vec2 l0) { void F0(out vec2 l0) {
float l1 = float(0); float l1 = float(0);
@ -46,3 +49,14 @@ void F3(out int l0) {
l0 = 1; l0 = 1;
return; return;
} }
void F4(in float l0) {
}
void F5(in int l0) {
}
void F6(void) {
F4(1.0);
F5(1);
}

View File

@ -17,5 +17,16 @@ func Float() float {
} }
func Int() int { func Int() int {
return 1 return 1.0
}
func TakeFloat(x float) {
}
func TakeInt(x int) {
}
func Foo3() {
TakeFloat(1.0)
TakeInt(1.0)
} }