internal/shader: refactoring

This commit is contained in:
Hajime Hoshi 2023-04-16 18:35:12 +09:00
parent e6d79889f9
commit df32901dce
5 changed files with 12 additions and 47 deletions

View File

@ -17,8 +17,6 @@ package graphics
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"go/parser"
"go/token"
"github.com/hajimehoshi/ebiten/v2/internal/shader" "github.com/hajimehoshi/ebiten/v2/internal/shader"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
@ -114,17 +112,11 @@ func CompileShader(src []byte) (*shaderir.Program, error) {
buf.Write(src) buf.Write(src)
buf.WriteString(shaderSuffix) buf.WriteString(shaderSuffix)
fs := token.NewFileSet()
f, err := parser.ParseFile(fs, "", buf.Bytes(), parser.AllErrors)
if err != nil {
return nil, err
}
const ( const (
vert = "__vertex" vert = "__vertex"
frag = "Fragment" frag = "Fragment"
) )
ir, err := shader.Compile(fs, f, vert, frag, ShaderImageCount) ir, err := shader.Compile(buf.Bytes(), vert, frag, ShaderImageCount)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,6 +18,7 @@ import (
"fmt" "fmt"
"go/ast" "go/ast"
gconstant "go/constant" gconstant "go/constant"
"go/parser"
"go/token" "go/token"
"strings" "strings"
@ -180,7 +181,13 @@ func (p *ParseError) Error() string {
return strings.Join(p.errs, "\n") 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{ s := &compileState{
fs: fs, fs: fs,
vertexEntry: vertexEntry, vertexEntry: vertexEntry,

View File

@ -16,8 +16,6 @@ package shader_test
import ( import (
"fmt" "fmt"
"go/parser"
"go/token"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -170,13 +168,7 @@ func TestCompile(t *testing.T) {
for _, tc := range tests { for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
fset := token.NewFileSet() s, err := shader.Compile(tc.Src, "Vertex", "Fragment", 0)
f, err := parser.ParseFile(fset, "", tc.Src, parser.AllErrors)
if err != nil {
t.Fatal(err)
}
s, err := shader.Compile(fset, f, "Vertex", "Fragment", 0)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View File

@ -16,8 +16,6 @@ package shader_test
import ( import (
"fmt" "fmt"
"go/parser"
"go/token"
"strings" "strings"
"testing" "testing"
@ -26,18 +24,7 @@ import (
) )
func compileToIR(src []byte) (*shaderir.Program, error) { func compileToIR(src []byte) (*shaderir.Program, error) {
fset := token.NewFileSet() return shader.Compile(src, "Vertex", "Fragment", 0)
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
} }
func TestSyntaxShadowing(t *testing.T) { func TestSyntaxShadowing(t *testing.T) {

View File

@ -15,8 +15,6 @@
package shaderir_test package shaderir_test
import ( import (
"go/parser"
"go/token"
"testing" "testing"
"github.com/hajimehoshi/ebiten/v2/internal/shader" "github.com/hajimehoshi/ebiten/v2/internal/shader"
@ -24,18 +22,7 @@ import (
) )
func compileToIR(src []byte) (*shaderir.Program, error) { func compileToIR(src []byte) (*shaderir.Program, error) {
fset := token.NewFileSet() return shader.Compile(src, "Vertex", "Fragment", 0)
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
} }
func areIntSlicesEqual(a, b []int) bool { func areIntSlicesEqual(a, b []int) bool {