From df32901dce1ab5475dccda063f6c9f27e7a59d19 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 16 Apr 2023 18:35:12 +0900 Subject: [PATCH] internal/shader: refactoring --- internal/graphics/shader.go | 10 +--------- internal/shader/shader.go | 9 ++++++++- internal/shader/shader_test.go | 10 +--------- internal/shader/syntax_test.go | 15 +-------------- internal/shaderir/reachable_test.go | 15 +-------------- 5 files changed, 12 insertions(+), 47 deletions(-) diff --git a/internal/graphics/shader.go b/internal/graphics/shader.go index fc121c8bf..f37256524 100644 --- a/internal/graphics/shader.go +++ b/internal/graphics/shader.go @@ -17,8 +17,6 @@ package graphics import ( "bytes" "fmt" - "go/parser" - "go/token" "github.com/hajimehoshi/ebiten/v2/internal/shader" "github.com/hajimehoshi/ebiten/v2/internal/shaderir" @@ -114,17 +112,11 @@ func CompileShader(src []byte) (*shaderir.Program, error) { buf.Write(src) buf.WriteString(shaderSuffix) - fs := token.NewFileSet() - f, err := parser.ParseFile(fs, "", buf.Bytes(), parser.AllErrors) - if err != nil { - return nil, err - } - const ( vert = "__vertex" frag = "Fragment" ) - ir, err := shader.Compile(fs, f, vert, frag, ShaderImageCount) + ir, err := shader.Compile(buf.Bytes(), vert, frag, ShaderImageCount) if err != nil { return nil, err } diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 1353c63ce..916b8b873 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -18,6 +18,7 @@ import ( "fmt" "go/ast" gconstant "go/constant" + "go/parser" "go/token" "strings" @@ -180,7 +181,13 @@ func (p *ParseError) Error() string { return strings.Join(p.errs, "\n") } -func Compile(fs *token.FileSet, f *ast.File, vertexEntry, fragmentEntry string, textureCount int) (*shaderir.Program, error) { +func Compile(src []byte, vertexEntry, fragmentEntry string, textureCount int) (*shaderir.Program, error) { + fs := token.NewFileSet() + f, err := parser.ParseFile(fs, "", src, parser.AllErrors) + if err != nil { + return nil, err + } + s := &compileState{ fs: fs, vertexEntry: vertexEntry, diff --git a/internal/shader/shader_test.go b/internal/shader/shader_test.go index 1b5c44ea1..48810a5c2 100644 --- a/internal/shader/shader_test.go +++ b/internal/shader/shader_test.go @@ -16,8 +16,6 @@ package shader_test import ( "fmt" - "go/parser" - "go/token" "os" "path/filepath" "runtime" @@ -170,13 +168,7 @@ func TestCompile(t *testing.T) { for _, tc := range tests { t.Run(tc.Name, func(t *testing.T) { - fset := token.NewFileSet() - f, err := parser.ParseFile(fset, "", tc.Src, parser.AllErrors) - if err != nil { - t.Fatal(err) - } - - s, err := shader.Compile(fset, f, "Vertex", "Fragment", 0) + s, err := shader.Compile(tc.Src, "Vertex", "Fragment", 0) if err != nil { t.Error(err) return diff --git a/internal/shader/syntax_test.go b/internal/shader/syntax_test.go index 2f2b4cf56..37d31c5a9 100644 --- a/internal/shader/syntax_test.go +++ b/internal/shader/syntax_test.go @@ -16,8 +16,6 @@ package shader_test import ( "fmt" - "go/parser" - "go/token" "strings" "testing" @@ -26,18 +24,7 @@ import ( ) func compileToIR(src []byte) (*shaderir.Program, error) { - fset := token.NewFileSet() - f, err := parser.ParseFile(fset, "", src, parser.AllErrors) - if err != nil { - return nil, err - } - - ir, err := shader.Compile(fset, f, "Vertex", "Fragment", 0) - if err != nil { - return nil, err - } - - return ir, nil + return shader.Compile(src, "Vertex", "Fragment", 0) } func TestSyntaxShadowing(t *testing.T) { diff --git a/internal/shaderir/reachable_test.go b/internal/shaderir/reachable_test.go index 5a4aec2ed..8f85e1652 100644 --- a/internal/shaderir/reachable_test.go +++ b/internal/shaderir/reachable_test.go @@ -15,8 +15,6 @@ package shaderir_test import ( - "go/parser" - "go/token" "testing" "github.com/hajimehoshi/ebiten/v2/internal/shader" @@ -24,18 +22,7 @@ import ( ) func compileToIR(src []byte) (*shaderir.Program, error) { - fset := token.NewFileSet() - f, err := parser.ParseFile(fset, "", src, parser.AllErrors) - if err != nil { - return nil, err - } - - ir, err := shader.Compile(fset, f, "Vertex", "Fragment", 0) - if err != nil { - return nil, err - } - - return ir, nil + return shader.Compile(src, "Vertex", "Fragment", 0) } func areIntSlicesEqual(a, b []int) bool {