From ce4732a7dc3b12b0f5b2bff295d763a09bfb98ac Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 5 Jul 2020 03:29:34 +0900 Subject: [PATCH] shader: Treat if's 'init' part correctly Updates #1230 --- internal/shader/stmt.go | 17 ++++++++++++++++- internal/shader/testdata/if_init.expected.vs | 14 ++++++++++++++ internal/shader/testdata/if_init.go | 9 +++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 internal/shader/testdata/if_init.expected.vs create mode 100644 internal/shader/testdata/if_init.go diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index a535eea1c..a21ba3020 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -111,7 +111,22 @@ func (cs *compileState) parseStmt(block *block, stmt ast.Stmt, inParams []variab return nil, false } case *ast.IfStmt: - // TODO: Parse stmt.Init + if stmt.Init != nil { + init := stmt.Init + stmt.Init = nil + b, ok := cs.parseBlock(block, []ast.Stmt{init, stmt}, nil, nil) + if !ok { + return nil, false + } + + stmts = append(stmts, shaderir.Stmt{ + Type: shaderir.BlockStmt, + Blocks: []shaderir.Block{ + b.ir, + }, + }) + return stmts, true + } exprs, ts, ss, ok := cs.parseExpr(block, stmt.Cond) if !ok { diff --git a/internal/shader/testdata/if_init.expected.vs b/internal/shader/testdata/if_init.expected.vs new file mode 100644 index 000000000..33b066a38 --- /dev/null +++ b/internal/shader/testdata/if_init.expected.vs @@ -0,0 +1,14 @@ +void F0(out vec2 l0) { + vec2 l1 = vec2(0); + l1 = vec2(0.0); + { + vec2 l2 = vec2(0); + l2 = vec2(1.0); + if (((l2).x) == (1.0)) { + l0 = l2; + return; + } + } + l0 = l1; + return; +} diff --git a/internal/shader/testdata/if_init.go b/internal/shader/testdata/if_init.go new file mode 100644 index 000000000..a506e523c --- /dev/null +++ b/internal/shader/testdata/if_init.go @@ -0,0 +1,9 @@ +package main + +func Foo() vec2 { + v := vec2(0) + if v := vec2(1); v.x == 1 { + return v + } + return v +}