mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
shader: Bug fix: Treat number literals in function calls correctly
This commit is contained in:
parent
0b1d29b6e7
commit
4ac11bf156
@ -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()
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
14
internal/shader/testdata/number.expected.vs
vendored
14
internal/shader/testdata/number.expected.vs
vendored
@ -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);
|
||||
}
|
||||
|
13
internal/shader/testdata/number.go
vendored
13
internal/shader/testdata/number.go
vendored
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user