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 (
"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
}

View File

@ -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,

View File

@ -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

View File

@ -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) {

View File

@ -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 {