mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 18:52:44 +01:00
shader: Bug fix: Use integer literals in the integer context
Fixes #1299
This commit is contained in:
parent
39d829d3bf
commit
50cd33ed9b
@ -52,6 +52,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
|
|||||||
default:
|
default:
|
||||||
cs.addError(e.Pos(), fmt.Sprintf("literal not implemented: %#v", e))
|
cs.addError(e.Pos(), fmt.Sprintf("literal not implemented: %#v", e))
|
||||||
}
|
}
|
||||||
|
|
||||||
case *ast.BinaryExpr:
|
case *ast.BinaryExpr:
|
||||||
var stmts []shaderir.Stmt
|
var stmts []shaderir.Stmt
|
||||||
|
|
||||||
@ -119,6 +120,24 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
|
|||||||
switch {
|
switch {
|
||||||
case op == shaderir.LessThanOp || op == shaderir.LessThanEqualOp || op == shaderir.GreaterThanOp || op == shaderir.GreaterThanEqualOp || op == shaderir.EqualOp || op == shaderir.NotEqualOp || op == shaderir.AndAnd || op == shaderir.OrOr:
|
case op == shaderir.LessThanOp || op == shaderir.LessThanEqualOp || op == shaderir.GreaterThanOp || op == shaderir.GreaterThanEqualOp || op == shaderir.EqualOp || op == shaderir.NotEqualOp || op == shaderir.AndAnd || op == shaderir.OrOr:
|
||||||
t = shaderir.Type{Main: shaderir.Bool}
|
t = shaderir.Type{Main: shaderir.Bool}
|
||||||
|
case lhs[0].Type == shaderir.NumberExpr && rhs[0].Type != shaderir.NumberExpr:
|
||||||
|
if rhst.Main == shaderir.Int {
|
||||||
|
if !canTruncateToInteger(lhs[0].Const) {
|
||||||
|
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", lhs[0].Const.String()))
|
||||||
|
return nil, nil, nil, false
|
||||||
|
}
|
||||||
|
lhs[0].ConstType = shaderir.ConstTypeInt
|
||||||
|
}
|
||||||
|
t = rhst
|
||||||
|
case lhs[0].Type != shaderir.NumberExpr && rhs[0].Type == shaderir.NumberExpr:
|
||||||
|
if lhst.Main == shaderir.Int {
|
||||||
|
if !canTruncateToInteger(rhs[0].Const) {
|
||||||
|
cs.addError(e.Pos(), fmt.Sprintf("constant %s truncated to integer", rhs[0].Const.String()))
|
||||||
|
return nil, nil, nil, false
|
||||||
|
}
|
||||||
|
rhs[0].ConstType = shaderir.ConstTypeInt
|
||||||
|
}
|
||||||
|
t = lhst
|
||||||
case lhst.Equal(&rhst):
|
case lhst.Equal(&rhst):
|
||||||
t = lhst
|
t = lhst
|
||||||
case lhst.Main == shaderir.Float || lhst.Main == shaderir.Int:
|
case lhst.Main == shaderir.Float || lhst.Main == shaderir.Int:
|
||||||
@ -162,6 +181,7 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
|
|||||||
Exprs: []shaderir.Expr{lhs[0], rhs[0]},
|
Exprs: []shaderir.Expr{lhs[0], rhs[0]},
|
||||||
},
|
},
|
||||||
}, []shaderir.Type{t}, stmts, true
|
}, []shaderir.Type{t}, stmts, true
|
||||||
|
|
||||||
case *ast.CallExpr:
|
case *ast.CallExpr:
|
||||||
var (
|
var (
|
||||||
callee shaderir.Expr
|
callee shaderir.Expr
|
||||||
|
@ -73,12 +73,22 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
|
|||||||
}
|
}
|
||||||
stmts = append(stmts, ss...)
|
stmts = append(stmts, ss...)
|
||||||
|
|
||||||
lhs, _, ss, ok := cs.parseExpr(block, stmt.Lhs[0])
|
lhs, ts, ss, ok := cs.parseExpr(block, stmt.Lhs[0])
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
stmts = append(stmts, ss...)
|
stmts = append(stmts, ss...)
|
||||||
|
|
||||||
|
if rhs[0].Type == shaderir.NumberExpr {
|
||||||
|
if ts[0].Main == shaderir.Int {
|
||||||
|
if !canTruncateToInteger(rhs[0].Const) {
|
||||||
|
cs.addError(stmt.Pos(), fmt.Sprintf("constant %s truncated to integer", rhs[0].Const.String()))
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
rhs[0].ConstType = shaderir.ConstTypeInt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stmts = append(stmts, shaderir.Stmt{
|
stmts = append(stmts, shaderir.Stmt{
|
||||||
Type: shaderir.Assign,
|
Type: shaderir.Assign,
|
||||||
Exprs: []shaderir.Expr{
|
Exprs: []shaderir.Expr{
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
void main(void) {
|
void main(void) {
|
||||||
vec4 l0 = vec4(0);
|
vec4 l0 = vec4(0);
|
||||||
for (int l1 = 0; l1 < 4; l1++) {
|
for (float l1 = 0.0; l1 < 4.0; l1++) {
|
||||||
(l0).x = ((l0).x) + ((l1) * (1.000000000e-02));
|
(l0).x = ((l0).x) + ((l1) * (1.000000000e-02));
|
||||||
}
|
}
|
||||||
gl_FragColor = l0;
|
gl_FragColor = l0;
|
||||||
|
@ -2,7 +2,7 @@ attribute vec2 A0;
|
|||||||
|
|
||||||
void main(void) {
|
void main(void) {
|
||||||
vec4 l0 = vec4(0);
|
vec4 l0 = vec4(0);
|
||||||
for (int l1 = 0; l1 < 4; l1++) {
|
for (float l1 = 0.0; l1 < 4.0; l1++) {
|
||||||
(l0).x = ((l0).x) + ((l1) * (1.000000000e-02));
|
(l0).x = ((l0).x) + ((l1) * (1.000000000e-02));
|
||||||
}
|
}
|
||||||
gl_Position = l0;
|
gl_Position = l0;
|
||||||
|
4
internal/shader/testdata/issue1245.go
vendored
4
internal/shader/testdata/issue1245.go
vendored
@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
func Vertex(position vec2) vec4 {
|
func Vertex(position vec2) vec4 {
|
||||||
var v vec4
|
var v vec4
|
||||||
for i := 0; i < 4.0; i++ {
|
for i := 0.0; i < 4.0; i++ {
|
||||||
v.x += i * 0.01
|
v.x += i * 0.01
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
@ -10,7 +10,7 @@ func Vertex(position vec2) vec4 {
|
|||||||
|
|
||||||
func Fragment(position vec4) vec4 {
|
func Fragment(position vec4) vec4 {
|
||||||
var v vec4
|
var v vec4
|
||||||
for i := 0; i < 4.0; i++ {
|
for i := 0.0; i < 4.0; i++ {
|
||||||
v.x += i * 0.01
|
v.x += i * 0.01
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
|
11
internal/shader/testdata/issue1299.expected.vs
vendored
Normal file
11
internal/shader/testdata/issue1299.expected.vs
vendored
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
void F0(out int l0);
|
||||||
|
|
||||||
|
void F0(out int l0) {
|
||||||
|
int l1 = 0;
|
||||||
|
l1 = 1;
|
||||||
|
l1 = (l1) * (2);
|
||||||
|
l1 = (l1) + (2);
|
||||||
|
l1 = (2) - (l1);
|
||||||
|
l0 = l1;
|
||||||
|
return;
|
||||||
|
}
|
9
internal/shader/testdata/issue1299.go
vendored
Normal file
9
internal/shader/testdata/issue1299.go
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func Foo() int {
|
||||||
|
x := 1
|
||||||
|
x *= 2
|
||||||
|
x = x + 2
|
||||||
|
x = 2 - x
|
||||||
|
return x
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user