shader: Enable to put global variables anywhere

This commit is contained in:
Hajime Hoshi 2020-06-03 22:30:34 +09:00
parent 39c09a4f88
commit 382ba75139
2 changed files with 15 additions and 4 deletions

View File

@ -140,8 +140,16 @@ func (s *compileState) addError(pos token.Pos, str string) {
} }
func (cs *compileState) parse(f *ast.File) { func (cs *compileState) parse(f *ast.File) {
// Parse GenDecl for global variables, and then parse functions.
for _, d := range f.Decls { 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 { if len(cs.errs) > 0 {

View File

@ -135,8 +135,6 @@ func Foo(foo vec2) vec4 {
Name: "vertex", Name: "vertex",
Src: `package main Src: `package main
var ScreenSize vec2
func Vertex(position vec2, texCoord vec2, color vec4) (position vec4, texCoord vec2, color vec4) { func Vertex(position vec2, texCoord vec2, color vec4) (position vec4, texCoord vec2, color vec4) {
projectionMatrix := mat4( projectionMatrix := mat4(
2 / ScreenSize.x, 0, 0, 0, 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, -1, -1, 0, 1,
) )
return projectionMatrix * vec4(position, 0, 1), texCoord, color return projectionMatrix * vec4(position, 0, 1), texCoord, color
}`, }
var ScreenSize vec2`,
VS: `uniform vec2 U0; VS: `uniform vec2 U0;
attribute vec2 A0; attribute vec2 A0;
attribute vec2 A1; attribute vec2 A1;
@ -161,6 +161,9 @@ void main(void) {
V1 = A2; V1 = A2;
return; return;
}`, }`,
FS: `uniform vec2 U0;
varying vec2 V0;
varying vec4 V1;`,
}, },
} }
for _, tc := range tests { for _, tc := range tests {