From 9b6b306ca31b2dfe1a7f430394db755ea25a385b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 10 Aug 2020 01:39:41 +0900 Subject: [PATCH] shader: Bug fix: Wrong local variable index in a block in an entry point --- internal/shader/shader.go | 9 ++++++ internal/shader/testdata/for5.expected.vs | 35 +++++++++++++++++++++++ internal/shader/testdata/for5.go | 26 +++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 internal/shader/testdata/for5.expected.vs create mode 100644 internal/shader/testdata/for5.go diff --git a/internal/shader/shader.go b/internal/shader/shader.go index f84642b15..58c4487eb 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -677,6 +677,15 @@ func (cs *compileState) parseBlock(outer *block, fname string, stmts []ast.Stmt, for b := outer; b != nil; b = b.outer { offset += len(b.vars) } + if fname == cs.vertexEntry { + offset -= len(cs.ir.Attributes) + offset-- // position + offset -= len(cs.ir.Varyings) + } + if fname == cs.fragmentEntry { + offset-- // position + offset -= len(cs.ir.Varyings) + } } block := &block{ diff --git a/internal/shader/testdata/for5.expected.vs b/internal/shader/testdata/for5.expected.vs new file mode 100644 index 000000000..4025d0369 --- /dev/null +++ b/internal/shader/testdata/for5.expected.vs @@ -0,0 +1,35 @@ +uniform float U0; +uniform float U1; +uniform float U2; +attribute vec2 A0; + +void F0(in int l0, out int l1); + +void F0(in int l0, out int l1) { + l1 = l0; + return; +} + +void main(void) { + int l0 = 0; + int l2 = 0; + l0 = 0; + for (int l1 = 0; l1 < 10; l1++) { + int l2 = 0; + int l3 = 0; + F0(l1, l2); + l3 = l2; + l0 = (l0) + (l3); + for (int l4 = 0; l4 < 10; l4++) { + int l5 = 0; + int l6 = 0; + F0(l4, l5); + l6 = l5; + l0 = (l0) + (l6); + } + } + l2 = 0; + l0 = (l0) + (l2); + gl_Position = vec4(l0); + return; +} diff --git a/internal/shader/testdata/for5.go b/internal/shader/testdata/for5.go new file mode 100644 index 000000000..f3a5e9fe9 --- /dev/null +++ b/internal/shader/testdata/for5.go @@ -0,0 +1,26 @@ +package main + +var ( + U0 float + U1 float + U2 float +) + +func Ident(x int) int { + return x +} + +func Vertex(pos vec2) vec4 { + sum := 0 + for i := 0; i < 10; i++ { + x := Ident(i) + sum += x + for j := 0; j < 10; j++ { + x := Ident(j) + sum += x + } + } + y := 0 + sum += y + return vec4(sum) +}