From 382ba7513924494eb8ebee73986efd8dd0ff4a43 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 3 Jun 2020 22:30:34 +0900 Subject: [PATCH] shader: Enable to put global variables anywhere --- internal/shader/shader.go | 10 +++++++++- internal/shader/shader_test.go | 9 ++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 9879a6d9b..b7f406dc7 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -140,8 +140,16 @@ func (s *compileState) addError(pos token.Pos, str string) { } func (cs *compileState) parse(f *ast.File) { + // Parse GenDecl for global variables, and then parse functions. for _, d := range f.Decls { - cs.parseDecl(&cs.global, d) + if _, ok := d.(*ast.FuncDecl); !ok { + cs.parseDecl(&cs.global, d) + } + } + for _, d := range f.Decls { + if _, ok := d.(*ast.FuncDecl); ok { + cs.parseDecl(&cs.global, d) + } } if len(cs.errs) > 0 { diff --git a/internal/shader/shader_test.go b/internal/shader/shader_test.go index 15e1a162e..a9e3eda03 100644 --- a/internal/shader/shader_test.go +++ b/internal/shader/shader_test.go @@ -135,8 +135,6 @@ func Foo(foo vec2) vec4 { Name: "vertex", Src: `package main -var ScreenSize vec2 - func Vertex(position vec2, texCoord vec2, color vec4) (position vec4, texCoord vec2, color vec4) { projectionMatrix := mat4( 2 / ScreenSize.x, 0, 0, 0, @@ -145,7 +143,9 @@ func Vertex(position vec2, texCoord vec2, color vec4) (position vec4, texCoord v -1, -1, 0, 1, ) return projectionMatrix * vec4(position, 0, 1), texCoord, color -}`, +} + +var ScreenSize vec2`, VS: `uniform vec2 U0; attribute vec2 A0; attribute vec2 A1; @@ -161,6 +161,9 @@ void main(void) { V1 = A2; return; }`, + FS: `uniform vec2 U0; +varying vec2 V0; +varying vec4 V1;`, }, } for _, tc := range tests {